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
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:
parent
3046e46d1c
commit
ccdad9728b
39 changed files with 1772 additions and 121 deletions
|
|
@ -37,6 +37,7 @@ import {
|
|||
testProvider,
|
||||
usageSummary,
|
||||
} from '../ai/service.ts';
|
||||
import { embedTexts, embeddingSettingsSchema, getEmbeddingSettings, embeddingStatus, rebuildIndex, saveEmbeddingSettings, scheduleIndexing } from '../services/semantic.ts';
|
||||
import { AIError } from '../ai/types.ts';
|
||||
import { badRequest, idParam, notFound, readBody, type AppContext } from '../http.ts';
|
||||
|
||||
|
|
@ -108,7 +109,10 @@ export function aiRoutes(app: AppContext): Hono {
|
|||
r.post('/ai/providers', async (c) => c.json({ provider: saveProvider(app, null, await readBody(c, providerInputSchema)) }, 201));
|
||||
r.patch('/ai/providers/:id', async (c) => c.json({ provider: saveProvider(app, idParam(c), await readBody(c, providerInputSchema)) }));
|
||||
r.delete('/ai/providers/:id', (c) => {
|
||||
deleteProvider(app, idParam(c));
|
||||
const id = idParam(c);
|
||||
deleteProvider(app, id);
|
||||
// Semantic search cannot keep using a provider that no longer exists.
|
||||
if (getEmbeddingSettings(db).providerId === id) saveEmbeddingSettings(app, { providerId: null, model: '', dimensions: null });
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
r.post('/ai/providers/:id/test', async (c) => c.json({ result: await testProvider(app, idParam(c)) }));
|
||||
|
|
@ -267,5 +271,27 @@ export function aiRoutes(app: AppContext): Hono {
|
|||
});
|
||||
});
|
||||
|
||||
/* ------------------------------ semantic search ------------------------------ */
|
||||
|
||||
r.get('/ai/embeddings', (c) => c.json(embeddingStatus(app)));
|
||||
r.put('/ai/embeddings', async (c) => {
|
||||
saveEmbeddingSettings(app, await readBody(c, embeddingSettingsSchema));
|
||||
return c.json(embeddingStatus(app));
|
||||
});
|
||||
/** Embeds one sentence to prove the model works before indexing everything. */
|
||||
r.post('/ai/embeddings/test', async (c) => {
|
||||
const started = Date.now();
|
||||
const [v] = await embedTexts(app, ['Furosemide is a loop diuretic.'], 'query');
|
||||
return c.json({ ok: true, dimensions: v.length, latencyMs: Date.now() - started });
|
||||
});
|
||||
r.post('/ai/embeddings/rebuild', (c) => {
|
||||
rebuildIndex(app);
|
||||
return c.json(embeddingStatus(app));
|
||||
});
|
||||
r.post('/ai/embeddings/resume', (c) => {
|
||||
scheduleIndexing(app);
|
||||
return c.json(embeddingStatus(app));
|
||||
});
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
);
|
||||
const decks = db.all<DeckRow & { cn: number }>(`SELECT d.*, (SELECT COUNT(*) FROM cards c WHERE c.deck_id = d.id) AS cn FROM decks d WHERE d.class_id = ?`, cls.id);
|
||||
const documents = db.all<Record<string, any>>(
|
||||
'SELECT id, folder_id, title, filename, kind, size, status, error, progress, page_count, token_count, needs_ocr, created_at, updated_at FROM documents WHERE class_id = ?',
|
||||
'SELECT id, folder_id, title, filename, kind, size, status, error, progress, page_count, token_count, needs_ocr, sort_order, created_at, updated_at FROM documents WHERE class_id = ?',
|
||||
cls.id,
|
||||
);
|
||||
const notes = db.all<NoteRow>('SELECT * FROM notes WHERE class_id = ?', cls.id);
|
||||
|
|
@ -128,6 +128,7 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
title: q.title,
|
||||
description: q.description,
|
||||
folderId: q.folder_id,
|
||||
sortOrder: q.sort_order,
|
||||
createdAt: q.created_at,
|
||||
updatedAt: q.updated_at,
|
||||
shared: shared('quiz', q.id),
|
||||
|
|
@ -145,6 +146,7 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
title: d.title,
|
||||
description: d.description,
|
||||
folderId: d.folder_id,
|
||||
sortOrder: d.sort_order,
|
||||
createdAt: d.created_at,
|
||||
updatedAt: d.updated_at,
|
||||
shared: shared('deck', d.id),
|
||||
|
|
@ -160,6 +162,7 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
title: d.title as string,
|
||||
description: '',
|
||||
folderId: d.folder_id as string | null,
|
||||
sortOrder: d.sort_order as number,
|
||||
createdAt: d.created_at as number,
|
||||
updatedAt: d.updated_at as number,
|
||||
shared: shared('document', d.id),
|
||||
|
|
@ -179,6 +182,7 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
title: n.title,
|
||||
description: n.content.replace(/[#*_>`|-]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 160),
|
||||
folderId: n.folder_id,
|
||||
sortOrder: n.sort_order,
|
||||
createdAt: n.created_at,
|
||||
updatedAt: n.updated_at,
|
||||
shared: shared('note', n.id),
|
||||
|
|
@ -278,6 +282,15 @@ export function libraryRoutes(app: AppContext): Hono {
|
|||
|
||||
const TABLES = { quiz: 'quizzes', deck: 'decks', note: 'notes', document: 'documents' } as const;
|
||||
|
||||
/** Manual order for one kind of material in a class. Ids from other classes are ignored. */
|
||||
r.post('/classes/:id/items/reorder', async (c) => {
|
||||
const cls = getClass(db, idParam(c));
|
||||
const body = await readBody(c, z.object({ kind: z.enum(['quiz', 'deck', 'note', 'document']), ids: z.array(z.string()).max(2000) }));
|
||||
const table = TABLES[body.kind];
|
||||
db.tx(() => body.ids.forEach((id, i) => db.run(`UPDATE ${table} SET sort_order = ? WHERE id = ? AND class_id = ?`, i, id, cls.id)));
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
|
||||
r.post('/items/move', async (c) => {
|
||||
const body = await readBody(
|
||||
c,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { Hono } from 'hono';
|
||||
import { getEmbeddingSettings, semanticSearch } from '../services/semantic.ts';
|
||||
import { z } from 'zod';
|
||||
import { badRequest, idParam, readBody, type AppContext } from '../http.ts';
|
||||
import { getAppSettings } from '../settings.ts';
|
||||
|
|
@ -125,14 +126,14 @@ export function studyRoutes(app: AppContext): Hono {
|
|||
|
||||
/* ------------------------------ search ------------------------------ */
|
||||
|
||||
r.get('/search', (c) => {
|
||||
r.get('/search', async (c) => {
|
||||
const q = (c.req.query('q') ?? '').trim().slice(0, 120);
|
||||
if (q.length < 2) return c.json({ query: q, results: [] });
|
||||
const like = `%${q.replace(/[\\%_]/g, (m) => `\\${m}`)}%`;
|
||||
const classId = c.req.query('classId') || null;
|
||||
const scoped = (col: string) => (classId ? `AND ${col} = ?` : '');
|
||||
const p = (...base: string[]) => (classId ? [...base, classId] : base);
|
||||
type Hit = { kind: string; id: string; title: string; snippet: string; classId: string; className: string; classColor: string; parentId?: string; page?: number | null };
|
||||
type Hit = { kind: string; id: string; title: string; snippet: string; classId: string; className: string; classColor: string; parentId?: string; page?: number | null; semantic?: boolean };
|
||||
const results: Hit[] = [];
|
||||
|
||||
for (const row of db.all<Record<string, string>>(`SELECT id, name, color FROM classes WHERE name LIKE ? ESCAPE '\\' OR code LIKE ? ESCAPE '\\' LIMIT 5`, like, like)) {
|
||||
|
|
@ -184,6 +185,25 @@ export function studyRoutes(app: AppContext): Hono {
|
|||
/* a malformed FTS query should never break search */
|
||||
}
|
||||
}
|
||||
// Passages that match the meaning rather than the words (when semantic search is set up).
|
||||
// Only for phrases or longer terms: single short words are already well served by keywords.
|
||||
if ((/\s/.test(q) || q.length >= 8) && getEmbeddingSettings(db).providerId) {
|
||||
try {
|
||||
const seen = new Set(results.filter((r) => r.kind === 'passage').map((r) => r.id));
|
||||
for (const hit of await semanticSearch(app, q, { classId }, 6, 0.35)) {
|
||||
if (seen.has(String(hit.id))) continue;
|
||||
const doc = db.get<Record<string, string>>(
|
||||
'SELECT k.id AS class_id, k.name AS class_name, k.color AS class_color FROM documents d JOIN classes k ON k.id = d.class_id WHERE d.id = ?',
|
||||
hit.documentId,
|
||||
);
|
||||
if (!doc) continue;
|
||||
const text = hit.text.replace(/\s+/g, ' ').trim();
|
||||
results.push({ kind: 'passage', id: String(hit.id), parentId: hit.documentId, page: hit.pageStart, title: hit.documentTitle, snippet: text.length > 160 ? `${text.slice(0, 160)} …` : text, classId: doc.class_id, className: doc.class_name, classColor: doc.class_color, semantic: true });
|
||||
}
|
||||
} catch (err) {
|
||||
app.log('warn', `Semantic search failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
return c.json({ query: q, results });
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue