Download src/app/api/feynman/teach-deep/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/feynman/teach-deep/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/feynman/teach-deep/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/feynman/teach-deep/route.ts
3.72 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { claude, parseClaudeJson } from "@/lib/claude"; | |
| import { withApiError } from "@/lib/api-error"; | |
| interface TeachDeepBody { | |
| topic: string; | |
| difficulty?: string; | |
| previousExplanation: string; | |
| previousTakeaways: string[]; | |
| } | |
| 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: TeachDeepBody; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | |
| } | |
| const { topic, difficulty, previousExplanation, previousTakeaways } = body; | |
| if (!topic || typeof topic !== "string") { | |
| return NextResponse.json({ error: "topic is required" }, { status: 400 }); | |
| } | |
| if (!previousExplanation || typeof previousExplanation !== "string") { | |
| return NextResponse.json( | |
| { error: "previousExplanation is required" }, | |
| { status: 400 }, | |
| ); | |
| } | |
| const difficultyCtx = difficulty | |
| ? ` The topic is considered ${difficulty}-level.` | |
| : ""; | |
| const takeawaysList = | |
| previousTakeaways && previousTakeaways.length > 0 | |
| ? `\n\nKey takeaways already covered:\n${previousTakeaways.map((t) => `- ${t}`).join("\n")}` | |
| : ""; | |
| try { | |
| const message = await claude.messages.create({ | |
| model: "claude-haiku-4-5-20251001", | |
| max_tokens: 2000, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: `You are a brilliant teacher. A student is learning about "${topic}" and wants to go deeper.${difficultyCtx} | |
| They've already read this initial explanation: | |
| --- | |
| ${previousExplanation} | |
| ---${takeawaysList} | |
| Now provide a deeper dive that: | |
| 1. Explains the "why" behind the concept — what makes it work, its underlying mechanism or logic | |
| 2. Gives 2 detailed, concrete examples showing the concept in action (real-world scenarios, not abstract) | |
| 3. Provides a memorable analogy that connects this to something from everyday life | |
| 4. Mentions 1-2 common misconceptions people have about this topic and why they're wrong | |
| 5. Briefly notes how this concept connects to related ideas (what it builds on, what it leads to) | |
| Do NOT repeat what was already covered in the initial explanation. Build on it. Go deeper, not wider. | |
| Respond ONLY in valid JSON (no markdown, no code fences): | |
| { | |
| "deeperExplanation": "2-3 paragraphs going deeper into the why/how", | |
| "examples": [ | |
| { "title": "short title", "description": "detailed example" }, | |
| { "title": "short title", "description": "detailed example" } | |
| ], | |
| "analogy": "a vivid, memorable analogy", | |
| "misconceptions": [ | |
| { "myth": "the misconception", "reality": "why it's wrong" } | |
| ], | |
| "connections": "brief note on related concepts" | |
| }`, | |
| }, | |
| ], | |
| }); | |
| const text = | |
| message.content[0].type === "text" ? message.content[0].text : ""; | |
| const parsed = parseClaudeJson(text) as { | |
| deeperExplanation: string; | |
| examples: Array<{ title: string; description: string }>; | |
| analogy: string; | |
| misconceptions: Array<{ myth: string; reality: string }>; | |
| connections: string; | |
| }; | |
| return NextResponse.json({ | |
| topic, | |
| deeperExplanation: parsed.deeperExplanation, | |
| examples: parsed.examples, | |
| analogy: parsed.analogy, | |
| misconceptions: parsed.misconceptions, | |
| connections: parsed.connections, | |
| }); | |
| } catch (err) { | |
| return NextResponse.json( | |
| { | |
| error: "Failed to generate deep teaching content", | |
| details: err instanceof Error ? err.message : String(err), | |
| }, | |
| { status: 500 }, | |
| ); | |
| } | |
| }); | |