mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-23 00:42:00 +00:00
数据目录阶段 1B 前端:画布和问数据页的过程里显示「参考了 N 张表」,展开按数据源列出表名,挑表失败时写明只提供了表名及原因
This commit is contained in:
@@ -13,7 +13,7 @@ import mdSamples from '../run/__tests__/markdown-samples.json'
|
||||
import evidenceFixture from '../run/__tests__/evidence-doc.json'
|
||||
import evidenceJudge from '../run/__tests__/evidence-judge.json'
|
||||
import {
|
||||
CALIBER_ONE_OUTPUT, CALIBERS_OUTPUT, COPILOT_STUCK, COPILOT_TOOLS_DROPPED, MIXED_OUTPUT, abandonedRun, cancelledRun, exhaustedTeam, longLoop,
|
||||
CALIBER_ONE_OUTPUT, CALIBERS_OUTPUT, COPILOT_CONTEXT, COPILOT_CONTEXT_FALLBACK, COPILOT_STUCK, COPILOT_TOOLS_DROPPED, MIXED_OUTPUT, abandonedRun, cancelledRun, exhaustedTeam, longLoop,
|
||||
markupRun, mixedRun, pipelineRun, repairRun, teamRun, timeoutRun,
|
||||
} from '../run/__tests__/synthetic'
|
||||
import '../index.css'
|
||||
@@ -323,6 +323,8 @@ function synthetic(name: string): StreamTurn[] {
|
||||
// 问数据的一轮:先建图、再执行。执行开始后「规划」收成一行
|
||||
const plan = decodeCopilot([
|
||||
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 6400 },
|
||||
{ op: 'context', elapsed_ms: 2100, sources: [
|
||||
{ source: 'warehouse', tables: ['v_device_kpi', 'dim_device'], selected_by: 'model', total: 53 }] },
|
||||
{ op: 'thinking', delta: '要先查表结构,再按设备汇总 KPI。' },
|
||||
{ op: 'plan', summary: '取数 → 汇总 → 出结论' },
|
||||
{ op: 'add_node', node: { id: 'q', type: 'tool', label: '查询设备 KPI' } },
|
||||
@@ -337,6 +339,13 @@ function synthetic(name: string): StreamTurn[] {
|
||||
graph: { nodes: [{ id: 'q', type: 'tool', data: { label: '查询设备 KPI' } },
|
||||
{ id: 'o', type: 'output', data: { label: '成果' } }] } }]
|
||||
}
|
||||
case 'copilot-context':
|
||||
// 大库按需求挑表:过程里一行「参考了 N 张表」,展开按数据源分组列表名
|
||||
return [{ id: 'copilot-context', question: '各渠道上月的入园人次', phase: 'done', status: '已应用到画布',
|
||||
steps: decodeCopilot(COPILOT_CONTEXT, { context: 'canvas' }) }]
|
||||
case 'copilot-fallback':
|
||||
return [{ id: 'copilot-fallback', question: '各渠道上月的入园人次', phase: 'done', status: '已应用到画布',
|
||||
steps: decodeCopilot(COPILOT_CONTEXT_FALLBACK, { context: 'canvas' }) }]
|
||||
case 'copilot':
|
||||
return [{ id: 'copilot', question: '做一个每天的出勤日报', phase: 'done',
|
||||
status: '已应用到画布,仍有 2 处问题待处理',
|
||||
|
||||
@@ -395,6 +395,41 @@ export const COPILOT_TOOLS_DROPPED = [
|
||||
] },
|
||||
]
|
||||
|
||||
/**
|
||||
* 助手按需求挑表(context 操作,copilot_context.DatasourceContext.op):大库挑了 3 张(含补进来的关联表),
|
||||
* 小库全部表都带字段。挑表用了 3 秒多,先到一拍心跳,再是 context,然后照常思考、起草、自查
|
||||
*/
|
||||
export const COPILOT_CONTEXT = [
|
||||
{ op: 'model', model: 'claude-sonnet-4' },
|
||||
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 3000 },
|
||||
{ op: 'context', elapsed_ms: 3420, sources: [
|
||||
{ source: 'scenic', tables: ['visits', 'channels', 'channel_visits'], selected_by: 'model', total: 51 },
|
||||
{ source: 'shop', tables: ['orders', 'customers'], selected_by: 'all', total: 2 },
|
||||
] },
|
||||
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 6000 },
|
||||
{ op: 'thinking', delta: '入园记录不带渠道,要经 channel_visits 连到渠道表,再按渠道汇总上月的入园人次。' },
|
||||
{ op: 'plan', summary: '取数 → 出结论' },
|
||||
{ op: 'add_node', node: { id: 'q', type: 'tool', label: '查询各渠道入园人次' } },
|
||||
{ op: 'add_node', node: { id: 'o', type: 'output', label: '成果' } },
|
||||
{ op: 'done', explanation: '两步' },
|
||||
{ op: 'check', status: 'passed', repaired: 0 },
|
||||
{ op: 'final', graph: { nodes: [{ id: 'q' }, { id: 'o' }] } },
|
||||
]
|
||||
|
||||
/** 挑表失败(超时):只给了表名,原因随 context 带来;生成照常进行 */
|
||||
export const COPILOT_CONTEXT_FALLBACK = [
|
||||
{ op: 'model', model: 'claude-sonnet-4' },
|
||||
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 18000 },
|
||||
{ op: 'context', elapsed_ms: 20004, sources: [
|
||||
{ source: 'scenic', tables: [], selected_by: 'fallback', total: 51, reason: '挑选数据表超过 20 秒未完成' },
|
||||
] },
|
||||
{ op: 'plan', summary: '先查表结构,再取数' },
|
||||
{ op: 'add_node', node: { id: 's', type: 'tool', label: '查看表结构' } },
|
||||
{ op: 'add_node', node: { id: 'o', type: 'output', label: '成果' } },
|
||||
{ op: 'done', explanation: '两步' },
|
||||
{ op: 'final', graph: { nodes: [{ id: 's' }, { id: 'o' }] } },
|
||||
]
|
||||
|
||||
/** Copilot 操作流:思考被心跳打断、自查修一轮没修好、模型写了不存在的节点类型 */
|
||||
export const COPILOT_STUCK = [
|
||||
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 3000 },
|
||||
|
||||
@@ -2404,6 +2404,83 @@ function labelsOf(ops: CopilotOp[], fallback?: (id: string) => string | undefine
|
||||
return (id) => map.get(id) ?? fallback?.(id)
|
||||
}
|
||||
|
||||
/** context 操作里一个数据源的那一项(服务端 copilot_context.DatasourceContext.op) */
|
||||
export interface CopilotContextSource {
|
||||
source: string
|
||||
/** 模型看得到全部字段的表(全名)。只给了表名的不在这里 */
|
||||
tables: string[]
|
||||
/** model:按需求挑的;all:库不大,全部表都带字段;fallback:没能挑出来,只给了表名 */
|
||||
selectedBy: 'model' | 'all' | 'fallback'
|
||||
/** 这个库一共几张表 */
|
||||
total?: number
|
||||
/** 没能挑出来的原因 */
|
||||
reason?: string
|
||||
}
|
||||
|
||||
/** 挑表失败时的说法:模型这一轮只看到表名,字段要它自己去查 */
|
||||
export const CONTEXT_FALLBACK_TEXT = '未能按需求挑选,已提供全部表名'
|
||||
|
||||
function contextSources(op: CopilotOp): CopilotContextSource[] {
|
||||
const list: unknown[] = Array.isArray(op.sources) ? op.sources : []
|
||||
return list
|
||||
.filter((x): x is Record<string, any> => !!x && typeof x === 'object' && typeof (x as any).source === 'string')
|
||||
.map((x) => {
|
||||
const total = num(x.total)
|
||||
return {
|
||||
source: x.source,
|
||||
tables: Array.isArray(x.tables) ? x.tables.map(String) : [],
|
||||
selectedBy: x.selected_by === 'model' || x.selected_by === 'fallback' ? x.selected_by : 'all',
|
||||
...(total != null ? { total } : {}),
|
||||
...(typeof x.reason === 'string' && x.reason ? { reason: x.reason } : {}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** 一个库在展开区里的那一行:怎么来的 + 表名 */
|
||||
function contextLine(g: CopilotContextSource): string {
|
||||
const names = g.tables.join('、')
|
||||
const of = g.total != null ? `共 ${g.total} 张表` : ''
|
||||
switch (g.selectedBy) {
|
||||
case 'all':
|
||||
return `「${g.source}」全部 ${g.tables.length} 张表:${names}`
|
||||
case 'model':
|
||||
return g.tables.length
|
||||
? `「${g.source}」按需求${g.total != null ? `从 ${g.total} 张表中` : ''}挑出 ${g.tables.length} 张:${names}`
|
||||
: `「${g.source}」${of ? `${of},` : ''}未挑中与需求相关的表,已提供全部表名`
|
||||
default:
|
||||
return `「${g.source}」${of ? `${of},` : ''}${CONTEXT_FALLBACK_TEXT}`
|
||||
+ (g.tables.length ? `;现有工作流用到的 ${g.tables.length} 张表附带全部字段:${names}` : '')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 助手这一轮参考了哪些表(context 操作)→ 过程里的一行。
|
||||
*
|
||||
* 大库放不下全部字段,服务端先按需求挑表,挑中的表给模型全部字段。这件事要看得见:生成的 SQL 用错了表,
|
||||
* 先得知道模型当时看到的是哪几张。标题只说几张(「参考了 8 张表」),展开按数据源分组列表名;挑表失败时
|
||||
* 标题直说只给了表名,原因放在展开区。认不出的形状返回 null:不出这一行,也不报错。
|
||||
*/
|
||||
export function copilotContext(op: CopilotOp): { title: string; sub?: string; detail: string; ms?: number } | null {
|
||||
const groups = contextSources(op)
|
||||
if (!groups.length) return null
|
||||
const count = groups.reduce((n, g) => n + g.tables.length, 0)
|
||||
const failed = groups.filter((g) => g.selectedBy === 'fallback')
|
||||
// 有表带着字段(小库,或者现有工作流用到的表)时标题仍说几张,没挑成的事放副标题
|
||||
const sub = count && failed.length
|
||||
? failed.length === groups.length ? CONTEXT_FALLBACK_TEXT
|
||||
: `「${failed.map((g) => g.source).join('」「')}」${CONTEXT_FALLBACK_TEXT}`
|
||||
: undefined
|
||||
const reasons = [...new Set(failed.map((g) => g.reason).filter(Boolean))]
|
||||
// 挑过表才有 elapsed_ms:挑表是生成之前多出来的一次模型调用,没挑成(比如超时)也要让人看到等了多久
|
||||
const ms = num(op.elapsed_ms)
|
||||
return {
|
||||
title: count ? `参考了 ${count} 张表` : CONTEXT_FALLBACK_TEXT,
|
||||
...(sub ? { sub } : {}),
|
||||
detail: [...groups.map(contextLine), ...reasons.map((r) => `原因:${r}`)].join('\n'),
|
||||
...(ms != null && ms >= 10 ? { ms } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
/** 用了限定范围之外的数据源:自查交回模型改也改不掉时,人要知道是范围的事,不是图写错了 */
|
||||
const OUT_OF_SCOPE_NEXT = '本轮限定了查询的数据源,但工作流使用了范围之外的数据库。请取消限定后重新提问,或将所需数据库加入查询范围'
|
||||
|
||||
@@ -2508,6 +2585,16 @@ export function decodeCopilot(ops: CopilotOp[], opts?: {
|
||||
...(elapsed ? { ms: elapsed, meta: formatDuration(elapsed) } : {}) })
|
||||
break
|
||||
}
|
||||
case 'context': {
|
||||
// 这一轮参考了哪些表:标题说几张,展开看按数据源分组的表名
|
||||
const ctx = copilotContext(op)
|
||||
if (!ctx) break
|
||||
add({ id: `cx-${i}`, seq: i, kind: 'schema', status: 'done', code: 'copilot_context',
|
||||
title: ctx.title, detail: ctx.detail,
|
||||
...(ctx.sub ? { sub: ctx.sub } : {}),
|
||||
...(ctx.ms != null ? { ms: ctx.ms, meta: formatDuration(ctx.ms) } : {}) })
|
||||
break
|
||||
}
|
||||
case 'plan':
|
||||
add({ id: `cp-${i}`, seq: i, kind: 'note', status: 'done',
|
||||
title: clip(String(op.summary ?? '已完成规划'), 120) })
|
||||
|
||||
@@ -12,7 +12,7 @@ import { humanizeError } from '../lib/errors'
|
||||
import { formatShortcut } from '../lib/keys'
|
||||
import { UPGRADE_TEXT } from '../lib/terms'
|
||||
import type { CopilotOp } from '../run/decode'
|
||||
import { endingOf, reduceTeam, settleTeam } from '../run/decode'
|
||||
import { copilotContext, endingOf, reduceTeam, settleTeam } from '../run/decode'
|
||||
import { activeEdgesOf, applyDerived } from '../run/derive'
|
||||
import {
|
||||
emptyTrace, finalizeTrace, foldEvent, isActivePhase, isSettled, isTerminal, runStatusOf,
|
||||
@@ -1888,6 +1888,12 @@ export const useStudio = create<StudioState>((set, get) => ({
|
||||
},
|
||||
})
|
||||
break
|
||||
case 'context': {
|
||||
// 服务端按需求挑了表:浮条和轮次卡上先说参考了几张表,展开的过程里有表名(decodeCopilot)
|
||||
const ctx = copilotContext(op)
|
||||
if (ctx) set({ copilot: { ...s.copilot, lastOp: ctx.title } })
|
||||
break
|
||||
}
|
||||
case 'plan':
|
||||
set({ copilot: { ...s.copilot, phase: 'planning', lastOp: op.summary ?? '规划中' } })
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user