import { memo } from 'react' import { Handle, Position, type NodeProps } from '@xyflow/react' import { AlertTriangle, Check, Hand, Loader2, Sparkles, Wrench } from 'lucide-react' import clsx from 'clsx' import { NODE_DEFS, sourceHandles } from './nodeDefs' import { formatDuration } from '../run/decode' import { useStudio, type FlowNode } from '../store/studio' import type { NodeType } from '../types' /** 卡片正文的一行摘要:不用打开属性面板就能看懂这个节点在干什么。 */ function summarize(type: NodeType, config: Record): string { const first = (...vals: any[]) => vals.find((v) => typeof v === 'string' && v.trim())?.trim() ?? '' switch (type) { case 'input': return (config.fields ?? []).map((f: any) => f.name).filter(Boolean).join(' · ') || '未定义输入' case 'output': { const names = (config.fields ?? []).map((f: any) => f.name).filter(Boolean).join(' · ') return (config.contract ? '⚖ 出具契约 · ' : '') + (names || '未定义成果') } case 'llm': return first(config.prompt, config.system) || '未填提示' case 'agent': { const tools = (config.tools ?? []).length return `${first(config.prompt, config.system) || '未填任务'}${tools ? ` · ${tools} 个工具` : ''}` } case 'supervisor': return `${(config.agents ?? []).map((a: any) => a.name).join(' / ') || '未配成员'}` case 'tool': return config.tool ? `${config.tool}()` : '未选择工具' case 'code': return `${config.language ?? 'python'} · ${(config.code ?? '').split('\n')[0].slice(0, 48) || '空'}` case 'branch': return (config.cases ?? []).map((c: any) => c.key).filter(Boolean).join(' / ') || '未配分支' case 'loop': return config.mode === 'while' ? `当 ${config.condition || '?'} 时重复` : `遍历 ${config.items || '?'}` case 'retrieve': return `${config.collection || 'default'} · 取 ${config.limit ?? 5} 条` case 'memory': return `${{ recall: '回忆', write: '记住', clear: '清空' }[config.action as string] ?? ''} @ ${config.scope || 'default'}` case 'human': return first(config.title) || '等待人工' case 'validate': return 'JSON Schema 校验' + (config.repair_with_llm ? ' · 失败自动返工' : '') case 'metrics': return `${config.caliber || '口径'}@${config.caliber_version || 'v1'} · ${(config.metrics ?? []).length} 个指标` case 'transform': return first(config.expression, config.template) || '未配置' case 'subgraph': return config.workflow_id ? '嵌套工作流' : '未选择工作流' default: return '' } } function NodeCardImpl({ id, data, selected }: NodeProps) { const def = NODE_DEFS[data.nodeType] const runtime = useStudio((s) => s.runtime[id]) // selector 必须返回稳定引用:filter() 每次都造新数组,会触发无限重渲染 const hasError = useStudio((s) => s.issues.some((i) => i.node_id === id && i.level === 'error')) const copilotNew = useStudio((s) => s.copilotNew.includes(id)) const Icon = def?.icon ?? Sparkles const handles = sourceHandles(data.nodeType, data.config) const status = runtime?.status ?? 'idle' const streamed = runtime?.tokens || runtime?.thinking const summary = summarize(data.nodeType, data.config) return (
{def?.hasTarget && ( )} {/* 标题栏 */}
{data.label || def?.label}
{status === 'running' && } {status === 'done' && } {status === 'waiting' && } {status === 'failed' && } {status === 'idle' && hasError && ( )}
{/* 摘要 */} {summary && (
{summary}
)} {/* 运行态:实时流出来的内容 */} {streamed && status !== 'idle' && (
{runtime?.thinking && !runtime?.tokens && (
思考中
)}
{(runtime?.tokens || runtime?.thinking || '').slice(-180)}
)} {/* 工具调用 */} {!!runtime?.toolCalls?.length && (
{runtime.toolCalls.slice(-4).map((call, i) => ( {call.tool} ))}
)} {/* 底部状态条 */} {(runtime?.durationMs != null || runtime?.error) && (
{runtime.durationMs != null && ( {formatDuration(runtime.durationMs)} )} {runtime.error && ( {runtime.error} )}
)} {/* 出口:有名字的分支把标签显示出来,不用点开就知道哪条是哪条 */} {handles.map((handle, i) => { const top = handles.length === 1 ? '50%' : `${((i + 1) / (handles.length + 1)) * 100}%` return (
{handle.label && ( {handle.label} )}
) })}
) } // 耗时格式化原本在这里又写了一份(formatMs),和 decode.ts 的规则不一样: // 画布节点上显示"0ms",右边助手栏里同一步什么都不显示。两块并排放着, // 用户看到的是两套说法。统一用 decode.ts 那份。 export const NodeCard = memo(NodeCardImpl)