feat(阶段3): oj 侧端点铺开(基线提交,未经评审)
由外部 agent (Codex) 在本会话额度中断期间完成。原样提交作为基线, 后续修复单独成 commit,便于区分与回退。 覆盖 oj 侧 65 个端点,新增 9 组路由(account/achievement/ai/classroom/ content/contest/flowchart/problemset/site)与对应 Zod 契约。 已核验:tsc --noEmit 退出码 0;API 可启动;/api/problems 返回真实数据; judge 与 flowchart worker 均 ready。 未核验:权限边界与数据泄露,评审进行中。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec274419c3
commit
8c00cdc947
52 files changed
+4273
-300
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { flowchartUpdateSchema } from "@oj2/contract"
|
||||
import { eq } from "drizzle-orm"
|
||||
|
||||
import { db, schema } from "../db"
|
||||
import { publishFlowchartUpdate } from "../events"
|
||||
import { completeChat } from "../services/ai"
|
||||
import type { FlowchartJobData } from "./job"
|
||||
|
||||
function evaluationPrompt(problem: typeof schema.problem.$inferSelect) {
|
||||
return `你是专业的编程教学助手,负责评估学生的 Mermaid 流程图。
|
||||
评分:逻辑正确性40分、完整性30分、规范性20分、清晰度10分。
|
||||
不要评价系统生成的节点ID。feedback不超过100字;suggestions最多3条且只针对真实问题。
|
||||
返回纯 JSON:{"score":85,"grade":"A","feedback":"...","suggestions":"...","criteria_details":{}}。
|
||||
等级:S=90-100,A=80-89,B=70-79,C=0-69。
|
||||
题目:${problem.title}\n${problem.description.slice(0, 2000)}`
|
||||
}
|
||||
|
||||
function parseEvaluation(value: string) {
|
||||
const block = value.match(/```(?:json)?\s*([\s\S]*?)```/)?.[1]
|
||||
const json = block ?? value.match(/\{[\s\S]*\}/)?.[0]
|
||||
if (!json) throw new Error("AI response did not contain JSON")
|
||||
const data = JSON.parse(json) as Record<string, unknown>
|
||||
if (typeof data.score !== "number" || typeof data.grade !== "string") throw new Error("AI response is missing score or grade")
|
||||
return {
|
||||
score: Math.max(0, Math.min(100, data.score)),
|
||||
grade: data.grade,
|
||||
feedback: typeof data.feedback === "string" ? data.feedback : "",
|
||||
suggestions: typeof data.suggestions === "string" ? data.suggestions : "",
|
||||
criteria: data.criteria_details && typeof data.criteria_details === "object" ? data.criteria_details : {},
|
||||
}
|
||||
}
|
||||
|
||||
export async function evaluateFlowchart(job: FlowchartJobData) {
|
||||
const [row] = await db.select({ flowchart: schema.flowchartSubmission, problem: schema.problem }).from(schema.flowchartSubmission)
|
||||
.innerJoin(schema.problem, eq(schema.flowchartSubmission.problemId, schema.problem.id))
|
||||
.where(eq(schema.flowchartSubmission.id, job.submissionId)).limit(1)
|
||||
if (!row || ![0, 1].includes(row.flowchart.status)) return
|
||||
await db.update(schema.flowchartSubmission).set({ status: 1 }).where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
const started = performance.now()
|
||||
try {
|
||||
const reference = row.problem.mermaidCode ? `\n标准答案参考:\n${row.problem.mermaidCode}` : "\n此题没有标准流程图。"
|
||||
const result = parseEvaluation(await completeChat(
|
||||
evaluationPrompt(row.problem),
|
||||
`学生流程图:\n${row.flowchart.mermaidCode}${reference}\n设计提示:${row.problem.flowchartHint ?? "无"}`,
|
||||
))
|
||||
await db.update(schema.flowchartSubmission).set({
|
||||
status: 2,
|
||||
aiScore: result.score,
|
||||
aiGrade: result.grade,
|
||||
aiFeedback: result.feedback,
|
||||
aiSuggestions: result.suggestions,
|
||||
aiCriteriaDetails: result.criteria,
|
||||
aiProvider: "deepseek",
|
||||
aiModel: process.env.AI_MODEL ?? "deepseek-v4-flash",
|
||||
processingTime: (performance.now() - started) / 1000,
|
||||
evaluationTime: new Date().toISOString(),
|
||||
}).where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
await publishFlowchartUpdate(row.flowchart.userId, flowchartUpdateSchema.parse({
|
||||
type: "flowchart_evaluation_completed",
|
||||
submission_id: row.flowchart.id,
|
||||
score: result.score,
|
||||
grade: result.grade,
|
||||
feedback: result.feedback,
|
||||
suggestions: result.suggestions,
|
||||
criteriaDetails: result.criteria,
|
||||
}))
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
await db.update(schema.flowchartSubmission).set({ status: 3 }).where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
await publishFlowchartUpdate(row.flowchart.userId, flowchartUpdateSchema.parse({
|
||||
type: "flowchart_evaluation_failed",
|
||||
submission_id: row.flowchart.id,
|
||||
error: message,
|
||||
}))
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user