Download src/app/api/debate/respond/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.89 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/respond/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/debate/respond/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/respond/route.ts
3.89 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { claude } from "@/lib/claude"; | |
| import { getOpponentById } from "@/lib/debate/opponents"; | |
| import { withApiError } from "@/lib/api-error"; | |
| interface RespondBody { | |
| topic: string; | |
| opponentId: string; | |
| round: number; | |
| totalRounds: number; | |
| userSide: "pro" | "con"; | |
| userArgument: string; | |
| /** Previous round exchanges for context */ | |
| history?: Array<{ | |
| round: number; | |
| userArgument: string; | |
| opponentArgument: string; | |
| }>; | |
| } | |
| /** | |
| * POST /api/debate/respond | |
| * | |
| * The user submits their argument; the AI opponent generates a rebuttal. | |
| */ | |
| 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: RespondBody; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | |
| } | |
| if (!body.topic || typeof body.topic !== "string") { | |
| return NextResponse.json({ error: "topic is required" }, { status: 400 }); | |
| } | |
| if (!body.opponentId || typeof body.opponentId !== "string") { | |
| return NextResponse.json({ error: "opponentId is required" }, { status: 400 }); | |
| } | |
| if (!body.userArgument || typeof body.userArgument !== "string" || body.userArgument.trim().length < 20) { | |
| return NextResponse.json( | |
| { error: "userArgument must be at least 20 characters" }, | |
| { status: 400 }, | |
| ); | |
| } | |
| const opponent = getOpponentById(body.opponentId); | |
| if (!opponent) { | |
| return NextResponse.json({ error: "Opponent not found" }, { status: 404 }); | |
| } | |
| const round = typeof body.round === "number" ? body.round : 1; | |
| const totalRounds = typeof body.totalRounds === "number" ? body.totalRounds : 3; | |
| const userSide = body.userSide === "con" ? "con" : "pro"; | |
| const opponentSide = userSide === "pro" ? "con" : "pro"; | |
| // Build conversation history for context | |
| let historyContext = ""; | |
| if (body.history && body.history.length > 0) { | |
| historyContext = "\n\nPREVIOUS ROUNDS:\n" + body.history | |
| .map( | |
| (h) => | |
| `Round ${h.round}:\n- User (${userSide}): ${h.userArgument}\n- ${opponent.name} (${opponentSide}): ${h.opponentArgument}`, | |
| ) | |
| .join("\n\n"); | |
| } | |
| const systemPrompt = `${opponent.systemPrompt} | |
| You are debating the topic: "${body.topic}" | |
| You are arguing the ${opponentSide.toUpperCase()} side. | |
| This is round ${round} of ${totalRounds}. | |
| CRITICAL RULES: | |
| - Stay in character as ${opponent.name}. | |
| - Directly address the user's arguments — do NOT ignore what they said. | |
| - Build on previous rounds if applicable. | |
| - Do not break the fourth wall or reference that you are an AI.`; | |
| const userMessage = `${historyContext} | |
| CURRENT ROUND ${round}: | |
| The user (arguing ${userSide.toUpperCase()}) says: | |
| ${body.userArgument.trim()} | |
| Respond with your ${opponentSide.toUpperCase()} argument. Output ONLY your debate argument as plain text — no JSON, no formatting instructions.`; | |
| try { | |
| const message = await claude.messages.create({ | |
| model: "claude-haiku-4-5-20251001", | |
| max_tokens: 1024, | |
| system: systemPrompt, | |
| messages: [{ role: "user", content: userMessage }], | |
| }); | |
| const opponentArgument = | |
| message.content[0].type === "text" ? message.content[0].text.trim() : ""; | |
| if (!opponentArgument) { | |
| return NextResponse.json( | |
| { error: "Opponent generated empty response" }, | |
| { status: 500 }, | |
| ); | |
| } | |
| return NextResponse.json({ | |
| round, | |
| opponentName: opponent.name, | |
| opponentArgument, | |
| }); | |
| } catch (err) { | |
| return NextResponse.json( | |
| { | |
| error: "Failed to generate opponent response", | |
| details: err instanceof Error ? err.message : String(err), | |
| }, | |
| { status: 500 }, | |
| ); | |
| } | |
| }); | |