import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { AlertTriangle, ArrowRight, Check, CheckCircle2, CornerDownRight, ListChecks, Plus, RotateCw, ShieldCheck, Sparkles, Trash2, Unlink, Wand2, Workflow, XCircle, } from 'lucide-react' import clsx from 'clsx' import { NODE_DEFS } from './nodeDefs' import { fixFieldLabel, fixValueLines, fixValueText, isBareUpgradeAdvice, isUpgradeAdvice, problemsOf, unboundToolOf, upgradeChangeKind, upgradeChangesFromDiff, upgradeEdgeText, upgradeGroups, upgradeNodeText, upgradeRuleText, upgradeStepText, upgradeSummary, upgradeTypeText, withToolBound, type FieldRef, type NodeNameOf, type Problem, } from './issues' import { PublishPreflightPane } from './PublishDialog' import { humanizeError } from '../lib/errors' import { formatShortcut } from '../lib/keys' import { PROVIDER_REF_TEXT, PUBLISH_FIX_TEXT, UPGRADE_TEXT } from '../lib/terms' import { useCatalog } from '../store/catalog' import { EDIT_LOCK_TEXT, upgradeUnsaved, useEditLock, useStudio, type UpgradeFlow } from '../store/studio' import { Spinner, useRadioGroup } from '../components/ui' import type { UpgradeChange, ValidationIssue } from '../types' /** * 问题面板:图级、节点、边上的 error 和 warning,一条一行,点一下定位。 * * 以前只有工具栏上一个点不动的「2 个错误」:图级的(找不到入口)只体现在计数里, * 边上的根本没地方看,有 error 时 warning 的计数还被藏起来——而 warning 恰恰是 * 「取到空值」这类运行期静默出错的那一类。现在按节点分组(图级在最前), * 点一行选中节点、镜头对过去、检查器翻到出问题的那个字段。F8 / ⇧F8 在问题之间跳。 */ export function ProblemsPane({ problems, activeId, onLocate, onDeleteEdge }: { problems: Problem[] activeId: string | null onLocate: (p: Problem) => void onDeleteEdge: (edgeId: string) => void }) { // 校验:画布上随改随查的那一份。发布前检查:按选定的发布等级另查一遍门禁,带修法—— // 在画布上就能提前发现、提前修,不用等到点发布那一刻 const [mode, setMode] = useState<'lint' | 'publish'>('lint') const radio = useRadioGroup(MODES, mode, setMode) const hasWorkflow = useStudio((s) => !!s.workflow) // 又开了一次升级预览(记录页的横幅跳过来、点了快速修复):它画在「校验」这一页,停在发布前检查上就看不见 const upgradeSeq = useStudio((s) => s.upgrade?.seq) useEffect(() => { if (upgradeSeq != null) setMode('lint') }, [upgradeSeq]) const errors = problems.filter((p) => p.level === 'error').length return ( <> {hasWorkflow && (
{MODES.map((m) => ( ))}
)} {mode === 'publish' && hasWorkflow ? { const { nodes } = useStudio.getState() const [p] = problemsOf([issue], nodes) if (p) onLocate(p) }} /> : } ) } const MODES = ['lint', 'publish'] as const function LintPane({ problems, activeId, onLocate, onDeleteEdge }: { problems: Problem[] activeId: string | null onLocate: (p: Problem) => void onDeleteEdge: (edgeId: string) => void }) { const nodes = useStudio((s) => s.nodes) const analysis = useStudio((s) => s.analysis) const analysisError = useStudio((s) => s.analysisError) const analyzeNow = useStudio((s) => s.analyzeNow) const offline = useCatalog((s) => s.backend === 'down') const list = useRef(null) // 「可以升级为可追溯结构」:校验给的建议,不是问题。预览开着时建议可能已经没了(又校验过一次),预览照样留着 const upgradeAdvice = useStudio((s) => s.advice.find(isUpgradeAdvice)) const upgrading = useStudio((s) => !!s.upgrade) const upgrade = upgradeAdvice || upgrading ? : null // F8 跳到的那一条要在视野里 useEffect(() => { if (!activeId) return list.current?.querySelector(`[data-problem="${CSS.escape(activeId)}"]`) ?.scrollIntoView({ block: 'nearest' }) }, [activeId]) // 只有后端整个断了,恢复连接时才会自动重跑(useOnReconnect);校验接口自己报错 // 不会有人替你再试,这时候承诺「会自动重新校验」就是在让人干等 const retryHint = offline ? '与服务端恢复连接后将自动重新校验' : '点击「重试」重新校验' if (analysis === 'failed' && !problems.length) { const failed = (
分析失败{analysisError ? `:${analysisError}` : ''} 暂时无法获取问题清单,画布仍可正常编辑。{retryHint}
) // 升级块照样摆着:后端断开时保存和校验一起失败,这时候最要紧的正是「没存上 → 重试保存」 if (!upgrade) return failed return (
{upgrade} {failed}
) } if (!problems.length) { const idle = (
{analysis === 'pending' ? <> 正在校验… : <> 未发现问题}
) if (!upgrade) return idle return (
{upgrade} {idle}
) } const byNode = new Map(nodes.map((n) => [n.id, n])) const groups: { key: string; title: React.ReactNode; items: Problem[] }[] = [] for (const p of problems) { const key = p.scope === 'node' ? `n:${p.nodeId}` : p.scope let g = groups.find((x) => x.key === key) if (!g) { const node = p.nodeId ? byNode.get(p.nodeId) : undefined const def = node ? NODE_DEFS[node.data.nodeType] : undefined g = { key, title: p.scope === 'graph' ? <> 整个工作流 : p.scope === 'edge' ? <> 连线 : <> {def ? : } {node?.data.label || p.nodeId} {def && · {def.label}} , items: [], } groups.push(g) } g.items.push(p) } return ( <> {/* 这次没分析成:清单还是上一次校验的,照样列出来(多半仍然有效),但说清楚它可能过时 */} {analysis === 'failed' && (
分析失败{analysisError ? `:${analysisError}` : ''} · 以下为上一次校验的结果,可能已过时。{retryHint}
)}
{upgrade}
{groups.map((g) => (
{g.title} {g.items.length}
{g.items.map((p) => ( onLocate(p)} onDeleteEdge={onDeleteEdge} /> ))}
))}
{formatShortcut('F8')} 下一个 · {formatShortcut('Shift+F8')} 上一个
) } function ProblemRow({ problem: p, active, onLocate, onDeleteEdge }: { problem: Problem; active: boolean; onLocate: () => void; onDeleteEdge: (id: string) => void }) { const node = useStudio((s) => (p.nodeId ? s.nodes.find((n) => n.id === p.nodeId) : undefined)) const updateNode = useStudio((s) => s.updateNode) const locked = useEditLock() != null const err = p.level === 'error' const Icon = err ? XCircle : AlertTriangle const where = fieldLabel(p.field, node?.data.nodeType, node?.data.config) const locatable = p.scope === 'node' && !!node // 提示词点了名的工具没绑:就地绑上,和检查器里那个按钮是同一个修法 const tool = unboundToolOf(p.message) const bound = node && tool ? withToolBound(node.data.config ?? {}, p.field, tool) : null return (
{bound && tool && ( )} {/* 悬空边(指向不存在的节点):React Flow 不画它,画布上看不见、点不到,只能在这儿删 */} {p.scope === 'edge' && p.edgeId && ( )}
) } // ------------------------------------------------------------------------- // 一键升级为可追溯结构:校验给了建议(evidence.upgrade_available)才有入口。老后端不给这条建议, // 入口也就不出现。和发布前修复同一套规矩:先出预览,人点「应用」才走现有的保存存成草稿 // ------------------------------------------------------------------------- const reasonOf = (e: unknown) => { const h = humanizeError(e) return h.reason ? `${h.title}:${h.reason}` : h.title } /** 建议那一行加上预览。画布锁着(正式运行在跑、助手在改)不给入口,说清为什么 */ function UpgradeBlock({ advice }: { advice?: ValidationIssue }) { const upgrade = useStudio((s) => s.upgrade) const previewUpgrade = useStudio((s) => s.previewUpgrade) const lock = useEditLock() // 预览出来、换了状态(算完、出错)时滚到眼前:连同上面那句建议一起,别只露出半截预览 const ref = useRef(null) const seq = upgrade?.seq const status = upgrade?.status useEffect(() => { if (seq != null) ref.current?.scrollIntoView({ block: 'nearest' }) }, [seq, status]) return (
{advice?.message || UPGRADE_TEXT.advice} {/* 后端的建议多半已经说了要改哪里、先预览;只有一句光秃秃的标题时才补这句说明 */} {(!advice?.message || isBareUpgradeAdvice(advice.message)) && ( {UPGRADE_TEXT.adviceHint} )} {!lock && !upgrade && ( )}
{lock &&
{UPGRADE_TEXT.locked(EDIT_LOCK_TEXT[lock])}
} {upgrade && }
) } /** 预览里念节点和字段要用的几样:名字、升级前后的类型、升级前的配置 */ interface UpgradeNames { nameOf: NodeNameOf /** 升级后的类型(新插入的节点也有) */ typeOf: (id?: string | null) => string | undefined /** 升级前的类型:换成报告撰写之后去掉的那几项(用户提示、角色设定)按原来的叫法念 */ wasTypeOf: (id?: string | null) => string | undefined configOf: (id?: string | null) => Record | undefined } /** * 节点 id → 名字和类型:先认画布上的,再认升级后的图里的(新插入的报告节点只在那里)。跟着 issues * 和预览换新,不订阅 nodes:拖一下节点这一块不必跟着重渲染 */ function useUpgradeNames(flow: UpgradeFlow): UpgradeNames { const tick = useStudio((s) => s.issues) const after = flow.status === 'ready' ? flow.result.graph : null const map = useMemo(() => { const m = new Map() for (const n of after?.nodes ?? []) m.set(n.id, { label: n.data?.label || n.id, type: n.type }) // 画布上的名字优先,类型取升级后的:改的是「写报告」这个节点,字段按报告撰写的叫法念 for (const n of useStudio.getState().nodes) { m.set(n.id, { label: n.data.label || n.id, type: m.get(n.id)?.type ?? n.data.nodeType }) } return m // eslint-disable-next-line react-hooks/exhaustive-deps }, [tick, after]) const nameOf = useCallback((id) => map.get(id)?.label, [map]) const typeOf = useCallback((id?: string | null) => (id ? map.get(id)?.type : undefined), [map]) // 列表项(成果字段第几项)按改之前的配置认名字:改的正是它 const base = flow.status === 'ready' ? flow.base : null const before = useCallback((id?: string | null) => (id ? base?.nodes.find((n) => n.id === id) : undefined), [base]) const configOf = useCallback((id?: string | null) => before(id)?.data?.config, [before]) const wasTypeOf = useCallback((id?: string | null) => before(id)?.type, [before]) return { nameOf, typeOf, wasTypeOf, configOf } } /** 升级预览:逐项「节点 · 字段:原值 → 新值」,节点类型的变化写成「模型调用 → 报告撰写」,说明原样列出 */ function UpgradePreview({ flow, locked }: { flow: UpgradeFlow; locked: boolean }) { const dirty = useStudio((s) => s.dirty) const previewUpgrade = useStudio((s) => s.previewUpgrade) const applyUpgrade = useStudio((s) => s.applyUpgrade) const retryUpgradeSave = useStudio((s) => s.retryUpgradeSave) const discard = useStudio((s) => s.discardUpgrade) const names = useUpgradeNames(flow) const { nameOf } = names const T = UPGRADE_TEXT const P = PUBLISH_FIX_TEXT const box = 'mt-1.5 rounded-md border px-2.5 py-2' const apply = async () => { if (await applyUpgrade()) void useCatalog.getState().refresh() } const retrySave = async () => { if (await retryUpgradeSave()) void useCatalog.getState().refresh() } if (flow.status === 'loading') { return (
{flow.assist ? T.assisting : T.previewing}
) } if (flow.status === 'unsupported' || flow.status === 'error') { return (
{flow.status === 'unsupported' ? T.unsupported : `${T.failed}:${reasonOf(flow.error)}`} {flow.status === 'error' && !locked && ( )}
) } const { result, base } = flow // 后端没给逐项改动时,按前后两张图自己列:照样逐项写,不替它编原因 const changes = result.changes.length ? result.changes : result.graph ? upgradeChangesFromDiff(base, result.graph) : [] const summary = result.graph ? upgradeSummary(base, result.graph) : [] const left = result.issues.filter((i) => i.level === 'error').length const usable = !!result.graph && changes.length > 0 // 没采用的那几步,后端往往也在说明里写了一句「……:没有采用,原因」:说明里已经有这个原因的不再列一遍 const rejected = result.rejected.filter((r) => !result.notes.some((n) => n.text.includes(r.reason))) const a = flow.apply const saving = a?.status === 'saving' const unsaved = upgradeUnsaved(a) return (
{changes.length ? T.title(changes.length) : T.noChange}
{summary.length > 0 &&
{T.summary(summary)}
} {changes.length > 0 && (
{upgradeGroups(changes).map((g) => (
{g.label && (
{g.rule && } {g.label}
)}
    {g.items.map((c, i) => ( ))}
))}
)} {result.notes.length > 0 && (
{T.notes}
    {result.notes.map((n, i) => (
  • {n.node_id && !n.text.includes(`「${nameOf(n.node_id) ?? n.node_id}」`) && ( 「{nameOf(n.node_id) ?? n.node_id}」 )} {n.text}
  • ))}
)} {rejected.length > 0 && (
{T.rejected}
    {rejected.map((r, i) => (
  • {upgradeStepText(r.fix_id, nameOf)}:{r.reason}
  • ))}
)} {result.assist && (result.assist.summary || !!result.assist.questions?.length || !!result.assist.warnings?.length) && (
{result.assist.summary && (
{P.assistSaid}:{result.assist.summary}
)} {!!result.assist.warnings?.length && (
{T.assistWarnings}
    {result.assist.warnings.map((w, i) => (
  • {w}
  • ))}
)} {!!result.assist.questions?.length && (
{P.questions}
    {result.assist.questions.map((q, i) =>
  • {q}
  • )}
)}
)} {usable && (
{left ? T.afterLeft(left) : T.afterOk}
)} {a?.status === 'stale' && (
{T.stale} {!locked && ( )}
)} {a?.status === 'locked' && !a.unsaved &&
{T.locked(a.why)}
} {unsaved && (
{a?.status === 'failed' ? `${P.saveFailed}:${reasonOf(a.error)}` : a?.status === 'locked' ? `${P.saveFailed}(${a.why})` : ''}。 {T.saveFailedHint} {/* 放弃只收起这份预览:画布上的升级留着(要退回就撤销),之后按平常的保存来存 */}
)} {!unsaved && (
{saving ? P.saving : locked ? '' : usable ? `${T.applyNote}${dirty ? `。${P.dirtyNote}` : ''}` : ''} {!flow.assist && !locked && ( )} {usable && !locked && ( )}
)}
) } /** 命中的改写规则:R1–R5 或 Copilot */ function RuleTag({ rule }: { rule: string }) { return {upgradeRuleText(rule)} } /** 这个字段按哪种节点的叫法念:升级后的类型认得就用它,认不得(换类型时去掉的用户提示)用原来的 */ function ownerType(field: string | null | undefined, id: string | null | undefined, names: UpgradeNames): string | undefined { const now = names.typeOf(id) const key = (field ?? '').split('.')[0].replace(/\[\d+\]$/, '') const has = (type?: string) => !!type && !!NODE_DEFS[type as keyof typeof NODE_DEFS]?.fields.some((f) => f.key === key) if (has(now)) return now const was = names.wasTypeOf(id) return has(was) ? was : now } /** 一项改动。改接的连线、新插入的节点、换了的类型各有写法,其余和发布前修复的预览一样 */ function UpgradeChangeRow({ change: c, names, showRule, showLabel }: { change: UpgradeChange; names: UpgradeNames; showRule: boolean; showLabel: boolean }) { const { nameOf } = names const kind = upgradeChangeKind(c) const T = UPGRADE_TEXT const where = c.node_title || (c.node_id ? nameOf(c.node_id) ?? c.node_id : '') || PUBLISH_FIX_TEXT.whole const arrow = let body: React.ReactNode if (kind === 'type') { body = ( <> 「{where}」 · {T.typeField}: {upgradeTypeText(c.before)} {arrow} {upgradeTypeText(c.after)} ) } else if (kind === 'node') { body = <>{T.added}:{upgradeNodeText(c.after)} } else if (kind === 'edge') { // 连线本身带一个箭头,改接不再用箭头图标,写「改接为」,免得两种箭头混在一行里 const from = c.before != null ? upgradeEdgeText(c.before, nameOf) : '' const to = c.after != null ? upgradeEdgeText(c.after, nameOf) : '' body = from && to ? <>{T.edge}:{from} {T.rewired}{to} : to ? <>{T.addedEdge}:{to} : <>{T.removedEdge}:{from} } else { const field = fixFieldLabel(c.field, ownerType(c.field, c.node_id, names), names.configOf(c.node_id)) body = ( <> 「{where}」{field && ` · ${field}`}: {arrow} ) } return (
  • {showRule && c.rule && } {body} {showLabel && c.label && {c.label}}
  • ) } /** 预览里的一个值:整份契约一键一行,别的压成一行;节点引用写节点名。和发布前修复的写法相同 */ function UpgradeValue({ value, field, nameOf, className, ...data }: { value: unknown; field?: string | null; nameOf: NodeNameOf; className?: string 'data-upgrade-before'?: string; 'data-upgrade-after'?: string }) { const lines = fixValueLines(value, field, nameOf) if (!lines) return {fixValueText(value, field, nameOf)} return ( {lines.map((line, i) => {line})} ) } const SUB_LABEL: Record = { condition: '的条件', key: '的标识', tools: '的工具', system: '的系统提示', provider: PROVIDER_REF_TEXT.memberField, } /** 节点定义里没有、问题却会落上去的字段:点名的模型接入(多数节点的检查器里没有这一栏)。不写的话露出键名 */ const FIELD_FALLBACK: Record = { provider: PROVIDER_REF_TEXT.field } function fieldLabel(field: FieldRef | null | undefined, type?: string, config?: Record): string { if (!field) return '' if (field.key === 'label') return '节点名称' const def = type ? NODE_DEFS[type as keyof typeof NODE_DEFS] : undefined const base = def?.fields.find((f) => f.key === field.key)?.label ?? FIELD_FALLBACK[field.key] ?? field.key if (field.index == null) return base const sub = (field.sub && SUB_LABEL[field.sub]) || '' // 成员有名字就叫名字:「第 2 个成员」还得数,「成员 writer」一眼就知道是谁 const name = field.key === 'agents' ? config?.agents?.[field.index]?.name : undefined if (name) return `成员「${name}」${sub}` const item = ({ cases: '分支', fields: '字段', metrics: '指标', agents: '成员' } as Record)[field.key] return item ? `第 ${field.index + 1} 个${item}${sub}` : `${base} · 第 ${field.index + 1} 项` }