RotNot / src /app /api /feynman /curate /route.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
2.08 kB
import { NextRequest, NextResponse } from "next/server";
import { curateSources, getGroundedTeaching, MIN_SOURCES } from "@/lib/sources/curate";
import { FEYNMAN_TOPICS } from "@/lib/feynman/topics";
import { withApiError } from "@/lib/api-error";
/**
* POST /api/feynman/curate
*
* Curate sources for a Feynman topic. If grounded=true, also generates
* grounded teaching content from the sources.
*
* Body: { topicId: string, grounded?: boolean }
*
* Returns:
* - sources: array of curated source objects
* - sourceReady: boolean (>= MIN_SOURCES found)
* - teaching?: grounded teaching content (if grounded=true and sources sufficient)
*/
export const POST = withApiError(async (request: NextRequest) => {
let body: { topicId: string; grounded?: boolean };
try {
body = await request.json();
} catch {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
const { topicId, grounded } = body;
if (!topicId || typeof topicId !== "string") {
return NextResponse.json({ error: "topicId is required" }, { status: 400 });
}
// Look up the topic
const topic = FEYNMAN_TOPICS.find((t) => t.id === topicId);
if (!topic) {
return NextResponse.json({ error: `Unknown topic: ${topicId}` }, { status: 404 });
}
// Curate sources
const sources = await curateSources(topicId, topic.topic, topic.domain);
const sourceReady = sources.length >= MIN_SOURCES;
const result: Record<string, unknown> = {
topicId,
topic: topic.topic,
sources,
sourceCount: sources.length,
sourceReady,
minRequired: MIN_SOURCES,
};
// Optionally generate grounded teaching content
if (grounded && sourceReady) {
if (!process.env.ANTHROPIC_API_KEY) {
return NextResponse.json(
{ error: "ANTHROPIC_API_KEY is not configured" },
{ status: 503 },
);
}
const teaching = await getGroundedTeaching(topicId, topic.topic, topic.difficulty, topic.domain);
if (teaching) {
result.teaching = teaching;
}
}
return NextResponse.json(result);
});