mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 15:12:33 +00:00
问数据一轮的「决定」下沉到后端 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:
@@ -880,7 +880,8 @@ export const api = {
|
||||
setModel: (body: { provider?: string | null; model?: string | null }) =>
|
||||
put<CopilotModel>('/copilot/model', body),
|
||||
/** 跑完之后复核一次。干净的运行后端直接返回 verdict='ok',不调模型 */
|
||||
review: (body: { run_id: string; question: string }) =>
|
||||
/** attempt:这一轮已经自动试了几次(从 0 数),服务端据此决定给不给 retry_instruction。老服务端不认它,忽略 */
|
||||
review: (body: { run_id: string; question: string; attempt?: number }) =>
|
||||
post<ReviewResult>('/copilot/review', body),
|
||||
/**
|
||||
* 一键升级为可追溯结构:先按确定的规则改写(llm 换成报告撰写、问数据的图插入报告节点……),
|
||||
|
||||
@@ -759,6 +759,21 @@ export function runCostText(usd: number | null | undefined, unpriced: number, ap
|
||||
return `${(approx || unpriced > 0) && usd > 0 ? '≥ ' : ''}${formatCost(usd)}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 问数据一轮的花费:助手建图(含挑表、接着写、自查修正)+ 运行 + 复核(next-round 细案 01 做法 3)。以前只算运行那份,
|
||||
* 这一轮的花费是低报的
|
||||
*/
|
||||
export const TURN_COST_TEXT = {
|
||||
copilot: '助手建图',
|
||||
run: '运行',
|
||||
review: '复核',
|
||||
/** 悬停说明的第一行,parts 是「助手建图 $0.012 · 运行 $0.03 · 复核 $0」 */
|
||||
title: (parts: string) => `这一轮的花费:${parts}`,
|
||||
/** n 次调用的金额估不出来:金额只是下限 */
|
||||
unpriced: (n: number) =>
|
||||
`其中 ${formatNumber(n)} 次调用的金额无法估算(模型不在价格目录中,或用量没有报全),未计入,实际花费不低于此数`,
|
||||
} as const
|
||||
|
||||
export const JUDGE_SETTING_TEXT = {
|
||||
label: '证据裁判',
|
||||
hint: '报告中的结论句(如「增长主要来自新客首单」)由另一个模型按证据逐句判断:正式运行由报告撰写节点当场裁判,探索运行在点击时逐句裁判。判断由模型给出,不是系统核对,界面上一律标注「非确定」。',
|
||||
|
||||
@@ -17,14 +17,15 @@ import { humanizeError } from '../lib/errors'
|
||||
import { explainRunError } from '../lib/explain'
|
||||
import { canLeave, leavePass } from '../lib/leave'
|
||||
import { formatNumber, formatTime } from '../lib/format'
|
||||
import { CHAT_BLUEPRINT_TEXT } from '../lib/terms'
|
||||
import { CHAT_BLUEPRINT_TEXT, TURN_COST_TEXT, runCostText } from '../lib/terms'
|
||||
import { useCatalog, useCatalogListState, useDatasources, useOnReconnect, type CatalogListState } from '../store/catalog'
|
||||
import { useAtLeast } from '../store/auth'
|
||||
import { useBlueprint } from '../store/blueprint'
|
||||
import type { BlueprintScope } from '../typesBlueprint'
|
||||
import { blueprintPath, useCreateScope } from './blueprints/shared'
|
||||
import {
|
||||
agentSteps, busyTurn, isBusy, stepsVisible, useChat, type Attempt, type ChatTurn, type Failure, type ScopeSource,
|
||||
agentSteps, busyTurn, isBusy, stepsVisible, turnCost, usageOfTurn, useChat, type Attempt, type ChatTurn, type Failure,
|
||||
type ScopeSource,
|
||||
} from '../store/chat'
|
||||
import { lastVisited, useConversations } from '../store/conversations'
|
||||
import {
|
||||
@@ -422,11 +423,34 @@ function toStreamTurn(turn: ChatTurn): StreamTurn {
|
||||
// 可能先到一两条事件,流会拿第一步的起点顶上,计时器从提问那天跑起
|
||||
startedAt: live ? turn.startedAt : null,
|
||||
elapsedMs: !live && turn.endedAt ? turn.endedAt - turn.startedAt : turn.meta?.ms ?? undefined,
|
||||
cost: costOf(turn),
|
||||
}
|
||||
streamCache.set(turn, out)
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* 这一轮的花费(助手建图 + 运行 + 复核,store 的 usageOfTurn):没花钱、也没有估不出的(演示模型、老轮次)不写。
|
||||
* 悬停写各部分,估不出的照实说金额只是下限
|
||||
*/
|
||||
function costOf(turn: ChatTurn): StreamTurn['cost'] {
|
||||
const usage = usageOfTurn(turn)
|
||||
const total = turnCost(usage)
|
||||
if (!usage || !total || (!total.usd && !total.unpriced)) return undefined
|
||||
const text = runCostText(total.usd, total.unpriced)
|
||||
if (!text) return undefined
|
||||
const part = (label: string, u: { cost_usd?: number | null; unpriced_calls?: number } | undefined) =>
|
||||
`${label} ${runCostText(u?.cost_usd ?? 0, u?.unpriced_calls ?? 0) ?? '—'}`
|
||||
const run = turnCost({ earlier: usage.earlier, run: usage.run })
|
||||
const parts = [
|
||||
part(TURN_COST_TEXT.copilot, usage.copilot),
|
||||
part(TURN_COST_TEXT.run, run ? { cost_usd: run.usd, unpriced_calls: run.unpriced } : undefined),
|
||||
part(TURN_COST_TEXT.review, usage.review),
|
||||
].join(' · ')
|
||||
return { text, title: [TURN_COST_TEXT.title(parts), total.unpriced ? TURN_COST_TEXT.unpriced(total.unpriced) : '']
|
||||
.filter(Boolean).join('\n') }
|
||||
}
|
||||
|
||||
/**
|
||||
* 交给流里报错块的那份:store 已经拆好的标题、原因、怎么办,照原样画。运行的失败在
|
||||
* store 里就按 lib/explain 讲过了(和记录页同一套说法),再把原话交过去会被翻第二遍;
|
||||
|
||||
@@ -119,6 +119,8 @@ export interface StreamTurn {
|
||||
startedAt?: number | null
|
||||
/** 跑完的轮次花了多久(毫秒)。不给就从「完成」那一行推 */
|
||||
elapsedMs?: number
|
||||
/** 这一轮的花费(问数据页给:助手建图 + 运行 + 复核)。text 进头部小标签,title 是悬停时各部分的明细 */
|
||||
cost?: { text: string; title: string }
|
||||
/**
|
||||
* 头部要不要提醒。phase 只分得出跑没跑完;「图放上去了,但还有 2 处问题」
|
||||
* 也是 done,头部却不该是一个安静的绿勾
|
||||
@@ -613,6 +615,7 @@ function TurnHead({ turn }: { turn: StreamTurn }) {
|
||||
progress.total ? `${progress.done}/${progress.total} 节点` : '',
|
||||
!live && queries ? `${queries} 次查询` : '',
|
||||
!live ? took : '',
|
||||
!live && turn.cost ? turn.cost.text : '',
|
||||
].filter(Boolean)
|
||||
const fraction = progress.total ? Math.min(1, progress.done / progress.total) : undefined
|
||||
// 节点轨:图不大时一个节点一格,按状态着色——跑到哪、哪一步跳过了、卡在哪一眼看全。
|
||||
@@ -636,7 +639,8 @@ function TurnHead({ turn }: { turn: StreamTurn }) {
|
||||
{verdict && <span data-verdict="" style={{ color: verdictTone }}> · {verdict}</span>}
|
||||
</span>
|
||||
{tags.length > 0 && (
|
||||
<span className="tnum min-w-0 shrink truncate text-2xs text-dim">{tags.join(' · ')}</span>
|
||||
<span className="tnum min-w-0 shrink truncate text-2xs text-dim" title={!live ? turn.cost?.title : undefined}
|
||||
data-turn-cost={!live && turn.cost ? '' : undefined}>{tags.join(' · ')}</span>
|
||||
)}
|
||||
{tier && (
|
||||
// 出具档位也挂在吸顶的头上:往下翻到报告中段时,"这份结论能不能当真"还看得见。
|
||||
|
||||
+142
-22
@@ -154,6 +154,32 @@ export interface TurnMeta {
|
||||
* 有多久没动静:后端按 updated_at 算,而重试一轮昨天的问题时 created_at 是昨天的
|
||||
*/
|
||||
at?: number
|
||||
/** 这一轮的花费:助手建图、运行、复核各一份(next-round 细案 01 做法 3),见 TurnUsage */
|
||||
usage?: TurnUsage
|
||||
}
|
||||
|
||||
/**
|
||||
* 一份用量:服务端 providers/usage.py 的汇总(助手建图的 usage 操作、复核结果的 usage),或者运行的 usage。cost_usd 只是
|
||||
* 估得出的那部分;unpriced_calls 次估不出(模型不在价格目录里,或者用量没报全),这时金额前写「≥」
|
||||
*/
|
||||
export interface UsageTotal {
|
||||
cost_usd?: number | null
|
||||
unpriced_calls?: number
|
||||
calls?: number
|
||||
input_tokens?: number
|
||||
output_tokens?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 这一轮的花费 = 助手建图 + 运行 + 复核(以前只有运行那份,问数据的花费是低报的)。自动重来、用户点的重试都还是这一轮:
|
||||
* 前几次建图、复核累加在 copilot / review 里,被换掉的运行累加在 earlier 里;run 是当前这次运行(落库时从 run.usage 抄一份,
|
||||
* 恢复的历史轮次不用回源就有数)
|
||||
*/
|
||||
export interface TurnUsage {
|
||||
copilot?: UsageTotal
|
||||
review?: UsageTotal
|
||||
earlier?: UsageTotal
|
||||
run?: UsageTotal
|
||||
}
|
||||
|
||||
export interface ChatTurn {
|
||||
@@ -227,6 +253,8 @@ export interface ChatTurn {
|
||||
writing?: { chars: number; thought: string } | null
|
||||
/** 这一问只查这几个数据源。重试、重新问都沿用它 */
|
||||
scope?: ScopeSource[]
|
||||
/** 这一轮到目前为止的助手建图、复核、被换掉的运行的用量(当前这次运行的在 run.usage),见 TurnUsage */
|
||||
usage?: TurnUsage
|
||||
/**
|
||||
* 助手问回来的那句话(N11):这一轮到此结束,问题作为这一轮的内容显示、进历史,输入框提示回答它;用户的回答就是下一轮。
|
||||
* 以前这里没有分支:只问不改的一轮流结束时既没有 final 也没有 reply,这一轮一直停在「正在理解需求」
|
||||
@@ -449,10 +477,58 @@ function buildMeta(t: ChatTurn): TurnMeta {
|
||||
asked: t.asked ? true : undefined,
|
||||
evidence: hasEvidence(t.output) || t.meta?.evidence || undefined,
|
||||
looks: lookList(t),
|
||||
usage: usageOfTurn(t),
|
||||
at: Date.now(),
|
||||
}
|
||||
}
|
||||
|
||||
const USAGE_KEYS = ['cost_usd', 'unpriced_calls', 'calls', 'input_tokens', 'output_tokens'] as const
|
||||
|
||||
/** 两份用量相加(缺的当 0)。b 不是对象时原样交回 a */
|
||||
export function addUsage(a: UsageTotal | undefined, b: UsageTotal | null | undefined): UsageTotal | undefined {
|
||||
if (!b || typeof b !== 'object') return a
|
||||
const out: UsageTotal = { ...(a ?? {}) }
|
||||
for (const k of USAGE_KEYS) {
|
||||
const v = Number(b[k] ?? 0)
|
||||
if (Number.isFinite(v) && v) out[k] = Number(out[k] ?? 0) + v
|
||||
}
|
||||
if (out.cost_usd != null) out.cost_usd = Math.round(out.cost_usd * 1e6) / 1e6
|
||||
return out
|
||||
}
|
||||
|
||||
/** 运行的 usage(形状同 UsageTotal,可能带别的键)→ 只留要记的几项 */
|
||||
function usageTotal(u: Record<string, any> | null | undefined): UsageTotal | undefined {
|
||||
return u && typeof u === 'object' ? addUsage({}, u) : undefined
|
||||
}
|
||||
|
||||
/** 这一轮现在的用量:内存里累加的那几份,加上当前这次运行的(运行还没回来时沿用落库的那份) */
|
||||
export function usageOfTurn(t: ChatTurn): TurnUsage | undefined {
|
||||
const base = t.usage ?? t.meta?.usage
|
||||
const run = usageTotal(t.run?.usage) ?? base?.run
|
||||
const out: TurnUsage = { ...(base ?? {}), ...(run ? { run } : {}) }
|
||||
return Object.keys(out).length ? out : undefined
|
||||
}
|
||||
|
||||
/** 当前这次运行换下来了(自动重来、用户点重试):它的用量并进 earlier,这一轮的花费不丢这一笔 */
|
||||
function retireRun(t: ChatTurn): TurnUsage | undefined {
|
||||
const u = usageOfTurn(t)
|
||||
if (!u?.run) return u
|
||||
const { run, ...rest } = u
|
||||
return { ...rest, earlier: addUsage(rest.earlier, run) }
|
||||
}
|
||||
|
||||
/**
|
||||
* 这一轮的花费:助手 + 运行 + 复核。一份都没有(老轮次、还没开始)是 null。unpriced 是估不出金额的调用次数:
|
||||
* 有的话金额只是下限,界面写「≥」(lib/terms 的 runCostText)
|
||||
*/
|
||||
export function turnCost(u: TurnUsage | undefined): { usd: number; unpriced: number; calls: number } | null {
|
||||
if (!u) return null
|
||||
const parts = [u.copilot, u.review, u.earlier, u.run].filter(Boolean) as UsageTotal[]
|
||||
if (!parts.length) return null
|
||||
const sum = parts.reduce<UsageTotal>((acc, p) => addUsage(acc, p) ?? acc, {})
|
||||
return { usd: Number(sum.cost_usd ?? 0), unpriced: Number(sum.unpriced_calls ?? 0), calls: Number(sum.calls ?? 0) }
|
||||
}
|
||||
|
||||
/** 这一轮的查看记录:操作流里有就取操作流的(重试会重新查看),恢复的历史轮次没有操作流,沿用 meta 里的 */
|
||||
function lookList(t: ChatTurn): LookEvent[] | undefined {
|
||||
const looks = looksOf(t.ops)
|
||||
@@ -934,6 +1010,8 @@ export const useChat = create<ChatState>((set, get) => ({
|
||||
runStatus: undefined, clipped: undefined, pendingRun: false, steps: undefined,
|
||||
hydrated: 'done', startedAt: Date.now(), endedAt: undefined, attempt: 0, cancel: null,
|
||||
noQuery: false, thinking: '', writing: null, scope: scope?.length ? scope : undefined,
|
||||
// 上一次的运行换下来了,它花的钱还算在这一轮里
|
||||
usage: retireRun(t),
|
||||
}))
|
||||
void flow.persist({ status: 'running', error: '', answer: '' })
|
||||
|
||||
@@ -1246,9 +1324,23 @@ function historyOf(conversationId: string, upToTurnId: string): string {
|
||||
return lines.slice(-6).join('\n\n')
|
||||
}
|
||||
|
||||
/** 一轮最多自动再试一次。第二次还不行多半是问题本身没说清,该让用户介入 */
|
||||
/**
|
||||
* 一轮最多自动再试一次。第二次还不行多半是问题本身没说清,该让用户介入。
|
||||
* 这几个决定(要不要运行、要不要重来、重来说什么)已经下沉到服务端 backend/app/chat/turn.py:final 带 next,复核带
|
||||
* retry_instruction,这里照它们走;下面这份常量和两种重来的措辞只在老服务端(没给这两个字段)时用,和服务端那份逐字
|
||||
* 对照(backend/tests/test_chat_turn_policy.py)
|
||||
*/
|
||||
const MAX_ATTEMPTS = 2
|
||||
|
||||
/** 重来时交给助手的话(同服务端 chat/turn.py 的 RETRY_WITH_BASE / RETRY_FROM_SCRATCH),只在老服务端时自己拼 */
|
||||
function retryInstruction(question: string, reason: string, hasBase: boolean): string {
|
||||
return hasBase
|
||||
? `这个工作流上一次运行没有成功:${reason}。\n\n`
|
||||
+ `请针对这个原因改它(比如调大最大步数、改正工具参数、换个查询方式),`
|
||||
+ `改完继续回答原来的问题:${question}`
|
||||
: `${question}\n\n(上一次没有成功:${reason}。请针对这个原因调整,重新给出完整的工作流。)`
|
||||
}
|
||||
|
||||
interface PlanArgs {
|
||||
/** 用户原本问的是什么。跑图取数、复核都要用它,不能被重试的补充说明污染 */
|
||||
question: string
|
||||
@@ -1298,26 +1390,25 @@ function plan(flow: Flow, args: PlanArgs) {
|
||||
* 这里会清空这一次的过程和答案——它是自动补救,用户还没拿到过答案。用户主动
|
||||
* 点的重试走 retryTurn,上一次要留档。
|
||||
*/
|
||||
const retry = (reason: string) => {
|
||||
if (attempt + 1 >= MAX_ATTEMPTS || !flow.alive()) return false
|
||||
const retry = (reason: string, instruction?: string) => {
|
||||
// 服务端给了那句话(复核的 retry_instruction)就是它已经按次数判过可以重来;老服务端由这里按次数判
|
||||
if ((!instruction && attempt + 1 >= MAX_ATTEMPTS) || !flow.alive()) return false
|
||||
const base = built && (built.nodes?.length ?? 0) > 0 ? built : null
|
||||
patch(() => ({
|
||||
patch((t) => ({
|
||||
attempt: attempt + 1,
|
||||
phase: 'planning',
|
||||
status: base ? '上次运行未成功,正在调整工作流…' : '上次运行未成功,正在重新生成工作流…',
|
||||
ops: [], events: [], run: null, output: null, final: undefined, runStatus: undefined,
|
||||
review: null, reviewRaw: null, rawOutput: null, lastSeq: 0, failure: null, error: '',
|
||||
// 换下来的那次运行花的钱还是这一轮的
|
||||
usage: retireRun(t),
|
||||
}))
|
||||
plan(flow, {
|
||||
question,
|
||||
attempt: attempt + 1,
|
||||
baseGraph: base,
|
||||
scope,
|
||||
instruction: base
|
||||
? `这个工作流上一次运行没有成功:${reason}。\n\n`
|
||||
+ `请针对这个原因改它(比如调大最大步数、改正工具参数、换个查询方式),`
|
||||
+ `改完继续回答原来的问题:${question}`
|
||||
: `${question}\n\n(上一次没有成功:${reason}。请针对这个原因调整,重新给出完整的工作流。)`,
|
||||
instruction: instruction ?? retryInstruction(question, reason, !!base),
|
||||
})
|
||||
return true
|
||||
}
|
||||
@@ -1417,8 +1508,13 @@ function plan(flow: Flow, args: PlanArgs) {
|
||||
const blockers = ((op.issues ?? []) as ValidationIssue[])
|
||||
.filter((i) => i.level === 'error')
|
||||
.map((i) => i.message)
|
||||
if (blockers.length && !selfChecked && retry(`生成的工作流没有通过校验:${blockers.join(';')}`)) break
|
||||
if (blockers.length) {
|
||||
// 建完之后怎么走由服务端定(final.next,backend/app/chat/turn.py 的 next_step):run / ready / fail。
|
||||
// 老服务端没给 next:照原来的规则——有 error 级问题时自查过就判失败、没自查过重来一次;autorun=false 只建不跑
|
||||
const next: string = op.next === 'run' || op.next === 'ready' || op.next === 'fail' ? op.next
|
||||
: blockers.length ? (selfChecked ? 'fail' : 'retry')
|
||||
: op.autorun === false ? 'ready' : 'run'
|
||||
if (next === 'retry' && retry(`生成的工作流没有通过校验:${blockers.join(';')}`)) break
|
||||
if (next === 'fail' || next === 'retry') {
|
||||
// 自查修正过(或重来一次)还是没过。校验消息是写给画布用户的("先拖一个节点进来"),
|
||||
// 直接甩给一个在问数据页打字的人毫无意义——先给一句他能照做的,
|
||||
// 原文留在技术细节里作线索
|
||||
@@ -1433,7 +1529,7 @@ function plan(flow: Flow, args: PlanArgs) {
|
||||
|
||||
// 用户要的是流程本身时(「设计一个每天跑的工作流」),建完就跑
|
||||
// 等于替他多花一次钱,而他要的是那张图
|
||||
if (op.autorun === false) {
|
||||
if (next === 'ready') {
|
||||
// 问了用户的不自动运行(服务端 autorun 为假):图摆着等「运行」,问题照样作为这一轮的内容、进历史
|
||||
patch(() => ({
|
||||
graph, phase: 'ready', status: asked ? CHAT_BLUEPRINT_TEXT.asked : PHASE_TEXT.ready, cancel: null,
|
||||
@@ -1447,9 +1543,15 @@ function plan(flow: Flow, args: PlanArgs) {
|
||||
graph, phase: 'running', status: PHASE_TEXT.running,
|
||||
explanation: op.explanation ?? '',
|
||||
}))
|
||||
void launch(flow, graph, question, retry)
|
||||
void launch(flow, graph, question, retry, attempt)
|
||||
break
|
||||
}
|
||||
case 'usage':
|
||||
// 这一轮助手建图花了多少(服务端 answer_ops 最后一条):累加进这一轮的用量、随 meta 落库。结局(final、reply)
|
||||
// 那一下已经落过库了,这里再写一次 meta
|
||||
patch((t) => ({ usage: { ...(t.usage ?? t.meta?.usage), copilot: addUsage((t.usage ?? t.meta?.usage)?.copilot, op) } }))
|
||||
void flow.persist()
|
||||
break
|
||||
case 'error':
|
||||
// 后端给的是三段:message 人话、hint 怎么办、detail 原始异常。
|
||||
// 原文不再截成一行甩给用户,收进可展开、可复制的技术细节
|
||||
@@ -1498,7 +1600,8 @@ function plan(flow: Flow, args: PlanArgs) {
|
||||
}
|
||||
|
||||
async function launch(
|
||||
flow: Flow, graph: GraphSpec, question: string, retry?: (reason: string) => boolean,
|
||||
flow: Flow, graph: GraphSpec, question: string, retry?: (reason: string, instruction?: string) => boolean,
|
||||
attempt = 0,
|
||||
) {
|
||||
const { patch } = flow
|
||||
try {
|
||||
@@ -1517,7 +1620,7 @@ async function launch(
|
||||
// 另开一页,它就成了「断在建图阶段」,放久了还会被当成断了写回 error。
|
||||
// 「跑一下」发起时库里那行还是 done,一并改成 running
|
||||
void flow.persist({ run_id: run.id, status: 'running' })
|
||||
watch(flow, run.id, 0, question, retry)
|
||||
watch(flow, run.id, 0, question, retry, attempt)
|
||||
} catch (e) {
|
||||
// 发起那一下没到后端:流程是好的,原样再发起就行。通用的「稍等几秒会自动重试」
|
||||
// 在这里不成立——这一页没有谁会替它重发
|
||||
@@ -1600,7 +1703,7 @@ function penOf(patch: Patch): { add: (e: RunEvent) => void; clear: () => void }
|
||||
*/
|
||||
function watch(
|
||||
flow: Flow, runId: string, after = 0,
|
||||
question = '', retry?: (reason: string) => boolean,
|
||||
question = '', retry?: (reason: string, instruction?: string) => boolean, attempt = 0,
|
||||
) {
|
||||
const { patch, conversationId, turnId } = flow
|
||||
/** 这条流上已经见到结局事件了,stream.end 不用再判一次 */
|
||||
@@ -1699,7 +1802,7 @@ function watch(
|
||||
patch(() => ({ status: '正在核对结果…', runStatus: 'succeeded' }))
|
||||
// 跑完不等于答对:先接住这次编排,确认没问题再交付
|
||||
void settle(flow, runId, question, d.output ?? null, {
|
||||
truncated: d.output_truncated === true, retry,
|
||||
truncated: d.output_truncated === true, retry, attempt,
|
||||
})
|
||||
break
|
||||
}
|
||||
@@ -1727,7 +1830,7 @@ function watch(
|
||||
if (status === 'succeeded') {
|
||||
// 结局事件在断线时丢了:成果以库里为准
|
||||
ended = true
|
||||
void settle(flow, runId, question, null, { retry })
|
||||
void settle(flow, runId, question, null, { retry, attempt })
|
||||
return
|
||||
}
|
||||
// 失败、取消、或者运行不见了:状态和原因让 hydrate 去读
|
||||
@@ -1754,7 +1857,11 @@ function watch(
|
||||
async function settle(
|
||||
flow: Flow, runId: string, question: string,
|
||||
eventOutput: Record<string, any> | null,
|
||||
opts: { truncated?: boolean; retry?: (reason: string) => boolean; run?: Run; endedAt?: number },
|
||||
opts: {
|
||||
truncated?: boolean; retry?: (reason: string, instruction?: string) => boolean; run?: Run; endedAt?: number
|
||||
/** 这一轮已经自动试了几次(从 0 数):交给复核,服务端据此决定给不给 retry_instruction */
|
||||
attempt?: number
|
||||
},
|
||||
) {
|
||||
const { patch } = flow
|
||||
|
||||
@@ -1781,7 +1888,7 @@ async function settle(
|
||||
// 复核慢到一定程度就不等了。它是补一层说明,不该反过来把答案扣住不放——
|
||||
// 干净的运行后端 15ms 就返回,会走到这个上限的都是真叫了模型的那些
|
||||
result = await Promise.race([
|
||||
api.copilot.review({ run_id: runId, question }),
|
||||
api.copilot.review({ run_id: runId, question, attempt: opts.attempt ?? 0 }),
|
||||
new Promise<null>((r) => setTimeout(() => r(null), REVIEW_TIMEOUT_MS)),
|
||||
])
|
||||
} catch { /* 复核用不了就按原样交付,下面照常走 */ }
|
||||
@@ -1796,8 +1903,21 @@ async function settle(
|
||||
// 事后排查"它当时为什么没警告我",靠的就是这个区别
|
||||
const review = result && result.verdict !== 'ok' ? result : null
|
||||
|
||||
// 答案根本不可信,而且原因说得清楚——重建一次图再跑,别让用户自己发现
|
||||
if (review?.retry && review.severity === 'broken' && opts.retry) {
|
||||
// 复核这一次的用量也是这一轮的花费(服务端 review_of_run 的 usage;老服务端没有)
|
||||
if (result?.usage) {
|
||||
const spent = result.usage
|
||||
patch((t) => ({ usage: { ...(t.usage ?? t.meta?.usage), review: addUsage((t.usage ?? t.meta?.usage)?.review, spent) } }))
|
||||
}
|
||||
|
||||
// 答案根本不可信,而且原因说得清楚——重建一次图再跑,别让用户自己发现。要不要重来、重来说什么由服务端定(复核的
|
||||
// retry_instruction,backend/app/chat/turn.py):给了那句话就照它重来,给的是 null 就不重来。老服务端没有这个字段时
|
||||
// 照原来的规则自己判
|
||||
if (opts.retry && result && 'retry_instruction' in result) {
|
||||
if (result.retry_instruction) {
|
||||
const reason = review ? review.signals.map((s) => s.detail).join(';') || review.note : ''
|
||||
if (opts.retry(reason, result.retry_instruction)) return
|
||||
}
|
||||
} else if (review?.retry && review.severity === 'broken' && opts.retry) {
|
||||
const reason = review.signals.map((s) => s.detail).join(';') || review.note
|
||||
if (opts.retry(reason)) return
|
||||
}
|
||||
|
||||
@@ -1699,6 +1699,17 @@ export interface ReviewResult {
|
||||
retry: boolean
|
||||
severity: 'broken' | 'degraded' | ''
|
||||
signals: ReviewSignal[]
|
||||
/**
|
||||
* 要不要带着原图重来一次、重来时交给助手的话(服务端 backend/app/chat/turn.py 按这一轮试过几次判):要就是那句话,不要
|
||||
* 是 null。老服务端没有这个字段,问数据页照原来的规则自己判
|
||||
*/
|
||||
retry_instruction?: string | null
|
||||
/** 复核这一次的用量(服务端 providers/usage.py 的汇总,没调模型时全 0):这一轮的花费 = 助手 + 运行 + 复核 */
|
||||
usage?: {
|
||||
cost_usd?: number | null; unpriced_calls?: number; calls?: number; input_tokens?: number; output_tokens?: number
|
||||
by_model?: Record<string, { input_tokens: number; output_tokens: number; calls: number; cost_usd: number
|
||||
unpriced_calls: number }>
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user