From 4cc19730cb29beb1312241617b018730f8b282d9 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Fri, 18 Sep 2026 10:32:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E5=8F=96=E5=80=BC=E5=A6=82=E5=AE=9E=E8=90=BD=E8=BF=9B?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=EF=BC=8C=E5=B9=B6=E4=BF=AE=E6=8E=89=E4=B8=80?= =?UTF-8?q?=E5=A4=84=E8=AF=AF=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两件事,都是实际跑了一遍才发现的。 **vars 的值原先不在任何落库事件里。** node.finished 的 preview 只取 updates["nodes"][node_id],不含 updates["vars"]。界面上要做"变量当前值" 这一列,就只能从 preview 反推(data ?? text ?? 整个 output)——那个推断 大多数时候对,而一个"大多数时候对"的调试工具比没有更糟:它会在你最需要它 的那一次骗你。改成如实带上,只多一个已截断的值,且只有真赋了值的节点才有 (截断比 preview 更狠,要全文有工件库;事件是审计凭证,能省则省)。 **输入节点还会把每个字段同时写进 vars**(run_input 的返回里 input 和 vars 是同一份)。上一版的分析器不知道,于是 {{ vars.topic }} 这种完全合法的写法 被报成"未定义"。误报是 linter 最坏的失败模式——它会让人把整个校验一起 无视掉,那就比没有校验还糟。 现在实跑验证: start | vars={"topic": "变量表"} t | vars={"note": "主题是 变量表"} out | vars=null (没赋值就不带这个字段) 校验:{{ vars.topic }} 干净,{{ vars.tpoic }} 报错并建议 {{ vars.topic }}。 --- backend/app/engine/compiler.py | 30 ++++++++++++++++++++++++++++++ backend/app/engine/variables.py | 12 +++++++++++- backend/tests/test_variables.py | 13 +++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) 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([