Download src/app/api/reading/questions/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/reading/questions/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/reading/questions/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/reading/questions/route.ts
3.28 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { claude, parseClaudeJson } from "@/lib/claude"; | |
| import { getArticleBySlug } from "@/lib/reading/articles"; | |
| import { withApiError } from "@/lib/api-error"; | |
| export interface ComprehensionQuestion { | |
| id: number; | |
| type: "recall" | "inference" | "critical"; | |
| question: string; | |
| } | |
| /** | |
| * POST /api/reading/questions | |
| * | |
| * Body: { slug: string } | { content: { title: string; body: string } } | |
| * | |
| * Calls Claude to generate 4 comprehension questions (recall, inference, critical) | |
| * based on article content. Accepts either a seeded article slug or custom content. | |
| */ | |
| export const POST = withApiError(async (request: NextRequest) => { | |
| if (!process.env.ANTHROPIC_API_KEY) { | |
| return NextResponse.json( | |
| { error: "ANTHROPIC_API_KEY is not configured" }, | |
| { status: 503 }, | |
| ); | |
| } | |
| let body: { slug?: string; content?: { title: string; body: string } }; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | |
| } | |
| const { slug, content } = body; | |
| // Resolve article content — seeded slug or custom content | |
| let articleTitle: string; | |
| let articleBody: string; | |
| if (slug && typeof slug === "string") { | |
| const article = getArticleBySlug(slug); | |
| if (!article) { | |
| return NextResponse.json({ error: "Article not found" }, { status: 404 }); | |
| } | |
| articleTitle = article.title; | |
| articleBody = article.body; | |
| } else if ( | |
| content && | |
| typeof content.title === "string" && | |
| typeof content.body === "string" | |
| ) { | |
| articleTitle = content.title; | |
| articleBody = content.body; | |
| } else { | |
| return NextResponse.json( | |
| { error: "Provide either a 'slug' or 'content' object with title and body" }, | |
| { status: 400 }, | |
| ); | |
| } | |
| let questions: ComprehensionQuestion[]; | |
| try { | |
| const message = await claude.messages.create({ | |
| model: "claude-haiku-4-5-20251001", | |
| max_tokens: 1024, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: `You are generating comprehension questions for a reading exercise. The reader has just finished reading the article below and must now answer questions from memory. | |
| Generate exactly 4 questions: | |
| - 1 recall question (tests direct memory of stated facts) | |
| - 2 inference questions (tests understanding of implied meaning or logical connections) | |
| - 1 critical thinking question (tests ability to evaluate, challenge, or extend the argument) | |
| Respond ONLY in valid JSON (no markdown, no code fences): | |
| [ | |
| { "id": 1, "type": "recall", "question": "..." }, | |
| { "id": 2, "type": "inference", "question": "..." }, | |
| { "id": 3, "type": "inference", "question": "..." }, | |
| { "id": 4, "type": "critical", "question": "..." } | |
| ] | |
| Article title: "${articleTitle}" | |
| Article: | |
| ${articleBody}`, | |
| }, | |
| ], | |
| }); | |
| const text = | |
| message.content[0].type === "text" ? message.content[0].text : "[]"; | |
| questions = parseClaudeJson(text) as ComprehensionQuestion[]; | |
| } catch (err) { | |
| return NextResponse.json( | |
| { | |
| error: "Failed to generate questions", | |
| details: err instanceof Error ? err.message : String(err), | |
| }, | |
| { status: 500 }, | |
| ); | |
| } | |
| return NextResponse.json({ questions }); | |
| }); | |