import { NextRequest, NextResponse } from "next/server"; import { withApiError, ApiError } from "@/lib/api-error"; import { pickProblem } from "@/lib/coding/bank"; import { registerProblem } from "@/lib/coding/store"; import { toPublicProblem } from "@/lib/coding/serialize"; import { CODING_PROBLEM_TYPES, CODING_DIFFICULTIES, CODING_LANGUAGES, isExecutableType, isExecutableLanguage, type CodingProblemType, type CodingDifficulty, type CodingLanguage, type CodingProblem, } from "@/lib/coding/types"; interface GenerateBody { type?: string; difficulty?: string; language?: string; } function parseParams(source: { type?: string | null; difficulty?: string | null; language?: string | null; }): { type: CodingProblemType; difficulty: CodingDifficulty; language: CodingLanguage } { const type = (source.type ?? "code-creation") as CodingProblemType; const difficulty = (source.difficulty ?? "easy") as CodingDifficulty; const language = (source.language ?? "javascript") as CodingLanguage; if (!CODING_PROBLEM_TYPES.includes(type)) { throw new ApiError(400, `Invalid type. Must be one of: ${CODING_PROBLEM_TYPES.join(", ")}`); } if (!CODING_DIFFICULTIES.includes(difficulty)) { throw new ApiError(400, `Invalid difficulty. Must be one of: ${CODING_DIFFICULTIES.join(", ")}`); } if (!CODING_LANGUAGES.includes(language)) { throw new ApiError(400, `Invalid language. Must be one of: ${CODING_LANGUAGES.join(", ")}`); } return { type, difficulty, language }; } function buildResponse(problem: CodingProblem, instanceId: string) { const instance: CodingProblem = { ...problem, id: instanceId }; registerProblem(instance); return { instanceId, problem: toPublicProblem(instance), meta: { gradedBy: isExecutableType(instance.type) ? "tests" : "llm-judge", executable: isExecutableType(instance.type) && isExecutableLanguage(instance.language), source: "bank", }, }; } /** * POST /api/coding/generate * Body: { type, difficulty, language } * * Returns a fresh problem-set instance (answer key stripped). The full * definition is registered server-side under `instanceId` for grading. */ export const POST = withApiError(async (request: NextRequest) => { let body: GenerateBody = {}; try { body = await request.json(); } catch { // allow empty body → defaults } const { type, difficulty, language } = parseParams(body); const problem = pickProblem({ type, difficulty, language }); if (!problem) { throw new ApiError( 422, `No ${difficulty} ${type} problem is available for ${language} yet.`, ); } const instanceId = `${problem.id}::${crypto.randomUUID()}`; return NextResponse.json(buildResponse(problem, instanceId)); }); /** GET variant for convenience (?type=&difficulty=&language=). */ export const GET = withApiError(async (request: NextRequest) => { const { searchParams } = new URL(request.url); const { type, difficulty, language } = parseParams({ type: searchParams.get("type"), difficulty: searchParams.get("difficulty"), language: searchParams.get("language"), }); const problem = pickProblem({ type, difficulty, language }); if (!problem) { throw new ApiError( 422, `No ${difficulty} ${type} problem is available for ${language} yet.`, ); } const instanceId = `${problem.id}::${crypto.randomUUID()}`; return NextResponse.json(buildResponse(problem, instanceId)); });