Files
AgentLab/backend/app/blueprints/templates/freeform.py
T
ThearchyHelios 3293ac0b58 自由形蓝图(后端):按导入的图列出「谁来做」,只定由谁做;没绑时 bp.people_unbound 指向那张卡片
为什么:自由形的骨架就是导入的图,图里派给岗位的人工节点、没登记查询的写入在应用里都要绑人,可蓝图上没有「谁来做」卡片。
第一次发布挡在 app.slot_unbound / app.reconciler_unbound 上,白话叫人「在「谁来做」里给这一步选人」,卡片却不存在;这时
应用还没建,也没法去应用编辑器绑,只能绕开蓝图(05-已知遗留 (d))。

做了什么:
- freeform 的 cards 加 people(「可选:只定每一步由谁做;会签、时限、超时在画布上改」)。freeform.people_rows 按 tree.slot_refs
  列岗位行(同一个岗位用在几处合成一行,label 用节点标题拼、detail 写全每一处用途,path 是 /people/<i>/who);写入能力没登记
  查询时加一行「写入结果不明时谁去核实」(path /writeback/reconciler);发起人、表达式、外部系统回调的步骤列成只读行。
  blueprint_out 给出 derived_people,界面照这一份画。
- 存储复用现有路径:岗位绑定存在 spec.people[](id 就是岗位名、只写 who),_common.slot_of / bindings_of 原样生效;核实人存在
  /writeback/reconciler。needs_reconciler 对自由形按图里的写入算(with_graph 把蓝图存的图带给模板函数)。
- bp_set 对自由形的 people 只收 who(只收人和组,只由用户定),改会签、时限、超时、加减步骤报 bp.template_mismatch「在画布上
  改」。apply_bp_op / blueprint_issues 新增 graph 参数,卡片接口、蓝图检查、试运行、实例化、蓝图助手都把蓝图存的图传下去。
- 检查:派生岗位、核实人没绑报 bp.people_unbound(试运行 warning、发布 error),路径就是那一行,带 slot;图里已经没有的岗位、
  卡片上还留着的绑定报 warning,不静默删除。发布单照核实人的办法,去掉同一件事的 app.slot_unbound(slots.<slot>)和
  app.reconciler_unbound。

偏离:岗位行不让助手代填(同「结果不明时谁去核实」,派活给谁是人的决定)。

测试:test_publish_plan 新增「财务审批(岗位)+ 写回(没登记查询)」建自由形蓝图一条(两行、不选人时只有 bp.people_unbound、
改会签被拒、选好人后绑到 finance 和核实岗位、发布单不再挡);老的两条按新说法改;test_blueprint_templates 新增 people_rows
的合并、升级岗位、只读行、残留绑定提醒。
2026-10-16 10:34:18 +08:00

177 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""自由形 freeform(§3.3):从已有的运行(问数据的一轮回答,经 from-run 提取)或已发布的工作流版本导入,图原样用,
没有关节。蓝图有目的、谁能看、多久更新、谁来做四张卡片;要改图只能回画布(搭建者)。
「谁来做」(docs/design/next-round/05-已知遗留.md (d)):骨架就是导入的图,卡片只填「谁」这个关节——按图列出用到的岗位
(人工节点派给谁、超时升级给谁、写入的发送确认派给谁,people_rows),写入能力没登记查询时再加一行「写入结果不明时谁去核实」。
只定由谁做:会签、时限、超时在画布上改(改了会改图,图哈希就和发布的那一版对不上)。绑定存在 spec.people[](Step.id 就是
岗位名、只写 who,_common.slot_of / bindings_of 原样生效),核实人存在 /writeback/reconciler。以前这张卡片不存在,第一次
发布挡在 app.slot_unbound / app.reconciler_unbound 上,白话却叫人去「谁来做」里选人。
图由实例化的上下文给(graph,导入时存在蓝图上)。应用骨架按 apps/derive.py 的派生规则:有人工节点或写入的按流程,
否则按看板;写入节点按能力登记的进程内快照认(和运行时同一个判断,capabilities.registry.writes_externally)。
"""
from __future__ import annotations
import copy
from typing import Any
from app.blueprints.templates import Joint, TemplateDef, register
from app.blueprints.templates import _common as c
ID = "freeform"
VERSION = 1
def imported(bp: dict[str, Any]) -> dict[str, Any]:
graph = c.context(bp).get("graph")
return copy.deepcopy(graph) if isinstance(graph, dict) else {"nodes": [], "edges": []}
def skeleton(bp: dict[str, Any]) -> tuple[dict[str, Any], list[Joint]]:
return imported(bp), []
def _writes(graph: dict[str, Any]) -> dict[str, str]:
from app.apps.tree import node_map
from app.capabilities.registry import cached_view, writes_externally
out: dict[str, str] = {}
for nid, n in node_map(graph).items():
tool = str(n["config"].get("tool") or "")
view = cached_view(tool) if n["type"] == "tool" and tool else None
if view is not None and writes_externally(tool, view.get("effect"), view.get("endpoint_id")):
out[nid] = tool
return out
#: 「谁来做」一行里每处用途怎么说(apps/tree.SLOT_ROLES,加上核实)
USE_TEXT = {"assignee": "处理", "escalation": "超时升级", "confirm": "发送前确认", "reconcile": "写入结果不明时去核实"}
#: 不按岗位派的步骤(只读行):发起人自己、按表达式算出来的人、等外部系统回调
FIXED_TEXT = {"initiator": "由发起人自己处理", "expression": "按图里的表达式算出由谁处理(在画布上改)",
"hook": "等外部系统回调,不派给人"}
#: 核实人那一行的名字
RECONCILER_LABEL = "写入结果不明时谁去核实"
def people_rows(bp: dict[str, Any]) -> list[dict[str, Any]]:
"""按导入的图列出「谁来做」:[{kind, slot, label, detail, path, who, uses}]。
kind=slot:图里引用的一个岗位(同一个岗位用在几处合成一行,按节点出现的顺序),path 是 /people/<i>/who(i 是岗位行的
序号),who 是卡片上绑的人或组;kind=reconciler:写入能力没登记查询时的核实人,path 是 /writeback/reconciler;
kind=initiator / expression / hook:不按岗位派的步骤,只读(path 为空)。label 用节点在图上的名字拼(岗位名是画布上
随手写的 id),detail 写全每一处用途。界面照这一份画,不自己从图里推。"""
from app.apps.derive import RECONCILER
from app.apps.tree import node_map, slot_refs
graph = imported(bp)
writes = _writes(graph)
nodes = node_map(graph)
bound = {str(step.get("id")): c.value(step.get("who")) for step in bp.get("people") or [] if isinstance(step, dict)}
rows: list[dict[str, Any]] = []
by_slot: dict[str, dict[str, Any]] = {}
for ref in slot_refs(graph, writes):
row = by_slot.get(ref["slot"])
if row is None:
row = by_slot[ref["slot"]] = {"kind": "slot", "slot": ref["slot"], "path": f"/people/{len(by_slot)}/who",
"who": bound.get(ref["slot"]), "uses": []}
rows.append(row)
row["uses"].append({"node_id": ref["node_id"], "title": ref["title"], "role": ref["role"]})
unchecked = [(nid, cap) for nid, cap in writes.items() if not c.write_lookup(cap)]
if unchecked:
rows.append({"kind": "reconciler", "slot": RECONCILER, "path": "/writeback/reconciler",
"who": c.value((bp.get("writeback") or {}).get("reconciler")),
"uses": [{"node_id": nid, "title": nodes[nid]["title"], "role": "reconcile"} for nid, _ in unchecked]})
for nid, n in nodes.items():
if n["type"] != "human" and nid not in writes:
continue
assignee = n["config"].get("assignee")
kind = assignee.get("kind") if isinstance(assignee, dict) else None
if kind in FIXED_TEXT:
rows.append({"kind": kind, "slot": None, "path": None, "who": None,
"uses": [{"node_id": nid, "title": n["title"],
"role": "assignee" if n["type"] == "human" else "confirm"}]})
for row in rows:
titles = list(dict.fromkeys(u["title"] for u in row["uses"]))
row["label"] = RECONCILER_LABEL if row["kind"] == "reconciler" else "、".join(f"「{t}」" for t in titles)
row["detail"] = FIXED_TEXT[row["kind"]] if row["kind"] in FIXED_TEXT else ";".join(
f"「{u['title']}」{USE_TEXT[u['role']]}" for u in row["uses"])
return rows
def needs_reconciler(bp: dict[str, Any]) -> bool:
"""导入的图里有写入能力没登记查询(同 app_skeleton 加核实岗位的判断)。"""
return any(not c.write_lookup(cap) for cap in _writes(imported(bp)).values())
def app_skeleton(bp: dict[str, Any], graph: dict[str, Any]) -> dict[str, Any]:
from app.apps.derive import ENTRY, INSTANCE_VIEW, PIN, VIEW, _entries, _results
from app.apps.forms import input_schema
from app.apps.tree import input_fields, node_map
name = c.app_name(bp, "应用")
nodes = node_map(graph)
writes = _writes(graph)
process = bool(writes) or any(n["type"] == "human" for n in nodes.values())
spec = c.app_base(bp, graph, use="process" if process else "view", name=name,
description=str(c.value(bp.get("purpose")) or ""), writes=writes)
fields = input_fields(graph)
if fields:
spec["forms"][ENTRY] = input_schema(fields)
names = list(spec["forms"][ENTRY]["properties"])
spec["entries"].append({"id": ENTRY, "label": f"发起{name}" if process else "按条件运行", "pin": PIN,
"form": {"$ref": f"#/forms/{ENTRY}"}, "map": {n: f"form.{n}" for n in names}})
spec["views"][INSTANCE_VIEW] = {"source": {"kind": "instance"}}
if not process:
spec["views"][VIEW] = c.latest_view(c.context(bp))
spec["pages"] = [{"id": "home", "title": name, "body": ([] if process else _results(nodes, VIEW, "home"))
+ _entries(spec)}]
if spec["entries"]:
spec["pages"].append(c.instance_page(graph, writes))
return c.finish_app(spec)
def schedule(bp: dict[str, Any]) -> dict[str, Any] | None:
"""导入的图入口字段正好是统计期三个字段时按「多久更新」填;别的字段没法替人定,只建任务、不填输入。"""
from app.apps.tree import input_fields
names = {f["name"] for f in input_fields(imported(bp))}
inputs = None if set(c.PERIOD_FIELDS) <= names else {}
return c.schedule_of(bp, c.app_name(bp, "应用"), inputs=inputs)
def example() -> dict[str, Any]:
item = c.user_item
return {"format": "agentlab.blueprint/1", "purpose": item("把这次问数据的回答做成每周自动出的看板"),
"template": {"id": ID, "version": VERSION}, "data": [], "cadence": item(c.default_cadence("weekly")),
"people": [], "fields": [], "writeback": None,
"access": {"use": [], "observe": [], "manage": []}, "asks": []}
def example_graph() -> dict[str, Any]:
"""示例导入的图:统计期 → 查库 → 口径卡 → 按指标出具(测试用)。"""
return {"nodes": [
c.node(c.START, "input", "统计期", fields=c.period_fields()),
c.node("q", "tool", "查订单", tool="db_query__shop", args={
"sql": "SELECT SUM(amount) AS gmv FROM orders WHERE paid_at BETWEEN '{{ input.period_start }}'"
" AND '{{ input.period_end }}'"}),
c.node(c.CALIBER, "metrics", "口径卡", caliber="周报口径", caliber_version="v1",
metrics=[{"id": "gmv", "name": "销售额", "unit": "元", "expression": "cell(nodes.q, 0, 'gmv')"}]),
c.node(c.DONE, "output", "出具", fields=[{"name": "销售额(元)", "value": "{{ nodes.caliber.metrics[0].value }}"}],
contract={"metrics_from": [c.CALIBER], "required": ["gmv"], "strict": True}),
], "edges": [c.edge(c.START, "q"), c.edge("q", c.CALIBER), c.edge(c.CALIBER, c.DONE)]}
TEMPLATE = register(TemplateDef(
id=ID, version=VERSION, label="自由形",
description="从已有的开始:一次问数据的回答,或者一个已发布的工作流,原样做成应用;改图要回画布。",
applies_when="用户已经有一次满意的问数据回答,或者一个已发布的工作流,想直接做成应用;需求在五个场景之外时也用它。",
cards={"purpose": "必填:一句话说清这个应用做什么",
"cadence": "可选:要不要定期自动出数",
"people": "可选:只定每一步由谁做;会签、时限、超时在画布上改",
"access": "可选:谁能用、谁能看全部、谁能管理"},
personal="导入的图满足问数据的规则(只读、没有人工节点和写入)时可以",
skeleton=skeleton, app_skeleton=app_skeleton, schedule=schedule, tags=["导入"],
example=example, example_fill=dict, imports_graph=True,
))