Cookbook
Storylines · for AI agents
并行步骤 — 同时执行多项操作,然后继续
AND-join:多个子流程按任意顺序并行运行,各自拥有独立的检查;仅当所有流程均完成时继续。
MCP tools:
create_storylinevalidate_storyline有些步骤并非单行执行——它们是“按任意顺序完成所有这些操作,每个操作都有各自的后续步骤,并且只有在所有操作都完成后才继续”。签证申请就是典型的例子:护照、银行对账单和邀请函各自需要收集并进行不同的审核,但在所有三项都通过之前,你不能提交。这是一个并行节点(分叉 / AND-连接),它是一种一等节点类型——你无需手动编写完成标志。
工作原理
parallel 节点声明了几个必需的分支,每个分支指向其各自子流程的入口。引擎将未完成的分支作为选项呈现,并原生跟踪完成状态(进入某个分支即记录该分支;返回到并行节点即标记该分支已完成),并在所有分支完成后自动执行该节点的连接出口——即第一个带有 to_node_key 的出口。你无需自行评估该条件;该标签仅供人类阅读。每个分支都可以是一个完整的多节点子流程,拥有自己的检查和循环;其最后一步只需采用正常出口返回到并行节点即可。
create_storyline(
agent_name="Visa", key="visa-docs", name="Visa documents",
graph={"nodes": [
{"node_key": "collect", "title": "Collect all documents", "type": "parallel",
"flags": {"is_entry": True},
"parallel": {"branches": [
{"key": "passport", "label": "Passport", "to_node_key": "p1"},
{"key": "bank", "label": "Bank statement", "to_node_key": "b1"},
{"key": "invite", "label": "Invitation letter","to_node_key": "i1"}
]},
"exits": [{"kind": "ai", "label": "all documents cleared", "to_node_key": "submit",
"ai_criteria": "all required documents have been checked"}]},
{"node_key": "p1", "title": "Check passport", "task": "Verify validity ≥ 6 months; if not, tell the user what to renew.",
"exits": [{"kind": "ai", "label": "passport ok", "to_node_key": "collect", "ai_criteria": "passport is valid and legible"}]},
{"node_key": "b1", "title": "Check bank statement", "task": "Confirm 3 months of history and a sufficient balance.",
"exits": [{"kind": "ai", "label": "bank ok", "to_node_key": "collect", "ai_criteria": "statement covers 3 months and meets the threshold"}]},
{"node_key": "i1", "title": "Check invitation", "task": "Confirm host details and dates match the trip.",
"exits": [{"kind": "ai", "label": "invite ok", "to_node_key": "collect", "ai_criteria": "invitation is consistent with the trip"}]},
{"node_key": "submit", "title": "Submit application", "task": "Package the cleared documents and submit.",
"flags": {"is_terminal": True}, "exits": []}
]}
)申请人可以按任意顺序提供这三份文件;每份文件在审核通过后都会返回到 collect。在所有三个分支都完成之前,submit 是不可达的——引擎仅在此时才执行连接出口。
为每个分支提供其所需的尽可能多的流程——例如,即将过期的护照可以在返回之前循环经过一个“续期并重新上传”节点;银行分支可以要求补充对账单。请确保分支 key 唯一。然后在发布之前运行 validate_storyline——它会标记出没有分支的并行节点、指向缺失节点的分支或缺少连接出口的情况。