Files
AgentLab/backend/app/blueprints/templates/approval.py
T
ThearchyHelios 0b6cde5c0c 自由形应用的两处小问题:入口按钮「发起」后英文名空一格,岗位显示名不再拿岗位名充数
一、入口按钮写成了「发起accept 报销写回应用」:派生(apps/derive)、审批模板、自由形模板都是 f"发起{名字}" 直接拼。
加 apps/derive.start_label,名字以英文字母、数字开头时中间空一格(「发起 accept 报销写回应用」),三处都用它。

二、岗位标签显示原始名 finance:派生和自由形导入的图里只写了岗位名(画布上随手写的 id),应用里岗位的显示名就填成了它,
看着像起好的名字。加 apps/tree.slot_label:按用到这个岗位的节点在图上的名字拼(派给它的写节点名,几处用到用顿号连;
超时升级给它的写「…超时升级」,同蓝图模板 slot_labels 的叫法;动作确认派给它的写「…发送前确认」);节点没起名字、
没有节点用到它的照实用岗位名。派生和蓝图模板(app_base)共用:卡片上起了名字的(审批模板的步骤名)照旧用卡片上的,
卡片上就是岗位名本身的才按节点名拼。应用编辑器的岗位一节里,显示名和岗位名一样时写一句「还没有起显示名,现在用的是图里的
岗位名」(文案在 lib/terms 的 APP_EDITOR_TEXT.slotLabelRaw),不让人误以为那是起好的名字。

测试:test_blueprint_templates.py 的 test_entry_labels_keep_a_space_before_a_latin_name、
test_slot_labels_use_the_node_names_and_say_so_when_there_is_none、test_derived_apps_use_the_same_slot_labels_and_entry_label;
test_app_spec_tools.py::test_derive_a_process_leaves_slots_empty 原来钉的是旧行为(显示名 = finance),改成节点名「财务审批」。
去掉修复回跑四条都失败。
2026-10-17 12:00:58 +08:00

116 lines
6.3 KiB
Python

"""审批流程 approval(§3.3):入口(表单)→ [按金额线分支] → 人工(一级或多级)→ [写回] → 已通过 / 已退回。
没有生成关节,全部按卡片确定性地填:
- 表单(填什么)→ 入口节点的字段;
- 每一步(谁来做)→ 一个人工节点:派给谁(岗位)、几个人同意、时限、超时怎么办;写了条件的那一步前面加一个分支,
条件只许「字段 比较 常量」,不满足的直接跳到下一步;
- 写回(可选)→ 调用工具节点:目标能力、字段对应,要人确认时派给最后一步的岗位。
任何一步驳回都到「已退回」。不能做成个人应用:要有人审批。
"""
from __future__ import annotations
from typing import Any
from app.blueprints.templates import Joint, TemplateDef, register
from app.blueprints.templates import _common as c
ID = "approval"
VERSION = 1
APPROVED = "approved"
REJECTED = "rejected"
WRITE = "writeback"
def skeleton(bp: dict[str, Any]) -> tuple[dict[str, Any], list[Joint]]:
nodes = [c.node(c.START, "input", "发起", fields=c.form_fields(bp))]
joints = [Joint(id="start.fields", kind="fields", node_id=c.START, field="/fields", from_card="/fields",
label="表单")]
steps = bp.get("people") or []
writeback = c.value((bp.get("writeback") or {}).get("capability"))
chain = c.approval_chain(bp, after=WRITE if writeback else APPROVED, rejected=REJECTED)
nodes += chain.nodes
joints += chain.joints
edges = [c.edge(c.START, chain.head), *chain.edges]
if writeback:
write, write_joints, _ = c.write_node(bp, WRITE, confirm_slot=c.slot_of(steps[-1]) if steps else None)
nodes.append(write)
joints += write_joints
edges.append(c.edge(WRITE, APPROVED))
nodes.append(c.node(APPROVED, "output", "已通过", fields=[{"name": "结论", "value": "已通过"}, *chain.notes]))
if steps:
nodes.append(c.node(REJECTED, "output", "已退回", fields=[{"name": "结论", "value": "已退回"},
*[dict(n) for n in chain.notes]]))
return {"nodes": nodes, "edges": edges}, joints
def app_skeleton(bp: dict[str, Any], graph: dict[str, Any]) -> dict[str, Any]:
name = c.app_name(bp, "审批")
capability = c.value((bp.get("writeback") or {}).get("capability"))
writes = {WRITE: capability} if capability else {}
spec = c.app_base(bp, graph, use="process", name=name, description=str(c.value(bp.get("purpose")) or ""),
writes=writes)
from app.apps.derive import start_label
c.entry_and_pages(spec, bp, graph, label=start_label(name), writes=writes)
return c.finish_app(spec)
def problems(bp: dict[str, Any]) -> list[dict[str, Any]]:
"""写回的字段对应只能取表单字段和常量:审批流程没有指标,也没有核对出来的行。"""
if not c.value((bp.get("writeback") or {}).get("capability")):
return []
_, bad = c.write_args(c.value(bp["writeback"].get("mapping")) or {})
return [c.mismatch("/writeback/mapping", f"审批流程写回时只能取表单里的字段或常量,这几个无法取值:{'、'.join(bad)}")] \
if bad else []
def _prefill(spec: dict[str, Any]) -> None:
if not spec.get("people"):
spec["people"] = [c.step("s1", "审批")]
if not spec.get("fields"):
spec["fields"] = [{"name": "reason", "spec": c.template_item({"type": "string", "title": "事由",
"x-widget": "textarea", "required": True}),
"sensitive": False}]
def example() -> dict[str, Any]:
"""示例:报销审批。金额线 5000 以上才要财务组全员同意,48 小时没处理完升级给总监;先过直属主管。"""
item = c.user_item
def field(name: str, spec: dict[str, Any]) -> dict[str, Any]:
return {"name": name, "spec": item(spec), "sensitive": False}
return {
"format": "agentlab.blueprint/1", "purpose": item("员工报销,主管先批,大额的再过财务"),
"template": {"id": ID, "version": VERSION}, "data": [], "cadence": item(None),
"people": [
{"id": "lead", "label": item("主管审批"), "who": item([{"kind": "user", "name": "zhang"}]),
"policy": item({"kind": "any"}), "due": item("P2D"), "on_timeout": item({"action": "remind"}),
"threshold": item(None)},
{"id": "finance", "label": item("财务会签"), "who": item([{"kind": "group", "name": "财务组"}]),
"policy": item({"kind": "all"}), "due": item("PT48H"),
"on_timeout": item({"action": "escalate", "to": [{"kind": "user", "name": "director"}]}),
"threshold": item({"field": "amount", "op": ">", "value": 5000})},
],
"fields": [field("amount", {"type": "number", "title": "金额(元)", "minimum": 0.01, "required": True}),
field("reason", {"type": "string", "title": "事由", "x-widget": "textarea", "required": True})],
"writeback": None,
"access": {"use": [{"kind": "group", "name": "全体员工"}], "observe": [], "manage": []}, "asks": [],
}
TEMPLATE = register(TemplateDef(
id=ID, version=VERSION, label="审批流程",
description="有人发起、有人审批:一级或多级,几个人同意算通过,超时提醒或升级,通过后可以写回别的系统。",
applies_when="用户要一个申请—审批的流程(报销、请假、采购、合同),有表单、有处理人,可能按金额分级、要会签。",
cards={"purpose": "必填:一句话说清审批什么",
"fields": "必填:申请人要填的字段(名称、类型、必填、上下限、选项)",
"people": "必填:每一步谁处理、几个人同意算通过、时限、超时给谁;某一步只在金额超过某个数时才走,写条件",
"writeback": "可选:通过后写回哪个系统、哪些字段、要不要人确认;能力没登记查询的,还要定结果不明时谁去核实",
"access": "可选:谁能发起、谁能看全部、谁能管理"},
personal=None,
skeleton=skeleton, app_skeleton=app_skeleton, schedule=None, tags=["流程", "审批"],
example=example, example_fill=dict, prefill=_prefill, problems=problems,
))