Cookbook
Storylines · for AI agents

平行步驟 — 同時執行多項任務,然後繼續

AND-join:多個子流程以任意順序運行,各自擁有獨立的檢查;僅當所有流程完成後才繼續。

MCP tools:create_storylinevalidate_storyline

有些步驟並非單一路徑——而是「完成所有這些項目,順序不限,每個項目都有各自的後續步驟,且必須全部完成後才能繼續」。簽證申請就是典型的案例:護照、銀行對帳單和邀請函各自需要收集並經過不同的審查,但在所有三項都通過之前,你無法提交申請。這是一種平行節點(分叉 / AND-join),作為一級節點類型存在——你不需要手動編寫完成狀態標誌。

運作方式

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": []}
  ]}
)

申請人可以以任何順序提供這三份文件;每份文件在通過審查後都會返回 collectsubmit所有三個分支完成之前是不可達的——引擎僅在此時才執行匯合出口。

為每個分支提供其所需的全部流程——過期的護照可以在返回之前循環經過「續簽並重新上傳」節點;銀行分支可以要求補齊對帳單。確保分支 key 的唯一性。然後在發布前執行 validate_storyline——它會標記出沒有分支的平行節點、指向不存在節點的分支,或缺少匯合出口的情況。