Download src/app/api/quests/generate/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/quests/generate/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/quests/generate/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/quests/generate/route.ts
4.14 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { claude, parseClaudeJson } from "@/lib/claude"; | |
| import { withApiError } from "@/lib/api-error"; | |
| export interface QuestExercise { | |
| id: number; | |
| type: "multiple-choice" | "short-answer"; | |
| question: string; | |
| options?: string[]; // for multiple-choice | |
| correctAnswer: string; // for MC: exact match; for SA: model answer | |
| } | |
| export interface QuestLesson { | |
| domain: string; | |
| difficulty: string; | |
| title: string; | |
| lesson: string; | |
| exercises: QuestExercise[]; | |
| } | |
| const DOMAINS = [ | |
| "Language", | |
| "Music Theory", | |
| "Mental Math", | |
| "Science", | |
| "Geography", | |
| "History", | |
| "Philosophy", | |
| "Economics", | |
| "Art & Architecture", | |
| "Astronomy", | |
| "Cooking & Food Science", | |
| "Logic & Puzzles", | |
| "Law & Justice", | |
| "Environmental Science", | |
| ] as const; | |
| export type QuestDomain = (typeof DOMAINS)[number]; | |
| const DIFFICULTIES = ["beginner", "intermediate", "advanced"] as const; | |
| function normalizeParam<T extends readonly string[]>( | |
| value: string | null, | |
| allowed: T, | |
| fallback: T[number], | |
| ): T[number] { | |
| if (value && (allowed as readonly string[]).includes(value)) { | |
| return value as T[number]; | |
| } | |
| return fallback; | |
| } | |
| export const GET = withApiError(async (request: NextRequest) => { | |
| if (!process.env.ANTHROPIC_API_KEY) { | |
| return NextResponse.json( | |
| { error: "ANTHROPIC_API_KEY is not configured" }, | |
| { status: 503 }, | |
| ); | |
| } | |
| const { searchParams } = new URL(request.url); | |
| const rawDomain = searchParams.get("domain"); | |
| const rawDifficulty = searchParams.get("difficulty"); | |
| const domain = normalizeParam(rawDomain, DOMAINS, "Science"); | |
| const difficulty = normalizeParam(rawDifficulty, DIFFICULTIES, "beginner"); | |
| let quest: QuestLesson; | |
| try { | |
| const message = await claude.messages.create({ | |
| model: "claude-haiku-4-5-20251001", | |
| max_tokens: 2048, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: `You are a master educator creating a ${difficulty} micro-lesson on a specific topic within the domain "${domain}". | |
| Pick an interesting, specific subtopic within "${domain}" that can be explained in 3-4 concise paragraphs. | |
| Create a micro-lesson with: | |
| 1. A clear, specific title | |
| 2. A lesson body: 3 short paragraphs totaling 150-250 words. Teach a concrete concept, fact, or skill — not vague generalities. | |
| 3. Exactly 4 exercises: 3 multiple-choice (each with exactly 4 options, one correct) and 1 short-answer (open-ended, testing application of the lesson). | |
| For multiple-choice: the correctAnswer must exactly match one of the 4 options strings. | |
| For short-answer: the correctAnswer should be a 1-2 sentence model answer used for grading. | |
| Respond ONLY with valid JSON (no markdown, no code fences): | |
| { | |
| "domain": "${domain}", | |
| "difficulty": "${difficulty}", | |
| "title": "...", | |
| "lesson": "...", | |
| "exercises": [ | |
| { | |
| "id": 1, | |
| "type": "multiple-choice", | |
| "question": "...", | |
| "options": ["A", "B", "C", "D"], | |
| "correctAnswer": "A" | |
| }, | |
| { | |
| "id": 2, | |
| "type": "multiple-choice", | |
| "question": "...", | |
| "options": ["A", "B", "C", "D"], | |
| "correctAnswer": "B" | |
| }, | |
| { | |
| "id": 3, | |
| "type": "multiple-choice", | |
| "question": "...", | |
| "options": ["A", "B", "C", "D"], | |
| "correctAnswer": "C" | |
| }, | |
| { | |
| "id": 4, | |
| "type": "short-answer", | |
| "question": "...", | |
| "correctAnswer": "Model answer..." | |
| } | |
| ] | |
| }`, | |
| }, | |
| ], | |
| }); | |
| const text = | |
| message.content[0].type === "text" ? message.content[0].text : ""; | |
| quest = parseClaudeJson(text) as QuestLesson; | |
| } catch (err) { | |
| return NextResponse.json( | |
| { | |
| error: "Failed to generate quest", | |
| details: err instanceof Error ? err.message : String(err), | |
| }, | |
| { status: 500 }, | |
| ); | |
| } | |
| // Validate minimal shape | |
| if ( | |
| !quest.title || | |
| !quest.lesson || | |
| !Array.isArray(quest.exercises) || | |
| quest.exercises.length === 0 | |
| ) { | |
| return NextResponse.json( | |
| { error: "Malformed quest response from Claude" }, | |
| { status: 500 }, | |
| ); | |
| } | |
| return NextResponse.json(quest); | |
| }); | |