变量的运行时取值如实落进事件,并修掉一处误报

两件事,都是实际跑了一遍才发现的。

**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 }}。
This commit is contained in:
ThearchyHelios
2026-09-18 10:32:09 +08:00
parent 66b8969bc5
commit 4cc19730cb
3 changed files with 54 additions and 1 deletions
+30
View File
@@ -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)
+11 -1
View File
@@ -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()
+13
View File
@@ -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([