Share-link generation and med math on request
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 34s

- Visitors of class and exam links can generate NCLEX-style quizzes (default
  and maximum 50 questions, 25% select-all, Python-checked, optional 5 med
  math questions) and flashcard sets from chosen class materials, once the
  owner turns it on for the link.
- 4 quizzes and 2 flashcard sets per link per day; failed attempts do not
  count; "Reset limits" in the share dialog gives a fresh allowance.
- Sets are drafts tied to the link (migration 7): listed for every visitor,
  explainable, kept off the owner's drafts list, and reviewable or savable
  from the share dialog.
- Generate gains "Include med math" with a count (default 5): exactly that
  many dosage calculation questions at the chosen difficulty, spread through
  the quiz, even when the material has none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-15 22:04:08 -07:00
parent 25bc03ea75
commit e945f589ce
16 changed files with 807 additions and 44 deletions

View file

@ -151,10 +151,16 @@ connections into OpenAI-compatible ones at `/v1`).
Tests: `tests/revision4.test.ts`; OpenRouter cases in `server/ai/providers/providers.test.ts`.
Follow-up the same day: visitors of class and exam links can generate quizzes (default and
maximum 50 questions, 25% select-all, optional 5 med math questions) and flashcards from chosen
class materials, 4 quizzes and 2 flashcard sets per link per day with a reset in the share
dialog (`server/routes/shares.ts`, `web/src/features/SharedGenerate.tsx`, migration 7). The
owner's Generate dialog gained "Include med math" with a count (default 5).
## Verification status
Baseline: 162 tests passed, TypeScript passed and production web/server builds passed.
Added six regression tests. Full `npm run check` passes: **257 tests** (fourth revision, 2026-11-15; 247 after the third, 208 after the second, 181 on 2026-11-12), TypeScript
Added six regression tests. Full `npm run check` passes: **261 tests** (share-link generation, 2026-11-15; 257 after the fourth revision; 247 after the third, 208 after the second, 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,

View file

@ -60,6 +60,8 @@ is an opt-in development tool, never part of startup.
content changes, accepts tagged or dragged-in materials, can switch models per chat and can
show its reasoning. It runs Python in a sandbox, and generated quizzes are checked with it.
- Built-in web search (Parallel; no account needed, optional sign-in for higher limits).
- Share links for classes and exams can let visitors generate their own quizzes and flashcards
(daily limits per link), and quizzes can include med math on request.
- Interactive charts, practice questions and other blocks inline in assistant replies, and
clarifying questions before the assistant generates.
- MCP servers (Streamable HTTP, with OAuth sign-in) as extra assistant tools, each with your own instructions and

View file

@ -190,6 +190,16 @@ calculations, safety points, a self-check after teaching), so blocks appear with
for. Explain uses a reduced library without Question and Flashcards and is told to give
information only (`genUiPrompt('explain')`). Library telemetry is off (`OPENUI_TELEMETRY_DISABLED=1`, `DO_NOT_TRACK=1`).
## Medication math on request
Generate → "Include med math" with a count (default 5) asks for exactly that many dosage
calculation questions (`mathQuestions`), written at the chosen difficulty even when the
material has no calculations: easy is single-step doses and conversions, medium weight-based
doses and pump rates, hard drip rates, titrations and heparin protocols. They are written in
their own batches without the excerpts, then spread evenly through the quiz; the other batches
write no calculations. Without the option, calculations still appear only when the material
involves them. The Python check is told the exact number requested.
## AI on share links
Each share link has two switches, both off by default:
@ -210,6 +220,20 @@ allowance: each browser gets a random visitor id in a signed, HTTP-only cookie
forged cookie is replaced with a new id. Because clearing cookies gives a fresh allowance, each
link also has a cap of 500 requests per hour across all visitors.
### Generating on share links
Class and exam links have a third visitor switch, **Generate quizzes and flashcards** (off by
default). Visitors choose only the kind, how many (quizzes default to 50, at most 50: one exam;
flashcards default to 30), the difficulty, which of the link's class materials to use and,
for quizzes, "Include med math" (5 calculation questions). Quizzes are NCLEX style with 25%
select-all and the Python check. A link may make 4 quizzes and 2 flashcard sets a day (local
midnight, failed attempts not counted), on top of the per-device request limit; **Reset
limits** in the share dialog starts a fresh allowance. Sets are drafts tied to the link
(`drafts.share_id`, migration 7): every visitor of the link sees them under "Make your own
practice", they can be explained like shared questions, and they stay off the owner's drafts
list. The share dialog lists them; opening one lets the owner review it, save it to the
library or discard it from the link. Deleting the link deletes them.
## Original documents
A document's page offers Extracted text, Side by side and Original. PDFs and images show the

View file

@ -62,12 +62,14 @@ interface DraftRow {
saved_kind: string | null;
saved_id: string | null;
conversation_id: string | null;
/** Made by a visitor of this share link (not listed with the owner's drafts). */
share_id: string | null;
created_at: number;
updated_at: number;
}
export interface DraftMeta {
origin?: 'generate' | 'assistant' | 'convert' | 'improve' | 'import';
origin?: 'generate' | 'assistant' | 'convert' | 'improve' | 'import' | 'share';
model?: string;
request?: Record<string, unknown>;
sources?: { documentId: string; title: string }[];
@ -102,6 +104,7 @@ export function mapDraft(row: DraftRow) {
savedKind: row.saved_kind,
savedId: row.saved_id,
conversationId: row.conversation_id,
shareId: row.share_id ?? null,
itemCount: payload.kind === 'quiz' ? payload.questions.length : payload.kind === 'deck' ? payload.cards.length : payload.content.length,
createdAt: row.created_at,
updatedAt: row.updated_at,
@ -117,12 +120,22 @@ export function getDraft(db: DB, id: string): DraftDto {
export function createDraft(
db: DB,
input: { kind: DraftRow['kind']; classId?: string | null; folderId?: string | null; title: string; payload?: ContentDraft; meta?: DraftMeta; status?: DraftRow['status']; conversationId?: string | null },
input: {
kind: DraftRow['kind'];
classId?: string | null;
folderId?: string | null;
title: string;
payload?: ContentDraft;
meta?: DraftMeta;
status?: DraftRow['status'];
conversationId?: string | null;
shareId?: string | null;
},
): string {
const id = newId();
const at = now();
db.run(
`INSERT INTO drafts (id, kind, class_id, folder_id, title, payload, meta, status, conversation_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
`INSERT INTO drafts (id, kind, class_id, folder_id, title, payload, meta, status, conversation_id, share_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
id,
input.kind,
input.classId ?? null,
@ -132,6 +145,7 @@ export function createDraft(
JSON.stringify(input.meta ?? {}),
input.status ?? 'ready',
input.conversationId ?? null,
input.shareId ?? null,
at,
at,
);
@ -140,7 +154,7 @@ export function createDraft(
export function listDrafts(db: DB) {
return db
.all<DraftRow>(`SELECT * FROM drafts WHERE status IN ('generating', 'ready', 'failed') ORDER BY updated_at DESC LIMIT 50`)
.all<DraftRow>(`SELECT * FROM drafts WHERE status IN ('generating', 'ready', 'failed') AND share_id IS NULL ORDER BY updated_at DESC LIMIT 50`)
.map(mapDraft)
.map(({ payload: _payload, ...rest }) => rest);
}
@ -252,7 +266,9 @@ export function discardDraft(db: DB, id: string): void {
export function tidyDrafts(db: DB): void {
db.run(`UPDATE drafts SET status = 'failed', error = 'Generation was interrupted by a restart. Start it again.' WHERE status = 'generating'`);
db.run(`DELETE FROM drafts WHERE status IN ('saved', 'discarded') AND updated_at < ?`, now() - 14 * 86_400_000);
db.run(`DELETE FROM drafts WHERE status IN ('ready', 'failed') AND updated_at < ?`, now() - 60 * 86_400_000);
db.run(`DELETE FROM drafts WHERE status IN ('ready', 'failed') AND share_id IS NULL AND updated_at < ?`, now() - 60 * 86_400_000);
// Sets made on a share link stay with the link; failed attempts go after a week.
db.run(`DELETE FROM drafts WHERE status = 'failed' AND share_id IS NOT NULL AND updated_at < ?`, now() - 7 * 86_400_000);
}
/* ------------------------------ request ------------------------------ */
@ -286,6 +302,12 @@ export const generateRequestSchema = z.object({
difficulty: z.enum(['easy', 'medium', 'hard', 'mixed']).optional(),
/** Share of select-all-that-apply questions, 0..1. */
sataShare: z.number().min(0).max(1).optional(),
/**
* Quizzes: exactly this many medication math (dosage calculation) questions, written at the
* chosen difficulty even when the material has no calculations. Absent: calculations only
* when the material involves them.
*/
mathQuestions: z.number().int().min(0).max(25).optional(),
cardStyle: z.enum(['concise', 'detailed', 'medication', 'definition']).optional(),
noteStyle: z.enum(['study_guide', 'summary', 'outline', 'cheat_sheet']).optional(),
modelId: z.string().nullish(),
@ -358,7 +380,15 @@ const CARD_SCHEMA = {
required: ['cards'],
};
const NUMERIC_RULE = `- Only write calculation items when the source material itself involves calculations, and never more than the number allowed in the request. 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.`;
/** What the medication math questions look like at each difficulty, when the user asks for them. */
const MATH_LEVELS: Record<string, string> = {
easy: 'single-step problems: tablets or capsules per dose, mL of an oral liquid, and simple unit conversions (g, mg, mcg; L, mL; lb to kg)',
medium: 'weight-based doses (mg/kg), mL to draw up from a vial, and IV pump rates in mL/h',
hard: 'multi-step problems: gravity drip rates in gtt/min with a drop factor, mcg/kg/min titrations, heparin units/h from a protocol, and reconstituted medications',
mixed: 'a spread from single-step dose and conversion problems to weight-based doses, IV pump rates (mL/h) and drip rates (gtt/min)',
};
const NUMERIC_RULE = `- Only write calculation items when the source material itself involves calculations (or the request asks for medication math), and never more than the number allowed in the request. 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.
@ -662,6 +692,25 @@ async function broadenTopics(app: AppContext, modelId: string | null | undefined
return out;
}
/** Places the matching items at even intervals among the others, keeping each group's order. */
export function spreadEvenly<T>(list: T[], pick: (item: T) => boolean): T[] {
const chosen = list.filter(pick);
const rest = list.filter((x) => !pick(x));
if (chosen.length === 0 || rest.length === 0) return list;
const out: T[] = [];
const step = (rest.length + chosen.length) / chosen.length;
let next = Math.floor(step / 2);
let c = 0;
for (let i = 0, r = 0; i < list.length; i += 1) {
if (c < chosen.length && i >= next) {
out.push(chosen[c++]);
next = Math.floor(step / 2 + c * step);
} else if (r < rest.length) out.push(rest[r++]);
else out.push(chosen[c++]);
}
return out;
}
/** True when the material involves calculations, so calculation questions are fair game. */
export function mentionsCalculations(text: string): boolean {
return CALCULATION_RE.test(text);
@ -710,7 +759,7 @@ const NOTE_TITLES: Record<string, string> = { study_guide: 'study guide', summar
export function startGeneration(
app: AppContext,
input: z.input<typeof generateRequestSchema>,
opts: { conversationId?: string | null; origin?: DraftMeta['origin']; onProgress?: (label: string) => void } = {},
opts: { conversationId?: string | null; origin?: DraftMeta['origin']; onProgress?: (label: string) => void; shareId?: string } = {},
): { draftId: string; done: Promise<DraftDto> } {
const req = generateRequestSchema.parse(input);
if (req.classId) getClass(app.db, req.classId);
@ -727,6 +776,7 @@ export function startGeneration(
title: req.title ?? '',
status: 'generating',
conversationId: opts.conversationId ?? null,
shareId: opts.shareId ?? null,
meta: { origin: opts.origin ?? 'generate', model: resolved.model.label, request: { ...req, source: { ...req.source, text: req.source.text ? `${req.source.text.slice(0, 200)}…` : undefined } } },
});
const controller = new AbortController();
@ -795,22 +845,35 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
// Calculation questions: only when the material involves calculations, and at most 5 per
// 50-question exam (10% of a practice set).
const mathAllowed = isQuiz && mentionsCalculations([sourcesText(sources), req.focus ?? '', req.instructions ?? ''].join('\n'));
const numericCap = !mathAllowed ? 0 : req.format === 'exam' ? Math.min(5, Math.max(1, Math.round(count / 10))) : Math.max(1, Math.round(count / 10));
// Medication math on request: exactly that many, written in their own batches after the rest.
const forcedMath = isQuiz && req.mathQuestions !== undefined ? Math.min(req.mathQuestions, count) : null;
const regularCount = forcedMath === null ? count : count - forcedMath;
const numericCap =
forcedMath !== null ? forcedMath : !mathAllowed ? 0 : req.format === 'exam' ? Math.min(5, Math.max(1, Math.round(count / 10))) : Math.max(1, Math.round(count / 10));
const numericCount = () => (draft.kind === 'quiz' ? draft.questions.filter((q) => q.type === 'numeric').length : 0);
// Broad topic labels, shared with the class's existing quizzes so results group sensibly.
const knownTopics = isQuiz && req.classId ? db.all<{ topic: string }>(`SELECT DISTINCT q.topic FROM questions q JOIN quizzes z ON z.id = q.quiz_id WHERE z.class_id = ? AND q.topic != '' LIMIT 60`, req.classId).map((r) => r.topic) : [];
const runBatch = async (index: number, want: number): Promise<number> => {
const runBatch = async (index: number, want: number, math = false): Promise<number> => {
const written = items().map((x) => `- ${('stem' in x ? x.stem : x.front).replace(/\s+/g, ' ').slice(0, 140)}`);
const sataCount = Math.round(want * sata);
const numericLeft = Math.max(0, numericCap - numericCount());
// With requested math, the regular batches write none; the math batches write only those.
const numericLeft = forcedMath !== null && !math ? 0 : Math.max(0, numericCap - numericCount());
const topicsSoFar = [...new Set([...knownTopics, ...(draft.kind === 'quiz' ? draft.questions.map((q) => q.topic) : [])].filter(Boolean))];
const ask = isQuiz
const ask = math
? [
`Write ${want} medication math (dosage calculation) questions, all of type "numeric": the student types the number.`,
`Difficulty ${difficulty}: ${MATH_LEVELS[difficulty] ?? MATH_LEVELS.mixed}.`,
'These do not need to come from the course material: write standard nursing med math with realistic orders, labels and client weights, using generic drug names (drugs from the material are welcome where they fit).',
'Each stem gives the order and what is on hand, and states the unit and the rounding rule ("Round to the nearest tenth."). Exactly one option: the correct answer with its unit, marked correct, with "why" showing the working step by step (dimensional analysis or desired over have). The rationale explains the method and one safety check.',
'Set the topic to "Dosage calculation". Vary the problem types.',
]
: isQuiz
? [
`Write ${want} NCLEX-style questions${req.focus ? ` about: ${req.focus}` : ''}.`,
sata <= 0 ? 'Use only multiple-choice (single) questions, apart from calculations.' : sata >= 1 ? 'Use only select-all-that-apply (multi) questions, apart from calculations.' : `Exactly ${sataCount} of them must be select-all-that-apply (type "multi"); the rest multiple choice (single).`,
numericLeft === 0
? 'Do not write any calculation (numeric) questions in this batch.'
? `Do not write any calculation (numeric) questions in this batch.${forcedMath ? ' Calculation questions are written separately.' : ''}`
: `The material involves calculations: you may include up to ${numericLeft} calculation question${numericLeft === 1 ? '' : 's'} (type "numeric", the student types the number). Do not exceed that.`,
`Topic labels must be broad (the main condition, drug class or body system, 1-3 words), not subtypes or aspects.${topicsSoFar.length ? ` Reuse these existing topics wherever they fit: ${topicsSoFar.slice(0, 40).join(', ')}.` : ''}`,
difficulty === 'mixed' ? 'Mix difficulty: mostly medium, with a few easy and a few hard.' : `Difficulty: ${difficulty}.`,
@ -823,7 +886,8 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
req.source.quizId ? 'Turn the quiz questions into flashcards: the front asks for the key fact, the back states it with the essential reasoning.' : '',
];
if (written.length > 0) ask.push(`Already written (do not repeat or closely paraphrase these):\n${written.slice(-80).join('\n')}`);
const user = [sourceBlock(sources, index), ask.filter(Boolean).join('\n')].filter(Boolean).join('\n\n======\n\n');
// Med math is standard material: the excerpts would only cost tokens.
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',
@ -850,21 +914,25 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
const ref = (item.source ?? '').replace(/[[\]\s]/g, '');
item.source = sources.refs.get(ref) ?? undefined;
}
if ('stem' in item && item.type === 'numeric' && numericCount() >= numericCap) continue; // over the calculation allowance; a top-up replaces it
if ('stem' in item && item.type === 'numeric' && (numericLeft === 0 || numericCount() >= numericCap)) continue; // over the calculation allowance; a top-up replaces it
if (math && (!('stem' in item) || item.type !== 'numeric')) continue; // a math batch keeps only calculations
if ('stem' in item && item.type !== 'numeric') item.options = shuffleOptions(item.options);
seen.add(k);
(items() as (DraftQuestion | DraftCard)[]).push(item);
added += 1;
if (math ? numericCount() >= numericCap : items().length - numericCount() >= regularCount && forcedMath !== null) break;
if (items().length >= count) break;
}
if (result.truncated) notes.push('One batch hit the model output limit, so it produced fewer items than requested.');
return added;
};
// Regular items written so far (with requested math, calculations are counted separately).
const regularDone = () => (forcedMath === null ? items().length : items().length - numericCount());
let failures = 0;
for (let i = 0; i < batches && items().length < count; i += 1) {
for (let i = 0; i < Math.ceil(regularCount / perBatch) && regularDone() < regularCount; i += 1) {
if (signal.aborted) throw new AIError('aborted', 'Generation was cancelled.');
const want = Math.min(perBatch, count - items().length);
const want = Math.min(perBatch, regularCount - regularDone());
setProgress(db, draftId, 0.05 + (0.9 * items().length) / count, `Writing ${isQuiz ? 'questions' : 'cards'} ${items().length + 1}-${Math.min(count, items().length + want)} of ${count}`, draft);
try {
await runBatch(i, want);
@ -876,9 +944,9 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
}
}
// Top up when validation or de-duplication left the draft short.
for (let extra = 0; extra < 2 && items().length < count && items().length > 0; extra += 1) {
for (let extra = 0; extra < 2 && regularDone() < regularCount && items().length > 0; extra += 1) {
if (signal.aborted) break;
const want = Math.min(perBatch, count - items().length);
const want = Math.min(perBatch, regularCount - regularDone());
setProgress(db, draftId, 0.92, `Writing the last ${want} ${isQuiz ? 'questions' : 'cards'}`, draft);
try {
if ((await runBatch(batches + extra, want)) === 0) break;
@ -886,6 +954,22 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
break;
}
}
// Requested medication math, in batches of up to eight, then spread evenly through the quiz.
if (forcedMath && draft.kind === 'quiz') {
for (let attempt = 0; attempt < Math.ceil(forcedMath / perBatch) + 2 && numericCount() < forcedMath; attempt += 1) {
if (signal.aborted) throw new AIError('aborted', 'Generation was cancelled.');
const want = Math.min(perBatch, forcedMath - numericCount());
setProgress(db, draftId, 0.9, `Writing ${want} med math question${want === 1 ? '' : 's'}`, draft);
try {
if ((await runBatch(batches + 10 + attempt, want, true)) === 0 && attempt > 0) break;
} catch (err) {
if (err instanceof AIError && err.code === 'aborted') throw err;
notes.push(`A med math batch failed: ${err instanceof Error ? err.message : 'unknown error'}`);
}
}
draft.questions = spreadEvenly(draft.questions, (q) => q.type === 'numeric');
if (numericCount() < forcedMath) notes.push(`Wrote ${numericCount()} of the ${forcedMath} med math questions requested.`);
}
if (items().length === 0) throw new AIError('invalid_response', 'The model did not produce any usable items. Try again, or choose a different model for content generation.');
if (items().length < count) notes.push(`Generated ${items().length} of the ${count} requested.`);
@ -901,7 +985,7 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
// Check counts, the type mix, answer keys and calculations with Python before review.
if (draft.kind === 'quiz' && req.verify !== false) {
setProgress(db, draftId, 0.96, 'Checking the questions with Python', draft);
const checked = await verifyQuizDraft(app, draft, { count, style, sataShare: sata, difficulty, numericCap }, { modelId: req.modelId, signal, onStep: (label) => setProgress(db, draftId, 0.97, label) });
const checked = await verifyQuizDraft(app, draft, { count, style, sataShare: sata, difficulty, numericCap, numericRequested: forcedMath !== null }, { modelId: req.modelId, signal, onStep: (label) => setProgress(db, draftId, 0.97, label) });
draft.questions = checked.draft.questions;
notes.push(...checked.notes);
}

View file

@ -76,6 +76,8 @@ export interface QuizRequestFacts {
difficulty?: string;
/** Most calculation (numeric) questions allowed; 0 when the material has no calculations. */
numericCap?: number;
/** The user asked for exactly `numericCap` medication math questions (not from the material). */
numericRequested?: boolean;
}
/** Long text is shortened rather than rejected: a verbose but correct review must not be lost. */
@ -142,7 +144,7 @@ const SUBMIT_REVIEW: ToolDef = {
function rulesFor(req: QuizRequestFacts): string {
const expectedMulti = req.sataShare <= 0 ? 'none' : req.sataShare >= 1 ? 'all non-numeric questions' : `about ${Math.round(req.sataShare * 100)}% of the non-numeric questions`;
return [
`Requested: ${req.count} NCLEX-style questions, difficulty "${req.difficulty ?? 'mixed'}", select-all (multi) share: ${expectedMulti}, calculation (numeric) questions: ${req.numericCap ? `at most ${req.numericCap}` : 'none (the material has no calculations)'}.`,
`Requested: ${req.count} NCLEX-style questions, difficulty "${req.difficulty ?? 'mixed'}", select-all (multi) share: ${expectedMulti}, calculation (numeric) questions: ${req.numericRequested ? (req.numericCap ? `exactly ${req.numericCap} medication math questions, requested by the user (they need not come from the material)` : 'none') : req.numericCap ? `at most ${req.numericCap}` : 'none (the material has no calculations)'}.`,
'Rules each question must meet:',
'- single: exactly one correct option and 4 options.',
'- multi (select all that apply): at least 2 correct options; for NCLEX style 5 or 6 options with 2 to 5 correct and a stem ending with "Select all that apply."',

View file

@ -440,6 +440,16 @@ UPDATE ai_providers SET
WHERE kind = 'ollama';
`;
/**
* Quizzes and flashcards that share-link visitors generate are drafts tied to the link (kept
* until the owner discards them or deletes the link); the daily allowance can be reset.
*/
const shareGeneration = `
ALTER TABLE drafts ADD COLUMN share_id TEXT REFERENCES shares(id) ON DELETE CASCADE;
CREATE INDEX drafts_share ON drafts(share_id, created_at);
ALTER TABLE shares ADD COLUMN generate_reset_at INTEGER;
`;
export const MIGRATIONS: Migration[] = [
{ id: 1, name: 'init', sql: init },
{ id: 2, name: 'embeddings', sql: embeddings },
@ -447,4 +457,5 @@ export const MIGRATIONS: Migration[] = [
{ id: 4, name: 'revision2', sql: revision2 },
{ id: 5, name: 'revision3', sql: revision3 },
{ id: 6, name: 'revision4', sql: revision4 },
{ id: 7, name: 'share-generation', sql: shareGeneration },
];

View file

@ -10,12 +10,14 @@ import { citedPassages, citedPassagesPrompt, EXPLAIN_SYSTEM, explainPrompt } fro
import { genUiPrompt } from '../ai/genui.ts';
import { officePreviewHtml, PREVIEW_CSP } from '../documents/preview.ts';
import { aiStatus, runChatWithMcp } from '../ai/service.ts';
import { startGeneration } from '../ai/generate.ts';
import { getAppSettings } from '../settings.ts';
import { AIError, type ChatMessage } from '../ai/types.ts';
import { HttpError, idParam, notFound, readBody, type AppContext } from '../http.ts';
import { listCards, listQuestions } from '../services/content.ts';
import { listChunks, searchChunks, sourceLabel, type ChunkHit } from '../services/documents.ts';
import { hybridSearch } from '../services/semantic.ts';
import { newId, newToken, now, parseJson } from '../util.ts';
import { newId, newToken, now, parseJson, startOfDay } from '../util.ts';
import type { ToolActivity } from '../ai/activity.ts';
import { sourceActivity, sse, type ExplainEvent } from './ai.ts';
@ -33,6 +35,7 @@ interface ShareRow {
created_at: number;
updated_at: number;
ai: string;
generate_reset_at: number | null;
}
/** AI features a share's visitors may use. Both are off until the owner turns them on. */
@ -41,9 +44,55 @@ export interface ShareAi {
explain: boolean;
/** A chat assistant that answers from the shared material only. */
assistant: boolean;
/** Visitors of a shared class or exam can generate quizzes and flashcards from its materials. */
generate: boolean;
}
const shareAi = (row: Pick<ShareRow, 'ai'>): ShareAi => ({ explain: false, assistant: false, ...parseJson<Partial<ShareAi>>(row.ai, {}) });
const shareAi = (row: Pick<ShareRow, 'ai'>): ShareAi => ({ explain: false, assistant: false, generate: false, ...parseJson<Partial<ShareAi>>(row.ai, {}) });
/** Sets a share link may generate per day (the app's time zone), until the owner resets it. */
export const SHARE_GENERATE_LIMITS = { quiz: 4, deck: 2 } as const;
/** A shared quiz is at most one exam long. */
export const SHARE_QUIZ_MAX = 50;
const SHARE_DECK_MAX = 50;
/** Medication math questions in a visitor's quiz when they ask for them. */
const SHARE_MATH_QUESTIONS = 5;
/** Today's generations on a link (since local midnight or the owner's last reset). */
function generateUsage(app: AppContext, share: Pick<ShareRow, 'id' | 'generate_reset_at'>) {
const s = getAppSettings(app.db, app.config.timezone);
const at = now();
const since = Math.max(startOfDay(at, s.timezone), share.generate_reset_at ?? 0);
const used = (kind: 'quiz' | 'deck') =>
app.db.get<{ n: number }>(`SELECT COUNT(*) AS n FROM drafts WHERE share_id = ? AND kind = ? AND status != 'failed' AND created_at >= ?`, share.id, kind, since)?.n ?? 0;
return {
quiz: { used: used('quiz'), limit: SHARE_GENERATE_LIMITS.quiz },
deck: { used: used('deck'), limit: SHARE_GENERATE_LIMITS.deck },
};
}
/** The sets made on a link that visitors can open (or are still being written). */
function generatedSets(app: AppContext, shareId: string) {
return app.db
.all<{ id: string; kind: 'quiz' | 'deck'; title: string; status: string; progress: number; progress_label: string; error: string; payload: string; created_at: number }>(
`SELECT id, kind, title, status, progress, progress_label, error, payload, created_at FROM drafts WHERE share_id = ? AND status IN ('generating', 'ready', 'failed') ORDER BY created_at DESC LIMIT 60`,
shareId,
)
.map((d) => {
const payload = parseJson<{ questions?: unknown[]; cards?: unknown[] }>(d.payload, {});
return {
id: d.id,
kind: d.kind,
title: d.title || (d.kind === 'quiz' ? 'Practice quiz' : 'Flashcards'),
status: d.status as 'generating' | 'ready' | 'failed',
progress: d.progress,
progressLabel: d.progress_label,
error: d.status === 'failed' ? 'This set could not be made. Try again later.' : '',
count: (d.kind === 'quiz' ? payload.questions?.length : payload.cards?.length) ?? 0,
createdAt: d.created_at,
};
});
}
const TABLE: Record<ShareKind, { table: string; title: string }> = {
quiz: { table: 'quizzes', title: 'title' },
@ -68,6 +117,11 @@ function mapShare(row: ShareRow) {
};
}
/** The owner's view of what visitors generated on a link. */
function ownerGenerated(app: AppContext, row: ShareRow) {
return { usage: generateUsage(app, row), sets: generatedSets(app, row.id).filter((s) => s.status !== 'failed') };
}
/* ------------------------------ admin ------------------------------ */
export function shareRoutes(app: AppContext): Hono {
@ -89,7 +143,7 @@ export function shareRoutes(app: AppContext): Hono {
}
const classId = row.kind === 'class' ? row.target_id : target.class_id;
const cls = classId ? db.get<{ name: string; color: string }>('SELECT name, color FROM classes WHERE id = ?', classId) : undefined;
out.push({ ...mapShare(row), title: target.title, classId: classId ?? null, className: cls?.name ?? '', classColor: cls?.color ?? 'slate' });
out.push({ ...mapShare(row), title: target.title, classId: classId ?? null, className: cls?.name ?? '', classColor: cls?.color ?? 'slate', generated: row.kind === 'class' || row.kind === 'folder' ? ownerGenerated(app, row) : null });
}
return c.json({ shares: out });
});
@ -111,7 +165,7 @@ export function shareRoutes(app: AppContext): Hono {
r.patch('/shares/:id', async (c) => {
const id = idParam(c);
const body = await readBody(c, z.object({ enabled: z.boolean().optional(), ai: z.object({ explain: z.boolean(), assistant: z.boolean() }).partial().optional() }));
const body = await readBody(c, z.object({ enabled: z.boolean().optional(), ai: z.object({ explain: z.boolean(), assistant: z.boolean(), generate: z.boolean() }).partial().optional() }));
const row = db.get<ShareRow>('SELECT * FROM shares WHERE id = ?', id);
if (!row) throw notFound('Share link');
const enabled = body.enabled ?? Boolean(row.enabled);
@ -120,6 +174,15 @@ export function shareRoutes(app: AppContext): Hono {
return c.json({ share: mapShare(db.get<ShareRow>('SELECT * FROM shares WHERE id = ?', id)!) });
});
/** Gives the link a fresh daily allowance of generated quizzes and flashcards. */
r.post('/shares/:id/generate/reset', (c) => {
const id = idParam(c);
const { changes } = db.run('UPDATE shares SET generate_reset_at = ?, updated_at = ? WHERE id = ?', now(), now(), id);
if (!changes) throw notFound('Share link');
const row = db.get<ShareRow>('SELECT * FROM shares WHERE id = ?', id)!;
return c.json({ share: mapShare(row), generated: ownerGenerated(app, row) });
});
/** Issues a new token: the old link stops working immediately. */
r.post('/shares/:id/rotate', (c) => {
const id = idParam(c);
@ -298,10 +361,12 @@ export function publicRoutes(app: AppContext): Hono {
/** The switches visitors see: on only when the owner allowed it and a model is configured. */
const publicAi = (share: ShareRow): ShareAi => {
const flags = shareAi(share);
if (!flags.explain && !flags.assistant) return flags;
const flags = { ...shareAi(share) };
// Generating needs class materials, which only class and exam links have.
if (share.kind !== 'class' && share.kind !== 'folder') flags.generate = false;
if (!flags.explain && !flags.assistant && !flags.generate) return flags;
const ready = aiStatus(app).ready;
return { explain: flags.explain && ready, assistant: flags.assistant && ready };
return { explain: flags.explain && ready, assistant: flags.assistant && ready, generate: flags.generate && ready };
};
const load = (token: string): ShareRow => {
@ -349,6 +414,13 @@ export function publicRoutes(app: AppContext): Hono {
if (share.kind !== 'class' && share.kind !== 'folder') throw gone();
const kind = c.req.param('kind');
const id = c.req.param('id');
if (kind === 'ai') {
// A quiz or flashcard set a visitor generated on this link.
const set = generatedSet(share, id);
c.header('Cache-Control', 'no-store');
c.header('X-Robots-Tag', 'noindex, nofollow');
return c.json({ ...set, ai: publicAi(share) });
}
const allowed = collectionItems(app, share).some((i) => i.kind === kind && i.id === id);
if (!allowed) throw gone();
c.header('Cache-Control', 'no-store');
@ -397,6 +469,90 @@ export function publicRoutes(app: AppContext): Hono {
limitVisitor(share.id, visitorId(app, c));
};
/** The class a class or exam link belongs to. */
const sharedClassId = (share: ShareRow): string | null =>
share.kind === 'class' ? share.target_id : share.kind === 'folder' ? (db.get<{ class_id: string }>('SELECT class_id FROM folders WHERE id = ?', share.target_id)?.class_id ?? null) : null;
/** A finished set generated on this link, shaped like a shared quiz or deck. */
function generatedSet(share: ShareRow, id: string) {
const row = db.get<{ id: string; kind: string; title: string; payload: string }>(`SELECT id, kind, title, payload FROM drafts WHERE id = ? AND share_id = ? AND status = 'ready'`, id, share.id);
if (!row) throw gone();
const payload = parseJson<Record<string, any>>(row.payload, {});
if (row.kind === 'quiz') {
type DraftQ = { type: 'single' | 'multi' | 'numeric'; stem: string; options: { text: string; correct: boolean; why?: string }[]; rationale?: string; topic?: string; difficulty?: string };
const questions = (Array.isArray(payload.questions) ? (payload.questions as DraftQ[]) : []).map((q, i) => ({
id: `q${i + 1}`,
type: q.type,
stem: q.stem,
options: q.options,
rationale: q.rationale ?? '',
topic: q.topic ?? '',
difficulty: q.difficulty ?? 'medium',
}));
return { kind: 'quiz' as const, id: row.id, title: row.title, description: 'Made with AI from the shared materials.', questions };
}
if (row.kind === 'deck') {
const cards = (Array.isArray(payload.cards) ? payload.cards : []).map((card: any, i: number) => ({ id: `c${i + 1}`, front: card.front, back: card.back, hint: card.hint ?? '' }));
return { kind: 'deck' as const, id: row.id, title: row.title, description: 'Made with AI from the shared materials.', cards };
}
throw gone();
}
/** What visitors can generate on this link, and what was made on it. */
r.get('/:token/generated', (c) => {
const share = load(c.req.param('token'));
if (!publicAi(share).generate) throw new HttpError(403, 'ai_disabled', 'The owner has not turned this on for this link.');
c.header('Cache-Control', 'no-store');
return c.json({ sets: generatedSets(app, share.id), usage: generateUsage(app, share), limits: { quizMax: SHARE_QUIZ_MAX, deckMax: SHARE_DECK_MAX, mathQuestions: SHARE_MATH_QUESTIONS } });
});
const generateSchema = z.object({
kind: z.enum(['quiz', 'deck']),
count: z.number().int().min(1).max(Math.max(SHARE_QUIZ_MAX, SHARE_DECK_MAX)).optional(),
difficulty: z.enum(['easy', 'medium', 'hard', 'mixed']).default('mixed'),
/** Class materials to use; empty or absent: all of them. */
documentIds: z.array(z.string().max(64)).max(50).optional(),
/** Quizzes: add medication math questions, whatever the material covers. */
math: z.boolean().optional(),
});
/**
* A visitor generates a quiz or flashcard set from the link's class materials. Simplified on
* purpose: count, difficulty, materials and med math; NCLEX style with 25% select-all, checked
* with Python. Limited per link per day (and by the per-device request limit).
*/
r.post('/:token/generate', async (c) => {
const share = load(c.req.param('token'));
const body = await readBody(c, generateSchema);
if (share.kind !== 'class' && share.kind !== 'folder') throw gone();
requireAi(c, share, 'generate');
const docs = collectionItems(app, share).filter((i) => i.kind === 'document');
if (docs.length === 0) throw new HttpError(400, 'no_materials', 'There are no class materials on this link to generate from.');
const chosen = body.documentIds?.length ? body.documentIds : docs.map((d) => d.id);
if (chosen.some((id) => !docs.some((d) => d.id === id))) throw gone();
const usage = generateUsage(app, share)[body.kind];
if (usage.used >= usage.limit) {
const what = body.kind === 'quiz' ? `${usage.limit} quizzes` : `${usage.limit} flashcard sets`;
throw new HttpError(429, 'generate_limit', `This link has made its ${what} for today. Try again tomorrow.`);
}
const isQuiz = body.kind === 'quiz';
const count = Math.min(body.count ?? (isQuiz ? SHARE_QUIZ_MAX : 30), isQuiz ? SHARE_QUIZ_MAX : SHARE_DECK_MAX);
const { draftId } = startGeneration(
app,
{
kind: body.kind,
classId: sharedClassId(share),
folderId: share.kind === 'folder' ? share.target_id : null,
source: { documentIds: chosen },
count,
difficulty: body.difficulty,
...(isQuiz ? { format: count >= 40 ? 'exam' : 'practice', sataShare: 0.25, verify: true, ...(body.math ? { mathQuestions: Math.min(SHARE_MATH_QUESTIONS, count) } : {}) } : { cardStyle: 'concise' }),
},
{ origin: 'share', shareId: share.id },
);
return c.json({ set: generatedSets(app, share.id).find((s) => s.id === draftId), usage: generateUsage(app, share) }, 202);
});
const explainSchema = z.discriminatedUnion('kind', [
z.object({ kind: z.literal('question'), quizId: z.string().max(64), questionId: z.string().max(64), selected: z.array(z.string().max(4000)).max(12).optional(), ask: z.string().max(500).optional() }),
z.object({ kind: z.literal('card'), deckId: z.string().max(64), cardId: z.string().max(64), ask: z.string().max(500).optional() }),
@ -413,18 +569,20 @@ export function publicRoutes(app: AppContext): Hono {
let prompt: string;
let sources: ReturnType<typeof citedPassages> = [];
if (body.kind === 'question') {
if (!inScope(share, 'quiz', body.quizId)) throw gone();
const q = listQuestions(db, body.quizId).find((x) => x.id === body.questionId);
const made = inScope(share, 'quiz', body.quizId) ? null : generatedSet(share, body.quizId);
if (made && made.kind !== 'quiz') throw gone();
const q = made && made.kind === 'quiz' ? made.questions.find((x) => x.id === body.questionId) : listQuestions(db, body.quizId).find((x) => x.id === body.questionId);
if (!q) throw gone();
const texts = new Set(q.options.map((o) => o.text));
prompt = explainPrompt({ kind: 'question', stem: q.stem, options: q.options.map((o) => ({ text: o.text, correct: o.correct })), selected: body.selected?.filter((s) => texts.has(s) || q.type === 'numeric').map((s) => s.slice(0, 200)), rationale: q.rationale, ask: body.ask });
// The pages the rationale cites, read only from the quiz's own class.
const classId = db.get<{ class_id: string }>('SELECT class_id FROM quizzes WHERE id = ?', body.quizId)?.class_id;
const classId = made ? sharedClassId(share) : db.get<{ class_id: string }>('SELECT class_id FROM quizzes WHERE id = ?', body.quizId)?.class_id;
if (classId && q.rationale) sources = citedPassages(db, q.rationale, { classIds: [classId], stem: q.stem });
prompt += citedPassagesPrompt(sources);
} else {
if (!inScope(share, 'deck', body.deckId)) throw gone();
const card = listCards(db, body.deckId).find((x) => x.id === body.cardId);
const made = inScope(share, 'deck', body.deckId) ? null : generatedSet(share, body.deckId);
if (made && made.kind !== 'deck') throw gone();
const card = made && made.kind === 'deck' ? made.cards.find((x) => x.id === body.cardId) : listCards(db, body.deckId).find((x) => x.id === body.cardId);
if (!card) throw gone();
prompt = explainPrompt({ kind: 'card', front: card.front, back: card.back, ask: body.ask });
}

View file

@ -75,6 +75,19 @@ function toolArgs(name: string, userText: string, all: string): Record<string, u
void all;
switch (name) {
case 'submit_questions':
// A medication math batch: calculations only.
if (/medication math/i.test(userText)) {
return {
questions: Array.from({ length: count }, (_, k) => ({
type: 'numeric',
stem: `Mock math ${k + 1 + offset}: give ${k + 2} mg of a drug supplied as 1 mg per mL. How many mL? Round to the nearest tenth.`,
options: [{ text: `${k + 2} mL`, correct: true, why: `${k + 2} mg / 1 mg/mL = ${k + 2} mL` }],
rationale: 'Desired over have.',
topic: 'Dosage calculation',
difficulty: 'medium',
})),
};
}
return { questions: questions(count, refs, offset) };
case 'submit_flashcards':
return { cards: Array.from({ length: count }, (_, i) => ({ front: `Mock term ${i + 1 + offset}`, back: `Mock definition ${i + 1 + offset}`, hint: '', tags: ['mock'] })) };

View file

@ -325,8 +325,8 @@ describe('AI on share links', () => {
it('is off until the owner turns it on, and then only explains what was shared', async () => {
resetShareAiLimits();
const { quiz, question, other, otherQ, share } = await sharedQuiz();
expect(share.ai).toEqual({ explain: false, assistant: false });
expect((await t.api('GET', `/api/public/${share.token}`, undefined, { cookie: null })).body.ai).toEqual({ explain: false, assistant: false });
expect(share.ai).toEqual({ explain: false, assistant: false, generate: false });
expect((await t.api('GET', `/api/public/${share.token}`, undefined, { cookie: null })).body.ai).toEqual({ explain: false, assistant: false, generate: false });
const ask = { kind: 'question', quizId: quiz.id, questionId: question.id, selected: ['Hypertension'] };
expect((await visitor('POST', `/api/public/${share.token}/explain`, ask)).status).toBe(403);

View file

@ -139,3 +139,124 @@ describe('OpenRouter replaces the Ollama type', () => {
expect(rows.map((r) => r.base_url)).toEqual(['http://localhost:11434/v1', 'http://box:11434/v1', 'http://box:11434/v1', 'http://box:11434/v1']);
});
});
/* ------------------------------ med math and share-link generation ------------------------------ */
async function waitDraft(id: string): Promise<any> {
for (let i = 0; i < 400; i += 1) {
const draft = (await t.api('GET', `/api/drafts/${id}`)).body.draft;
if (draft.status !== 'generating') return draft;
await new Promise((r) => setTimeout(r, 25));
}
throw new Error('generation did not finish');
}
async function classWithMaterial() {
const cls = (await t.api('POST', '/api/classes', { name: 'Pharm' })).body.class;
const doc = (await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Heart failure', text: '# Heart failure\n\nFurosemide is a loop diuretic. Monitor potassium and daily weights.' })).body.document;
await queueIdle(t.ctx);
return { cls, doc };
}
describe('med math on request', () => {
it('writes exactly that many calculations, spread through the quiz, even without calculations in the material', async () => {
await connectAi();
const { cls } = await classWithMaterial();
const started = await t.api('POST', '/api/ai/generate', { kind: 'quiz', classId: cls.id, source: { allClassDocuments: true }, count: 8, mathQuestions: 2, verify: false });
expect(started.status).toBe(202);
const draft = await waitDraft(started.body.draftId);
expect(draft.status).toBe('ready');
const types = draft.payload.questions.map((q: any) => q.type);
expect(types).toHaveLength(8);
expect(types.filter((x: string) => x === 'numeric')).toHaveLength(2);
// Not bunched at the end.
expect(types.slice(-2)).not.toEqual(['numeric', 'numeric']);
const sent = JSON.stringify(chatCalls().map((r) => r.body.messages));
expect(sent).toContain('medication math');
});
it('spreads items evenly', async () => {
const { spreadEvenly } = await import('../server/ai/generate.ts');
const list = [...Array.from({ length: 8 }, (_, i) => `r${i}`), 'm0', 'm1'];
expect(spreadEvenly(list, (x) => x.startsWith('m'))).toEqual(['r0', 'r1', 'm0', 'r2', 'r3', 'r4', 'r5', 'm1', 'r6', 'r7']);
});
});
describe('share-link visitors can generate', () => {
beforeEach(async () => {
const { resetShareAiLimits } = await import('../server/routes/shares.ts');
resetShareAiLimits();
});
async function waitSets(token: string): Promise<any[]> {
for (let i = 0; i < 400; i += 1) {
const res = await t.api('GET', `/api/public/${token}/generated`);
if (!res.body.sets.some((s: any) => s.status === 'generating')) return res.body.sets;
await new Promise((r) => setTimeout(r, 25));
}
throw new Error('generation did not finish');
}
it('is off until the owner allows it, then makes quizzes and flashcards within daily limits', async () => {
await connectAi();
const { cls, doc } = await classWithMaterial();
const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share;
const token = share.token;
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 4 })).status).toBe(403);
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { generate: true, explain: true } });
expect((await t.api('GET', `/api/public/${token}`)).body.ai.generate).toBe(true);
// At most 50 questions, materials from the link only.
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 51 })).status).toBe(422);
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', documentIds: ['not-shared'] })).status).toBe(404);
mock.requests.length = 0;
const first = await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 8, difficulty: 'hard', documentIds: [doc.id], math: true });
expect(first.status).toBe(202);
expect(first.body.usage.quiz).toEqual({ used: 1, limit: 4 });
const [set] = await waitSets(token);
expect(set).toMatchObject({ kind: 'quiz', status: 'ready', count: 8 });
const quiz = (await t.api('GET', `/api/public/${token}/items/ai/${set.id}`)).body;
expect(quiz.kind).toBe('quiz');
expect(quiz.questions.filter((q: any) => q.type === 'numeric')).toHaveLength(5);
expect(quiz.questions[0].id).toBe('q1');
const sent = JSON.stringify(chatCalls().map((r) => r.body.messages));
expect(sent).toContain('Difficulty: hard');
expect(sent).toMatch(/Exactly 1 of them must be select-all-that-apply/);
// Explain works on a generated question.
const explained = await t.raw('POST', `/api/public/${token}/explain`, { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ kind: 'question', quizId: set.id, questionId: 'q1' }) });
expect(explained.status).toBe(200);
// The owner's drafts list does not show visitors' sets; the share settings do.
expect((await t.api('GET', '/api/drafts')).body.drafts.some((d: any) => d.id === set.id)).toBe(false);
const listed = (await t.api('GET', '/api/shares')).body.shares.find((s: any) => s.id === share.id);
expect(listed.generated.sets.map((s: any) => s.id)).toEqual([set.id]);
// Four quizzes and two flashcard sets a day.
for (let i = 0; i < 3; i += 1) expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 })).status).toBe(202);
const over = await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 });
expect(over.status).toBe(429);
expect(over.body.error.message).toContain('4 quizzes');
for (let i = 0; i < 2; i += 1) expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'deck', count: 3 })).status).toBe(202);
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'deck', count: 3 })).status).toBe(429);
await waitSets(token);
// The owner resets the allowance.
const reset = await t.api('POST', `/api/shares/${share.id}/generate/reset`);
expect(reset.body.generated.usage.quiz.used).toBe(0);
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 })).status).toBe(202);
await waitSets(token);
});
it('defaults to a 50-question quiz', async () => {
await connectAi();
const { cls } = await classWithMaterial();
const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share;
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { generate: true } });
const res = await t.api('POST', `/api/public/${share.token}/generate`, { kind: 'quiz' });
expect(res.status).toBe(202);
const draft = await waitDraft(res.body.set.id);
expect(draft.meta.request).toMatchObject({ count: 50, sataShare: 0.25, format: 'exam' });
});
});

View file

@ -45,6 +45,8 @@ export function GenerateDialog({ open, onClose, classId, folderId, preset }: { o
const [modelId, setModelId] = useState('');
const [templateId, setTemplateId] = useState('');
const [verify, setVerify] = useLocalStorage('sd.generate.verify', true);
const [mathOn, setMathOn] = useLocalStorage('sd.generate.math', false);
const [mathCount, setMathCount] = useState(5);
const [busy, setBusy] = useState(false);
/** The exam (folder) this draft is for: its documents are the only ones offered, and the draft is saved there. */
const [examId, setExamId] = useState<string | null>(folderId ?? null);
@ -118,7 +120,7 @@ export function GenerateDialog({ open, onClose, classId, folderId, preset }: { o
else if (mode === 'text') source.text = text;
else if (mode === 'weak') source.weakAreas = true;
const body: Record<string, unknown> = { kind, classId, folderId: examId, source, focus: focus.trim() || undefined, instructions: instructions.trim() || undefined, modelId: modelId || undefined };
if (kind === 'quiz') Object.assign(body, { count, format, style: 'nclex', difficulty, sataShare: sata / 100, verify });
if (kind === 'quiz') Object.assign(body, { count, format, style: 'nclex', difficulty, sataShare: sata / 100, verify, ...(mathOn ? { mathQuestions: Math.min(mathCount, count) } : {}) });
else if (kind === 'deck') Object.assign(body, { count, cardStyle });
else Object.assign(body, { noteStyle });
if (fixedSource && kind !== 'note') delete body.count;
@ -285,6 +287,16 @@ export function GenerateDialog({ open, onClose, classId, folderId, preset }: { o
<Field label={`Select-all-that-apply: ${sata}%`}>
<input type="range" min={0} max={100} step={10} value={sata} onChange={(e) => setSata(Number(e.target.value))} className="mt-2.5 w-full accent-[var(--accent)]" />
</Field>
<div className="flex items-end gap-4 sm:col-span-full">
<div className="min-w-0 flex-1">
<Switch checked={mathOn} onChange={setMathOn} label="Include med math" description="Adds dosage calculation questions at the chosen difficulty, even if the material has none." />
</div>
{mathOn && (
<Field label="How many" className="w-24 shrink-0">
<Input type="number" min={1} max={Math.min(25, count)} value={mathCount} onChange={(e) => setMathCount(Math.max(1, Math.min(25, Number(e.target.value) || 1)))} aria-label="Med math questions" />
</Field>
)}
</div>
<div className="sm:col-span-full">
<Switch checked={verify} onChange={setVerify} label="Double-check with Python" description="After writing, the model counts questions, select-all items and correct answers and recomputes calculations in a sandbox, then fixes what is wrong." />
</div>

View file

@ -0,0 +1,239 @@
/**
* Generating on a share link: visitors of a shared class or exam make their own NCLEX-style
* quiz or flashcard set from its class materials, with only the choices that matter (how many,
* how hard, which materials, med math). The owner's link allows a few sets a day.
*/
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { BookOpen, ChevronRight, CircleAlert, FileText, Layers, Sparkles } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Link } from 'react-router';
import { api, errorMessage } from '@/lib/api';
import type { PublicCollection, SharedGenerated } from '@/lib/types';
import { cx, plural } from '@/lib/util';
import { Button, Callout, Dialog, Field, Input, ProgressBar, Segmented, Select, Switch } from '@/ui';
type Kind = 'quiz' | 'deck';
export function SharedGenerate({ token, data }: { token: string; data: PublicCollection }) {
const qc = useQueryClient();
const key = ['public', token, 'generated'];
const q = useQuery({
queryKey: key,
queryFn: ({ signal }) => api.pub<SharedGenerated>(`/api/public/${token}/generated`, signal),
// While a set is being written, check on it every few seconds.
refetchInterval: (query) => (query.state.data?.sets.some((s) => s.status === 'generating') ? 3000 : false),
});
const [open, setOpen] = useState(false);
const info = q.data;
if (!info) return null;
const left = (k: Kind) => Math.max(0, info.usage[k].limit - info.usage[k].used);
const quizzes = info.sets.filter((s) => s.kind === 'quiz');
const decks = info.sets.filter((s) => s.kind === 'deck');
return (
<section className="mt-7" aria-label="Practice made with AI">
<div className="mb-2.5 flex flex-wrap items-end justify-between gap-3">
<div>
<h2 className="text-[17px] font-semibold">Make your own practice</h2>
<p className="text-[13px] text-ink-3">
NCLEX-style quizzes and flashcards from these class materials. Left today: {plural(left('quiz'), 'quiz', 'quizzes')}, {plural(left('deck'), 'flashcard set')}.
</p>
</div>
<Button variant="primary" onClick={() => setOpen(true)} disabled={left('quiz') + left('deck') === 0}>
Generate
</Button>
</div>
{info.sets.length > 0 && (
<div className="card overflow-hidden">
{[
['quiz', 'Quizzes', quizzes] as const,
['deck', 'Flashcards', decks] as const,
]
.filter(([, , list]) => list.length > 0)
.map(([kind, heading, list], n) => (
<section key={kind} aria-label={`AI-made ${heading.toLowerCase()}`}>
<h3 className={cx('eyebrow flex items-center gap-2 border-b border-line bg-sunken/60 px-4 py-2 [&>svg]:size-3.5', n > 0 && 'border-t')}>
{kind === 'quiz' ? <BookOpen /> : <Layers />}
{heading} made with AI
<span className="num font-normal">{list.length}</span>
</h3>
<ul className="divide-y divide-line">
{list.map((s) => {
const icon = (
<span className={cx('grid size-10 shrink-0 place-items-center rounded-[11px] [&>svg]:size-[18px]', s.status === 'failed' ? 'bg-bad-soft text-bad' : kind === 'quiz' ? 'bg-accent-soft text-accent' : 'bg-marker/45 text-ink dark:bg-marker/20 dark:text-marker')}>
{s.status === 'failed' ? <CircleAlert /> : s.status === 'generating' ? <Sparkles className="animate-pulse" /> : kind === 'quiz' ? <BookOpen /> : <Layers />}
</span>
);
if (s.status !== 'ready') {
return (
<li key={s.id} className="flex items-center gap-3 px-4 py-3.5">
{icon}
<span className="min-w-0 flex-1">
<span className="block truncate text-[15px] font-medium">{s.title}</span>
<span className={cx('text-xs', s.status === 'failed' ? 'text-bad' : 'text-ink-3')}>{s.status === 'failed' ? s.error : s.progressLabel || 'Starting…'}</span>
{s.status === 'generating' && <ProgressBar value={Math.max(0.04, s.progress)} className="mt-1.5 h-1" label={`Making ${s.title}`} />}
</span>
</li>
);
}
return (
<li key={s.id}>
<Link to={`/s/${token}/ai/${s.id}`} className="flex items-center gap-3 px-4 py-3.5 hover:bg-ink/4">
{icon}
<span className="min-w-0 flex-1">
<span className="block truncate text-[15px] font-medium">{s.title}</span>
<span className="text-xs text-ink-3">{plural(s.count, kind === 'quiz' ? 'question' : 'card')}</span>
</span>
<ChevronRight className="size-4 text-ink-3" />
</Link>
</li>
);
})}
</ul>
</section>
))}
</div>
)}
<GenerateDialog
open={open}
onClose={() => setOpen(false)}
token={token}
info={info}
documents={data.items.filter((i) => i.kind === 'document')}
onStarted={(next) => qc.setQueryData(key, next)}
/>
</section>
);
}
function GenerateDialog({
open,
onClose,
token,
info,
documents,
onStarted,
}: {
open: boolean;
onClose: () => void;
token: string;
info: SharedGenerated;
documents: PublicCollection['items'];
onStarted: (next: SharedGenerated) => void;
}) {
const left = (k: Kind) => Math.max(0, info.usage[k].limit - info.usage[k].used);
const [kind, setKind] = useState<Kind>('quiz');
const [count, setCount] = useState(info.limits.quizMax);
const [difficulty, setDifficulty] = useState<'mixed' | 'easy' | 'medium' | 'hard'>('mixed');
const [docIds, setDocIds] = useState<string[]>([]);
const [math, setMath] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const max = kind === 'quiz' ? info.limits.quizMax : info.limits.deckMax;
useEffect(() => {
if (!open) return;
const k: Kind = left('quiz') > 0 || left('deck') === 0 ? 'quiz' : 'deck';
setKind(k);
setCount(k === 'quiz' ? info.limits.quizMax : 30);
setDocIds(documents.map((d) => d.id));
setError('');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);
const submit = async () => {
setBusy(true);
setError('');
try {
const res = await api.pubPost<{ set: SharedGenerated['sets'][number]; usage: SharedGenerated['usage'] }>(`/api/public/${token}/generate`, {
kind,
count,
difficulty,
documentIds: docIds.length === documents.length ? [] : docIds,
math: kind === 'quiz' && math,
});
onStarted({ ...info, sets: [res.set, ...info.sets], usage: res.usage });
onClose();
} catch (e) {
setError(errorMessage(e));
} finally {
setBusy(false);
}
};
const none = left(kind) === 0;
return (
<Dialog
open={open}
onOpenChange={(o) => !o && onClose()}
title="Make your own practice"
description="Written by AI from this link's class materials. It takes a minute or two; you can keep studying meanwhile."
footer={
<>
<Button variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button variant="primary" loading={busy} disabled={none || docIds.length === 0} onClick={() => void submit()}>
Generate
</Button>
</>
}
>
<div className="space-y-4">
<Segmented
label="What to make"
value={kind}
onChange={(k) => {
setKind(k);
setCount(k === 'quiz' ? info.limits.quizMax : 30);
}}
options={[
{ value: 'quiz', label: `Quiz (${left('quiz')} left today)`, icon: <BookOpen /> },
{ value: 'deck', label: `Flashcards (${left('deck')} left)`, icon: <Layers /> },
]}
className="w-full"
/>
{none && <Callout tone="warn">This link has made all its {kind === 'quiz' ? 'quizzes' : 'flashcard sets'} for today. Try again tomorrow.</Callout>}
<div className="grid gap-4 sm:grid-cols-2">
<Field label={kind === 'quiz' ? 'Questions' : 'Cards'} hint={`Up to ${max}.`}>
<Input type="number" min={1} max={max} value={count} onChange={(e) => setCount(Math.max(1, Math.min(max, Number(e.target.value) || 1)))} />
</Field>
<Field label="Difficulty">
<Select value={difficulty} onChange={(e) => setDifficulty(e.target.value as typeof difficulty)}>
<option value="mixed">Mixed</option>
<option value="easy">Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</Select>
</Field>
</div>
<div>
<div className="mb-1.5 flex items-baseline justify-between text-[13px] font-medium">
<span>Class materials to use</span>
<button type="button" className="text-xs font-medium text-accent hover:underline" onClick={() => setDocIds(docIds.length === documents.length ? [] : documents.map((d) => d.id))}>
{docIds.length === documents.length ? 'Clear' : 'Select all'}
</button>
</div>
<div className="max-h-48 divide-y divide-line overflow-y-auto rounded-xl border border-line">
{documents.map((d) => (
<label key={d.id} className="flex cursor-pointer items-center gap-2.5 px-3 py-2.5 hover:bg-ink/3">
<input type="checkbox" className="size-4 accent-[var(--accent)]" checked={docIds.includes(d.id)} onChange={(e) => setDocIds(e.target.checked ? [...docIds, d.id] : docIds.filter((x) => x !== d.id))} />
<FileText className="size-4 shrink-0 text-ink-3" />
<span className="min-w-0 flex-1 truncate text-sm">{d.title}</span>
</label>
))}
</div>
</div>
{kind === 'quiz' && (
<Switch checked={math} onChange={setMath} label="Include med math" description={`Adds ${info.limits.mathQuestions} dosage calculation questions at this difficulty, even if the materials have none.`} />
)}
<p className="text-xs text-ink-3">{kind === 'quiz' ? 'NCLEX style with about 25% select-all-that-apply, double-checked with Python.' : 'One fact per card.'} AI can make mistakes: check anything important against the class materials.</p>
{error && (
<Callout tone="bad" className="py-2">
{error}
</Callout>
)}
</div>
</Dialog>
);
}

View file

@ -1,11 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { Check, Copy, ExternalLink, Globe, Link2Off, RefreshCw } from 'lucide-react';
import { BookOpen, Check, Copy, ExternalLink, Globe, Layers, Link2Off, RefreshCw, RotateCcw } from 'lucide-react';
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router';
import { Link, useNavigate } from 'react-router';
import { toast } from 'sonner';
import { api, errorMessage } from '@/lib/api';
import { qk, useClasses, useRefresh } from '@/lib/queries';
import type { ClassDetail, Share, Template } from '@/lib/types';
import type { ClassDetail, Share, ShareListItem, Template } from '@/lib/types';
import { copyText, plural, timeAgo } from '@/lib/util';
import { Button, Callout, Dialog, Field, Input, Select, Spinner, Switch, Textarea } from '@/ui';
@ -287,17 +287,24 @@ const SHARE_BLURB: Record<ShareKind, string> = {
export function ShareDialog({ target, onClose }: { target: { kind: ShareKind; id: string; title: string } | null; onClose: () => void }) {
const refresh = useRefresh();
const [share, setShare] = useState<Share | null>(null);
const [generated, setGenerated] = useState<ShareListItem['generated']>(null);
const [loading, setLoading] = useState(false);
const [copied, setCopied] = useState(false);
const load = (t: NonNullable<typeof target>) =>
api.get<{ shares: ShareListItem[] }>('/api/shares').then((r) => {
const found = r.shares.find((s) => s.kind === t.kind && s.targetId === t.id) ?? null;
setShare(found);
setGenerated(found?.generated ?? null);
});
useEffect(() => {
if (!target) return;
setShare(null);
setGenerated(null);
setCopied(false);
setLoading(true);
api
.get<{ shares: (Share & { kind: string; targetId: string })[] }>('/api/shares')
.then((r) => setShare(r.shares.find((s) => s.kind === target.kind && s.targetId === target.id) ?? null))
load(target)
.catch(() => undefined)
.finally(() => setLoading(false));
}, [target]);
@ -376,6 +383,58 @@ export function ShareDialog({ target, onClose }: { target: { kind: ShareKind; id
label="Ask about this material"
description="A chat that answers from what you shared. It can't see the rest of your library."
/>
{(target.kind === 'class' || target.kind === 'folder') && (
<>
<Switch
checked={Boolean(share.ai?.generate)}
onChange={(v) => act(async () => {
const res = await api.patch<{ share: Share }>(`/api/shares/${share.id}`, { ai: { generate: v } });
await load(target);
return res;
})}
label="Generate quizzes and flashcards"
description={`Visitors can make NCLEX-style quizzes (up to 50 questions, 25% select-all, optional med math) and flashcards from the class materials. Up to ${generated?.usage.quiz.limit ?? 4} quizzes and ${generated?.usage.deck.limit ?? 2} flashcard sets a day on this link.`}
/>
{share.ai?.generate && generated && (
<div className="space-y-2 rounded-lg bg-sunken/60 p-3 text-[13px]">
<div className="flex flex-wrap items-center justify-between gap-2">
<span className="text-ink-2">
Today: {generated.usage.quiz.used} of {generated.usage.quiz.limit} quizzes, {generated.usage.deck.used} of {generated.usage.deck.limit} flashcard sets
</span>
<Button
size="sm"
icon={<RotateCcw />}
disabled={generated.usage.quiz.used + generated.usage.deck.used === 0}
onClick={() =>
act(async () => {
const res = await api.post<{ share: Share; generated: NonNullable<ShareListItem['generated']> }>(`/api/shares/${share.id}/generate/reset`);
setGenerated(res.generated);
toast.success('Limits reset for today');
return { share: res.share };
})
}
>
Reset limits
</Button>
</div>
{generated.sets.length > 0 && (
<ul className="space-y-1">
{generated.sets.slice(0, 8).map((s) => (
<li key={s.id} className="flex items-center gap-2">
{s.kind === 'quiz' ? <BookOpen className="size-3.5 shrink-0 text-ink-3" /> : <Layers className="size-3.5 shrink-0 text-ink-3" />}
<Link to={`/drafts/${s.id}`} onClick={onClose} className="min-w-0 flex-1 truncate font-medium text-ink-2 hover:text-ink hover:underline">
{s.title}
</Link>
<span className="shrink-0 text-xs text-ink-3">{s.status === 'generating' ? 'Writing…' : plural(s.count, s.kind === 'quiz' ? 'question' : 'card')}</span>
</li>
))}
</ul>
)}
{generated.sets.length > 0 && <p className="text-xs text-ink-3">Made by visitors. Open one to review it, save it to your library or discard it from the link.</p>}
</div>
)}
</>
)}
<p className="text-xs text-ink-3">Uses your AI models and is billed to your API keys. MCP servers take part only if you allowed them on share links in Settings → AI. Each device can make 20 requests per 10 minutes, and the link 500 per hour.</p>
</fieldset>
</>

View file

@ -70,6 +70,7 @@ export const api = {
upload: <T>(url: string, form: FormData) => request<T>('POST', url, form),
/** Public (share-link) calls never trigger the login screen. */
pub: <T>(url: string, signal?: AbortSignal) => request<T>('GET', url, undefined, { signal, publicCall: true }),
pubPost: <T>(url: string, body?: unknown) => request<T>('POST', url, body ?? {}, { publicCall: true }),
};
export function errorMessage(e: unknown, fallback = 'Something went wrong.'): string {

View file

@ -82,6 +82,33 @@ export interface DueCounts {
export interface ShareAi {
explain: boolean;
assistant: boolean;
/** Visitors of a class or exam link can generate quizzes and flashcards. */
generate?: boolean;
}
/** Generations a share link has used today. */
export interface ShareGenerateUsage {
quiz: { used: number; limit: number };
deck: { used: number; limit: number };
}
/** A quiz or flashcard set a visitor generated on a share link. */
export interface SharedSet {
id: string;
kind: 'quiz' | 'deck';
title: string;
status: 'generating' | 'ready' | 'failed';
progress: number;
progressLabel: string;
error: string;
count: number;
createdAt: number;
}
export interface SharedGenerated {
sets: SharedSet[];
usage: ShareGenerateUsage;
limits: { quizMax: number; deckMax: number; mathQuestions: number };
}
export interface Share {
@ -344,6 +371,8 @@ export interface ShareListItem extends Share {
classId: string | null;
className: string;
classColor: ClassColor;
/** Class and exam links: what visitors generated, and today's usage. */
generated: { usage: ShareGenerateUsage; sets: SharedSet[] } | null;
}
/* ------------------------------ AI ------------------------------ */

View file

@ -6,9 +6,10 @@ import { isFullyCorrect, scoreQuestion, seededShuffle } from '@shared/content';
import { Explain } from '@/features/Explain';
import { CramSession, freshCramState, reconcileCramState, type CramState } from '@/features/flashcards';
import { SharedAssistant } from '@/features/SharedAssistant';
import { SharedGenerate } from '@/features/SharedGenerate';
import { QuizResults, QuizRunner } from '@/features/quiz';
import { api, ApiError } from '@/lib/api';
import type { AnswerState, PublicCollection, PublicDeck, PublicDocument, PublicNote, PublicPayload, PublicQuiz, SnapshotQuestion } from '@/lib/types';
import type { AnswerState, PublicCollection, PublicDeck, PublicDocument, PublicNote, PublicPayload, PublicQuiz, ShareAi, SnapshotQuestion } from '@/lib/types';
import { cx, formatBytes, plural, useDocumentTitle, useLocalStorage, useMediaQuery, useTheme } from '@/lib/util';
import { Markdown } from '@/ui/Markdown';
import { Button, EmptyState, IconButton, Input, PageLoading, Segmented, Switch } from '@/ui';
@ -484,7 +485,7 @@ const ITEM_ICON = { quiz: <BookOpen />, deck: <Layers />, note: <NotebookPen />,
const KIND_ORDER = ['quiz', 'deck', 'note', 'document'] as const;
const KIND_HEADING = { quiz: 'Quizzes', deck: 'Flashcards', note: 'Notes', document: 'Class Materials' } as const;
function SharedCollection({ data, token }: { data: PublicCollection; token: string }) {
function SharedCollection({ data, token }: { data: PublicCollection & { ai?: ShareAi }; token: string }) {
// One section per exam in the class's order; material outside any exam comes last.
const groups = [...data.folders, { id: null as string | null, name: 'Other' }].map((f) => ({ ...f, items: data.items.filter((i) => (f.id === null ? !i.folderId || !data.folders.some((x) => x.id === i.folderId) : i.folderId === f.id)) })).filter((g) => g.items.length > 0);
return (
@ -494,6 +495,7 @@ function SharedCollection({ data, token }: { data: PublicCollection; token: stri
<h1 className="display text-[32px] sm:text-[40px]">{data.title}</h1>
{data.description && <p className="mt-2 text-[15px] text-ink-2">{data.description}</p>}
</div>
{data.ai?.generate && <SharedGenerate token={token} data={data} />}
{groups.length === 0 ? (
<div className="card mt-8 p-6 text-sm text-ink-2">Nothing has been added here yet.</div>
) : (