Second revision: MCP servers, shared-link AI, flashcard modes, document previews
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
Generation - Every quiz is NCLEX style; "Practice exam" writes 50 questions. - Calculation questions only when the material involves calculations (max 5 per exam). - Broad topics: the class's topics are listed in the prompt, a quick grouping call maps narrow labels onto broad ones, and leftover variants are merged. - Exact select-all count per batch; option order shuffled. - Python check: at most 3 runs, fixed problems count as passed, cosmetic remarks ignored. Assistant - Text, tool steps and cards appear in the order they happen, with paragraph breaks between steps. - Optional thinking box: fixed height, follows the newest text, can be scrolled back. - "+" opens a material picker listing recent items; items can be dragged in from class pages; shorter placeholder. MCP servers - Streamable HTTP client; encrypted headers. - Per-server instructions and switches for chat, Explain, generation and the quiz check. - Settings UI. Share links - Optional "Explain this" and "Ask about this material" per link, off by default. - Items are loaded on the server and checked against the share; no tools; rate-limited. - Migration 4 stores the switches. Study and documents - Standard flashcard mode (optionally repeat missed cards until known) next to spaced repetition. - Streak visuals restyled in the app palette. - Side by side / Original document views, with sandboxed previews of Word and PowerPoint files. Other - Built-in embedding model baked into the image; sharp stubbed if it cannot load; real load error shown in Settings. - Python runner blocks network and process modules in JavaScript too. - "View questions and answers" removed from the quiz start screen. - Docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5ea61c882c
commit
031f7bec31
45 changed files with 2918 additions and 397 deletions
|
|
@ -21,6 +21,7 @@ import { dueCounts } from '../services/srs.ts';
|
|||
import { topicStats, totals } from '../services/stats.ts';
|
||||
import { newId, now, parseJson } from '../util.ts';
|
||||
import { cancelGeneration, createDraft, getDraft, startGeneration, startImprove } from './generate.ts';
|
||||
import { loadMcpTools, mcpPromptNote } from './mcp.ts';
|
||||
import { runPython } from './sandbox.ts';
|
||||
import { resolveModel, runChat } from './service.ts';
|
||||
import { pythonOutput, pythonPrelude, quizForPython, RUN_PYTHON_DESCRIPTION } from './verify.ts';
|
||||
|
|
@ -46,6 +47,8 @@ export type AssistantEvent =
|
|||
| { type: 'start'; conversationId: string; model: string }
|
||||
| { type: 'text'; delta: string }
|
||||
| { type: 'reasoning'; delta: string }
|
||||
/** A new model step begins (after tool results): text that follows is a new paragraph. */
|
||||
| { type: 'step' }
|
||||
| { type: 'tool'; tool: ToolActivity }
|
||||
| { type: 'ui'; item: UiItem }
|
||||
| { type: 'error'; message: string; code: string; detail?: string }
|
||||
|
|
@ -380,8 +383,8 @@ const TOOLS: ToolSpec[] = [
|
|||
class_id: { ...S, description: 'Class the quiz belongs to. Defaults to the current class.' },
|
||||
title: S,
|
||||
topic: { ...S, description: 'What the questions should focus on, e.g. "pediatric respiratory disorders". Also used to pick the relevant passages.' },
|
||||
count: { type: 'integer', description: 'Number of questions, 1-100. Default 15.' },
|
||||
style: { type: 'string', enum: ['nclex', 'standard', 'recall'], description: 'nclex = clinical-judgment items (default).' },
|
||||
count: { type: 'integer', description: 'Number of questions, 1-100. Default 15, or 50 for an exam.' },
|
||||
format: { type: 'string', enum: ['practice', 'exam'], description: 'exam = a full practice exam (50 questions by default, at most 5 calculations). Use it when the user asks for an exam or test.' },
|
||||
difficulty: { type: 'string', enum: ['easy', 'medium', 'hard', 'mixed'] },
|
||||
sata_share: { type: 'number', description: 'Fraction (0-1) of select-all-that-apply questions.' },
|
||||
document_ids: { type: 'array', items: S, description: 'Documents to base the quiz on.' },
|
||||
|
|
@ -393,15 +396,15 @@ const TOOLS: ToolSpec[] = [
|
|||
[],
|
||||
),
|
||||
},
|
||||
label: (i) => `Writing ${i.count ?? 15} quiz questions`,
|
||||
label: (i) => `Writing ${i.count ?? (i.format === 'exam' ? 50 : 15)} quiz questions`,
|
||||
run: (input, env) =>
|
||||
runGenerate(env, {
|
||||
kind: 'quiz',
|
||||
classId: requireClass(env, input.class_id),
|
||||
title: input.title || undefined,
|
||||
focus: input.topic || undefined,
|
||||
count: Math.max(1, Math.min(100, Number(input.count) || 15)),
|
||||
style: input.style || 'nclex',
|
||||
count: Math.max(1, Math.min(100, Number(input.count) || (input.format === 'exam' ? 50 : 15))),
|
||||
format: input.format === 'exam' ? 'exam' : 'practice',
|
||||
difficulty: input.difficulty || undefined,
|
||||
sataShare: typeof input.sata_share === 'number' ? Math.max(0, Math.min(1, input.sata_share)) : undefined,
|
||||
instructions: input.instructions || undefined,
|
||||
|
|
@ -792,8 +795,13 @@ interface MessageMeta {
|
|||
ui?: UiItem[];
|
||||
model?: string;
|
||||
error?: string;
|
||||
/** The model's reasoning text for this step, when it shows any (kept for the "Thinking" view). */
|
||||
reasoning?: string;
|
||||
}
|
||||
|
||||
/** The order things happened in one assistant reply: text, tool steps and cards interleaved. */
|
||||
export type MessagePart = { kind: 'text'; text: string } | { kind: 'tool'; id: string } | { kind: 'ui'; index: number };
|
||||
|
||||
export interface DisplayMessage {
|
||||
id: number;
|
||||
role: 'user' | 'assistant';
|
||||
|
|
@ -801,6 +809,8 @@ export interface DisplayMessage {
|
|||
tags?: TaggedItem[];
|
||||
tools: ToolActivity[];
|
||||
ui: UiItem[];
|
||||
parts?: MessagePart[];
|
||||
reasoning?: string;
|
||||
model?: string;
|
||||
error?: string;
|
||||
createdAt: number;
|
||||
|
|
@ -822,12 +832,24 @@ export function displayMessages(db: DB, conversationId: string): DisplayMessage[
|
|||
continue;
|
||||
}
|
||||
if (!current) {
|
||||
current = { id: row.id, role: 'assistant', text: '', tools: [], ui: [], createdAt: row.created_at };
|
||||
current = { id: row.id, role: 'assistant', text: '', tools: [], ui: [], parts: [], createdAt: row.created_at };
|
||||
out.push(current);
|
||||
}
|
||||
if (text) current.text += (current.text ? '\n\n' : '') + text;
|
||||
if (meta.tools) current.tools.push(...meta.tools);
|
||||
if (meta.ui) current.ui.push(...meta.ui);
|
||||
const parts = current.parts!;
|
||||
// Within one step the model writes its text first, then calls tools; cards come from the tools.
|
||||
if (text) {
|
||||
current.text += (current.text ? '\n\n' : '') + text;
|
||||
parts.push({ kind: 'text', text });
|
||||
}
|
||||
if (meta.tools) {
|
||||
current.tools.push(...meta.tools);
|
||||
for (const t of meta.tools) parts.push({ kind: 'tool', id: t.id });
|
||||
}
|
||||
if (meta.ui) {
|
||||
for (const u of meta.ui) parts.push({ kind: 'ui', index: current.ui.length + meta.ui.indexOf(u) });
|
||||
current.ui.push(...meta.ui);
|
||||
}
|
||||
if (meta.reasoning) current.reasoning = `${current.reasoning ? `${current.reasoning}\n\n` : ''}${meta.reasoning}`;
|
||||
if (meta.model) current.model = meta.model;
|
||||
if (meta.error) current.error = meta.error;
|
||||
}
|
||||
|
|
@ -928,6 +950,7 @@ function systemPrompt(app: AppContext, context: AssistantContext): string {
|
|||
- For "what should I study" questions, call get_performance and give specific, prioritised advice.
|
||||
- When a request is clear, act on it without asking for permission. Ask one short question only when a missing detail would change the result (for example which class, when it is ambiguous).
|
||||
- Be concise and concrete. Use Markdown. Teach like a good clinical instructor: explain the why, connect to patient care, and prefer current standard practice.
|
||||
- Talk while you work, like a person would: before a tool call that does real work (generating, checking, searching several places), first say in one short sentence what you are about to do ("I'll write 20 questions from the lecture, then check them with Python."), then call the tool in the same reply. After each step, say briefly what happened before the next one.
|
||||
- After a tool produces a draft or a session link, reply with one or two sentences; the card already shows the details.
|
||||
- Use run_python whenever exactness matters: dosage and rate calculations, unit conversions, counting questions, options or correct answers, checking that a quiz meets what was asked for. Quizzes from generate_quiz are already checked this way; when you write questions yourself with create_draft, check the draft with run_python (pass its draft_id) before telling the user it is ready, and fix problems you find.`,
|
||||
);
|
||||
|
|
@ -1105,9 +1128,20 @@ export async function runAssistantTurn(
|
|||
emit({ type: 'start', conversationId, model: resolved.model.label });
|
||||
|
||||
const env: ToolEnv = { app, conversationId, context, signal, modelId: req.modelId };
|
||||
const tools = resolved.model.capabilities.tools ? TOOLS : [];
|
||||
// Tools from the user's MCP servers join the built-in ones for this turn.
|
||||
const mcp = resolved.model.capabilities.tools ? await loadMcpTools(app, 'chat', signal) : { tools: [], servers: [], unavailable: [] };
|
||||
const mcpSpecs: ToolSpec[] = mcp.tools.map((b) => ({
|
||||
def: b.def,
|
||||
label: () => `${b.serverName}: ${b.toolName}`,
|
||||
run: async (input, e) => {
|
||||
const text = await b.call(input, e.signal);
|
||||
return { output: text, summary: clip(text.replace(/\s+/g, ' ').trim(), 100) };
|
||||
},
|
||||
}));
|
||||
const tools = resolved.model.capabilities.tools ? [...TOOLS, ...mcpSpecs] : [];
|
||||
const system =
|
||||
systemPrompt(app, context) +
|
||||
mcpPromptNote(mcp) +
|
||||
(tools.length === 0 ? '\n\nNote: the selected model cannot use tools, so you cannot read the library or create drafts in this conversation. Say so if the user asks for that, and suggest choosing a tool-capable model in Settings.' : '');
|
||||
const window = resolved.model.contextWindow ?? 32_000;
|
||||
const historyBudget = Math.max(6_000, Math.floor(window * 0.6));
|
||||
|
|
@ -1115,6 +1149,8 @@ export async function runAssistantTurn(
|
|||
try {
|
||||
for (let step = 0; step < MAX_STEPS; step += 1) {
|
||||
if (signal?.aborted) throw new AIError('aborted', 'Stopped.');
|
||||
let stepReasoning = '';
|
||||
if (step > 0) emit({ type: 'step' });
|
||||
const result = await runChat(app, {
|
||||
task: 'chat',
|
||||
purpose: 'assistant',
|
||||
|
|
@ -1126,7 +1162,10 @@ export async function runAssistantTurn(
|
|||
signal,
|
||||
onEvent: (e) => {
|
||||
if (e.type === 'text') emit({ type: 'text', delta: e.delta });
|
||||
else if (e.type === 'reasoning') emit({ type: 'reasoning', delta: e.delta });
|
||||
else if (e.type === 'reasoning') {
|
||||
stepReasoning += e.delta;
|
||||
emit({ type: 'reasoning', delta: e.delta });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -1136,7 +1175,7 @@ export async function runAssistantTurn(
|
|||
const results: ContentPart[] = [];
|
||||
|
||||
for (const call of calls) {
|
||||
const spec = TOOLS.find((t) => t.def.name === call.name);
|
||||
const spec = tools.find((t) => t.def.name === call.name);
|
||||
const activity: ToolActivity = { id: call.id, name: call.name, label: spec ? spec.label(call.input) : call.name, status: 'running' };
|
||||
emit({ type: 'tool', tool: { ...activity } });
|
||||
let output: string;
|
||||
|
|
@ -1166,7 +1205,7 @@ export async function runAssistantTurn(
|
|||
emit({ type: 'tool', tool: { ...activity } });
|
||||
}
|
||||
|
||||
save('assistant', result.message, { tools: activities, ui, model: resolved.model.label });
|
||||
save('assistant', result.message, { tools: activities, ui, model: resolved.model.label, ...(stepReasoning ? { reasoning: stepReasoning.slice(-20_000) } : {}) });
|
||||
if (calls.length === 0) {
|
||||
if (result.stopReason === 'max_tokens') emit({ type: 'text', delta: '\n\n*(The reply was cut off by the output limit. Ask me to continue.)*' });
|
||||
if (result.stopReason === 'refusal' && !result.text) emit({ type: 'text', delta: 'I can\'t help with that request.' });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue