diff --git a/backend/app/engine/compiler.py b/backend/app/engine/compiler.py index 2a06c831..9ee85fb3 100644 --- a/backend/app/engine/compiler.py +++ b/backend/app/engine/compiler.py @@ -98,6 +98,12 @@ def _wrap(node: GraphNode, run_ctx: RunContext) -> Callable[[GraphState], Awaita duration_ms=elapsed, attempt=attempt + 1, preview=_preview(updates, node.id), + # assign_to 写进 vars 的值原先不在任何落库事件里,界面上 + # 的"变量当前值"只能从 preview 反推(data ?? text ?? 整个 + # output)。那个推断大多数时候对——而一个"大多数时候对" + # 的调试工具比没有更糟:它会在你最需要它的那次骗你。 + # 如实发出来,只多一个已截断的值,且只有真的赋了值的节点才有。 + vars=_vars_preview(updates), artifact=artifact_id, ) trail = { @@ -162,6 +168,30 @@ def _describe(error: BaseException | None) -> str: return f"{type(error).__name__}: {error}" +def _vars_preview(updates: dict[str, Any]) -> dict[str, Any] | None: + """这一步往 vars 里写了什么。没写就不带这个字段。 + + 截断比 preview 更狠:这是给变量表当"当前值"用的,看个大概就够, + 要全文有工件库。事件是审计凭证,能省则省。 + """ + written = updates.get("vars") + if not isinstance(written, dict) or not written: + return None + out: dict[str, Any] = {} + for key, value in list(written.items())[:20]: + if isinstance(value, str): + out[key] = value[:600] + elif isinstance(value, (int, float, bool, type(None))): + out[key] = value + elif isinstance(value, list): + out[key] = f"[{len(value)} 项]" + elif isinstance(value, dict): + out[key] = {k: str(v)[:120] for k, v in list(value.items())[:12]} + else: + out[key] = str(value)[:300] + return out + + def _preview(updates: dict[str, Any], node_id: str) -> Any: """给前端卡片用的输出摘要,避免把整坨数据塞进事件流。""" payload = (updates.get("nodes") or {}).get(node_id) diff --git a/backend/app/engine/variables.py b/backend/app/engine/variables.py index baa8957f..418393e8 100644 --- a/backend/app/engine/variables.py +++ b/backend/app/engine/variables.py @@ -154,10 +154,20 @@ def analyze(spec: GraphSpec) -> VariableReport: name = str((field or {}).get("name") or "").strip() if not name: continue + desc = str((field or {}).get("description") or "") ensure( f"input.{name}", kind="input", label=f"入口输入「{name}」", produced_by=node.id, produced_by_label=label, order=order, - description=str((field or {}).get("description") or ""), + description=desc, + ) + # 入口节点同时把每个字段写进 vars(run_input 的返回里 + # input 和 vars 是同一份)。不认这一条的话,{{ vars.topic }} + # 这种完全合法的写法会被报成"未定义"——误报是 linter 最坏的 + # 失败模式,它会让人把整个校验一起无视掉 + ensure( + f"vars.{name}", kind="input", label=f"入口输入「{name}」(同名变量)", + produced_by=node.id, produced_by_label=label, order=order, + description=desc, ) var_name = str(cfg.get("assign_to") or "").strip() diff --git a/backend/tests/test_variables.py b/backend/tests/test_variables.py index c1b122f2..683254ad 100644 --- a/backend/tests/test_variables.py +++ b/backend/tests/test_variables.py @@ -38,6 +38,19 @@ def test_lists_what_each_node_produces() -> None: assert "nodes.start" in paths and "nodes.q" in paths +def test_input_fields_are_also_available_as_vars() -> None: + """入口节点把每个字段同时写进 vars(run_input 里 input 和 vars 是同一份)。 + + 不认这一条,{{ vars.topic }} 这种完全合法的写法会被报成"未定义"—— + 误报是 linter 最坏的失败模式,它会让人把整个校验一起无视掉。 + """ + g = _graph([ + {"id": "start", "type": "input", "config": {"fields": [{"name": "topic"}]}}, + {"id": "a", "type": "llm", "config": {"prompt": "{{ vars.topic }}"}}, + ], [("start", "a")]) + assert [i for i in analyze(g).issues if i.level != "info"] == [] + + def test_typo_is_reported_with_a_suggestion() -> None: """拼错是这里最常见的错误。只说"不存在"没用,能猜就直说。""" g = _graph([