File size: 3,496 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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));
});