feat: add workflow action runtime policies

This commit is contained in:
Xin Wang
2026-07-31 23:30:04 +08:00
parent c2f0f5eb04
commit f155f98e6e
10 changed files with 445 additions and 71 deletions

View File

@@ -724,19 +724,29 @@ class WorkflowBrain(BaseBrain):
self._state.enter(node_id, WorkflowStatus.RUNNING_ACTION)
await self._emit_node_active(node_id)
data = self._engine.data(node_id)
runtime = self._require_runtime()
block_user_input = data.get("userInputPolicy") == "block"
if block_user_input and runtime.set_input_enabled:
# Blocking only suppresses new audio/text input while the Action
# runs. It deliberately does not cancel the tool. The default
# queue policy leaves input enabled; the turn lock serializes any
# completed user turn until this automatic path has finished.
runtime.set_input_enabled(False)
tool_id = str(data.get("toolId") or "")
tool = self._tool_by_id.get(tool_id)
if not tool:
self._store.values["system__last_action_status"] = "error"
self._store.values["system__last_action_error"] = f"工具不存在:{tool_id}"
return
try:
if not tool:
raise ToolExecutionError(f"工具不存在:{tool_id}")
arguments = self._store.render_data(data.get("arguments") or {})
result = await self._tools.execute(
tool,
arguments,
result_assignments=data.get("resultAssignments") or {},
result_assignments=self._action_result_assignments(data),
)
if result.get("status") != "ok":
raise ToolExecutionError(
str(result.get("message") or "工具返回执行失败状态")
)
updated_variables = list(result.get("updated_variables") or [])
if updated_variables:
await self._emit_variables(
@@ -749,6 +759,26 @@ class WorkflowBrain(BaseBrain):
except (ToolExecutionError, ValueError) as exc:
self._store.values["system__last_action_status"] = "error"
self._store.values["system__last_action_error"] = str(exc)[:2048]
finally:
if block_user_input and runtime.set_input_enabled:
runtime.set_input_enabled(True)
@staticmethod
def _action_result_assignments(
data: dict[str, Any],
) -> dict[str, str] | None:
"""Resolve node mapping semantics for ToolExecutor.
``None`` means inherit the reusable tool's mapping, while an empty
dictionary explicitly disables all result assignments.
"""
mode = str(data.get("resultAssignmentMode") or "none")
if mode == "inherit":
return None
if mode == "override":
assignments = data.get("resultAssignments")
return dict(assignments) if isinstance(assignments, dict) else {}
return {}
async def _enter_handoff(self, node_id: str) -> None:
self._state.enter(node_id, WorkflowStatus.HANDOFF)

View File

@@ -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 节点")