Share-link sets save into the class; flashcards up to 75
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 28s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 28s
- Sets visitors generate show in the owner's Drafts while being written, then save into the class (and exam, for an exam link) automatically, as if approved. If the save fails, the draft waits in Drafts for review. - Deleting a share link keeps what was made on it; running jobs finish and save. - Visitor flashcard sets go up to 75 cards (default 30); quizzes stay at 50. - The visitor page links finished sets to the class items; the separate AI-set view is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e945f589ce
commit
76112cb4ae
8 changed files with 146 additions and 103 deletions
|
|
@ -54,7 +54,7 @@ const shareAi = (row: Pick<ShareRow, 'ai'>): ShareAi => ({ explain: false, assis
|
|||
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;
|
||||
const SHARE_DECK_MAX = 75;
|
||||
/** Medication math questions in a visitor's quiz when they ask for them. */
|
||||
const SHARE_MATH_QUESTIONS = 5;
|
||||
|
||||
|
|
@ -71,26 +71,37 @@ function generateUsage(app: AppContext, share: Pick<ShareRow, 'id' | 'generate_r
|
|||
};
|
||||
}
|
||||
|
||||
/** The sets made on a link that visitors can open (or are still being written). */
|
||||
/**
|
||||
* Sets made on a link, newest first. Finished sets are saved into the class like the owner's own
|
||||
* material: `itemId` is the quiz or deck, which the link shows with everything else. A set whose
|
||||
* automatic save failed waits in the owner's drafts (`review`).
|
||||
*/
|
||||
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`,
|
||||
.all<{ id: string; kind: 'quiz' | 'deck'; title: string; status: string; progress: number; progress_label: string; payload: string; saved_kind: string | null; saved_id: string | null; created_at: number }>(
|
||||
`SELECT id, kind, title, status, progress, progress_label, payload, saved_kind, saved_id, created_at FROM drafts WHERE share_id = ? AND status IN ('generating', 'ready', 'failed', 'saved') ORDER BY created_at DESC LIMIT 30`,
|
||||
shareId,
|
||||
)
|
||||
.map((d) => {
|
||||
.flatMap((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 saved = d.status === 'saved' && d.saved_id ? app.db.get<{ title: string }>(`SELECT title FROM ${d.saved_kind === 'deck' ? 'decks' : 'quizzes'} WHERE id = ?`, d.saved_id) : null;
|
||||
// Saved, then deleted by the owner: gone from the link too.
|
||||
if (d.status === 'saved' && !saved) return [];
|
||||
const status = d.status === 'saved' ? ('ready' as const) : d.status === 'ready' ? ('review' as const) : (d.status as 'generating' | 'failed');
|
||||
return [
|
||||
{
|
||||
id: d.id,
|
||||
kind: d.kind,
|
||||
title: saved?.title || d.title || (d.kind === 'quiz' ? 'Practice quiz' : 'Flashcards'),
|
||||
status,
|
||||
itemId: saved ? d.saved_id : null,
|
||||
progress: d.progress,
|
||||
progressLabel: d.progress_label,
|
||||
error: status === 'failed' ? 'This set could not be made. Try again later.' : status === 'review' ? 'Made, and waiting for the owner to check it.' : '',
|
||||
count: (d.kind === 'quiz' ? payload.questions?.length : payload.cards?.length) ?? 0,
|
||||
createdAt: d.created_at,
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -122,6 +133,11 @@ function ownerGenerated(app: AppContext, row: ShareRow) {
|
|||
return { usage: generateUsage(app, row), sets: generatedSets(app, row.id).filter((s) => s.status !== 'failed') };
|
||||
}
|
||||
|
||||
/** Sets made on a link belong to the class: deleting the link keeps them (and lets running ones finish). */
|
||||
function releaseGenerated(app: AppContext, shareId: string): void {
|
||||
app.db.run('UPDATE drafts SET share_id = NULL WHERE share_id = ?', shareId);
|
||||
}
|
||||
|
||||
/* ------------------------------ admin ------------------------------ */
|
||||
|
||||
export function shareRoutes(app: AppContext): Hono {
|
||||
|
|
@ -138,6 +154,7 @@ export function shareRoutes(app: AppContext): Hono {
|
|||
row.target_id,
|
||||
);
|
||||
if (!target) {
|
||||
releaseGenerated(app, row.id);
|
||||
db.run('DELETE FROM shares WHERE id = ?', row.id);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -192,6 +209,7 @@ export function shareRoutes(app: AppContext): Hono {
|
|||
});
|
||||
|
||||
r.delete('/shares/:id', (c) => {
|
||||
releaseGenerated(app, idParam(c));
|
||||
const { changes } = db.run('DELETE FROM shares WHERE id = ?', idParam(c));
|
||||
if (!changes) throw notFound('Share link');
|
||||
return c.json({ ok: true });
|
||||
|
|
@ -414,13 +432,6 @@ 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');
|
||||
|
|
@ -473,31 +484,6 @@ export function publicRoutes(app: AppContext): Hono {
|
|||
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'));
|
||||
|
|
@ -508,7 +494,7 @@ export function publicRoutes(app: AppContext): Hono {
|
|||
|
||||
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(),
|
||||
count: z.number().int().min(1).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(),
|
||||
|
|
@ -536,7 +522,8 @@ export function publicRoutes(app: AppContext): Hono {
|
|||
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);
|
||||
if (isQuiz && (body.count ?? 0) > SHARE_QUIZ_MAX) throw new HttpError(400, 'too_many', `A quiz can have at most ${SHARE_QUIZ_MAX} questions.`);
|
||||
const count = body.count ?? (isQuiz ? SHARE_QUIZ_MAX : 30);
|
||||
const { draftId } = startGeneration(
|
||||
app,
|
||||
{
|
||||
|
|
@ -548,7 +535,8 @@ export function publicRoutes(app: AppContext): Hono {
|
|||
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 },
|
||||
// Saved into the class (and exam) as if the owner had approved it; in Drafts while it is written.
|
||||
{ origin: 'share', shareId: share.id, autoSave: { description: 'Made with AI on a share link.' } },
|
||||
);
|
||||
return c.json({ set: generatedSets(app, share.id).find((s) => s.id === draftId), usage: generateUsage(app, share) }, 202);
|
||||
});
|
||||
|
|
@ -569,20 +557,18 @@ export function publicRoutes(app: AppContext): Hono {
|
|||
let prompt: string;
|
||||
let sources: ReturnType<typeof citedPassages> = [];
|
||||
if (body.kind === 'question') {
|
||||
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 (!inScope(share, 'quiz', body.quizId)) throw gone();
|
||||
const q = 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 = made ? sharedClassId(share) : db.get<{ class_id: string }>('SELECT class_id FROM quizzes WHERE id = ?', body.quizId)?.class_id;
|
||||
const classId = 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 {
|
||||
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 (!inScope(share, 'deck', body.deckId)) throw gone();
|
||||
const card = 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 });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue