Download src/app/api/debate/start/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/start/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/debate/start/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/start/route.ts
3.6 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { | |
| getDebateTopicById, | |
| getRandomDebateTopic, | |
| type DebateCategory, | |
| DEBATE_CATEGORIES, | |
| } from "@/lib/debate/topics"; | |
| import { | |
| getOpponentById, | |
| getRandomOpponent, | |
| type OpponentDifficulty, | |
| } from "@/lib/debate/opponents"; | |
| import { withApiError } from "@/lib/api-error"; | |
| const VALID_DIFFICULTIES: OpponentDifficulty[] = ["easy", "medium", "hard"]; | |
| const VALID_ROUNDS = [1, 3, 5]; | |
| /** | |
| * POST /api/debate/start | |
| * | |
| * Body: | |
| * topicId?: string — pick a specific topic | |
| * customTopic?: string — free-text topic (overrides topicId) | |
| * category?: DebateCategory — filter for random topic | |
| * opponentId?: string — pick a specific opponent | |
| * difficulty?: "easy"|"medium"|"hard" — for random opponent | |
| * rounds?: 1|3|5 — number of debate rounds (default 3) | |
| * userSide?: "pro"|"con" — which side the user argues (default "pro") | |
| * | |
| * Returns a debate session configuration object that the client | |
| * holds in state across the multi-round flow. | |
| */ | |
| export const POST = withApiError(async (request: NextRequest) => { | |
| let body: Record<string, unknown>; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | |
| } | |
| // --- Resolve topic --- | |
| let topic: { id: string; topic: string; category: string; description: string }; | |
| if (typeof body.customTopic === "string" && body.customTopic.trim().length >= 5) { | |
| topic = { | |
| id: "custom", | |
| topic: body.customTopic.trim(), | |
| category: "Custom", | |
| description: "User-provided debate topic", | |
| }; | |
| } else if (typeof body.topicId === "string") { | |
| const found = getDebateTopicById(body.topicId); | |
| if (!found) { | |
| return NextResponse.json({ error: "Topic not found" }, { status: 404 }); | |
| } | |
| topic = found; | |
| } else { | |
| const category = | |
| typeof body.category === "string" && | |
| DEBATE_CATEGORIES.includes(body.category as DebateCategory) | |
| ? (body.category as DebateCategory) | |
| : undefined; | |
| const random = getRandomDebateTopic(category); | |
| topic = random; | |
| } | |
| // --- Resolve opponent --- | |
| let opponent: { id: string; name: string; difficulty: string; description: string; icon: string; era?: string; systemPrompt: string }; | |
| if (typeof body.opponentId === "string") { | |
| const found = getOpponentById(body.opponentId); | |
| if (!found) { | |
| return NextResponse.json({ error: "Opponent not found" }, { status: 404 }); | |
| } | |
| opponent = found; | |
| } else { | |
| const difficulty: OpponentDifficulty = | |
| typeof body.difficulty === "string" && | |
| VALID_DIFFICULTIES.includes(body.difficulty as OpponentDifficulty) | |
| ? (body.difficulty as OpponentDifficulty) | |
| : "medium"; | |
| opponent = getRandomOpponent(difficulty); | |
| } | |
| // --- Rounds --- | |
| const rounds = | |
| typeof body.rounds === "number" && VALID_ROUNDS.includes(body.rounds) | |
| ? body.rounds | |
| : 3; | |
| // --- User side --- | |
| const userSide = | |
| body.userSide === "con" ? "con" : "pro"; | |
| return NextResponse.json({ | |
| topic: { | |
| id: topic.id, | |
| topic: topic.topic, | |
| category: topic.category, | |
| description: topic.description, | |
| }, | |
| opponent: { | |
| id: opponent.id, | |
| name: opponent.name, | |
| difficulty: opponent.difficulty, | |
| description: opponent.description, | |
| icon: opponent.icon, | |
| era: opponent.era ?? null, | |
| }, | |
| rounds, | |
| userSide, | |
| // systemPrompt is NOT returned to the client — it's used server-side only | |
| }); | |
| }); | |