Download src/app/api/coding/submit-code/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/coding/submit-code/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/coding/submit-code/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/coding/submit-code/route.ts
3.17 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { withApiError, ApiError } from "@/lib/api-error"; | |
| import { resolveProblem } from "@/lib/coding/store"; | |
| import { runJavaScriptTests } from "@/lib/coding/harness"; | |
| import { gradeCodeRun } from "@/lib/coding/grade"; | |
| import { awardCodingXp } from "@/lib/coding/award"; | |
| import { | |
| isExecutableProblem, | |
| isExecutableLanguage, | |
| type CodingDifficulty, | |
| } from "@/lib/coding/types"; | |
| interface SubmitCodeBody { | |
| instanceId?: string; | |
| code?: string; | |
| durationSecs?: number; | |
| userId?: number; | |
| } | |
| /** | |
| * POST /api/coding/submit-code | |
| * Body: { instanceId, code, durationSecs? } | |
| * | |
| * Runs the user's code against the FULL (incl. hidden) test suite in the | |
| * sandbox. When every test passes, awards "code" XP for the coding category. | |
| * This is the test-driven correctness check. | |
| */ | |
| export const POST = withApiError(async (request: NextRequest) => { | |
| let body: SubmitCodeBody; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| throw new ApiError(400, "Invalid JSON body"); | |
| } | |
| const { instanceId, code, durationSecs } = body; | |
| const userId = body.userId ?? 1; | |
| if (!instanceId || typeof code !== "string" || code.trim().length === 0) { | |
| throw new ApiError(400, "instanceId and non-empty code are required"); | |
| } | |
| const problem = resolveProblem(instanceId); | |
| if (!problem) { | |
| throw new ApiError(404, "Problem not found (it may have expired — regenerate it)."); | |
| } | |
| if (!isExecutableProblem(problem)) { | |
| throw new ApiError(400, "This problem type is graded by written answer, not code."); | |
| } | |
| if (!isExecutableLanguage(problem.language)) { | |
| throw new ApiError( | |
| 400, | |
| `Auto-grading by execution supports JavaScript/TypeScript only.`, | |
| ); | |
| } | |
| const run = runJavaScriptTests(code, problem.entryFunction, problem.tests); | |
| const grade = gradeCodeRun(run); | |
| // Hide nothing post-submission — full TDD feedback helps the user learn. | |
| const baseResponse = { | |
| correct: grade.isCorrect, | |
| passed: run.passed, | |
| total: run.total, | |
| results: run.results, | |
| stdout: run.stdout, | |
| compileError: run.compileError ?? null, | |
| qualityScore: grade.qualityScore, | |
| }; | |
| if (!grade.isCorrect) { | |
| return NextResponse.json({ | |
| ...baseResponse, | |
| awarded: false, | |
| message: | |
| run.compileError != null | |
| ? "Your code did not compile." | |
| : `${run.passed}/${run.total} tests passed. Keep going!`, | |
| }); | |
| } | |
| const award = await awardCodingXp({ | |
| userId, | |
| exerciseType: "code", | |
| description: `Solved ${problem.difficulty} ${problem.type} "${problem.title}" (code)`, | |
| qualityScore: grade.qualityScore, | |
| difficulty: problem.difficulty as CodingDifficulty, | |
| durationSecs, | |
| metadata: { | |
| problemId: problem.id, | |
| problemType: problem.type, | |
| language: problem.language, | |
| difficulty: problem.difficulty, | |
| passed: run.passed, | |
| total: run.total, | |
| kind: "code", | |
| }, | |
| }); | |
| return NextResponse.json({ | |
| ...baseResponse, | |
| awarded: true, | |
| ...award, | |
| message: award.isCriticalHit | |
| ? "Critical Hit! All tests pass!" | |
| : "All tests pass — solved!", | |
| }); | |
| }); | |