import { useEffect, useState } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import { Database, MessageSquare, RotateCw } from 'lucide-react'
import { AssistantStream, StreamEmpty, type StreamTurn } from '../run/AssistantStream'
import { Markdown } from '../run/Markdown'
import { ApprovalCard } from '../run/RunPanel'
import { decodeCopilot, decodeRun } from '../run/decode'
import { ToastHost } from '../components/ui'
import type { RunEvent } from '../types'
import fixtures from '../run/__tests__/fixtures.json'
import mdSamples from '../run/__tests__/markdown-samples.json'
import {
CALIBER_ONE_OUTPUT, CALIBERS_OUTPUT, COPILOT_STUCK, COPILOT_TOOLS_DROPPED, MIXED_OUTPUT, abandonedRun, cancelledRun, exhaustedTeam, longLoop,
markupRun, mixedRun, pipelineRun, repairRun, teamRun, timeoutRun,
} from '../run/__tests__/synthetic'
import '../index.css'
/**
* AssistantStream 的离线预览。
*
* 只有 dev server 会加载(vite build 的 input 只有 index.html),所以它不进
* 生产包。存在的理由:要改这个组件的排版,不该每次都真跑一次图——真跑一次
* 意味着要连数据库、等模型、还得凑巧撞上人工介入分支才能看到审批卡长什么样。
* 这里用的是真实导出的事件(fixtures.json)和照后端字段合成的事件
* (synthetic.ts),走的是同一个 decodeRun,只有事件来源是静态的。
*
* 打开 http://localhost:5273/preview.html;scripts/check-stream.mjs 按下面这些
* 查询参数逐个打开截图、断言。
*/
type Bucket = keyof typeof fixtures
const CASES: { key: Bucket; question: string; phase: StreamTurn['phase']; status: string }[] = [
{ key: 'db', question: '这个库里有多少数据?挑最大的表说说', phase: 'done', status: '已完成' },
{ key: 'think', question: '分析上季度华东区销量下滑的原因', phase: 'running', status: '运行中' },
{ key: 'human', question: '把这批客户的信用额度调高 10%', phase: 'waiting', status: '等待审批' },
{ key: 'issue', question: '出一份本月经营分析', phase: 'done', status: '已完成' },
{ key: 'failed', question: '查一下不存在的那张表', phase: 'error', status: '' },
{ key: 'loop_approve', question: '写条上线公告,我过一眼再发', phase: 'done', status: '已完成' },
{ key: 'supervisor', question: '冬虫夏草是什么', phase: 'done', status: '已完成' },
]
function buildTurns(): StreamTurn[] {
return CASES.map((c) => {
const events = (fixtures as any)[c.key] as any[]
const steps = decodeRun(events)
const finished = events.find((e) => e.type === 'run.finished')
const failed = events.find((e) => e.type === 'run.failed')
return {
id: c.key,
question: c.question,
phase: c.phase,
status: c.status,
steps,
thinking: c.phase === 'running'
? '先看一下 ANALYTICS 里有哪些和销量相关的视图,再按区域和月份聚合……'
: undefined,
output: finished?.data?.output ?? null,
error: failed ? String(failed.data?.error ?? '运行失败') : undefined,
runId: events[0]?.run_id,
}
})
}
/**
* 合成事件的 ts 固定在过去某一刻,拿来演"进行中"时秒表会显示几天。
* 整体平移到"最后一条是 lagMs 之前",实时计时就是一个合理的数
*/
function rebase(events: RunEvent[], lagMs = 3200): RunEvent[] {
const last = events[events.length - 1]?.ts ?? 0
const shift = (Date.now() - lagMs) / 1000 - last
return events.map((e) => ({ ...e, ts: e.ts + shift }))
}
/**
* 一段恶意输入。这类测例必须是构造的——真实输出不会自带攻击,但模型输出
* 是不可信内容,渲染层要么天生免疫,要么就是个 XSS 口子。
*/
const HOSTILE = [
'正常文本 **加粗**。',
'',
'',
'',
'[点我](javascript:window.__pwned=1)',
'[正常链接](https://example.com)',
'',
'``',
].join('\n')
/**
* 一份会超出折叠上限的报告,表格正好横跨切点。
*
* 之前折叠是按字符硬切的,切点落进表格中间——前面几行渲染成表格、最后半行
* 留成原始的 `| 1 | ThearchyHelios | …`。用户看到的是一份被咬掉一口的报告,
* 而它其实是完整的,只是被折叠了。
*/
function longReport(): string {
const head = [
"## 总结", "",
"- **管理员(平台级/区域级角色)**:共 9 人", "- **商家账户**:共 5 个", "",
"口径说明:".padEnd(400, "口"), "",
"## 一、管理员名单", "",
].join("\n")
const rows = Array.from({ length: 40 }, (_, i) =>
`| ${i + 1} | user_${i} | user${i}@example.com | 昵称${i} | ` +
`85e11976-6a7b-4167-b561-deec4fd${String(i).padStart(4, "0")} | 商家 ${i} | c${i}@example.com |`)
return [
head,
"| userID | 用户名 | 邮箱 | 昵称 | providerID | 商家名称 | 联系人邮箱 |",
"| --- | --- | --- | --- | --- | --- | --- |",
...rows,
"", "> 说明:部分账户身兼管理员与商家双重身份。",
].join("\n")
}
/** 直接回答的那一轮:没查库,界面上必须说破 */
const NO_QUERY_TURN: StreamTurn = {
id: "noquery", question: "刚才那 6 个管理员是怎么算的?", phase: "done", status: "已完成",
steps: [], noQuery: true,
output: { answer: "第 1 轮走的是 `role.level IN (platform, regional)` 这个口径。要换口径我重新查一遍。" },
}
/** 复核判了「不可信」:说明排在成果上方,且用报警色 */
const REVIEW_BROKEN_TURN: StreamTurn = {
id: "reviewbroken", question: "商家账户有多少个?", phase: "done", status: "已完成",
steps: [],
review: {
verdict: "annotated", severity: "broken", retry: true,
note: "这次取数没跑通(表名对不上),下面这个数是模型根据上下文推断的,不能当结论用。",
answer: null,
signals: [
{ kind: "tool_error", severity: "broken",
detail: "工具 db_query__shop 调用失败:no such table: merchant_account" },
{ kind: "step_limit", severity: "broken", detail: "agent 用满了 8 步还没给出结论" },
],
},
output: { answer: "大约 5 个左右。" },
}
/** 复核只是补了一句说明,答案照常给;外加改写前的原文可展开 */
const REVIEW_DEGRADED_TURN: StreamTurn = {
id: "reviewdegraded", question: "谁能改别人的权限?", phase: "done", status: "已完成",
steps: [],
review: {
verdict: "rewritten", severity: "degraded", retry: false,
note: "这次知识库检索退回了纯关键词匹配,换了说法的内容可能没被搜到。",
answer: null,
signals: [
{ kind: "retrieve_degraded", severity: "degraded",
detail: "重排结果解析不出来,按初筛顺序返回" },
],
},
output: { answer: "只有平台管理员可以修改他人权限,且需要走角色变更流程。" },
rawOutput: { answer: "平台管理员可以修改他人权限。" },
}
/** 协作团队的泳道:前两轮并行、第三轮汇总 */
const TEAM_TURN: StreamTurn = {
id: "team", question: "比较三种向量数据库在中文检索上的取舍", phase: "done", status: "已完成",
steps: [{
id: "n-team", seq: 1, kind: "node", status: "done", title: "调研团队", meta: "24.1 s", ms: 24100,
team: {
members: ["researcher", "analyst", "writer"],
savedMs: 11800,
finished: true,
rounds: [
{
round: 0, parallel: 2, wallMs: 9600, sumMs: 16400,
reason: "两边资料互不相干,可以同时查",
members: [
{ agent: "researcher", instruction: "查 Milvus 与 Qdrant 的中文分词支持", ms: 9600,
status: "done", result: "Milvus 2.4 起内置 jieba…" },
{ agent: "analyst", instruction: "查 pgvector 的中文检索方案", ms: 6800,
status: "done", result: "pgvector 本身不分词,需配 zhparser…" },
],
},
{
round: 1, parallel: 2, wallMs: 7400, sumMs: 12400,
reason: "两组基准测试互不依赖",
members: [
{ agent: "researcher", instruction: "跑召回率基准", ms: 7400, status: "done",
result: "hit@1:Milvus 0.91 / Qdrant 0.89 / pgvector 0.84" },
{ agent: "analyst", instruction: "跑写入吞吐基准", ms: 5000, status: "done",
result: "10 万条:Milvus 42s / Qdrant 51s / pgvector 88s" },
],
},
{
round: 2, parallel: 1, wallMs: 7100, sumMs: 7100,
reason: "两份基准都出来了,可以汇总",
members: [
{ agent: "writer", instruction: "根据前面的结论写选型建议", ms: 7100,
status: "done", result: "## 结论\n中文为主且要开箱即用 → Milvus…" },
],
},
],
},
}],
output: { 建议: "中文为主且要开箱即用选 Milvus;已有 Postgres 且数据量在百万以内,pgvector + zhparser 的运维成本最低。" },
}
const LONG_TURN: StreamTurn = {
id: "long", question: "需要能够显示总结内容", phase: "done", status: "已完成",
steps: [], output: { result: longReport() },
}
/** 合成场景:新后端才有的事件,老库里导不出来 */
function synthetic(name: string): StreamTurn[] {
switch (name) {
case 'team-live': {
const ev = rebase(teamRun('members'))
return [{ id: 'team-live', question: '比较三家供应商的交期风险', phase: 'running', status: '运行中',
steps: decodeRun(ev), runId: 'syn-team-live' }]
}
case 'team-cancelled': {
// 第 1 轮三人都还在跑时被取消:那一轮不知道本来要多久
const ev = rebase(teamRun('members').filter((e) =>
!(e.data?.agent === '物流员' && (e.type === 'agent.step.end' || e.type === 'llm.end'))))
const last = ev[ev.length - 1]
ev.push({ seq: last.seq + 1, type: 'run.cancelled', node_id: null, ts: last.ts + 1, data: {} })
return [{ id: 'team-cancelled', question: '比较三家供应商的交期风险', phase: 'done', statusCode: 'cancelled',
steps: decodeRun(ev), runId: 'syn-team-cancelled' }]
}
case 'team-routing': {
const ev = rebase(teamRun('routing'))
return [{ id: 'team-routing', question: '比较三家供应商的交期风险', phase: 'running', status: '运行中',
steps: decodeRun(ev), runId: 'syn-team-routing' }]
}
case 'team': {
const ev = teamRun('done')
return [{ id: 'team-done', question: '比较三家供应商的交期风险', phase: 'done', status: '已完成',
steps: decodeRun(ev), output: ev[ev.length - 1].data.output, runId: 'syn-team-done' }]
}
case 'long': {
const ev = longLoop(145)
return [{ id: 'long-loop', question: '逐拍读取传感器,跑完这一批', phase: 'done', status: '已完成',
steps: decodeRun(ev), output: ev[ev.length - 1].data.output, runId: 'syn-long' }]
}
case 'mixed': {
const ev = mixedRun()
const end = ev[ev.length - 1].data
return [{ id: 'mixed', question: '9 月 19 日各班次出勤率', phase: 'error', status: '失败',
steps: decodeRun(ev), output: MIXED_OUTPUT, runId: 'syn-mixed', runClass: 'exploratory',
error: { error: String(end.error), detail: end.detail } }]
}
case 'issued': {
// 同一份出具,跑完的样子:横幅、正文里画出来的数字、复制 / 导出
const ev = mixedRun().filter((e) => e.type !== 'node.failed' && e.type !== 'run.failed')
return [{ id: 'issued', question: '9 月 19 日各班次出勤率', phase: 'done', status: '已完成',
steps: decodeRun(ev, { status: 'succeeded' }), output: MIXED_OUTPUT, runId: 'syn-issued',
runClass: 'exploratory', review: REVIEW_DEGRADED_TURN.review }]
}
case 'calibers':
case 'caliber-one': {
// 按口径卡清点回指的数字:两张卡各数各的;只有一张卡时直说都来自它
return [{ id: name, question: '上周订单情况', phase: 'done', status: '已完成',
steps: decodeRun(pipelineRun(2), { status: 'succeeded' }), runId: `syn-${name}`, runClass: 'formal',
output: name === 'calibers' ? CALIBERS_OUTPUT : CALIBER_ONE_OUTPUT }]
}
case 'live': {
// 取数正在进行:第一条查询还没回来
const all = mixedRun()
const cut = all.findIndex((e) => e.type === 'tool.end' && e.data.call_id === 'q1')
const ev = rebase(all.slice(0, cut), 12400)
return [{ id: 'live', question: '9 月 19 日各班次出勤率', phase: 'running', status: '运行中',
steps: decodeRun(ev), runId: 'syn-live' }]
}
case 'cancelled':
return [{ id: 'cancelled', question: '把大表全量拉一遍', phase: 'done', statusCode: 'cancelled',
steps: decodeRun(cancelledRun()), runId: 'syn-cancel' }]
case 'chat': {
// 问数据的一轮:先建图、再执行。执行开始后「规划」收成一行
const plan = decodeCopilot([
{ op: 'heartbeat', phase: 'planning', elapsed_ms: 6400 },
{ op: 'thinking', delta: '要先查表结构,再按设备汇总 KPI。' },
{ op: 'plan', summary: '取数 → 汇总 → 出结论' },
{ op: 'add_node', node: { id: 'q', type: 'tool', label: '查询设备 KPI' } },
{ op: 'add_node', node: { id: 'o', type: 'output', label: '成果' } },
{ op: 'done', explanation: '两步' },
{ op: 'final', graph: { nodes: [{ id: 'q' }, { id: 'o' }] } },
])
const ev = (fixtures as any).db as RunEvent[]
return [{ id: 'chat', question: '这个库里有多少数据?挑最大的表说说', phase: 'done', status: '已完成',
steps: [...plan, ...decodeRun(ev)], runId: 'syn-chat',
output: ev[ev.length - 1].data.output,
graph: { nodes: [{ id: 'q', type: 'tool', data: { label: '查询设备 KPI' } },
{ id: 'o', type: 'output', data: { label: '成果' } }] } }]
}
case 'copilot':
return [{ id: 'copilot', question: '做一个每天的出勤日报', phase: 'done',
status: '已放到画布,但还有 2 处问题要你处理',
steps: decodeCopilot(COPILOT_STUCK, { context: 'canvas' }) }]
case 'codes':
// 编号、分组长得像数:'1063'、'001'。它们不该右对齐,也不该出「按 attribute_group 从高到低排」
return [{ id: 'codes', question: '各产线本周产量', phase: 'done', status: '已完成', steps: [],
output: { 结果: JSON.stringify({
columns: ['factory_code', 'line_name', 'attribute_group', 'output_qty'],
rows: [['1063', '一号线', '001', 1520], ['1064', '二号线', '002', 1310], ['1065', '三号线', '001', 980]],
row_count: 3 }) } }]
case 'schema': {
const ev = mixedRun().slice(0, 10)
return [{ id: 'schema', question: '看看有哪些人事表', phase: 'done', status: '已完成',
steps: decodeRun(ev, { status: 'succeeded' }) }]
}
// ---- 平台问题的几种结局(NI-3/4/5):失败的轮次把 run.failed 的原话交给流,和运行页、画布右栏一样
case 'exhausted':
case 'markup':
case 'repair': {
const ev = name === 'exhausted' ? exhaustedTeam('fail') : name === 'markup' ? markupRun() : repairRun()
const end = ev[ev.length - 1].data
return [{ id: name, question: '上月各区域订单额', phase: 'error', steps: decodeRun(ev), runId: `syn-${name}`,
error: { error: String(end.error), ...(end.detail ? { detail: end.detail } : {}) } }]
}
case 'exhausted-degrade': {
const ev = exhaustedTeam('degrade')
return [{ id: 'exhausted-degrade', question: '上月各区域订单额', phase: 'done', steps: decodeRun(ev),
output: { 结论: ev[ev.length - 1].data.output.结论, _issuance: {
tier: 'degraded', calibers: [], metrics_checked: 0, matched_numbers: 0, unmatched_numbers: [],
missing_required: [], missing_expected: [],
gaps: ['协作团队「销售分析团队」用完 2 轮仍未完成,交来的是成员最后的原话'] } },
runId: 'syn-exhausted-degrade', runClass: 'exploratory' }]
}
case 'exhausted-closing': {
const ev = rebase(exhaustedTeam('closing'))
return [{ id: 'exhausted-closing', question: '上月各区域订单额', phase: 'running', steps: decodeRun(ev),
runId: 'syn-exhausted-closing' }]
}
case 'timeout-live': {
// 查询已经走了 34 秒,时限是 30 秒:秒表要说破「已超出上限」
const ev = rebase(timeoutRun('live'), 34000)
return [{ id: 'timeout-live', question: '把订单明细全拉出来', phase: 'running', steps: decodeRun(ev),
runId: 'syn-timeout-live' }]
}
case 'timeout':
return [{ id: 'timeout', question: '把订单明细全拉出来', phase: 'error', steps: decodeRun(timeoutRun('done')),
runId: 'syn-timeout', error: { error: '查询超过 30s 没有返回,已放弃等待(数据库那边可能还在跑,连接会在后台收回)。加上 WHERE 条件或 LIMIT 缩小范围再查' } }]
case 'abandoned':
return [{ id: 'abandoned', question: '把这份报表发出去', phase: 'done', statusCode: 'cancelled',
steps: decodeRun(abandonedRun()), runId: 'syn-abandoned' }]
case 'structured':
// 页面已经把报错拆好了(问数据页的 Failure):照原样画,不再翻译一遍
return [{ id: 'structured', question: '上周各区域订单数', phase: 'error', steps: [],
error: { title: '操作超时', reason: '查询超时:数据库 90 秒没有返回', hint: '缩小时间范围后点「重试这一轮」',
detail: 'OperationalError: (3024) maximum statement execution time exceeded' } }]
case 'restored':
// 从库里恢复的一轮:没有事件,查询次数由页面从落库的 meta 带过来
return [{ id: 'restored', question: '上周各区域订单数', phase: 'done', steps: [], queries: 3, elapsedMs: 8400,
output: { answer: '华东 1,204 单,华南 986 单,华北 731 单。' }, runId: 'syn-restored' }]
case 'tools-dropped':
return [{ id: 'tools-dropped', question: '只查上月的数据', phase: 'done', status: '已更新画布(修改 1)',
tone: 'warn', steps: decodeCopilot(COPILOT_TOOLS_DROPPED, { context: 'canvas' }) }]
default:
return []
}
}
/** Markdown 渲染用真实输出验,不用编的样本 */
function MarkdownCases() {
return (