diff --git a/.env.example b/.env.example index 3afb422..603adbd 100644 --- a/.env.example +++ b/.env.example @@ -32,5 +32,9 @@ # Number of automatic daily database snapshots to keep. # BACKUP_KEEP=7 +# Use the built-in embedding model for semantic search until another is chosen (default on). +# The model (~35 MB) is downloaded once into DATA_DIR/models. +# LOCAL_EMBEDDINGS=on + # Timezone used for "today", streaks and due dates until you change it in Settings. # TZ=America/Phoenix diff --git a/Dockerfile b/Dockerfile index 3e5847a..01cfb16 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,11 @@ FROM deps AS test RUN npm run typecheck && npm test FROM deps AS build -RUN npm run build && npm prune --omit=dev +# ONNX Runtime (built-in embedding model) ships binaries for every OS and CPU; keep only this one. +RUN npm run build && npm prune --omit=dev \ + && ARCH="$(node -p process.arch)" \ + && find node_modules/onnxruntime-node/bin -mindepth 2 -maxdepth 2 -type d ! -name linux -exec rm -rf {} + \ + && find node_modules/onnxruntime-node/bin -mindepth 3 -maxdepth 3 -type d ! -name "$ARCH" -exec rm -rf {} + FROM node:24-bookworm-slim AS runtime ENV NODE_ENV=production HOST=0.0.0.0 PORT=3726 DATA_DIR=/data WEB_DIR=/app/dist/web diff --git a/PROGRESS.md b/PROGRESS.md index b0c1e1e..06d1a1b 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -61,10 +61,34 @@ type-grouped materials, copyable import instructions and assistant library links enabled from Settings and indexed; a 1-minute exam counted down on the wall clock and auto-submitted at 60 s; a shared session kept original questions after the owner edited them. +## 2026-11-15: first revision (owner's 21-item list) + +Implemented and verified on an isolated instance (`.test-data/rev1`, port 3743): +DeepSeek provider (`server/ai/providers/deepseek.ts`); 7 reasoning-effort levels mapped per +adapter; the "does not support tool calling" errors were DeepSeek rejecting forced tool choice +in thinking mode: compatible servers now get the call requested in the prompt, and only a +genuine "no tools" 400 reports unsupported. Cached-input prices (migration 3) and cache-write +/off-peak aware costs. Built-in embeddings (`server/ai/localEmbed.ts`, bge-small, default when +nothing is chosen). Assistant turns run server-side independent of the browser (re-attach via +`GET /api/ai/chat/:id/stream`, stop via `POST /api/ai/chat/:id/stop`); drafts tray in the +sidebar/Today. Quiz start screen, navigator ring/legend fix, timer only for timed exams, +7-point letter grades, numeric questions (migration 3 rebuilds `questions` for the new type; +answers are compared numerically, units ignored, optional ± tolerance). Dialog footer padding +(`safe-pb-3`), keyboard hints removed, small assistant button, full-width assistant page, model +switcher and @-tagging. Editable generator instructions and built-in presets (builtin = 2 marks +an edited built-in), separate chatbot import instructions. Copy buttons work over plain HTTP +inside dialogs. Class/folder shares include processed documents. + +Live DeepSeek (`deepseek-flash` only) checks passed: discovery, quiz generation at default and +Max effort (numeric items produced, answer keys correct with thinking on), multi-step tool +turns, follow-up turns, tagged quiz to flashcards, an answer finishing after disconnect; zero +errors in Usage. Docker image builds; ONNX Runtime and the embedding model run in the container. +The image grew to about 1.3 GB because of ONNX Runtime. + ## Verification status Baseline: 162 tests passed, TypeScript passed and production web/server builds passed. -Added six regression tests. Full `npm run check` passes: **181 tests** (2026-11-12), TypeScript +Added six regression tests. Full `npm run check` passes: **208 tests** (2026-11-15; 181 on 2026-11-12), TypeScript and both production builds. The Docker `test` stage runs the same 181 tests on Node 24. Tests were run with loopback access for the mock provider. The earlier agent manually exercised quiz completion/results, flashcards, streak effects, @@ -83,7 +107,7 @@ full offline administrator study is not implemented. 2. With owner-supplied provider connections: verify live discovery, streamed chat, generation, OCR and task routing. No real-provider calls should be inferred from mock test results. 3. Future optional work: approved ChatGPT account OAuth, more robust offline studying and - semantic embeddings (current retrieval is lexical FTS5). + (semantic embeddings were added on 2026-11-12, the built-in model on 2026-11-15). ## Resume safely diff --git a/README.md b/README.md index d0e41a3..29fcc31 100644 --- a/README.md +++ b/README.md @@ -33,19 +33,24 @@ is an opt-in development tool, never part of startup. ## What is implemented - Classes, folders, search, edit/duplicate/move/delete actions; Markdown and embedded images. -- Multiple-choice and select-all quizzes; practice feedback, exam timers, shuffle, partial - credit, saved sessions, missed-question review, results and history. +- Multiple-choice, select-all and numeric (type-the-answer calculation) questions; a start + screen that hides answers, practice feedback, optional exam time limits, shuffle, partial + credit, saved sessions, missed-question review, 7-point letter grades and history. - Flashcard editing, browse/self-assessment sessions, FSRS scheduling, persistent reviews, undo, keyboard shortcuts and streak effects. -- Public quiz/deck/class/folder/note links and explicitly shared documents. Collection shares - include current and future quizzes, decks and notes; uploaded documents stay private. +- Public quiz/deck/class/folder/note/document links. Collection (class and folder) shares + include current and future quizzes, decks, notes and processed documents. - JSON import previews, validation, templates, content exports and a V1 SQLite import path. - PDF/DOCX/PPTX/TXT/Markdown/image uploads; text extraction, OCR via a configured vision model, chunking and SQLite FTS retrieval with source references. -- OpenAI, Anthropic, Gemini, OpenAI-compatible and Ollama provider connections, manual/discovered - models, task routing, encrypted keys and usage estimates. -- AI generation with editable drafts; an assistant that searches materials, reads notes, - creates drafts, finds weak topics and proposes confirmed content changes. +- OpenAI, Anthropic, Gemini, DeepSeek, OpenAI-compatible and Ollama provider connections, + manual/discovered models, task routing with reasoning effort from Off to Max, encrypted keys + and usage estimates that price cached input separately. +- Semantic search with cloud embeddings or a built-in model that runs on the server. +- AI generation with editable drafts and editable instructions/presets; generation and + assistant answers keep running on the server when you leave the page. The assistant + searches materials, reads notes, creates drafts, finds weak topics, proposes confirmed + content changes, accepts @-tagged materials and can switch models per chat. - Responsive light/dark interface and an installable app shell. Offline studying is limited: private data is not cached; most operations need a server connection. diff --git a/docs/AI.md b/docs/AI.md index e7175ec..ce957ed 100644 --- a/docs/AI.md +++ b/docs/AI.md @@ -5,8 +5,20 @@ discover models where supported (or add a custom model), enable models and selec for generation, chat, explanations and vision. Output limits, capabilities, pricing and reasoning settings can be edited per model. Catalog prices are estimates, not billing records. -Adapters use OpenAI Responses, Anthropic Messages, Gemini generateContent, OpenAI-compatible -Chat Completions and Ollama's native chat API. Configure the endpoint as shown in Settings. +Adapters use OpenAI Responses, Anthropic Messages, Gemini generateContent, DeepSeek and +OpenAI-compatible Chat Completions, and Ollama's native chat API. + +Reasoning effort has seven levels (Off, Minimal, Low, Medium, High, Extra high, Max); each +adapter maps them onto the nearest value its API accepts. DeepSeek accepts low / high / max +and turns thinking off at "Off". DeepSeek rejects forced tool calls in thinking mode, so +structured generation keeps thinking on and asks for the tool call in the prompt; answer keys +were noticeably less reliable with thinking off. Its `reasoning_content` is replayed between +tool steps, as the API requires. + +Cost estimates split input into fresh, cache-hit and cache-write tokens. A model's "Cached +input price" is used when set; otherwise the provider's published discount applies (10% of +input for GPT-5+, Claude and Gemini; DeepSeek's catalog cache-hit price). DeepSeek's +off-peak half price is applied by time of call. Configure the endpoint as shown in Settings. Local models are reached **from the server/container**: `localhost` inside Docker is the container itself, not your workstation or Unraid host. Use a reachable LAN address instead. @@ -31,7 +43,10 @@ warning on the document. At most 600 pages are read per file. Settings → AI → Semantic search chooses an embeddings provider and model (suggested: OpenAI `text-embedding-3-small`, Gemini `gemini-embedding-001`, Ollama `nomic-embed-text`; -Anthropic has no embeddings API). Every document passage is embedded in the background and +Anthropic and DeepSeek have no embeddings API). Until something else is chosen, the built-in +model is used: BAAI bge-small-en-v1.5 (8-bit ONNX, 384 dimensions) running on the server's CPU +through transformers.js. It is downloaded once (about 35 MB) from huggingface.co into +`DATA_DIR/models` on first use and costs nothing. `LOCAL_EMBEDDINGS=off` disables the default. Every document passage is embedded in the background and stored as a vector in SQLite (`chunk_vectors`). Search, the assistant's document search and focused generation then merge keyword (FTS5) and meaning-based rankings with reciprocal-rank fusion, so exact terms still match while paraphrased questions find the right passage. If the diff --git a/package-lock.json b/package-lock.json index c711a90..64c1d24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@fontsource-variable/jetbrains-mono": "^5.3.0", "@fontsource-variable/newsreader": "^5.3.0", "@hono/node-server": "^2.1.6", + "@huggingface/transformers": "^4.3.2", "@tanstack/react-query": "^5.107.1", "clsx": "^2.1.1", "fast-xml-parser": "^5.12.0", @@ -50,6 +51,16 @@ "node": ">=24" } }, + "node_modules/@emnapi/runtime": { + "version": "1.11.3", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.3.tgz", + "integrity": "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.28.2", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", @@ -569,6 +580,582 @@ "hono": "^4 || ^5.0.0-0" } }, + "node_modules/@huggingface/jinja": { + "version": "0.5.13", + "resolved": "https://registry.npmjs.org/@huggingface/jinja/-/jinja-0.5.13.tgz", + "integrity": "sha512-jM17MqFily1iyT/1LD3r5w8/CZiyapVB0+7vOw48PfVtzyKsXStmHE4jVTyDAD05Tqv3un1xrS9ILUU1ugQAYQ==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@huggingface/tokenizers": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@huggingface/tokenizers/-/tokenizers-0.2.0.tgz", + "integrity": "sha512-LidMHe1FpcSYH4vcSjXooda34pC0M8m1gmDOv7SQi6Iv+ib1zX7J3sHBCC9/3FPCkn2k1b55FneVqMRxrr99pg==", + "license": "Apache-2.0" + }, + "node_modules/@huggingface/transformers": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/@huggingface/transformers/-/transformers-4.3.2.tgz", + "integrity": "sha512-V3+dqf3iWlRYJ9I9GreM3zGQd0lAXnnoPeddmkOQYWAEE4l/+Fi6p7tx/qVUJlKQgLXGim2W76rTOzz+eC0i7g==", + "license": "Apache-2.0", + "dependencies": { + "@huggingface/jinja": "^0.5.10", + "@huggingface/tokenizers": "^0.2.0", + "onnxruntime-node": "1.30.0", + "onnxruntime-web": "1.31.0-dev.20260914-8d85527a0", + "sharp": "^0.35.4" + } + }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.35.5.tgz", + "integrity": "sha512-QRUlFQ0WxvdWyqqG/WtI3iupfD5rBzmCHXSdPsY91sAtVtTo7Q4cb6zOccZ3gqEqkr0f1As1ehLqmEpDsRf+lg==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.35.5.tgz", + "integrity": "sha512-+BR255RhDlpygUpOc/Jdt1nT6DQ3XG/ERo5wbcdOf5Q320dKtPCKPLR1LJs9VGXRaMa8l1uUa0tkCNOXiAxZUw==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-freebsd-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-freebsd-wasm32/-/sharp-freebsd-wasm32-0.35.5.tgz", + "integrity": "sha512-Y/z91nEZ4uIBX5X3nfTovjU9lHNKFYbL2lpHCLVNmXQK03VIZvXBBt0KxbPGp2SdGSF+2mQU4e+hQaWOt86iAw==", + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.3.4.tgz", + "integrity": "sha512-5R89nBYiRdUlSWJxPhO+GVtaXzXSxKnRu/xqMn3KTA3L9EB9Oy/P+Nn2f2vlhPuUdy/Zusb2DarbyTpGCfEDuw==", + "cpu": [ + "arm64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.3.4.tgz", + "integrity": "sha512-iR2OKH80yi0U+dUplyh3/xdpFvps6YkCwsXenIJxqxR1v9o+xtKTGbS9H7cps+2Vxjc8B1j96p75NmTGjIhtpQ==", + "cpu": [ + "x64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.3.4.tgz", + "integrity": "sha512-LmRtTsOHuvM2+wlO2Db37dx5MiZhB0FvSunciw48YjdOkZz9KAiRbm8ujeMOA1INqmei5NapFxYEK1D1ZSidmw==", + "cpu": [ + "arm" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.3.4.tgz", + "integrity": "sha512-Y3dgX/6lE2QhQb+Gxy0WZxfg9MEm/JBjamZpS2IklP7xIQoKN4hzAm7KcMVGtaVDt3neE9OKBC7vAfonA/Lr1A==", + "cpu": [ + "arm64" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.3.4.tgz", + "integrity": "sha512-Le6boB8Tai0Nis+gIxIpKx68UDVVIqdR8Tin5Yf1z2LJJQLDJvCDRqRu+jC2qCoD+eIomonmOwB4smBRxfVpYQ==", + "cpu": [ + "ppc64" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.3.4.tgz", + "integrity": "sha512-aHkkIEHPRdQEegJN20MLmGtxYD9R2wQr3Cwpddnu5+YKMt6Uzax7S9h5gpZTo8wyrGuZSlfQ63OevL5mTyOC7Q==", + "cpu": [ + "riscv64" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.3.4.tgz", + "integrity": "sha512-ra/mB6MikESDUO7Yg+Mi95bFBb9GsObURuhnOv3OqknjGe9sZrG8tCe9q0xSIGrtLgvgw0gKnFWcK4blSgQOuQ==", + "cpu": [ + "s390x" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.3.4.tgz", + "integrity": "sha512-GJ//SSXbnwSDes02umB3nDJLFcQzw8a18V8fyhqr6tV515tOEMdImjjxj1AoafMRz56F3PHgftnj1QEKSU1zkw==", + "cpu": [ + "x64" + ], + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.3.4.tgz", + "integrity": "sha512-hvulFwtjUcagsis6BBxHwGFwWoNZjgYmULGVrZcyfNbjA8hKILbRxGg15/7w5HDyXHXUos/j6baAWqnCyQ2DWA==", + "cpu": [ + "arm64" + ], + "libc": [ + "musl" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.3.4.tgz", + "integrity": "sha512-6zXKeE/p39I1AmA3cJG35eyBGNqNddLnUXjhwBnsGjFPWqf5VKkDBEqaEkPDoTEtkxwi2vv8Tcr2mDyP4So7Fg==", + "cpu": [ + "x64" + ], + "libc": [ + "musl" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.35.5.tgz", + "integrity": "sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ==", + "cpu": [ + "arm" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.35.5.tgz", + "integrity": "sha512-LYVx5JTsOM2CBzmxreh+nl64/3H6Xb09iSLknqH47z2T2DFFxDeFLP5y4dJwe6H7uGQlHPyEEtIqyo3DYsRwdQ==", + "cpu": [ + "arm64" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.35.5.tgz", + "integrity": "sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA==", + "cpu": [ + "ppc64" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.35.5.tgz", + "integrity": "sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ==", + "cpu": [ + "riscv64" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.35.5.tgz", + "integrity": "sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA==", + "cpu": [ + "s390x" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.35.5.tgz", + "integrity": "sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog==", + "cpu": [ + "x64" + ], + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.35.5.tgz", + "integrity": "sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg==", + "cpu": [ + "arm64" + ], + "libc": [ + "musl" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.35.5.tgz", + "integrity": "sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A==", + "cpu": [ + "x64" + ], + "libc": [ + "musl" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.35.5.tgz", + "integrity": "sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ==", + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.11.3" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-webcontainers-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-webcontainers-wasm32/-/sharp-webcontainers-wasm32-0.35.5.tgz", + "integrity": "sha512-hfhF/FmoQyTUkA0bIKFOtw536BQSeBMe6BF6QyWlrPxT754+TFLaZ7sKKTfvvM0yJgKgaYTwnFCIZ/GuDw5SUA==", + "cpu": [ + "wasm32" + ], + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.35.5.tgz", + "integrity": "sha512-X4t7g+7ZA5DKblCBEXGjUqqemj4vczING/5viFwAL8h4N3qYeyjwdCvRLHi4EdOUI+2Z7UFlp1VM+p/AuEtm6Q==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.35.5.tgz", + "integrity": "sha512-5Zm82LoBc43nhwNybZlG7Y1KO//Zhsn306fQl29ZOuStHLGTo3BWL83q3cznX0poxSAMuYL1On/BHBxkBeKr6A==", + "cpu": [ + "ia32" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.35.5.tgz", + "integrity": "sha512-x76eH0vEiHlcMQu8Y8IenntaACtddpT6W0wmXtWrnKcnKI7ME5DdgqhAD6SEWOEl1v2zDvkZDhFA9KnURwpfqg==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.13", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", @@ -668,6 +1255,63 @@ "pako": "^1.0.10" } }, + "node_modules/@protobufjs/aspromise": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", + "integrity": "sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/base64": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/base64/-/base64-1.1.2.tgz", + "integrity": "sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/codegen": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@protobufjs/codegen/-/codegen-2.0.5.tgz", + "integrity": "sha512-zgXFLzW3Ap33e6d0Wlj4MGIm6Ce8O89n/apUaGNB/jx+hw+ruWEp7EwGUshdLKVRCxZW12fp9r40E1mQrf/34g==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/eventemitter": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@protobufjs/eventemitter/-/eventemitter-1.1.1.tgz", + "integrity": "sha512-vW1GmwMZNnL+gMRaovlh9yZX74kc+TTU3FObkkurpMaRtBfLP3ldjS9KQWlwZgraRE0+dheEEoAxdzcJQ8eXZg==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/fetch": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@protobufjs/fetch/-/fetch-1.1.1.tgz", + "integrity": "sha512-GpptLrs57adMSuHi3VNj0mAF8dwh36LMaYF6XyJ6JMWlVsc+t42tm1HSEDmOs3A8fC9yyeisgLhsTVQokOZ0zw==", + "license": "BSD-3-Clause", + "dependencies": { + "@protobufjs/aspromise": "^1.1.1" + } + }, + "node_modules/@protobufjs/float": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@protobufjs/float/-/float-1.0.2.tgz", + "integrity": "sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/path": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/path/-/path-1.1.2.tgz", + "integrity": "sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/pool": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/pool/-/pool-1.1.0.tgz", + "integrity": "sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/utf8": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.2.tgz", + "integrity": "sha512-b1UQwcEZ4yCnMCD8DAL1VlbvBJE9/IX4FTIp7BG1xYpf29SLazLSrqUkj4w7Y5y7cCVP6E5tcqqcI0xemPkHug==", + "license": "BSD-3-Clause" + }, "node_modules/@radix-ui/number": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@radix-ui/number/-/number-1.1.3.tgz", @@ -2909,7 +3553,6 @@ "version": "26.6.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.6.tgz", "integrity": "sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog==", - "dev": true, "license": "MIT", "dependencies": { "undici-types": "~8.9.0" @@ -3363,6 +4006,15 @@ "node": ">=10.0.0" } }, + "node_modules/adm-zip": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.6.1.tgz", + "integrity": "sha512-Xwrja8nx9e5o2N1my4DsKCeKpdrnACyr1wtbPxBDgGzKzKyE9kRtBFA8mWldI+RVlD7CBZNWY/wQ2+ydwOR6kQ==", + "license": "MIT", + "engines": { + "node": ">=14.0" + } + }, "node_modules/ansi-regex": { "version": "6.6.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.6.0.tgz", @@ -3642,6 +4294,40 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/define-data-property": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", + "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==", + "license": "MIT", + "dependencies": { + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", + "gopd": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/define-properties": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.1.tgz", + "integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==", + "license": "MIT", + "dependencies": { + "define-data-property": "^1.0.1", + "has-property-descriptors": "^1.0.0", + "object-keys": "^1.1.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/dequal": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", @@ -3655,7 +4341,6 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", - "dev": true, "license": "Apache-2.0", "engines": { "node": ">=8" @@ -3716,6 +4401,24 @@ "node": ">=10.13.0" } }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/es-module-lexer": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.2.tgz", @@ -3886,6 +4589,12 @@ "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", "license": "MIT" }, + "node_modules/flatbuffers": { + "version": "25.9.23", + "resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-25.9.23.tgz", + "integrity": "sha512-MI1qs7Lo4Syw0EOzUl0xjs2lsoeqFku44KpngfIduHBYvzm8h2+7K8YMQh1JtVVVrUvhLpNwqVi4DERegUJhPQ==", + "license": "Apache-2.0" + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -3933,6 +4642,49 @@ "node": ">=6" } }, + "node_modules/global-agent": { + "version": "4.1.7", + "resolved": "https://registry.npmjs.org/global-agent/-/global-agent-4.1.7.tgz", + "integrity": "sha512-pxaZqdsMwPA4i7TuRKpDUONm2xGRjvYdRoidCQz7VleUKVuUfh/4p5Q3QDTGrpAE5Kz5TxxcZ+uqEgTkoKRNtg==", + "license": "BSD-3-Clause", + "dependencies": { + "globalthis": "^1.0.2", + "matcher": "^4.0.0", + "semver": "^7.3.5", + "serialize-error": "^8.1.0" + }, + "engines": { + "node": ">=10.0" + } + }, + "node_modules/globalthis": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/globalthis/-/globalthis-1.0.4.tgz", + "integrity": "sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ==", + "license": "MIT", + "dependencies": { + "define-properties": "^1.2.1", + "gopd": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/graceful-fs": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", @@ -3940,6 +4692,24 @@ "dev": true, "license": "ISC" }, + "node_modules/guid-typescript": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/guid-typescript/-/guid-typescript-1.0.9.tgz", + "integrity": "sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==", + "license": "ISC" + }, + "node_modules/has-property-descriptors": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", + "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==", + "license": "MIT", + "dependencies": { + "es-define-property": "^1.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/hast-util-to-jsx-runtime": { "version": "2.3.7", "resolved": "https://registry.npmjs.org/hast-util-to-jsx-runtime/-/hast-util-to-jsx-runtime-2.3.7.tgz", @@ -4404,6 +5174,12 @@ "url": "https://opencollective.com/parcel" } }, + "node_modules/long": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", + "integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==", + "license": "Apache-2.0" + }, "node_modules/longest-streak": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.1.0.tgz", @@ -4482,6 +5258,33 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/matcher": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/matcher/-/matcher-4.0.0.tgz", + "integrity": "sha512-S6x5wmcDmsDRRU/c2dkccDwQPXoFczc5+HpQ2lON8pnvHlnvHAHj5WlLVvw6n6vNyHuVugYrFohYxbS+pvFpKQ==", + "license": "MIT", + "dependencies": { + "escape-string-regexp": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/matcher/node_modules/escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/mdast-util-find-and-replace": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.4.tgz", @@ -5365,6 +6168,15 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, + "node_modules/object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/obug": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", @@ -5379,6 +6191,49 @@ "node": ">=12.20.0" } }, + "node_modules/onnxruntime-common": { + "version": "1.30.0", + "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.30.0.tgz", + "integrity": "sha512-7fdVWjAID1dVhH/G8qK3APARunV4VkBFoCQAP7qp4Wkab0mrorvmc+sqiT+mKXOzDqdjN5j+/Z9nb4gzNPWcyA==", + "license": "MIT" + }, + "node_modules/onnxruntime-node": { + "version": "1.30.0", + "resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.30.0.tgz", + "integrity": "sha512-twhs1C2C/BFkz1yc5OY0KIU2GUq6DURO7hD4bx5Q2Qy3nAMJwRXW8xU3NVczE29VA9lolLOYepoD8fjTGOfIqw==", + "hasInstallScript": true, + "license": "MIT", + "os": [ + "win32", + "darwin", + "linux" + ], + "dependencies": { + "adm-zip": "^0.6.0", + "global-agent": "^4.1.3", + "onnxruntime-common": "1.30.0" + } + }, + "node_modules/onnxruntime-web": { + "version": "1.31.0-dev.20260914-8d85527a0", + "resolved": "https://registry.npmjs.org/onnxruntime-web/-/onnxruntime-web-1.31.0-dev.20260914-8d85527a0.tgz", + "integrity": "sha512-Iy7rtadoBgxS/LLvDr3QW38DB1PNXRnr0GJMcL0TAt7c9qjgVQl83UlGCVyeAnK2InpmW8Uc3PL8XIuqtDeF6g==", + "license": "MIT", + "dependencies": { + "flatbuffers": "^25.1.24", + "guid-typescript": "^1.0.9", + "long": "^5.2.3", + "onnxruntime-common": "1.31.0-dev.20260911-2a43ec07e", + "platform": "^1.3.6", + "protobufjs": "^7.2.4" + } + }, + "node_modules/onnxruntime-web/node_modules/onnxruntime-common": { + "version": "1.31.0-dev.20260911-2a43ec07e", + "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.31.0-dev.20260911-2a43ec07e.tgz", + "integrity": "sha512-gBuF6U32YErKIAt+yD7DeGBpjRxhJ1uJwako6ygjokSZULDU6Pd+KWComX8Tumk1hYVHRxjPj7mdMnD0ryAPOw==", + "license": "MIT" + }, "node_modules/option": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/option/-/option-0.2.4.tgz", @@ -5469,6 +6324,12 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/platform": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz", + "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==", + "license": "MIT" + }, "node_modules/postcss": { "version": "8.5.32", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.32.tgz", @@ -5514,6 +6375,29 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/protobufjs": { + "version": "7.6.6", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.6.tgz", + "integrity": "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==", + "hasInstallScript": true, + "license": "BSD-3-Clause", + "dependencies": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.5", + "@protobufjs/eventemitter": "^1.1.1", + "@protobufjs/fetch": "^1.1.1", + "@protobufjs/float": "^1.0.2", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.1", + "@types/node": ">=13.7.0", + "long": "^5.3.2" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/radix-ui": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/radix-ui/-/radix-ui-1.8.1.tgz", @@ -5875,12 +6759,88 @@ "integrity": "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw==", "license": "MIT" }, + "node_modules/semver": { + "version": "7.8.6", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.6.tgz", + "integrity": "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/serialize-error": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/serialize-error/-/serialize-error-8.1.0.tgz", + "integrity": "sha512-3NnuWfM6vBYoy5gZFvHiYsVbafvI9vZv/+jlIigFn4oP4zjNPK3LhcY0xSCgeb1a5L8jO71Mit9LlNoi2UfDDQ==", + "license": "MIT", + "dependencies": { + "type-fest": "^0.20.2" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/setimmediate": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", "license": "MIT" }, + "node_modules/sharp": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.35.5.tgz", + "integrity": "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g==", + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.1.0", + "detect-libc": "^2.1.2", + "semver": "^7.8.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.35.5", + "@img/sharp-darwin-x64": "0.35.5", + "@img/sharp-freebsd-wasm32": "0.35.5", + "@img/sharp-libvips-darwin-arm64": "1.3.4", + "@img/sharp-libvips-darwin-x64": "1.3.4", + "@img/sharp-libvips-linux-arm": "1.3.4", + "@img/sharp-libvips-linux-arm64": "1.3.4", + "@img/sharp-libvips-linux-ppc64": "1.3.4", + "@img/sharp-libvips-linux-riscv64": "1.3.4", + "@img/sharp-libvips-linux-s390x": "1.3.4", + "@img/sharp-libvips-linux-x64": "1.3.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4", + "@img/sharp-libvips-linuxmusl-x64": "1.3.4", + "@img/sharp-linux-arm": "0.35.5", + "@img/sharp-linux-arm64": "0.35.5", + "@img/sharp-linux-ppc64": "0.35.5", + "@img/sharp-linux-riscv64": "0.35.5", + "@img/sharp-linux-s390x": "0.35.5", + "@img/sharp-linux-x64": "0.35.5", + "@img/sharp-linuxmusl-arm64": "0.35.5", + "@img/sharp-linuxmusl-x64": "0.35.5", + "@img/sharp-webcontainers-wasm32": "0.35.5", + "@img/sharp-win32-arm64": "0.35.5", + "@img/sharp-win32-ia32": "0.35.5", + "@img/sharp-win32-x64": "0.35.5" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, "node_modules/shell-quote": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.12.0.tgz", @@ -6162,6 +7122,18 @@ "fsevents": "~2.3.3" } }, + "node_modules/type-fest": { + "version": "0.20.2", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", + "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", + "license": "(MIT OR CC0-1.0)", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/typescript": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", @@ -6207,7 +7179,6 @@ "version": "8.9.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz", "integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==", - "dev": true, "license": "MIT" }, "node_modules/unified": { diff --git a/package.json b/package.json index 4fe52ae..b1f830b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@fontsource-variable/jetbrains-mono": "^5.3.0", "@fontsource-variable/newsreader": "^5.3.0", "@hono/node-server": "^2.1.6", + "@huggingface/transformers": "^4.3.2", "@tanstack/react-query": "^5.107.1", "clsx": "^2.1.1", "fast-xml-parser": "^5.12.0", diff --git a/server/ai/assistant.ts b/server/ai/assistant.ts index f63652c..7980874 100644 --- a/server/ai/assistant.ts +++ b/server/ai/assistant.ts @@ -11,7 +11,7 @@ import { z } from 'zod'; import type { DraftCard, DraftQuestion } from '../../shared/content.ts'; import { draftsFromValue } from '../../shared/importer.ts'; import type { DB } from '../db.ts'; -import { badRequest, notFound, type AppContext } from '../http.ts'; +import { badRequest, HttpError, notFound, type AppContext } from '../http.ts'; import { getAppSettings } from '../settings.ts'; import { createAttempt, missedQuestionIds } from '../services/attempts.ts'; import { createClass, createFolder, getClass, getDeck, getQuiz, listCards, listQuestions, resolveFolder } from '../services/content.ts'; @@ -52,6 +52,16 @@ export type AssistantEvent = export interface AssistantContext { classId?: string | null; documentIds?: string[]; + /** Materials the user tagged with @ in this message. */ + items?: TaggedItem[]; +} + +export const TAG_KINDS = ['quiz', 'deck', 'note', 'document', 'folder'] as const; +export type TagKind = (typeof TAG_KINDS)[number]; +export interface TaggedItem { + kind: TagKind; + id: string; + title: string; } interface ToolRun { @@ -483,7 +493,7 @@ const TOOLS: ToolSpec[] = [ type: 'array', items: obj( { - type: { type: 'string', enum: ['single', 'multi'] }, + type: { type: 'string', enum: ['single', 'multi', 'numeric'], description: 'numeric = the student types a number (calculations); its options hold only the correct answer, e.g. "12.5 mL".' }, stem: S, options: { type: 'array', items: obj({ text: S, correct: { type: 'boolean' }, why: S }, ['text', 'correct']) }, rationale: S, @@ -744,6 +754,8 @@ interface MessageRow { interface MessageMeta { hidden?: boolean; toolResults?: boolean; + /** Materials tagged in a user message. */ + tags?: TaggedItem[]; tools?: ToolActivity[]; ui?: UiItem[]; model?: string; @@ -754,6 +766,7 @@ export interface DisplayMessage { id: number; role: 'user' | 'assistant'; text: string; + tags?: TaggedItem[]; tools: ToolActivity[]; ui: UiItem[]; model?: string; @@ -773,7 +786,7 @@ export function displayMessages(db: DB, conversationId: string): DisplayMessage[ const text = msg.content.filter((p) => p.type === 'text').map((p) => (p as { text: string }).text).join(''); if (row.role === 'user') { current = null; - out.push({ id: row.id, role: 'user', text, tools: [], ui: [], createdAt: row.created_at }); + out.push({ id: row.id, role: 'user', text, tools: [], ui: [], createdAt: row.created_at, ...(meta.tags?.length ? { tags: meta.tags } : {}) }); continue; } if (!current) { @@ -857,6 +870,20 @@ function systemPrompt(app: AppContext, context: AssistantContext): string { const docs = db.all<{ id: string; title: string }>(`SELECT id, title FROM documents WHERE id IN (${context.documentIds.map(() => '?').join(',')})`, ...context.documentIds); if (docs.length) lines.push(`The user has these documents in focus: ${docs.map((d) => `"${d.title}" (id ${d.id})`).join(', ')}. Questions like "summarise this" refer to them.`); } + if (context.items?.length) { + const how: Record = { + quiz: 'open it with get_quiz', + deck: 'open it with get_deck', + note: 'read it with read_note', + document: 'search it with search_documents or read it with read_document; pass its id as a source when generating', + folder: 'list its contents with list_materials', + }; + lines.push( + `In their latest message the user tagged these materials, so "this", "these" and similar words refer to them. Use the ids directly instead of searching for them:\n${context.items + .map((i) => `- ${i.kind} "${i.title}" — id ${i.id}${TAG_URL[i.kind] ? `, link ${TAG_URL[i.kind]}${i.id}` : ''} (${how[i.kind]})`) + .join('\n')}`, + ); + } lines.push( `How to work: - Use tools instead of guessing. To answer questions about their material, search or read the documents first and base the answer on what you find. @@ -877,10 +904,131 @@ function systemPrompt(app: AppContext, context: AssistantContext): string { export const assistantRequestSchema = z.object({ conversationId: z.string().nullish(), message: z.string().trim().min(1).max(20_000), - context: z.object({ classId: z.string().nullish(), documentIds: z.array(z.string()).max(20).optional() }).optional(), + context: z + .object({ + classId: z.string().nullish(), + documentIds: z.array(z.string()).max(20).optional(), + items: z.array(z.object({ kind: z.enum(TAG_KINDS), id: z.string().max(60) })).max(20).optional(), + }) + .optional(), modelId: z.string().nullish(), }); +/** App paths for linking tagged items in answers (folders have no page of their own). */ +const TAG_URL: Record = { quiz: '/quizzes/', deck: '/decks/', note: '/notes/', document: '/documents/', folder: '' }; + +const TAG_TABLE: Record = { + quiz: { table: 'quizzes', title: 'title' }, + deck: { table: 'decks', title: 'title' }, + note: { table: 'notes', title: 'title' }, + document: { table: 'documents', title: 'title' }, + folder: { table: 'folders', title: 'name' }, +}; + +/** Looks tagged items up so the model (and the transcript) get their real titles; unknown ids are dropped. */ +function resolveTags(db: DB, items: { kind: TagKind; id: string }[] | undefined): TaggedItem[] { + const out: TaggedItem[] = []; + for (const i of items ?? []) { + const meta = TAG_TABLE[i.kind]; + const row = db.get<{ title: string }>(`SELECT ${meta.title} AS title FROM ${meta.table} WHERE id = ?`, i.id); + if (row && !out.some((o) => o.kind === i.kind && o.id === i.id)) out.push({ kind: i.kind, id: i.id, title: row.title }); + } + return out; +} + +/** Library items the user can tag with @: one class's materials, or a title search across everything. */ +export function mentionables(db: DB, opts: { classId?: string | null; q?: string }): (TaggedItem & { className: string; classColor: string; folderName?: string })[] { + const q = (opts.q ?? '').trim().slice(0, 80); + const like = `%${q.replace(/[\\%_]/g, (m) => `\\${m}`)}%`; + const out: (TaggedItem & { className: string; classColor: string; folderName?: string })[] = []; + const limit = opts.classId ? 300 : 12; + for (const kind of TAG_KINDS) { + const meta = TAG_TABLE[kind]; + const where = [opts.classId ? 't.class_id = ?' : '1 = 1', q ? `t.${meta.title} LIKE ? ESCAPE '\\'` : '1 = 1']; + const params = [...(opts.classId ? [opts.classId] : []), ...(q ? [like] : [])]; + const folderJoin = kind === 'folder' ? '' : 'LEFT JOIN folders f ON f.id = t.folder_id'; + const folderCol = kind === 'folder' ? "'' AS folder_name" : 'f.name AS folder_name'; + const rows = db.all<{ id: string; title: string; class_name: string; class_color: string; folder_name: string | null }>( + `SELECT t.id, t.${meta.title} AS title, k.name AS class_name, k.color AS class_color, ${folderCol} + FROM ${meta.table} t JOIN classes k ON k.id = t.class_id ${folderJoin} + WHERE ${where.join(' AND ')} ${kind === 'document' ? "AND t.status = 'ready'" : ''} + ORDER BY t.updated_at DESC LIMIT ?`, + ...params, + limit, + ); + for (const r of rows) out.push({ kind, id: r.id, title: r.title, className: r.class_name, classColor: r.class_color, ...(r.folder_name ? { folderName: r.folder_name } : {}) }); + } + return out; +} + +/* ------------------------------ running turns ------------------------------ */ + +/** + * A turn keeps running on the server when the browser disconnects (the user leaves the + * page, closes the drawer, switches to another conversation). Its events are buffered so a + * client that comes back can replay them and follow along; only "Stop" cancels it. + */ +interface LiveTurn { + events: AssistantEvent[]; + listeners: Set<(event: AssistantEvent) => void>; + controller: AbortController; +} +const live = new Map(); + +export function isTurnRunning(conversationId: string): boolean { + return live.has(conversationId); +} + +/** Replays the running turn's events, then streams the rest. Returns an unsubscribe function, or null when nothing is running. */ +export function followTurn(conversationId: string, listener: (event: AssistantEvent) => void): (() => void) | null { + const turn = live.get(conversationId); + if (!turn) return null; + for (const e of turn.events) listener(e); + turn.listeners.add(listener); + return () => turn.listeners.delete(listener); +} + +export function stopTurn(conversationId: string): boolean { + const turn = live.get(conversationId); + if (!turn) return false; + turn.controller.abort(); + return true; +} + +/** + * Starts a turn in the background and returns its conversation id at once. Problems that + * prevent the turn from starting (no model configured, unknown model, a turn already + * running in this conversation) throw here, before anything is saved. + */ +export function startAssistantTurn(app: AppContext, input: z.input): { conversationId: string } { + const req = assistantRequestSchema.parse(input); + if (req.conversationId && live.has(req.conversationId)) throw new HttpError(409, 'busy', 'The assistant is still answering in this conversation. Wait for it or press Stop.'); + resolveModel(app, 'chat', req.modelId); + const controller = new AbortController(); + const turn: LiveTurn = { events: [], listeners: new Set(), controller }; + let id = ''; + const emit = (event: AssistantEvent) => { + turn.events.push(event); + for (const fn of turn.listeners) fn(event); + }; + const started = runAssistantTurn(app, req, emit, controller.signal, (conversationId) => { + id = conversationId; + live.set(conversationId, turn); + }); + void started + .catch((err) => { + const ai = err instanceof AIError ? err : null; + if (!ai) app.log('error', 'Assistant turn failed', err); + emit({ type: 'error', code: ai?.code ?? 'server', message: ai?.message ?? 'The assistant stopped unexpectedly.' }); + emit({ type: 'done', conversationId: id, title: '' }); + }) + .finally(() => { + if (id && live.get(id) === turn) live.delete(id); + }); + if (!id) throw new AIError('server', 'The assistant could not start.'); + return { conversationId: id }; +} + const MAX_STEPS = 12; const TOOL_OUTPUT_LIMIT = 28_000; @@ -890,10 +1038,16 @@ export async function runAssistantTurn( input: z.input, emit: (event: AssistantEvent) => void, signal?: AbortSignal, + /** Called synchronously, before the first await, with the conversation id. */ + onConversation?: (conversationId: string) => void, ): Promise { const { db } = app; const req = assistantRequestSchema.parse(input); - const context: AssistantContext = { classId: req.context?.classId ?? null, documentIds: req.context?.documentIds }; + const tags = resolveTags(db, req.context?.items); + // Tagged documents join the documents in focus so retrieval and generation use them. + const taggedDocs = tags.filter((t) => t.kind === 'document').map((t) => t.id); + const documentIds = [...new Set([...(req.context?.documentIds ?? []), ...taggedDocs])]; + const context: AssistantContext = { classId: req.context?.classId ?? null, documentIds: documentIds.length ? documentIds : undefined, items: tags }; const resolved = resolveModel(app, 'chat', req.modelId); const at = now(); @@ -912,8 +1066,9 @@ export async function runAssistantTurn( const save = (role: 'user' | 'assistant', message: ChatMessage, meta: MessageMeta = {}) => db.run('INSERT INTO messages (conversation_id, role, content, meta, created_at) VALUES (?, ?, ?, ?, ?)', conversationId, role, JSON.stringify(message), JSON.stringify(meta), now()); - save('user', { role: 'user', content: [{ type: 'text', text: req.message }] }); + save('user', { role: 'user', content: [{ type: 'text', text: req.message }] }, tags.length ? { tags } : {}); db.run('UPDATE conversations SET updated_at = ? WHERE id = ?', now(), conversationId); + onConversation?.(conversationId); emit({ type: 'start', conversationId, model: resolved.model.label }); const env: ToolEnv = { app, conversationId, context, signal, modelId: req.modelId }; diff --git a/server/ai/generate.ts b/server/ai/generate.ts index 025265c..c2bb2c3 100644 --- a/server/ai/generate.ts +++ b/server/ai/generate.ts @@ -39,6 +39,7 @@ import { import { chunkById, formatExcerpts, searchChunks, selectContext, sourceLabel, type ChunkHit } from '../services/documents.ts'; import { hybridSearch } from '../services/semantic.ts'; import { weakTopics } from '../services/stats.ts'; +import { getSetting, setSetting } from '../settings.ts'; import { newId, now, parseJson } from '../util.ts'; import { generateStructured, resolveModel, runChat } from './service.ts'; import { AIError } from './types.ts'; @@ -297,10 +298,15 @@ const QUESTION_SCHEMA = { items: { type: 'object', properties: { - type: { type: 'string', enum: ['single', 'multi'], description: 'single = one correct answer; multi = select all that apply' }, + type: { + type: 'string', + enum: ['single', 'multi', 'numeric'], + description: 'single = one correct answer; multi = select all that apply; numeric = a calculation the student answers by typing the number', + }, stem: { type: 'string', description: 'The question. Markdown allowed.' }, options: { type: 'array', + description: 'Answer choices. For numeric questions: exactly one option, the correct answer with its unit (e.g. "12.5 mL"), correct = true, and "why" showing the calculation.', items: { type: 'object', properties: { @@ -343,6 +349,8 @@ const CARD_SCHEMA = { required: ['cards'], }; +const NUMERIC_RULE = `- Calculation items (dosages, weight-based doses, IV and drip rates, conversions, intake and output) use type "numeric": the student types the number instead of choosing. Give exactly one option, the correct answer with its unit (for example "12.5 mL"), marked correct, with "why" showing the working. State the unit and the rounding rule in the stem ("Round to the nearest tenth."). Only the number is checked, so the student may leave out the unit.`; + const NCLEX_RULES = `Write NCLEX-style (Next Generation NCLEX) items: - Test clinical judgment at the application and analysis level: recognise cues, analyse, prioritise, choose and evaluate actions. Avoid pure recall. - Open with a brief, realistic client scenario containing only relevant data (age, diagnosis, key findings, vital signs or lab values with units). @@ -351,18 +359,21 @@ const NCLEX_RULES = `Write NCLEX-style (Next Generation NCLEX) items: - Every distractor must be plausible to a student with partial knowledge and wrong for a specific reason. Keep choices parallel in grammar and similar in length. Never use "all of the above" or "none of the above". Do not give the answer away through wording or length. - Multiple-choice items have exactly 4 choices with exactly one best answer. Select-all-that-apply items have 5 or 6 choices with 2 to 5 correct, and the stem ends with "Select all that apply." - Use generic drug names. Use current, standard practice. -- The rationale teaches: state why the correct answer is correct, name the principle behind it, and give each choice its own "why".`; +- The rationale teaches: state why the correct answer is correct, name the principle behind it, and give each choice its own "why". +${NUMERIC_RULE}`; const STANDARD_RULES = `Write clear exam-quality questions: - Each question tests one idea and has one defensible answer set. - Mix understanding and application; avoid trivia and trick wording. - Distractors are plausible and parallel. Never use "all of the above" or "none of the above". - Multiple-choice items have 4 choices with exactly one correct. Select-all items have 4 to 6 choices with at least 2 correct. -- The rationale explains the correct answer, and each choice gets its own "why".`; +- The rationale explains the correct answer, and each choice gets its own "why". +${NUMERIC_RULE}`; const RECALL_RULES = `Write straightforward recall questions that check key facts, definitions, normal values and classifications. - Multiple-choice items have 4 choices with exactly one correct. Select-all items have 4 to 6 choices with at least 2 correct. -- Keep stems short and unambiguous. Each choice gets a brief "why".`; +- Keep stems short and unambiguous. Each choice gets a brief "why". +${NUMERIC_RULE}`; const CARD_RULES: Record = { concise: 'Each card tests exactly one fact. Fronts are specific questions or terms; backs are short (a phrase or one or two lines).', @@ -380,9 +391,71 @@ const NOTE_RULES: Record = { cheat_sheet: 'Write a one-page cheat sheet in Markdown: dense, scannable, organised under short headings, using tables for values, drug classes and comparisons, and bold for must-know facts.', }; -function baseSystem(req: GenerateRequest, hasSources: boolean): string { +const BASE_ROLE = 'You are an expert educator writing study material for a nursing student. Accuracy is critical: this material is used to prepare for licensing exams.'; + +/* ------------------------------ editable instructions ------------------------------ */ + +/** + * The instructions the built-in generator (Generate and the assistant) sends to the model. + * Every entry can be replaced in Settings → Templates; an absent value means the built-in + * wording. These are separate from the instructions for outside chatbots + * (`LLM_INSTRUCTIONS`), which also have to describe the import JSON format. + */ +export const GENERATION_INSTRUCTIONS = { + base: { group: 'All content', label: 'Role', text: BASE_ROLE }, + 'quiz.nclex': { group: 'Quiz questions', label: 'NCLEX style', text: NCLEX_RULES }, + 'quiz.standard': { group: 'Quiz questions', label: 'Standard exam style', text: STANDARD_RULES }, + 'quiz.recall': { group: 'Quiz questions', label: 'Quick recall', text: RECALL_RULES }, + 'deck.concise': { group: 'Flashcards', label: 'Concise', text: CARD_RULES.concise }, + 'deck.detailed': { group: 'Flashcards', label: 'Detailed', text: CARD_RULES.detailed }, + 'deck.medication': { group: 'Flashcards', label: 'Medication', text: CARD_RULES.medication }, + 'deck.definition': { group: 'Flashcards', label: 'Term and definition', text: CARD_RULES.definition }, + 'note.study_guide': { group: 'Study guides', label: 'Study guide', text: NOTE_RULES.study_guide }, + 'note.summary': { group: 'Study guides', label: 'Summary', text: NOTE_RULES.summary }, + 'note.outline': { group: 'Study guides', label: 'Outline', text: NOTE_RULES.outline }, + 'note.cheat_sheet': { group: 'Study guides', label: 'Cheat sheet', text: NOTE_RULES.cheat_sheet }, +} as const; +export type InstructionKey = keyof typeof GENERATION_INSTRUCTIONS; +const INSTRUCTIONS_SETTING = 'ai.instructions'; + +export function instructionOverrides(db: DB): Partial> { + return getSetting>>(db, INSTRUCTIONS_SETTING, {}); +} + +export function instruction(db: DB, key: InstructionKey): string { + const own = instructionOverrides(db)[key]; + return own?.trim() ? own : GENERATION_INSTRUCTIONS[key].text; +} + +export function listInstructions(db: DB) { + const own = instructionOverrides(db); + return (Object.keys(GENERATION_INSTRUCTIONS) as InstructionKey[]).map((key) => ({ + key, + group: GENERATION_INSTRUCTIONS[key].group, + label: GENERATION_INSTRUCTIONS[key].label, + text: own[key]?.trim() ? own[key]! : GENERATION_INSTRUCTIONS[key].text, + builtin: GENERATION_INSTRUCTIONS[key].text, + customized: Boolean(own[key]?.trim()), + })); +} + +export const instructionPatchSchema = z.object({ + key: z.enum(Object.keys(GENERATION_INSTRUCTIONS) as [InstructionKey, ...InstructionKey[]]), + /** null restores the built-in wording. */ + text: z.string().trim().min(10).max(20_000).nullable(), +}); + +export function saveInstruction(db: DB, input: z.input): void { + const { key, text } = instructionPatchSchema.parse(input); + const next = { ...instructionOverrides(db) }; + if (text === null || text === GENERATION_INSTRUCTIONS[key].text) delete next[key]; + else next[key] = text; + setSetting(db, INSTRUCTIONS_SETTING, next); +} + +function baseSystem(db: DB, req: GenerateRequest, hasSources: boolean): string { const lines = [ - 'You are an expert educator writing study material for a nursing student. Accuracy is critical: this material is used to prepare for licensing exams.', + instruction(db, 'base'), hasSources ? 'Base everything on the provided source material. Do not introduce facts that contradict it. You may rely on standard, well-established knowledge only to write plausible distractors and clear explanations.' : 'No source material was provided, so rely on current, well-established knowledge. If something is uncertain or varies by institution, prefer the most widely taught standard.', @@ -598,8 +671,8 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ const notes: string[] = []; const style = req.style ?? 'nclex'; - const rules = isQuiz ? (style === 'nclex' ? NCLEX_RULES : style === 'recall' ? RECALL_RULES : STANDARD_RULES) : CARD_RULES[req.cardStyle ?? 'concise']; - const system = `${baseSystem(req, hasSources)}\n\n${rules}`; + const rules = isQuiz ? instruction(db, `quiz.${style}`) : instruction(db, `deck.${req.cardStyle ?? 'concise'}`); + const system = `${baseSystem(db, req, hasSources)}\n\n${rules}`; const sata = req.sataShare ?? (style === 'nclex' ? 0.3 : 0.2); const difficulty = req.difficulty ?? 'mixed'; @@ -609,7 +682,8 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ const ask = isQuiz ? [ `Write ${want} ${style === 'nclex' ? 'NCLEX-style ' : ''}questions${req.focus ? ` about: ${req.focus}` : ''}.`, - sata <= 0 ? 'Use only multiple-choice (single) questions.' : sata >= 1 ? 'Use only select-all-that-apply (multi) questions.' : `About ${sataCount} of them should be select-all-that-apply (multi); the rest multiple choice (single).`, + sata <= 0 ? 'Use only multiple-choice (single) questions, apart from calculations.' : sata >= 1 ? 'Use only select-all-that-apply (multi) questions, apart from calculations.' : `About ${sataCount} of them should be select-all-that-apply (multi); the rest multiple choice (single).`, + 'If the material involves calculations (dosages, IV rates, conversions), write those as numeric questions the student answers by typing the number.', difficulty === 'mixed' ? 'Mix difficulty: mostly medium, with a few easy and a few hard.' : `Difficulty: ${difficulty}.`, sources.groups.length > 0 ? 'Spread the questions across the excerpts instead of clustering on one. Set "source" to the id of the excerpt each question is based on.' : '', req.source.deckId ? 'Turn the flashcards into questions: use each card\'s answer as the correct choice and write distractors that a student could confuse with it.' : '', @@ -703,7 +777,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque const hasSources = sources.groups.length > 0 || Boolean(sources.inline); if (!hasSources && !req.focus) throw badRequest('Choose source material or describe a topic to write about.'); const title = defaultTitle(app, req, sources); - const system = `${baseSystem(req, hasSources)}\n\n${NOTE_RULES[style]}\n\n${ + const system = `${baseSystem(db, req, hasSources)}\n\n${instruction(db, `note.${style}`)}\n\n${ sources.groups.length > 0 ? 'Cite the excerpts you draw on by putting their id in brackets at the end of the relevant line or paragraph, e.g. [c42]. Use only ids that appear in the source.' : '' }\nStart directly with the content (no preamble, no title line).`; @@ -740,7 +814,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque task: 'generate', purpose: 'study guide', modelId: req.modelId, - system: `${baseSystem(req, true)}\n\nCondense this part of the material into detailed notes: every key concept, value, medication, cause, sign, intervention and warning, as Markdown bullet points under short headings. Keep the bracketed excerpt ids (e.g. [c42]) next to the points they support. Do not add outside information.`, + system: `${baseSystem(db, req, true)}\n\nCondense this part of the material into detailed notes: every key concept, value, medication, cause, sign, intervention and warning, as Markdown bullet points under short headings. Keep the bracketed excerpt ids (e.g. [c42]) next to the points they support. Do not add outside information.`, messages: [{ role: 'user', content: [{ type: 'text', text: `${req.focus ? `Focus on: ${req.focus}\n\n` : ''}SOURCE EXCERPTS (part ${i + 1} of ${passes.length}):\n\n${formatExcerpts(passes[i])}` }] }], maxOutputTokens: 6_000, signal, diff --git a/server/ai/localEmbed.ts b/server/ai/localEmbed.ts new file mode 100644 index 0000000..7bc2e7b --- /dev/null +++ b/server/ai/localEmbed.ts @@ -0,0 +1,101 @@ +/** + * Built-in embedding model, used for semantic search when no cloud embedding provider is + * configured. Runs on the server's CPU through ONNX Runtime (transformers.js), so it needs no + * API key and costs nothing. + * + * Model: BAAI bge-small-en-v1.5 (ONNX, 8-bit quantised, about 34 MB, 384 dimensions). It is a + * strong small retrieval model; queries get BGE's recommended instruction prefix. The files + * are downloaded from Hugging Face on first use into DATA_DIR/models and reused afterwards. + * + * The library is imported lazily: the app starts (and tests run) without loading ONNX Runtime + * until something actually needs an embedding. + */ +import fs from 'node:fs'; +import path from 'node:path'; +import { AIError } from './types.ts'; + +export const LOCAL_PROVIDER_ID = 'local'; +export const LOCAL_EMBEDDING_MODEL = 'Xenova/bge-small-en-v1.5'; +export const LOCAL_EMBEDDING_LABEL = 'bge-small-en-v1.5 (built in)'; +export const LOCAL_EMBEDDING_DIMS = 384; +const QUERY_PREFIX = 'Represent this sentence for searching relevant passages: '; +/** Inputs per forward pass: small enough to keep memory flat on a home server. */ +const BATCH = 16; +/** About 512 tokens, the model's window. Longer input is truncated by the tokenizer anyway. */ +const MAX_CHARS = 2000; + +type Extractor = (texts: string[], opts: { pooling: 'cls' | 'mean'; normalize: boolean }) => Promise<{ tolist(): number[][] }>; + +const state: { extractor: Promise | null; cacheDir: string; status: 'idle' | 'loading' | 'ready' | 'error'; error: string } = { + extractor: null, + cacheDir: '', + status: 'idle', + error: '', +}; + +export function localModelDir(dataDir: string): string { + return path.join(dataDir, 'models'); +} + +/** True when the model files are already on disk (no download needed). */ +export function localModelDownloaded(dataDir: string): boolean { + const dir = path.join(localModelDir(dataDir), ...LOCAL_EMBEDDING_MODEL.split('/')); + try { + return fs.existsSync(path.join(dir, 'onnx', 'model_quantized.onnx')) && fs.existsSync(path.join(dir, 'tokenizer.json')); + } catch { + return false; + } +} + +export function localModelStatus(dataDir: string) { + return { model: LOCAL_EMBEDDING_MODEL, label: LOCAL_EMBEDDING_LABEL, dimensions: LOCAL_EMBEDDING_DIMS, downloaded: localModelDownloaded(dataDir), status: state.status, error: state.error }; +} + +async function load(dataDir: string): Promise { + const cacheDir = localModelDir(dataDir); + if (state.extractor && state.cacheDir === cacheDir) return state.extractor; + state.cacheDir = cacheDir; + state.status = 'loading'; + state.error = ''; + state.extractor = (async () => { + fs.mkdirSync(cacheDir, { recursive: true }); + let lib: typeof import('@huggingface/transformers'); + try { + lib = await import('@huggingface/transformers'); + } catch (err) { + throw new AIError('unsupported', 'The built-in embedding model is not available in this installation.', { cause: err }); + } + lib.env.cacheDir = cacheDir; + lib.env.allowRemoteModels = true; + lib.env.allowLocalModels = true; + lib.env.localModelPath = cacheDir; + const extractor = await lib.pipeline('feature-extraction', LOCAL_EMBEDDING_MODEL, { dtype: 'q8' }); + return extractor as unknown as Extractor; + })(); + try { + const ready = await state.extractor; + state.status = 'ready'; + return ready; + } catch (err) { + state.extractor = null; + state.status = 'error'; + const message = err instanceof AIError ? err.message : `The built-in embedding model could not be loaded: ${err instanceof Error ? err.message : String(err)}`; + state.error = message; + throw err instanceof AIError ? err : new AIError('network', `${message}. It is downloaded once (about 35 MB) from huggingface.co; check that the server can reach the internet.`, { cause: err, retryable: true }); + } +} + +/** Embeds texts with the built-in model. Vectors come back L2-normalised. */ +export async function localEmbed(dataDir: string, texts: string[], purpose: 'document' | 'query', signal?: AbortSignal): Promise { + const extractor = await load(dataDir); + const out: number[][] = []; + for (let i = 0; i < texts.length; i += BATCH) { + if (signal?.aborted) throw new AIError('aborted', 'The request was cancelled.'); + const batch = texts.slice(i, i + BATCH).map((t) => (purpose === 'query' ? QUERY_PREFIX : '') + (t.slice(0, MAX_CHARS) || ' ')); + const tensor = await extractor(batch, { pooling: 'cls', normalize: true }); + out.push(...tensor.tolist()); + // Let the event loop serve requests between batches; indexing runs in the background. + await new Promise((resolve) => setImmediate(resolve)); + } + return out; +} diff --git a/server/ai/providers/anthropic.ts b/server/ai/providers/anthropic.ts index 9b28c01..f2dc592 100644 --- a/server/ai/providers/anthropic.ts +++ b/server/ai/providers/anthropic.ts @@ -38,7 +38,7 @@ import { type TestResult, } from '../types.ts'; import { lookupModelDefaults } from './catalog.ts'; -import { isChatModelId, makeUsage, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; +import { isChatModelId, makeUsage, nearestEffort, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; import { ensureOk, errorFromProvider, httpRequest, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext } from './http.ts'; type Json = Record; @@ -120,7 +120,10 @@ export function claudeProfile(model: string): ClaudeProfile { } /** Thinking budgets for the legacy `budget_tokens` models. */ -const LEGACY_BUDGET = { low: 2_048, medium: 8_192, high: 16_384 } as const; +const LEGACY_BUDGET = { minimal: 1_024, low: 2_048, medium: 8_192, high: 16_384, xhigh: 24_576, max: 32_000 } as const; + +/** `output_config.effort` levels. "xhigh" exists on the newest models only; a rejection steps it down. */ +const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'] as const; /* ------------------------------------------------------------------ */ /* Request building */ @@ -133,6 +136,8 @@ interface BuildOptions { useFallbacks: boolean; eagerToolInput: boolean; promptCache: boolean; + /** Highest effort level the model accepted, after a rejection. */ + effortCap?: (typeof CLAUDE_EFFORTS)[number]; } /** Tool-call ids must match ^[a-zA-Z0-9_-]+$; ids minted by other providers may not. */ @@ -268,7 +273,7 @@ function buildBody(req: ChatRequest, profile: ClaudeProfile, opts: BuildOptions) // `display: "summarized"` because these models hide the reasoning text by // default, which would look like a long pause in the UI. body.thinking = adaptive; - if (effort) outputConfig.effort = effort; + if (effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS); } else if (effort) { // Forced tool use cannot be combined with thinking on the older generations. if (profile.thinking === 'opt_in' && !forcing) body.thinking = adaptive; @@ -276,7 +281,7 @@ function buildBody(req: ChatRequest, profile: ClaudeProfile, opts: BuildOptions) const budget = Math.min(LEGACY_BUDGET[effort], maxTokens - 1_024); // must be < max_tokens, min 1024 if (budget >= 1_024) body.thinking = { type: 'enabled', budget_tokens: budget }; } - if (profile.effort) outputConfig.effort = effort; + if (profile.effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS); } if (Object.keys(outputConfig).length > 0) body.output_config = outputConfig; } @@ -375,6 +380,7 @@ async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator = { 'gemini-2.5-flash-lite': { label: 'Gemini 2.5 Flash-Lite', priceIn: 0.1, priceOut: 0.4, capabilities: GEMINI_CAPS }, }; +/* ------------------------------------------------------------------ */ +/* DeepSeek */ +/* Prices and limits: https://api-docs.deepseek.com/quick_start/pricing (fetched 2026-11-15) +/* Peak-hour list prices are stored. Off-peak hours (everything outside 01:00-04:00 and +/* 06:00-10:00 UTC, Monday to Friday) cost half; `priceMultiplier` applies that per call. +/* deepseek-v4-flash / deepseek-v4-flash-vision-exp are retired aliases served by +/* deepseek-flash at the Flash price. +/* ------------------------------------------------------------------ */ + +const DS_LIMITS = { contextWindow: 1_048_576, maxOutput: 393_216 }; + +const DEEPSEEK: Record = { + 'deepseek-flash': { label: 'DeepSeek V4.1 Flash', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } }, + 'deepseek-v4-flash': { label: 'DeepSeek V4 Flash (alias of Flash)', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } }, + 'deepseek-v4-pro': { label: 'DeepSeek V4 Pro', ...DS_LIMITS, priceIn: 1.32, priceCachedIn: 0.044, priceOut: 3.96, capabilities: { tools: true, vision: false, pdf: false, reasoning: true, temperature: true } }, +}; + const TABLES: Record> = { openai: OPENAI, anthropic: ANTHROPIC, gemini: GEMINI, + deepseek: DEEPSEEK, // Aggregators and local servers host arbitrary models at their own prices. openai_compatible: {}, ollama: {}, @@ -168,6 +186,7 @@ function lookupIn(table: Record, id: string): Partial/" (`anthropic/claude-sonnet-5-5`). // Borrow capabilities and limits from the vendor's table, never its prices. const bare = id.slice(id.lastIndexOf('/') + 1).replace(/:[a-z]+$/, ''); - for (const table of [OPENAI, ANTHROPIC, GEMINI]) { + for (const table of [OPENAI, ANTHROPIC, GEMINI, DEEPSEEK]) { const hit = lookupIn(table, bare); if (Object.keys(hit).length > 0) { delete hit.priceIn; delete hit.priceOut; + delete hit.priceCachedIn; // Temperature limits are properties of the first-party APIs, not of the router. if (hit.capabilities) delete hit.capabilities.temperature; return hit; @@ -204,3 +224,51 @@ export function lookupModelDefaults(kind: ProviderKind, modelId: string): Partia export function catalogModelIds(kind: ProviderKind): string[] { return Object.keys(TABLES[kind]); } + +/** + * What a cache-hit input token costs when a model has no cached price of its own, as a + * fraction of the normal input price. These are the providers' published discounts for + * their current models: OpenAI GPT-5 and later, Claude cache reads and Gemini 2.5 and later + * all bill cached input at 10%. Older OpenAI models (GPT-4.1 25%, GPT-4o / o-series 50%) + * discount less. Servers behind the compatible adapter and Ollama have no known discount. + */ +export function cachedInputRatio(kind: ProviderKind, modelId: string): number { + const id = modelId.slice(modelId.lastIndexOf('/') + 1); + switch (kind) { + case 'openai': + if (/^gpt-4\.1/.test(id)) return 0.25; + if (/^(gpt-4o|o\d)/.test(id)) return 0.5; + return 0.1; + case 'anthropic': + case 'gemini': + return 0.1; + case 'deepseek': + return 0.02; + default: + return 1; + } +} + +/** USD per million cached input tokens: the model's own price when set, otherwise the provider discount. */ +export function cachedInputPrice(kind: ProviderKind, modelId: string, priceIn: number | null, priceCachedIn: number | null): number { + if (priceCachedIn !== null && priceCachedIn !== undefined) return priceCachedIn; + return (priceIn ?? 0) * cachedInputRatio(kind, modelId); +} + +/** Cache writes: Claude bills writing the (5-minute) prompt cache at 1.25x the input price. */ +export function cacheWriteRatio(kind: ProviderKind): number { + return kind === 'anthropic' ? 1.25 : 1; +} + +/** + * Time-of-day pricing. DeepSeek charges half its list price outside peak hours + * (01:00-04:00 and 06:00-10:00 UTC, Monday to Friday). + */ +export function priceMultiplier(kind: ProviderKind, at: number = Date.now()): number { + if (kind !== 'deepseek') return 1; + const d = new Date(at); + const day = d.getUTCDay(); + const hour = d.getUTCHours(); + const peak = day >= 1 && day <= 5 && ((hour >= 1 && hour < 4) || (hour >= 6 && hour < 10)); + return peak ? 1 : 0.5; +} diff --git a/server/ai/providers/common.ts b/server/ai/providers/common.ts index 5b9c5aa..13fed7a 100644 --- a/server/ai/providers/common.ts +++ b/server/ai/providers/common.ts @@ -4,7 +4,7 @@ * connection-test wrapper. */ import { jsonrepair } from 'jsonrepair'; -import { AIError, type DiscoveredModel, type ProviderKind, type TestResult, type Usage } from '../types.ts'; +import { AIError, REASONING_EFFORTS, type DiscoveredModel, type ProviderKind, type ReasoningEffort, type TestResult, type Usage } from '../types.ts'; import { lookupModelDefaults } from './catalog.ts'; import { isRecord } from './http.ts'; @@ -151,3 +151,22 @@ export async function runTest(label: string, check: () => Promise<{ modelCount?: /** Timeout used by `test()` so a dead endpoint fails fast instead of hanging the settings page. */ export const TEST_TIMEOUT_MS = 20_000; + +/** + * Maps a requested effort onto the closest level a provider accepts. Ties go to the higher + * level, so "medium" on a low/high-only API becomes "high" rather than silently weaker. + */ +export function nearestEffort(effort: ReasoningEffort, allowed: readonly T[]): T { + const rank = (e: string) => REASONING_EFFORTS.indexOf(e as ReasoningEffort); + const want = rank(effort); + let best = allowed[0]; + let bestDistance = Number.POSITIVE_INFINITY; + for (const level of allowed) { + const d = Math.abs(rank(level) - want); + if (d < bestDistance || (d === bestDistance && rank(level) > rank(best))) { + best = level; + bestDistance = d; + } + } + return best; +} diff --git a/server/ai/providers/deepseek.ts b/server/ai/providers/deepseek.ts new file mode 100644 index 0000000..ba24cb7 --- /dev/null +++ b/server/ai/providers/deepseek.ts @@ -0,0 +1,112 @@ +/** + * DeepSeek — Chat Completions at https://api.deepseek.com. + * https://api-docs.deepseek.com/api/create-chat-completion/ + * https://api-docs.deepseek.com/guides/thinking_mode/ + * + * The wire format is OpenAI-compatible, so this reuses the compatible adapter's engine with + * the differences DeepSeek documents: + * - Thinking is ON by default. `thinking: {type: "disabled"}` turns it off; + * `reasoning_effort` accepts none / low / high / max. + * - In thinking mode `tool_choice: "required"` and named tool choices are rejected with a + * 400. Structured generation keeps thinking on (answer keys are noticeably more accurate + * with it) and asks for the tool call in the prompt with `tool_choice: "auto"`. Only when + * the effort is set to "none" is thinking off, and then the tool choice is forced. + * - When tools are present, the `reasoning_content` of every earlier assistant turn must be + * sent back, or the API answers 400. It is kept in `providerData` and replayed. + * - Usage reports `prompt_cache_hit_tokens`; cache hits are billed at a much lower rate. + * - `/models` lists context window, output limit, input modalities and effort levels. + */ +import { AIError, type ChatRequest, type DiscoveredModel, type ModelCapabilities, type ProviderAdapter, type ProviderConfig, type TestResult } from '../types.ts'; +import { isChatModelId, nearestEffort, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; +import { isRecord, requestJson, trimBaseUrl } from './http.ts'; +import { streamChatCompletions, type CompatOptions } from './openaiCompatible.ts'; + +export const DEEPSEEK_BASE_URL = 'https://api.deepseek.com'; +const LABEL = 'DeepSeek'; +const EFFORTS = ['low', 'high', 'max'] as const; + +function base(cfg: ProviderConfig): string { + return trimBaseUrl(cfg.baseUrl?.trim() || DEEPSEEK_BASE_URL); +} + +/** "none" turns thinking off; a model marked as not reasoning is treated the same way. */ +function effortOf(req: ChatRequest): ChatRequest['reasoningEffort'] { + return req.capabilities?.reasoning === false ? 'none' : req.reasoningEffort; +} + +function options(cfg: ProviderConfig): CompatOptions { + if (!cfg.apiKey) throw new AIError('not_configured', `Add your DeepSeek API key for "${cfg.name}" in Settings.`); + return { + kind: 'deepseek', + label: cfg.name || LABEL, + // DeepSeek serves the same API at the root and under /v1; the explicit /v1 keeps the + // compatible engine from guessing. + baseUrl: `${base(cfg)}/v1`, + apiKey: cfg.apiKey, + headers: cfg.headers, + replayReasoning: true, + forcedToolChoice: (req) => effortOf(req) === 'none', + adjustBody: (body, req) => { + const effort = effortOf(req); + delete body.reasoning_effort; + if (effort === 'none' || body.tool_choice === 'required' || isRecord(body.tool_choice)) { + // Non-thinking mode (also the only mode that accepts a forced tool choice). + body.thinking = { type: 'disabled' }; + } else { + body.thinking = { type: 'enabled' }; + if (effort) body.reasoning_effort = nearestEffort(effort, EFFORTS); + // Sampling parameters have no effect in thinking mode; leave them out. + delete body.temperature; + } + }, + }; +} + +async function listModels(cfg: ProviderConfig, signal?: AbortSignal, timeoutMs?: number): Promise { + const o = options(cfg); + const res = await requestJson(`${base(cfg)}/models`, { + provider: o.label, + secrets: [o.apiKey, ...Object.values(o.headers ?? {})], + headers: { ...o.headers, Authorization: `Bearer ${o.apiKey}` }, + signal, + firstByteTimeoutMs: timeoutMs, + totalTimeoutMs: timeoutMs, + }); + const list = isRecord(res) && Array.isArray(res.data) ? res.data : []; + const found: DiscoveredModel[] = []; + for (const m of list) { + if (!isRecord(m)) continue; + const id = str(m.id); + if (!id || !isChatModelId(id)) continue; + const model: DiscoveredModel = { id }; + if (str(m.name) && str(m.name) !== id) model.label = str(m.name); + if (num(m.context_window)) model.contextWindow = num(m.context_window); + if (num(m.max_output_tokens)) model.maxOutput = num(m.max_output_tokens); + const caps: Partial = { tools: true, pdf: false }; + if (Array.isArray(m.input_modalities)) caps.vision = m.input_modalities.includes('image'); + if (isRecord(m.effort)) caps.reasoning = true; + model.capabilities = caps; + found.push(model); + } + return withCatalogDefaults('deepseek', found); +} + +async function test(cfg: ProviderConfig): Promise { + return runTest(cfg.name || LABEL, async () => ({ modelCount: (await listModels(cfg, undefined, TEST_TIMEOUT_MS)).length })); +} + +export const deepseekAdapter: ProviderAdapter = { + kind: 'deepseek', + meta: { + label: 'DeepSeek', + defaultBaseUrl: DEEPSEEK_BASE_URL, + requiresApiKey: true, + requiresBaseUrl: false, + supportsDiscovery: true, + docsUrl: 'https://platform.deepseek.com/api_keys', + keyHint: 'Starts with sk-', + }, + stream: (cfg, req) => streamChatCompletions(options(cfg), req), + listModels: (cfg, signal) => listModels(cfg, signal), + test, +}; diff --git a/server/ai/providers/gemini.ts b/server/ai/providers/gemini.ts index d834fa5..5fc8878 100644 --- a/server/ai/providers/gemini.ts +++ b/server/ai/providers/gemini.ts @@ -29,7 +29,7 @@ import { type EmbedRequest, type EmbedResult, type StreamEvent, type TestResult, } from '../types.ts'; -import { isChatModelId, makeUsage, newToolCallId, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; +import { isChatModelId, makeUsage, nearestEffort, newToolCallId, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; import { ensureOk, errorFromProvider, httpRequest, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext, type HttpResponse } from './http.ts'; type Json = Record; @@ -140,7 +140,8 @@ function thinkingStyleFor(model: string): ThinkingStyle { return 'off'; } -const BUDGETS = { none: 0, low: 2_048, medium: 8_192, high: 24_576 } as const; +const BUDGETS = { none: 0, minimal: 512, low: 2_048, medium: 8_192, high: 24_576, xhigh: 32_768, max: 32_768 } as const; +const GEMINI_LEVELS = ['minimal', 'low', 'medium', 'high'] as const; function buildBody(req: ChatRequest, thinking: ThinkingStyle, jsonMode: boolean): Json { const caps = req.capabilities ?? {}; @@ -165,7 +166,7 @@ function buildBody(req: ChatRequest, thinking: ThinkingStyle, jsonMode: boolean) const effort = req.reasoningEffort; if (thinking === 'level') { const cfg: Json = { includeThoughts: true }; - if (effort) cfg.thinkingLevel = effort === 'none' ? 'minimal' : effort; + if (effort) cfg.thinkingLevel = nearestEffort(effort, GEMINI_LEVELS); gen.thinkingConfig = cfg; } else if (effort) { // 2.5 Pro cannot switch thinking off; its minimum budget is 128 tokens. diff --git a/server/ai/providers/index.ts b/server/ai/providers/index.ts index 01ca1e4..0aab24b 100644 --- a/server/ai/providers/index.ts +++ b/server/ai/providers/index.ts @@ -1,5 +1,6 @@ import { AIError, type ProviderAdapter, type ProviderKind } from '../types.ts'; import { anthropicAdapter } from './anthropic.ts'; +import { deepseekAdapter } from './deepseek.ts'; import { geminiAdapter } from './gemini.ts'; import { ollamaAdapter } from './ollama.ts'; import { openaiAdapter } from './openai.ts'; @@ -9,6 +10,7 @@ const ADAPTERS: Record = { openai: openaiAdapter, anthropic: anthropicAdapter, gemini: geminiAdapter, + deepseek: deepseekAdapter, openai_compatible: openaiCompatibleAdapter, ollama: ollamaAdapter, }; @@ -23,4 +25,4 @@ export function listAdapterMeta(): Array<{ kind: ProviderKind } & ProviderAdapte return (Object.keys(ADAPTERS) as ProviderKind[]).map((kind) => ({ kind, ...ADAPTERS[kind].meta })); } -export { CATALOG_UPDATED, lookupModelDefaults } from './catalog.ts'; +export { CATALOG_UPDATED, cachedInputPrice, cacheWriteRatio, lookupModelDefaults, priceMultiplier } from './catalog.ts'; diff --git a/server/ai/providers/openai.ts b/server/ai/providers/openai.ts index ce78ff8..5a3e4ba 100644 --- a/server/ai/providers/openai.ts +++ b/server/ai/providers/openai.ts @@ -103,6 +103,9 @@ function toInput(messages: ChatMessage[], caps: Partial, drop return input; } +/** The next value to try when a model rejects a reasoning effort. */ +const OPENAI_EFFORT_FALLBACK: Record = { none: 'minimal', minimal: 'low', low: null, medium: null, high: null, xhigh: 'high' }; + interface Flags { /** Value sent as reasoning.effort, or null to omit the field. */ effort: string | null; @@ -139,8 +142,9 @@ function buildBody(req: ChatRequest, flags: Flags): Json { async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator { const ctx = errorContext(cfg); const flags: Flags = { - // "none" is only understood by recent models; if it is rejected we fall back to "low". - effort: req.reasoningEffort ?? null, + // OpenAI's top level is "xhigh". "none" and "xhigh" are only understood by recent + // models; a rejected value steps towards "medium" and is finally omitted. + effort: req.reasoningEffort ? (req.reasoningEffort === 'max' ? 'xhigh' : req.reasoningEffort) : null, summary: true, temperature: true, dropReasoning: false, @@ -156,7 +160,7 @@ async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator= 5) throw err; const detail = (err.providerMessage ?? '').toLowerCase(); - if (flags.effort && /reasoning\.effort|reasoning_effort|effort/.test(detail)) flags.effort = flags.effort === 'none' ? 'minimal' : flags.effort === 'minimal' ? 'low' : null; + if (flags.effort && /reasoning\.effort|reasoning_effort|effort/.test(detail)) flags.effort = OPENAI_EFFORT_FALLBACK[flags.effort] ?? null; else if (flags.summary && /summary/.test(detail)) flags.summary = false; else if (flags.include && /include|encrypted_content/.test(detail)) flags.include = false; else if (flags.temperature && /temperature/.test(detail)) flags.temperature = false; diff --git a/server/ai/providers/openaiCompatible.ts b/server/ai/providers/openaiCompatible.ts index d8b543d..b3fbaf2 100644 --- a/server/ai/providers/openaiCompatible.ts +++ b/server/ai/providers/openaiCompatible.ts @@ -27,10 +27,10 @@ import { type EmbedRequest, type EmbedResult, type StreamEvent, type TestResult, } from '../types.ts'; -import { parseEmbeddings, dataUrl, isChatModelId, makeUsage, newToolCallId, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; +import { parseEmbeddings, dataUrl, isChatModelId, makeUsage, nearestEffort, newToolCallId, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; import { ensureOk, errorFromProvider, hostOf, httpRequest, isLocalUrl, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext, type HttpResponse } from './http.ts'; -type Json = Record; +export type Json = Record; /** Accepts ".../v1", ".../v1/", ".../v1/chat/completions" or a bare host and returns the API root. */ export function normalizeCompatBase(url: string): string { @@ -51,8 +51,26 @@ export interface CompatOptions { baseUrl: string; apiKey: string | null; headers?: Record; + /** + * Server-specific request tweaks (DeepSeek's `thinking` switch, its effort levels...). + * Called on the finished body before it is sent. + */ + adjustBody?: (body: Json, req: ChatRequest) => void; + /** + * The server requires the reasoning text of earlier assistant turns to be sent back + * (`reasoning_content`), as DeepSeek does in thinking mode with tools. + */ + replayReasoning?: boolean; + /** + * Whether `required` / named tool choices may be sent for this request. When false the + * tool stays available with `auto` and the system prompt asks for the call instead. + */ + forcedToolChoice?: (req: ChatRequest) => boolean; } +/** Levels sent as `reasoning_effort` by default. "max" has no OpenAI-style equivalent. */ +const COMPAT_EFFORTS = ['minimal', 'low', 'medium', 'high', 'xhigh'] as const; + function userContent(parts: ContentPart[], caps: Partial): string | Json[] { const blocks: Json[] = []; for (const p of parts) { @@ -72,7 +90,7 @@ function userContent(parts: ContentPart[], caps: Partial): st return blocks; } -export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'developer' = 'system'): Json[] { +export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'developer' = 'system', replay?: { kind: ProviderKind }): Json[] { const caps = req.capabilities ?? {}; const out: Json[] = []; if (req.system?.trim()) out.push({ role: systemRole, content: req.system.trim() }); @@ -82,6 +100,12 @@ export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'develop const calls = msg.content.filter((p) => p.type === 'tool_call') as Extract[]; if (!text && calls.length === 0) continue; const wire: Json = { role: 'assistant', content: text || null }; + if (replay) { + // Same server: hand back its reasoning verbatim. A turn written by another provider + // has none, which these servers accept as an empty string. + const pd = msg.providerData; + wire.reasoning_content = pd?.kind === replay.kind && isRecord(pd.data) && typeof pd.data.reasoning_content === 'string' ? pd.data.reasoning_content : ''; + } if (calls.length > 0) { wire.tool_calls = calls.map((c) => ({ id: c.id, type: 'function', function: { name: c.name, arguments: JSON.stringify(c.input ?? {}) } })); } @@ -103,6 +127,8 @@ export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'develop interface BodyFlags { tools: boolean; + /** Send `required` / named tool choices as such; false = ask for the call in the prompt instead. */ + forcedChoice: boolean; usage: boolean; jsonMode: boolean; temperature: boolean; @@ -110,23 +136,40 @@ interface BodyFlags { maxKey: 'max_tokens' | 'max_completion_tokens' | null; } -function buildBody(req: ChatRequest, flags: BodyFlags): Json { +function forcedToolNote(choice: ChatRequest['toolChoice']): string | null { + if (choice === 'required') return 'For this turn you must call one of the available tools. Begin your response with the tool call.'; + if (isRecord(choice)) return `For this turn you must call the \`${choice.name}\` tool. Begin your response with that tool call and do not answer in plain text.`; + return null; +} + +function buildBody(req: ChatRequest, flags: BodyFlags, o: CompatOptions): Json { const caps = req.capabilities ?? {}; - const body: Json = { model: req.model, messages: toChatMessages(req), stream: true }; + const sendTools = flags.tools && caps.tools !== false && Boolean(req.tools?.length); + const note = sendTools && !flags.forcedChoice ? forcedToolNote(req.toolChoice) : null; + const effective = note ? { ...req, system: [req.system?.trim(), note].filter(Boolean).join('\n\n') } : req; + const body: Json = { model: req.model, messages: toChatMessages(effective, 'system', o.replayReasoning ? { kind: o.kind } : undefined), stream: true }; if (flags.usage) body.stream_options = { include_usage: true }; - if (flags.tools && caps.tools !== false && req.tools?.length) { - body.tools = req.tools.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: { type: 'object', ...t.parameters } } })); + if (sendTools) { + body.tools = req.tools!.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: { type: 'object', ...t.parameters } } })); const choice = req.toolChoice; - if (choice === 'none' || choice === 'required' || choice === 'auto') body.tool_choice = choice; - else if (isRecord(choice)) body.tool_choice = { type: 'function', function: { name: choice.name } }; + if (choice === 'none' || choice === 'auto') body.tool_choice = choice; + else if (choice === 'required') body.tool_choice = flags.forcedChoice ? 'required' : 'auto'; + else if (isRecord(choice)) body.tool_choice = flags.forcedChoice ? { type: 'function', function: { name: choice.name } } : 'auto'; } if (flags.temperature && req.temperature !== undefined && caps.temperature !== false) body.temperature = req.temperature; if (flags.maxKey && req.maxOutputTokens) body[flags.maxKey] = req.maxOutputTokens; - if (flags.reasoning && caps.reasoning === true && req.reasoningEffort && req.reasoningEffort !== 'none') body.reasoning_effort = req.reasoningEffort; + if (flags.reasoning && caps.reasoning === true && req.reasoningEffort && req.reasoningEffort !== 'none') body.reasoning_effort = nearestEffort(req.reasoningEffort, COMPAT_EFFORTS); if (flags.jsonMode && req.jsonMode) body.response_format = { type: 'json_object' }; + if (o.adjustBody) { + o.adjustBody(body, req); + if (!flags.reasoning) delete body.reasoning_effort; + } return body; } +/** A 400 that says the model cannot do tool calling at all (not just a rejected parameter). */ +const NO_TOOLS_RE = /(does not|doesn't|do not|cannot|can't|not) support(s|ed)? (tool|function)|(tool|function)[ _-]?(call(ing|s)?|use)? (is|are) not (supported|available|enabled)|unsupported.{0,20}(tool|function)|no tool support/; + function mapFinish(reason: string): StopReason { switch (reason) { case 'stop': @@ -160,14 +203,15 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) const ctx = errorContext(o); const base = normalizeCompatBase(o.baseUrl); const local = isLocalUrl(base); - const flags: BodyFlags = { tools: true, usage: true, jsonMode: true, temperature: true, reasoning: true, maxKey: 'max_tokens' }; + const flags: BodyFlags = { tools: true, forcedChoice: o.forcedToolChoice ? o.forcedToolChoice(req) : true, usage: true, jsonMode: true, temperature: true, reasoning: true, maxKey: 'max_tokens' }; + const forcing = req.toolChoice === 'required' || isRecord(req.toolChoice); let http: HttpResponse; for (let attempt = 0; ; attempt += 1) { http = await httpRequest(`${base}/chat/completions`, { ...ctx, headers: authHeaders(o), - body: buildBody(req, flags), + body: buildBody(req, flags, o), signal: req.signal, // A local server may spend minutes loading a model into memory before the first byte. firstByteTimeoutMs: local ? 10 * 60_000 : undefined, @@ -184,9 +228,12 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) else if (flags.reasoning && /reasoning_effort|reasoning/.test(detail) && req.reasoningEffort) flags.reasoning = false; else if (flags.temperature && /temperature/.test(detail)) flags.temperature = false; else if (flags.jsonMode && req.jsonMode && /response_format|json_object|json mode/.test(detail)) flags.jsonMode = false; - else if (flags.tools && req.tools?.length && /tool|function/.test(detail)) { + // A forced tool choice is often what is rejected (DeepSeek in thinking mode, some + // routers): keep the tools and ask for the call in the prompt instead. + else if (flags.forcedChoice && forcing && flags.tools && req.tools?.length && /tool_choice|tool choice|required|function_call|named/.test(detail)) flags.forcedChoice = false; + else if (flags.tools && req.tools?.length && NO_TOOLS_RE.test(detail)) { // The model (or server) cannot do tool calling at all. - if (req.toolChoice === 'required' || isRecord(req.toolChoice)) { + if (forcing) { throw new AIError('unsupported', `${o.label} reports that "${req.model}" does not support tool calling. Pick a different model for this task.`, { status: err.status, providerMessage: err.providerMessage, @@ -200,6 +247,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) let text = ''; let finish = ''; let reasoningSeen = false; + let reasoningText = ''; const usage = { input: 0, output: 0, cacheRead: 0, reasoning: 0 }; const calls: { id: string; name: string; args: string }[] = []; @@ -211,6 +259,8 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) usage.input = num(u.prompt_tokens) || usage.input; usage.output = num(u.completion_tokens) || usage.output; if (isRecord(u.prompt_tokens_details)) usage.cacheRead = num(u.prompt_tokens_details.cached_tokens); + // DeepSeek's own field (the same number as cached_tokens where both are sent). + if (!usage.cacheRead && num(u.prompt_cache_hit_tokens)) usage.cacheRead = num(u.prompt_cache_hit_tokens); if (isRecord(u.completion_tokens_details)) usage.reasoning = num(u.completion_tokens_details.reasoning_tokens); } const choice = Array.isArray(data.choices) ? data.choices[0] : undefined; @@ -222,6 +272,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) const reasoning = str(delta.reasoning_content) || str(delta.reasoning) || str(delta.thinking); if (reasoning) { reasoningSeen = true; + if (o.replayReasoning) reasoningText += reasoning; yield { type: 'reasoning', delta: reasoning }; } const piece = typeof delta.content === 'string' ? delta.content : ''; @@ -270,6 +321,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest) throw new AIError('invalid_response', `${o.label} returned an empty response.`, { retryable: true }); } const message: ChatMessage = { role: 'assistant', content }; + if (o.replayReasoning) message.providerData = { kind: o.kind, data: { reasoning_content: reasoningText } }; yield { type: 'done', message, diff --git a/server/ai/providers/providers.test.ts b/server/ai/providers/providers.test.ts index 2b4887d..1dc3cbd 100644 --- a/server/ai/providers/providers.test.ts +++ b/server/ai/providers/providers.test.ts @@ -6,7 +6,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; import { AIError, type ChatMessage, type ChatRequest, type ProviderAdapter, type ProviderConfig, type ProviderKind, type StreamEvent, type ToolDef, type Usage } from '../types.ts'; import { anthropicAdapter } from './anthropic.ts'; -import { lookupModelDefaults } from './catalog.ts'; +import { cachedInputPrice, lookupModelDefaults, priceMultiplier } from './catalog.ts'; +import { nearestEffort } from './common.ts'; +import { deepseekAdapter } from './deepseek.ts'; import { toGeminiSchema, geminiAdapter } from './gemini.ts'; import { parseSSE } from './http.ts'; import { ollamaAdapter } from './ollama.ts'; @@ -751,6 +753,139 @@ describe('openai-compatible stream_options fallback', () => { }); }); +/** A Chat Completions stream that calls one tool, with optional reasoning text first. */ +function compatToolStream(name: string, args: string, reasoning?: string): string[] { + return [ + ...(reasoning ? [ev({ choices: [{ index: 0, delta: { reasoning_content: reasoning } }] })] : []), + ev({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'call_9', type: 'function', function: { name, arguments: args } }] } }] }), + ev({ choices: [{ index: 0, delta: {}, finish_reason: 'tool_calls' }], usage: { prompt_tokens: 100, completion_tokens: 5, prompt_cache_hit_tokens: 60 } }), + DONE, + ]; +} + +describe('openai-compatible forced tool choice', () => { + it('asks for the call in the prompt when a forced tool_choice is rejected, instead of reporting no tool support', async () => { + stubFetch( + queue( + () => jsonResponse({ error: { message: 'deepseek-flash does not support this tool_choice in thinking mode', type: 'invalid_request_error' } }, 400), + () => sseResponse(compatToolStream('lookup', '{"q":"cats"}')), + ), + ); + const { done } = await run(compatKit.adapter, compatKit.cfg, { model: 'some-model', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' } }); + expect(calls).toHaveLength(2); + expect(calls[0]!.body.tool_choice).toEqual({ type: 'function', function: { name: 'lookup' } }); + expect(calls[1]!.body.tool_choice).toBe('auto'); + expect(calls[1]!.body.tools).toHaveLength(1); + expect(calls[1]!.body.messages[0].content).toContain('must call the `lookup` tool'); + expect(done.message.content).toEqual([{ type: 'tool_call', id: 'call_9', name: 'lookup', input: { q: 'cats' } }]); + }); + + it('only reports "unsupported" when the server says tools are not supported at all', async () => { + stubFetch(queue(() => jsonResponse({ error: { message: 'This model does not support tools.' } }, 400))); + const err = await failure(run(compatKit.adapter, compatKit.cfg, { model: 'tiny', messages: [user('hi')], tools: [TOOL], toolChoice: 'required' })); + expect(err.code).toBe('unsupported'); + }); + + it('does not mistake an unrelated 400 that mentions tools for missing tool support', async () => { + stubFetch(queue(() => jsonResponse({ error: { message: "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'." } }, 400))); + const err = await failure(run(compatKit.adapter, compatKit.cfg, { model: 'm', messages: [user('hi')], tools: [TOOL], toolChoice: 'required' })); + expect(err.code).not.toBe('unsupported'); + }); +}); + +describe('deepseek adapter', () => { + const cfg = cfgOf('deepseek', 'sk-test-key-123456'); + + it('keeps thinking on for forced tool calls and asks for the call in the prompt instead', async () => { + stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"x"}')))); + const { done } = await run(deepseekAdapter, cfg, { model: 'deepseek-flash', system: 'Base', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' }, reasoningEffort: 'high', capabilities: { reasoning: true, tools: true } }); + expect(calls[0]!.url).toBe('https://api.deepseek.com/v1/chat/completions'); + expect(calls[0]!.headers.Authorization).toBe('Bearer sk-test-key-123456'); + expect(calls[0]!.body.thinking).toEqual({ type: 'enabled' }); + expect(calls[0]!.body.reasoning_effort).toBe('high'); + expect(calls[0]!.body.tool_choice).toBe('auto'); + expect(calls[0]!.body.messages[0].content).toMatch(/^Base\n\nFor this turn you must call the `lookup` tool/); + expect(done.message.content[0]).toMatchObject({ type: 'tool_call', name: 'lookup' }); + }); + + it('forces the tool choice only with thinking off (effort "none")', async () => { + stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"x"}')))); + await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' }, reasoningEffort: 'none', capabilities: { reasoning: true, tools: true } }); + expect(calls[0]!.body.thinking).toEqual({ type: 'disabled' }); + expect(calls[0]!.body.reasoning_effort).toBeUndefined(); + expect(calls[0]!.body.tool_choice).toEqual({ type: 'function', function: { name: 'lookup' } }); + }); + + it('maps effort levels onto low / high / max and keeps thinking on', async () => { + for (const [effort, expected] of [['minimal', 'low'], ['medium', 'high'], ['xhigh', 'max'], ['max', 'max']] as const) { + stubFetch(queue(() => sseResponse(compatKit.text(['ok'])))); + await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], reasoningEffort: effort, capabilities: { reasoning: true }, temperature: 0.3 }); + expect(calls[0]!.body.thinking).toEqual({ type: 'enabled' }); + expect(calls[0]!.body.reasoning_effort).toBe(expected); + expect(calls[0]!.body.temperature).toBeUndefined(); + } + stubFetch(queue(() => sseResponse(compatKit.text(['ok'])))); + await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], reasoningEffort: 'none', capabilities: { reasoning: true } }); + expect(calls[0]!.body.thinking).toEqual({ type: 'disabled' }); + }); + + it('keeps reasoning_content and replays it on the next request, with an empty string for other providers', async () => { + stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"cats"}', 'Let me look that up.')))); + const first = await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], tools: [TOOL], toolChoice: 'auto' }); + expect(first.done.message.providerData).toEqual({ kind: 'deepseek', data: { reasoning_content: 'Let me look that up.' } }); + expect(first.done.usage).toMatchObject({ inputTokens: 100, cacheReadTokens: 60 }); + + stubFetch(queue(() => sseResponse(compatKit.text(['done'])))); + const foreign = toolCallMsg({ providerData: { kind: 'openai', data: { x: 1 } } }); + await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi'), first.done.message, toolResultMsg, user('again'), foreign, toolResultMsg], tools: [TOOL], toolChoice: 'auto' }); + const assistants = calls[0]!.body.messages.filter((m: any) => m.role === 'assistant'); + expect(assistants[0].reasoning_content).toBe('Let me look that up.'); + expect(assistants[1].reasoning_content).toBe(''); + }); + + it('lists models with limits, vision and reasoning from /models', async () => { + stubFetch( + queue(() => + jsonResponse({ + data: [ + { id: 'deepseek-flash', name: 'DeepSeek-V4.1-Flash', context_window: 1048576, max_output_tokens: 393216, input_modalities: ['text', 'image'], effort: { supported_levels: ['low', 'high', 'max'] } }, + { id: 'deepseek-v4-pro', name: 'DeepSeek-V4-Pro', context_window: 1048576, max_output_tokens: 393216, input_modalities: ['text'], effort: { supported_levels: ['low', 'high', 'max'] } }, + ], + }), + ), + ); + const models = await deepseekAdapter.listModels(cfg); + expect(calls[0]!.url).toBe('https://api.deepseek.com/models'); + const flash = models.find((m) => m.id === 'deepseek-flash')!; + expect(flash).toMatchObject({ contextWindow: 1048576, maxOutput: 393216, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2 }); + expect(flash.capabilities).toMatchObject({ tools: true, vision: true, reasoning: true }); + expect(models.find((m) => m.id === 'deepseek-v4-pro')!.capabilities?.vision).toBe(false); + }); +}); + +describe('reasoning effort and cache pricing helpers', () => { + it('maps efforts to the nearest supported level, rounding ties up', () => { + expect(nearestEffort('medium', ['low', 'high', 'max'] as const)).toBe('high'); + expect(nearestEffort('minimal', ['low', 'high', 'max'] as const)).toBe('low'); + expect(nearestEffort('xhigh', ['minimal', 'low', 'medium', 'high'] as const)).toBe('high'); + expect(nearestEffort('max', ['low', 'medium', 'high', 'xhigh', 'max'] as const)).toBe('max'); + }); + + it('uses a model cached price when set and the provider discount otherwise', () => { + expect(cachedInputPrice('deepseek', 'deepseek-flash', 0.3, 0.006)).toBe(0.006); + expect(cachedInputPrice('anthropic', 'claude-sonnet-5-5', 2, null)).toBeCloseTo(0.2); + expect(cachedInputPrice('openai', 'gpt-4o', 2.5, null)).toBeCloseTo(1.25); + expect(cachedInputPrice('openai_compatible', 'x', 1, null)).toBe(1); + }); + + it('halves DeepSeek prices outside peak hours only', () => { + expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 16, 2, 30))).toBe(1); // Monday 02:30 UTC + expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 16, 12, 0))).toBe(0.5); // Monday noon + expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 15, 2, 30))).toBe(0.5); // Sunday + expect(priceMultiplier('openai', Date.UTC(2026, 10, 16, 12, 0))).toBe(1); + }); +}); + describe('openai-compatible listModels', () => { it('parses OpenRouter-style metadata, filters embeddings and merges vendor catalog defaults', async () => { stubFetch( diff --git a/server/ai/service.ts b/server/ai/service.ts index 94ccb23..945720f 100644 --- a/server/ai/service.ts +++ b/server/ai/service.ts @@ -10,11 +10,12 @@ import type { DB } from '../db.ts'; import { HttpError, notFound, type AppContext } from '../http.ts'; import { getSetting, setSetting } from '../settings.ts'; import { newId, now, parseJson } from '../util.ts'; -import { getAdapter, lookupModelDefaults } from './providers/index.ts'; +import { cachedInputPrice, cacheWriteRatio, getAdapter, lookupModelDefaults, priceMultiplier } from './providers/index.ts'; import { AIError, DEFAULT_CAPABILITIES, PROVIDER_KINDS, + REASONING_EFFORTS, type ChatMessage, type ChatRequest, type DiscoveredModel, @@ -57,6 +58,7 @@ interface ModelRow { max_output: number | null; price_in: number | null; price_out: number | null; + price_cached_in: number | null; } export const TASKS = ['chat', 'generate', 'vision', 'quick'] as const; @@ -82,7 +84,7 @@ const EMPTY_TASK: TaskConfig = { modelId: null, temperature: null, reasoningEffo export const taskConfigSchema = z.object({ modelId: z.string().nullable(), temperature: z.number().min(0).max(2).nullable(), - reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).nullable(), + reasoningEffort: z.enum(REASONING_EFFORTS).nullable(), maxOutputTokens: z.number().int().min(256).max(200000).nullable(), }); @@ -122,6 +124,8 @@ function mapModel(row: ModelRow & { provider_name?: string; provider_kind?: stri maxOutput: row.max_output, priceIn: row.price_in, priceOut: row.price_out, + /** Null = not set; costs then use the provider's usual cache discount. */ + priceCachedIn: row.price_cached_in, }; } export type ModelDto = ReturnType; @@ -249,11 +253,12 @@ export async function discoverModels(app: AppContext, id: string): Promise<{ add if (current) { // Refresh facts the provider reports, but never overwrite prices or flags the user may have edited. app.db.run( - 'UPDATE ai_models SET context_window = COALESCE(?, context_window), max_output = COALESCE(?, max_output), price_in = COALESCE(price_in, ?), price_out = COALESCE(price_out, ?), updated_at = ? WHERE id = ?', + 'UPDATE ai_models SET context_window = COALESCE(?, context_window), max_output = COALESCE(?, max_output), price_in = COALESCE(price_in, ?), price_out = COALESCE(price_out, ?), price_cached_in = COALESCE(price_cached_in, ?), updated_at = ? WHERE id = ?', m.contextWindow ?? null, m.maxOutput ?? null, m.priceIn ?? null, m.priceOut ?? null, + m.priceCachedIn ?? null, at, current.id, ); @@ -270,8 +275,8 @@ function insertModel(db: DB, providerId: string, m: DiscoveredModel, source: str const id = newId(); const at = now(); db.run( - `INSERT INTO ai_models (id, provider_id, model, label, source, enabled, capabilities, context_window, max_output, price_in, price_out, created_at, updated_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + `INSERT INTO ai_models (id, provider_id, model, label, source, enabled, capabilities, context_window, max_output, price_in, price_out, price_cached_in, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, id, providerId, m.id, @@ -283,6 +288,7 @@ function insertModel(db: DB, providerId: string, m: DiscoveredModel, source: str m.maxOutput ?? null, m.priceIn ?? null, m.priceOut ?? null, + m.priceCachedIn ?? null, at, at, ); @@ -331,6 +337,7 @@ export const modelPatchSchema = z.object({ maxOutput: z.number().int().min(16).max(10_000_000).nullable().optional(), priceIn: z.number().min(0).max(100000).nullable().optional(), priceOut: z.number().min(0).max(100000).nullable().optional(), + priceCachedIn: z.number().min(0).max(100000).nullable().optional(), }); export function patchModel(db: DB, id: string, input: z.input): ModelDto { @@ -339,7 +346,7 @@ export function patchModel(db: DB, id: string, input: z.input>(row.capabilities, {}), ...patch.capabilities }; db.run( - 'UPDATE ai_models SET enabled = ?, label = ?, capabilities = ?, context_window = ?, max_output = ?, price_in = ?, price_out = ?, updated_at = ? WHERE id = ?', + 'UPDATE ai_models SET enabled = ?, label = ?, capabilities = ?, context_window = ?, max_output = ?, price_in = ?, price_out = ?, price_cached_in = ?, updated_at = ? WHERE id = ?', patch.enabled === undefined ? row.enabled : patch.enabled ? 1 : 0, patch.label ?? row.label, JSON.stringify(caps), @@ -347,6 +354,7 @@ export function patchModel(db: DB, id: string, input: z.input, usage: Usage): number | null { +/** + * Estimated USD cost of one call. Input tokens are split into fresh, cache-hit and + * cache-write tokens, each at its own rate: the model's cached price when set, otherwise + * the provider's published cache discount; Claude's cache writes cost 1.25x. Providers + * with time-of-day pricing (DeepSeek off-peak) are adjusted for when the call was made. + */ +export function estimateCost( + model: Pick & { priceCachedIn?: number | null; providerKind?: ProviderKind | '' }, + usage: Usage, + at: number = Date.now(), +): number | null { if (model.priceIn === null && model.priceOut === null) return null; - // Cached input is billed at a fraction of the list price by every provider; 10% is a close, conservative stand-in. - const cached = usage.cacheReadTokens ?? 0; - const fresh = Math.max(0, usage.inputTokens - cached); - return ((fresh + cached * 0.1) * (model.priceIn ?? 0) + usage.outputTokens * (model.priceOut ?? 0)) / 1_000_000; + const kind = (model.providerKind || 'openai_compatible') as ProviderKind; + const priceIn = model.priceIn ?? 0; + const cachedRate = cachedInputPrice(kind, model.model, model.priceIn, model.priceCachedIn ?? null); + const cached = Math.min(usage.inputTokens, usage.cacheReadTokens ?? 0); + const written = Math.min(usage.inputTokens - cached, usage.cacheWriteTokens ?? 0); + const fresh = Math.max(0, usage.inputTokens - cached - written); + const input = fresh * priceIn + cached * cachedRate + written * priceIn * cacheWriteRatio(kind); + return ((input + usage.outputTokens * (model.priceOut ?? 0)) * priceMultiplier(kind, at)) / 1_000_000; } function recordUsage(app: AppContext, r: ResolvedModel, task: string, usage: Usage | null, started: number, error?: string): void { try { app.db.run( - `INSERT INTO ai_usage (at, provider_id, provider_name, model, task, input_tokens, output_tokens, cache_read_tokens, cost, ok, error, duration_ms) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + `INSERT INTO ai_usage (at, provider_id, provider_name, model, task, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost, ok, error, duration_ms) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, now(), r.provider.id, r.provider.name, @@ -444,6 +466,7 @@ function recordUsage(app: AppContext, r: ResolvedModel, task: string, usage: Usa usage?.inputTokens ?? 0, usage?.outputTokens ?? 0, usage?.cacheReadTokens ?? 0, + usage?.cacheWriteTokens ?? 0, usage ? estimateCost(r.model, usage) : null, error ? 0 : 1, (error ?? '').slice(0, 300), @@ -597,7 +620,7 @@ export async function generateStructured(app: AppContext, opts: StructuredOption export function usageSummary(db: DB, days: number) { const since = now() - days * 86_400_000; const byModel = db.all>( - `SELECT provider_name AS provider, model, COUNT(*) AS calls, SUM(input_tokens) AS input, SUM(output_tokens) AS output, SUM(cost) AS cost, + `SELECT provider_name AS provider, model, COUNT(*) AS calls, SUM(input_tokens) AS input, SUM(cache_read_tokens) AS cached, SUM(output_tokens) AS output, SUM(cost) AS cost, SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END) AS errors, SUM(CASE WHEN cost IS NULL AND ok = 1 THEN 1 ELSE 0 END) AS unpriced FROM ai_usage WHERE at >= ? GROUP BY provider_name, model ORDER BY cost DESC, calls DESC`, since, @@ -607,7 +630,7 @@ export function usageSummary(db: DB, days: number) { since, ); const total = db.get>( - `SELECT COUNT(*) AS calls, COALESCE(SUM(input_tokens), 0) AS input, COALESCE(SUM(output_tokens), 0) AS output, COALESCE(SUM(cost), 0) AS cost FROM ai_usage WHERE at >= ?`, + `SELECT COUNT(*) AS calls, COALESCE(SUM(input_tokens), 0) AS input, COALESCE(SUM(cache_read_tokens), 0) AS cached, COALESCE(SUM(output_tokens), 0) AS output, COALESCE(SUM(cost), 0) AS cost FROM ai_usage WHERE at >= ?`, since, )!; const recentErrors = db.all>(`SELECT at, provider_name AS provider, model, task, error FROM ai_usage WHERE ok = 0 AND at >= ? ORDER BY at DESC LIMIT 10`, since); diff --git a/server/ai/types.ts b/server/ai/types.ts index b8fe834..a35178a 100644 --- a/server/ai/types.ts +++ b/server/ai/types.ts @@ -6,9 +6,9 @@ * `server/ai/providers/` may depend on a provider-specific request or response. */ -export type ProviderKind = 'openai' | 'anthropic' | 'gemini' | 'openai_compatible' | 'ollama'; +export type ProviderKind = 'openai' | 'anthropic' | 'gemini' | 'deepseek' | 'openai_compatible' | 'ollama'; -export const PROVIDER_KINDS: ProviderKind[] = ['openai', 'anthropic', 'gemini', 'openai_compatible', 'ollama']; +export const PROVIDER_KINDS: ProviderKind[] = ['openai', 'anthropic', 'gemini', 'deepseek', 'openai_compatible', 'ollama']; /** Decrypted provider connection, only ever held server-side. */ export interface ProviderConfig { @@ -51,7 +51,12 @@ export interface ToolDef { parameters: Record; } -export type ReasoningEffort = 'none' | 'low' | 'medium' | 'high'; +/** + * Reasoning effort, from "off" to the most a provider offers. Adapters map each level onto + * the nearest value their API accepts (see `nearestEffort`). + */ +export const REASONING_EFFORTS = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const; +export type ReasoningEffort = (typeof REASONING_EFFORTS)[number]; export interface ModelCapabilities { tools: boolean; @@ -108,6 +113,8 @@ export interface DiscoveredModel { /** USD per million tokens, when known. */ priceIn?: number; priceOut?: number; + /** USD per million cached (cache-hit) input tokens, when the provider prices them separately. */ + priceCachedIn?: number; } export interface TestResult { diff --git a/server/app.ts b/server/app.ts index 1f2c70a..f78ae85 100644 --- a/server/app.ts +++ b/server/app.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import { setLocalEmbeddingDefault } from './services/semantic.ts'; import path from 'node:path'; import { Hono } from 'hono'; import { compress } from 'hono/compress'; @@ -50,6 +51,7 @@ const AI_STATUS: Partial> = { not_configured: 40 export function createApp(app: AppContext): Hono { const hono = new Hono(); + setLocalEmbeddingDefault(app.config.localEmbeddings); hono.onError((err, c) => { if (err instanceof HttpError) { diff --git a/server/config.ts b/server/config.ts index 1707029..1f5c0fd 100644 --- a/server/config.ts +++ b/server/config.ts @@ -23,6 +23,11 @@ export interface Config { maxUploadBytes: number; backupKeep: number; timezone: string; + /** + * Use the built-in embedding model for semantic search when no embedding provider has been + * chosen. On by default; off in tests. LOCAL_EMBEDDINGS=off disables it. + */ + localEmbeddings: boolean; } function bool(v: string | undefined, fallback: boolean): boolean { @@ -89,6 +94,7 @@ export function loadConfig(overrides: Partial = {}, envVars: NodeJS.Proc maxUploadBytes: Math.max(1, Number(envVars.MAX_UPLOAD_MB ?? 100)) * 1024 * 1024, backupKeep: Math.max(1, Number(envVars.BACKUP_KEEP ?? 7)), timezone, + localEmbeddings: bool(envVars.LOCAL_EMBEDDINGS, env !== 'test'), ...overrides, }; } diff --git a/server/migrations.ts b/server/migrations.ts index 9bf0d88..406f76b 100644 --- a/server/migrations.ts +++ b/server/migrations.ts @@ -380,7 +380,38 @@ CREATE TABLE chunk_vectors ( CREATE INDEX chunk_vectors_model ON chunk_vectors(model_key); `; +/** + * Revision 1: + * - cached-input prices per model and cache-write token counts, for accurate cost estimates; + * - numeric-answer questions ("enter the dose"). SQLite cannot alter a CHECK constraint, so + * the questions table is rebuilt (nothing references it with a foreign key). + */ +const revision1 = ` +ALTER TABLE ai_models ADD COLUMN price_cached_in REAL; +ALTER TABLE ai_usage ADD COLUMN cache_write_tokens INTEGER NOT NULL DEFAULT 0; + +CREATE TABLE questions_new ( + id TEXT PRIMARY KEY, + quiz_id TEXT NOT NULL REFERENCES quizzes(id) ON DELETE CASCADE, + type TEXT NOT NULL CHECK (type IN ('single','multi','numeric')), + stem TEXT NOT NULL, + options TEXT NOT NULL, + rationale TEXT NOT NULL DEFAULT '', + topic TEXT NOT NULL DEFAULT '', + difficulty TEXT NOT NULL DEFAULT 'medium', + tags TEXT NOT NULL DEFAULT '[]', + sort_order INTEGER NOT NULL DEFAULT 0, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL +); +INSERT INTO questions_new SELECT id, quiz_id, type, stem, options, rationale, topic, difficulty, tags, sort_order, created_at, updated_at FROM questions; +DROP TABLE questions; +ALTER TABLE questions_new RENAME TO questions; +CREATE INDEX questions_quiz ON questions(quiz_id, sort_order); +`; + export const MIGRATIONS: Migration[] = [ { id: 1, name: 'init', sql: init }, { id: 2, name: 'embeddings', sql: embeddings }, + { id: 3, name: 'revision1', sql: revision1 }, ]; diff --git a/server/routes/ai.ts b/server/routes/ai.ts index eb227b7..857190a 100644 --- a/server/routes/ai.ts +++ b/server/routes/ai.ts @@ -1,7 +1,7 @@ import { Hono, type Context } from 'hono'; import { streamSSE } from 'hono/streaming'; import { z } from 'zod'; -import { assistantRequestSchema, displayMessages, listConversations, resolveAction, runAssistantTurn, type AssistantEvent } from '../ai/assistant.ts'; +import { assistantRequestSchema, displayMessages, followTurn, isTurnRunning, listConversations, mentionables, resolveAction, startAssistantTurn, stopTurn, type AssistantEvent } from '../ai/assistant.ts'; import { cancelGeneration, createDraft, @@ -10,6 +10,9 @@ import { generateRequestSchema, getDraft, improveRequestSchema, + instructionPatchSchema, + listInstructions, + saveInstruction, listDrafts, saveDraft, startGeneration, @@ -39,7 +42,7 @@ import { } 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'; +import { badRequest, HttpError, idParam, notFound, readBody, type AppContext } from '../http.ts'; type Emit = (event: T) => void; @@ -72,7 +75,7 @@ const contentDraftSchema = z.discriminatedUnion('kind', [ questions: z .array( z.object({ - type: z.enum(['single', 'multi']), + type: z.enum(['single', 'multi', 'numeric']), stem: z.string().max(20000), options: z.array(z.object({ text: z.string().max(4000), correct: z.boolean(), why: z.string().max(20000).optional() })).max(12), rationale: z.string().max(20000).optional().default(''), @@ -103,6 +106,13 @@ export function aiRoutes(app: AppContext): Hono { /* ------------------------------ configuration ------------------------------ */ r.get('/ai/meta', (c) => c.json({ adapters: listAdapterMeta(), tasks: TASK_INFO, catalogUpdated: CATALOG_UPDATED })); + + /** Instructions the built-in generator sends to the model (editable in Settings → Templates). */ + r.get('/ai/instructions', (c) => c.json({ instructions: listInstructions(db) })); + r.put('/ai/instructions', async (c) => { + saveInstruction(db, await readBody(c, instructionPatchSchema)); + return c.json({ instructions: listInstructions(db) }); + }); r.get('/ai/status', (c) => c.json(aiStatus(app))); r.get('/ai/providers', (c) => c.json({ providers: listProviders(app) })); @@ -181,28 +191,63 @@ export function aiRoutes(app: AppContext): Hono { /* ------------------------------ assistant ------------------------------ */ + /** Streams a running turn's events (replayed from its start) until it ends or the client leaves. */ + const follow = (conversationId: string, emit: Emit, signal: AbortSignal) => + new Promise((resolve) => { + let unsubscribe: (() => void) | null = null; + const finish = () => { + unsubscribe?.(); + resolve(); + }; + unsubscribe = followTurn(conversationId, (event) => { + emit(event); + if (event.type === 'done') queueMicrotask(finish); + }); + if (!unsubscribe) { + emit({ type: 'done', conversationId, title: '' }); + return resolve(); + } + // Leaving the page only stops the stream; the turn keeps running on the server. + signal.addEventListener('abort', finish, { once: true }); + }); + r.post('/ai/chat', async (c) => { const body = await readBody(c, assistantRequestSchema); return sse(c, async (emit, signal) => { + let conversationId: string; try { - await runAssistantTurn(app, body, emit, signal); + ({ conversationId } = startAssistantTurn(app, body)); } catch (err) { - // Failures before the turn starts (no model configured, bad input) still reach the client as an event. + // Failures before the turn starts (no model configured, bad input, already running) still reach the client as an event. const ai = err instanceof AIError ? err : null; - if (!ai) app.log('error', 'Assistant request failed', err); - emit({ type: 'error', code: ai?.code ?? 'server', message: ai?.message ?? 'The assistant could not start.', detail: ai?.providerMessage }); + const http = err instanceof HttpError ? err : null; + if (!ai && !http) app.log('error', 'Assistant request failed', err); + emit({ type: 'error', code: ai?.code ?? http?.code ?? 'server', message: ai?.message ?? http?.message ?? 'The assistant could not start.', detail: ai?.providerMessage }); emit({ type: 'done', conversationId: body.conversationId ?? '', title: '' }); + return; } + await follow(conversationId, emit, signal); }); }); - r.get('/ai/conversations', (c) => c.json({ conversations: listConversations(db) })); + /** Re-attaches to a turn that is still running (after leaving and coming back). */ + r.get('/ai/chat/:id/stream', (c) => { + const id = idParam(c); + return sse(c, (emit, signal) => follow(id, emit, signal)); + }); + + r.post('/ai/chat/:id/stop', (c) => c.json({ stopped: stopTurn(idParam(c)) })); + + /** Library items that can be tagged with @ in the assistant. */ + r.get('/ai/mentionables', (c) => c.json({ items: mentionables(db, { classId: c.req.query('classId') || null, q: c.req.query('q') ?? '' }) })); + + r.get('/ai/conversations', (c) => c.json({ conversations: listConversations(db).map((x) => ({ ...x, running: isTurnRunning(x.id) })) })); r.get('/ai/conversations/:id', (c) => { const id = idParam(c); const row = db.get<{ id: string; title: string; class_id: string | null }>('SELECT id, title, class_id FROM conversations WHERE id = ?', id); if (!row) throw notFound('Conversation'); - return c.json({ conversation: { id: row.id, title: row.title, classId: row.class_id }, messages: displayMessages(db, id) }); + return c.json({ conversation: { id: row.id, title: row.title, classId: row.class_id, running: isTurnRunning(id) }, messages: displayMessages(db, id) }); }); r.delete('/ai/conversations/:id', (c) => { diff --git a/server/routes/data.ts b/server/routes/data.ts index 8f9c173..298c429 100644 --- a/server/routes/data.ts +++ b/server/routes/data.ts @@ -10,7 +10,7 @@ import { badRequest, HttpError, idParam, readBody, type AppContext } from '../ht import { appSettingsSchema, deleteSetting, getAppSettings, getSetting, setSetting } from '../settings.ts'; import { createSnapshot, exportBundle, fullBackupStream, listBackups } from '../services/backup.ts'; import { createClass, createDeck, createFolder, createNote, createQuiz, draftCardToInput, draftQuestionToInput, getClass, getDeck, getQuiz, listCards, listQuestions } from '../services/content.ts'; -import { createTemplate, deleteTemplate, listTemplates, templateInputSchema, updateTemplate } from '../services/templates.ts'; +import { createTemplate, deleteTemplate, listTemplates, resetTemplate, templateInputSchema, updateTemplate } from '../services/templates.ts'; import { importV1 } from '../services/v1import.ts'; import { slugify } from '../util.ts'; @@ -92,6 +92,7 @@ export function dataRoutes(app: AppContext): Hono { quiz: getSetting(db, 'llm.quiz', null) ?? LLM_INSTRUCTIONS.quiz, deck: getSetting(db, 'llm.deck', null) ?? LLM_INSTRUCTIONS.deck, customized: { quiz: getSetting(db, 'llm.quiz', null) !== null, deck: getSetting(db, 'llm.deck', null) !== null }, + builtin: LLM_INSTRUCTIONS, }), ); @@ -258,6 +259,8 @@ export function dataRoutes(app: AppContext): Hono { return c.json({ template: updateTemplate(db, idParam(c), body) }); }); + r.post('/templates/:id/reset', (c) => c.json({ template: resetTemplate(db, idParam(c)) })); + r.delete('/templates/:id', (c) => { deleteTemplate(db, idParam(c)); return c.json({ ok: true }); diff --git a/server/routes/shares.ts b/server/routes/shares.ts index ef97dd6..adc17b1 100644 --- a/server/routes/shares.ts +++ b/server/routes/shares.ts @@ -157,7 +157,7 @@ function publicDocument(app: AppContext, id: string) { return { kind: 'document' as const, id: doc.id as string, title: doc.title as string, filename: doc.filename as string, docKind: doc.kind as string, size: doc.size as number, pageCount: doc.page_count as number | null }; } -/** Quizzes, decks and notes inside a class or folder. Uploaded documents are never included in a collection share. */ +/** Quizzes, decks, notes and processed documents inside a class or folder. */ function collectionItems(app: AppContext, share: ShareRow) { const { db } = app; const byFolder = share.kind === 'folder'; @@ -171,10 +171,13 @@ function collectionItems(app: AppContext, share: ShareRow) { share.target_id, ); const notes = db.all>(`SELECT id, title, folder_id FROM notes WHERE ${where} ORDER BY sort_order, created_at`, share.target_id); + // Documents are shared once they have been processed (a failed or half-read upload stays private). + const documents = db.all>(`SELECT id, title, folder_id, page_count, kind FROM documents WHERE ${where} AND status = 'ready' ORDER BY sort_order, created_at`, share.target_id); return [ ...quizzes.filter((q) => q.n > 0).map((q) => ({ kind: 'quiz' as const, id: q.id as string, title: q.title as string, description: q.description as string, folderId: q.folder_id as string | null, count: q.n as number })), ...decks.filter((d) => d.n > 0).map((d) => ({ kind: 'deck' as const, id: d.id as string, title: d.title as string, description: d.description as string, folderId: d.folder_id as string | null, count: d.n as number })), ...notes.map((n) => ({ kind: 'note' as const, id: n.id as string, title: n.title as string, description: '', folderId: n.folder_id as string | null, count: 0 })), + ...documents.map((d) => ({ kind: 'document' as const, id: d.id as string, title: d.title as string, description: String(d.kind ?? ''), folderId: d.folder_id as string | null, count: (d.page_count as number | null) ?? 0 })), ]; } @@ -232,14 +235,12 @@ export function publicRoutes(app: AppContext): Hono { c.header('X-Robots-Tag', 'noindex, nofollow'); if (kind === 'quiz') return c.json(publicQuiz(app, id)); if (kind === 'deck') return c.json(publicDeck(app, id)); + if (kind === 'document') return c.json(publicDocument(app, id)); return c.json(publicNote(app, id)); }); - /** Download for an explicitly shared document. */ - r.get('/:token/file', (c) => { - const share = load(c.req.param('token')); - if (share.kind !== 'document') throw gone(); - const doc = db.get<{ filename: string; mime: string; storage_name: string }>('SELECT filename, mime, storage_name FROM documents WHERE id = ?', share.target_id); + const sendDocument = (documentId: string) => { + const doc = db.get<{ filename: string; mime: string; storage_name: string }>('SELECT filename, mime, storage_name FROM documents WHERE id = ?', documentId); if (!doc) throw gone(); const file = path.join(app.config.uploadsDir, path.basename(doc.storage_name)); if (!fs.existsSync(file)) throw gone(); @@ -254,6 +255,22 @@ export function publicRoutes(app: AppContext): Hono { 'X-Robots-Tag': 'noindex, nofollow', }, }); + }; + + /** Download for an explicitly shared document. */ + r.get('/:token/file', (c) => { + const share = load(c.req.param('token')); + if (share.kind !== 'document') throw gone(); + return sendDocument(share.target_id); + }); + + /** Download for a document inside a shared class or folder. Membership is re-checked. */ + r.get('/:token/items/document/:id/file', (c) => { + const share = load(c.req.param('token')); + if (share.kind !== 'class' && share.kind !== 'folder') throw gone(); + const id = c.req.param('id'); + if (!collectionItems(app, share).some((i) => i.kind === 'document' && i.id === id)) throw gone(); + return sendDocument(id); }); return r; diff --git a/server/services/attempts.ts b/server/services/attempts.ts index de7dcc4..2234960 100644 --- a/server/services/attempts.ts +++ b/server/services/attempts.ts @@ -363,7 +363,7 @@ export function getAttempt(db: DB, id: string): AttemptDto { export const attemptPatchSchema = z.object({ answers: z - .record(z.string(), z.object({ selected: z.array(z.string().max(40)).max(12), checked: z.boolean().optional().default(false) })) + .record(z.string(), z.object({ selected: z.array(z.string().max(80)).max(12), checked: z.boolean().optional().default(false) })) .optional(), flags: z.array(z.string()).max(1000).optional(), currentIndex: z.number().int().min(0).optional(), @@ -390,7 +390,8 @@ export function patchAttempt(db: DB, id: string, input: z.input o.id)); - const selected = [...new Set(a.selected.filter((s) => valid.has(s)))]; + // Numeric questions store what was typed; choice questions store option ids. + const selected = q.type === 'numeric' ? a.selected.slice(0, 1).map((s) => s.trim().slice(0, 60)).filter(Boolean) : [...new Set(a.selected.filter((s) => valid.has(s)))]; const checked = row.mode === 'practice' && a.checked && selected.length > 0; answers[qid] = { selected, checked, at }; if (checked) recordResponse(db, row, q, selected, settings.sataScoring, at); diff --git a/server/services/semantic.ts b/server/services/semantic.ts index 8a3637c..7b98283 100644 --- a/server/services/semantic.ts +++ b/server/services/semantic.ts @@ -11,6 +11,7 @@ * ("why do patients on furosemide need bananas?") also find the right passage. */ import { z } from 'zod'; +import { LOCAL_EMBEDDING_LABEL, LOCAL_EMBEDDING_MODEL, LOCAL_PROVIDER_ID, localEmbed, localModelStatus } from '../ai/localEmbed.ts'; import { getAdapter } from '../ai/providers/index.ts'; import { providerConfigById } from '../ai/service.ts'; import { AIError, type ProviderKind } from '../ai/types.ts'; @@ -23,6 +24,7 @@ import { chunkById, searchChunks, type ChunkHit, type ChunkScope } from './docum /* ------------------------------ settings ------------------------------ */ export interface EmbeddingSettings { + /** A provider id, `local` for the built-in model, or null for keyword search only. */ providerId: string | null; model: string; /** Requested vector size for models that support shortening (OpenAI v3, Gemini). Null = native. */ @@ -31,6 +33,13 @@ export interface EmbeddingSettings { const SETTINGS_KEY = 'embeddings'; const EMPTY: EmbeddingSettings = { providerId: null, model: '', dimensions: null }; +const LOCAL: EmbeddingSettings = { providerId: LOCAL_PROVIDER_ID, model: LOCAL_EMBEDDING_MODEL, dimensions: null }; + +/** Whether an install that never chose an embedding provider uses the built-in model. */ +let localDefault = false; +export function setLocalEmbeddingDefault(on: boolean): void { + localDefault = on; +} /** Suggested embedding model per provider type. Anthropic has no embeddings API. */ export const DEFAULT_EMBEDDING_MODELS: Record = { @@ -39,6 +48,7 @@ export const DEFAULT_EMBEDDING_MODELS: Record = { ollama: 'nomic-embed-text', openai_compatible: '', anthropic: null, + deepseek: null, }; /** USD per million input tokens, for the usage log. */ @@ -56,7 +66,11 @@ export const embeddingSettingsSchema = z.object({ }); export function getEmbeddingSettings(db: DB): EmbeddingSettings { - return { ...EMPTY, ...getSetting>(db, SETTINGS_KEY, EMPTY) }; + const stored = getSetting | null>(db, SETTINGS_KEY, null); + // Nothing chosen yet: the built-in model, so semantic search works without a cloud account. + if (!stored) return localDefault ? { ...LOCAL } : { ...EMPTY }; + const s = { ...EMPTY, ...stored }; + return s.providerId === LOCAL_PROVIDER_ID ? { ...LOCAL } : s; } const modelKey = (s: EmbeddingSettings) => `${s.providerId}:${s.model}:${s.dimensions ?? 'native'}`; @@ -64,7 +78,10 @@ const isConfigured = (s: EmbeddingSettings) => Boolean(s.providerId && s.model); export function saveEmbeddingSettings(app: AppContext, input: z.input): EmbeddingSettings { const next = embeddingSettingsSchema.parse(input); - if (next.providerId) { + if (next.providerId === LOCAL_PROVIDER_ID) { + next.model = LOCAL_EMBEDDING_MODEL; + next.dimensions = null; + } else if (next.providerId) { const { kind } = providerConfigById(app, next.providerId); if (!getAdapter(kind).embed) throw badRequest('This provider has no embeddings API. Choose OpenAI, Gemini, Ollama or an OpenAI-compatible server.'); if (!next.model) throw badRequest('Enter the embedding model to use.'); @@ -99,6 +116,11 @@ function normalise(v: number[]): Float32Array { export async function embedTexts(app: AppContext, texts: string[], purpose: 'document' | 'query', signal?: AbortSignal): Promise { const s = getEmbeddingSettings(app.db); if (!isConfigured(s)) throw new AIError('not_configured', 'Semantic search is not set up. Choose an embedding model in Settings → AI.'); + if (s.providerId === LOCAL_PROVIDER_ID) { + // Free and on this machine: nothing to log in the usage table. + const vectors = await localEmbed(app.config.dataDir, texts, purpose, signal); + return vectors.map(normalise); + } const { cfg, kind, name } = providerConfigById(app, s.providerId!); const adapter = getAdapter(kind); if (!adapter.embed) throw new AIError('unsupported', `${name} has no embeddings API.`); @@ -202,7 +224,16 @@ export function embeddingStatus(app: AppContext) { const s = getEmbeddingSettings(app.db); const total = app.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM chunks')?.n ?? 0; const indexed = isConfigured(s) ? (app.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM chunk_vectors WHERE model_key = ?', modelKey(s))?.n ?? 0) : 0; - return { settings: s, configured: isConfigured(s), indexed, total, running: state.running, lastError: state.lastError, defaults: DEFAULT_EMBEDDING_MODELS }; + return { + settings: s, + configured: isConfigured(s), + indexed, + total, + running: state.running, + lastError: state.lastError, + defaults: DEFAULT_EMBEDDING_MODELS, + local: { ...localModelStatus(app.config.dataDir), label: LOCAL_EMBEDDING_LABEL, enabledByDefault: localDefault }, + }; } /* ------------------------------ vector search ------------------------------ */ diff --git a/server/services/templates.ts b/server/services/templates.ts index 1559b8d..fc09154 100644 --- a/server/services/templates.ts +++ b/server/services/templates.ts @@ -2,7 +2,8 @@ * Reusable templates. * - quiz / deck: a content skeleton (or a full copy of an existing item) to start from. * - generation: a saved set of AI generation options. - * Built-in templates are (re)seeded at startup and cannot be edited, only copied. + * Built-in templates are (re)seeded at startup. They can be edited: an edited built-in is + * stored with builtin = 2, which seeding leaves alone, and "reset" restores the original. */ import { z } from 'zod'; import type { DB } from '../db.ts'; @@ -33,7 +34,9 @@ const mapTemplate = (r: TemplateRow) => ({ name: r.name, description: r.description, payload: parseJson>(r.payload, {}), - builtin: Boolean(r.builtin), + builtin: r.builtin > 0, + /** A built-in template the user has changed. */ + edited: r.builtin === 2, updatedAt: r.updated_at, }); @@ -177,11 +180,12 @@ export function seedTemplates(db: DB): void { const at = now(); db.tx(() => { const keep = BUILTIN.map((t) => t.id); - db.run(`DELETE FROM templates WHERE builtin = 1 AND id NOT IN (${keep.map(() => '?').join(',')})`, ...keep); + db.run(`DELETE FROM templates WHERE builtin > 0 AND id NOT IN (${keep.map(() => '?').join(',')})`, ...keep); for (const t of BUILTIN) { db.run( `INSERT INTO templates (id, kind, name, description, payload, builtin, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 1, ?, ?) - ON CONFLICT(id) DO UPDATE SET kind = excluded.kind, name = excluded.name, description = excluded.description, payload = excluded.payload, updated_at = excluded.updated_at`, + ON CONFLICT(id) DO UPDATE SET kind = excluded.kind, name = excluded.name, description = excluded.description, payload = excluded.payload, updated_at = excluded.updated_at + WHERE templates.builtin = 1`, t.id, t.kind, t.name, @@ -210,19 +214,28 @@ export function createTemplate(db: DB, input: z.input }) { - const row = db.get('SELECT * FROM templates WHERE id = ? AND builtin = 0', id); + const row = db.get('SELECT * FROM templates WHERE id = ?', id); if (!row) throw notFound('Template'); db.run( - 'UPDATE templates SET name = ?, description = ?, payload = ?, updated_at = ? WHERE id = ?', + 'UPDATE templates SET name = ?, description = ?, payload = ?, builtin = ?, updated_at = ? WHERE id = ?', input.name?.trim().slice(0, 120) || row.name, input.description ?? row.description, input.payload ? JSON.stringify(input.payload) : row.payload, + row.builtin > 0 ? 2 : 0, now(), id, ); return mapTemplate(db.get('SELECT * FROM templates WHERE id = ?', id)!); } +/** Puts an edited built-in template back to how it shipped. */ +export function resetTemplate(db: DB, id: string) { + const original = BUILTIN.find((t) => t.id === id); + if (!original) throw notFound('Template'); + db.run('UPDATE templates SET name = ?, description = ?, payload = ?, builtin = 1, updated_at = ? WHERE id = ?', original.name, original.description, JSON.stringify(original.payload), now(), id); + return mapTemplate(db.get('SELECT * FROM templates WHERE id = ?', id)!); +} + export function deleteTemplate(db: DB, id: string): void { const { changes } = db.run('DELETE FROM templates WHERE id = ? AND builtin = 0', id); if (!changes) throw notFound('Template'); diff --git a/shared/content.ts b/shared/content.ts index ef0c9d9..07fb5c3 100644 --- a/shared/content.ts +++ b/shared/content.ts @@ -19,7 +19,7 @@ export const LIMITS = { noteContent: 400000, } as const; -export const QUESTION_TYPES = ['single', 'multi'] as const; +export const QUESTION_TYPES = ['single', 'multi', 'numeric'] as const; export type QuestionType = (typeof QUESTION_TYPES)[number]; export const DIFFICULTIES = ['easy', 'medium', 'hard'] as const; @@ -31,12 +31,17 @@ export type ClassColor = (typeof CLASS_COLORS)[number]; export const QUESTION_TYPE_LABEL: Record = { single: 'Multiple choice', multi: 'Select all that apply', + numeric: 'Enter the answer', }; /* ------------------------------------------------------------------ */ /* Stored shapes */ /* ------------------------------------------------------------------ */ +/** + * An answer choice. For numeric questions each option is an accepted answer ("12.5 mL", + * optionally "12.5 ± 0.1 mL"); the student types a value instead of picking one. + */ export interface Option { id: string; text: string; @@ -100,13 +105,18 @@ export const questionInputSchema = z id: z.string().min(1).max(40).optional(), type: z.enum(QUESTION_TYPES), stem: z.string().trim().min(1, 'Question text is required').max(LIMITS.markdown), - options: z.array(optionInputSchema).min(2, 'Add at least two answer choices').max(LIMITS.options), + options: z.array(optionInputSchema).min(1, 'Add the correct answer').max(LIMITS.options), rationale: z.string().max(LIMITS.markdown).optional().default(''), topic: z.string().trim().max(LIMITS.topic).optional().default(''), difficulty: z.enum(DIFFICULTIES).optional().default('medium'), tags: z.array(tagSchema).max(LIMITS.tags).optional().default([]), }) .superRefine((q, ctx) => { + if (q.type === 'numeric') { + if (q.options.some((o) => parseNumericAnswer(o.text) === null)) ctx.addIssue({ code: 'custom', path: ['options'], message: 'Each accepted answer must contain a number' }); + return; + } + if (q.options.length < 2) ctx.addIssue({ code: 'custom', path: ['options'], message: 'Add at least two answer choices' }); const correct = q.options.filter((o) => o.correct).length; if (q.type === 'single' && correct !== 1) { ctx.addIssue({ code: 'custom', path: ['options'], message: 'Multiple choice needs exactly one correct answer' }); @@ -204,6 +214,12 @@ export function validateDraftQuestion(q: DraftQuestion, index: number): Issue[] const warn = (field: string, message: string) => issues.push({ level: 'warning', index, field, message }); if (!q.stem?.trim()) err('stem', 'Question text is missing'); const options = q.options ?? []; + if (q.type === 'numeric') { + if (options.length === 0 || options.every((o) => !o.text?.trim())) err('options', 'Enter the correct answer'); + else if (options.some((o) => o.text?.trim() && parseNumericAnswer(o.text) === null)) err('options', 'The correct answer must contain a number'); + if (!q.rationale?.trim() && !options.some((o) => o.why?.trim())) warn('rationale', 'No rationale'); + return issues; + } if (options.length < 2) err('options', 'Needs at least two answer choices'); if (options.length > LIMITS.options) err('options', `No more than ${LIMITS.options} answer choices`); if (options.some((o) => !o.text?.trim())) err('options', 'An answer choice is empty'); @@ -248,6 +264,7 @@ export function validateDraft(draft: ContentDraft): Issue[] { } export function blankQuestion(type: QuestionType = 'single'): DraftQuestion { + if (type === 'numeric') return { type, stem: '', options: [{ text: '', correct: true, why: '' }], rationale: '', topic: '', difficulty: 'medium', tags: [] }; return { type, stem: '', @@ -289,7 +306,61 @@ export type SataScoring = 'partial' | 'strict'; export interface ScorableQuestion { type: QuestionType; - options: { id: string; correct: boolean }[]; + options: { id: string; correct: boolean; text?: string }[]; +} + +/* ------------------------------------------------------------------ */ +/* Numeric answers */ +/* ------------------------------------------------------------------ */ + +export interface NumericAnswer { + value: number; + /** Absolute tolerance; 0 means "equal after rounding noise". */ + tolerance: number; +} + +const NUMBER = String.raw`[-−]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?|[-−]?\.\d+`; +const NUMBER_RE = new RegExp(String.raw`(${NUMBER})(?:\s*/\s*(${NUMBER}))?`); +const TOLERANCE_RE = new RegExp(String.raw`(?:±|\+/-|\+-|plus or minus)\s*(${NUMBER})`, 'i'); + +function toNumber(raw: string): number { + return Number(raw.replace(/−/g, '-').replace(/,/g, '')); +} + +/** + * Reads the number in an answer and ignores any unit around it: "12.5 mL", "12.5mL/hr", + * "1,250 units", "0.5", ".5", "1/2" and "-3" all work. "12.5 ± 0.1 mL" also sets a tolerance. + * Returns null when there is no number. + */ +export function parseNumericAnswer(text: string | null | undefined): NumericAnswer | null { + if (!text) return null; + const t = text.trim(); + const m = NUMBER_RE.exec(t); + if (!m) return null; + let value = toNumber(m[1]); + if (m[2] !== undefined) { + const denominator = toNumber(m[2]); + if (!denominator) return null; + value /= denominator; + } + if (!Number.isFinite(value)) return null; + const tol = TOLERANCE_RE.exec(t.slice(m.index + m[0].length)); + const tolerance = tol ? Math.abs(toNumber(tol[1])) : 0; + return { value, tolerance: Number.isFinite(tolerance) ? tolerance : 0 }; +} + +/** True when the typed answer matches any accepted answer, comparing numbers only. */ +export function numericMatches(input: string | null | undefined, accepted: { text?: string; correct: boolean }[]): boolean { + const given = parseNumericAnswer(input); + if (!given) return false; + return accepted.some((o) => { + if (o.correct === false) return false; + const want = parseNumericAnswer(o.text); + if (!want) return false; + // Rounding noise only ("0.30" vs "0.3"), unless the answer states a tolerance. + const slack = Math.max(want.tolerance, 1e-9 * Math.max(1, Math.abs(want.value))); + return Math.abs(given.value - want.value) <= slack; + }); } /** @@ -300,6 +371,7 @@ export interface ScorableQuestion { * - multi (strict): all-or-nothing. */ export function scoreQuestion(question: ScorableQuestion, selected: string[], sata: SataScoring = 'partial'): number { + if (question.type === 'numeric') return numericMatches(selected[0], question.options) ? 1 : 0; const picked = new Set(selected); const correctIds = question.options.filter((o) => o.correct).map((o) => o.id); if (correctIds.length === 0 || picked.size === 0) return 0; @@ -320,6 +392,30 @@ export function isFullyCorrect(score: number): boolean { return score >= 0.999; } +/** + * The 7-point grading scale used by most nursing programs: + * A 93-100, B 85-92, C 77-84, D 70-76, F below 70. Percentages are rounded first, so 92.6% is + * an A. C is the usual minimum passing grade. + */ +export const GRADE_SCALE = [ + { letter: 'A', min: 93 }, + { letter: 'B', min: 85 }, + { letter: 'C', min: 77 }, + { letter: 'D', min: 70 }, + { letter: 'F', min: 0 }, +] as const; +export type LetterGrade = (typeof GRADE_SCALE)[number]['letter']; + +export function letterGrade(percentScore: number): LetterGrade { + const p = Math.round(percentScore); + return (GRADE_SCALE.find((g) => p >= g.min) ?? GRADE_SCALE[GRADE_SCALE.length - 1]).letter; +} + +/** Passing on the 7-point scale means a C (77%) or better. */ +export function isPassing(percentScore: number): boolean { + return Math.round(percentScore) >= 77; +} + export function percent(score: number, max: number): number { if (!max) return 0; return Math.round((score / max) * 100); diff --git a/shared/importer.ts b/shared/importer.ts index 7d8e217..0244776 100644 --- a/shared/importer.ts +++ b/shared/importer.ts @@ -18,6 +18,7 @@ import { type Issue, type NoteDraft, type QuizDraft, + parseNumericAnswer, validateDraft, } from './content.ts'; @@ -122,8 +123,27 @@ function normalizeQuestion(raw: unknown, notices: Issue[], index: number): Draft notices.push({ level: 'warning', index, message: `Question ${index + 1} is not an object and was skipped` }); return null; } - const options = normalizeOptions(pick(raw, 'options', 'choices', 'answers', 'answerOptions', 'answer_options')); + let options = normalizeOptions(pick(raw, 'options', 'choices', 'answers', 'answerOptions', 'answer_options')); const answerKey = pick(raw, 'correctAnswers', 'correct_answers', 'correctAnswer', 'correct_answer', 'answer', 'correct', 'key'); + const rawTypeEarly = str(pick(raw, 'type', 'questionType', 'question_type', 'kind')).toLowerCase().replace(/[\s-]/g, '_'); + const numericType = NUMERIC_TYPES.includes(rawTypeEarly); + // Calculation questions: the "options" are the accepted answers, or there is only an answer key. + if (numericType || (options.length === 0 && (typeof answerKey === 'number' || (typeof answerKey === 'string' && parseNumericAnswer(answerKey) !== null)))) { + if (options.length === 0 && answerKey !== undefined) { + const keys = Array.isArray(answerKey) ? answerKey : [answerKey]; + options = keys.map((k) => ({ text: str(k).trim(), correct: true, why: '' })).filter((o) => o.text); + } + return { + type: 'numeric', + stem: str(pick(raw, 'stem', 'prompt', 'question', 'text', 'q')).trim(), + options: options.map((o) => ({ ...o, correct: true })), + rationale: str(pick(raw, 'rationale', 'explanation', 'reasoning', 'feedback')).trim(), + topic: str(pick(raw, 'topic', 'category', 'subject', 'concept')).trim().slice(0, 80), + difficulty: toDifficulty(pick(raw, 'difficulty', 'level')), + tags: toTags(pick(raw, 'tags', 'keywords')), + source: str(pick(raw, 'source', 'citation', 'reference')).trim() || undefined, + }; + } if (answerKey !== undefined && !options.some((o) => o.correct)) { for (const idx of resolveAnswerRefs(answerKey, options)) options[idx].correct = true; } @@ -195,8 +215,13 @@ function noteFrom(raw: Raw): NoteDraft { return { kind: 'note', title: str(pick(raw, 'title', 'name')).trim(), content: str(pick(raw, 'content', 'markdown', 'body', 'text')) }; } +const NUMERIC_TYPES = ['numeric', 'number', 'numerical', 'calculation', 'calc', 'math', 'fill_in', 'fill_in_the_blank', 'short_answer', 'dosage']; + function looksLikeQuestion(v: unknown): boolean { - return isObj(v) && pick(v, 'options', 'choices', 'answers', 'answerOptions') !== undefined; + if (!isObj(v)) return false; + if (pick(v, 'options', 'choices', 'answers', 'answerOptions') !== undefined) return true; + const type = str(pick(v, 'type', 'questionType', 'question_type')).toLowerCase().replace(/[\s-]/g, '_'); + return NUMERIC_TYPES.includes(type) && pick(v, 'stem', 'question', 'prompt') !== undefined; } function looksLikeCard(v: unknown): boolean { return isObj(v) && pick(v, 'front', 'term') !== undefined && pick(v, 'back', 'definition') !== undefined; @@ -305,7 +330,7 @@ export function parseTextQuestions(text: string): DraftQuestion[] { const questions: DraftQuestion[] = []; for (const block of blocks) { const lines = block.split('\n').filter((l) => l.trim()); - if (lines.length < 3) continue; + if (lines.length < 2) continue; const stem: string[] = []; const options: DraftOption[] = []; let rationale = ''; @@ -343,6 +368,11 @@ export function parseTextQuestions(text: string): DraftQuestion[] { else if (section === 'rationale') rationale += `\n${line.trim()}`; else if (options.length) options[options.length - 1].text += ` ${line.trim()}`; } + if (stem.length > 0 && options.length === 0 && answer && parseNumericAnswer(answer) !== null) { + // "Answer: 12.5 mL" with no choices: a calculation the student types in. + questions.push({ type: 'numeric', stem: stem.join('\n'), options: [{ text: answer.trim(), correct: true, why: '' }], rationale: rationale.trim(), topic, difficulty: 'medium', tags: [] }); + continue; + } if (options.length < 2 || stem.length === 0) continue; if (answer && !options.some((o) => o.correct)) for (const idx of resolveAnswerRefs(answer, options)) options[idx].correct = true; const correctCount = options.filter((o) => o.correct).length; @@ -513,6 +543,15 @@ export const QUIZ_JSON_TEMPLATE = JSON.stringify( difficulty: 'medium', tags: [], }, + { + type: 'numeric', + stem: 'Heparin 5,000 units subcut is ordered. The vial contains 10,000 units/mL. How many mL should the nurse give? Round to the nearest tenth.', + options: [{ text: '0.5 mL', correct: true, why: '5,000 units ÷ 10,000 units/mL = 0.5 mL.' }], + rationale: 'Desired over have: 5,000 ÷ 10,000 × 1 mL = 0.5 mL. Only the number is checked, so "0.5" and "0.5 mL" are both accepted.', + topic: 'Dosage calculation', + difficulty: 'easy', + tags: [], + }, ], }), null, @@ -546,7 +585,12 @@ Topic: Electrolytes C. Hypertension *D. Bradycardia Rationale: Early toxicity is gastrointestinal and visual; bradycardia and dysrhythmias follow. -Topic: Pharmacology`; +Topic: Pharmacology + +3. The provider orders 500 mL of normal saline over 4 hours. At what rate in mL/hr should the nurse set the pump? +Answer: 125 mL/hr +Rationale: 500 mL ÷ 4 hr = 125 mL/hr. +Topic: Dosage calculation`; export const TEXT_CARDS_TEMPLATE = `Furosemide\tLoop diuretic; monitor potassium Metoprolol\tBeta-1 selective blocker; hold for HR below 60 @@ -588,9 +632,10 @@ Use exactly this shape: } Rules: -- "type" is "single" (multiple choice) or "multi" (select all that apply). +- "type" is "single" (multiple choice), "multi" (select all that apply) or "numeric" (the student types a number). - "single" questions have exactly 4 options and exactly one with "correct": true. - "multi" questions have 5 or 6 options, 2 to 5 of them correct, and the stem ends with "Select all that apply." +- Use "numeric" for calculations (dosages, IV rates, drip rates, conversions, intake and output). Its "options" holds only the correct answer, e.g. [{ "text": "12.5 mL", "correct": true, "why": "Show the calculation" }]. The stem states the unit and how to round, e.g. "Round to the nearest tenth." Only the number is checked, so units in the answer are optional. - Write NCLEX-style questions that test clinical judgment: a short client scenario, one clear question, priority wording where it fits (first, best, most important), and distractors that are plausible but wrong for a specific reason. Never use "all of the above" or "none of the above". - Every option needs a one-sentence "why". The "rationale" explains the principle behind the correct answer. - "topic" is a short label. Reuse the same label for related questions and keep the number of different topics small. diff --git a/tests/api.test.ts b/tests/api.test.ts index 0446676..c2b4a9c 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -3,6 +3,7 @@ import os from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { createTestApp, PASSWORD, sampleQuestions, type TestApp } from './helpers.ts'; +import { queueIdle } from '../server/services/documents.ts'; let t: TestApp; beforeEach(async () => { @@ -275,14 +276,16 @@ describe('sharing', () => { expect((await t.api('GET', `/api/public/${rotated.token}`, undefined, anon)).status).toBe(404); }); - it('shares a class as a collection without exposing documents', async () => { + it("shares a class as a collection without exposing other classes' content", async () => { const cls = await makeClass(); const quizId = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Q', questions: sampleQuestions })).body.quiz.id; const deckId = (await t.api('POST', '/api/decks', { classId: cls.id, title: 'D', cards: [{ front: 'a', back: 'b' }] })).body.deck.id; await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Empty quiz' }); - await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Secret notes', text: 'private lecture content' }); const other = await makeClass('Other'); const otherQuiz = (await t.api('POST', '/api/quizzes', { classId: other.id, title: 'Other', questions: sampleQuestions })).body.quiz.id; + // Documents in the shared class are included once processed (see revision1.test.ts); other classes' never are. + await t.api('POST', '/api/documents/text', { classId: other.id, title: 'Secret notes', text: 'private lecture content' }); + await queueIdle(t.ctx); const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share; const anon = { cookie: null }; diff --git a/tests/mockAi.ts b/tests/mockAi.ts index 5d16ff3..220464b 100644 --- a/tests/mockAi.ts +++ b/tests/mockAi.ts @@ -16,6 +16,8 @@ export interface MockAi { 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; + /** Delay every chat call by this many milliseconds (0 turns it off). */ + delayChat: (ms: number) => void; close: () => Promise; } @@ -108,11 +110,12 @@ function chooseTool(userText: string, names: string[]): string | null { export function startMockAi(port = 0): Promise { const requests: MockAi['requests'] = []; let fail = { status: 0, times: 0 }; + let delayMs = 0; const server = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on('data', (c) => chunks.push(c)); - req.on('end', () => { + req.on('end', async () => { const raw = Buffer.concat(chunks).toString('utf8'); let body: any = null; try { @@ -135,6 +138,7 @@ export function startMockAi(port = 0): Promise { 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 (delayMs > 0) await new Promise((r) => setTimeout(r, delayMs)); 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']; @@ -212,6 +216,9 @@ export function startMockAi(port = 0): Promise { failNext: (status, times = 1) => { fail = { status, times }; }, + delayChat: (ms) => { + delayMs = ms; + }, close: () => new Promise((done) => server.close(() => done())), }); }); diff --git a/tests/revision1.test.ts b/tests/revision1.test.ts new file mode 100644 index 0000000..273c673 --- /dev/null +++ b/tests/revision1.test.ts @@ -0,0 +1,316 @@ +/** + * Regression tests for the first round of V2 revisions: numeric questions, the 7-point grading + * scale, editable instructions and presets, documents in collection shares, background + * assistant turns with stop/re-attach, @-tagged materials, assistant model switching, cached + * pricing and the built-in embedding default. + */ +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; +import { isPassing, letterGrade, numericMatches, parseNumericAnswer, scoreQuestion } from '../shared/content.ts'; +import { parseImport } from '../shared/importer.ts'; +import { estimateCost } from '../server/ai/service.ts'; +import { queueIdle } from '../server/services/documents.ts'; +import { getEmbeddingSettings, setLocalEmbeddingDefault } from '../server/services/semantic.ts'; +import { seedTemplates } from '../server/services/templates.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; + mock.delayChat(0); +}); +afterEach(() => t.close()); + +async function connectAi() { + const provider = (await t.api('POST', '/api/ai/providers', { name: 'Mock', kind: 'openai_compatible', baseUrl: mock.url, apiKey: 'mock-key' })).body.provider; + const models = (await t.api('POST', `/api/ai/providers/${provider.id}/discover`)).body.models as any[]; + for (const m of models) await t.api('PATCH', `/api/ai/models/${m.id}`, { enabled: true, capabilities: { tools: true } }); + return models; +} + +function parseEvents(raw: string): any[] { + return raw + .split(/\n\n/) + .map((block) => block.split('\n').filter((l) => l.startsWith('data:')).map((l) => l.slice(5).trim()).join('')) + .filter(Boolean) + .map((d) => JSON.parse(d)); +} + +async function sse(url: string, body?: unknown): Promise { + const res = await t.raw(body === undefined ? 'GET' : 'POST', url, body === undefined ? {} : { headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); + expect(res.status).toBe(200); + return parseEvents(await res.text()); +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +async function waitIdle(conversationId: string) { + for (let i = 0; i < 400; i += 1) { + const c = (await t.api('GET', `/api/ai/conversations/${conversationId}`)).body; + if (!c.conversation.running) return c; + await sleep(25); + } + throw new Error('turn never finished'); +} + +/* ------------------------------ numeric answers + grading ------------------------------ */ + +describe('numeric answers', () => { + it('reads the number and ignores units', () => { + expect(parseNumericAnswer('12.5 mL/hr')).toEqual({ value: 12.5, tolerance: 0 }); + expect(parseNumericAnswer('1,250 units')?.value).toBe(1250); + expect(parseNumericAnswer('.5')?.value).toBe(0.5); + expect(parseNumericAnswer('1/4 tab')?.value).toBe(0.25); + expect(parseNumericAnswer('12.5 ± 0.1 mL')).toEqual({ value: 12.5, tolerance: 0.1 }); + expect(parseNumericAnswer('no number')).toBeNull(); + expect(numericMatches('12.50ml', [{ text: '12.5 mL', correct: true }])).toBe(true); + expect(numericMatches('12.4', [{ text: '12.5 mL', correct: true }])).toBe(false); + expect(numericMatches('12.45', [{ text: '12.5 ± 0.1', correct: true }])).toBe(true); + expect(scoreQuestion({ type: 'numeric', options: [{ id: 'a', text: '0.5 mL', correct: true }] }, ['0.5'])).toBe(1); + expect(scoreQuestion({ type: 'numeric', options: [{ id: 'a', text: '0.5 mL', correct: true }] }, [])).toBe(0); + }); + + it('imports calculation questions from JSON and from text blocks', () => { + const json = parseImport(JSON.stringify({ kind: 'quiz', title: 'Math', questions: [{ type: 'numeric', stem: 'Rate?', answer: '125 mL/hr', rationale: 'r' }, { question: 'Dose?', correctAnswer: 2 }] })); + const qs = json.items[0].kind === 'quiz' ? json.items[0].questions : []; + expect(qs.map((q) => q.type)).toEqual(['numeric', 'numeric']); + expect(qs[0].options).toEqual([{ text: '125 mL/hr', correct: true, why: '' }]); + const text = parseImport('1. Infuse 500 mL over 4 hours. Rate in mL/hr?\nAnswer: 125 mL/hr\nRationale: 500/4'); + expect(text.items[0]).toMatchObject({ kind: 'quiz', questions: [{ type: 'numeric', options: [{ text: '125 mL/hr', correct: true }] }] }); + }); + + it('stores, validates, takes and scores numeric questions through the API', async () => { + const cls = (await t.api('POST', '/api/classes', { name: 'Dosage' })).body.class; + const bad = await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Bad', questions: [{ type: 'numeric', stem: 'How much?', options: [{ text: 'half a tablet', correct: true }] }] }); + expect(bad.status).toBe(422); + const quiz = ( + await t.api('POST', '/api/quizzes', { + classId: cls.id, + title: 'Calc', + questions: [ + { type: 'numeric', stem: 'Give how many mL?', options: [{ text: '0.5 mL', correct: true, why: '5000/10000' }], rationale: 'Desired over have' }, + { type: 'numeric', stem: 'Rate?', options: [{ text: '125 mL/hr', correct: true }] }, + ], + }) + ).body.quiz; + const attempt = (await t.api('POST', '/api/attempts', { mode: 'practice', source: { type: 'quiz', quizId: quiz.id }, shuffleQuestions: false })).body.attempt; + const [q1, q2] = attempt.questions; + const patched = (await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q1.id]: { selected: ['0.50 ml'], checked: true }, [q2.id]: { selected: ['120'], checked: true } } })).body.attempt; + expect(patched.answers[q1.id].selected).toEqual(['0.50 ml']); + const done = (await t.api('POST', `/api/attempts/${attempt.id}/complete`, {})).body.attempt; + expect(done.score).toBe(1); + expect(done.maxScore).toBe(2); + expect(done.results[q1.id].correct).toBe(true); + expect(done.results[q2.id].correct).toBe(false); + }); +}); + +describe('7-point grading scale', () => { + it('maps percentages to letters with C as the passing grade', () => { + expect([100, 93, 92.6, 92, 85, 84, 77, 76, 70, 69, 0].map(letterGrade)).toEqual(['A', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'F', 'F']); + expect(isPassing(77)).toBe(true); + expect(isPassing(76)).toBe(false); + }); +}); + +/* ------------------------------ pricing ------------------------------ */ + +describe('cost estimates', () => { + it('bills cached input at the cached price and applies DeepSeek off-peak pricing', () => { + const offPeak = Date.UTC(2026, 10, 16, 12, 0); + const flash = { model: 'deepseek-flash', providerKind: 'deepseek' as const, priceIn: 0.3, priceOut: 1.2, priceCachedIn: 0.006 }; + const cost = estimateCost(flash, { inputTokens: 1_000_000, cacheReadTokens: 800_000, outputTokens: 100_000 }, offPeak)!; + // (200k * 0.3 + 800k * 0.006 + 100k * 1.2) / 1M, halved off-peak + expect(cost).toBeCloseTo(((200_000 * 0.3 + 800_000 * 0.006 + 100_000 * 1.2) / 1_000_000) * 0.5, 8); + const claude = { model: 'claude-sonnet-5-5', providerKind: 'anthropic' as const, priceIn: 2, priceOut: 10, priceCachedIn: null }; + expect(estimateCost(claude, { inputTokens: 1_000_000, cacheReadTokens: 500_000, cacheWriteTokens: 100_000, outputTokens: 0 })!).toBeCloseTo((400_000 * 2 + 500_000 * 0.2 + 100_000 * 2.5) / 1_000_000, 8); + expect(estimateCost({ model: 'x', providerKind: 'openai_compatible', priceIn: null, priceOut: null }, { inputTokens: 5, outputTokens: 5 })).toBeNull(); + }); + + it('stores an editable cached price per model', async () => { + const models = await connectAi(); + const updated = (await t.api('PATCH', `/api/ai/models/${models[0].id}`, { priceIn: 1, priceOut: 2, priceCachedIn: 0.05 })).body.model; + expect(updated.priceCachedIn).toBe(0.05); + const cleared = (await t.api('PATCH', `/api/ai/models/${models[0].id}`, { priceCachedIn: null })).body.model; + expect(cleared.priceCachedIn).toBeNull(); + }); + + it('accepts the full range of reasoning efforts in task settings', async () => { + for (const effort of ['minimal', 'xhigh', 'max']) { + const res = await t.api('PUT', '/api/ai/tasks', { chat: { modelId: null, temperature: null, reasoningEffort: effort, maxOutputTokens: null } }); + expect(res.status).toBe(200); + expect(res.body.tasks.chat.reasoningEffort).toBe(effort); + } + }); +}); + +/* ------------------------------ instructions + presets ------------------------------ */ + +describe('editable generation instructions and presets', () => { + it('saves and resets generator instructions, which generation then uses', async () => { + const list = (await t.api('GET', '/api/ai/instructions')).body.instructions as any[]; + expect(list.find((i) => i.key === 'quiz.nclex').text).toMatch(/numeric/); + const custom = 'Write every question about pediatric dosing. Calculation items use type numeric.'; + const saved = (await t.api('PUT', '/api/ai/instructions', { key: 'quiz.nclex', text: custom })).body.instructions as any[]; + expect(saved.find((i) => i.key === 'quiz.nclex')).toMatchObject({ text: custom, customized: true }); + + await connectAi(); + const cls = (await t.api('POST', '/api/classes', { name: 'Peds' })).body.class; + const { draftId } = (await t.api('POST', '/api/ai/generate', { kind: 'quiz', classId: cls.id, focus: 'fever', count: 2 })).body; + for (let i = 0; i < 200 && (await t.api('GET', `/api/drafts/${draftId}`)).body.draft.status === 'generating'; i += 1) await sleep(25); + const call = mock.requests.find((r) => r.path.endsWith('/chat/completions'))!; + expect(JSON.stringify(call.body.messages[0])).toContain('pediatric dosing'); + + const reset = (await t.api('PUT', '/api/ai/instructions', { key: 'quiz.nclex', text: null })).body.instructions as any[]; + expect(reset.find((i) => i.key === 'quiz.nclex').customized).toBe(false); + }); + + it('keeps the chatbot import instructions separate and editable', async () => { + const before = (await t.api('GET', '/api/llm-instructions')).body; + expect(before.builtin.quiz).toContain('"numeric"'); + await t.api('PUT', '/api/llm-instructions', { kind: 'quiz', text: 'My own chatbot instructions, long enough.' }); + const after = (await t.api('GET', '/api/llm-instructions')).body; + expect(after.quiz).toBe('My own chatbot instructions, long enough.'); + expect(after.customized.quiz).toBe(true); + // The built-in generator's instructions are untouched. + expect((await t.api('GET', '/api/ai/instructions')).body.instructions.every((i: any) => !i.customized)).toBe(true); + }); + + it('lets built-in presets be edited, keeps the edit across restarts and resets it', async () => { + const preset = (await t.api('GET', '/api/templates?kind=generation')).body.templates.find((x: any) => x.id === 'builtin-gen-nclex'); + const edited = (await t.api('PATCH', `/api/templates/${preset.id}`, { name: 'My NCLEX set', payload: { ...preset.payload, count: 40 } })).body.template; + expect(edited).toMatchObject({ name: 'My NCLEX set', builtin: true, edited: true }); + seedTemplates(t.ctx.db); + const afterSeed = (await t.api('GET', '/api/templates?kind=generation')).body.templates.find((x: any) => x.id === preset.id); + expect(afterSeed).toMatchObject({ name: 'My NCLEX set', edited: true }); + expect(afterSeed.payload.count).toBe(40); + const reset = (await t.api('POST', `/api/templates/${preset.id}/reset`)).body.template; + expect(reset).toMatchObject({ name: preset.name, edited: false }); + expect(reset.payload.count).toBe(preset.payload.count); + // Built-ins still cannot be deleted. + expect((await t.api('DELETE', `/api/templates/${preset.id}`)).status).toBe(404); + }); +}); + +/* ------------------------------ sharing ------------------------------ */ + +describe('class shares include documents', () => { + it('lists processed documents and serves them only through the share', async () => { + const cls = (await t.api('POST', '/api/classes', { name: 'Shared' })).body.class; + const doc = (await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Lecture notes', text: 'Furosemide causes potassium loss.' })).body.document; + await queueIdle(t.ctx); + const other = (await t.api('POST', '/api/classes', { name: 'Private' })).body.class; + const hidden = (await t.api('POST', '/api/documents/text', { classId: other.id, title: 'Private notes', text: 'secret' })).body.document; + await queueIdle(t.ctx); + + const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share; + const anon = { cookie: null }; + const pub = (await t.api('GET', `/api/public/${share.token}`, undefined, anon)).body; + expect(pub.items).toEqual([expect.objectContaining({ kind: 'document', id: doc.id, title: 'Lecture notes' })]); + const meta = (await t.api('GET', `/api/public/${share.token}/items/document/${doc.id}`, undefined, anon)).body; + expect(meta).toMatchObject({ kind: 'document', id: doc.id }); + const file = await t.raw('GET', `/api/public/${share.token}/items/document/${doc.id}/file`, { cookie: null }); + expect(file.status).toBe(200); + expect(await file.text()).toContain('potassium'); + expect((await t.raw('GET', `/api/public/${share.token}/items/document/${hidden.id}/file`, { cookie: null })).status).toBe(404); + // Turning the share off closes the file route too. + await t.api('PATCH', `/api/shares/${share.id}`, { enabled: false }); + expect((await t.raw('GET', `/api/public/${share.token}/items/document/${doc.id}/file`, { cookie: null })).status).toBe(404); + }); +}); + +/* ------------------------------ assistant ------------------------------ */ + +describe('assistant turns', () => { + it('keeps answering after the browser disconnects, and can be followed again', async () => { + await connectAi(); + mock.delayChat(150); + const res = await t.raw('POST', '/api/ai/chat', { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ message: 'Explain the difference between left-sided and right-sided heart failure' }) }); + const reader = res.body!.getReader(); + const first = new TextDecoder().decode((await reader.read()).value); + const conversationId = parseEvents(first).find((e) => e.type === 'start').conversationId; + // Leave the page. + await reader.cancel(); + expect((await t.api('GET', `/api/ai/conversations/${conversationId}`)).body.conversation.running).toBe(true); + expect((await t.api('GET', '/api/ai/conversations')).body.conversations.find((c: any) => c.id === conversationId).running).toBe(true); + + // Come back: the stream replays from the start of the turn and runs to the end. + const replay = await sse(`/api/ai/chat/${conversationId}/stream`); + expect(replay[0].type).toBe('start'); + expect(replay.at(-1).type).toBe('done'); + const convo = await waitIdle(conversationId); + expect(convo.messages.map((m: any) => m.role)).toEqual(['user', 'assistant']); + expect(convo.messages[1].text.length).toBeGreaterThan(10); + // Following a finished conversation ends at once. + expect((await sse(`/api/ai/chat/${conversationId}/stream`)).map((e) => e.type)).toEqual(['done']); + }); + + it('stops only when asked, and refuses a second turn while one is running', async () => { + await connectAi(); + mock.delayChat(300); + const res = await t.raw('POST', '/api/ai/chat', { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ message: 'Tell me about digoxin' }) }); + const reader = res.body!.getReader(); + const conversationId = parseEvents(new TextDecoder().decode((await reader.read()).value)).find((e) => e.type === 'start').conversationId; + await reader.cancel(); + const busy = await sse('/api/ai/chat', { conversationId, message: 'And another thing' }); + expect(busy.find((e) => e.type === 'error').code).toBe('busy'); + expect((await t.api('POST', `/api/ai/chat/${conversationId}/stop`)).body.stopped).toBe(true); + const convo = await waitIdle(conversationId); + expect(convo.messages.at(-1).text).toContain('Stopped'); + expect((await t.api('POST', `/api/ai/chat/${conversationId}/stop`)).body.stopped).toBe(false); + }); + + it('passes tagged materials to the model and remembers them on the message', async () => { + await connectAi(); + const cls = (await t.api('POST', '/api/classes', { name: 'Pharm' })).body.class; + const quiz = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Cardiac drugs quiz' })).body.quiz; + const deck = (await t.api('POST', '/api/decks', { classId: cls.id, title: 'Antibiotics deck', cards: [{ front: 'a', back: 'b' }] })).body.deck; + + const items = (await t.api('GET', `/api/ai/mentionables?classId=${cls.id}`)).body.items as any[]; + expect(items.map((i) => i.title).sort()).toEqual(['Antibiotics deck', 'Cardiac drugs quiz']); + expect((await t.api('GET', '/api/ai/mentionables?q=antib')).body.items.map((i: any) => i.id)).toEqual([deck.id]); + + const events = await sse('/api/ai/chat', { message: 'Make flashcards from this', context: { classId: cls.id, items: [{ kind: 'quiz', id: quiz.id }, { kind: 'deck', id: 'no-such-deck-xyz' }] } }); + const call = mock.requests.find((r) => r.path.endsWith('/chat/completions'))!; + const system = JSON.stringify(call.body.messages[0]); + expect(system).toContain('tagged these materials'); + expect(system).toContain(`quiz \\"Cardiac drugs quiz\\" — id ${quiz.id}`); + expect(system).not.toContain('no-such-deck-xyz'); + const convo = await waitIdle(events[0].conversationId); + expect(convo.messages[0].tags).toEqual([{ kind: 'quiz', id: quiz.id, title: 'Cardiac drugs quiz' }]); + }); + + it('uses the model chosen for the chat', async () => { + const models = await connectAi(); + const small = models.find((m) => m.model === 'mock-small'); + const events = await sse('/api/ai/chat', { message: 'Explain the difference between left-sided and right-sided heart failure', modelId: small.id }); + expect(events[0]).toMatchObject({ type: 'start', model: small.label }); + expect(mock.requests.filter((r) => r.path.endsWith('/chat/completions')).every((r) => r.body.model === 'mock-small')).toBe(true); + }); +}); + +/* ------------------------------ semantic search default ------------------------------ */ + +describe('built-in embeddings', () => { + it('is the default until another choice is saved, and can be turned off', async () => { + setLocalEmbeddingDefault(true); + try { + expect(getEmbeddingSettings(t.ctx.db)).toMatchObject({ providerId: 'local', model: 'Xenova/bge-small-en-v1.5' }); + const status = (await t.api('GET', '/api/ai/embeddings')).body; + expect(status.local).toMatchObject({ model: 'Xenova/bge-small-en-v1.5', dimensions: 384 }); + await t.api('PUT', '/api/ai/embeddings', { providerId: null, model: '', dimensions: null }); + expect(getEmbeddingSettings(t.ctx.db).providerId).toBeNull(); + await t.api('PUT', '/api/ai/embeddings', { providerId: 'local', model: '', dimensions: null }); + expect(getEmbeddingSettings(t.ctx.db)).toMatchObject({ providerId: 'local', model: 'Xenova/bge-small-en-v1.5' }); + } finally { + setLocalEmbeddingDefault(false); + } + }); +}); diff --git a/web/src/app/Shell.tsx b/web/src/app/Shell.tsx index e4b4368..c3673bf 100644 --- a/web/src/app/Shell.tsx +++ b/web/src/app/Shell.tsx @@ -8,9 +8,10 @@ import { api, errorMessage } from '@/lib/api'; import { assistant, useAssistantState } from '@/lib/assistantStore'; import { qk, useClasses, useOverview, useRefresh } from '@/lib/queries'; import type { ClassInfo, SearchHit } from '@/lib/types'; -import { cx, modKey, useDebounced, useHotkeys, useIsDesktop, useTheme, type ThemeChoice } from '@/lib/util'; +import { cx, useDebounced, useHotkeys, useIsDesktop, useTheme, type ThemeChoice } from '@/lib/util'; import { Brand } from '@/pages/Auth'; -import { Button, ClassDot, Dialog, Field, IconButton, Input, Kbd, Menu, MenuItem, MenuLabel, MenuSeparator, Spinner, Textarea } from '@/ui'; +import { Button, ClassDot, Dialog, Field, IconButton, Input, Menu, MenuItem, MenuLabel, MenuSeparator, Spinner, Textarea, Tooltip } from '@/ui'; +import { SidebarDrafts, useDraftNotifications } from '@/features/DraftsTray'; const AssistantPanel = lazy(() => import('@/features/AssistantPanel')); @@ -260,18 +261,6 @@ function CommandPalette({ open, onOpenChange, onNewClass }: { open: boolean; onO )) )} -
- - ↑ - ↓ to move - - - Enter to open - - - Esc to close - -
); @@ -350,7 +339,6 @@ function Sidebar({ onSearch, onNewClass }: { onSearch: () => void; onNewClass: ( + +
Classes @@ -539,6 +529,7 @@ export function Shell() { const navigate = useNavigate(); const location = useLocation(); const { open: assistantOpen, seed } = useAssistantState(); + useDraftNotifications(); const onAssistantPage = location.pathname.startsWith('/assistant'); // Study screens lay themselves out edge to edge and bring their own header and footer. const immersive = location.pathname === '/review' || /^\/decks\/[^/]+\/cram$/.test(location.pathname); @@ -581,7 +572,8 @@ export function Shell() { setPaletteOpen(true)} onNewClass={() => setClassDialog(true)} />
{!immersive && setPaletteOpen(true)} onNewClass={() => setClassDialog(true)} />} -
+ {/* The assistant page lays itself out edge to edge; other pages sit in the reading column. */} +
@@ -600,15 +592,16 @@ export function Shell() { )} {!showDrawer && !onAssistantPage && !immersive && isDesktop && ( - + + + )} setClassDialog(true)} /> diff --git a/web/src/features/AiSettings.tsx b/web/src/features/AiSettings.tsx index f14161b..f1db932 100644 --- a/web/src/features/AiSettings.tsx +++ b/web/src/features/AiSettings.tsx @@ -5,7 +5,7 @@ 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 { 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'; @@ -19,6 +19,7 @@ const KIND_BLURB: Record = { 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.', }; @@ -182,6 +183,34 @@ function ProviderDialog({ open, onClose, adapters, existing, onSaved }: { open: /* ------------------------------ 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 = { + 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 }); @@ -189,6 +218,7 @@ function ModelDialog({ model, onClose, onSaved }: { model: AiModel | null; onClo 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; @@ -198,13 +228,14 @@ function ModelDialog({ model, onClose, onSaved }: { model: AiModel | null; onClo 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) }); + 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) { @@ -254,6 +285,9 @@ function ModelDialog({ model, onClose, onSaved }: { model: AiModel | null; onClo setPriceOut(e.target.value)} placeholder="Unknown" /> + + setPriceCached(e.target.value)} placeholder={model?.priceIn != null ? `About ${cachedGuess(model)}` : 'Unknown'} /> +
@@ -495,10 +529,11 @@ function TaskRow({ name, label, description, config, models, resolved, onChange @@ -536,13 +571,14 @@ function Usage() {
No AI requests in this period.
) : (
-
+
{[ ['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]) => ( -
+
{k}
{v}
@@ -555,6 +591,7 @@ function Usage() { Model Requests In + Cached Out Cost @@ -571,6 +608,7 @@ function Usage() { {m.calls} {formatTokens(m.input)} + {formatTokens(m.cached)} {formatTokens(m.output)} {m.unpriced > 0 && !m.cost ? 'no price set' : formatCost(m.cost)} diff --git a/web/src/features/AssistantPanel.tsx b/web/src/features/AssistantPanel.tsx index 6ca9657..fc33cb0 100644 --- a/web/src/features/AssistantPanel.tsx +++ b/web/src/features/AssistantPanel.tsx @@ -1,13 +1,13 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { ArrowUp, BookOpen, Check, ChevronRight, CircleAlert, FileText, History, Layers, LoaderCircle, NotebookPen, Play, Plus, Sparkles, Square, Trash2, X } from 'lucide-react'; +import { ArrowUp, AtSign, BookOpen, Check, ChevronDown, ChevronRight, CircleAlert, FileText, Folder, History, Layers, LoaderCircle, NotebookPen, Play, Plus, Sparkles, Square, Trash2, X } from 'lucide-react'; import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { Link, useNavigate } from 'react-router'; import { toast } from 'sonner'; import { api, errorMessage, streamEvents } from '@/lib/api'; import { assistant, useAssistantState } from '@/lib/assistantStore'; -import { useAiStatus, useClasses, useRefresh } from '@/lib/queries'; -import type { AssistantEvent, ChatMessage, ToolActivity, UiItem } from '@/lib/types'; -import { cx, plural, timeAgo } from '@/lib/util'; +import { useAiModels, useAiStatus, useClasses, useRefresh } from '@/lib/queries'; +import type { AssistantEvent, ChatMessage, Mentionable, TaggedItem, TagKind, ToolActivity, UiItem } from '@/lib/types'; +import { cx, plural, timeAgo, useDebounced, useLocalStorage } from '@/lib/util'; import { citedSources, Markdown, type Citation } from '@/ui/Markdown'; import { Badge, Button, Callout, IconButton, Menu, MenuItem, MenuLabel, MenuSeparator, Spinner } from '@/ui'; @@ -16,6 +16,7 @@ interface Conversation { title: string; classId: string | null; updatedAt: number; + running?: boolean; } const ITEM_URL = { quiz: 'quizzes', deck: 'decks', note: 'notes', document: 'documents', class: 'classes', attempt: 'session' } as const; @@ -172,6 +173,82 @@ function AssistantMessage({ message, onNavigate, onAction }: { message: ChatMess ); } +/* ------------------------------ composer: model picker and @ tags ------------------------------ */ + +const TAG_ICON: Record = { quiz: , deck: , note: , document: , folder: }; +const TAG_NOUN: Record = { quiz: 'Quiz', deck: 'Flashcards', note: 'Note', document: 'Document', folder: 'Folder' }; + +function TagChip({ item, onRemove, inverted }: { item: TaggedItem; onRemove?: () => void; inverted?: boolean }) { + return ( + svg]:size-3 [&>svg]:shrink-0', inverted ? 'bg-paper/15 text-paper' : 'bg-accent-soft text-accent')}> + {TAG_ICON[item.kind]} + {item.title} + {onRemove && ( + + )} + + ); +} + +/** Model chooser for this chat. "Default" follows the Assistant model set in Settings. */ +function ModelPicker({ value, onChange, defaultLabel }: { value: string; onChange: (id: string) => void; defaultLabel?: string }) { + const models = useAiModels(); + const usable = (models.data ?? []).filter((m) => m.available); + const chosen = usable.find((m) => m.id === value); + const providers = [...new Set(usable.map((m) => m.providerName))]; + return ( + + {chosen ? chosen.label : defaultLabel ? `${defaultLabel}` : 'Default model'} + + + } + > + Model for this chat + onChange('')} hint={!chosen ? : undefined}> + Default{defaultLabel ? ` · ${defaultLabel}` : ''} + + {providers.map((p) => ( +
+ + {p} + {usable + .filter((m) => m.providerName === p) + .map((m) => ( + onChange(m.id)} hint={m.id === value ? : !m.capabilities.tools ? 'no tools' : undefined}> + {m.label} + + ))} +
+ ))} +
+ ); +} + +/** Finds an "@query" being typed just before the caret. */ +function mentionAt(text: string, caret: number): { start: number; query: string } | null { + const before = text.slice(0, caret); + const m = /(^|\s)@([^\s@]{0,40})$/.exec(before); + return m ? { start: caret - m[2].length - 1, query: m[2] } : null; +} + +function useMentionables(classId: string | null, query: string, enabled: boolean) { + const debounced = useDebounced(query, 150); + // Inside a class: load its materials once and filter here. Elsewhere: search by title. + const q = useQuery({ + queryKey: ['ai', 'mentionables', classId ?? '', classId ? '' : debounced], + queryFn: () => api.get<{ items: Mentionable[] }>(`/api/ai/mentionables?${new URLSearchParams(classId ? { classId } : { q: debounced })}`).then((r) => r.items), + enabled: enabled && (Boolean(classId) || debounced.length >= 1), + staleTime: 30_000, + }); + const needle = query.toLowerCase(); + return (q.data ?? []).filter((i) => !needle || i.title.toLowerCase().includes(needle) || (i.folderName ?? '').toLowerCase().includes(needle)).slice(0, 40); +} + /* ------------------------------ panel ------------------------------ */ export default function AssistantPanel({ @@ -186,56 +263,166 @@ export default function AssistantPanel({ onConversationChange?: (id: string | null) => void; }) { const ai = useAiStatus(); + const models = useAiModels(); const classes = useClasses(); const refresh = useRefresh(); const qc = useQueryClient(); const { seed, context } = useAssistantState(); - const [conversationId, setConversationId] = useState(routeConversationId ?? null); + const initialConversation = variant === 'page' ? (routeConversationId ?? null) : assistant.drawerConversation; + const [conversationId, setConversationId] = useState(initialConversation); const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [busy, setBusy] = useState(false); const [loadingHistory, setLoadingHistory] = useState(false); const [focusDocs, setFocusDocs] = useState([]); - const abort = useRef(null); + const [tags, setTags] = useState([]); + const [mention, setMention] = useState<{ start: number; query: string } | null>(null); + const [mentionIndex, setMentionIndex] = useState(0); + const [modelId, setModelId] = useLocalStorage('sd.assistant.model', ''); + /** The connection to the running turn. Aborting it only detaches: the turn continues on the server. */ + const stream = useRef(null); + const stopRequested = useRef(false); + const conversationRef = useRef(initialConversation); const scroller = useRef(null); const inputRef = useRef(null); const stick = useRef(true); const handledSeed = useRef(0); const history = useQuery({ queryKey: ['ai', 'conversations'], queryFn: () => api.get<{ conversations: Conversation[] }>('/api/ai/conversations').then((r) => r.conversations), staleTime: 30_000 }); + const availableModel = (models.data ?? []).find((m) => m.id === modelId && m.available); + const effectiveModelId = availableModel ? modelId : ''; - const load = useCallback(async (id: string) => { - setLoadingHistory(true); - try { - const res = await api.get<{ messages: ChatMessage[] }>(`/api/ai/conversations/${id}`); - setMessages(res.messages); + const setConversation = useCallback( + (id: string | null) => { + conversationRef.current = id; setConversationId(id); - stick.current = true; - } catch { - setConversationId(null); - setMessages([]); - } finally { - setLoadingHistory(false); - } + if (variant === 'drawer') assistant.rememberDrawerConversation(id); + }, + [variant], + ); + + /** Applies one streamed event to the pending assistant message. */ + const applyEvent = useCallback((pendingId: string, e: AssistantEvent) => { + const patch = (fn: (m: ChatMessage) => ChatMessage) => setMessages((list) => list.map((m) => (m.id === pendingId ? fn(m) : m))); + if (e.type === 'start') patch((m) => ({ ...m, model: e.model })); + else if (e.type === 'text') patch((m) => ({ ...m, text: m.text + e.delta })); + else if (e.type === 'reasoning') patch((m) => ({ ...m, reasoning: (m.reasoning ?? '') + e.delta })); + else if (e.type === 'tool') patch((m) => ({ ...m, tools: m.tools.some((t) => t.id === e.tool.id) ? m.tools.map((t) => (t.id === e.tool.id ? e.tool : t)) : [...m.tools, e.tool] })); + else if (e.type === 'ui') patch((m) => ({ ...m, ui: [...m.ui, e.item] })); + else if (e.type === 'error') patch((m) => ({ ...m, error: e.message })); + }, []); + + /** Follows a turn (new or already running) until it finishes or this panel detaches. */ + const follow = useCallback( + async (pendingId: string, run: (signal: AbortSignal, onEvent: (e: AssistantEvent) => void) => Promise) => { + stream.current?.abort(); + const controller = new AbortController(); + stream.current = controller; + stopRequested.current = false; + setBusy(true); + let finished = false; + let startedId: string | null = null; + try { + await run(controller.signal, (e) => { + if (e.type === 'start') { + startedId = e.conversationId; + if (stopRequested.current) void api.post(`/api/ai/chat/${e.conversationId}/stop`).catch(() => undefined); + if (e.conversationId !== conversationRef.current) { + setConversation(e.conversationId); + onConversationChange?.(e.conversationId); + } + } + if (e.type === 'done') finished = true; + applyEvent(pendingId, e); + }); + } catch (e) { + if ((e as Error)?.name !== 'AbortError') setMessages((list) => list.map((m) => (m.id === pendingId ? { ...m, error: errorMessage(e) } : m))); + } finally { + if (stream.current === controller) { + stream.current = null; + setBusy(false); + } + // Detached (left the conversation): leave the turn running and the message as it is. + if (finished || !controller.signal.aborted) { + setMessages((list) => + list.map((m) => + m.id === pendingId + ? { ...m, pending: false, tools: m.tools.map((t) => (t.status === 'running' ? { ...t, status: 'error', summary: 'Stopped' } : t)), text: m.text || (m.error || m.ui.length || m.tools.length ? m.text : stopRequested.current ? '*(Stopped.)*' : m.text) } + : m, + ), + ); + void qc.invalidateQueries({ queryKey: ['ai', 'conversations'] }); + // Tools may have created drafts, classes or sessions. + void refresh(); + } + void startedId; + } + }, + [applyEvent, onConversationChange, qc, refresh, setConversation], + ); + + const load = useCallback( + async (id: string) => { + stream.current?.abort(); + setLoadingHistory(true); + try { + const res = await api.get<{ conversation: { id: string; running?: boolean }; messages: ChatMessage[] }>(`/api/ai/conversations/${id}`); + setConversation(id); + stick.current = true; + if (!res.conversation.running) { + setMessages(res.messages); + setBusy(false); + return; + } + // Still answering: show what is saved up to the question, then replay the live turn. + const lastUser = res.messages.map((m) => m.role).lastIndexOf('user'); + const pendingId = `p${Date.now()}`; + setMessages([...res.messages.slice(0, lastUser + 1), { id: pendingId, role: 'assistant', text: '', tools: [], ui: [], createdAt: Date.now(), pending: true }]); + setLoadingHistory(false); + void follow(pendingId, (signal, onEvent) => streamEvents(`/api/ai/chat/${id}/stream`, undefined, onEvent, signal)); + } catch { + setConversation(null); + setMessages([]); + } finally { + setLoadingHistory(false); + } + }, + [follow, setConversation], + ); + + // Open the remembered or routed conversation on mount. + useEffect(() => { + if (initialConversation) void load(initialConversation); + // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { - if (routeConversationId && routeConversationId !== conversationId) void load(routeConversationId); - if (!routeConversationId && variant === 'page' && conversationId && !busy) { - setConversationId(null); + if (variant !== 'page') return; + if (routeConversationId && routeConversationId !== conversationRef.current) void load(routeConversationId); + if (!routeConversationId && conversationRef.current && !busy) { + setConversation(null); setMessages([]); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [routeConversationId]); const newChat = useCallback(() => { - abort.current?.abort(); - setConversationId(null); + // Detach only: an answer still being written keeps going and is saved in its conversation. + stream.current?.abort(); + setBusy(false); + setConversation(null); setMessages([]); setFocusDocs([]); + setTags([]); onConversationChange?.(null); setTimeout(() => inputRef.current?.focus(), 30); - }, [onConversationChange]); + }, [onConversationChange, setConversation]); + + const stop = useCallback(() => { + stopRequested.current = true; + const id = conversationRef.current; + if (id) void api.post(`/api/ai/chat/${id}/stop`).catch(() => undefined); + }, []); const activeDocs = focusDocs.length > 0 ? focusDocs : context.documentIds; const className = classes.data?.find((c) => c.id === context.classId)?.name; @@ -244,47 +431,37 @@ export default function AssistantPanel({ async (text: string, opts: { conversationId?: string | null; documentIds?: string[]; classId?: string | null } = {}) => { const message = text.trim(); if (!message || busy) return; - const convo = opts.conversationId !== undefined ? opts.conversationId : conversationId; - setBusy(true); + const convo = opts.conversationId !== undefined ? opts.conversationId : conversationRef.current; + const sentTags = tags; setInput(''); + setTags([]); + setMention(null); stick.current = true; const pendingId = `p${Date.now()}`; - setMessages((m) => [...m, { id: `u${Date.now()}`, role: 'user', text: message, tools: [], ui: [], createdAt: Date.now() }, { id: pendingId, role: 'assistant', text: '', tools: [], ui: [], createdAt: Date.now(), pending: true }]); - const patch = (fn: (m: ChatMessage) => ChatMessage) => setMessages((list) => list.map((m) => (m.id === pendingId ? fn(m) : m))); - const controller = new AbortController(); - abort.current = controller; - let newId: string | null = null; - try { - await streamEvents( + setMessages((m) => [ + ...m, + { id: `u${Date.now()}`, role: 'user', text: message, tags: sentTags.length ? sentTags : undefined, tools: [], ui: [], createdAt: Date.now() }, + { id: pendingId, role: 'assistant', text: '', tools: [], ui: [], createdAt: Date.now(), pending: true }, + ]); + await follow(pendingId, (signal, onEvent) => + streamEvents( '/api/ai/chat', - { conversationId: convo, message, context: { classId: opts.classId !== undefined ? opts.classId : context.classId, documentIds: opts.documentIds ?? activeDocs } }, - (e) => { - if (e.type === 'start') { - newId = e.conversationId; - patch((m) => ({ ...m, model: e.model })); - } else if (e.type === 'text') patch((m) => ({ ...m, text: m.text + e.delta })); - else if (e.type === 'reasoning') patch((m) => ({ ...m, reasoning: (m.reasoning ?? '') + e.delta })); - else if (e.type === 'tool') patch((m) => ({ ...m, tools: m.tools.some((t) => t.id === e.tool.id) ? m.tools.map((t) => (t.id === e.tool.id ? e.tool : t)) : [...m.tools, e.tool] })); - else if (e.type === 'ui') patch((m) => ({ ...m, ui: [...m.ui, e.item] })); - else if (e.type === 'error') patch((m) => ({ ...m, error: e.message })); + { + conversationId: convo, + message, + modelId: effectiveModelId || undefined, + context: { + classId: opts.classId !== undefined ? opts.classId : context.classId, + documentIds: opts.documentIds ?? activeDocs, + items: sentTags.map((t) => ({ kind: t.kind, id: t.id })), + }, }, - controller.signal, - ); - } catch (e) { - if ((e as Error)?.name !== 'AbortError') patch((m) => ({ ...m, error: errorMessage(e) })); - } finally { - patch((m) => ({ ...m, pending: false, tools: m.tools.map((t) => (t.status === 'running' ? { ...t, status: 'error', summary: 'Stopped' } : t)), text: m.text || (m.error || m.ui.length || m.tools.length ? m.text : controller.signal.aborted ? '*(Stopped.)*' : m.text) })); - setBusy(false); - if (newId && newId !== convo) { - setConversationId(newId); - onConversationChange?.(newId); - } - void qc.invalidateQueries({ queryKey: ['ai', 'conversations'] }); - // Tools may have created drafts, classes or sessions. - void refresh(); - } + onEvent, + signal, + ), + ); }, - [busy, conversationId, context.classId, activeDocs, onConversationChange, qc, refresh], + [busy, tags, follow, effectiveModelId, context.classId, activeDocs], ); // Requests from elsewhere in the app ("Ask about this document", "New questions on these topics"). @@ -293,8 +470,9 @@ export default function AssistantPanel({ handledSeed.current = seed.id; assistant.consumeSeed(); if (seed.newChat) { - abort.current?.abort(); - setConversationId(null); + stream.current?.abort(); + setBusy(false); + setConversation(null); setMessages([]); onConversationChange?.(null); } @@ -304,14 +482,15 @@ export default function AssistantPanel({ if (seed.prompt) setInput(seed.prompt); setTimeout(() => inputRef.current?.focus(), 60); } - }, [seed, ai.data, send, onConversationChange]); + }, [seed, ai.data, send, onConversationChange, setConversation]); // Keep the newest content in view unless the user has scrolled up to read. useEffect(() => { const el = scroller.current; if (el && stick.current) el.scrollTop = el.scrollHeight; }, [messages]); - useEffect(() => () => abort.current?.abort(), []); + // Leaving only disconnects; the server finishes the answer and saves it. + useEffect(() => () => stream.current?.abort(), []); const updateAction = (actionId: string, status: string, result: string) => { setMessages((list) => list.map((m) => ({ ...m, ui: m.ui.map((u) => (u.type === 'action' && u.actionId === actionId ? { ...u, status: status as typeof u.status, result } : u)) }))); @@ -329,16 +508,54 @@ export default function AssistantPanel({ }, [activeDocs.length, className]); const contextLabel = activeDocs.length > 0 ? (context.documentIds.length && context.label ? context.label : plural(activeDocs.length, 'document')) : className; + const options = useMentionables(context.classId, mention?.query ?? '', mention !== null); + const pickable = options.filter((o) => !tags.some((t) => t.kind === o.kind && t.id === o.id)); + + const updateMention = (text: string, caret: number) => { + const next = mentionAt(text, caret); + setMention(next); + if (!next || next.query !== mention?.query) setMentionIndex(0); + }; + const choose = (item: Mentionable) => { + setTags((t) => (t.some((x) => x.kind === item.kind && x.id === item.id) ? t : [...t, { kind: item.kind, id: item.id, title: item.title }])); + if (mention) { + const caret = inputRef.current?.selectionStart ?? input.length; + const next = `${input.slice(0, mention.start)}${input.slice(caret)}`.replace(/\s{2,}/g, ' '); + setInput(next); + setTimeout(() => { + const el = inputRef.current; + if (el) { + el.focus(); + el.setSelectionRange(mention.start, mention.start); + } + }, 0); + } + setMention(null); + }; + const openPicker = () => { + const el = inputRef.current; + const caret = el?.selectionStart ?? input.length; + const needsSpace = caret > 0 && !/\s/.test(input[caret - 1] ?? ''); + const next = `${input.slice(0, caret)}${needsSpace ? ' ' : ''}@${input.slice(caret)}`; + setInput(next); + const at = caret + (needsSpace ? 1 : 0) + 1; + setMention({ start: at - 1, query: '' }); + setMentionIndex(0); + setTimeout(() => { + el?.focus(); + el?.setSelectionRange(at, at); + }, 0); + }; return (
-
- +
+
Assistant
-
{ai.data?.resolved.chat ? ai.data.resolved.chat.label : ai.data && !ai.data.ready ? 'Not set up' : ''}
+ {ai.data?.ready ? :
{ai.data ? 'Not set up' : ''}
}
@@ -354,7 +571,7 @@ export default function AssistantPanel({ Recent conversations {(history.data ?? []).length === 0 &&
Nothing yet
} {(history.data ?? []).slice(0, 12).map((c) => ( - load(c.id)} hint={timeAgo(c.updatedAt)}> + load(c.id)} hint={c.running ? 'working…' : timeAgo(c.updatedAt)}> {c.title} ))} @@ -365,7 +582,9 @@ export default function AssistantPanel({ icon={} danger onSelect={async () => { - await api.del(`/api/ai/conversations/${conversationId}`).catch(() => undefined); + const id = conversationId; + stop(); + await api.del(`/api/ai/conversations/${id}`).catch(() => undefined); void qc.invalidateQueries({ queryKey: ['ai', 'conversations'] }); newChat(); }} @@ -385,7 +604,7 @@ export default function AssistantPanel({ const el = e.currentTarget; stick.current = el.scrollHeight - el.scrollTop - el.clientHeight < 80; }} - className={cx('min-h-0 flex-1 overflow-y-auto px-4 py-5', variant === 'page' && 'lg:px-1')} + className={cx('min-h-0 flex-1 overflow-y-auto px-4 py-5', variant === 'page' && 'lg:px-6')} aria-live="polite" > {ai.isLoading || loadingHistory ? ( @@ -395,18 +614,18 @@ export default function AssistantPanel({ ) : !ai.data?.ready ? (
} title="Connect an AI model to use the assistant"> - Add a provider such as OpenAI, Anthropic, Google Gemini or a local model, then switch on a model. + Add a provider such as OpenAI, Anthropic, Google Gemini, DeepSeek or a local model, then switch on a model.
+ Open AI settings +
) : messages.length === 0 ? (

What are we working on?

-

I can read your documents, look at your results, and prepare quizzes, flashcards and study guides for you to review.

+

I can read your documents, look at your results, and prepare quizzes, flashcards and study guides for you to review. Type @ to point me at a specific quiz, deck, note or document.

{suggestions.map((s) => (
{ai.data?.ready && ( -
-
+
+
{contextLabel && (
@@ -447,41 +675,104 @@ export default function AssistantPanel({ )}
)} + {mention && ( +
+
{context.classId ? `Materials in ${className ?? 'this class'}` : 'Your library'}
+ {pickable.length === 0 ? ( +
{context.classId || mention.query ? 'Nothing matches.' : 'Type to search your quizzes, decks, notes and documents.'}
+ ) : ( + pickable.map((o, i) => ( + + )) + )} +
+ )}
{ e.preventDefault(); - if (busy) abort.current?.abort(); + if (busy) stop(); else void send(input); }} > -