Separate settings per generation step; explain Practice and Exam on shares
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 29s

- Settings -> AI: Planning, Writing, Python check and Second review each get
  their own model, reasoning effort, temperature and output limit, shown under
  Content generation. Unset choices follow Content generation, then the
  default model.
- Shared quizzes say what Practice and Exam mean under the mode toggle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 11:08:21 -07:00
parent 2454ab6e95
commit 10be5c39da
9 changed files with 128 additions and 36 deletions

View file

@ -778,7 +778,7 @@ export function startGeneration(
if (!folder || (req.classId && folder.class_id !== req.classId)) throw badRequest('That exam is not part of this class.');
}
// Fail fast (before creating a draft) when no model is configured.
const resolved = resolveModel(app, 'generate', req.modelId);
const resolved = resolveModel(app, 'write', req.modelId);
const draftId = createDraft(app.db, {
kind: req.kind,
classId: req.classId ?? null,
@ -856,7 +856,7 @@ async function runWithRestart(app: AppContext, draftId: string, req: GenerateReq
async function runGeneration(app: AppContext, draftId: string, req: GenerateRequest, signal: AbortSignal): Promise<void> {
const { db } = app;
const resolved = resolveModel(app, 'generate', req.modelId);
const resolved = resolveModel(app, 'write', req.modelId);
const window = resolved.model.contextWindow ?? 32_000;
const budgetTokens = Math.max(2_500, Math.min(Math.floor(window * 0.4), 28_000));
setProgress(db, draftId, 0.03, 'Reading your material');
@ -944,7 +944,7 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
const user = [math ? '' : sourceBlock(sources, index), ask.filter(Boolean).join('\n')].filter(Boolean).join('\n\n======\n\n');
const result = await generateStructured(app, {
task: 'generate',
task: 'write',
purpose: isQuiz ? 'quiz generation' : 'flashcard generation',
modelId: req.modelId,
system,
@ -992,7 +992,7 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
if (!isQuiz || n <= 0) return [];
try {
const result = await generateStructured(app, {
task: 'generate',
task: 'plan',
purpose: 'planning questions',
modelId: req.modelId,
system: `${baseSystem(db, req, hasSources)}\n\nYou plan NCLEX-style quizzes. List distinct angles, one per question: each names the client situation and the single decision or fact it tests, in one line (for example "Continuous IV heparin, aPTT 3x control with gum bleeding: first action"). No two angles may test the same decision, fact or teaching point, even in a different scenario. Spread them over the client-needs areas the material supports (pharmacology, safety, prioritisation, delegation, teaching, lab values, complications, psychosocial). When the material is short, vary the situations and the area tested rather than repeating its points.`,
@ -1016,7 +1016,7 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
toolName: 'plan_questions',
toolDescription: 'Submit the planned angles.',
schema: { type: 'object', properties: { angles: { type: 'array', items: { type: 'string' } } }, required: ['angles'] },
maxOutputTokens: outputBudget(resolved, 8_000),
maxOutputTokens: outputBudget(resolveModel(app, 'plan', req.modelId), 8_000),
signal,
});
const list = (result.data as { angles?: unknown }).angles;
@ -1271,7 +1271,7 @@ export async function nameSet(
async function generateNote(app: AppContext, draftId: string, req: GenerateRequest, signal: AbortSignal, budgetTokens: number, window: number): Promise<void> {
const { db } = app;
const resolved = resolveModel(app, 'generate', req.modelId);
const resolved = resolveModel(app, 'write', req.modelId);
const style = req.noteStyle ?? 'study_guide';
// A guide reads the material in as few passes as possible; allow a bigger slice per pass than item generation.
const perPass = Math.max(budgetTokens, Math.min(Math.floor(window * 0.55), 90_000));
@ -1304,7 +1304,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque
if (passes.length <= 1) {
setProgress(db, draftId, 0.2, 'Writing');
const body = [passes[0]?.length ? `SOURCE EXCERPTS:\n\n${formatExcerpts(passes[0])}` : '', sources.inline, ask].filter(Boolean).join('\n\n======\n\n');
const res = await runChatWithMcp(app, { task: 'generate', purpose: 'study guide', modelId: req.modelId, system, messages: [{ role: 'user', content: [{ type: 'text', text: body }] }], maxOutputTokens: outputBudget(resolved, 16_000), signal }, 'generate');
const res = await runChatWithMcp(app, { task: 'write', purpose: 'study guide', modelId: req.modelId, system, messages: [{ role: 'user', content: [{ type: 'text', text: body }] }], maxOutputTokens: outputBudget(resolved, 16_000), signal }, 'generate');
content = res.text;
} else {
// Long material: condense each part, then write the final piece from the condensed notes.
@ -1313,7 +1313,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque
if (signal.aborted) throw new AIError('aborted', 'Generation was cancelled.');
setProgress(db, draftId, 0.05 + (0.7 * i) / passes.length, `Reading part ${i + 1} of ${passes.length}`);
const res = await runChat(app, {
task: 'generate',
task: 'write',
purpose: 'study guide',
modelId: req.modelId,
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.`,
@ -1327,7 +1327,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque
const res = await runChatWithMcp(
app,
{
task: 'generate',
task: 'write',
purpose: 'study guide',
modelId: req.modelId,
system,
@ -1387,7 +1387,7 @@ export function startImprove(app: AppContext, input: z.input<typeof improveReque
if (req.questionIds?.length) questions = questions.filter((q) => req.questionIds!.includes(q.id));
if (questions.length === 0) throw badRequest('No questions to improve');
questions = questions.slice(0, 60);
const resolved = resolveModel(app, 'generate', req.modelId);
const resolved = resolveModel(app, 'write', req.modelId);
const draftId = createDraft(app.db, {
kind: 'quiz',
classId: quiz.class_id,
@ -1420,7 +1420,7 @@ Return the questions in the same order, one output question per input question.$
)
.join('\n\n');
const result = await generateStructured(app, {
task: 'generate',
task: 'write',
purpose: 'improve questions',
modelId: req.modelId,
system,

View file

@ -64,12 +64,20 @@ interface ModelRow {
price_cached_in: number | null;
}
export const TASKS = ['chat', 'generate', 'vision', 'quick'] as const;
export const TASKS = ['chat', 'generate', 'plan', 'write', 'check', 'review', 'vision', 'quick'] as const;
export type TaskName = (typeof TASKS)[number];
export const TASK_INFO: Record<TaskName, { label: string; description: string }> = {
/**
* Tasks, and the steps of content generation. A step with nothing set uses Content generation's
* model and parameters (then the default's), so the steps only matter once the user sets them.
*/
export const TASK_INFO: Record<TaskName, { label: string; description: string; parent?: TaskName }> = {
chat: { label: 'Assistant', description: 'The assistant that searches your library, answers questions and runs tools.' },
generate: { label: 'Content generation', description: 'Writing quizzes, flashcards and study guides. Benefits most from a strong model.' },
generate: { label: 'Content generation', description: 'Quizzes, flashcards and study guides. Each step below can have its own settings.' },
plan: { label: 'Planning', description: 'Lists a distinct angle for every question before writing.', parent: 'generate' },
write: { label: 'Writing', description: 'Writes the questions, flashcards and study guides.', parent: 'generate' },
check: { label: 'Python check', description: 'Checks counts, answer keys and calculations with Python.', parent: 'generate' },
review: { label: 'Second review', description: 'Reads every question for badly written items.', parent: 'generate' },
vision: { label: 'Vision and OCR', description: 'Reading scanned PDFs and images. Needs a model that accepts images.' },
quick: { label: 'Quick explanations', description: 'Short explanations while studying. A fast, inexpensive model works well.' },
};
@ -379,7 +387,7 @@ export function getTaskSettings(db: DB): TaskSettings {
}
export function saveTaskSettings(db: DB, input: unknown): TaskSettings {
const schema = z.object({ default: taskConfigSchema, chat: taskConfigSchema, generate: taskConfigSchema, vision: taskConfigSchema, quick: taskConfigSchema }).partial();
const schema = z.object(Object.fromEntries(['default', ...TASKS].map((t) => [t, taskConfigSchema])) as Record<TaskName | 'default', typeof taskConfigSchema>).partial();
const patch = schema.parse(input);
const next = { ...getTaskSettings(db), ...patch } as TaskSettings;
setSetting(db, 'ai.tasks', next);
@ -399,7 +407,10 @@ export interface ResolvedModel {
export function resolveModel(app: AppContext, task: TaskName, overrideModelId?: string | null): ResolvedModel {
const tasks = getTaskSettings(app.db);
const cfg = tasks[task];
const candidates = [overrideModelId, cfg.modelId, tasks.default.modelId].filter((x): x is string => Boolean(x));
// A generation step falls back to Content generation, then to the default.
const parent = TASK_INFO[task].parent;
const chain = parent ? [cfg, tasks[parent]] : [cfg];
const candidates = [overrideModelId, ...chain.map((c) => c.modelId), tasks.default.modelId].filter((x): x is string => Boolean(x));
let model: ModelDto | undefined;
for (const id of candidates) {
const row = app.db.get<ModelRow>(`${MODEL_SELECT} WHERE m.id = ? AND p.enabled = 1`, id);
@ -420,12 +431,17 @@ export function resolveModel(app: AppContext, task: TaskName, overrideModelId?:
);
}
const provider = providerConfig(app, providerRow(app.db, model.providerId));
const own = cfg.modelId === model.id || (!cfg.modelId && tasks.default.modelId === model.id) ? cfg : EMPTY_TASK;
// A level's parameters apply when it chose this model, or chose none and inherits this model.
const applies = chain.filter((level, i) => {
const inherited = [...chain.slice(i + 1), tasks.default].find((l) => l.modelId)?.modelId ?? null;
return level.modelId ? level.modelId === model.id : inherited === model.id;
});
const fallback = tasks.default;
const pick = <K extends 'temperature' | 'reasoningEffort' | 'maxOutputTokens'>(key: K): TaskConfig[K] => applies.map((l) => l[key]).find((v) => v !== null) ?? fallback[key];
const params: ResolvedModel['params'] = {};
const temperature = own.temperature ?? fallback.temperature;
const effort = own.reasoningEffort ?? fallback.reasoningEffort;
const maxOut = own.maxOutputTokens ?? fallback.maxOutputTokens;
const temperature = pick('temperature');
const effort = pick('reasoningEffort');
const maxOut = pick('maxOutputTokens');
if (temperature !== null && model.capabilities.temperature) params.temperature = temperature;
if (effort !== null && model.capabilities.reasoning) params.reasoningEffort = effort;
if (maxOut !== null) params.maxOutputTokens = maxOut;

View file

@ -274,7 +274,7 @@ export async function verifyQuizDraft(
req: QuizRequestFacts,
opts: { modelId?: string | null; signal?: AbortSignal; onStep?: (label: string) => void } = {},
): Promise<QuizCheck> {
const resolved = resolveModel(app, 'generate', opts.modelId);
const resolved = resolveModel(app, 'check', opts.modelId);
if (!resolved.model.capabilities.tools) {
return { draft, notes: ['The Python answer check was skipped: the generation model cannot use tools.'], review: null, pythonRuns: 0, failed: [], applied: [], refused: [], removed: 0, rewritten: 0, incomplete: true };
}
@ -306,7 +306,7 @@ export async function verifyQuizDraft(
else messages.push({ role: 'user', content: [nudge] });
}
const res = await runChat(app, {
task: 'generate',
task: 'check',
purpose: 'quiz check',
modelId: opts.modelId,
system,
@ -474,7 +474,7 @@ export async function proofreadQuiz(
opts: { modelId?: string | null; signal?: AbortSignal } = {},
): Promise<{ draft: QuizDraft; flagged: number; fixes: QuizFix[]; applied: string[]; refused: string[]; rewritten: number; removed: number; incomplete: boolean }> {
const none = { draft, flagged: 0, fixes: [], applied: [], refused: [], rewritten: 0, removed: 0 };
const resolved = resolveModel(app, 'generate', opts.modelId);
const resolved = resolveModel(app, 'review', opts.modelId);
if (!resolved.model.capabilities.tools) return { ...none, incomplete: true };
const data = quizForPython(draft);
const listing = data.questions
@ -484,7 +484,7 @@ export async function proofreadQuiz(
const prompt = `${hints.length ? `Wording that looks like a leftover or a change of mind (confirm and fix each):\n${hints.map((h) => `- ${h}`).join('\n')}\n\n` : ''}THE QUIZ (${data.question_count} questions):\n\n${listing}`;
try {
const res = await runChat(app, {
task: 'generate',
task: 'review',
purpose: 'quiz second review',
modelId: opts.modelId,
system: PROOFREAD_SYSTEM,