feat(AI 提示): 两段式提示——先诊断出错误标签,再生成提示
Deploy / deploy (push) Has been cancelled

AI 时代 OJ 设计的 2b。标准答案能让提示准得多,但不能进生成提示的 prompt
(学生代码里一段注释就能把它套走),所以拆成两段:
- 诊断:看得到标准答案、第一个没过的测试点、带行号的学生代码;出参只允许
  { tag, lines, confidence },safeParse 过闸、多余字段剥掉,没有自由文本通道
- 生成提示:看不到标准答案和测试点原文,只多一句「问题定位:X,大约在第 a 行」
- 诊断失败(20 秒超时、不是 JSON、校验不过)退回单段式;同一条提交复用诊断;
  编译失败不诊断
- 契约新增 HINT_ERROR_TAGS(13 个,落库值,只增不改)与 hintDiagnosisSchema
- 迁移 0018:ai_hint 加 diagnosis / diagnosis_error 两列
- 单段式 prompt 原样搬进 services/hint-diagnosis.ts,记版本 1;两段式记版本 2
- completeChat 支持 JSON 模式和自定义超时,现有调用不受影响
- 开关 AI_HINT_DIAGNOSE 默认关(2a 的基线还在攒),两套生产 compose 透传

实跑(一次性库 + 本地假 LLM):开关关时请求与 2a 逐字节相同;开时正常 /
复用 / 坏 JSON / 非法标签 / 行号越界 / 超时 / 编译失败七种场景符合预期;
12 次模型请求里诊断全都带标准答案和测试点,生成全都不带。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-19 06:56:38 -06:00
co-authored by Claude Opus 5
parent 47d8f46bdb
commit 06ad6745b7
12 changed files with 4308 additions and 30 deletions
+10 -3
View File
@@ -5,13 +5,15 @@ interface ChatMessage {
content: string
}
function requestBody(messages: ChatMessage[], stream: boolean) {
function requestBody(messages: ChatMessage[], stream: boolean, json = false) {
return {
model: config.aiModel,
messages,
stream,
temperature: 0,
thinking: { type: "disabled" },
// DeepSeek 的 JSON 模式:保证回的是合法 JSON,但 prompt 里得出现「json」字样
...(json ? { response_format: { type: "json_object" } } : {}),
}
}
@@ -22,11 +24,15 @@ function requestBody(messages: ChatMessage[], stream: boolean) {
*/
const COMPLETE_TIMEOUT_MS = 60_000
export async function completeChat(system: string, user: string) {
export async function completeChat(
system: string,
user: string,
options: { json?: boolean; timeoutMs?: number } = {},
) {
if (!config.aiKey) throw new Error("缺少 AI_KEY")
const response = await fetch(new URL("/chat/completions", config.aiBaseUrl), {
method: "POST",
signal: AbortSignal.timeout(COMPLETE_TIMEOUT_MS),
signal: AbortSignal.timeout(options.timeoutMs ?? COMPLETE_TIMEOUT_MS),
headers: {
"content-type": "application/json",
authorization: `Bearer ${config.aiKey}`,
@@ -38,6 +44,7 @@ export async function completeChat(system: string, user: string) {
{ role: "user", content: user },
],
false,
options.json,
),
),
})