Add drag reordering, exam deadlines, frozen shared quizzes, batched OCR and semantic search
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 33s

- Drag-to-reorder for class materials, folders, quiz questions and deck cards. Pointer-based
  so touch works, plus keyboard (arrows/Home/End) with screen-reader announcements.
- Timed exams get a wall-clock deadline. The server refuses late answers, submits overdue
  exams with what was saved, and the countdown keeps running while the page is closed.
- Shared quiz sessions keep a frozen copy of their questions and flag later owner edits.
- Scanned PDFs are read in cached 8-page batches with real page numbers (pdf-lib), removing
  the 30 MB whole-file limit. Compatible providers now receive PDFs as file parts.
- Semantic search: embeddings for OpenAI, Gemini, Ollama and compatible servers, background
  indexing into chunk_vectors (migration 2), hybrid keyword + vector ranking for search, the
  assistant and focused generation, and a Settings -> AI section.

181 tests pass locally and in the Docker test stage on Node 24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-12 11:48:20 -07:00
parent 3046e46d1c
commit ccdad9728b
39 changed files with 1772 additions and 121 deletions

View file

@ -375,3 +375,56 @@ describe('assistant', () => {
expect(bogus.at(-1).type).toBe('done');
});
});
describe('reading long scanned PDFs', () => {
async function scannedPdf(pages: number) {
const { PDFDocument } = await import('pdf-lib');
const doc = await PDFDocument.create();
for (let i = 0; i < pages; i++) doc.addPage([612, 792]);
return doc.save();
}
const ocrCalls = () => mock.requests.filter((r) => r.path.endsWith('/chat/completions') && JSON.stringify(r.body?.messages ?? '').includes('Transcribe this study material'));
it('reads the PDF in page batches, keeps real page numbers and caches finished batches', async () => {
const { models } = await connectAi();
for (const m of models) await t.api('PATCH', `/api/ai/models/${m.id}`, { capabilities: { vision: true, pdf: true } });
const cls = (await t.api('POST', '/api/classes', { name: 'Med-Surg' })).body.class;
const up = await upload(cls.id, 'scanned-textbook.pdf', await scannedPdf(20), 'application/pdf');
expect(up.status).toBe(201);
const id = up.body.documents[0].id;
const doc = (await t.api('GET', `/api/documents/${id}`)).body.document;
expect(doc).toMatchObject({ status: 'ready', needsOcr: false, pageCount: 20 });
// 20 pages at 8 per batch: pages 1-8, 9-16, 17-20, each sent as its own PDF.
const calls = ocrCalls();
expect(calls).toHaveLength(3);
const prompts = calls.map((c) => JSON.stringify(c.body.messages));
expect(prompts[0]).toContain('pages 1-8 of a 20-page document');
expect(prompts[2]).toContain('pages 17-20 of a 20-page document');
expect(prompts[1]).toContain('data:application/pdf;base64,');
const chunks = (await t.api('GET', `/api/documents/${id}/chunks`)).body.chunks as { text: string; pageStart: number | null; pageEnd: number | null }[];
const text = chunks.map((c) => c.text).join('\n');
for (const n of [1, 8, 9, 20]) expect(text).toContain(`page ${n}:`);
expect(Math.min(...chunks.map((c) => c.pageStart ?? 99))).toBe(1);
expect(Math.max(...chunks.map((c) => c.pageEnd ?? 0))).toBe(20);
expect(fs.readdirSync(path.join(t.dir, 'uploads')).some((f) => f.endsWith('.ocr.json'))).toBe(true);
// Reprocessing without asking for a fresh read reuses the cached batches: no new AI calls.
mock.requests.length = 0;
await t.api('POST', `/api/documents/${id}/reprocess`, {});
await queueIdle(t.ctx);
expect(ocrCalls()).toHaveLength(0);
expect((await t.api('GET', `/api/documents/${id}`)).body.document.needsOcr).toBe(false);
// "Read with AI" asks for a fresh read and sends every batch again.
await t.api('POST', `/api/documents/${id}/reprocess`, { ocr: true });
await queueIdle(t.ctx);
expect(ocrCalls()).toHaveLength(3);
// Deleting the document removes the cache with the file.
await t.api('DELETE', `/api/documents/${id}`);
expect(fs.readdirSync(path.join(t.dir, 'uploads')).some((f) => f.endsWith('.ocr.json'))).toBe(false);
});
});

View file

@ -6,6 +6,7 @@
* - 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';
@ -21,6 +22,27 @@ export interface MockAi {
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;
@ -107,6 +129,11 @@ export function startMockAi(port = 0): Promise<MockAi> {
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 (fail.times > 0) {
fail.times -= 1;
@ -144,6 +171,17 @@ export function startMockAi(port = 0): Promise<MockAi> {
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') {

View file

@ -0,0 +1,104 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createTestApp, sampleQuestions, type TestApp } from './helpers.ts';
let t: TestApp;
beforeEach(async () => {
t = await createTestApp();
});
afterEach(() => {
vi.restoreAllMocks();
t.close();
});
async function makeClass(name = 'Pharmacology') {
return (await t.api('POST', '/api/classes', { name })).body.class as { id: string };
}
describe('manual ordering', () => {
it('stores a custom order per kind and exposes it on the class', async () => {
const cls = await makeClass();
const ids: string[] = [];
for (const title of ['A', 'B', 'C']) ids.push((await t.api('POST', '/api/quizzes', { classId: cls.id, title, questions: sampleQuestions })).body.quiz.id);
const other = await makeClass('Other');
const foreign = (await t.api('POST', '/api/quizzes', { classId: other.id, title: 'Foreign', questions: sampleQuestions })).body.quiz.id;
const res = await t.api('POST', `/api/classes/${cls.id}/items/reorder`, { kind: 'quiz', ids: [ids[2], foreign, ids[0], ids[1]] });
expect(res.status).toBe(200);
const items = (await t.api('GET', `/api/classes/${cls.id}`)).body.items as { id: string; kind: string; title: string; sortOrder: number }[];
const order = items.filter((i) => i.kind === 'quiz').sort((a, b) => a.sortOrder - b.sortOrder).map((i) => i.title);
expect(order).toEqual(['C', 'A', 'B']);
// An id from another class is ignored, not moved.
const otherItems = (await t.api('GET', `/api/classes/${other.id}`)).body.items as { id: string; sortOrder: number }[];
expect(otherItems.find((i) => i.id === foreign)?.sortOrder).toBe(0);
});
it('rejects unknown kinds', async () => {
const cls = await makeClass();
expect((await t.api('POST', `/api/classes/${cls.id}/items/reorder`, { kind: 'class', ids: [] })).status).toBe(422);
});
});
describe('timed exam deadlines', () => {
async function startExam(minutes: number) {
const cls = await makeClass();
const quiz = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Timed', questions: sampleQuestions })).body.quiz;
const res = await t.api('POST', '/api/attempts', { mode: 'exam', source: { type: 'quiz', quizId: quiz.id }, timeLimitMin: minutes, shuffleQuestions: false, shuffleOptions: false });
expect(res.status).toBe(201);
return { attempt: res.body.attempt, quiz };
}
it('fixes a wall-clock deadline when the exam starts', async () => {
const before = Date.now();
const { attempt } = await startExam(10);
expect(attempt.settings.deadlineAt).toBeGreaterThanOrEqual(before + 600_000);
expect(attempt.settings.deadlineAt).toBeLessThanOrEqual(Date.now() + 600_000);
expect(typeof attempt.serverNow).toBe('number');
});
it('accepts answers before the deadline and refuses them after, submitting what was saved', async () => {
const { attempt } = await startExam(1);
const q = attempt.questions[0];
const right = q.options.find((o: { correct: boolean }) => o.correct).id;
expect((await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q.id]: { selected: [right] } } })).status).toBe(200);
const start = attempt.settings.deadlineAt - 60_000;
vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 30_000);
const q2 = attempt.questions[1];
const late = await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q2.id]: { selected: [q2.options[0].id] } } });
expect(late.status).toBe(409);
expect(late.body.error.code).toBe('time_up');
const after = (await t.api('GET', `/api/attempts/${attempt.id}`)).body.attempt;
expect(after.status).toBe('completed');
expect(after.answers[q2.id]).toBeUndefined();
expect(after.score).toBe(1);
// Time used is capped at the limit even though the clock ran on.
expect(after.elapsedSec).toBe(60);
expect(after.startedAt).toBe(start);
});
it('allows a short grace period for saves in flight at the deadline', async () => {
const { attempt } = await startExam(1);
const q = attempt.questions[0];
vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 2_000);
expect((await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q.id]: { selected: [q.options[0].id] } } })).status).toBe(200);
});
it('submits overdue exams before lists are read, so they are not offered for resuming', async () => {
const { attempt, quiz } = await startExam(1);
vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 60_000);
const active = (await t.api('GET', '/api/attempts?status=active')).body.attempts;
expect(active).toHaveLength(0);
const cls = (await t.api('GET', `/api/classes/${quiz.classId}`)).body;
expect(cls.items.find((i: { id: string }) => i.id === quiz.id).activeAttemptId).toBeNull();
});
it('leaves untimed sessions alone', async () => {
const cls = await makeClass();
const quiz = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Untimed', questions: sampleQuestions })).body.quiz;
const attempt = (await t.api('POST', '/api/attempts', { mode: 'exam', source: { type: 'quiz', quizId: quiz.id } })).body.attempt;
expect(attempt.settings.deadlineAt).toBeNull();
vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 86_400_000);
expect((await t.api('GET', `/api/attempts/${attempt.id}`)).body.attempt.status).toBe('active');
});
});

120
tests/semantic.test.ts Normal file
View file

@ -0,0 +1,120 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { queueIdle } from '../server/services/documents.ts';
import { hybridSearch, indexingIdle } from '../server/services/semantic.ts';
import { createTestApp, type TestApp } from './helpers.ts';
import { startMockAi, type MockAi } from './mockAi.ts';
let mock: MockAi;
let t: TestApp;
beforeAll(async () => {
mock = await startMockAi();
});
afterAll(() => mock.close());
beforeEach(async () => {
t = await createTestApp();
mock.requests.length = 0;
});
afterEach(async () => {
await indexingIdle();
t.close();
});
const DIURETICS = `# Loop diuretics
Furosemide acts on the ascending limb of the loop of Henle. Watch for hypokalemia: encourage potassium-rich foods and check serum levels before each dose.`;
const DIGOXIN = `# Cardiac glycosides
Digoxin strengthens contraction and slows the heart rate. Toxicity shows as nausea, vomiting and yellow-green halos around lights. Take the apical pulse for one minute first.`;
async function setup() {
const provider = (await t.api('POST', '/api/ai/providers', { name: 'Mock', kind: 'openai_compatible', baseUrl: mock.url, apiKey: 'mock-key' })).body.provider;
const cls = (await t.api('POST', '/api/classes', { name: 'Pharmacology' })).body.class;
for (const [title, text] of [
['Diuretics lecture', DIURETICS],
['Cardiac glycosides lecture', DIGOXIN],
]) {
expect((await t.api('POST', '/api/documents/text', { classId: cls.id, title, text })).status).toBe(201);
}
await queueIdle(t.ctx);
return { provider, cls };
}
async function enable(providerId: string, model = 'mock-embedding-1') {
const res = await t.api('PUT', '/api/ai/embeddings', { providerId, model, dimensions: null });
expect(res.status).toBe(200);
await indexingIdle();
return (await t.api('GET', '/api/ai/embeddings')).body;
}
describe('semantic search', () => {
it('is off by default and search falls back to keywords', async () => {
const { cls } = await setup();
const status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status).toMatchObject({ configured: false, indexed: 0 });
expect(status.total).toBeGreaterThan(0);
const hits = await hybridSearch(t.ctx, 'digoxin', { classId: cls.id }, 3);
expect(hits[0].documentTitle).toBe('Cardiac glycosides lecture');
expect(mock.requests.filter((r) => r.path.endsWith('/embeddings'))).toHaveLength(0);
});
it('indexes every passage once and finds paraphrased questions that share no keywords', async () => {
const { provider, cls } = await setup();
const status = await enable(provider.id);
expect(status).toMatchObject({ configured: true, running: false, lastError: '' });
expect(status.indexed).toBe(status.total);
// No word of this query appears in the passage, so keyword search alone finds nothing.
const query = 'why should lasix patients eat bananas';
const search = (await t.api('GET', `/api/search?q=${encodeURIComponent(query)}&classId=${cls.id}`)).body.results as { kind: string; title: string; semantic?: boolean }[];
const passages = search.filter((r) => r.kind === 'passage');
expect(passages[0]).toMatchObject({ title: 'Diuretics lecture', semantic: true });
const hits = await hybridSearch(t.ctx, query, { classId: cls.id }, 2);
expect(hits[0].documentTitle).toBe('Diuretics lecture');
// Usage is logged, and a second index run embeds nothing new.
const usage = t.ctx.db.all<{ task: string }>("SELECT task FROM ai_usage WHERE task LIKE 'semantic%'");
expect(usage.some((u) => u.task === 'semantic indexing')).toBe(true);
const before = mock.requests.length;
await t.api('POST', '/api/ai/embeddings/resume');
await indexingIdle();
expect(mock.requests.length).toBe(before);
});
it('indexes documents added later and drops vectors when the model changes', async () => {
const { provider, cls } = await setup();
await enable(provider.id);
await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Insulin notes', text: '# Insulin\n\nRapid-acting insulin starts within 15 minutes; give it with a meal.' });
await queueIdle(t.ctx);
await indexingIdle();
let status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status.indexed).toBe(status.total);
status = await enable(provider.id, 'mock-embedding-2');
expect(status.indexed).toBe(status.total);
const keys = t.ctx.db.all<{ model_key: string }>('SELECT DISTINCT model_key FROM chunk_vectors');
expect(keys).toHaveLength(1);
expect(keys[0].model_key).toContain('mock-embedding-2');
});
it('refuses providers without an embeddings API and clears settings when the provider is deleted', async () => {
const { provider } = await setup();
const claude = (await t.api('POST', '/api/ai/providers', { name: 'Claude', kind: 'anthropic', apiKey: 'sk-ant-test' })).body.provider;
const refused = await t.api('PUT', '/api/ai/embeddings', { providerId: claude.id, model: 'anything', dimensions: null });
expect(refused.status).toBe(400);
await enable(provider.id);
await t.api('DELETE', `/api/ai/providers/${provider.id}`);
const status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status).toMatchObject({ configured: false, indexed: 0 });
});
it('keeps working on keywords when the embedding provider fails', async () => {
const { provider, cls } = await setup();
await enable(provider.id);
await t.api('PATCH', `/api/ai/providers/${provider.id}`, { apiKey: 'wrong-key' });
const hits = await hybridSearch(t.ctx, 'apical pulse before digoxin', { classId: cls.id }, 2);
expect(hits[0].documentTitle).toBe('Cardiac glycosides lecture');
});
});