diff --git a/backend/app/engine/nodes/tools.py b/backend/app/engine/nodes/tools.py index 5720c775..09ad34ee 100644 --- a/backend/app/engine/nodes/tools.py +++ b/backend/app/engine/nodes/tools.py @@ -121,8 +121,14 @@ async def run_code(state: GraphState, ctx: NodeContext) -> dict[str, Any]: # 留空跟随整机默认。要的那档不可用时会安静退回默认,不阻断运行。 isolation = ctx.cfg("isolation", "") or None + # 把真正要跑的那份代码发出来。用户在编辑器里写的是带 {{ }} 的模板, + # 送进沙箱的是渲染后的字符串——两者可能差很远:一个带引号或换行的值 + # 插进字符串字面量里就会把程序写坏,而报错指的是渲染后的行号,用户对着 + # 自己那份数怎么也对不上。这条事件是他们唯一能看到"实际跑了什么"的地方。 ctx.emit(EventType.SANDBOX_START, language=language, limits=limits.model_dump(), - isolation=isolation or "auto") + isolation=isolation or "auto", code=code[:8000], + code_truncated=len(code) > 8000, + interpolated="{{" in str(ctx.cfg("code", ""))) result = await sandbox_manager.run( code, language=language, diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index 68c1e3e4..8c0bcc45 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -1,6 +1,6 @@ import type { Approval, GraphSpec, KbDocument, MemoryItem, Provider, Run, RunEvent, - Skill, ToolInfo, ValidationIssue, Workflow, + Skill, ToolInfo, ValidationIssue, VarIssue, Variable, Workflow, } from '../types' const BASE = '/api' @@ -74,6 +74,9 @@ export const api = { `/workflows/${id}/publish`, { level, version }), validate: (graph: GraphSpec) => post<{ ok: boolean; issues: ValidationIssue[] }>('/workflows/validate', { graph }), + /** 这张图里有哪些变量、谁产出、谁引用。纯静态分析,不需要跑过 */ + variables: (graph: GraphSpec) => + post<{ variables: Variable[]; issues: VarIssue[] }>('/workflows/variables', { graph }), }, // ---- 运行 ---- diff --git a/frontend/src/canvas/VariablesDrawer.tsx b/frontend/src/canvas/VariablesDrawer.tsx new file mode 100644 index 00000000..b873ad35 --- /dev/null +++ b/frontend/src/canvas/VariablesDrawer.tsx @@ -0,0 +1,291 @@ +import { useEffect, useMemo, useState } from 'react' +import { + AlertTriangle, ChevronDown, Crosshair, Database, Hash, Info, Package, Variable as VarIcon, X, +} from 'lucide-react' +import clsx from 'clsx' +import { useStudio } from '../store/studio' +import type { Variable, VarIssue } from '../types' + +/** + * 画布底部的变量抽屉。 + * + * 这套编排靠 `{{ }}` 串数据流,但"这张图里到底有哪些变量"此前在界面上 + * 根本无处可查——只能靠翻每个节点的 assign_to 自己拼。拼错一个名字还不会 + * 报错(模板取不到值渲染成空字符串),于是排查只能靠猜。 + * + * 做成挤压式而不是浮层:React Flow 的 Controls 和 MiniMap 是绝对定位在 + * 画布容器里的,浮层会把它俩埋掉;挤压会把它俩顶上去。代价是画布底部被 + * 裁掉一截(React Flow v12 的 ResizeObserver 只更新尺寸,不会自动重新取景), + * 所以要限高,并且保证画布至少剩一截——高度归零时 React Flow 会直接报错。 + */ +const MIN_CANVAS = 200 +const DEFAULT_HEIGHT = 260 + +export function VariablesDrawer({ open, onClose }: { open: boolean; onClose: () => void }) { + const variables = useStudio((s) => s.variables) + const varIssues = useStudio((s) => s.varIssues) + const analyzeNow = useStudio((s) => s.analyzeNow) + const [picked, setPicked] = useState(null) + const [height, setHeight] = useState(DEFAULT_HEIGHT) + + // 打开时立刻算一次:变量表是按结构签名跳过的,可能已经很久没刷新了 + useEffect(() => { if (open) void analyzeNow() }, [open, analyzeNow]) + + if (!open) return null + + const selected = variables.find((v) => v.path === picked) ?? null + + return ( +
+ + +
+ + 变量 + {variables.length} 个 + + + +
+ +
+
+ +
+ {selected && ( +
+ setPicked(null)} /> +
+ )} +
+
+ ) +} + +/** 拖拽改高。上下都要夹住:高度归零会让 React Flow 直接报错 004。 */ +function ResizeHandle({ height, onResize }: { + height: number; onResize: (h: number) => void +}) { + return ( +
{ + e.preventDefault() + const startY = e.clientY + const startH = height + const max = Math.max(MIN_CANVAS, window.innerHeight * 0.6) + const move = (ev: PointerEvent) => { + onResize(Math.min(max, Math.max(120, startH - (ev.clientY - startY)))) + } + const up = () => { + window.removeEventListener('pointermove', move) + window.removeEventListener('pointerup', up) + } + window.addEventListener('pointermove', move) + window.addEventListener('pointerup', up) + }} + /> + ) +} + +function IssueChips({ issues }: { issues: VarIssue[] }) { + const errors = issues.filter((i) => i.level === 'error').length + const warns = issues.filter((i) => i.level === 'warning').length + const infos = issues.filter((i) => i.level === 'info').length + return ( + <> + {errors > 0 && ( + + {errors} 个引用不到 + + )} + {warns > 0 && ( + {warns} 个取值时机不对 + )} + {infos > 0 && {infos} 个没人用} + + ) +} + +const KIND_META: Record = { + input: { icon: Database, text: '入口输入' }, + var: { icon: VarIcon, text: '节点写入' }, + node: { icon: Package, text: '节点输出' }, + builtin: { icon: Hash, text: '内置' }, +} + +function VarTable({ variables, picked, onPick }: { + variables: Variable[]; picked: string | null; onPick: (p: string | null) => void +}) { + const runtime = useRuntimeValues() + + if (!variables.length) { + return ( +
+ 画布上还没有节点 +
+ ) + } + + return ( + + + + + + + + + + + {variables.map((v) => { + const meta = KIND_META[v.kind] ?? KIND_META.builtin + const value = runtime[v.path] + return ( + onPick(picked === v.path ? null : v.path)} + > + + + + + + ) + })} + +
变量来自被引用最近一次运行的值
+ + {`{{ ${v.path} }}`} + + + + + {v.produced_by_label || meta.text} + + + {v.refs.length + ? {v.refs.length} 处 + : —} + + {value === undefined + ? — + : {value}} +
+ ) +} + +function VarDetail({ variable, onClose }: { variable: Variable; onClose: () => void }) { + const select = useStudio((s) => s.select) + const runtime = useRuntimeValues() + const value = runtime[variable.path] + + return ( +
+
+ + {`{{ ${variable.path} }}`} + + +
+
{variable.label}
+ {variable.description && ( +
+ {variable.description} +
+ )} + + {variable.produced_by && ( +
+ +
+ )} + +
+ {variable.refs.length ? variable.refs.map((r, i) => ( + + )) : ( +
+ 没有任何地方引用它。改图改了一半的话这很正常 +
+ )} +
+ +
+ {value === undefined ? ( +
+ 这次运行里没有它的记录。跑一次就能看到 +
+ ) : ( +
+            {value}
+          
+ )} +
+
+ ) +} + +function Section({ title, children }: { title: string; children: React.ReactNode }) { + return ( +
+
+ {title} +
+ {children} +
+ ) +} + +/** + * 每个变量在最近一次运行里的实际取值。 + * + * 从事件里读,不从别处猜:node.finished 现在带 vars(那一步往变量池写了 + * 什么)和 preview(那一步自己的输出)。以前 vars 不在任何事件里,只能从 + * preview 反推 data ?? text ?? output——那个推断大多数时候对,而一个 + * "大多数时候对"的调试工具比没有更糟。 + */ +function useRuntimeValues(): Record { + const events = useStudio((s) => s.events) + return useMemo(() => { + const out: Record = {} + const show = (v: unknown) => + typeof v === 'string' ? v : JSON.stringify(v, null, 2) ?? String(v) + for (const e of events) { + if (e.type !== 'node.finished') continue + const d: any = e.data ?? {} + for (const [k, v] of Object.entries(d.vars ?? {})) { + out[`vars.${k}`] = show(v) + // 入口节点写进 vars 的那几个同时也是 input.* + if (e.node_id && d.preview && typeof d.preview === 'object' && k in d.preview) { + out[`input.${k}`] = show(v) + } + } + if (e.node_id && d.preview !== undefined && d.preview !== null) { + out[`nodes.${e.node_id}`] = show(d.preview) + } + } + return out + }, [events]) +} diff --git a/frontend/src/pages/StudioPage.tsx b/frontend/src/pages/StudioPage.tsx index df512197..1bb9c9c1 100644 --- a/frontend/src/pages/StudioPage.tsx +++ b/frontend/src/pages/StudioPage.tsx @@ -1,13 +1,14 @@ import { useCallback, useEffect, useState } from 'react' import { AlertTriangle, Check, ChevronDown, Copy, LayoutGrid, Plus, Save, ShieldCheck, - Trash2, Wand2, + Trash2, Variable, Wand2, } from 'lucide-react' import clsx from 'clsx' import { api } from '../api/client' import { FlowCanvas } from '../canvas/FlowCanvas' import { Palette } from '../canvas/Palette' import { InspectorSheet } from '../canvas/InspectorSheet' +import { VariablesDrawer } from '../canvas/VariablesDrawer' import { AssistantPanel } from '../run/AssistantPanel' import { RunControl } from '../run/RunControl' import { useStudio, toGraph } from '../store/studio' @@ -27,6 +28,7 @@ export function StudioPage() { const [picker, setPicker] = useState(false) const [saving, setSaving] = useState(false) const [publishing, setPublishing] = useState(false) + const [vars, setVars] = useState(false) const copilotActive = useStudio((s) => s.copilot.active) // 首次进来自动打开第一张图 @@ -146,6 +148,13 @@ export function StudioPage() { > Copilot +