Study/web/src/features/AiSettings.tsx
Elijah 031f7bec31
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
Second revision: MCP servers, shared-link AI, flashcard modes, document previews
Generation
- Every quiz is NCLEX style; "Practice exam" writes 50 questions.
- Calculation questions only when the material involves calculations (max 5 per exam).
- Broad topics: the class's topics are listed in the prompt, a quick grouping call maps
  narrow labels onto broad ones, and leftover variants are merged.
- Exact select-all count per batch; option order shuffled.
- Python check: at most 3 runs, fixed problems count as passed, cosmetic remarks ignored.

Assistant
- Text, tool steps and cards appear in the order they happen, with paragraph breaks
  between steps.
- Optional thinking box: fixed height, follows the newest text, can be scrolled back.
- "+" opens a material picker listing recent items; items can be dragged in from class
  pages; shorter placeholder.

MCP servers
- Streamable HTTP client; encrypted headers.
- Per-server instructions and switches for chat, Explain, generation and the quiz check.
- Settings UI.

Share links
- Optional "Explain this" and "Ask about this material" per link, off by default.
- Items are loaded on the server and checked against the share; no tools; rate-limited.
- Migration 4 stores the switches.

Study and documents
- Standard flashcard mode (optionally repeat missed cards until known) next to spaced
  repetition.
- Streak visuals restyled in the app palette.
- Side by side / Original document views, with sandboxed previews of Word and PowerPoint
  files.

Other
- Built-in embedding model baked into the image; sharp stubbed if it cannot load; real
  load error shown in Settings.
- Python runner blocks network and process modules in JavaScript too.
- "View questions and answers" removed from the quiz start screen.
- Docs updated.

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

750 lines
37 KiB
TypeScript

import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Brain, Check, CircleAlert, Eye, ExternalLink, KeyRound, MoreHorizontal, Pencil, Plus, RefreshCw, Search, Server, Trash2, Wrench, Zap } from 'lucide-react';
import { useEffect, useMemo, useState } from 'react';
import { toast } from 'sonner';
import { api, errorMessage } from '@/lib/api';
import { qk } from '@/lib/queries';
import { McpSettings } from './McpSettings';
import { SemanticSettings } from './SemanticSettings';
import { REASONING_EFFORTS, type AdapterMeta, type AiModel, type AiStatus, type Provider, type ProviderKind, type TaskConfig, type TaskName, type 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';
interface Meta {
adapters: AdapterMeta[];
tasks: Record<TaskName, { label: string; description: string }>;
catalogUpdated: string;
}
const KIND_BLURB: Record<ProviderKind, string> = {
openai: 'GPT models with an OpenAI API key.',
anthropic: 'Claude models with an Anthropic API key.',
gemini: 'Gemini models with a Google AI Studio key.',
deepseek: 'DeepSeek Flash and Pro with a DeepSeek platform key.',
openai_compatible: 'OpenRouter, Groq, Together, LM Studio, vLLM and other compatible servers.',
ollama: 'Models running on your own machine or server.',
};
function useAiData() {
const qc = useQueryClient();
const meta = useQuery({ queryKey: ['ai', 'meta'], queryFn: () => api.get<Meta>('/api/ai/meta'), staleTime: Infinity });
const providers = useQuery({ queryKey: qk.aiProviders, queryFn: () => api.get<{ providers: Provider[] }>('/api/ai/providers').then((r) => r.providers) });
const models = useQuery({ queryKey: qk.aiModels, queryFn: () => api.get<{ models: AiModel[] }>('/api/ai/models').then((r) => r.models) });
const tasks = useQuery({ queryKey: ['ai', 'tasks'], queryFn: () => api.get<{ tasks: TaskSettings }>('/api/ai/tasks').then((r) => r.tasks) });
const status = useQuery({ queryKey: qk.aiStatus, queryFn: () => api.get<AiStatus>('/api/ai/status') });
const reload = () => qc.invalidateQueries({ queryKey: ['ai'] });
return { meta, providers, models, tasks, status, reload };
}
/* ------------------------------ provider dialog ------------------------------ */
function ProviderDialog({ open, onClose, adapters, existing, onSaved }: { open: boolean; onClose: () => void; adapters: AdapterMeta[]; existing: Provider | null; onSaved: (p: Provider, isNew: boolean) => void }) {
const [kind, setKind] = useState<ProviderKind | null>(null);
const [name, setName] = useState('');
const [apiKey, setApiKey] = useState('');
const [baseUrl, setBaseUrl] = useState('');
const [headers, setHeaders] = useState('');
const [busy, setBusy] = useState(false);
const adapter = adapters.find((a) => a.kind === kind);
useEffect(() => {
if (!open) return;
setKind(existing?.kind ?? null);
setName(existing?.name ?? '');
setApiKey('');
setBaseUrl(existing?.baseUrl ?? '');
setHeaders('');
}, [open, existing]);
const pick = (k: ProviderKind) => {
const a = adapters.find((x) => x.kind === k)!;
setKind(k);
setName(k === 'openai_compatible' ? '' : a.label);
setBaseUrl(k === 'ollama' ? (a.defaultBaseUrl ?? '') : '');
};
const save = async () => {
if (!kind || !adapter) return;
setBusy(true);
try {
const parsedHeaders: Record<string, string> = {};
for (const line of headers.split('\n')) {
const at = line.indexOf(':');
if (at > 0) parsedHeaders[line.slice(0, at).trim()] = line.slice(at + 1).trim();
}
const body = {
name: name.trim() || adapter.label,
kind,
baseUrl: baseUrl.trim() || null,
apiKey: apiKey.trim() ? apiKey.trim() : existing ? undefined : null,
headers: headers.trim() ? parsedHeaders : existing ? undefined : {},
};
const res = existing ? await api.patch<{ provider: Provider }>(`/api/ai/providers/${existing.id}`, body) : await api.post<{ provider: Provider }>('/api/ai/providers', body);
onSaved(res.provider, !existing);
onClose();
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(false);
}
};
const needsUrl = adapter?.requiresBaseUrl;
const canSave = Boolean(kind) && (!needsUrl || baseUrl.trim()) && (!adapter?.requiresApiKey || apiKey.trim() || existing?.hasKey);
return (
<Dialog
open={open}
onOpenChange={(o) => !o && onClose()}
title={existing ? `Edit ${existing.name}` : kind ? `Connect ${adapter?.label}` : 'Add an AI provider'}
description={!kind ? 'Keys are encrypted and stay on your server. They are never sent to the browser.' : undefined}
footer={
kind ? (
<>
{!existing && (
<Button variant="ghost" className="mr-auto" onClick={() => setKind(null)}>
Back
</Button>
)}
<Button variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button variant="primary" loading={busy} disabled={!canSave} onClick={save}>
{existing ? 'Save' : 'Connect'}
</Button>
</>
) : undefined
}
>
{!kind ? (
<div className="grid gap-2">
{adapters.map((a) => (
<button key={a.kind} type="button" onClick={() => pick(a.kind)} className="flex items-center gap-3 rounded-xl border border-line-strong p-3.5 text-left hover:border-ink-3 hover:bg-ink/3">
<span className="grid size-10 shrink-0 place-items-center rounded-[11px] bg-sunken text-ink-2">{a.kind === 'ollama' || a.kind === 'openai_compatible' ? <Server className="size-[18px]" /> : <Brain className="size-[18px]" />}</span>
<span className="min-w-0 flex-1">
<span className="block font-semibold">{a.label}</span>
<span className="block text-[13px] text-ink-2">{KIND_BLURB[a.kind]}</span>
</span>
</button>
))}
</div>
) : (
adapter && (
<form
className="space-y-4"
onSubmit={(e) => {
e.preventDefault();
if (canSave) void save();
}}
>
<Field label="Name" hint="Shown in model pickers. Useful when you connect the same service with two keys.">
<Input value={name} onChange={(e) => setName(e.target.value)} placeholder={adapter.kind === 'openai_compatible' ? 'OpenRouter' : adapter.label} maxLength={80} />
</Field>
{(needsUrl || adapter.kind === 'ollama') && (
<Field label="Server URL" hint={adapter.kind === 'ollama' ? 'From inside Docker, "localhost" is the container. Use your server address, for example http://192.168.1.10:11434.' : 'The API root, for example https://openrouter.ai/api/v1 or http://192.168.1.10:1234/v1.'}>
<Input value={baseUrl} onChange={(e) => setBaseUrl(e.target.value)} placeholder={adapter.defaultBaseUrl ?? 'https://…/v1'} inputMode="url" spellCheck={false} className="num text-[13px]" />
</Field>
)}
<Field
label="API key"
optional={!adapter.requiresApiKey}
hint={
<>
{existing?.hasKey ? `Stored key: ${existing.keyPreview}. Leave blank to keep it. ` : ''}
{adapter.requiresApiKey && (
<a href={adapter.docsUrl} target="_blank" rel="noreferrer" className="inline-flex items-center gap-1 font-medium text-accent hover:underline">
Get a key <ExternalLink className="size-3" />
</a>
)}
</>
}
>
<Input type="password" value={apiKey} onChange={(e) => setApiKey(e.target.value)} placeholder={existing?.hasKey ? '••••••••' : adapter.keyHint} autoComplete="off" spellCheck={false} className="num text-[13px]" />
</Field>
<details className="rounded-xl border border-line">
<summary className="px-3.5 py-2.5 text-sm font-medium text-ink-2 hover:text-ink">Advanced</summary>
<div className="space-y-4 border-t border-line p-3.5">
{!needsUrl && adapter.kind !== 'ollama' && (
<Field label="Custom endpoint" optional hint="Only for proxies and gateways. Leave blank for the official API.">
<Input value={baseUrl} onChange={(e) => setBaseUrl(e.target.value)} placeholder={adapter.defaultBaseUrl ?? ''} spellCheck={false} className="num text-[13px]" />
</Field>
)}
<Field label="Extra HTTP headers" optional hint={existing?.headerNames.length ? `Stored: ${existing.headerNames.join(', ')}. Entering anything here replaces them.` : 'One per line, as Name: value.'}>
<Textarea rows={2} value={headers} onChange={(e) => setHeaders(e.target.value)} placeholder="HTTP-Referer: https://study.example.com" spellCheck={false} className="num text-[13px]" />
</Field>
</div>
</details>
<button type="submit" hidden />
</form>
)
)}
</Dialog>
);
}
/* ------------------------------ model dialog ------------------------------ */
/** The discount a provider gives on cached input when a model has no cached price of its own (mirrors the server). */
const CACHE_HINT: Record<ProviderKind, string> = {
openai: "OpenAI's discount (90% off for GPT-5 and later)",
anthropic: "Claude's cache-read rate (10% of input)",
gemini: "Gemini's cache rate (10% of input)",
deepseek: "DeepSeek's cache-hit rate",
openai_compatible: 'the normal input price',
ollama: 'the normal input price',
};
function cachedGuess(model: AiModel): string {
const id = model.model.slice(model.model.lastIndexOf('/') + 1);
const ratio =
model.providerKind === 'openai'
? /^gpt-4\.1/.test(id)
? 0.25
: /^(gpt-4o|o\d)/.test(id)
? 0.5
: 0.1
: model.providerKind === 'anthropic' || model.providerKind === 'gemini'
? 0.1
: model.providerKind === 'deepseek'
? 0.02
: 1;
const v = (model.priceIn ?? 0) * ratio;
return `$${v < 0.01 ? v.toFixed(4) : v.toFixed(3)}`;
}
function ModelDialog({ model, onClose, onSaved }: { model: AiModel | null; onClose: () => void; onSaved: () => void }) {
const [label, setLabel] = useState('');
const [caps, setCaps] = useState(model?.capabilities ?? { tools: true, vision: false, pdf: false, reasoning: false, temperature: true });
const [ctx, setCtx] = useState('');
const [maxOut, setMaxOut] = useState('');
const [priceIn, setPriceIn] = useState('');
const [priceOut, setPriceOut] = useState('');
const [priceCached, setPriceCached] = useState('');
const [busy, setBusy] = useState(false);
useEffect(() => {
if (!model) return;
setLabel(model.label);
setCaps(model.capabilities);
setCtx(model.contextWindow?.toString() ?? '');
setMaxOut(model.maxOutput?.toString() ?? '');
setPriceIn(model.priceIn?.toString() ?? '');
setPriceOut(model.priceOut?.toString() ?? '');
setPriceCached(model.priceCachedIn?.toString() ?? '');
}, [model]);
const num = (s: string) => (s.trim() === '' ? null : Number(s));
const save = async () => {
if (!model) return;
setBusy(true);
try {
await api.patch(`/api/ai/models/${model.id}`, { label, capabilities: caps, contextWindow: num(ctx), maxOutput: num(maxOut), priceIn: num(priceIn), priceOut: num(priceOut), priceCachedIn: num(priceCached) });
onSaved();
onClose();
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(false);
}
};
return (
<Dialog
open={model !== null}
onOpenChange={(o) => !o && onClose()}
title="Model details"
description={model?.model}
footer={
<>
<Button variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button variant="primary" loading={busy} onClick={save}>
Save
</Button>
</>
}
>
<div className="space-y-4">
<Field label="Display name">
<Input value={label} onChange={(e) => setLabel(e.target.value)} maxLength={120} />
</Field>
<div className="space-y-3.5 rounded-xl border border-line p-4">
<Switch checked={caps.tools} onChange={(v) => setCaps({ ...caps, tools: v })} label="Tool calling" description="Needed for the assistant to act on your library. Generation falls back to plain JSON without it." />
<Switch checked={caps.vision} onChange={(v) => setCaps({ ...caps, vision: v })} label="Reads images" />
<Switch checked={caps.pdf} onChange={(v) => setCaps({ ...caps, pdf: v })} label="Reads PDF files directly" description="Used to transcribe scanned PDFs." />
<Switch checked={caps.reasoning} onChange={(v) => setCaps({ ...caps, reasoning: v })} label="Adjustable reasoning effort" />
<Switch checked={caps.temperature} onChange={(v) => setCaps({ ...caps, temperature: v })} label="Accepts a temperature" description="Turn off for models that reject it." />
</div>
<div className="grid grid-cols-2 gap-4">
<Field label="Context window" hint="Tokens">
<Input type="number" min={0} value={ctx} onChange={(e) => setCtx(e.target.value)} placeholder="Unknown" />
</Field>
<Field label="Max output" hint="Tokens">
<Input type="number" min={0} value={maxOut} onChange={(e) => setMaxOut(e.target.value)} placeholder="Unknown" />
</Field>
<Field label="Input price" hint="USD per million tokens">
<Input type="number" min={0} step="0.01" value={priceIn} onChange={(e) => setPriceIn(e.target.value)} placeholder="Unknown" />
</Field>
<Field label="Output price" hint="USD per million tokens">
<Input type="number" min={0} step="0.01" value={priceOut} onChange={(e) => setPriceOut(e.target.value)} placeholder="Unknown" />
</Field>
<Field label="Cached input price" hint={`USD per million cached (cache-hit) input tokens. Blank uses ${model ? CACHE_HINT[model.providerKind] : 'the provider discount'}.`} className="col-span-2">
<Input type="number" min={0} step="0.001" value={priceCached} onChange={(e) => setPriceCached(e.target.value)} placeholder={model?.priceIn != null ? `About ${cachedGuess(model)}` : 'Unknown'} />
</Field>
</div>
</div>
</Dialog>
);
}
/* ------------------------------ provider card ------------------------------ */
function ProviderCard({ provider, models, adapter, onEdit, reload }: { provider: Provider; models: AiModel[]; adapter?: AdapterMeta; onEdit: () => void; reload: () => void }) {
const confirm = useConfirm();
const [busy, setBusy] = useState<'test' | 'discover' | null>(null);
const [showAll, setShowAll] = useState(false);
const [filter, setFilter] = useState('');
const [manual, setManual] = useState('');
const [editing, setEditing] = useState<AiModel | null>(null);
const enabled = models.filter((m) => m.enabled);
const needle = filter.trim().toLowerCase();
const list = (showAll || enabled.length === 0 ? models : enabled).filter((m) => !needle || m.model.toLowerCase().includes(needle) || m.label.toLowerCase().includes(needle));
const test = async () => {
setBusy('test');
try {
const res = await api.post<{ result: { ok: boolean; message: string } }>(`/api/ai/providers/${provider.id}/test`);
if (res.result.ok) toast.success(res.result.message);
else toast.error(res.result.message);
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(null);
reload();
}
};
const discover = async () => {
setBusy('discover');
try {
const res = await api.post<{ added: number; total: number }>(`/api/ai/providers/${provider.id}/discover`);
toast.success(res.added > 0 ? `Found ${plural(res.added, 'new model')}` : `Model list is up to date (${res.total})`);
if (enabled.length === 0) setShowAll(true);
} catch (e) {
toast.error(errorMessage(e));
} finally {
setBusy(null);
reload();
}
};
const toggleModel = async (m: AiModel, on: boolean) => {
await api.patch(`/api/ai/models/${m.id}`, { enabled: on }).catch((e) => toast.error(errorMessage(e)));
reload();
};
const addManual = async () => {
if (!manual.trim()) return;
try {
await api.post('/api/ai/models', { providerId: provider.id, model: manual.trim() });
setManual('');
reload();
} catch (e) {
toast.error(errorMessage(e));
}
};
const remove = async () => {
if (!(await confirm({ title: `Remove ${provider.name}?`, message: 'Its key and model list are deleted. Tasks that used its models fall back to your default model.', confirmLabel: 'Remove', danger: true }))) return;
await api.del(`/api/ai/providers/${provider.id}`).catch((e) => toast.error(errorMessage(e)));
reload();
};
return (
<div className={cx('card overflow-hidden', !provider.enabled && 'opacity-70')}>
<div className="flex flex-wrap items-center gap-3 px-4 py-3.5">
<span className="grid size-10 shrink-0 place-items-center rounded-[11px] bg-sunken text-ink-2">{provider.kind === 'ollama' || provider.kind === 'openai_compatible' ? <Server className="size-[18px]" /> : <Brain className="size-[18px]" />}</span>
<div className="min-w-0 flex-1 basis-40">
<div className="flex flex-wrap items-center gap-2">
<span className="font-semibold">{provider.name}</span>
<Badge>{adapter?.label ?? provider.kind}</Badge>
{!provider.enabled && <Badge tone="warn">Off</Badge>}
</div>
<div className="mt-0.5 flex flex-wrap items-center gap-x-2.5 text-xs text-ink-3">
{provider.hasKey && (
<span className="num flex items-center gap-1">
<KeyRound className="size-3" /> {provider.keyPreview}
</span>
)}
{provider.baseUrl && <span className="num truncate">{provider.baseUrl}</span>}
{provider.lastTestOk !== null && (
<span className={cx('flex items-center gap-1', provider.lastTestOk ? 'text-good' : 'text-bad')}>
{provider.lastTestOk ? <Check className="size-3" /> : <CircleAlert className="size-3" />}
{provider.lastTestOk ? 'Connected' : provider.lastTestMessage} · {timeAgo(provider.lastTestAt)}
</span>
)}
</div>
</div>
<div className="flex items-center gap-1.5">
<Button size="sm" loading={busy === 'test'} disabled={busy !== null} onClick={test} icon={<Zap />}>
Test
</Button>
<Button size="sm" loading={busy === 'discover'} disabled={busy !== null} onClick={discover} icon={<RefreshCw />}>
Find models
</Button>
<Menu
trigger={
<IconButton label="Provider options" size="sm" tooltip={false}>
<MoreHorizontal />
</IconButton>
}
>
<MenuItem icon={<Pencil />} onSelect={onEdit}>
Edit connection
</MenuItem>
<MenuItem
icon={<Zap />}
onSelect={async () => {
await api.patch(`/api/ai/providers/${provider.id}`, { name: provider.name, kind: provider.kind, baseUrl: provider.baseUrl, enabled: !provider.enabled }).catch((e) => toast.error(errorMessage(e)));
reload();
}}
>
{provider.enabled ? 'Turn off' : 'Turn on'}
</MenuItem>
<MenuSeparator />
<MenuItem icon={<Trash2 />} danger onSelect={remove}>
Remove
</MenuItem>
</Menu>
</div>
</div>
<div className="border-t border-line bg-paper/50">
{models.length === 0 ? (
<p className="px-4 py-4 text-[13px] text-ink-2">No models yet. Use "Find models" to load the list, or add a model id below.</p>
) : (
<>
<div className="flex flex-wrap items-center gap-2 px-4 pt-3 pb-1.5">
<span className="eyebrow">
{enabled.length} of {models.length} models on
</span>
{models.length > 8 && (showAll || enabled.length === 0) && (
<div className="relative ml-auto">
<Search className="pointer-events-none absolute top-1/2 left-2 size-3.5 -translate-y-1/2 text-ink-3" />
<Input value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="Filter models" aria-label="Filter models" className="h-8 w-44 pl-7 text-[13px]" />
</div>
)}
{enabled.length > 0 && enabled.length < models.length && (
<button type="button" className={cx('text-xs font-medium text-accent hover:underline', !(models.length > 8 && showAll) && 'ml-auto')} onClick={() => setShowAll(!showAll)}>
{showAll ? 'Show only models that are on' : `Show all ${models.length}`}
</button>
)}
</div>
<ul className="max-h-80 divide-y divide-line overflow-y-auto">
{list.map((m) => (
<li key={m.id} className="flex items-center gap-3 px-4 py-2">
<input type="checkbox" role="switch" aria-label={`Use ${m.label}`} checked={m.enabled} onChange={(e) => toggleModel(m, e.target.checked)} className="size-4 shrink-0 accent-[var(--accent)]" />
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium">{m.label}</div>
<div className="num truncate text-[11px] text-ink-3">{m.model}</div>
</div>
<div className="hidden shrink-0 items-center gap-1 sm:flex">
{m.capabilities.tools && (
<Tooltip label="Tool calling">
<span className="grid size-6 place-items-center rounded-md bg-sunken text-ink-3">
<Wrench className="size-3.5" />
</span>
</Tooltip>
)}
{m.capabilities.vision && (
<Tooltip label={m.capabilities.pdf ? 'Reads images and PDFs' : 'Reads images'}>
<span className="grid size-6 place-items-center rounded-md bg-sunken text-ink-3">
<Eye className="size-3.5" />
</span>
</Tooltip>
)}
{m.capabilities.reasoning && (
<Tooltip label="Adjustable reasoning">
<span className="grid size-6 place-items-center rounded-md bg-sunken text-ink-3">
<Brain className="size-3.5" />
</span>
</Tooltip>
)}
</div>
<span className="num hidden w-28 shrink-0 text-right text-[11px] text-ink-3 md:block">{m.priceIn !== null && m.priceOut !== null ? `$${m.priceIn} / $${m.priceOut}` : 'price unknown'}</span>
<IconButton label={`Edit ${m.label}`} size="sm" onClick={() => setEditing(m)}>
<Pencil />
</IconButton>
</li>
))}
{list.length === 0 && <li className="px-4 py-4 text-[13px] text-ink-3">No models match.</li>}
</ul>
</>
)}
<form
className="flex gap-2 border-t border-line px-4 py-3"
onSubmit={(e) => {
e.preventDefault();
void addManual();
}}
>
<Input value={manual} onChange={(e) => setManual(e.target.value)} placeholder="Add a model id manually" aria-label="Model id" spellCheck={false} className="num h-9 text-[13px]" />
<Button type="submit" size="sm" className="h-9" icon={<Plus />} disabled={!manual.trim()}>
Add
</Button>
</form>
</div>
<ModelDialog model={editing} onClose={() => setEditing(null)} onSaved={reload} />
</div>
);
}
/* ------------------------------ tasks ------------------------------ */
function TaskRow({ name, label, description, config, models, resolved, onChange }: { name: TaskName | 'default'; label: string; description: string; config: TaskConfig; models: AiModel[]; resolved?: string; onChange: (c: TaskConfig) => void }) {
const model = models.find((m) => m.id === config.modelId);
const hasOverrides = config.temperature !== null || config.reasoningEffort !== null || config.maxOutputTokens !== null;
const byProvider = useMemo(() => {
const map = new Map<string, AiModel[]>();
for (const m of models) map.set(m.providerName, [...(map.get(m.providerName) ?? []), m]);
return [...map.entries()];
}, [models]);
return (
<div className="px-4 py-3.5">
<div className="grid items-center gap-x-4 gap-y-2 sm:grid-cols-[1fr_280px]">
<div>
<div className="text-sm font-semibold">{label}</div>
<div className="text-[13px] text-ink-2">{description}</div>
</div>
<Select value={config.modelId ?? ''} onChange={(e) => onChange({ ...config, modelId: e.target.value || null })} aria-label={`Model for ${label}`}>
<option value="">{name === 'default' ? 'First available model' : `Same as default${resolved ? ` (${resolved})` : ''}`}</option>
{byProvider.map(([provider, list]) => (
<optgroup key={provider} label={provider}>
{list.map((m) => (
<option key={m.id} value={m.id} disabled={name === 'vision' && !m.capabilities.vision}>
{m.label}
{name === 'vision' && !m.capabilities.vision ? ' (no vision)' : ''}
</option>
))}
</optgroup>
))}
</Select>
</div>
<details className="mt-2" open={hasOverrides}>
<summary className="w-fit text-xs font-medium text-ink-3 hover:text-ink-2">Parameters{hasOverrides ? ' (customised)' : ''}</summary>
<div className="mt-2.5 grid gap-3 sm:grid-cols-3">
<Field label="Reasoning effort" hint={model && !model.capabilities.reasoning ? 'Not supported by this model' : undefined}>
<Select value={config.reasoningEffort ?? ''} onChange={(e) => onChange({ ...config, reasoningEffort: (e.target.value || null) as TaskConfig['reasoningEffort'] })}>
<option value="">Model default</option>
{REASONING_EFFORTS.map((e) => (
<option key={e.value} value={e.value}>
{e.label}
</option>
))}
</Select>
</Field>
<Field label="Temperature" hint={model && !model.capabilities.temperature ? 'Ignored by this model' : '0 to 2'}>
<Input type="number" min={0} max={2} step={0.1} value={config.temperature ?? ''} placeholder="Model default" onChange={(e) => onChange({ ...config, temperature: e.target.value === '' ? null : Math.max(0, Math.min(2, Number(e.target.value))) })} />
</Field>
<Field label="Output limit" hint="Tokens per response">
<Input type="number" min={256} step={256} value={config.maxOutputTokens ?? ''} placeholder="Automatic" onChange={(e) => onChange({ ...config, maxOutputTokens: e.target.value === '' ? null : Math.max(256, Math.round(Number(e.target.value))) })} />
</Field>
</div>
</details>
</div>
);
}
/* ------------------------------ usage ------------------------------ */
function Usage() {
const [days, setDays] = useState(30);
const q = useQuery({ queryKey: ['ai', 'usage', days], queryFn: () => api.get<any>(`/api/ai/usage?days=${days}`) });
const u = q.data;
return (
<Section
title="Usage"
action={
<Select value={days} onChange={(e) => setDays(Number(e.target.value))} aria-label="Period" className="h-8 w-32 text-[13px]">
<option value={7}>Last 7 days</option>
<option value={30}>Last 30 days</option>
<option value={90}>Last 90 days</option>
</Select>
}
>
{!u ? (
<Skeleton className="h-28" />
) : u.total.calls === 0 ? (
<div className="card px-4 py-5 text-sm text-ink-2">No AI requests in this period.</div>
) : (
<div className="card overflow-hidden">
<div className="grid grid-cols-2 gap-px border-b border-line bg-line sm:grid-cols-4">
{[
['Estimated cost', formatCost(u.total.cost)],
['Requests', u.total.calls.toLocaleString()],
['Tokens in / out', `${formatTokens(u.total.input)} / ${formatTokens(u.total.output)}`],
['Cached input', u.total.input ? `${formatTokens(u.total.cached)} (${Math.round((u.total.cached / u.total.input) * 100)}%)` : '0'],
].map(([k, v]) => (
<div key={k} className="bg-surface px-4 py-3">
<div className="eyebrow">{k}</div>
<div className="num mt-1 text-lg font-semibold">{v}</div>
</div>
))}
</div>
<div className="overflow-x-auto">
<table className="w-full text-[13px]">
<thead>
<tr className="text-left text-ink-3">
<th className="px-4 py-2 font-medium">Model</th>
<th className="px-3 py-2 text-right font-medium">Requests</th>
<th className="px-3 py-2 text-right font-medium">In</th>
<th className="px-3 py-2 text-right font-medium">Cached</th>
<th className="px-3 py-2 text-right font-medium">Out</th>
<th className="px-4 py-2 text-right font-medium">Cost</th>
</tr>
</thead>
<tbody className="divide-y divide-line border-t border-line">
{u.byModel.map((m: any) => (
<tr key={`${m.provider}:${m.model}`}>
<td className="px-4 py-2">
<div className="font-medium">{m.model}</div>
<div className="text-xs text-ink-3">
{m.provider}
{m.errors > 0 ? ` · ${plural(m.errors, 'failed request')}` : ''}
</div>
</td>
<td className="num px-3 py-2 text-right">{m.calls}</td>
<td className="num px-3 py-2 text-right">{formatTokens(m.input)}</td>
<td className="num px-3 py-2 text-right">{formatTokens(m.cached)}</td>
<td className="num px-3 py-2 text-right">{formatTokens(m.output)}</td>
<td className="num px-4 py-2 text-right">{m.unpriced > 0 && !m.cost ? 'no price set' : formatCost(m.cost)}</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="flex flex-wrap gap-x-4 gap-y-1 border-t border-line px-4 py-2.5 text-xs text-ink-3">
{u.byTask.map((t: any) => (
<span key={t.task}>
{t.task}: <span className="num text-ink-2">{formatCost(t.cost)}</span> ({t.calls})
</span>
))}
</div>
{u.recentErrors.length > 0 && (
<details className="border-t border-line">
<summary className="px-4 py-2.5 text-[13px] font-medium text-bad">Recent errors ({u.recentErrors.length})</summary>
<ul className="space-y-1.5 px-4 pb-3 text-[13px]">
{u.recentErrors.map((e: any, i: number) => (
<li key={i} className="text-ink-2">
<span className="text-ink-3">{timeAgo(e.at)} · {e.model}:</span> {e.error}
</li>
))}
</ul>
</details>
)}
</div>
)}
<p className="mt-2 text-xs text-ink-3">Costs are estimates from token counts and the list prices on each model (editable). Your provider's bill is the source of truth.</p>
</Section>
);
}
/* ------------------------------ page section ------------------------------ */
export default function AiSettings() {
const { meta, providers, models, tasks, status, reload } = useAiData();
const [dialog, setDialog] = useState<{ open: boolean; existing: Provider | null }>({ open: false, existing: null });
const usable = (models.data ?? []).filter((m) => m.available);
const afterSave = async (p: Provider, isNew: boolean) => {
await reload();
// A new connection is tested and its models loaded straight away.
try {
const test = await api.post<{ result: { ok: boolean; message: string } }>(`/api/ai/providers/${p.id}/test`);
if (!test.result.ok) {
toast.error(test.result.message);
} else {
const found = await api.post<{ added: number; total: number }>(`/api/ai/providers/${p.id}/discover`).catch(() => null);
toast.success(isNew ? `${p.name} connected${found ? `: ${plural(found.total, 'model')} found. Switch on the ones you want.` : ''}` : test.result.message);
}
} catch (e) {
toast.error(errorMessage(e));
}
await reload();
};
const saveTask = async (name: TaskName | 'default', config: TaskConfig) => {
try {
await api.put('/api/ai/tasks', { [name]: config });
await reload();
} catch (e) {
toast.error(errorMessage(e));
}
};
if (!meta.data || !providers.data || !models.data || !tasks.data) {
return (
<div className="space-y-4">
<Skeleton className="h-24" />
<Skeleton className="h-48" />
</div>
);
}
return (
<div>
{providers.data.length > 0 && usable.length === 0 && (
<Callout tone="warn" icon={<CircleAlert />} title="No models are switched on" className="mb-5">
Use "Find models" on a provider, then tick the models you want to use.
</Callout>
)}
<Section
title="Providers and models"
action={
<Button size="sm" variant="primary" icon={<Plus />} onClick={() => setDialog({ open: true, existing: null })}>
Add provider
</Button>
}
>
{providers.data.length === 0 ? (
<div className="card border-dashed">
<EmptyState
icon={<Brain />}
title="Connect an AI model"
action={
<Button variant="primary" icon={<Plus />} onClick={() => setDialog({ open: true, existing: null })}>
Add provider
</Button>
}
>
Study Desk works without AI. Connecting a model adds question and flashcard generation from your documents, explanations, and the assistant. You can connect several providers and use different models for different jobs.
</EmptyState>
</div>
) : (
<div className="space-y-3">
{providers.data.map((p) => (
<ProviderCard key={p.id} provider={p} models={models.data!.filter((m) => m.providerId === p.id)} adapter={meta.data!.adapters.find((a) => a.kind === p.kind)} onEdit={() => setDialog({ open: true, existing: p })} reload={reload} />
))}
</div>
)}
<p className="mt-2.5 text-xs text-ink-3">
API keys are encrypted on the server and never sent back to the browser. These connections use each provider's API billing. ChatGPT plan sign-in requires a separate approved integration for remotely hosted apps; it is not configured in this installation.
</p>
</Section>
{usable.length > 0 && (
<Section title="Which model does what">
<div className="card divide-y divide-line">
<TaskRow name="default" label="Default model" description="Used for everything that has no model of its own below." config={tasks.data.default} models={usable} onChange={(c) => saveTask('default', c)} />
{(Object.keys(meta.data.tasks) as TaskName[]).map((t) => (
<TaskRow key={t} name={t} label={meta.data!.tasks[t].label} description={meta.data!.tasks[t].description} config={tasks.data![t]} models={usable} resolved={status.data?.resolved[t]?.label} onChange={(c) => saveTask(t, c)} />
))}
</div>
<p className="mt-2.5 text-xs text-ink-3">You can also pick a different model for a single draft from "More options" when generating.</p>
</Section>
)}
{providers.data.length > 0 && <SemanticSettings providers={providers.data} />}
{providers.data.length > 0 && <McpSettings />}
{providers.data.length > 0 && <Usage />}
<ProviderDialog open={dialog.open} onClose={() => setDialog((d) => ({ ...d, open: false }))} adapters={meta.data.adapters} existing={dialog.existing} onSaved={afterSave} />
</div>
);
}