mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-26 08:12:02 +00:00
AgentLab:可视化 Agent 编排实验台
在画布上拖拽编排 agent 工作流,并实时看着它执行。 后端 FastAPI + LangGraph:把画布 JSON 编译成 StateGraph,15 种节点 (模型/Agent/多Agent、工具/沙箱、分支/循环/子图、记忆/检索/整形、 人工介入/结构校验)。选 LangGraph 是因为可视化编排真正难的是暂停、 恢复和回放,而 checkpointer + interrupt() 正好把这几件事做在了底层。 前端 React + React Flow:节点元数据驱动属性面板与连接桩,运行时 节点高亮、边流动画、卡片内流式 token 全部由同一份事件流驱动。 关键实现: - Agent 内工具审批用 @task 缓存模型调用,interrupt 重放时不重复计费 - 沙箱保持只读根文件系统,代码经 exec+base64 写入(put_archive 会被 只读容器拒绝),断网、非 root、内存/CPU/超时限额 - 检索为向量与 BM25 混合,本地哈希 embedding 零配置可用 - 分支条件走 AST 白名单求值器;HTTP 工具逐 IP 拦截内网与元数据地址 - Mock provider 让无 API Key 也能跑通完整链路 7 个内置模板全部冒烟通过,含真实模型端到端验证。
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { useCallback, useMemo, useRef } from 'react'
|
||||
import {
|
||||
Background, BackgroundVariant, Controls, MiniMap, ReactFlow, ReactFlowProvider,
|
||||
useReactFlow, type Edge,
|
||||
} from '@xyflow/react'
|
||||
import { NodeCard } from './NodeCard'
|
||||
import { useStudio } from '../store/studio'
|
||||
import type { NodeType } from '../types'
|
||||
|
||||
const nodeTypes = { card: NodeCard }
|
||||
|
||||
const NODE_COLOR: Record<string, string> = {
|
||||
input: '#3fb950', output: '#a371f7', llm: '#4f8cff', agent: '#6366f1',
|
||||
supervisor: '#db61a2', tool: '#f0883e', code: '#2dd4bf', branch: '#d29922',
|
||||
loop: '#ec4899', human: '#f85149', validate: '#84cc16', memory: '#14b8a6',
|
||||
retrieve: '#0ea5e9', transform: '#8b949e', subgraph: '#bc8cff',
|
||||
}
|
||||
|
||||
function CanvasInner() {
|
||||
const wrapper = useRef<HTMLDivElement>(null)
|
||||
const { screenToFlowPosition } = useReactFlow()
|
||||
const nodes = useStudio((s) => s.nodes)
|
||||
const edges = useStudio((s) => s.edges)
|
||||
const activeEdges = useStudio((s) => s.activeEdges)
|
||||
const { onNodesChange, onEdgesChange, onConnect, addNode, select } = useStudio()
|
||||
|
||||
// 正在流动的边加动画,让"数据走到哪了"看得见
|
||||
const decorated = useMemo<Edge[]>(
|
||||
() =>
|
||||
edges.map((e) =>
|
||||
activeEdges.includes(e.id)
|
||||
? { ...e, className: 'edge-active', animated: true }
|
||||
: { ...e, className: undefined, animated: false },
|
||||
),
|
||||
[edges, activeEdges],
|
||||
)
|
||||
|
||||
const onDrop = useCallback(
|
||||
(event: React.DragEvent) => {
|
||||
event.preventDefault()
|
||||
const type = event.dataTransfer.getData('application/agentlab-node') as NodeType
|
||||
if (!type) return
|
||||
addNode(type, screenToFlowPosition({ x: event.clientX, y: event.clientY }))
|
||||
},
|
||||
[addNode, screenToFlowPosition],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="h-full w-full" ref={wrapper}>
|
||||
<ReactFlow
|
||||
nodes={nodes}
|
||||
edges={decorated}
|
||||
nodeTypes={nodeTypes}
|
||||
onNodesChange={onNodesChange}
|
||||
onEdgesChange={onEdgesChange}
|
||||
onConnect={onConnect}
|
||||
onNodeClick={(_, node) => select(node.id)}
|
||||
onPaneClick={() => select(null)}
|
||||
onDrop={onDrop}
|
||||
onDragOver={(e) => {
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = 'move'
|
||||
}}
|
||||
fitView
|
||||
fitViewOptions={{ padding: 0.12, maxZoom: 1.1, minZoom: 0.5 }}
|
||||
minZoom={0.2}
|
||||
maxZoom={2}
|
||||
defaultEdgeOptions={{ type: 'smoothstep' }}
|
||||
proOptions={{ hideAttribution: true }}
|
||||
deleteKeyCode={['Backspace', 'Delete']}
|
||||
>
|
||||
<Background variant={BackgroundVariant.Dots} gap={18} size={1} color="var(--canvas-dot)" />
|
||||
<Controls showInteractive={false} />
|
||||
<MiniMap
|
||||
pannable
|
||||
zoomable
|
||||
maskColor="color-mix(in srgb, var(--bg) 82%, transparent)"
|
||||
nodeColor={(n) => NODE_COLOR[(n.data as any)?.nodeType as string] ?? '#666'}
|
||||
style={{ width: 140, height: 96 }}
|
||||
/>
|
||||
</ReactFlow>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function FlowCanvas() {
|
||||
return (
|
||||
<ReactFlowProvider>
|
||||
<CanvasInner />
|
||||
</ReactFlowProvider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,447 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Copy, Plus, Settings2, Trash2, X } from 'lucide-react'
|
||||
import clsx from 'clsx'
|
||||
import { NODE_DEFS, type FieldDef } from './nodeDefs'
|
||||
import { useStudio } from '../store/studio'
|
||||
import { useCatalog, modelOptions } from '../store/catalog'
|
||||
import { JsonInput } from '../components/ui'
|
||||
|
||||
/** 属性面板。所有字段都由 nodeDefs 的声明驱动渲染,加节点类型不用改这里。 */
|
||||
export function Inspector() {
|
||||
const selectedId = useStudio((s) => s.selectedId)
|
||||
const node = useStudio((s) => s.nodes.find((n) => n.id === s.selectedId))
|
||||
const allIssues = useStudio((s) => s.issues)
|
||||
const issues = useMemo(
|
||||
() => allIssues.filter((i) => i.node_id === selectedId),
|
||||
[allIssues, selectedId],
|
||||
)
|
||||
const runtime = useStudio((s) => (selectedId ? s.runtime[selectedId] : undefined))
|
||||
const { updateNode, removeNode, duplicateNode, select } = useStudio()
|
||||
const [showAdvanced, setShowAdvanced] = useState(false)
|
||||
|
||||
if (!node) {
|
||||
return (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-2 p-6 text-center">
|
||||
<Settings2 size={22} className="text-faint opacity-40" />
|
||||
<div className="text-xs text-faint">选中一个节点来编辑它</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const def = NODE_DEFS[node.data.nodeType]
|
||||
const config = node.data.config ?? {}
|
||||
const setConfig = (key: string, value: any) =>
|
||||
updateNode(node.id, { config: { ...config, [key]: value } })
|
||||
|
||||
const visible = def.fields.filter((f) => !f.when || f.when(config))
|
||||
const basic = visible.filter((f) => !f.advanced)
|
||||
const advanced = visible.filter((f) => f.advanced)
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<div className="flex items-center gap-2 border-b px-3 py-2">
|
||||
<def.icon size={13} style={{ color: 'var(--nt)' }} className={`nt-${node.data.nodeType}`} />
|
||||
<span className="flex-1 truncate text-xs font-semibold">{def.label}</span>
|
||||
<button className="btn btn-ghost btn-sm" title="复制节点" onClick={() => duplicateNode(node.id)}>
|
||||
<Copy size={12} />
|
||||
</button>
|
||||
<button className="btn btn-ghost btn-sm" title="删除节点" onClick={() => removeNode(node.id)}>
|
||||
<Trash2 size={12} className="text-[var(--err)]" />
|
||||
</button>
|
||||
<button className="btn btn-ghost btn-sm" onClick={() => select(null)}>
|
||||
<X size={12} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-3">
|
||||
<div className="mb-3 text-[11px] leading-relaxed text-faint">{def.description}</div>
|
||||
|
||||
{!!issues.length && (
|
||||
<div className="mb-3 space-y-1">
|
||||
{issues.map((issue, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className="rounded border px-2 py-1.5 text-[11px] leading-snug"
|
||||
style={{
|
||||
borderColor: issue.level === 'error' ? 'var(--err)' : 'var(--warn)',
|
||||
color: issue.level === 'error' ? 'var(--err)' : 'var(--warn)',
|
||||
}}
|
||||
>
|
||||
{issue.message}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mb-3">
|
||||
<label className="label">节点名称</label>
|
||||
<input
|
||||
className="field"
|
||||
value={node.data.label ?? ''}
|
||||
onChange={(e) => updateNode(node.id, { label: e.target.value })}
|
||||
placeholder={def.label}
|
||||
/>
|
||||
<div className="mt-1 text-[10px] text-faint">
|
||||
ID: <code className="mono">{node.id}</code> · 下游用 {`{{ nodes.${node.id}.text }}`} 引用
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{basic.map((field) => (
|
||||
<Field key={field.key} field={field} value={config[field.key]} onChange={(v) => setConfig(field.key, v)} />
|
||||
))}
|
||||
|
||||
{!!advanced.length && (
|
||||
<div className="mt-3 border-t pt-3">
|
||||
<button
|
||||
className="mb-2 text-[11px] text-faint hover:text-dim"
|
||||
onClick={() => setShowAdvanced(!showAdvanced)}
|
||||
>
|
||||
{showAdvanced ? '▾' : '▸'} 高级选项({advanced.length})
|
||||
</button>
|
||||
{showAdvanced &&
|
||||
advanced.map((field) => (
|
||||
<Field key={field.key} field={field} value={config[field.key]} onChange={(v) => setConfig(field.key, v)} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{runtime?.preview != null && (
|
||||
<div className="mt-4 border-t pt-3">
|
||||
<div className="label">上次运行输出</div>
|
||||
<pre className="mono max-h-60 overflow-auto rounded border bg-bg p-2 text-[10.5px] leading-relaxed whitespace-pre-wrap break-words">
|
||||
{typeof runtime.preview === 'string'
|
||||
? runtime.preview
|
||||
: JSON.stringify(runtime.preview, null, 2)}
|
||||
</pre>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
function Field({ field, value, onChange }: {
|
||||
field: FieldDef; value: any; onChange: (v: any) => void
|
||||
}) {
|
||||
return (
|
||||
<div className="mb-3">
|
||||
{field.type !== 'switch' && <label className="label">{field.label}</label>}
|
||||
<FieldInput field={field} value={value} onChange={onChange} />
|
||||
{field.help && field.type !== 'switch' && (
|
||||
<div className="mt-1 text-[10px] leading-snug text-faint">{field.help}</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldInput({ field, value, onChange }: {
|
||||
field: FieldDef; value: any; onChange: (v: any) => void
|
||||
}) {
|
||||
const catalog = useCatalog()
|
||||
|
||||
switch (field.type) {
|
||||
case 'text':
|
||||
return (
|
||||
<input className="field" value={value ?? ''} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value)} />
|
||||
)
|
||||
|
||||
case 'number':
|
||||
return (
|
||||
<input
|
||||
className="field" type="number" value={value ?? ''} min={field.min} max={field.max}
|
||||
step={field.step ?? 1} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value === '' ? undefined : Number(e.target.value))}
|
||||
/>
|
||||
)
|
||||
|
||||
case 'textarea':
|
||||
case 'prompt':
|
||||
return (
|
||||
<textarea
|
||||
className={clsx('field', field.type === 'prompt' && 'mono text-[11.5px]')}
|
||||
rows={field.type === 'prompt' ? 4 : 3}
|
||||
value={value ?? ''} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
)
|
||||
|
||||
case 'code':
|
||||
return (
|
||||
<textarea
|
||||
className="field mono text-[11px]" rows={10} spellCheck={false}
|
||||
value={value ?? ''} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
)
|
||||
|
||||
case 'select':
|
||||
return (
|
||||
<select className="field" value={value ?? ''} onChange={(e) => onChange(e.target.value)}>
|
||||
{(field.options ?? []).map((o) => (
|
||||
<option key={o.value} value={o.value}>{o.label}</option>
|
||||
))}
|
||||
</select>
|
||||
)
|
||||
|
||||
case 'switch':
|
||||
return (
|
||||
<label className="flex cursor-pointer items-center gap-2 py-0.5">
|
||||
<input type="checkbox" checked={!!value} onChange={(e) => onChange(e.target.checked)}
|
||||
className="accent-[var(--accent)]" />
|
||||
<span className="text-xs">{field.label}</span>
|
||||
{field.help && <span className="text-[10px] text-faint">· {field.help}</span>}
|
||||
</label>
|
||||
)
|
||||
|
||||
case 'json':
|
||||
return <JsonInput value={value} onChange={onChange} placeholder={field.placeholder} />
|
||||
|
||||
case 'model': {
|
||||
const options = modelOptions(catalog.providers)
|
||||
const groups = [...new Set(options.map((o) => o.group))]
|
||||
return (
|
||||
<select className="field" value={value ?? ''} onChange={(e) => onChange(e.target.value)}>
|
||||
<option value="">默认(用第一个可用 provider)</option>
|
||||
{groups.map((g) => (
|
||||
<optgroup key={g} label={g}>
|
||||
{options.filter((o) => o.group === g).map((o) => (
|
||||
<option key={g + o.value} value={o.value}>{o.label}</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
)
|
||||
}
|
||||
|
||||
case 'collection':
|
||||
return (
|
||||
<input className="field" list="kb-collections" value={value ?? ''}
|
||||
placeholder="default" onChange={(e) => onChange(e.target.value)} />
|
||||
)
|
||||
|
||||
case 'skills':
|
||||
return (
|
||||
<MultiPick
|
||||
options={catalog.skills.filter((s) => s.enabled).map((s) => ({ value: s.name, label: s.name, hint: s.description }))}
|
||||
value={Array.isArray(value) ? value : []}
|
||||
onChange={onChange}
|
||||
empty="还没有 Skill,去「方法论」页创建"
|
||||
/>
|
||||
)
|
||||
|
||||
case 'tools': {
|
||||
const options = catalog.tools.map((t) => ({
|
||||
value: t.id, label: t.name, hint: t.description, danger: t.dangerous, group: t.category,
|
||||
}))
|
||||
// tool 节点只选一个,agent 节点可以多选
|
||||
if (field.help?.includes('只能选一个')) {
|
||||
return (
|
||||
<select className="field" value={value ?? ''} onChange={(e) => onChange(e.target.value)}>
|
||||
<option value="">— 选择工具 —</option>
|
||||
{[...new Set(options.map((o) => o.group))].map((g) => (
|
||||
<optgroup key={g} label={g}>
|
||||
{options.filter((o) => o.group === g).map((o) => (
|
||||
<option key={o.value} value={o.value}>{o.label}{o.danger ? ' ⚠' : ''}</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<MultiPick options={options} value={Array.isArray(value) ? value : []} onChange={onChange}
|
||||
empty="没有可用工具" />
|
||||
)
|
||||
}
|
||||
|
||||
case 'ioFields':
|
||||
return <IoFieldList value={value ?? []} onChange={onChange} />
|
||||
|
||||
case 'cases':
|
||||
return <CaseList value={value ?? []} onChange={onChange} />
|
||||
|
||||
case 'agents':
|
||||
return <AgentList value={value ?? []} onChange={onChange} />
|
||||
|
||||
default:
|
||||
return <input className="field" value={value ?? ''} onChange={(e) => onChange(e.target.value)} />
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 复合编辑器
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
function MultiPick({ options, value, onChange, empty }: {
|
||||
options: { value: string; label: string; hint?: string; danger?: boolean; group?: string }[]
|
||||
value: string[]; onChange: (v: string[]) => void; empty: string
|
||||
}) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [query, setQuery] = useState('')
|
||||
const toggle = (v: string) =>
|
||||
onChange(value.includes(v) ? value.filter((x) => x !== v) : [...value, v])
|
||||
|
||||
const filtered = options.filter(
|
||||
(o) => !query || o.label.toLowerCase().includes(query.toLowerCase()) ||
|
||||
(o.hint ?? '').toLowerCase().includes(query.toLowerCase()),
|
||||
)
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="mb-1.5 flex flex-wrap gap-1">
|
||||
{value.map((v) => {
|
||||
const opt = options.find((o) => o.value === v)
|
||||
return (
|
||||
<span key={v} className="chip" style={{ borderColor: 'var(--accent)', color: 'var(--accent)' }}>
|
||||
{opt?.label ?? v}
|
||||
<button onClick={() => toggle(v)} className="ml-0.5 hover:opacity-60"><X size={9} /></button>
|
||||
</span>
|
||||
)
|
||||
})}
|
||||
{!value.length && <span className="text-[10.5px] text-faint">未选择</span>}
|
||||
</div>
|
||||
<button className="btn btn-sm w-full justify-center" onClick={() => setOpen(!open)}>
|
||||
<Plus size={11} /> {open ? '收起' : '添加'}
|
||||
</button>
|
||||
{open && (
|
||||
<div className="mt-1.5 rounded border bg-bg">
|
||||
<input className="field rounded-b-none border-0 border-b" placeholder="搜索…"
|
||||
value={query} onChange={(e) => setQuery(e.target.value)} autoFocus />
|
||||
<div className="max-h-52 overflow-y-auto p-1">
|
||||
{!filtered.length && <div className="px-2 py-3 text-center text-[10.5px] text-faint">{empty}</div>}
|
||||
{filtered.map((o) => (
|
||||
<button
|
||||
key={o.value}
|
||||
onClick={() => toggle(o.value)}
|
||||
className={clsx(
|
||||
'flex w-full items-start gap-2 rounded px-2 py-1.5 text-left hover:bg-hover',
|
||||
value.includes(o.value) && 'bg-hover',
|
||||
)}
|
||||
>
|
||||
<input type="checkbox" readOnly checked={value.includes(o.value)}
|
||||
className="mt-0.5 accent-[var(--accent)]" />
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block text-[11.5px]">
|
||||
{o.label}
|
||||
{o.danger && <span className="ml-1 text-[9px] text-[var(--warn)]">需确认</span>}
|
||||
</span>
|
||||
{o.hint && <span className="block truncate text-[10px] text-faint">{o.hint}</span>}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Row({ children, onRemove }: { children: React.ReactNode; onRemove: () => void }) {
|
||||
return (
|
||||
<div className="mb-1.5 rounded border bg-bg p-2">
|
||||
<div className="flex items-start gap-1.5">
|
||||
<div className="min-w-0 flex-1 space-y-1.5">{children}</div>
|
||||
<button className="btn btn-ghost btn-sm shrink-0" onClick={onRemove}>
|
||||
<Trash2 size={11} className="text-[var(--err)]" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function IoFieldList({ value, onChange }: { value: any[]; onChange: (v: any[]) => void }) {
|
||||
const update = (i: number, patch: any) =>
|
||||
onChange(value.map((f, idx) => (idx === i ? { ...f, ...patch } : f)))
|
||||
// 有 value 的是成果字段(output),有 required 的是输入字段
|
||||
const isOutput = value.some((f) => 'value' in f)
|
||||
|
||||
return (
|
||||
<div>
|
||||
{value.map((field, i) => (
|
||||
<Row key={i} onRemove={() => onChange(value.filter((_, idx) => idx !== i))}>
|
||||
<input className="field" placeholder="字段名" value={field.name ?? ''}
|
||||
onChange={(e) => update(i, { name: e.target.value })} />
|
||||
{isOutput ? (
|
||||
<input className="field mono text-[11px]" placeholder="{{ vars.xxx }}"
|
||||
value={field.value ?? ''} onChange={(e) => update(i, { value: e.target.value })} />
|
||||
) : (
|
||||
<>
|
||||
<input className="field" placeholder="说明(可选)" value={field.description ?? ''}
|
||||
onChange={(e) => update(i, { description: e.target.value })} />
|
||||
<div className="flex items-center gap-3">
|
||||
<label className="flex items-center gap-1.5 text-[11px]">
|
||||
<input type="checkbox" checked={!!field.required} className="accent-[var(--accent)]"
|
||||
onChange={(e) => update(i, { required: e.target.checked })} />
|
||||
必填
|
||||
</label>
|
||||
<input className="field flex-1" placeholder="默认值" value={field.default ?? ''}
|
||||
onChange={(e) => update(i, { default: e.target.value })} />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Row>
|
||||
))}
|
||||
<button className="btn btn-sm w-full justify-center"
|
||||
onClick={() => onChange([...value, isOutput ? { name: '', value: '' } : { name: '', required: false }])}>
|
||||
<Plus size={11} /> 添加字段
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function CaseList({ value, onChange }: { value: any[]; onChange: (v: any[]) => void }) {
|
||||
const update = (i: number, patch: any) =>
|
||||
onChange(value.map((c, idx) => (idx === i ? { ...c, ...patch } : c)))
|
||||
return (
|
||||
<div>
|
||||
{value.map((c, i) => (
|
||||
<Row key={i} onRemove={() => onChange(value.filter((_, idx) => idx !== i))}>
|
||||
<input className="field" placeholder="分支标识(会成为连线出口)" value={c.key ?? ''}
|
||||
onChange={(e) => update(i, { key: e.target.value })} />
|
||||
<input className="field mono text-[11px]" placeholder="条件,如 len(vars.text) > 100"
|
||||
value={c.condition ?? ''} onChange={(e) => update(i, { condition: e.target.value })} />
|
||||
<input className="field" placeholder="说明(模型分类时作为类别描述)" value={c.label ?? ''}
|
||||
onChange={(e) => update(i, { label: e.target.value })} />
|
||||
</Row>
|
||||
))}
|
||||
<button className="btn btn-sm w-full justify-center"
|
||||
onClick={() => onChange([...value, { key: '', condition: '', label: '' }])}>
|
||||
<Plus size={11} /> 添加分支
|
||||
</button>
|
||||
<div className="mt-1.5 text-[10px] leading-snug text-faint">
|
||||
所有条件都不满足时走 default 出口,记得连一条
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function AgentList({ value, onChange }: { value: any[]; onChange: (v: any[]) => void }) {
|
||||
const catalog = useCatalog()
|
||||
const update = (i: number, patch: any) =>
|
||||
onChange(value.map((a, idx) => (idx === i ? { ...a, ...patch } : a)))
|
||||
return (
|
||||
<div>
|
||||
{value.map((agent, i) => (
|
||||
<Row key={i} onRemove={() => onChange(value.filter((_, idx) => idx !== i))}>
|
||||
<input className="field" placeholder="成员名(英文,如 researcher)" value={agent.name ?? ''}
|
||||
onChange={(e) => update(i, { name: e.target.value })} />
|
||||
<input className="field" placeholder="职责一句话(调度者据此分派)" value={agent.description ?? ''}
|
||||
onChange={(e) => update(i, { description: e.target.value })} />
|
||||
<textarea className="field" rows={2} placeholder="角色设定 system prompt"
|
||||
value={agent.system ?? ''} onChange={(e) => update(i, { system: e.target.value })} />
|
||||
<MultiPick
|
||||
options={catalog.tools.map((t) => ({ value: t.id, label: t.name, hint: t.description, group: t.category }))}
|
||||
value={agent.tools ?? []} onChange={(v) => update(i, { tools: v })} empty="没有可用工具"
|
||||
/>
|
||||
</Row>
|
||||
))}
|
||||
<button className="btn btn-sm w-full justify-center"
|
||||
onClick={() => onChange([...value, { name: '', description: '', system: '', tools: [] }])}>
|
||||
<Plus size={11} /> 添加成员
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
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 { useStudio, type FlowNode } from '../store/studio'
|
||||
import type { NodeType } from '../types'
|
||||
|
||||
/** 卡片正文的一行摘要:不用打开属性面板就能看懂这个节点在干什么。 */
|
||||
function summarize(type: NodeType, config: Record<string, any>): 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':
|
||||
return (config.fields ?? []).map((f: any) => f.name).filter(Boolean).join(' · ') || '未定义成果'
|
||||
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 'transform':
|
||||
return first(config.expression, config.template) || '未配置'
|
||||
case 'subgraph':
|
||||
return config.workflow_id ? '嵌套工作流' : '未选择工作流'
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function NodeCardImpl({ id, data, selected }: NodeProps<FlowNode>) {
|
||||
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 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 (
|
||||
<div
|
||||
className={clsx(
|
||||
`nt-${data.nodeType} group relative rounded-lg border bg-elev transition-shadow`,
|
||||
status === 'running' && 'node-running',
|
||||
status === 'done' && 'node-done',
|
||||
status === 'failed' && 'node-failed',
|
||||
status === 'waiting' && 'node-waiting',
|
||||
selected && 'ring-2 ring-[var(--accent)] ring-offset-1 ring-offset-[var(--bg)]',
|
||||
)}
|
||||
style={{ width: 238, borderColor: selected ? 'var(--accent)' : undefined }}
|
||||
>
|
||||
{def?.hasTarget && (
|
||||
<Handle type="target" position={Position.Left} style={{ left: -5 }} />
|
||||
)}
|
||||
|
||||
{/* 标题栏 */}
|
||||
<div className="flex items-center gap-2 rounded-t-lg px-2.5 py-2"
|
||||
style={{ background: 'color-mix(in srgb, var(--nt) 9%, transparent)' }}>
|
||||
<div
|
||||
className="flex h-5 w-5 shrink-0 items-center justify-center rounded"
|
||||
style={{ background: 'color-mix(in srgb, var(--nt) 20%, transparent)', color: 'var(--nt)' }}
|
||||
>
|
||||
<Icon size={12} />
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="truncate text-[12px] font-medium leading-tight">
|
||||
{data.label || def?.label}
|
||||
</div>
|
||||
</div>
|
||||
{status === 'running' && <Loader2 size={12} className="animate-spin" style={{ color: 'var(--nt)' }} />}
|
||||
{status === 'done' && <Check size={12} className="text-[var(--ok)]" />}
|
||||
{status === 'waiting' && <Hand size={12} className="text-[var(--warn)]" />}
|
||||
{status === 'failed' && <AlertTriangle size={12} className="text-[var(--err)]" />}
|
||||
{status === 'idle' && hasError && (
|
||||
<AlertTriangle size={12} className="text-[var(--err)] opacity-70" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 摘要 */}
|
||||
{summary && (
|
||||
<div className="px-2.5 pb-2 pt-1.5 text-[11px] leading-snug text-dim">
|
||||
<div className="line-clamp-2 break-words">{summary}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 运行态:实时流出来的内容 */}
|
||||
{streamed && status !== 'idle' && (
|
||||
<div className="mx-2 mb-2 max-h-20 overflow-hidden rounded border bg-bg px-2 py-1.5">
|
||||
{runtime?.thinking && !runtime?.tokens && (
|
||||
<div className="mb-1 text-[9.5px] uppercase tracking-wide text-faint">思考中</div>
|
||||
)}
|
||||
<div className="mono whitespace-pre-wrap break-words text-[10px] leading-relaxed text-dim">
|
||||
{(runtime?.tokens || runtime?.thinking || '').slice(-180)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 工具调用 */}
|
||||
{!!runtime?.toolCalls?.length && (
|
||||
<div className="mx-2 mb-2 flex flex-wrap gap-1">
|
||||
{runtime.toolCalls.slice(-4).map((call, i) => (
|
||||
<span
|
||||
key={i}
|
||||
className="chip"
|
||||
style={{
|
||||
borderColor: call.ok === false ? 'var(--err)' : undefined,
|
||||
color: call.ok === undefined ? 'var(--accent)' : undefined,
|
||||
}}
|
||||
title={call.result?.slice(0, 300)}
|
||||
>
|
||||
<Wrench size={8} />
|
||||
{call.tool}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 底部状态条 */}
|
||||
{(runtime?.durationMs != null || runtime?.error) && (
|
||||
<div className="flex items-center gap-2 border-t px-2.5 py-1">
|
||||
{runtime.durationMs != null && (
|
||||
<span className="text-[10px] text-faint">{formatMs(runtime.durationMs)}</span>
|
||||
)}
|
||||
{runtime.error && (
|
||||
<span className="truncate text-[10px] text-[var(--err)]" title={runtime.error}>
|
||||
{runtime.error}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 出口:有名字的分支把标签显示出来,不用点开就知道哪条是哪条 */}
|
||||
{handles.map((handle, i) => {
|
||||
const top = handles.length === 1 ? '50%' : `${((i + 1) / (handles.length + 1)) * 100}%`
|
||||
return (
|
||||
<div key={handle.id}>
|
||||
<Handle
|
||||
id={handle.id}
|
||||
type="source"
|
||||
position={Position.Right}
|
||||
style={{ top, right: -5, borderColor: handle.color }}
|
||||
/>
|
||||
{handle.label && (
|
||||
<span
|
||||
className="pointer-events-none absolute left-full ml-2 -translate-y-1/2 whitespace-nowrap text-[9.5px]"
|
||||
style={{ top, color: handle.color ?? 'var(--text-faint)' }}
|
||||
>
|
||||
{handle.label}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function formatMs(ms: number): string {
|
||||
if (ms < 1000) return `${ms}ms`
|
||||
if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`
|
||||
return `${Math.floor(ms / 60_000)}m${Math.round((ms % 60_000) / 1000)}s`
|
||||
}
|
||||
|
||||
export const NodeCard = memo(NodeCardImpl)
|
||||
@@ -0,0 +1,87 @@
|
||||
import { useState } from 'react'
|
||||
import { NODE_CATEGORIES, NODE_DEFS } from './nodeDefs'
|
||||
import { useStudio } from '../store/studio'
|
||||
import type { NodeType } from '../types'
|
||||
|
||||
/** 左侧节点库。拖到画布,或点一下放在视野中间。 */
|
||||
export function Palette() {
|
||||
const [query, setQuery] = useState('')
|
||||
const addNode = useStudio((s) => s.addNode)
|
||||
|
||||
const defs = Object.values(NODE_DEFS).filter(
|
||||
(d) =>
|
||||
!query ||
|
||||
d.label.includes(query) ||
|
||||
d.description.includes(query) ||
|
||||
d.type.includes(query.toLowerCase()),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<div className="border-b p-2">
|
||||
<input
|
||||
className="field"
|
||||
placeholder="搜索节点…"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex-1 overflow-y-auto p-2">
|
||||
{NODE_CATEGORIES.map((category) => {
|
||||
const items = defs.filter((d) => d.category === category)
|
||||
if (!items.length) return null
|
||||
return (
|
||||
<div key={category} className="mb-3">
|
||||
<div className="mb-1.5 px-1 text-[10px] font-semibold uppercase tracking-wider text-faint">
|
||||
{category}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
{items.map((def) => (
|
||||
<div
|
||||
key={def.type}
|
||||
draggable
|
||||
onDragStart={(e) => {
|
||||
e.dataTransfer.setData('application/agentlab-node', def.type)
|
||||
e.dataTransfer.effectAllowed = 'move'
|
||||
}}
|
||||
onClick={() =>
|
||||
addNode(def.type as NodeType, {
|
||||
x: 220 + Math.random() * 160,
|
||||
y: 160 + Math.random() * 200,
|
||||
})
|
||||
}
|
||||
className={`nt-${def.type} group flex cursor-grab items-start gap-2 rounded-md border border-transparent px-2 py-1.5 hover:border-[var(--border)] hover:bg-hover active:cursor-grabbing`}
|
||||
title={def.description}
|
||||
>
|
||||
<div
|
||||
className="mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded"
|
||||
style={{
|
||||
background: 'color-mix(in srgb, var(--nt) 18%, transparent)',
|
||||
color: 'var(--nt)',
|
||||
}}
|
||||
>
|
||||
<def.icon size={12} />
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<div className="text-[11.5px] font-medium leading-tight">{def.label}</div>
|
||||
<div className="truncate text-[10px] leading-tight text-faint">
|
||||
{def.description}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{!defs.length && (
|
||||
<div className="px-2 py-6 text-center text-[11px] text-faint">没有匹配的节点</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="border-t px-3 py-2 text-[10px] leading-relaxed text-faint">
|
||||
拖到画布,或点击直接添加。<br />
|
||||
从节点右侧圆点拖到另一个节点左侧即可连线。
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
import {
|
||||
Bot, Braces, Brain, CheckCircle2, Code2, Database, FileInput, FileOutput,
|
||||
GitBranch, Hand, Repeat, Search, Shuffle, Users, Wrench,
|
||||
} from 'lucide-react'
|
||||
import type { NodeType } from '../types'
|
||||
|
||||
export type FieldType =
|
||||
| 'text' | 'textarea' | 'prompt' | 'code' | 'number' | 'select' | 'switch'
|
||||
| 'json' | 'model' | 'tools' | 'skills' | 'collection'
|
||||
| 'ioFields' | 'cases' | 'agents'
|
||||
|
||||
export interface FieldDef {
|
||||
key: string
|
||||
label: string
|
||||
type: FieldType
|
||||
placeholder?: string
|
||||
help?: string
|
||||
options?: { value: string; label: string }[]
|
||||
min?: number
|
||||
max?: number
|
||||
step?: number
|
||||
/** 只在满足条件时显示,避免面板一次糊一屏用不上的选项 */
|
||||
when?: (config: Record<string, any>) => boolean
|
||||
advanced?: boolean
|
||||
}
|
||||
|
||||
export interface HandleDef {
|
||||
id: string
|
||||
label: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
export interface NodeDef {
|
||||
type: NodeType
|
||||
label: string
|
||||
category: string
|
||||
description: string
|
||||
icon: typeof Bot
|
||||
hasTarget: boolean
|
||||
/** 静态出口;branch/loop 这类由配置动态生成的返回 null */
|
||||
sources: HandleDef[] | null
|
||||
fields: FieldDef[]
|
||||
defaults: Record<string, any>
|
||||
}
|
||||
|
||||
const MODEL_FIELDS: FieldDef[] = [
|
||||
{ key: 'model', label: '模型', type: 'model', help: '留空则用默认 provider' },
|
||||
{
|
||||
key: 'thinking', label: '思考模式', type: 'select', advanced: true,
|
||||
options: [
|
||||
{ value: '', label: '跟随模型默认' },
|
||||
{ value: 'summarized', label: '开启并显示摘要' },
|
||||
{ value: 'adaptive', label: '开启但不显示' },
|
||||
{ value: 'off', label: '关闭' },
|
||||
],
|
||||
help: 'Claude 4.6+ 默认会先思考再回答,思考也消耗 token',
|
||||
},
|
||||
{
|
||||
key: 'effort', label: '投入程度', type: 'select', advanced: true,
|
||||
options: [
|
||||
{ value: '', label: '默认' }, { value: 'low', label: 'low' },
|
||||
{ value: 'medium', label: 'medium' }, { value: 'high', label: 'high' },
|
||||
{ value: 'xhigh', label: 'xhigh' }, { value: 'max', label: 'max' },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'temperature', label: '温度', type: 'number', min: 0, max: 2, step: 0.1, advanced: true,
|
||||
help: 'Claude 4.6 之后的模型不支持,填了也会被自动忽略',
|
||||
},
|
||||
{ key: 'max_tokens', label: '最大输出 token', type: 'number', min: 1, advanced: true },
|
||||
]
|
||||
|
||||
const COMMON_TAIL: FieldDef[] = [
|
||||
{
|
||||
key: 'assign_to', label: '结果存为变量', type: 'text', placeholder: '例如 result',
|
||||
help: '下游用 {{ vars.变量名 }} 引用',
|
||||
},
|
||||
{ key: 'skip_if', label: '跳过条件', type: 'text', advanced: true, placeholder: 'vars.count == 0' },
|
||||
{ key: 'retries', label: '失败重试次数', type: 'number', min: 0, max: 5, advanced: true },
|
||||
{
|
||||
key: 'on_error', label: '出错时', type: 'select', advanced: true,
|
||||
options: [{ value: '', label: '中断整个运行' }, { value: 'continue', label: '记录错误并继续' }],
|
||||
},
|
||||
]
|
||||
|
||||
export const NODE_DEFS: Record<NodeType, NodeDef> = {
|
||||
input: {
|
||||
type: 'input', label: '输入', category: '起止', icon: FileInput,
|
||||
description: '工作流入口,声明需要哪些输入',
|
||||
hasTarget: false, sources: [{ id: 'out', label: '' }],
|
||||
fields: [{ key: 'fields', label: '输入字段', type: 'ioFields' }],
|
||||
defaults: { fields: [{ name: 'question', required: true }] },
|
||||
},
|
||||
output: {
|
||||
type: 'output', label: '成果', category: '起止', icon: FileOutput,
|
||||
description: '收集最终结构化成果',
|
||||
hasTarget: true, sources: [],
|
||||
fields: [{ key: 'fields', label: '成果字段', type: 'ioFields' }],
|
||||
defaults: { fields: [{ name: '结果', value: '{{ last_message }}' }] },
|
||||
},
|
||||
llm: {
|
||||
type: 'llm', label: '模型调用', category: '模型', icon: Brain,
|
||||
description: '单次 LLM 调用,可要求结构化输出',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'system', label: '系统提示', type: 'textarea', placeholder: '你是…' },
|
||||
{ key: 'prompt', label: '用户提示', type: 'prompt', placeholder: '{{ input.question }}' },
|
||||
{ key: 'skills', label: '挂载 Skill', type: 'skills' },
|
||||
...MODEL_FIELDS,
|
||||
{
|
||||
key: 'output_schema', label: '结构化输出 Schema', type: 'json', advanced: true,
|
||||
help: '填了就强制模型按此 JSON Schema 返回',
|
||||
},
|
||||
{ key: 'use_history', label: '带上对话历史', type: 'switch', advanced: true },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { system: '', prompt: '{{ input.question }}' },
|
||||
},
|
||||
agent: {
|
||||
type: 'agent', label: 'Agent', category: '模型', icon: Bot,
|
||||
description: '带工具循环,自己决定调什么工具',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'system', label: '角色设定', type: 'textarea' },
|
||||
{ key: 'prompt', label: '任务', type: 'prompt' },
|
||||
{ key: 'tools', label: '可用工具', type: 'tools' },
|
||||
{ key: 'skills', label: '挂载 Skill', type: 'skills' },
|
||||
{ key: 'max_steps', label: '最大步数', type: 'number', min: 1, max: 25 },
|
||||
{
|
||||
key: 'approval', label: '工具审批', type: 'select',
|
||||
options: [
|
||||
{ value: 'dangerous', label: '仅危险工具需要确认' },
|
||||
{ value: 'always', label: '每次调用都确认' },
|
||||
{ value: 'never', label: '全部自动放行' },
|
||||
],
|
||||
help: '需要确认时运行会暂停,等你在审批面板放行',
|
||||
},
|
||||
...MODEL_FIELDS,
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { max_steps: 8, approval: 'dangerous', tools: [] },
|
||||
},
|
||||
supervisor: {
|
||||
type: 'supervisor', label: '多 Agent 协作', category: '模型', icon: Users,
|
||||
description: '调度者按进展把任务分派给多个专家',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'goal', label: '团队目标', type: 'prompt' },
|
||||
{ key: 'agents', label: '团队成员', type: 'agents' },
|
||||
{ key: 'max_rounds', label: '最大轮次', type: 'number', min: 1, max: 25 },
|
||||
...MODEL_FIELDS,
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { max_rounds: 6, agents: [] },
|
||||
},
|
||||
tool: {
|
||||
type: 'tool', label: '调用工具', category: '执行', icon: Wrench,
|
||||
description: '直接调用一个指定工具',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'tool', label: '工具', type: 'tools', help: '只能选一个' },
|
||||
{ key: 'args', label: '参数', type: 'json', help: '值里可以用 {{ }} 引用上游' },
|
||||
{
|
||||
key: 'approval', label: '执行前确认', type: 'select',
|
||||
options: [
|
||||
{ value: 'dangerous', label: '危险工具需要确认' },
|
||||
{ value: 'always', label: '总是确认' },
|
||||
{ value: 'never', label: '不确认' },
|
||||
],
|
||||
},
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { args: {}, approval: 'dangerous' },
|
||||
},
|
||||
code: {
|
||||
type: 'code', label: '沙箱代码', category: '执行', icon: Code2,
|
||||
description: '在隔离容器里执行代码',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{
|
||||
key: 'language', label: '语言', type: 'select',
|
||||
options: [
|
||||
{ value: 'python', label: 'Python' }, { value: 'bash', label: 'Bash' },
|
||||
{ value: 'node', label: 'Node.js' },
|
||||
],
|
||||
},
|
||||
{ key: 'code', label: '代码', type: 'code', help: '代码里可以用 {{ }} 插值' },
|
||||
{ key: 'timeout', label: '超时(秒)', type: 'number', min: 1, max: 300 },
|
||||
{ key: 'memory_mb', label: '内存上限 MB', type: 'number', min: 64, max: 4096, advanced: true },
|
||||
{ key: 'network', label: '允许联网', type: 'switch', help: '默认断网' },
|
||||
{
|
||||
key: 'approval', label: '执行前确认', type: 'select', advanced: true,
|
||||
options: [{ value: 'never', label: '不确认' }, { value: 'always', label: '总是确认(可改代码)' }],
|
||||
},
|
||||
{ key: 'fail_fast', label: '执行失败即中断', type: 'switch', advanced: true },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { language: 'python', code: 'print("hello")', timeout: 30, network: false, fail_fast: true },
|
||||
},
|
||||
branch: {
|
||||
type: 'branch', label: '条件分支', category: '控制', icon: GitBranch,
|
||||
description: '按条件或语义分类走不同的路',
|
||||
hasTarget: true, sources: null,
|
||||
fields: [
|
||||
{
|
||||
key: 'mode', label: '判断方式', type: 'select',
|
||||
options: [
|
||||
{ value: 'expression', label: '表达式判断' },
|
||||
{ value: 'llm', label: '让模型分类' },
|
||||
],
|
||||
},
|
||||
{ key: 'cases', label: '分支', type: 'cases' },
|
||||
{
|
||||
key: 'input', label: '待分类内容', type: 'prompt',
|
||||
when: (c) => c.mode === 'llm', placeholder: '{{ last_message }}',
|
||||
},
|
||||
{ key: 'instruction', label: '分类说明', type: 'textarea', when: (c) => c.mode === 'llm' },
|
||||
...MODEL_FIELDS.filter((f) => f.key === 'model').map((f) => ({
|
||||
...f, when: (c: any) => c.mode === 'llm',
|
||||
})),
|
||||
...COMMON_TAIL.filter((f) => f.key !== 'assign_to'),
|
||||
],
|
||||
defaults: { mode: 'expression', cases: [{ key: 'yes', condition: '', label: '' }] },
|
||||
},
|
||||
loop: {
|
||||
type: 'loop', label: '循环', category: '控制', icon: Repeat,
|
||||
description: '遍历列表或按条件重复执行',
|
||||
hasTarget: true, sources: null,
|
||||
fields: [
|
||||
{
|
||||
key: 'mode', label: '循环方式', type: 'select',
|
||||
options: [
|
||||
{ value: 'foreach', label: '遍历列表' },
|
||||
{ value: 'while', label: '条件成立时重复' },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'items', label: '列表来源', type: 'text', when: (c) => c.mode !== 'while',
|
||||
placeholder: '{{ input.items }}',
|
||||
},
|
||||
{ key: 'item_var', label: '当前项变量名', type: 'text', when: (c) => c.mode !== 'while' },
|
||||
{
|
||||
key: 'condition', label: '继续条件', type: 'text', when: (c) => c.mode === 'while',
|
||||
placeholder: 'vars.done != true',
|
||||
},
|
||||
{ key: 'max_iterations', label: '最大迭代次数', type: 'number', min: 1, max: 100 },
|
||||
],
|
||||
defaults: { mode: 'foreach', item_var: 'item', max_iterations: 10 },
|
||||
},
|
||||
subgraph: {
|
||||
type: 'subgraph', label: '子工作流', category: '控制', icon: Braces,
|
||||
description: '把另一张工作流当成一个节点嵌进来',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'workflow_id', label: '工作流', type: 'select', options: [] },
|
||||
{ key: 'input', label: '传入参数', type: 'json' },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { input: {} },
|
||||
},
|
||||
memory: {
|
||||
type: 'memory', label: '长期记忆', category: '上下文', icon: Database,
|
||||
description: '读取或写入跨运行的长期记忆',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{
|
||||
key: 'action', label: '操作', type: 'select',
|
||||
options: [
|
||||
{ value: 'recall', label: '回忆(检索)' },
|
||||
{ value: 'write', label: '记住(写入)' },
|
||||
{ value: 'clear', label: '清空该作用域' },
|
||||
],
|
||||
},
|
||||
{ key: 'scope', label: '作用域', type: 'text', placeholder: 'default' },
|
||||
{ key: 'query', label: '回忆什么', type: 'prompt', when: (c) => c.action !== 'write' && c.action !== 'clear' },
|
||||
{ key: 'limit', label: '返回条数', type: 'number', min: 1, max: 20, when: (c) => c.action !== 'write' },
|
||||
{ key: 'content', label: '记住的内容', type: 'prompt', when: (c) => c.action === 'write' },
|
||||
{
|
||||
key: 'kind', label: '记忆类型', type: 'select', when: (c) => c.action === 'write',
|
||||
options: [
|
||||
{ value: 'fact', label: '事实' }, { value: 'preference', label: '偏好' },
|
||||
{ value: 'episode', label: '事件' },
|
||||
],
|
||||
},
|
||||
{ key: 'importance', label: '重要程度', type: 'number', min: 0, max: 1, step: 0.1, when: (c) => c.action === 'write' },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { action: 'recall', scope: 'default', limit: 5, query: '{{ last_message }}' },
|
||||
},
|
||||
retrieve: {
|
||||
type: 'retrieve', label: '知识检索', category: '上下文', icon: Search,
|
||||
description: '从知识库里混合检索相关片段',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'query', label: '检索问题', type: 'prompt' },
|
||||
{ key: 'collection', label: '知识库', type: 'collection' },
|
||||
{ key: 'limit', label: '返回片段数', type: 'number', min: 1, max: 20 },
|
||||
{
|
||||
key: 'alpha', label: '向量 vs 关键词', type: 'number', min: 0, max: 1, step: 0.1,
|
||||
help: '1 = 纯语义相似,0 = 纯关键词匹配',
|
||||
},
|
||||
{ key: 'min_score', label: '最低分数', type: 'number', min: 0, max: 1, step: 0.05, advanced: true },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { query: '{{ last_message }}', collection: 'default', limit: 5, alpha: 0.5 },
|
||||
},
|
||||
transform: {
|
||||
type: 'transform', label: '数据整形', category: '上下文', icon: Shuffle,
|
||||
description: '不调模型,直接把数据揉成下游要的形状',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{
|
||||
key: 'mode', label: '方式', type: 'select',
|
||||
options: [
|
||||
{ value: 'template', label: '文本模板' },
|
||||
{ value: 'expression', label: '表达式' },
|
||||
{ value: 'json', label: 'JSON 模板' },
|
||||
],
|
||||
},
|
||||
{ key: 'template', label: '模板', type: 'textarea', when: (c) => c.mode !== 'expression' },
|
||||
{
|
||||
key: 'expression', label: '表达式', type: 'text', when: (c) => c.mode === 'expression',
|
||||
placeholder: "len(vars.items)",
|
||||
},
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { mode: 'template', template: '{{ last_message }}' },
|
||||
},
|
||||
human: {
|
||||
type: 'human', label: '人工介入', category: '把关', icon: Hand,
|
||||
description: '暂停运行,等人确认或补充信息',
|
||||
hasTarget: true, sources: null,
|
||||
fields: [
|
||||
{
|
||||
key: 'mode', label: '介入方式', type: 'select',
|
||||
options: [
|
||||
{ value: 'approve', label: '批准 / 驳回' },
|
||||
{ value: 'input', label: '补充输入' },
|
||||
{ value: 'edit', label: '编辑草稿' },
|
||||
],
|
||||
},
|
||||
{ key: 'title', label: '标题', type: 'text' },
|
||||
{ key: 'message', label: '给人看的内容', type: 'prompt' },
|
||||
{ key: 'draft', label: '草稿内容', type: 'prompt', when: (c) => c.mode === 'edit' },
|
||||
{ key: 'stop_on_reject', label: '驳回即终止运行', type: 'switch', when: (c) => c.mode === 'approve' },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { mode: 'approve', title: '需要你确认', message: '{{ last_message }}' },
|
||||
},
|
||||
validate: {
|
||||
type: 'validate', label: '结构校验', category: '把关', icon: CheckCircle2,
|
||||
description: '按 JSON Schema 校验,不合格可让模型自动返工',
|
||||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||||
fields: [
|
||||
{ key: 'source', label: '待校验内容', type: 'prompt', placeholder: '{{ last_message }}' },
|
||||
{ key: 'schema', label: 'JSON Schema', type: 'json' },
|
||||
{ key: 'max_retries', label: '最多返工次数', type: 'number', min: 0, max: 5 },
|
||||
{ key: 'repair_with_llm', label: '让模型修复', type: 'switch' },
|
||||
{ key: 'fail_fast', label: '校验失败即中断', type: 'switch' },
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: {
|
||||
source: '{{ last_message }}', max_retries: 2, repair_with_llm: true, fail_fast: true,
|
||||
schema: { type: 'object', properties: {}, required: [] },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export const NODE_CATEGORIES = ['起止', '模型', '执行', '控制', '上下文', '把关']
|
||||
|
||||
/** branch / loop / human 的出口由配置决定,这里统一算出来。 */
|
||||
export function sourceHandles(type: NodeType, config: Record<string, any>): HandleDef[] {
|
||||
const def = NODE_DEFS[type]
|
||||
if (def.sources) return def.sources
|
||||
if (type === 'branch') {
|
||||
const cases = (config.cases ?? []) as { key?: string; label?: string }[]
|
||||
return [
|
||||
...cases
|
||||
.filter((c) => c.key)
|
||||
.map((c) => ({ id: c.key!, label: c.label || c.key!, color: 'var(--ok)' })),
|
||||
{ id: 'default', label: '其他', color: 'var(--text-faint)' },
|
||||
]
|
||||
}
|
||||
if (type === 'loop') {
|
||||
return [
|
||||
{ id: 'body', label: '循环体', color: 'var(--nt)' },
|
||||
{ id: 'done', label: '结束', color: 'var(--text-faint)' },
|
||||
]
|
||||
}
|
||||
if (type === 'human') {
|
||||
if (config.mode === 'approve') {
|
||||
return [
|
||||
{ id: 'approved', label: '通过', color: 'var(--ok)' },
|
||||
{ id: 'rejected', label: '驳回', color: 'var(--err)' },
|
||||
]
|
||||
}
|
||||
return [{ id: 'out', label: '' }]
|
||||
}
|
||||
return [{ id: 'out', label: '' }]
|
||||
}
|
||||
Reference in New Issue
Block a user