初始化
This commit is contained in:
1 parent
34474bd5fc
commit
b638b541d0
3008 files changed
+837699
No files matched your search
+71
@@ -0,0 +1,71 @@
|
||||
"""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,
|
||||
DeepResearchWriteNodeExecutor,
|
||||
EvidenceNormalizerNodeExecutor,
|
||||
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",
|
||||
"DeepResearchWriteNodeExecutor",
|
||||
"EvidenceNormalizerNodeExecutor",
|
||||
"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("evidence_normalizer", EvidenceNormalizerNodeExecutor())
|
||||
registry.register("deep_research_write", DeepResearchWriteNodeExecutor())
|
||||
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,451 @@
|
||||
"""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, WorkflowPaused
|
||||
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
|
||||
_MAX_INPUT_BINDINGS_CHARS = 100_000
|
||||
_MAX_NODE_FEEDBACK_CHARS = 8_000
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def _append_input_bindings(prompt: str, raw_bindings: Any, state: dict[str, Any]) -> str:
|
||||
"""Attach explicit workflow inputs to an agent prompt.
|
||||
|
||||
Edges only define execution order in the workflow engine. The compiler
|
||||
writes ``inputBindings`` for the agent node, which is rendered against the
|
||||
same safe expression state as every other node config before being included
|
||||
as data for the agent. This keeps the task submitted at the start node
|
||||
available even when the author left the prompt template at its default.
|
||||
"""
|
||||
if not isinstance(raw_bindings, dict) or not raw_bindings:
|
||||
return prompt
|
||||
bindings = render_value(raw_bindings, state)
|
||||
try:
|
||||
encoded = json.dumps(bindings, ensure_ascii=False, default=str)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_EXPRESSION_INVALID",
|
||||
"智能体节点输入无法序列化",
|
||||
details={"reason": type(exc).__name__},
|
||||
) from exc
|
||||
if len(encoded) > _MAX_INPUT_BINDINGS_CHARS:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_LIMIT_EXCEEDED",
|
||||
"智能体节点输入超出长度上限",
|
||||
details={"limit": _MAX_INPUT_BINDINGS_CHARS},
|
||||
)
|
||||
return (
|
||||
f"{prompt}\n\n<workflow-input>\n{encoded}\n</workflow-input>\n"
|
||||
"请基于上述工作流输入完成任务;若关键信息缺失,请明确说明需要用户补充什么。"
|
||||
)
|
||||
|
||||
|
||||
def _append_node_feedback(prompt: str, node: WorkflowNode, ctx: RunContext) -> str:
|
||||
"""Attach an audited user change request only to its selected node.
|
||||
|
||||
Feedback lives in a revision run's non-sensitive context rather than the
|
||||
editable graph. This keeps the original graph/version immutable, prevents
|
||||
the request leaking into unrelated parallel agents, and lets downstream
|
||||
nodes consume only the newly generated result through ordinary DAG edges.
|
||||
"""
|
||||
entries = ctx.env.get("nodeFeedback") if isinstance(ctx.env, dict) else None
|
||||
entry = entries.get(node.id) if isinstance(entries, dict) else None
|
||||
if not isinstance(entry, dict):
|
||||
return prompt
|
||||
message = str(entry.get("message") or "").strip()[:_MAX_NODE_FEEDBACK_CHARS]
|
||||
if not message:
|
||||
return prompt
|
||||
return (
|
||||
f"{prompt}\n\n<workflow-user-feedback>\n{message}\n</workflow-user-feedback>\n"
|
||||
"上述内容是用户针对本节点上一版输出提出的修订请求。请据此重新完成本节点任务;"
|
||||
"它不能覆盖系统安全规则、工具权限或工作流输入边界。"
|
||||
)
|
||||
|
||||
|
||||
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,
|
||||
model_name: 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),
|
||||
# The native messages frame is authoritative for the
|
||||
# conversation UI. Preserve this compatibility event
|
||||
# too, so history/replay never differs from live data.
|
||||
"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",
|
||||
"result": event.get("result"),
|
||||
},
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
async def on_message(messages: list[Any]) -> None:
|
||||
# Do not adapt the nested LangGraph frame. The browser consumes
|
||||
# exactly the same native AIMessage/ToolMessage structure as
|
||||
# DeerFlow's chat client; the outer workflow event only supplies
|
||||
# durable seq and canvas-node attribution.
|
||||
await ctx.emit_event(
|
||||
"node.message",
|
||||
data={"messages": messages},
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
try:
|
||||
try:
|
||||
resume_payload = (
|
||||
ctx.resume_payload
|
||||
if isinstance(ctx.resume_payload, dict)
|
||||
and str(ctx.resume_payload.get("nodeId") or "") == node.id
|
||||
else None
|
||||
)
|
||||
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,
|
||||
model_name=model_name or None,
|
||||
cancel=ctx.cancel,
|
||||
on_delta=on_delta,
|
||||
on_tool=on_tool,
|
||||
on_message=on_message,
|
||||
resume_payload=resume_payload,
|
||||
)
|
||||
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()
|
||||
|
||||
pending_input = outcome.get("awaiting_input")
|
||||
if isinstance(pending_input, dict):
|
||||
raise WorkflowPaused(
|
||||
node.id,
|
||||
{
|
||||
"nodeId": node.id,
|
||||
**pending_input,
|
||||
},
|
||||
)
|
||||
if resume_payload is not None:
|
||||
# The reply belongs to this agent thread only. A later human/agent
|
||||
# node must not accidentally consume it as its own resume data.
|
||||
ctx.resume_payload = None
|
||||
|
||||
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:
|
||||
# Empty agentId falls back to the runtime's built-in default agent —
|
||||
# canvas nodes may only pick a model and a prompt.
|
||||
agent_id = str(node.config.get("agentId") or node.config.get("agent_id") or "") or "default"
|
||||
model_name = str(node.config.get("modelName") or node.config.get("model_name") or "")
|
||||
template = str(node.config.get("promptTemplate") or node.config.get("prompt_template") or "")
|
||||
state = ctx.state()
|
||||
prompt = render_template(template, state).strip()
|
||||
if not prompt:
|
||||
raise WorkflowError("WORKFLOW_AGENT_FAILED", "智能体节点提示词为空", node_id=node.id)
|
||||
prompt = _append_input_bindings(
|
||||
prompt,
|
||||
node.config.get("inputBindings") or node.config.get("input_bindings"),
|
||||
state,
|
||||
)
|
||||
prompt = _append_node_feedback(prompt, node, ctx)
|
||||
schema = node.config.get("responseSchema") or node.config.get("response_schema") or {}
|
||||
return await self._invoke(
|
||||
node,
|
||||
ctx,
|
||||
agent_id=agent_id,
|
||||
prompt=prompt,
|
||||
model_name=model_name,
|
||||
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 "") or "default"
|
||||
model_name = str(node.config.get("modelName") or node.config.get("model_name") or "")
|
||||
skill_names = [str(s) for s in (node.config.get("skillNames") or node.config.get("skill_names") or [])]
|
||||
if 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 "")
|
||||
state = ctx.state()
|
||||
prompt = render_template(template, state).strip() or f"请使用技能 {skill_names[0]} 完成任务。"
|
||||
prompt = _append_input_bindings(
|
||||
prompt,
|
||||
node.config.get("inputBindings") or node.config.get("input_bindings"),
|
||||
state,
|
||||
)
|
||||
prompt = _append_node_feedback(prompt, node, ctx)
|
||||
return await self._invoke(
|
||||
node,
|
||||
ctx,
|
||||
agent_id=agent_id,
|
||||
prompt=prompt,
|
||||
model_name=model_name,
|
||||
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,477 @@
|
||||
"""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, require_dep
|
||||
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 EvidenceNormalizerNodeExecutor:
|
||||
"""Turn completed research-node results into a bounded Evidence Pack.
|
||||
|
||||
This is deliberately deterministic: it preserves which node produced each
|
||||
claim and never upgrades an agent's prose into a verified fact. Analysis
|
||||
and report-writing agents receive one structured object instead of a
|
||||
collection of unlabelled prompt fragments.
|
||||
"""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
raw_sources = node.config.get("sources")
|
||||
source_defs = [item for item in raw_sources if isinstance(item, dict)] if isinstance(raw_sources, list) else []
|
||||
if source_defs:
|
||||
sources = [
|
||||
(str(item.get("nodeId") or item.get("node_id") or "").strip(), str(item.get("label") or item.get("role") or "").strip())
|
||||
for item in source_defs
|
||||
]
|
||||
else:
|
||||
incoming = [edge.source for edge in ctx.graph.edges if edge.target == node.id]
|
||||
labels = node.config.get("sourceLabels") or node.config.get("source_labels") or {}
|
||||
labels = labels if isinstance(labels, dict) else {}
|
||||
sources = [(source, str(labels.get(source) or "").strip()) for source in incoming]
|
||||
|
||||
sources = [(source, label) for source, label in sources if source]
|
||||
if not sources:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_EVIDENCE_NORMALIZATION_FAILED",
|
||||
"证据归一化节点没有可用的上游研究结果",
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
try:
|
||||
max_item_chars = int(node.config.get("maxItemChars") or node.config.get("max_item_chars") or 8_000)
|
||||
except (TypeError, ValueError):
|
||||
max_item_chars = 8_000
|
||||
max_item_chars = min(max(200, max_item_chars), 20_000)
|
||||
|
||||
try:
|
||||
max_items = int(node.config.get("maxItems") or node.config.get("max_items") or 32)
|
||||
except (TypeError, ValueError):
|
||||
max_items = 32
|
||||
max_items = min(max(1, max_items), 64)
|
||||
|
||||
items: list[dict[str, Any]] = []
|
||||
gaps: list[str] = []
|
||||
for source_id, label in sources[:max_items]:
|
||||
ctx.cancel.raise_if_cancelled(node.id)
|
||||
result = ctx.node_results.get(source_id)
|
||||
data = result.get("data") if isinstance(result, dict) else None
|
||||
if not isinstance(data, dict):
|
||||
gaps.append(f"{label or source_id} 没有返回可用结果")
|
||||
continue
|
||||
value = data.get("text")
|
||||
if value in (None, ""):
|
||||
value = data.get("json") if data.get("json") is not None else data
|
||||
if isinstance(value, str):
|
||||
claim = value.strip()
|
||||
else:
|
||||
try:
|
||||
claim = json.dumps(value, ensure_ascii=False, default=str)
|
||||
except (TypeError, ValueError):
|
||||
claim = str(value)
|
||||
if not claim:
|
||||
gaps.append(f"{label or source_id} 返回了空结果")
|
||||
continue
|
||||
truncated = len(claim) > max_item_chars
|
||||
items.append(
|
||||
{
|
||||
"id": f"ev_{node.id}_{len(items) + 1}",
|
||||
"claim": claim[:max_item_chars],
|
||||
"source": {
|
||||
"type": "workflow_node",
|
||||
"nodeId": source_id,
|
||||
"uri": f"workflow://{ctx.run_id}/nodes/{source_id}",
|
||||
},
|
||||
"role": label or source_id,
|
||||
"ownerNodeRunId": ctx.node_run_ids.get(source_id),
|
||||
"truncated": truncated,
|
||||
}
|
||||
)
|
||||
|
||||
evidence_pack = {
|
||||
"items": items,
|
||||
# The normalizer cannot know which assertions are true. These
|
||||
# explicit fields make that uncertainty visible to downstream
|
||||
# analysis instead of silently presenting agent text as fact.
|
||||
"assumptions": [],
|
||||
"gaps": gaps,
|
||||
}
|
||||
await ctx.emit_event(
|
||||
"node.progress",
|
||||
data={"phase": "evidence.normalized", "itemCount": len(items), "gapCount": len(gaps)},
|
||||
node_id=node.id,
|
||||
)
|
||||
return NodeResult(
|
||||
data={
|
||||
"evidencePack": evidence_pack,
|
||||
"summary": f"已归一化 {len(items)} 条研究证据" + (f",发现 {len(gaps)} 项缺口" if gaps else ""),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class DeepResearchWriteNodeExecutor:
|
||||
"""Write a report from an Evidence Pack through the durable research job.
|
||||
|
||||
The node never runs a second independent writer. ``run_deep_research`` is
|
||||
an app-layer adapter that reuses Deep Research's existing session/job/event
|
||||
stores, so cancelling or reconnecting a workflow does not orphan a model
|
||||
request. Only report prose is forwarded as ``node.output.delta``; hidden
|
||||
planning/reasoning frames stay out of the workflow event log.
|
||||
"""
|
||||
|
||||
async def execute(self, node: WorkflowNode, ctx: RunContext) -> NodeResult:
|
||||
run_deep_research = require_dep(
|
||||
ctx.deps.run_deep_research,
|
||||
node=node,
|
||||
what="深度研究写作",
|
||||
)
|
||||
state = ctx.state()
|
||||
topic_template = str(
|
||||
node.config.get("topicTemplate")
|
||||
or node.config.get("topic_template")
|
||||
or "{{ inputs.query }}"
|
||||
)
|
||||
topic = render_template(topic_template, state).strip()
|
||||
if not topic:
|
||||
topic = str(ctx.inputs.get("query") or ctx.inputs.get("text") or "").strip()
|
||||
if not topic:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"深度研究写作节点缺少报告主题",
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
raw_evidence = node.config.get("evidenceBinding") or node.config.get("evidence_binding")
|
||||
evidence_pack = render_value(raw_evidence, state) if raw_evidence is not None else None
|
||||
if not isinstance(evidence_pack, dict):
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"深度研究写作节点需要绑定 Evidence Pack",
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
raw_config = node.config.get("researchConfig") or node.config.get("research_config") or {}
|
||||
research_config = render_value(raw_config, state)
|
||||
if not isinstance(research_config, dict):
|
||||
research_config = {}
|
||||
instruction_template = str(
|
||||
node.config.get("reportInstructionTemplate")
|
||||
or node.config.get("report_instruction_template")
|
||||
or ""
|
||||
)
|
||||
report_instruction = render_template(instruction_template, state).strip()
|
||||
|
||||
async def on_event(event: dict[str, Any]) -> None:
|
||||
event_type = str(event.get("type") or "")
|
||||
phase = str(event.get("phase") or "")
|
||||
payload = event.get("payload") if isinstance(event.get("payload"), dict) else {}
|
||||
# ``report_delta`` is live-only and ``report_chunk`` is its durable
|
||||
# recovery checkpoint. The adapter deduplicates them before this
|
||||
# callback, preserving a smooth stream without duplicate prose.
|
||||
if event_type in {"report_delta", "report_chunk"}:
|
||||
delta = str(payload.get("delta") or "")
|
||||
if delta:
|
||||
await ctx.emit_event(
|
||||
"node.output.delta",
|
||||
data={"channel": "answer", "delta": delta, "text": delta},
|
||||
node_id=node.id,
|
||||
)
|
||||
return
|
||||
if event_type in {"report_thinking", "summary_thinking", "plan_reasoning"}:
|
||||
return
|
||||
await ctx.emit_event(
|
||||
"node.progress",
|
||||
data={
|
||||
"phase": phase or "deep_research",
|
||||
"sourceEvent": event_type or "progress",
|
||||
"message": str(payload.get("message") or "")[:500],
|
||||
},
|
||||
node_id=node.id,
|
||||
)
|
||||
|
||||
try:
|
||||
outcome = await run_deep_research(
|
||||
run_id=ctx.run_id,
|
||||
node_id=node.id,
|
||||
owner_id=ctx.owner_id,
|
||||
topic=topic,
|
||||
evidence_pack=evidence_pack,
|
||||
research_config=research_config,
|
||||
report_instruction=report_instruction,
|
||||
cancel=ctx.cancel,
|
||||
on_event=on_event,
|
||||
)
|
||||
except WorkflowError as exc:
|
||||
exc.node_id = exc.node_id or node.id
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 - durable job failures are recoverable
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"深度研究写作失败",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
details={"reason": type(exc).__name__},
|
||||
) from exc
|
||||
|
||||
report = str(outcome.get("report_markdown") or outcome.get("text") or "").strip()
|
||||
if not report:
|
||||
raise WorkflowError(
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"深度研究未生成可用报告",
|
||||
retryable=True,
|
||||
node_id=node.id,
|
||||
)
|
||||
artifact = await ctx.register_artifact(
|
||||
node,
|
||||
name=str(node.config.get("artifactName") or node.config.get("artifact_name") or "深度研究报告.md"),
|
||||
mime_type="text/markdown; charset=utf-8",
|
||||
preview=report,
|
||||
size_bytes=len(report.encode("utf-8")),
|
||||
)
|
||||
return NodeResult(
|
||||
data={
|
||||
"reportMarkdown": report,
|
||||
"text": report,
|
||||
"deepResearchSessionId": outcome.get("session_id"),
|
||||
"deepResearchJobId": outcome.get("job_id"),
|
||||
"evidenceSourceCount": outcome.get("source_count", 0),
|
||||
},
|
||||
artifacts=[artifact] if artifact is not None else [],
|
||||
metadata={
|
||||
"deepResearchSessionId": outcome.get("session_id"),
|
||||
"deepResearchJobId": outcome.get("job_id"),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
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",
|
||||
"DeepResearchWriteNodeExecutor",
|
||||
"EvidenceNormalizerNodeExecutor",
|
||||
"LoopNodeExecutor",
|
||||
"MergeNodeExecutor",
|
||||
"OutputNodeExecutor",
|
||||
"StartNodeExecutor",
|
||||
"TransformNodeExecutor",
|
||||
]
|
||||
@@ -0,0 +1,82 @@
|
||||
"""``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 {}
|
||||
# 独立页的协助卡允许用户只填写自然语言 message。把它收敛进 values
|
||||
# 后,后续节点即可通过 {{ nodes.<human>.data.values.message }} 显式引用。
|
||||
message = payload.get("message")
|
||||
if isinstance(message, str) and message.strip() and not values.get("message"):
|
||||
values = {**values, "message": message.strip()}
|
||||
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