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

@ -4,6 +4,7 @@ import { useEffect, useMemo, useState } from 'react';
import { toast } from 'sonner';
import { api, errorMessage } from '@/lib/api';
import { qk } from '@/lib/queries';
import { SemanticSettings } from './SemanticSettings';
import type { AdapterMeta, AiModel, AiStatus, Provider, ProviderKind, TaskConfig, TaskName, TaskSettings } from '@/lib/types';
import { cx, formatCost, formatTokens, plural, timeAgo } from '@/lib/util';
import { Badge, Button, Callout, Dialog, EmptyState, Field, IconButton, Input, Menu, MenuItem, MenuSeparator, Section, Select, Skeleton, Switch, Textarea, Tooltip, useConfirm } from '@/ui';
@ -698,6 +699,8 @@ export default function AiSettings() {
</Section>
)}
{providers.data.length > 0 && <SemanticSettings providers={providers.data} />}
{providers.data.length > 0 && <Usage />}
<ProviderDialog open={dialog.open} onClose={() => setDialog((d) => ({ ...d, open: false }))} adapters={meta.data.adapters} existing={dialog.existing} onSaved={afterSave} />

View file

@ -0,0 +1,180 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { CircleAlert, RefreshCw, Sparkles, Zap } from 'lucide-react';
import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import { api, errorMessage } from '@/lib/api';
import type { Provider, ProviderKind } from '@/lib/types';
import { plural } from '@/lib/util';
import { Badge, Button, Callout, Field, Input, ProgressBar, Section, Select, useConfirm } from '@/ui';
interface EmbeddingStatus {
settings: { providerId: string | null; model: string; dimensions: number | null };
configured: boolean;
indexed: number;
total: number;
running: boolean;
lastError: string;
defaults: Record<ProviderKind, string | null>;
}
const KEY = ['ai', 'embeddings'];
/**
* Semantic search settings: which provider and embedding model turn document passages into
* vectors, and how far indexing has got.
*/
export function SemanticSettings({ providers }: { providers: Provider[] }) {
const qc = useQueryClient();
const confirm = useConfirm();
const q = useQuery({
queryKey: KEY,
queryFn: () => api.get<EmbeddingStatus>('/api/ai/embeddings'),
// Poll while indexing so the progress bar moves.
refetchInterval: (query) => (query.state.data?.running ? 1500 : false),
});
const data = q.data;
const eligible = providers.filter((p) => p.kind !== 'anthropic' && p.enabled);
const [providerId, setProviderId] = useState('');
const [model, setModel] = useState('');
const [dimensions, setDimensions] = useState('');
const [busy, setBusy] = useState<'save' | 'test' | 'rebuild' | null>(null);
useEffect(() => {
if (!data) return;
setProviderId(data.settings.providerId ?? '');
setModel(data.settings.model);
setDimensions(data.settings.dimensions ? String(data.settings.dimensions) : '');
}, [data]);
if (!data) return null;
const chosen = providers.find((p) => p.id === providerId);
const suggested = chosen ? data.defaults[chosen.kind] : null;
const dirty = providerId !== (data.settings.providerId ?? '') || model.trim() !== data.settings.model || dimensions !== (data.settings.dimensions ? String(data.settings.dimensions) : '');
const reload = () => qc.invalidateQueries({ queryKey: KEY });
const save = async () => {
if (data.configured && data.indexed > 0 && providerId && (providerId !== data.settings.providerId || model.trim() !== data.settings.model)) {
const ok = await confirm({ title: 'Switch embedding model?', message: `Vectors from different models cannot be mixed, so all ${plural(data.indexed, 'passage')} will be indexed again with the new model.`, confirmLabel: 'Switch and re-index' });
if (!ok) return;
}
setBusy('save');
try {
await api.put('/api/ai/embeddings', { providerId: providerId || null, model: providerId ? model.trim() : '', dimensions: dimensions ? Number(dimensions) : null });
toast.success(providerId ? 'Semantic search is on. Indexing your documents…' : 'Semantic search is off');
await reload();
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(null);
}
};
const test = async () => {
setBusy('test');
try {
const r = await api.post<{ dimensions: number; latencyMs: number }>('/api/ai/embeddings/test');
toast.success(`Working: ${r.dimensions}-dimension vectors in ${r.latencyMs} ms`);
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(null);
}
};
const act = async (path: 'rebuild' | 'resume') => {
if (path === 'rebuild' && !(await confirm({ title: 'Rebuild the index?', message: 'Every passage is embedded again. This repeats the embedding cost.', confirmLabel: 'Rebuild' }))) return;
setBusy('rebuild');
try {
await api.post(`/api/ai/embeddings/${path}`);
await reload();
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(null);
}
};
const pct = data.total > 0 ? data.indexed / data.total : 0;
return (
<Section
title="Semantic search"
action={data.configured ? <Badge tone={data.indexed >= data.total ? 'good' : 'accent'}>{data.indexed >= data.total ? 'On' : 'Indexing'}</Badge> : <Badge>Off</Badge>}
>
<div className="card p-4 sm:p-5">
<p className="text-sm text-ink-2">
Finds passages by meaning as well as by words, so "why do patients on Lasix need bananas?" finds the section on furosemide and potassium. Used by search, the assistant and
focused generation. Keyword search keeps working without it.
</p>
{eligible.length === 0 ? (
<Callout tone="accent" icon={<Sparkles />} className="mt-4" title="Needs an embeddings provider">
Connect OpenAI, Gemini, Ollama or an OpenAI-compatible server above. Anthropic does not offer embeddings.
</Callout>
) : (
<div className="mt-4 grid gap-3 sm:grid-cols-[1fr_1fr_auto]">
<Field label="Provider">
<Select
value={providerId}
onChange={(e) => {
const next = providers.find((p) => p.id === e.target.value);
setProviderId(e.target.value);
if (next && (!model || !dirty)) setModel(data.defaults[next.kind] ?? '');
}}
>
<option value="">Off (keywords only)</option>
{eligible.map((p) => (
<option key={p.id} value={p.id}>
{p.name}
</option>
))}
</Select>
</Field>
<Field label="Embedding model" hint={suggested ? `Suggested: ${suggested}` : chosen ? 'The model id your server uses for embeddings' : undefined}>
<Input value={model} onChange={(e) => setModel(e.target.value)} placeholder={suggested || 'text-embedding-3-small'} disabled={!providerId} />
</Field>
<Field label="Size" hint="Optional">
<Input type="number" min={64} max={4096} step={64} value={dimensions} onChange={(e) => setDimensions(e.target.value)} placeholder="Native" disabled={!providerId} className="sm:w-28" />
</Field>
</div>
)}
{eligible.length > 0 && (
<div className="mt-3 flex flex-wrap gap-2">
<Button variant="primary" size="sm" disabled={!dirty || (Boolean(providerId) && !model.trim())} loading={busy === 'save'} onClick={save}>
Save
</Button>
{data.configured && !dirty && (
<Button size="sm" icon={<Zap />} loading={busy === 'test'} onClick={test}>
Test
</Button>
)}
</div>
)}
{data.configured && (
<div className="mt-5 border-t border-line pt-4">
<div className="flex items-center justify-between gap-3 text-[13px]">
<span className="text-ink-2">
<span className="num font-medium text-ink">{data.indexed.toLocaleString()}</span> of <span className="num">{data.total.toLocaleString()}</span> passages indexed
{data.running ? ' · working…' : ''}
</span>
<span className="flex gap-1">
{!data.running && data.indexed < data.total && (
<Button size="sm" variant="ghost" loading={busy === 'rebuild'} onClick={() => act('resume')}>
Continue
</Button>
)}
<Button size="sm" variant="ghost" icon={<RefreshCw />} disabled={data.running} loading={busy === 'rebuild'} onClick={() => act('rebuild')}>
Rebuild
</Button>
</span>
</div>
<ProgressBar value={pct} className="mt-2" label="Indexing progress" />
{data.lastError && (
<Callout tone="warn" icon={<CircleAlert />} className="mt-3" title="Indexing paused">
{data.lastError}
</Callout>
)}
</div>
)}
</div>
<p className="mt-2 text-xs text-ink-3">Embedding costs are small (OpenAI's small model is about $0.02 per million tokens) and appear in Usage. Local Ollama models are free.</p>
</Section>
);
}

View file

@ -110,6 +110,11 @@ export interface RunnerProps {
index: number;
elapsedSec: number;
timeLimitSec: number | null;
/**
* Fixed end time on this device's clock. When set, the countdown follows the wall clock
* and keeps running while the page is hidden or closed. Without it the clock pauses.
*/
deadlineAt?: number | null;
sataScoring: SataScoring;
onAnswer: (questionId: string, selected: string[], checked: boolean) => void;
onFlags: (flags: string[]) => void;
@ -131,7 +136,6 @@ export function QuizRunner(p: RunnerProps) {
const topRef = useRef<HTMLDivElement>(null);
const tick = useLatest(p.onTick);
const finish = useLatest(p.onFinish);
const elapsedRef = useLatest(elapsed);
const autoSubmitted = useRef(false);
const total = p.questions.length;
@ -156,24 +160,36 @@ export function QuizRunner(p: RunnerProps) {
}
return n > 0 ? { n, points, percent: Math.round((points / n) * 100) } : null;
}, [p.mode, p.questions, p.answers, p.sataScoring]);
const remaining = p.timeLimitSec !== null ? Math.max(0, p.timeLimitSec - elapsed) : null;
const [wallNow, setWallNow] = useState(() => Date.now());
const deadline = p.deadlineAt && p.timeLimitSec !== null ? p.deadlineAt : null;
const remaining = deadline !== null ? Math.max(0, Math.ceil((deadline - wallNow) / 1000)) : p.timeLimitSec !== null ? Math.max(0, p.timeLimitSec - elapsed) : null;
// Time used: wall clock for deadline exams, otherwise the active-study clock.
const clock = deadline !== null && p.timeLimitSec !== null ? Math.max(elapsed, p.timeLimitSec - (remaining ?? 0)) : elapsed;
const clockRef = useLatest(clock);
// Session clock: pauses while the tab is hidden.
useEffect(() => {
const timer = setInterval(() => {
setWallNow(Date.now());
// The untimed/legacy clock pauses while the tab is hidden.
if (document.visibilityState === 'visible') setElapsed((e) => e + 1);
}, 1000);
return () => clearInterval(timer);
// Catch up immediately when returning to a deadline exam.
const onVisible = () => document.visibilityState === 'visible' && setWallNow(Date.now());
document.addEventListener('visibilitychange', onVisible);
return () => {
clearInterval(timer);
document.removeEventListener('visibilitychange', onVisible);
};
}, []);
useEffect(() => {
if (elapsed > 0 && elapsed % 10 === 0) tick.current?.(elapsed);
}, [elapsed, tick]);
if (elapsed > 0 && elapsed % 10 === 0) tick.current?.(clockRef.current);
}, [elapsed, tick, clockRef]);
useEffect(() => {
if (remaining === 0 && !p.finishing && !autoSubmitted.current) {
autoSubmitted.current = true;
finish.current(elapsedRef.current);
finish.current(clockRef.current);
}
}, [remaining, p.finishing, finish, elapsedRef]);
}, [remaining, p.finishing, finish, clockRef]);
useEffect(() => {
topRef.current?.scrollIntoView({ block: 'start' });
}, [index]);
@ -197,7 +213,7 @@ export function QuizRunner(p: RunnerProps) {
);
if (!ok) return;
}
p.onFinish(elapsed);
p.onFinish(clock);
};
const primary = () => {
@ -248,7 +264,7 @@ export function QuizRunner(p: RunnerProps) {
)}
<div className={cx('num flex items-center gap-1.5 rounded-full px-2.5 py-1 text-[13px] font-medium', remaining !== null && remaining < 60 ? 'bg-bad-soft text-bad' : 'text-ink-2')} aria-label={remaining !== null ? 'Time remaining' : 'Time elapsed'}>
<Clock className="size-3.5" />
{formatDuration(remaining ?? elapsed)}
{formatDuration(remaining ?? clock)}
</div>
<button type="button" onClick={() => setNavOpen(true)} className="num flex h-9 items-center gap-1.5 rounded-[9px] border border-line-strong bg-surface px-2.5 text-[13px] font-medium shadow-1 hover:border-ink-3" aria-label="Question navigator">
<LayoutGrid className="size-3.5 text-ink-3" />