代码提交
This commit is contained in:
1 parent
4ac7bc21cd
commit
17cf849361
110 files changed
+17007
-518
No files matched your search
+65
@@ -0,0 +1,65 @@
|
||||
"""Node executors for the workflow runtime."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.workflows.nodes.agents import (
|
||||
AgentNodeExecutor,
|
||||
SkillNodeExecutor,
|
||||
SubworkflowNodeExecutor,
|
||||
)
|
||||
from deerflow.workflows.nodes.base import ACTIVE_PORTS, NodeExecutor, NodeRegistry
|
||||
from deerflow.workflows.nodes.core import (
|
||||
ConditionNodeExecutor,
|
||||
LoopNodeExecutor,
|
||||
MergeNodeExecutor,
|
||||
OutputNodeExecutor,
|
||||
StartNodeExecutor,
|
||||
TransformNodeExecutor,
|
||||
)
|
||||
from deerflow.workflows.nodes.human import HumanInputNodeExecutor
|
||||
from deerflow.workflows.nodes.integrations import (
|
||||
CodeNodeExecutor,
|
||||
HttpNodeExecutor,
|
||||
SqlReadNodeExecutor,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ACTIVE_PORTS",
|
||||
"AgentNodeExecutor",
|
||||
"CodeNodeExecutor",
|
||||
"ConditionNodeExecutor",
|
||||
"HttpNodeExecutor",
|
||||
"HumanInputNodeExecutor",
|
||||
"LoopNodeExecutor",
|
||||
"MergeNodeExecutor",
|
||||
"NodeExecutor",
|
||||
"NodeRegistry",
|
||||
"OutputNodeExecutor",
|
||||
"SkillNodeExecutor",
|
||||
"SqlReadNodeExecutor",
|
||||
"StartNodeExecutor",
|
||||
"SubworkflowNodeExecutor",
|
||||
"TransformNodeExecutor",
|
||||
"build_default_registry",
|
||||
]
|
||||
|
||||
|
||||
def build_default_registry(config: WorkflowConfig | None = None) -> NodeRegistry:
|
||||
"""Registry with every executable node type wired up."""
|
||||
cfg = config or WorkflowConfig()
|
||||
registry = NodeRegistry()
|
||||
registry.register("start", StartNodeExecutor())
|
||||
registry.register("output", OutputNodeExecutor())
|
||||
registry.register("transform", TransformNodeExecutor())
|
||||
registry.register("condition", ConditionNodeExecutor())
|
||||
registry.register("merge", MergeNodeExecutor())
|
||||
registry.register("loop", LoopNodeExecutor())
|
||||
registry.register("human_input", HumanInputNodeExecutor())
|
||||
registry.register("http", HttpNodeExecutor(cfg))
|
||||
registry.register("sql_read", SqlReadNodeExecutor(cfg))
|
||||
registry.register("code", CodeNodeExecutor(cfg))
|
||||
registry.register("agent", AgentNodeExecutor())
|
||||
registry.register("skill", SkillNodeExecutor())
|
||||
registry.register("subworkflow", SubworkflowNodeExecutor())
|
||||
return registry
|
||||
@@ -0,0 +1,361 @@
|
||||
"""LLM-backed nodes: ``agent``, ``skill``, ``subworkflow``.
|
||||
|
||||
These are the only nodes that call the DeerFlow LangGraph lead agent. The agent
|
||||
invocation itself is injected (``deps.run_agent``) so the harness keeps its
|
||||
import firewall; this module owns prompt rendering, streaming fan-out, JSON
|
||||
response coercion, and error typing.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import render_template, render_value
|
||||
from deerflow.workflows.nodes.base import require_dep
|
||||
from deerflow.workflows.runtime.context import RunContext
|
||||
from deerflow.workflows.schemas import ArtifactRef, NodeResult, WorkflowNode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_JSON_BLOCK = re.compile(r"```(?:json)?\s*(.+?)\s*```", re.DOTALL)
|
||||
_DELTA_FLUSH_SECONDS = 0.05
|
||||
_DELTA_FLUSH_CHARS = 1024
|
||||
|
||||
|
||||
class _DeltaCoalescer:
|
||||
"""Merge model tokens into 50ms / 1KB event payloads (§9.5)."""
|
||||
|
||||
def __init__(self, ctx: RunContext, node_id: str) -> None:
|
||||
self._ctx = ctx
|
||||
self._node_id = node_id
|
||||
self._parts: list[str] = []
|
||||
self._chars = 0
|
||||
self._task: asyncio.Task | None = None
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def push(self, chunk: str) -> None:
|
||||
if not chunk:
|
||||
return
|
||||
async with self._lock:
|
||||
self._parts.append(chunk)
|
||||
self._chars += len(chunk)
|
||||
if self._chars >= _DELTA_FLUSH_CHARS:
|
||||
await self._flush_unlocked()
|
||||
return
|
||||
if self._task is None or self._task.done():
|
||||
self._task = asyncio.create_task(self._delayed())
|
||||
|
||||
async def _delayed(self) -> None:
|
||||
await asyncio.sleep(_DELTA_FLUSH_SECONDS)
|
||||
async with self._lock:
|
||||
await self._flush_unlocked()
|
||||
|
||||
async def _flush_unlocked(self) -> None:
|
||||
if not self._parts:
|
||||
return
|
||||
text = "".join(self._parts)
|
||||
self._parts.clear()
|
||||
self._chars = 0
|
||||
await self._ctx.emit_event(
|
||||
"node.output.delta",
|
||||
data={"channel": "answer", "delta": text, "text": text},
|
||||
node_id=self._node_id,
|
||||
)
|
||||
|
||||
async def flush(self) -> None:
|
||||
task = self._task
|
||||
if task is not None and not task.done():
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
async with self._lock:
|
||||
await self._flush_unlocked()
|
||||
|
||||
|
||||
def _extract_json(text: str) -> Any:
|
||||
candidate = text.strip()
|
||||
match = _JSON_BLOCK.search(candidate)
|
||||
if match:
|
||||
candidate = match.group(1).strip()
|
||||
else:
|
||||
start = min((i for i in (candidate.find("{"), candidate.find("[")) if i >= 0), default=-1)
|
||||
if start > 0:
|
||||
candidate = candidate[start:]
|
||||
try:
|
||||
return json.loads(candidate)
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
|
||||
|
||||
_SAFE_ARG_STRING_CHARS = 160
|
||||
_SAFE_ARG_MAX_KEYS = 12
|
||||
|
||||
|
||||
def _safe_tool_args(args: Any) -> dict[str, Any]:
|
||||
"""Minimal safe projection of tool-call args for the event log.
|
||||
|
||||
Scalars only (strings truncated), no nested containers, bounded key count —
|
||||
enough for the canvas to label the step without leaking full payloads such
|
||||
as entire documents, prompts or credentials that a tool may carry."""
|
||||
if not isinstance(args, dict):
|
||||
return {}
|
||||
out: dict[str, Any] = {}
|
||||
for key, value in list(args.items())[:_SAFE_ARG_MAX_KEYS]:
|
||||
if isinstance(value, bool) or not isinstance(value, (str, int, float)):
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = value[:_SAFE_ARG_STRING_CHARS]
|
||||
out[str(key)[:64]] = value
|
||||
return out
|
||||
|
||||
|
||||
class _AgentInvocation:
|
||||
"""Shared plumbing for ``agent`` and ``skill`` (agent_skill mode)."""
|
||||
|
||||
async def _invoke(
|
||||
self,
|
||||
node: WorkflowNode,
|
||||
ctx: RunContext,
|
||||
*,
|
||||
agent_id: str,
|
||||
prompt: str,
|
||||
skill_names: list[str],
|
||||
response_mode: str,
|
||||
response_schema: dict[str, Any],
|
||||
thread_mode: str,
|
||||
failure_code: str,
|
||||
) -> NodeResult:
|
||||
run_agent = require_dep(ctx.deps.run_agent, node=node, what="智能体执行")
|
||||
coalescer = _DeltaCoalescer(ctx, node.id)
|
||||
|
||||
async def on_delta(chunk: str) -> None:
|
||||
await coalescer.push(chunk)
|
||||
|
||||
async def on_tool(event: dict[str, Any]) -> None:
|
||||
phase = str(event.get("phase") or "started")
|
||||
name = str(event.get("name") or "")
|
||||
call_id = str(event.get("id") or "")
|
||||
if phase == "started":
|
||||
await ctx.emit_event(
|
||||
"node.tool.started",
|
||||
data={
|
||||
"name": name,
|
||||
"toolCallId": call_id,
|
||||
"title": str(event.get("title") or name),
|
||||
"args": _safe_tool_args(event.get("args")),
|
||||
},
|
||||
node_id=node.id,
|
||||
)
|
||||
return
|
||||
ok = event.get("ok")
|
||||
await ctx.emit_event(
|
||||
"node.tool.finished",
|
||||
data={
|
||||
"name": name,
|
||||
"toolCallId": call_id,
|
||||
"status": "succeeded" if ok in (True, None) else "failed",
|
||||
"summary": event.get("summary"),
|
||||
},
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
try:
|
||||
try:
|
||||
outcome = await run_agent(
|
||||
run_id=ctx.run_id,
|
||||
node_id=node.id,
|
||||
owner_id=ctx.owner_id,
|
||||
agent_id=agent_id,
|
||||
prompt=prompt,
|
||||
skill_names=skill_names,
|
||||
thread_mode=thread_mode,
|
||||
cancel=ctx.cancel,
|
||||
on_delta=on_delta,
|
||||
on_tool=on_tool,
|
||||
)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 - agent failures are expected
|
||||
logger.warning("workflow agent node %s failed", node.id, exc_info=True)
|
||||
raise WorkflowError(
|
||||
failure_code,
|
||||
"智能体执行失败",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"reason": type(exc).__name__},
|
||||
) from exc
|
||||
finally:
|
||||
await coalescer.flush()
|
||||
|
||||
text = str(outcome.get("text") or "")
|
||||
data: dict[str, Any] = {"text": text}
|
||||
if response_mode == "json":
|
||||
payload = outcome.get("json")
|
||||
if payload is None:
|
||||
payload = _extract_json(text)
|
||||
if payload is None:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_OUTPUT_SCHEMA_MISMATCH",
|
||||
"智能体未返回可解析的 JSON",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"preview": text[:500]},
|
||||
)
|
||||
required = response_schema.get("required") if isinstance(response_schema, dict) else None
|
||||
if isinstance(required, list) and isinstance(payload, dict):
|
||||
missing = [str(k) for k in required if payload.get(str(k)) in (None, "")]
|
||||
if missing:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_OUTPUT_SCHEMA_MISMATCH",
|
||||
f"智能体返回缺少字段:{'、'.join(missing)}",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"missing": missing},
|
||||
)
|
||||
data["json"] = payload
|
||||
|
||||
artifacts: list[ArtifactRef] = []
|
||||
for raw in outcome.get("artifacts") or []:
|
||||
if not isinstance(raw, dict) or not raw.get("name"):
|
||||
continue
|
||||
ref = await ctx.register_artifact(
|
||||
node,
|
||||
name=str(raw.get("name")),
|
||||
mime_type=str(raw.get("mime_type") or raw.get("mimeType") or "application/octet-stream"),
|
||||
path=str(raw.get("path") or ""),
|
||||
size_bytes=raw.get("size_bytes") or raw.get("sizeBytes"),
|
||||
preview=raw.get("preview"),
|
||||
)
|
||||
if ref is not None:
|
||||
artifacts.append(ref)
|
||||
|
||||
return NodeResult(
|
||||
data=data,
|
||||
messages=[m for m in (outcome.get("messages") or []) if isinstance(m, dict)],
|
||||
artifacts=artifacts,
|
||||
metadata={
|
||||
"agentId": agent_id,
|
||||
"threadId": outcome.get("thread_id"),
|
||||
"usage": outcome.get("usage") or {},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class AgentNodeExecutor(_AgentInvocation):
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
agent_id = str(node.config.get("agentId") or node.config.get("agent_id") or "")
|
||||
if not agent_id:
|
||||
raise WorkflowError("WORKFLOW_AGENT_FAILED", "智能体节点未选择智能体", node_id=node.id)
|
||||
template = str(node.config.get("promptTemplate") or node.config.get("prompt_template") or "")
|
||||
prompt = render_template(template, ctx.state()).strip()
|
||||
if not prompt:
|
||||
raise WorkflowError("WORKFLOW_AGENT_FAILED", "智能体节点提示词为空", node_id=node.id)
|
||||
schema = node.config.get("responseSchema") or node.config.get("response_schema") or {}
|
||||
return await self._invoke(
|
||||
node,
|
||||
ctx,
|
||||
agent_id=agent_id,
|
||||
prompt=prompt,
|
||||
skill_names=[str(s) for s in (node.config.get("skillNames") or node.config.get("skill_names") or [])],
|
||||
response_mode=str(node.config.get("responseMode") or node.config.get("response_mode") or "text"),
|
||||
response_schema=schema if isinstance(schema, dict) else {},
|
||||
thread_mode=str(node.config.get("threadMode") or node.config.get("thread_mode") or "isolated_per_node"),
|
||||
failure_code="WORKFLOW_AGENT_FAILED",
|
||||
)
|
||||
|
||||
|
||||
class SkillNodeExecutor(_AgentInvocation):
|
||||
"""``agent_skill`` runs the agent with a restricted skill set.
|
||||
``callable_skill`` (direct script invocation) is not executable yet."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
mode = str(node.config.get("mode") or "agent_skill")
|
||||
if mode != "agent_skill":
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_SKILL_FAILED",
|
||||
"callable_skill 模式暂未开放执行,请改用 agent_skill",
|
||||
node_id=node.id,
|
||||
details={"mode": mode},
|
||||
)
|
||||
agent_id = str(node.config.get("agentId") or node.config.get("agent_id") or "")
|
||||
skill_names = [str(s) for s in (node.config.get("skillNames") or node.config.get("skill_names") or [])]
|
||||
if not agent_id or not skill_names:
|
||||
raise WorkflowError("WORKFLOW_SKILL_FAILED", "技能节点需要同时指定智能体与技能", node_id=node.id)
|
||||
template = str(node.config.get("promptTemplate") or node.config.get("prompt_template") or "")
|
||||
prompt = render_template(template, ctx.state()).strip() or f"请使用技能 {skill_names[0]} 完成任务。"
|
||||
return await self._invoke(
|
||||
node,
|
||||
ctx,
|
||||
agent_id=agent_id,
|
||||
prompt=prompt,
|
||||
skill_names=skill_names,
|
||||
response_mode="text",
|
||||
response_schema={},
|
||||
thread_mode="isolated_per_node",
|
||||
failure_code="WORKFLOW_SKILL_FAILED",
|
||||
)
|
||||
|
||||
|
||||
class SubworkflowNodeExecutor:
|
||||
"""Runs a published child workflow synchronously and folds in its output."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
run_subworkflow = require_dep(ctx.deps.run_subworkflow, node=node, what="子工作流执行")
|
||||
workflow_id = str(node.config.get("workflowId") or node.config.get("workflow_id") or "")
|
||||
version_id = str(node.config.get("versionId") or node.config.get("version_id") or "")
|
||||
if not workflow_id or not version_id:
|
||||
raise WorkflowError("WORKFLOW_SUBWORKFLOW_FAILED", "子工作流节点缺少 workflowId/versionId", node_id=node.id)
|
||||
mapping = node.config.get("inputMapping") or node.config.get("input_mapping") or {}
|
||||
state = ctx.state()
|
||||
inputs = {str(key): render_value(template, state) for key, template in mapping.items()} if isinstance(mapping, dict) else {}
|
||||
await ctx.emit_event(
|
||||
"node.progress",
|
||||
data={"phase": "subworkflow.start", "workflowId": workflow_id, "versionId": version_id},
|
||||
node_id=node.id,
|
||||
)
|
||||
try:
|
||||
outcome = await run_subworkflow(
|
||||
parent_run_id=ctx.run_id,
|
||||
node_id=node.id,
|
||||
owner_id=ctx.owner_id,
|
||||
workflow_id=workflow_id,
|
||||
version_id=version_id,
|
||||
inputs=inputs,
|
||||
cancel=ctx.cancel,
|
||||
)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_SUBWORKFLOW_FAILED",
|
||||
"子工作流执行失败",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"reason": type(exc).__name__},
|
||||
) from exc
|
||||
|
||||
status = str(outcome.get("status") or "completed")
|
||||
if status != "completed":
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_SUBWORKFLOW_FAILED",
|
||||
f"子工作流未成功完成({status})",
|
||||
node_id=node.id,
|
||||
details={"childRunId": outcome.get("run_id"), "status": status},
|
||||
)
|
||||
output = outcome.get("output")
|
||||
return NodeResult(
|
||||
data=output if isinstance(output, dict) else {"value": output},
|
||||
metadata={"childRunId": outcome.get("run_id"), "workflowId": workflow_id},
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["AgentNodeExecutor", "SkillNodeExecutor", "SubworkflowNodeExecutor"]
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Node executor protocol and registry."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Protocol, runtime_checkable
|
||||
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.ports import ACTIVE_PORTS
|
||||
from deerflow.workflows.runtime.context import RunContext
|
||||
from deerflow.workflows.schemas import NodeResult, NodeType, WorkflowNode
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class NodeExecutor(Protocol):
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult: ...
|
||||
|
||||
|
||||
class NodeRegistry:
|
||||
"""Maps node type → executor. Unknown types fail with a typed error."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._executors: dict[str, NodeExecutor] = {}
|
||||
|
||||
def register(self, node_type: NodeType | str, executor: NodeExecutor) -> None:
|
||||
self._executors[str(node_type)] = executor
|
||||
|
||||
def get(self, node_type: str) -> NodeExecutor:
|
||||
executor = self._executors.get(node_type)
|
||||
if executor is None:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_RESOURCE_MISSING",
|
||||
f"节点类型暂不支持执行:{node_type}",
|
||||
details={"nodeType": node_type},
|
||||
)
|
||||
return executor
|
||||
|
||||
def supported(self) -> list[str]:
|
||||
return sorted(self._executors)
|
||||
|
||||
|
||||
def require_dep(value: Any, *, node: WorkflowNode, what: str) -> Any:
|
||||
if value is None:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_RESOURCE_MISSING",
|
||||
f"运行环境缺少{what}能力,无法执行该节点",
|
||||
node_id=node.id,
|
||||
details={"capability": what},
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
__all__ = ["ACTIVE_PORTS", "NodeExecutor", "NodeRegistry", "require_dep"]
|
||||
@@ -0,0 +1,238 @@
|
||||
"""Deterministic control / data nodes: start, output, transform, condition, merge, loop."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import evaluate_condition, render_template, render_value
|
||||
from deerflow.workflows.nodes.base import ACTIVE_PORTS
|
||||
from deerflow.workflows.runtime.context import RunContext
|
||||
from deerflow.workflows.schema_validation import schema_issues
|
||||
from deerflow.workflows.schemas import NodeResult, WorkflowNode
|
||||
|
||||
DEFAULT_PORT = "else"
|
||||
LOOP_CONTINUE_PORT = "continue"
|
||||
LOOP_DONE_PORT = "done"
|
||||
|
||||
|
||||
def _required_keys(schema: dict[str, Any]) -> list[str]:
|
||||
required = schema.get("required")
|
||||
if isinstance(required, list):
|
||||
return [str(k) for k in required]
|
||||
return []
|
||||
|
||||
|
||||
class StartNodeExecutor:
|
||||
"""Validates run inputs against the graph input schema and exposes them."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
schema = ctx.graph.input_schema or {}
|
||||
missing = [key for key in _required_keys(schema) if ctx.inputs.get(key) in (None, "")]
|
||||
if missing:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_INPUT_INVALID",
|
||||
f"缺少必填输入:{'、'.join(missing)}",
|
||||
node_id=node.id,
|
||||
details={"missing": missing},
|
||||
)
|
||||
# Full JSON Schema pass (types / enums / nested objects) beyond the
|
||||
# friendly missing-key check above.
|
||||
issues = schema_issues(ctx.inputs, schema)
|
||||
if issues:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_INPUT_INVALID",
|
||||
"输入不符合 schema:" + ";".join(i["message"] for i in issues[:3]),
|
||||
node_id=node.id,
|
||||
details={"errors": issues},
|
||||
)
|
||||
declared = node.config.get("fields")
|
||||
data = dict(ctx.inputs)
|
||||
if isinstance(declared, list) and declared:
|
||||
names = {str(f.get("name")) for f in declared if isinstance(f, dict) and f.get("name")}
|
||||
data = {k: v for k, v in data.items() if k in names} or data
|
||||
return NodeResult(data=data)
|
||||
|
||||
|
||||
class OutputNodeExecutor:
|
||||
"""Projects the final run output. Empty mapping = merge all upstream data."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
mapping = node.config.get("mapping") or {}
|
||||
state = ctx.state()
|
||||
if isinstance(mapping, dict) and mapping:
|
||||
data = {str(key): render_value(template, state) for key, template in mapping.items()}
|
||||
else:
|
||||
data = {}
|
||||
for result in ctx.node_results.values():
|
||||
payload = result.get("data")
|
||||
if isinstance(payload, dict):
|
||||
data.update(payload)
|
||||
schema = ctx.graph.output_schema or {}
|
||||
missing = [key for key in _required_keys(schema) if data.get(key) in (None, "")]
|
||||
if missing:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_OUTPUT_SCHEMA_MISMATCH",
|
||||
f"输出缺少必填字段:{'、'.join(missing)}",
|
||||
node_id=node.id,
|
||||
details={"missing": missing},
|
||||
)
|
||||
issues = schema_issues(data, schema)
|
||||
if issues:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_OUTPUT_SCHEMA_MISMATCH",
|
||||
"输出不符合 schema:" + ";".join(i["message"] for i in issues[:3]),
|
||||
node_id=node.id,
|
||||
details={"errors": issues},
|
||||
)
|
||||
return NodeResult(data=data, metadata={"isRunOutput": True})
|
||||
|
||||
|
||||
class TransformNodeExecutor:
|
||||
"""Pure data reshaping. Supported ops: set / pick / omit / json_parse / join."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
state = ctx.state()
|
||||
data: dict[str, Any] = {}
|
||||
template = node.config.get("template")
|
||||
if isinstance(template, str) and template:
|
||||
data["text"] = render_template(template, state)
|
||||
|
||||
for raw in node.config.get("operations") or []:
|
||||
if not isinstance(raw, dict):
|
||||
continue
|
||||
op = str(raw.get("op") or "set")
|
||||
target = str(raw.get("target") or raw.get("name") or "value")
|
||||
try:
|
||||
if op == "set":
|
||||
data[target] = render_value(raw.get("value"), state)
|
||||
elif op == "pick":
|
||||
source = render_value(raw.get("source"), state)
|
||||
keys = [str(k) for k in (raw.get("keys") or [])]
|
||||
data[target] = {k: source.get(k) for k in keys} if isinstance(source, dict) else None
|
||||
elif op == "omit":
|
||||
source = render_value(raw.get("source"), state)
|
||||
keys = {str(k) for k in (raw.get("keys") or [])}
|
||||
data[target] = {k: v for k, v in source.items() if k not in keys} if isinstance(source, dict) else None
|
||||
elif op == "json_parse":
|
||||
text = render_value(raw.get("source"), state)
|
||||
data[target] = json.loads(text) if isinstance(text, str) and text.strip() else None
|
||||
elif op == "join":
|
||||
items = render_value(raw.get("source"), state)
|
||||
separator = str(raw.get("separator") or "\n")
|
||||
data[target] = separator.join(str(i) for i in items) if isinstance(items, list) else ""
|
||||
else:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_TRANSFORM_FAILED",
|
||||
f"不支持的转换操作:{op}",
|
||||
node_id=node.id,
|
||||
details={"op": op},
|
||||
)
|
||||
except WorkflowError:
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 - surface as typed node failure
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_TRANSFORM_FAILED",
|
||||
f"转换操作 {op} 执行失败",
|
||||
node_id=node.id,
|
||||
details={"op": op, "target": target, "reason": type(exc).__name__},
|
||||
) from exc
|
||||
return NodeResult(data=data)
|
||||
|
||||
|
||||
class ConditionNodeExecutor:
|
||||
"""Picks the first matching branch; activates only that outgoing port."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
state = ctx.state()
|
||||
branches = node.config.get("branches") or []
|
||||
if not isinstance(branches, list) or not branches:
|
||||
raise WorkflowError("WORKFLOW_CONDITION_FAILED", "条件节点未配置分支", node_id=node.id)
|
||||
evaluated: list[dict[str, Any]] = []
|
||||
chosen: str | None = None
|
||||
for raw in branches:
|
||||
if not isinstance(raw, dict):
|
||||
continue
|
||||
name = str(raw.get("name") or "")
|
||||
expression = str(raw.get("expression") or "")
|
||||
matched = evaluate_condition(expression, state)
|
||||
evaluated.append({"name": name, "matched": matched})
|
||||
if matched and chosen is None:
|
||||
chosen = name
|
||||
if chosen is None:
|
||||
chosen = str(node.config.get("defaultBranch") or node.config.get("default_branch") or DEFAULT_PORT)
|
||||
return NodeResult(
|
||||
data={"branch": chosen, "evaluated": evaluated},
|
||||
metadata={ACTIVE_PORTS: [chosen]},
|
||||
)
|
||||
|
||||
|
||||
class MergeNodeExecutor:
|
||||
"""Collects upstream results per strategy (all / first_success / any_success)."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
strategy = str(node.config.get("strategy") or "all")
|
||||
sources = [str(s) for s in (node.config.get("sources") or [])]
|
||||
if not sources:
|
||||
sources = [edge.source for edge in ctx.graph.edges if edge.target == node.id]
|
||||
collected: dict[str, Any] = {}
|
||||
succeeded: list[str] = []
|
||||
for source in sources:
|
||||
result = ctx.node_results.get(source)
|
||||
if result is None:
|
||||
continue
|
||||
collected[source] = result.get("data")
|
||||
succeeded.append(source)
|
||||
if strategy in ("first_success", "any_success") and not succeeded:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_MERGE_FAILED",
|
||||
"合并节点没有任何成功的上游结果",
|
||||
node_id=node.id,
|
||||
details={"strategy": strategy, "sources": sources},
|
||||
)
|
||||
if strategy == "first_success":
|
||||
first = succeeded[0]
|
||||
return NodeResult(data={"selected": first, "value": collected[first]})
|
||||
return NodeResult(data={"branches": collected, "succeeded": succeeded})
|
||||
|
||||
|
||||
class LoopNodeExecutor:
|
||||
"""Iteration gate. Activates ``continue`` (back-edge) or ``done``.
|
||||
|
||||
Iteration accounting lives in ``ctx.loop_state`` so the cap is enforced even
|
||||
across a resume, and the engine invalidates body node results before the
|
||||
next pass.
|
||||
"""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
iteration = int(ctx.loop_state.get(node.id, 0))
|
||||
configured = int(node.config.get("maxIterations") or node.config.get("max_iterations") or 1)
|
||||
cap = min(configured, ctx.graph.settings.max_loop_iterations)
|
||||
expression = str(node.config.get("continueWhen") or node.config.get("continue_when") or "")
|
||||
should_continue = bool(expression) and evaluate_condition(expression, ctx.state())
|
||||
next_iteration = iteration + 1
|
||||
if should_continue and next_iteration >= cap:
|
||||
should_continue = False
|
||||
port = LOOP_CONTINUE_PORT if should_continue else LOOP_DONE_PORT
|
||||
if should_continue:
|
||||
ctx.loop_state[node.id] = next_iteration
|
||||
return NodeResult(
|
||||
data={
|
||||
"iteration": iteration,
|
||||
"nextIteration": next_iteration if should_continue else iteration,
|
||||
"maxIterations": cap,
|
||||
"continue": should_continue,
|
||||
},
|
||||
metadata={ACTIVE_PORTS: [port]},
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ConditionNodeExecutor",
|
||||
"LoopNodeExecutor",
|
||||
"MergeNodeExecutor",
|
||||
"OutputNodeExecutor",
|
||||
"StartNodeExecutor",
|
||||
"TransformNodeExecutor",
|
||||
]
|
||||
@@ -0,0 +1,77 @@
|
||||
"""``human_input`` node: suspends the run and validates the resumed payload."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import render_template
|
||||
from deerflow.workflows.runtime.context import RunContext, WorkflowPaused
|
||||
from deerflow.workflows.schemas import NodeResult, WorkflowNode
|
||||
|
||||
DEFAULT_ACTIONS = ("submit",)
|
||||
|
||||
|
||||
def _form_schema(node: WorkflowNode) -> dict[str, Any]:
|
||||
schema = node.config.get("formSchema") or node.config.get("form_schema") or {}
|
||||
return schema if isinstance(schema, dict) else {}
|
||||
|
||||
|
||||
def _actions(node: WorkflowNode) -> list[str]:
|
||||
raw = node.config.get("actions")
|
||||
actions = [str(a) for a in raw] if isinstance(raw, list) and raw else list(DEFAULT_ACTIONS)
|
||||
return actions
|
||||
|
||||
|
||||
class HumanInputNodeExecutor:
|
||||
"""First pass raises :class:`WorkflowPaused`; the resume pass consumes the
|
||||
payload the API stored on the context and validates it against the form."""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
payload = ctx.resume_payload
|
||||
if not payload or str(payload.get("nodeId") or "") != node.id:
|
||||
raise WorkflowPaused(
|
||||
node.id,
|
||||
{
|
||||
"nodeId": node.id,
|
||||
"name": node.name,
|
||||
"prompt": render_template(str(node.config.get("prompt") or ""), ctx.state()),
|
||||
"formSchema": _form_schema(node),
|
||||
"actions": _actions(node),
|
||||
},
|
||||
)
|
||||
|
||||
action = str(payload.get("action") or "submit")
|
||||
allowed = _actions(node)
|
||||
if action not in allowed:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_HUMAN_INPUT_INVALID",
|
||||
f"不支持的操作:{action}",
|
||||
node_id=node.id,
|
||||
details={"allowed": allowed},
|
||||
)
|
||||
values = payload.get("values")
|
||||
values = values if isinstance(values, dict) else {}
|
||||
schema = _form_schema(node)
|
||||
required = schema.get("required")
|
||||
if isinstance(required, list) and action != "cancel":
|
||||
missing = [str(key) for key in required if values.get(str(key)) in (None, "")]
|
||||
if missing:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_HUMAN_INPUT_INVALID",
|
||||
f"缺少必填项:{'、'.join(missing)}",
|
||||
node_id=node.id,
|
||||
details={"missing": missing},
|
||||
)
|
||||
if action == "cancel":
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_CANCELLED",
|
||||
"用户在人工节点选择终止运行",
|
||||
node_id=node.id,
|
||||
)
|
||||
# One-shot: a later human node in the same run must pause again.
|
||||
ctx.resume_payload = None
|
||||
return NodeResult(data={"action": action, "values": values})
|
||||
|
||||
|
||||
__all__ = ["HumanInputNodeExecutor"]
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
"""External-effect nodes: ``http``, ``sql_read``, ``code``."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import render_template, render_value
|
||||
from deerflow.workflows.runtime.code_runner import run_python_snippet
|
||||
from deerflow.workflows.runtime.context import RunContext
|
||||
from deerflow.workflows.runtime.sql_runner import execute_read_query
|
||||
from deerflow.workflows.schemas import NodeResult, WorkflowNode
|
||||
from deerflow.workflows.security.http_policy import enforce_http_target, enforce_redirect
|
||||
from deerflow.workflows.security.sql_policy import (
|
||||
assert_parameters_bound,
|
||||
assert_read_only,
|
||||
assert_tables_allowed,
|
||||
enforce_limit,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Response headers safe to echo back into the event log.
|
||||
_SAFE_RESPONSE_HEADERS = ("content-type", "content-length", "date", "etag", "location")
|
||||
_REDACTED_REQUEST_HEADERS = ("authorization", "cookie", "x-api-key", "proxy-authorization")
|
||||
|
||||
|
||||
class HttpNodeExecutor:
|
||||
def __init__(self, config: WorkflowConfig) -> None:
|
||||
self._config = config
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
policy = self._config.http
|
||||
state = ctx.state()
|
||||
url = render_template(str(node.config.get("url") or ""), state).strip()
|
||||
if not url:
|
||||
raise WorkflowError("WORKFLOW_HTTP_FAILED", "HTTP 节点缺少 URL", node_id=node.id)
|
||||
method = str(node.config.get("method") or "GET").upper()
|
||||
headers = {str(k): str(v) for k, v in (render_value(node.config.get("headers") or {}, state) or {}).items()}
|
||||
query = render_value(node.config.get("query") or {}, state) or {}
|
||||
body = render_value(node.config.get("body"), state)
|
||||
|
||||
credential_ref = node.config.get("credentialRef") or node.config.get("credential_ref")
|
||||
if credential_ref:
|
||||
headers.update(await self._credential_headers(str(credential_ref), node, ctx))
|
||||
|
||||
try:
|
||||
target = await enforce_http_target(url, policy)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
|
||||
timeout = min(
|
||||
int(node.config.get("timeoutSeconds") or node.config.get("timeout_seconds") or policy.timeout_seconds),
|
||||
policy.timeout_seconds,
|
||||
)
|
||||
max_bytes = min(
|
||||
int(node.config.get("maxResponseBytes") or node.config.get("max_response_bytes") or policy.max_response_bytes),
|
||||
policy.max_response_bytes,
|
||||
)
|
||||
allow_redirects = bool(node.config.get("allowRedirects") or node.config.get("allow_redirects"))
|
||||
max_hops = policy.max_redirects if allow_redirects else 0
|
||||
|
||||
await ctx.emit_event(
|
||||
"node.progress",
|
||||
data={
|
||||
"phase": "http.request",
|
||||
"method": method,
|
||||
"host": target["host"],
|
||||
"headers": [k for k in headers if k.lower() not in _REDACTED_REQUEST_HEADERS],
|
||||
},
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
import httpx
|
||||
|
||||
current_url = url
|
||||
hops = 0
|
||||
raw = b""
|
||||
truncated = False
|
||||
async with httpx.AsyncClient(timeout=timeout, follow_redirects=False) as client:
|
||||
while True:
|
||||
ctx.cancel.raise_if_cancelled(node.id)
|
||||
try:
|
||||
request = client.build_request(
|
||||
method,
|
||||
current_url,
|
||||
headers=headers,
|
||||
params=query or None,
|
||||
json=body if isinstance(body, (dict, list)) else None,
|
||||
content=body if isinstance(body, (str, bytes)) else None,
|
||||
)
|
||||
response = await client.send(request, stream=True)
|
||||
except httpx.HTTPError as exc:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_HTTP_FAILED",
|
||||
"HTTP 请求失败",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"reason": type(exc).__name__},
|
||||
) from exc
|
||||
|
||||
if response.status_code in (301, 302, 303, 307, 308) and hops < max_hops:
|
||||
location = response.headers.get("location") or ""
|
||||
await response.aclose()
|
||||
if not location:
|
||||
break
|
||||
current_url = await enforce_redirect(location, current_url, policy)
|
||||
hops += 1
|
||||
continue
|
||||
|
||||
chunks: list[bytes] = []
|
||||
size = 0
|
||||
try:
|
||||
async for chunk in response.aiter_bytes():
|
||||
size += len(chunk)
|
||||
if size > max_bytes:
|
||||
truncated = True
|
||||
break
|
||||
chunks.append(chunk)
|
||||
finally:
|
||||
await response.aclose()
|
||||
raw = b"".join(chunks)
|
||||
break
|
||||
|
||||
text_body = raw.decode("utf-8", errors="replace")
|
||||
parsed: Any = None
|
||||
content_type = response.headers.get("content-type", "")
|
||||
if "json" in content_type.lower():
|
||||
try:
|
||||
parsed = json.loads(text_body)
|
||||
except json.JSONDecodeError:
|
||||
parsed = None
|
||||
|
||||
data = {
|
||||
"status": response.status_code,
|
||||
"ok": 200 <= response.status_code < 300,
|
||||
"headers": {k: v for k, v in response.headers.items() if k.lower() in _SAFE_RESPONSE_HEADERS},
|
||||
"json": parsed,
|
||||
"text": None if parsed is not None else text_body,
|
||||
"truncated": truncated,
|
||||
"redirects": hops,
|
||||
}
|
||||
if not data["ok"]:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_HTTP_FAILED",
|
||||
f"HTTP 请求返回 {response.status_code}",
|
||||
retryable=response.status_code >= 500 or response.status_code == 429,
|
||||
node_id=node.id,
|
||||
details={"status": response.status_code, "preview": text_body[:500]},
|
||||
)
|
||||
return NodeResult(data=data, metadata={"host": target["host"]})
|
||||
|
||||
async def _credential_headers(self, ref: str, node: WorkflowNode, ctx: RunContext) -> dict[str, str]:
|
||||
if ctx.deps.resolve_credential is None:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_RESOURCE_MISSING",
|
||||
"运行环境未配置凭据解析能力",
|
||||
node_id=node.id,
|
||||
)
|
||||
credential = await ctx.deps.resolve_credential(ref)
|
||||
if not credential:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_RESOURCE_MISSING",
|
||||
"引用的凭据不存在或无权访问",
|
||||
node_id=node.id,
|
||||
details={"credentialRef": ref},
|
||||
)
|
||||
headers = credential.get("headers")
|
||||
return {str(k): str(v) for k, v in headers.items()} if isinstance(headers, dict) else {}
|
||||
|
||||
|
||||
class SqlReadNodeExecutor:
|
||||
def __init__(self, config: WorkflowConfig) -> None:
|
||||
self._config = config
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
policy = self._config.sql
|
||||
if not policy.enabled:
|
||||
raise WorkflowError("WORKFLOW_SQL_POLICY_DENIED", "SQL 节点已被系统禁用", node_id=node.id)
|
||||
data_source_id = str(node.config.get("dataSourceId") or node.config.get("data_source_id") or "")
|
||||
if not data_source_id:
|
||||
raise WorkflowError("WORKFLOW_SQL_FAILED", "SQL 节点未选择数据源", node_id=node.id)
|
||||
if ctx.deps.resolve_data_source is None:
|
||||
raise WorkflowError("WORKFLOW_RESOURCE_MISSING", "运行环境未配置数据源解析能力", node_id=node.id)
|
||||
source = await ctx.deps.resolve_data_source(data_source_id)
|
||||
if not source or not source.get("dsn"):
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_RESOURCE_MISSING",
|
||||
"数据源不存在或无权访问",
|
||||
node_id=node.id,
|
||||
details={"dataSourceId": data_source_id},
|
||||
)
|
||||
|
||||
statement = assert_read_only(str(node.config.get("statement") or ""))
|
||||
parameters = render_value(node.config.get("parameters") or {}, ctx.state()) or {}
|
||||
if not isinstance(parameters, dict):
|
||||
raise WorkflowError("WORKFLOW_SQL_FAILED", "SQL 参数必须是对象", node_id=node.id)
|
||||
assert_parameters_bound(statement, parameters)
|
||||
assert_tables_allowed(statement, list(source.get("allowed_tables") or []))
|
||||
max_rows = min(
|
||||
int(node.config.get("maxRows") or node.config.get("max_rows") or policy.max_rows),
|
||||
policy.max_rows,
|
||||
int(source.get("max_rows") or policy.max_rows),
|
||||
)
|
||||
statement = enforce_limit(statement, max_rows)
|
||||
|
||||
await ctx.emit_event(
|
||||
"node.progress",
|
||||
data={"phase": "sql.query", "dataSourceId": data_source_id, "maxRows": max_rows},
|
||||
node_id=node.id,
|
||||
)
|
||||
try:
|
||||
result = await execute_read_query(
|
||||
dsn=str(source["dsn"]),
|
||||
statement=statement,
|
||||
parameters={str(k): v for k, v in parameters.items()},
|
||||
max_rows=max_rows,
|
||||
timeout_seconds=policy.statement_timeout_seconds,
|
||||
max_cell_chars=policy.max_cell_chars,
|
||||
)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
return NodeResult(data=result)
|
||||
|
||||
|
||||
class CodeNodeExecutor:
|
||||
def __init__(self, config: WorkflowConfig) -> None:
|
||||
self._config = config
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
policy = self._config.code
|
||||
if not policy.enabled:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_CODE_SANDBOX_FAILED",
|
||||
"代码节点未启用(需管理员在 config.yaml 中开启 workflows.code.enabled)",
|
||||
node_id=node.id,
|
||||
)
|
||||
source = str(node.config.get("source") or "")
|
||||
if not source.strip():
|
||||
raise WorkflowError("WORKFLOW_CODE_SANDBOX_FAILED", "代码节点没有源代码", node_id=node.id)
|
||||
if len(source) > policy.max_source_chars:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_LIMIT_EXCEEDED",
|
||||
f"代码长度超过上限 {policy.max_source_chars}",
|
||||
node_id=node.id,
|
||||
)
|
||||
inputs = render_value(node.config.get("inputs") or {}, ctx.state()) or {}
|
||||
timeout = min(
|
||||
int(node.config.get("timeoutSeconds") or node.config.get("timeout_seconds") or policy.timeout_seconds),
|
||||
policy.timeout_seconds,
|
||||
)
|
||||
allow_network = bool((node.config.get("allowNetwork") or node.config.get("allow_network")) and policy.allow_network)
|
||||
try:
|
||||
outcome = await run_python_snippet(
|
||||
source=source,
|
||||
inputs=inputs if isinstance(inputs, dict) else {"value": inputs},
|
||||
timeout_seconds=timeout,
|
||||
max_output_chars=policy.max_output_chars,
|
||||
memory_limit_mb=policy.memory_limit_mb,
|
||||
allow_network=allow_network,
|
||||
)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
return NodeResult(
|
||||
data=outcome["result"],
|
||||
metadata={"stdout": outcome["stdout"][-4000:], "stderr": outcome["stderr"][-2000:]},
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["CodeNodeExecutor", "HttpNodeExecutor", "SqlReadNodeExecutor"]
|
||||
Reference in new issue
Block a user