/** * 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. */ 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; close: () => Promise; } const text = (content: unknown): string => typeof content === 'string' ? content : Array.isArray(content) ? content.map((p: any) => (typeof p?.text === 'string' ? p.text : '')).join('\n') : ''; 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 { 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 { const requests: MockAi['requests'] = []; let fail = { status: 0, times: 0 }; const server = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on('data', (c) => chunks.push(c)); req.on('end', () => { 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('/chat/completions')) return json(404, { error: { message: 'not found' } }); 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, 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)); }; 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 }; }, close: () => new Promise((done) => server.close(() => done())), }); }); }); }