feat(AI 提示): 提示分级(L0 反问 / L1 定位 / L2 概念)与输出后过滤
Deploy / deploy (push) Canceled after 0s

提示不再一上来就把话说完。等级记在「学生 × 题目」上,没有单独的表 —— 它就是
ai_hint.level 的历史,当前等级 = 这道题上(最近一次 AC 之后)给过的最高一级。

阶梯(services/hint-level.ts)
- 只有学生点「再多一点提示」才升级(请求带 more),不带就按当前等级再生成一次
- 升一级要先再交一次:锚点是「这一级是**什么时候**开出来的」,也就是这一级最早那条
  提示的 ai_hint.create_time,必须有比这个时刻更新的提交才准 +1。锚点不能用提示所在
  那条提交的时间 —— 端点谁的提交 id 都认(只校验归属),拿一条老提交去要提示,锚点
  就退回到那条老提交的时间,连点两下 more 就能从 L0 爬到 L2,一次新提交都不用交
- AC 之后清零;编译失败自成一档(level = -1),既不消耗也不推进阶梯
- HINT_MIN_FAILURES 3 → 1:门槛的活由阶梯接走了,第一次失败只开放 L0,而 L0 只反问、
  什么都不泄露,拦着它没有意义
- canEscalate 由后端算好在 done 事件里给,前端不自己推阶梯

输出后过滤(services/hint-filter.ts)
- 「不要给代码」写在 prompt 里只是软约束,模型忍不住一次就把这一级的意义废掉了。
  所以整段生成、过滤通过才推给前端,逐字显示改由前端模拟 —— 边流式边过滤做不到,
  发现违规时内容已经在学生屏幕上了
- 判定只用客观、低误报的信号:代码块、过长的行内代码、整行不含中文的类代码行、
  和标准答案重合 3 行以上、L0 一句问句都没有
- 违规就重生成一次,只重一次,再不过发写死的兜底话术。重试措辞按档分叉:编译档本来
  就允许给片段,对它说「不要出现任何代码」等于用阶梯的标准把这一档也砍了
- 两次都留痕(filter_attempt / filter_blocked / filter_reason),7.5 的输出过滤触发率
  就是从这三列出来的

services/ai.ts 加 streamWhole:事件形状和 streamChat 一样,前端不分叉。produce 期间
每 15 秒发一行 SSE 注释当心跳 —— 这条流中间有一大段静默(诊断 20s + 生成 60s +
重生成 60s,最坏 140 秒),而 NPM / nginx 的 proxy_read_timeout 默认 60 秒,超了学生
看到「请求失败」,后端却还在烧第二次调用,那条提示照样落库、照样把等级推上去。

prompt 版本另开 3 / 4(阶梯上每一级都换了 system),编译档仍走 1 / 2 的单段式基线,
两批数据不混在一起。迁移 0021 给 ai_hint 加四列,都可空、不带默认值,已有的行留 null
表示「分级上线前」。

实跑
- 阶梯:在 dev 库上用真实行驱动 decideHintLevel。正常路径 S1→S2→S3 走出 L0→L1→L2,
  同级连点 more 不升,AC 之后回 L0,编译档给 L-1 且不推进阶梯。把旧锚点规则复刻出来
  跑同一组数据做对照:只拿最老那条提交反复 POST,旧规则 L0→L1→L2(零新提交),
  新规则钉死在 L0,正常路径两者行为一致
- 过滤:起假 AI 服务端走完整条链路。L1 摊平代码→重生成后合规(attempt 2 / 未拦),
  L0 两次都甩代码块→兜底话术(blocked,reason 两条相连),编译档抄标程→命中「和标准
  答案重合 3 行」且追加的是分叉后的措辞
- streamWhole:produce 拖 16.5 秒收到 1 条心跳;同一份流喂给前端 consumeJSONEventStream
  只解析出 delta + done(注释行被静默跳过,前端零改动);中途 cancel 断开后 produce
  跑完不抛
- api / web typecheck、check:routes、fmt 全过

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-21 19:03:21 -06:00
co-authored by Claude Opus 5
parent 8b4d8899f9
commit 954797a9a5
11 changed files with 4674 additions and 39 deletions
+6
View File
@@ -0,0 +1,6 @@
-- AI 提示的分级与输出过滤留痕(AI 时代 OJ 设计 2c),字段含义见 schema.ts 的 aiHint。
-- 四列都可空、不带默认值,加列只改目录不重写表;已有的行留 null,表示「分级上线前」。
ALTER TABLE "ai_hint" ADD COLUMN "level" integer;--> statement-breakpoint
ALTER TABLE "ai_hint" ADD COLUMN "filter_attempt" integer;--> statement-breakpoint
ALTER TABLE "ai_hint" ADD COLUMN "filter_blocked" boolean;--> statement-breakpoint
ALTER TABLE "ai_hint" ADD COLUMN "filter_reason" text;
File diff suppressed because it is too large Load Diff
+7
View File
@@ -148,6 +148,13 @@
"when": 1789906612433,
"tag": "0020_drop_unsupported_languages",
"breakpoints": true
},
{
"idx": 21,
"version": "7",
"when": 1789990062378,
"tag": "0021_ai_hint_level",
"breakpoints": true
}
]
}
+23
View File
@@ -1034,6 +1034,29 @@ export const aiHint = pgTable(
diagnosis: jsonb().$type<HintDiagnosis>(),
// 诊断失败的原因(超时、回的不是 JSON、校验不过)。这时第二段退回单段式的 prompt
diagnosisError: text("diagnosis_error"),
/**
* 这条提示给的是哪一级(契约的 `HINT_LEVELS`AI 时代 OJ 设计 2c)。
* -1 = 编译失败那一档,**不在阶梯上**;null = 2c 上线前那批不分级的提示。
* 查阶梯的 SQL 一律 `level >= 0`,两者都被摘掉。
*/
level: integer(),
/**
* 一共生成了几次(1 = 一次就过,2 = 重生成过)。**整段生成、过滤后才推**(边流式边
* 过滤做不到:发现违规时内容已经在学生屏幕上了),违规就重生成一次,只重一次。
*
* **不是「发出去的是第几次生成」** —— `filter_blocked` 为真时两次都作废了,发的是
* 兜底话术。7.5 的「输出过滤触发率」= `filter_attempt = 2` ÷ `filter_attempt` 非空。
*/
filterAttempt: integer("filter_attempt"),
/**
* 两次都违规,发的是写死的兜底话术(content 是那句话,不是模型的输出)。
*
* **这种行的 `error` 仍是 null** —— 生成本身没失败,是内容被拦了。所以算「生成失败率」
* 看 `error`,算「学生有没有真拿到提示」要另外扣掉 `filter_blocked` 为真的这批。
*/
filterBlocked: boolean("filter_blocked"),
// 被拦下的原因,两次都被拦时是两条(用 ; 连)。null = 一次都没触发过过滤
filterReason: text("filter_reason"),
// 学生的评价:null = 没评
helpful: boolean(),
feedbackTime: timestamp("feedback_time", {
+43 -8
View File
@@ -6,6 +6,7 @@ import {
classPkAnalysisRequestSchema,
HINT_MIN_FAILURES,
type AiAnalysisRecord,
type AiHintDone,
type AiDetail,
type DurationData,
type HintDiagnosis,
@@ -36,8 +37,14 @@ import { config } from "../config"
import { db, schema } from "../db"
import { JudgeStatus, type JudgeStatusValue } from "../judge/status"
import { failure, success } from "../http"
import { completeChat, streamChat } from "../services/ai"
import { hintDiagnosis, hintPrompt } from "../services/hint-diagnosis"
import { completeChat, streamChat, streamWhole } from "../services/ai"
import { generateFilteredHint } from "../services/hint-filter"
import { decideHintLevel } from "../services/hint-level"
import {
hintDiagnosis,
hintPrompt,
referenceAnswer,
} from "../services/hint-diagnosis"
import { consumeToken } from "../services/throttling"
import {
calendarDay,
@@ -942,9 +949,11 @@ async function recordHint(
promptVersion: number
diagnosis: HintDiagnosis | null
diagnosisError: string | null
level: number
},
content: string,
error: string | null,
filter?: { attempt: number; blocked: boolean; reason: string | null },
) {
try {
const [row] = await db
@@ -958,6 +967,11 @@ async function recordHint(
durationMs: Math.round(performance.now() - base.startedAt),
diagnosis: base.diagnosis,
diagnosisError: base.diagnosisError,
level: base.level,
// 生成就失败的那条没走到过滤,三列都留 null(分母里不该有它)
filterAttempt: filter?.attempt ?? null,
filterBlocked: filter?.blocked ?? null,
filterReason: filter?.reason ?? null,
createTime: new Date().toISOString(),
})
.returning({ id: schema.aiHint.id })
@@ -1020,28 +1034,49 @@ aiRoutes.post("/ai/hint", requireAuth, async (c) => {
}
const limited = await throttleAi(c)
if (limited) return limited
// 这次按第几级生成。等级记在「学生 × 题目」上,怎么算出来的见 services/hint-level.ts
const { level, canEscalate } = await decideHintLevel(
c.get("user")!.id,
row.submission,
parsed.data.more === true,
)
// 标准答案**只进诊断那一段**、出参只有枚举和行号;生成提示这一段看不到它。
// 为什么这么拆、诊断怎么退回单段式,见 services/hint-diagnosis.ts 的文件头
const startedAt = performance.now()
const { diagnosis, error: diagnosisError } = await hintDiagnosis(row)
const { system, prompt, version } = hintPrompt(row, diagnosis)
const { system, prompt, version } = hintPrompt(row, diagnosis, level)
const base = {
submissionId: row.submission.id,
startedAt,
promptVersion: version,
diagnosis,
diagnosisError,
level,
}
return streamChat(system, prompt, {
onComplete: async (content) => {
const id = await recordHint(base, content, null)
// 不是 streamChat:提示要整段生成、过滤通过才推给学生(设计 2.6),
// 边流式边过滤做不到 —— 发现违规时内容已经在屏幕上了
return streamWhole(
async () => {
const filtered = await generateFilteredHint({
system,
prompt,
level,
// 标程只用来核「有没有把它抄出来」,不进任何 prompt
referenceCode: referenceAnswer(row)?.code ?? null,
})
// 落库失败就不带 id:前端据此不出评价按钮,提示本身照常显示
return id === null ? undefined : { hintId: id }
const hintId = await recordHint(base, filtered.content, null, filtered)
return {
content: filtered.content,
extra: { hintId, level, canEscalate } satisfies AiHintDone,
}
},
{
onError: async (message) => {
await recordHint(base, "", message)
},
})
},
)
})
aiRoutes.post("/ai/hint/:id/feedback", requireAuth, async (c) => {
+70
View File
@@ -175,3 +175,73 @@ export function streamChat(
},
})
}
/**
* AI OJ 2.6
*
* AI `streamChat`****
* `produce`
* 稿** delta**
*
* `streamChat` start / delta / done / error + `event: end`
* `consumeJSONEventStream` `event: start` `produce`
*
*
* **`produce` ** `streamChat`
* 20s + 60s + 60s 140 NPM / nginx
* `proxy_read_timeout` 60
*
* `HEARTBEAT_MS` SSE `utils/stream.ts` `event:` / `data:`
*
*/
export function streamWhole(
produce: () => Promise<{ content: string; extra?: Record<string, unknown> }>,
hooks: { onError?: (message: string) => Promise<void> } = {},
) {
const encoder = new TextEncoder()
// 反代的读超时是 60s(见上),取它的四分之一,够抗一次抖动
const HEARTBEAT_MS = 15_000
let closed = false
const body = new ReadableStream<Uint8Array>({
async start(controller) {
// 学生关掉页面之后 enqueue 会抛,而这时 produce 还在跑(留痕要它跑完),
// 所以 send 自己吞掉异常并记下「已经断了」,后面几步不用各写一遍 try
const send = (value: string) => {
if (closed) return
try {
controller.enqueue(encoder.encode(value))
} catch {
closed = true
}
}
const heartbeat = setInterval(() => send(": ping\n\n"), HEARTBEAT_MS)
try {
send("event: start\n\n")
const { content, extra } = await produce()
send(`data: ${JSON.stringify({ type: "delta", content })}\n\n`)
send(`data: ${JSON.stringify({ ...extra, type: "done" })}\n\n`)
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
// 先留痕再回前端
await hooks.onError?.(message).catch((e) => {
console.error("streamWhole onError hook failed", e)
})
send(`data: ${JSON.stringify({ type: "error", message })}\n\n`)
} finally {
clearInterval(heartbeat)
send("event: end\n\n")
if (!closed) controller.close()
}
},
cancel() {
closed = true
},
})
return new Response(body, {
headers: {
"content-type": "text/event-stream; charset=utf-8",
"cache-control": "no-cache",
"x-accel-buffering": "no",
},
})
}
+82 -8
View File
@@ -3,6 +3,8 @@ import { resolve } from "node:path"
import {
HINT_ERROR_TAGS,
HINT_LEVEL_COMPILE,
HINT_LEVELS,
hintDiagnosisSchema,
type HintDiagnosis,
} from "@oj2/contract"
@@ -39,9 +41,14 @@ type HintRow = {
/**
* prompt ai_hint.prompt_version****
* 1 2026-09-19 线
*
* 1 / 2 1
* system 2c 3 / 4
*/
export const HINT_PROMPT_SINGLE = 1
export const HINT_PROMPT_DIAGNOSED = 2
export const HINT_PROMPT_LEVELED = 3
export const HINT_PROMPT_LEVELED_DIAGNOSED = 4
/** 诊断这一段让学生干等着(提示还没开始流),超时就退回单段式,别让按钮一直转 */
const DIAGNOSE_TIMEOUT_MS = 20_000
@@ -64,7 +71,7 @@ function numbered(code: string) {
}
/** 同语言的标准答案优先;没有就拿别的语言的(思路一样,照样能帮诊断);再没有就 null */
function referenceAnswer(row: HintRow) {
export function referenceAnswer(row: HintRow) {
const answers = Array.isArray(row.problem.answers)
? row.problem.answers.map((item) => objectValue(item))
: []
@@ -205,18 +212,56 @@ export async function hintDiagnosis(row: HintRow): Promise<{
: { diagnosis: null, error: result.error }
}
/** 第二段(生成提示)的 prompt。**这里永远不放标准答案和测试点原文**,理由见文件头 */
export function hintPrompt(row: HintRow, diagnosis: HintDiagnosis | null) {
if (!diagnosis) {
// 单段式,2026-09-19 起的基线,一个字都别改(要改就换版本号,见上)
const prompt = `题目:${row.problem.title}\n描述:${row.problem.description.slice(0, 2000)}\n语言:${row.submission.language}\n结果:${judgeStatusName(row.submission.result)}\n错误:${errInfo(row)}\n代码:${row.submission.code.slice(0, 2000)}`
return { system: SINGLE_SYSTEM, prompt, version: HINT_PROMPT_SINGLE }
/**
* AI OJ 2.2 **
* prompt ** prompt `hint-filter.ts`
*/
const LEVEL_COMMON = `你是编程助教,面对的是刚开始学编程的中职学生。用中文、Markdown,语气平和,不要说教。
`
const LEVEL_RULES: Record<number, string> = {
0: `只能用提问引导学生自己想,一个结论都不能给:
- 23
-
- 4 `,
1: `只能告诉学生问题出在哪一块,不能说为什么错,更不能说怎么改:
- / /
-
- 3 `,
2: `把这里涉及的概念讲清楚,但不落到这份代码该怎么改:
-
- X
- 6 `,
}
function levelSystem(level: number) {
const entry = HINT_LEVELS.find((item) => item.level === level)
const head = entry
? `现在是 L${entry.level}${entry.name}):${entry.summary}`
: ""
return `${LEVEL_COMMON}\n${head}\n${LEVEL_RULES[level] ?? LEVEL_RULES[0]!}`
}
/** 诊断结果在 prompt 里的那一句;没诊断就是空串 */
function locatedLine(diagnosis: HintDiagnosis) {
const where = diagnosis.lines
? diagnosis.lines[0] === diagnosis.lines[1]
? `,大约在第 ${diagnosis.lines[0]}`
: `,大约在第 ${diagnosis.lines[0]}${diagnosis.lines[1]}`
: ""
return `问题定位:${HINT_ERROR_TAGS[diagnosis.tag]}${where}(把握:${diagnosis.confidence === "high" ? "高" : "低"}`
}
/**
* prompt****沿 2026-09-19 线
*
*/
function compilePrompt(row: HintRow, diagnosis: HintDiagnosis | null) {
if (!diagnosis) {
const prompt = `题目:${row.problem.title}\n描述:${row.problem.description.slice(0, 2000)}\n语言:${row.submission.language}\n结果:${judgeStatusName(row.submission.result)}\n错误:${errInfo(row)}\n代码:${row.submission.code.slice(0, 2000)}`
return { system: SINGLE_SYSTEM, prompt, version: HINT_PROMPT_SINGLE }
}
const system = `${SINGLE_SYSTEM}\n问题已经定位好了,会在「问题定位」里给出,围绕它来提示。把握低时换个方式问学生,别说得太肯定。不要提到「诊断」「定位」这些说法。`
const prompt = [
`题目:${row.problem.title}`,
@@ -224,8 +269,37 @@ export function hintPrompt(row: HintRow, diagnosis: HintDiagnosis | null) {
`语言:${row.submission.language}`,
`结果:${judgeStatusName(row.submission.result)}`,
`错误:${errInfo(row)}`,
`问题定位:${HINT_ERROR_TAGS[diagnosis.tag]}${where}(把握:${diagnosis.confidence === "high" ? "高" : "低"}`,
locatedLine(diagnosis),
`代码:\n${numbered(row.submission.code.slice(0, 2000))}`,
].join("\n")
return { system, prompt, version: HINT_PROMPT_DIAGNOSED }
}
/**
* prompt****
* `level` `compilePrompt`
*/
export function hintPrompt(
row: HintRow,
diagnosis: HintDiagnosis | null,
level: number,
) {
if (level === HINT_LEVEL_COMPILE) return compilePrompt(row, diagnosis)
const system = diagnosis
? `${levelSystem(level)}\n问题已经定位好了,会在「问题定位」里给出,就围着它说。把握低时别说得太肯定。不要提到「诊断」「定位」这些说法。`
: levelSystem(level)
const prompt = [
`题目:${row.problem.title}`,
`描述:${row.problem.description.slice(0, 2000)}`,
`语言:${row.submission.language}`,
`结果:${judgeStatusName(row.submission.result)}`,
`错误:${errInfo(row)}`,
...(diagnosis ? [locatedLine(diagnosis)] : []),
`代码:\n${numbered(row.submission.code.slice(0, 2000))}`,
].join("\n")
return {
system,
prompt,
version: diagnosis ? HINT_PROMPT_LEVELED_DIAGNOSED : HINT_PROMPT_LEVELED,
}
}
+168
View File
@@ -0,0 +1,168 @@
import { HINT_LEVEL_COMPILE } from "@oj2/contract"
import { completeChat } from "./ai"
/**
* ****AI OJ 2.5 / 2.6
*
* prompt
*
* ****
*
* ****
* `ai_hint.filter_attempt` / `filter_blocked` / `filter_reason`
*
*
* ****
*
*
* - ```)和过长的行内代码:阶梯上的每一级都不许出现代码。
* - ****
* `;` `=` `(`
* -
* - L0 L0
*
* `HINT_LEVEL_COMPILE`
* HINT_LEVEL_COMPILE
*/
/** 行内代码超过这个长度就当代码片段,短的(`int`、`n`、`%d`、`a[i]`)是正常讲解 */
const INLINE_CODE_MAX = 24
/** 和标准答案重合到这么多行就算抄答案 */
const ANSWER_LINE_HITS = 3
/** 参与重合比对的行至少这么长,短行(`}`、`return 0;`)谁写都一样 */
const ANSWER_LINE_MIN = 12
const CJK = /[一-龥]/
/** 兜底话术:两次都被拦时发它,`content` 存的就是这句,不是模型的输出 */
const FALLBACK: Record<number, string> = {
[HINT_LEVEL_COMPILE]:
"这次没能给出有效的提示。编译报错的第一行通常就写着出错的行号,先跳到那一行,再往上看一两行 —— 漏分号、括号不配对,报错点往往在真正出错的下一行。",
0: "这次没能给出有效的提示。先别急着改代码,问自己三个问题:这题的输入一共有几个数?每一步我想算的是什么?我的程序在哪种情况下会算得不对?",
1: "这次没能给出有效的提示。把你的代码分成「读入、计算、输出」三段,一段一段对着题目要求核一遍,先找出是哪一段没按题目说的做。",
2: "这次没能给出有效的提示。想一想这道题主要用到哪个知识点,把教程里对应的那一节再看一遍,然后带着它回来读自己的代码。",
}
function fallbackText(level: number) {
return FALLBACK[level] ?? FALLBACK[0]!
}
/** 标准答案里值得比对的行(去掉空白,短行不算) */
function answerLines(code: string) {
return code
.split("\n")
.map((line) => line.replace(/\s+/g, ""))
.filter((line) => line.length >= ANSWER_LINE_MIN)
}
/** 整行不含中文、又带着代码标点的行,多半是摊平成正文的代码 */
function looksLikeCode(line: string) {
const text = line.trim()
if (text.length < 6 || CJK.test(text)) return false
if (/^[-=*_#>|\s]+$/.test(text)) return false // Markdown 的分隔线、空列表项
return /[;=]|\w\s*\(/.test(text)
}
/**
* null
* `referenceCode`
*/
export function hintFilterReason(
content: string,
level: number,
referenceCode: string | null,
): string | null {
const text = content.trim()
if (!text) return "空回复"
if (level !== HINT_LEVEL_COMPILE) {
if (/```/.test(text)) return "出现代码块"
const inline = text.match(/`([^`\n]+)`/g) ?? []
if (inline.some((item) => item.length - 2 > INLINE_CODE_MAX))
return "行内代码过长"
// 围栏里的代码已经被上面拦掉了,这里找的是摊平进正文的
const bare = text.split("\n").filter(looksLikeCode)
if (bare.length) return `正文里出现代码:${bare[0]!.trim().slice(0, 60)}`
if (level === 0 && !/[?]/.test(text)) return "L0 没有一句问句"
}
if (referenceCode) {
const flat = text.replace(/\s+/g, "")
const hits = new Set(
answerLines(referenceCode).filter((line) => flat.includes(line)),
)
if (hits.size >= ANSWER_LINE_HITS) return `和标准答案重合 ${hits.size}`
}
return null
}
export interface FilteredHint {
/** 真正发给学生的正文;两次都被拦时是兜底话术 */
content: string
/**
* 1 = 2 = ****
* `blocked`
*/
attempt: number
/** 两次都违规,发的是兜底话术 —— 这条提示等于没给,只是没让学生空手而归 */
blocked: boolean
/** 触发过的违规原因,两次都触发时用 `; ` 连起来。null = 一次都没触发 */
reason: string | null
}
/**
* 线
*
* **** `HINT_LEVEL_COMPILE`
*
*
*/
function retrySystem(system: string, reason: string, level: number) {
const demand =
level === HINT_LEVEL_COMPILE
? "重写一遍,只讲怎么看报错、怎么定位到出错的那一行,不要把标准答案的内容搬进来。"
: "重写一遍,务必守住上面的限制:不要出现任何代码或代码块,不要把代码摊平写在正文里。"
return `${system}\n\n上一次的回答被判为违规(${reason}),已经作废。${demand}`
}
/**
*
*
* AI
* ****退
*/
export async function generateFilteredHint(options: {
system: string
prompt: string
level: number
referenceCode: string | null
}): Promise<FilteredHint> {
const { system, prompt, level, referenceCode } = options
const first = (await completeChat(system, prompt)).trim()
const firstReason = hintFilterReason(first, level, referenceCode)
if (!firstReason)
return { content: first, attempt: 1, blocked: false, reason: null }
let second: string
try {
second = (
await completeChat(retrySystem(system, firstReason, level), prompt)
).trim()
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return {
content: fallbackText(level),
attempt: 2,
blocked: true,
reason: `${firstReason}; 重生成失败:${message}`,
}
}
const secondReason = hintFilterReason(second, level, referenceCode)
if (!secondReason)
return { content: second, attempt: 2, blocked: false, reason: firstReason }
return {
content: fallbackText(level),
attempt: 2,
blocked: true,
reason: `${firstReason}; ${secondReason}`,
}
}
+136
View File
@@ -0,0 +1,136 @@
import { HINT_LEVEL_COMPILE, HINT_MAX_LEVEL } from "@oj2/contract"
import { and, desc, eq, gt, gte, isNull } from "drizzle-orm"
import { db, schema } from "../db"
import { JudgeStatus } from "../judge/status"
/**
* AI AI OJ 2.2 / 2.6
*
* ** × ** `ai_hint.level`
* = AC
*
*
*
*
* 1. ****`more`
* 2. ********
* `ai_hint.create_time`**** +1
* 3. **AC ** AC
*
* 2 ****
* id ****退
* more L0 L2
*
*
* `HINT_LEVEL_COMPILE` `level >= 0`
* 2c 线 `level` null `error`
*
*/
/** 最近一次 AC 的时刻;没 AC 过就是 null。AST 未通过不算,那种情况学生还要再改 */
async function lastAcceptedAt(userId: number, problemId: number) {
const [row] = await db
.select({ createTime: schema.submission.createTime })
.from(schema.submission)
.where(
and(
eq(schema.submission.userId, userId),
eq(schema.submission.problemId, problemId),
eq(schema.submission.result, JudgeStatus.ACCEPTED),
),
)
.orderBy(desc(schema.submission.createTime))
.limit(1)
return row?.createTime ?? null
}
/**
* ** `hintAt` **
* `hintAt` 2
*/
async function hasNewerSubmission(
userId: number,
problemId: number,
hintAt: string,
) {
const [row] = await db
.select({ id: schema.submission.id })
.from(schema.submission)
.where(
and(
eq(schema.submission.userId, userId),
eq(schema.submission.problemId, problemId),
gt(schema.submission.createTime, hintAt),
),
)
.limit(1)
return row !== undefined
}
/**
*
* window function
*/
async function currentLadder(userId: number, problemId: number) {
const since = await lastAcceptedAt(userId, problemId)
const rows = await db
.select({ level: schema.aiHint.level, hintAt: schema.aiHint.createTime })
.from(schema.aiHint)
.innerJoin(
schema.submission,
eq(schema.aiHint.submissionId, schema.submission.id),
)
.where(
and(
eq(schema.submission.userId, userId),
eq(schema.submission.problemId, problemId),
gte(schema.aiHint.level, 0),
isNull(schema.aiHint.error),
since ? gt(schema.submission.createTime, since) : undefined,
),
)
if (!rows.length) return null
const level = Math.max(...rows.map((row) => row.level ?? 0))
// 锚点取这一级**最早**那条:同一级重复给过几次时,锚点不能跟着往后挪,
// 否则学生每按一次「让 AI 分析」都得多交一次才升得上去
const anchor = rows
.filter((row) => row.level === level)
.map((row) => row.hintAt)
.sort()[0]!
return { level, anchor }
}
export interface HintLevelDecision {
/** 这次要按哪一级生成。`HINT_LEVEL_COMPILE` 不在阶梯上 */
level: number
/** 生成完之后,再点一次「再多一点提示」还升不升得动 —— 直接进 done 事件 */
canEscalate: boolean
}
/**
* `submission`
*
* `canEscalate` **** false
*
*
*/
export async function decideHintLevel(
userId: number,
submission: { problemId: number; result: number; createTime: string },
more: boolean,
): Promise<HintLevelDecision> {
// 编译失败自成一档:不看阶梯、不动阶梯,也就没有「再多一点」可点
if (submission.result === JudgeStatus.COMPILE_ERROR)
return { level: HINT_LEVEL_COMPILE, canEscalate: false }
const problemId = submission.problemId
const ladder = await currentLadder(userId, problemId)
// 这道题还没开过阶梯:从 L0 起,而这条提示就是 L0 的锚点,不可能已经有更新的提交
if (!ladder) return { level: 0, canEscalate: false }
const unlocked =
ladder.level < HINT_MAX_LEVEL &&
(await hasNewerSubmission(userId, problemId, ladder.anchor))
if (more && unlocked) return { level: ladder.level + 1, canEscalate: false }
return { level: ladder.level, canEscalate: unlocked }
}
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { Icon } from "@iconify/vue"
import { useThemeVars } from "naive-ui"
import { HINT_MIN_FAILURES } from "@oj2/contract"
import { HINT_MIN_FAILURES, hintLevelLabel } from "@oj2/contract"
import type { JudgeCaseResult } from "@oj2/contract"
import { JUDGE_STATUS, SubmissionStatus } from "utils/constants"
import {
@@ -28,15 +28,65 @@ const isDark = useDark()
const problemStore = useProblemStore()
const theme = useThemeVars()
// AI
// AI
// hintTarget hintContent ""
// 2c 2.6
//
const hintTarget = ref("")
const hintContent = ref("")
const hintLoading = ref(false)
const hintError = ref("")
// ai_hint id done
const hintId = ref<number | null>(null)
// -1 =
// done
const hintLevel = ref<number | null>(null)
const hintCanEscalate = ref(false)
const hintHelpful = ref<boolean | null>(null)
const hintFeedbackSending = ref(false)
// 24ms 3 125 /
// MdPreview Markdown
const TYPE_STEP = 3
const TYPE_INTERVAL = 24
let typingTimer: ReturnType<typeof setInterval> | null = null
const hintTyping = computed(
() => hintContent.value.length < hintTarget.value.length,
)
function stopTyping() {
if (typingTimer === null) return
clearInterval(typingTimer)
typingTimer = null
}
function startTyping() {
if (typingTimer !== null) return
typingTimer = setInterval(() => {
if (!hintTyping.value) {
stopTyping()
return
}
hintContent.value = hintTarget.value.slice(
0,
hintContent.value.length + TYPE_STEP,
)
}, TYPE_INTERVAL)
}
function resetHint() {
stopTyping()
hintTarget.value = ""
hintContent.value = ""
hintError.value = ""
hintId.value = null
hintLevel.value = null
hintCanEscalate.value = false
hintHelpful.value = null
}
onUnmounted(stopTyping)
//
const msg = computed(() => {
if (!props.submission) return ""
@@ -97,26 +147,22 @@ const showAIHint = computed(() => {
watch(
() => props.submission?.id,
() => {
hintContent.value = ""
hintError.value = ""
resetHint()
hintLoading.value = false
hintId.value = null
hintHelpful.value = null
},
)
async function fetchHint(submissionId: string) {
// more =
// done level
async function fetchHint(submissionId: string, more = false) {
hintLoading.value = true
hintContent.value = ""
hintError.value = ""
hintId.value = null
hintHelpful.value = null
resetHint()
try {
const response = await fetch("/api/ai/hint", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ submissionId }),
body: JSON.stringify({ submissionId, more }),
})
if (!response.ok) throw await aiStreamError(response)
@@ -126,12 +172,17 @@ async function fetchHint(submissionId: string) {
type: string
content?: string
message?: string
hintId?: number
hintId?: number | null
level?: number
canEscalate?: boolean
}) => {
if (data.type === "delta" && data.content) {
hintContent.value += data.content
hintTarget.value += data.content
startTyping()
} else if (data.type === "done") {
hintId.value = data.hintId ?? null
hintLevel.value = data.level ?? null
hintCanEscalate.value = data.canEscalate === true
} else if (data.type === "error") {
hintError.value = data.message || "AI 提示生成失败"
}
@@ -269,21 +320,42 @@ const columns: DataTableColumn<JudgeCaseResult>[] = [
class="mb-3"
/>
<n-button
v-if="!hintContent && !hintLoading"
v-if="!hintTarget && !hintLoading"
type="primary"
@click="fetchHint(submission.id)"
>
AI 分析我的代码
</n-button>
<n-spin v-else-if="hintLoading && !hintContent" size="small" />
<n-spin v-else-if="hintLoading && !hintTarget" size="small" />
<MdPreview
v-if="hintContent"
:model-value="hintContent"
preview-theme="vuepress"
:theme="isDark ? 'dark' : 'light'"
/>
<!-- 等级和再多一点提示按钮出不出由后端 done 里的 canEscalate
前端不自己推阶梯要再交一次才升得动规则在 services/hint-level.ts -->
<n-flex
v-if="hintId !== null && !hintLoading"
v-if="hintLevel !== null && !hintLoading && !hintTyping"
align="center"
size="small"
style="margin-top: 8px"
>
<n-tag size="small" :bordered="false">
{{ hintLevelLabel(hintLevel) }}
</n-tag>
<n-button
v-if="hintCanEscalate"
size="tiny"
type="primary"
ghost
@click="fetchHint(submission.id, true)"
>
再多一点提示
</n-button>
</n-flex>
<n-flex
v-if="hintId !== null && !hintLoading && !hintTyping"
align="center"
size="small"
style="margin-top: 8px"
+60 -3
View File
@@ -114,14 +114,70 @@ export const aiAnalysisRequestSchema = z.object({
/**
* AI
* POST /ai/hint 3
* POST /ai/hint
*
*
*
*
* **2c 3 1** 2.6
* L0 L0
* 3
*/
export const HINT_MIN_FAILURES = 3
export const HINT_MIN_FAILURES = 1
export const aiHintRequestSchema = z.object({ submissionId: z.string().min(1) })
/**
* AI AI OJ 2.2**`ai_hint.level`**
*
*
*
* 02 ****
* 2.6L3 / L4 2d
*/
export const HINT_LEVELS = [
{ level: 0, name: "反问", summary: "只反问,不给结论" },
{ level: 1, name: "定位", summary: "只说问题在哪,不说为什么" },
{ level: 2, name: "概念", summary: "讲清涉及的概念,不给改法" },
] as const
export const HINT_MAX_LEVEL = 2
/**
* **** SQL `level >= 0`
*
*
*
* HINT_MIN_FAILURES
*
* `ai_hint.level` null2c 线
*/
export const HINT_LEVEL_COMPILE = -1
/** 给界面看的等级名。编译失败那一档不叫 L-1 */
export function hintLevelLabel(level: number) {
if (level === HINT_LEVEL_COMPILE) return "编译错误"
const item = HINT_LEVELS.find((entry) => entry.level === level)
return item ? `L${item.level} ${item.name}` : `L${level}`
}
/**
* `more` = ****
* +1
*/
export const aiHintRequestSchema = z.object({
submissionId: z.string().min(1),
more: z.boolean().optional(),
})
/**
* `/ai/hint` `done` `hintId` null
* `canEscalate`
*
*/
export const aiHintDoneSchema = z.object({
hintId: z.number().int().nullable(),
level: z.number().int(),
canEscalate: z.boolean(),
})
/**
* AI **key `ai_hint.diagnosis.tag`
@@ -230,6 +286,7 @@ export type LoginSummary = z.infer<typeof loginSummarySchema>
export type AiAnalysisRequest = z.infer<typeof aiAnalysisRequestSchema>
export type AiHintRequest = z.infer<typeof aiHintRequestSchema>
export type AiHintDone = z.infer<typeof aiHintDoneSchema>
export type AiHintFeedbackRequest = z.infer<typeof aiHintFeedbackRequestSchema>
export type ClassAnalysisRequest = z.infer<typeof classAnalysisRequestSchema>
export type ClassPkAnalysisRequest = z.infer<