问数据一轮的「决定」下沉到后端 chat/turn.py,问数据页照后端给的走(细案 01 做法 1,总方案 2.1);这一轮的花费 = 助手 + 运行 + 复核

- chat/turn.py:next_step(有 error 级问题时自查过就判失败、没自查过的老服务端才重来一次;autorun=false 只建不跑;reply、只有反问直接收尾;其余运行)、fill_input(规则同 chat.ts 的 fillInput)、retry_after_review(retry、broken、这一轮还没试满)、retry_instruction(两种措辞原样搬过来)
- answer_ops 的 final(含只问不改那条)带 next(run | ready | fail);/copilot/review 的结果带 retry_instruction(ReviewIn.attempt 按这一轮试过几次判,要重来是那句话,不重来是 null)
- 前端 store/chat.ts:final 照 next 走,复核照 retry_instruction 走(原样交回给助手),launch / watch / settle 把 attempt 带到复核;老后端没有这两个字段时退回原来的规则(常量和措辞留在 retryInstruction 里,和后端逐字对照)
- 这一轮的用量:usage 操作累加进 copilot,复核结果的 usage 累加进 review,自动重来和用户点重试换下来的运行并进 earlier,当前运行的抄进 run,一起随 meta.usage 落库;问数据页轮次头部写这一轮的花费(runCostText:估不出的写「≥」或「金额无法估算」),悬停分得清助手建图、运行、复核,文案在 lib/terms.ts 的 TURN_COST_TEXT;ReviewResult 类型补 retry_instruction、usage
- 新增 tests/test_chat_turn_policy.py(各条决定的分支;前后端对照 MAX_ATTEMPTS、认问题字段的正则、两种重来措辞——改一个字回跑会失败;复核按 attempt 给不给 retry_instruction);test_copilot_answer_ops 的基线比对改成「final 只多一个 next」
- check-chat 补一段「新服务端」:照 next=ready 不发起、照 retry_instruction 原样重来、第二次复核带 attempt=1、meta.usage 四份都记下、头部写 $0.067 且悬停分得清三部分
This commit is contained in:
ThearchyHelios
2026-10-16 10:59:15 +08:00
parent ba28dee1f0
commit ee6e07959b
10 changed files with 502 additions and 30 deletions
+83 -1
View File
@@ -380,8 +380,10 @@ const TABLES = {
const log = {
patches: [], cancels: [], continues: [], runStarts: [], archived: [], posts: [], approvalGets: [], runGets: [],
deleted: [], gens: [], sourceGets: 0, catalogPatches: [],
deleted: [], gens: [], sourceGets: 0, catalogPatches: [], reviews: [],
}
/** 新服务端复核给的重来那句话:前端得原样交回去,不自己拼 */
const DECIDE_RETRY = '(服务端拼好的重来说法)照服务端的决定重新回答:有几家门店'
const ctl = {
slowMs: 0, failE: true, wsClosed: {}, approvals: [], thinkGo: false, writingGo: false, writingAt: 0,
/** 列表只给前 8 个:筛选框刚好出现,删一个就不够 8 个了 */
@@ -495,6 +497,22 @@ async function fakeApi(route) {
return route.fulfill({ status: 200, contentType: 'text/event-stream',
body: ops.map((o) => `data: ${JSON.stringify(o)}\n\n`).join('') })
}
if (String(b.instruction ?? '').includes('照服务端的决定') || String(b.instruction ?? '').includes(DECIDE_RETRY)) {
// 新服务端:final 带 next(backend/app/chat/turn.py),最后一条是这一轮建图的用量(answer_ops 的 usage)。
// 「只建不跑」那一问:autorun 没写 false,next 却是 ready——前端得照 next 走
const ready = String(b.instruction ?? '').includes('只建不跑')
const ops = [
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 300 },
...GRAPH.nodes.map((n) => ({ op: 'add_node', node: n })),
{ op: 'done', explanation: '一张三步的图' },
{ op: 'check', status: 'passed', repaired: 0, warnings: [] },
{ op: 'final', graph: GRAPH, issues: [], explanation: '一张三步的图', autorun: true, next: ready ? 'ready' : 'run' },
{ op: 'usage', by_model: { m: { input_tokens: 4000, output_tokens: 300, calls: 1, cost_usd: 0.012, unpriced_calls: 0 } },
input_tokens: 4000, output_tokens: 300, calls: 1, cost_usd: 0.012, unpriced_calls: 0 },
]
return route.fulfill({ status: 200, contentType: 'text/event-stream',
body: ops.map((o) => `data: ${JSON.stringify(o)}\n\n`).join('') })
}
if (String(b.instruction ?? '').includes('先问清楚')) {
// 助手要先问清楚才能动手(服务端 copilot_gen 的 ask):问题转出去,这一轮到此为止——没有 final,也没有 reply
const ops = [
@@ -553,6 +571,15 @@ async function fakeApi(route) {
if (path === '/copilot/review' && method === 'POST') {
const wait = ctl.reviewDelay[body().run_id]
if (wait) await new Promise((r) => setTimeout(r, wait))
if (String(body().run_id).startsWith('run-decide')) {
// 新服务端:第一次判不可用、给出重来的那句话;重来之后的那次没问题、不再重来。各带复核的用量
log.reviews.push(body())
const first = body().attempt === 0
return json({ verdict: first ? 'annotated' : 'ok', note: first ? '查到一半就停了' : '', answer: null, retry: first,
severity: first ? 'broken' : '', signals: first ? [{ kind: 'step_limit', detail: '用满了步数', severity: 'broken' }] : [],
retry_instruction: first ? DECIDE_RETRY : null,
usage: { cost_usd: first ? 0.003 : 0, unpriced_calls: 0, calls: first ? 1 : 0 } })
}
// 老后端的复核不认 _evidence,照样回一次改写
if (body().run_id === 'run-evid' || body().run_id === 'run-evq') {
return json({ verdict: 'rewritten', note: '检索降级过,结论请对照原始数据', answer: '改写后的答案:销售额大约四万多',
@@ -584,6 +611,7 @@ async function fakeApi(route) {
: conversation.includes('正在写') ? 'run-writing'
: conversation.includes('强杀') ? 'run-killed'
: conversation.includes('步数用满') ? `run-steps-${++runSeq}`
: conversation.includes('照服务端的决定') ? `run-decide-${++runSeq}`
: `run-busy-${++runSeq}`
log.runStarts.push({ id, graph: b.graph })
return json({ id, status: 'queued', run_class: 'exploratory', output: {}, usage: {} })
@@ -605,6 +633,10 @@ async function fakeApi(route) {
if (id.startsWith('run-404')) return json({ detail: '运行不存在' }, 404)
if (RUNS[id]) return json({ id, run_class: 'exploratory', usage: {}, ...RUNS[id] })
if (id.startsWith('run-c') || id === 'run-b1') return json({ id, status: 'succeeded', run_class: 'exploratory', output: {}, usage: {} })
if (id.startsWith('run-decide')) {
return json({ id, status: 'succeeded', run_class: 'exploratory', output: { answer: '三家门店。' },
usage: { cost_usd: 0.02, calls: 2, input_tokens: 6000, output_tokens: 400 } })
}
return json({ id, status: 'interrupted', run_class: 'exploratory', output: {}, usage: {} })
}
if (path.startsWith('/approvals')) {
@@ -773,6 +805,17 @@ function script(runId, after = 0) {
end: null,
}
}
if (runId.startsWith('run-decide')) {
return {
events: [
ev(1, 'run.started', null, { nodes: 3 }),
ev(2, 'node.started', 'ag', {}),
ev(3, 'node.finished', 'ag', { duration_ms: 900 }),
ev(4, 'run.finished', null, { output: { answer: '三家门店。' } }),
],
end: 'succeeded',
}
}
if (runId.startsWith('run-steps')) {
return {
events: [
@@ -1546,6 +1589,45 @@ for (const theme of THEMES) {
await ctx.close()
})
await section('decide', '新服务端:照 final.next 和复核的 retry_instruction 走,这一轮的花费 = 助手 + 运行 + 复核', async () => {
const { page, ctx, errors } = await open(theme)
await goto(page, 'c0emptg')
const gens = log.gens.length
const runs = log.runStarts.length
const reviews = log.reviews.length
await send(page, '照服务端的决定:有几家门店')
const done = await page.waitForFunction(() => {
const t = window.__chat.getState().byConversation.c0emptg?.at(-1)
return t?.phase === 'done' && t?.run?.id?.startsWith('run-decide')
}, null, { timeout: 8000 }).then(() => true, () => false)
await page.waitForTimeout(500)
const turn = (await chatState(page)).byConversation.c0emptg.at(-1)
check('复核判不可用:带着原图重来一次', done && log.gens.length - gens === 2 && log.runStarts.length - runs === 2,
`${log.gens.length - gens} 次生成、${log.runStarts.length - runs} 次运行`)
check('重来时交回去的就是服务端给的那句话', log.gens.at(-1)?.instruction === DECIDE_RETRY, log.gens.at(-1)?.instruction)
check('第二次复核带着这一轮试过的次数', log.reviews.slice(reviews).map((r) => r.attempt).join(',') === '0,1',
JSON.stringify(log.reviews.slice(reviews)))
const last = log.patches.filter((x) => x.body?.meta?.usage).at(-1)?.body?.meta?.usage
check('meta.usage 记下助手、复核、换下来的运行和这一次运行', !!last && Math.abs(last.copilot?.cost_usd - 0.024) < 1e-9
&& Math.abs(last.review?.cost_usd - 0.003) < 1e-9 && Math.abs(last.earlier?.cost_usd - 0.02) < 1e-9
&& Math.abs(last.run?.cost_usd - 0.02) < 1e-9, JSON.stringify(last))
const tag = page.locator(`[data-turn="${turn.id}"] [data-turn-cost]`)
const tagText = await tag.innerText().catch(() => '')
const title = await tag.getAttribute('title').catch(() => '')
check('头部写这一轮的花费,悬停分得清助手、运行、复核', tagText.includes('$0.067') && (title ?? '').includes('助手建图')
&& (title ?? '').includes('复核'), `${tagText} | ${title}`)
// 只建不跑:autorun 没写 false,next 是 ready,前端照 next 走、不发起运行
const before = log.runStarts.length
await send(page, '照服务端的决定,只建不跑')
const ready = await page.waitForFunction(() => window.__chat.getState().byConversation.c0emptg?.at(-1)?.phase === 'ready',
null, { timeout: 6000 }).then(() => true, () => false)
await page.waitForTimeout(300)
check('next=ready:图摆着等运行,不发起', ready && log.runStarts.length === before, `${log.runStarts.length - before} 次运行`)
check('没有运行时报错', errors.length === 0, errors[0] ?? '')
await ctx.close()
})
await section('makeapp', '做成应用:带着这次运行进蓝图,不再跳画布发布', async () => {
const { page, ctx, errors } = await open(theme)
const made = []