feat: add workflow action runtime policies
This commit is contained in:
@@ -11,6 +11,8 @@ SPEC_VERSION = "3"
|
||||
NODE_TYPES = {"start", "agent", "action", "handoff", "end"}
|
||||
EDGE_MODES = {"llm", "expression", "always"}
|
||||
AGENT_ENTRY_MODES = {"wait_user", "generate", "fixed_speech"}
|
||||
ACTION_RESULT_ASSIGNMENT_MODES = {"inherit", "override", "none"}
|
||||
ACTION_USER_INPUT_POLICIES = {"queue", "block"}
|
||||
AUTOMATIC_NODE_TYPES = {"start", "action", "handoff"}
|
||||
EXPRESSION_OPERATORS = {
|
||||
"eq",
|
||||
@@ -154,6 +156,24 @@ def _normalize_agent_data(data: dict[str, Any]) -> None:
|
||||
data["inheritGlobalConfig"] = not has_node_overrides
|
||||
|
||||
|
||||
def _normalize_action_data(data: dict[str, Any]) -> None:
|
||||
"""Add Action defaults while preserving the behavior of saved graphs.
|
||||
|
||||
Older Action nodes always passed an empty mapping when no node-level
|
||||
assignments were configured, so they did *not* inherit the reusable
|
||||
tool's assignments. Infer ``none`` for that shape and ``override`` for
|
||||
an existing non-empty mapping. Newly created nodes should persist their
|
||||
intended mode explicitly.
|
||||
"""
|
||||
if "resultAssignmentMode" not in data:
|
||||
assignments = data.get("resultAssignments")
|
||||
data["resultAssignmentMode"] = (
|
||||
"override" if isinstance(assignments, dict) and assignments else "none"
|
||||
)
|
||||
data.setdefault("resultAssignments", {})
|
||||
data.setdefault("userInputPolicy", "queue")
|
||||
|
||||
|
||||
def _normalize_settings(settings: dict[str, Any], *, global_prompt: str = "") -> None:
|
||||
settings.setdefault("globalPrompt", global_prompt)
|
||||
settings.setdefault("defaultLlmResourceId", "")
|
||||
@@ -179,10 +199,11 @@ def normalize_graph(graph: dict[str, Any] | None) -> dict[str, Any]:
|
||||
source.setdefault("nodes", [])
|
||||
source.setdefault("edges", [])
|
||||
for node in source["nodes"]:
|
||||
if node.get("type") != "agent":
|
||||
continue
|
||||
data = node.setdefault("data", {})
|
||||
_normalize_agent_data(data)
|
||||
if node.get("type") == "agent":
|
||||
_normalize_agent_data(data)
|
||||
elif node.get("type") == "action":
|
||||
_normalize_action_data(data)
|
||||
return source
|
||||
|
||||
nodes = source.get("nodes") or []
|
||||
@@ -215,6 +236,8 @@ def normalize_graph(graph: dict[str, Any] | None) -> dict[str, Any]:
|
||||
data.setdefault("scope", "session")
|
||||
elif new_type == "agent":
|
||||
_normalize_agent_data(data)
|
||||
elif new_type == "action":
|
||||
_normalize_action_data(data)
|
||||
elif new_type == "start":
|
||||
prompt = str(data.pop("prompt", "") or "").strip()
|
||||
if prompt:
|
||||
@@ -326,6 +349,20 @@ def validate_graph(graph: dict[str, Any]) -> list[str]:
|
||||
data.get("entrySpeech") or ""
|
||||
).strip():
|
||||
errors.append(f"Agent 节点 {node_id} 的固定进入语不能为空")
|
||||
elif node_type == "action":
|
||||
data = node.get("data") or {}
|
||||
assignment_mode = data.get("resultAssignmentMode")
|
||||
if assignment_mode not in ACTION_RESULT_ASSIGNMENT_MODES:
|
||||
errors.append(
|
||||
f"Action 节点 {node_id} 的结果变量模式无效:{assignment_mode}"
|
||||
)
|
||||
if not isinstance(data.get("resultAssignments"), dict):
|
||||
errors.append(f"Action 节点 {node_id} 的结果变量映射必须是对象")
|
||||
input_policy = data.get("userInputPolicy")
|
||||
if input_policy not in ACTION_USER_INPUT_POLICIES:
|
||||
errors.append(
|
||||
f"Action 节点 {node_id} 的用户输入策略无效:{input_policy}"
|
||||
)
|
||||
|
||||
if counts["start"] != 1:
|
||||
errors.append("工作流必须有且仅有一个 Start 节点")
|
||||
|
||||
Reference in New Issue
Block a user