Study/tests/mockAi.ts
Elijah f0aabe4d5d
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m2s
First revision: DeepSeek, numeric questions, background assistant, UI fixes
Providers and pricing
- Add a DeepSeek provider: thinking mode with low/high/max effort, reasoning_content
  replayed between tool steps, cache-hit usage, /models discovery and catalog prices.
- Fix "does not support tool calling": a rejected forced tool_choice (DeepSeek thinking
  mode) now asks for the call in the prompt; only a genuine no-tools 400 is "unsupported".
- Seven reasoning-effort levels (Off to Max) mapped per adapter.
- Cached-input price per model, cache-write and DeepSeek off-peak aware cost estimates.
- Built-in bge-small embedding model (transformers.js) as the semantic search default.

Quizzes
- Numeric (type-the-answer) questions: units ignored, optional ± tolerance; editor,
  runner, importer, templates and generation prompts.
- Quiz start screen instead of showing answers; navigator ring no longer clipped and
  key legend removed; clock only for timed exams; 7-point letter grades.

Assistant and generation
- Assistant turns run on the server independently of the browser, with re-attach and
  an explicit stop; drafts tray shows generations in progress.
- Per-chat model switcher, @-tagging of class materials, full-width assistant page and
  a smaller assistant button.
- Editable generation instructions and built-in presets, separate from the chatbot
  import instructions.

Other
- Dialog footer padding, keyboard hints removed, copy buttons work over plain HTTP,
  class and folder shares include documents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:48:17 -07:00

226 lines
12 KiB
TypeScript

/**
* A small OpenAI-compatible server with scripted behaviour, for tests and for trying the
* AI features locally without a real provider (see scripts/mock-ai.mjs).
*
* - Forced tool call -> returns that tool call with plausible arguments.
* - Tools + user turn -> picks a tool from keywords in the message.
* - Tool result turn -> answers in text, citing the first [cN] ref it was given.
* - No tools -> streams a short Markdown answer.
* - /embeddings -> deterministic word-hash vectors (see mockEmbedding).
*/
import http from 'node:http';
import type { AddressInfo } from 'node:net';
export interface MockAi {
url: string;
requests: { path: string; headers: http.IncomingHttpHeaders; body: any }[];
/** Force the next N chat calls to fail with this status. */
failNext: (status: number, times?: number) => void;
/** Delay every chat call by this many milliseconds (0 turns it off). */
delayChat: (ms: number) => void;
close: () => Promise<void>;
}
const text = (content: unknown): string =>
typeof content === 'string' ? content : Array.isArray(content) ? content.map((p: any) => (typeof p?.text === 'string' ? p.text : '')).join('\n') : '';
/**
* Deterministic "embeddings": hashed bag of words over 64 dimensions, with a few nursing
* synonyms folded together so a paraphrased query lands near the right passage.
*/
const SYNONYMS: Record<string, string> = {
lasix: 'furosemide', loop: 'furosemide', diuretic: 'furosemide', diuretics: 'furosemide',
banana: 'potassium', bananas: 'potassium', hypokalemia: 'potassium', k: 'potassium',
halos: 'digoxin', lanoxin: 'digoxin', glycoside: 'digoxin',
};
export function mockEmbedding(input: string, dims = 64): number[] {
const v = new Array(dims).fill(0);
for (const raw of input.toLowerCase().match(/[a-z]+/g) ?? []) {
if (raw.length < 3 && raw !== 'k') continue;
const word = SYNONYMS[raw] ?? raw;
let h = 2166136261;
for (const ch of word) h = Math.imul(h ^ ch.charCodeAt(0), 16777619) >>> 0;
v[h % dims] += 1;
}
return v;
}
function questions(n: number, refs: string[], offset = 0) {
return Array.from({ length: n }, (_, k) => {
const i = k + offset;
const multi = i % 3 === 2;
return {
type: multi ? 'multi' : 'single',
stem: `Mock question ${i + 1}: which action is the priority for the client described in case ${i + 1}?`,
options: [
{ text: `Correct action ${i + 1}`, correct: true, why: 'This addresses the immediate risk.' },
{ text: `Plausible distractor ${i + 1}a`, correct: multi, why: multi ? 'Also indicated.' : 'Lower priority.' },
{ text: `Plausible distractor ${i + 1}b`, correct: false, why: 'Not indicated.' },
{ text: `Plausible distractor ${i + 1}c`, correct: false, why: 'Contraindicated.' },
],
rationale: `Rationale for question ${i + 1}.`,
topic: i % 2 === 0 ? 'Heart failure' : 'Pharmacology',
difficulty: 'medium',
source: refs.length ? refs[i % refs.length] : '',
};
});
}
function toolArgs(name: string, userText: string, all: string): Record<string, unknown> {
const count = Number(/(?:Write|write)\s+(\d+)/.exec(userText)?.[1] ?? /(\d+)[- ](?:question|card|flashcard)/.exec(userText)?.[1] ?? 3);
// Only refs from the user's turn count: the system prompt contains an example id.
const refs = [...new Set([...userText.matchAll(/\[(c\d+)\]/g)].map((m) => m[1]))];
// Continue numbering after anything already written, so later batches are not duplicates.
const offset = (userText.split('Already written')[1]?.match(/^- /gm) ?? []).length;
void all;
switch (name) {
case 'submit_questions':
return { questions: questions(count, refs, offset) };
case 'submit_flashcards':
return { cards: Array.from({ length: count }, (_, i) => ({ front: `Mock term ${i + 1 + offset}`, back: `Mock definition ${i + 1 + offset}`, hint: '', tags: ['mock'] })) };
case 'generate_quiz':
return { count: Math.min(count, 5), topic: 'heart failure', style: 'nclex', use_all_class_documents: /document/i.test(userText), from_weak_areas: /wrong|weak|miss/i.test(userText) };
case 'generate_flashcards':
return { count: Math.min(count, 6), topic: 'medications', use_all_class_documents: /document/i.test(userText) };
case 'search_documents':
return { query: userText.replace(/[^\w\s]/g, ' ').split(/\s+/).filter((w) => w.length > 3).slice(0, 6).join(' ') };
case 'get_performance':
case 'list_classes':
case 'list_materials':
return {};
case 'propose_changes': {
const m = /(delete|rename)\s+(quiz|deck|note|document)\s+([\w-]{8,})/i.exec(userText);
return { reason: 'Requested by the user.', changes: [{ op: m?.[1]?.toLowerCase() ?? 'delete', kind: m?.[2]?.toLowerCase() ?? 'quiz', id: m?.[3] ?? 'missing', title: 'Renamed by mock' }] };
}
default:
return {};
}
}
function chooseTool(userText: string, names: string[]): string | null {
const pick = (name: string) => (names.includes(name) ? name : null);
if (/\b(delete|rename)\b/i.test(userText)) return pick('propose_changes');
if (/flashcard/i.test(userText)) return pick('generate_flashcards');
if (/\bquiz\b|practice questions/i.test(userText)) return pick('generate_quiz');
if (/should i study|performance|how am i doing/i.test(userText)) return pick('get_performance');
if (/\?|what|explain|summar/i.test(userText)) return pick('search_documents');
return null;
}
export function startMockAi(port = 0): Promise<MockAi> {
const requests: MockAi['requests'] = [];
let fail = { status: 0, times: 0 };
let delayMs = 0;
const server = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on('data', (c) => chunks.push(c));
req.on('end', async () => {
const raw = Buffer.concat(chunks).toString('utf8');
let body: any = null;
try {
body = raw ? JSON.parse(raw) : null;
} catch {
/* ignore */
}
const path = req.url ?? '';
requests.push({ path, headers: req.headers, body });
const json = (status: number, data: unknown) => {
res.writeHead(status, { 'content-type': 'application/json' });
res.end(JSON.stringify(data));
};
if (req.headers.authorization !== 'Bearer mock-key') return json(401, { error: { message: 'Incorrect API key provided: ' + String(req.headers.authorization), type: 'invalid_request_error' } });
if (path.endsWith('/models')) return json(200, { object: 'list', data: [{ id: 'mock-large' }, { id: 'mock-small' }, { id: 'mock-embedding-1' }] });
if (path.endsWith('/embeddings')) {
const inputs: string[] = Array.isArray(body?.input) ? body.input : [String(body?.input ?? '')];
const dims = Number(body?.dimensions) || 64;
return json(200, { object: 'list', model: body?.model, data: inputs.map((t, index) => ({ object: 'embedding', index, embedding: mockEmbedding(t, dims) })), usage: { prompt_tokens: Math.ceil(inputs.join(' ').length / 4), total_tokens: Math.ceil(inputs.join(' ').length / 4) } });
}
if (!path.endsWith('/chat/completions')) return json(404, { error: { message: 'not found' } });
if (delayMs > 0) await new Promise((r) => setTimeout(r, delayMs));
if (fail.times > 0) {
fail.times -= 1;
const kind = fail.status === 401 ? ['Incorrect API key provided', 'invalid_request_error'] : fail.status === 429 ? ['Rate limit reached', 'rate_limit_error'] : ['The server had an error', 'server_error'];
return json(fail.status, { error: { message: kind[0], type: kind[1] } });
}
const messages: any[] = body?.messages ?? [];
const tools: any[] = body?.tools ?? [];
const names = tools.map((t) => t.function?.name);
const last = messages[messages.length - 1] ?? {};
const lastUser = [...messages].reverse().find((m) => m.role === 'user');
const userText = text(lastUser?.content);
const all = messages.map((m) => text(m.content)).join('\n');
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' });
const send = (delta: Record<string, unknown>, finish: string | null = null) =>
res.write(`data: ${JSON.stringify({ id: 'mock', object: 'chat.completion.chunk', model: body?.model, choices: [{ index: 0, delta, finish_reason: finish }] })}\n\n`);
const end = (finish: string, outTokens: number) => {
send({}, finish);
res.write(`data: ${JSON.stringify({ id: 'mock', object: 'chat.completion.chunk', choices: [], usage: { prompt_tokens: Math.ceil(raw.length / 4), completion_tokens: outTokens } })}\n\n`);
res.write('data: [DONE]\n\n');
res.end();
};
const callTool = (name: string) => {
const args = JSON.stringify(toolArgs(name, userText, all));
send({ role: 'assistant', content: null, tool_calls: [{ index: 0, id: `call_${Math.random().toString(36).slice(2, 10)}`, type: 'function', function: { name, arguments: '' } }] });
// Arguments arrive in fragments, like a real provider.
for (let i = 0; i < args.length; i += 37) send({ tool_calls: [{ index: 0, function: { arguments: args.slice(i, i + 37) } }] });
end('tool_calls', Math.ceil(args.length / 4));
};
const say = (content: string) => {
send({ role: 'assistant', content: '' });
for (const word of content.match(/\S+\s*/g) ?? []) send({ content: word });
end('stop', Math.ceil(content.length / 4));
};
// Document reading: answer with one "## Page N" section per page of the attached batch.
if (/Transcribe this study material/.test(userText)) {
const file = (Array.isArray(lastUser?.content) ? lastUser.content : []).find((p: any) => p?.type === 'file');
if (!file?.file?.file_data?.startsWith('data:application/pdf;base64,')) return say('[no readable file attached]');
const range = /contains pages? (\d+)(?:-(\d+))? of a/i.exec(userText);
const from = Number(range?.[1] ?? 1);
const to = Number(range?.[2] ?? range?.[1] ?? 1);
const pages = Array.from({ length: to - from + 1 }, (_, k) => `## Page ${from + k}\n\nScanned lecture text for page ${from + k}: nursing priority ${from + k}.`);
return say(pages.join('\n\n'));
}
const forced = body?.tool_choice?.function?.name as string | undefined;
if (forced) return callTool(forced);
if (last.role === 'tool') {
const ref = /"ref":"(c\d+)"/.exec(text(last.content))?.[1];
if (/draft_ready|revisions_ready/.test(text(last.content))) return say('The draft is ready. Review it and save it when you are happy with it.');
if (/awaiting_user_confirmation/.test(text(last.content))) return say('I have prepared the change. Press **Confirm** to apply it.');
return say(ref ? `Based on your documents, loop diuretics relieve congestion and need potassium monitoring [${ref}].` : 'Here is what I found in your library.');
}
if (names.length > 0 && body?.tool_choice !== 'none') {
const tool = chooseTool(userText, names);
if (tool) return callTool(tool);
}
if (/JSON Schema/.test(text(messages[0]?.content))) {
// Prompt-JSON fallback for models flagged as not supporting tools.
return say(JSON.stringify(/cards/.test(text(messages[0]?.content)) ? toolArgs('submit_flashcards', userText, all) : toolArgs('submit_questions', userText, all)));
}
const ref = /\[(c\d+)\]/.exec(userText)?.[1];
return say(`## Overview\n\nThis is a mock study guide.\n\n- Key point one${ref ? ` [${ref}]` : ''}\n- Key point two\n\n## Check yourself\n\n1. What is the priority?`);
});
});
return new Promise((resolve) => {
server.listen(port, '127.0.0.1', () => {
const { port: actual } = server.address() as AddressInfo;
resolve({
url: `http://127.0.0.1:${actual}/v1`,
requests,
failNext: (status, times = 1) => {
fail = { status, times };
},
delayChat: (ms) => {
delayMs = ms;
},
close: () => new Promise((done) => server.close(() => done())),
});
});
});
}