File size: 4,140 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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);
});
|