feat(workflow): 增加会话级多智能体任务规划运行能力
提交范围聚焦于工作流编排新增:新增 workflow_planning 路由与前后端运行入口(workflow_planning.py、workflow_proposal_planner.py、workflow_deep_research_adapter.py),支持基于会话的提案-规划-执行流程,满足任务驱动式多智能体协作。 完成网关层运行时适配:扩展 workflow_resource_checks、workflow_executor、workflow_runs、workflow_agent_runner 与依赖注入,新增/更新配置(config.yaml、workflow_config),并补充 lead_agent 路径与节点实现,确保 Planner 与普通工作流运行态衔接一致。 补齐持久化与建模:新增 workflow_planning 持久化模块与 MySQL migration(20260831_01_workflow_planning_sessions),注册 SQL 与模型、内存 store,并扩展错误/表达式/validator/节点语义以承载新会话态输入输出。 增强文档与测试:新增会话式多智能体编排中文说明、更新开发文档与 AGENTS 指南、补充 workflow_engine/workflow_runs 回归测试,提高新路径的行为可验证性;同时留存 seed 资产(workflow-planner)便于一体化部署。
This commit is contained in:
1 parent
ac92f29db1
commit
1706a24182
39 files changed
+4896
-81
No files matched your search
+19
-14
@@ -401,9 +401,9 @@ def _build_middlewares(
|
||||
if summarization_middleware is not None:
|
||||
middlewares.append(summarization_middleware)
|
||||
|
||||
# Add TodoList middleware if plan mode is enabled
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
# Add TodoList middleware if plan mode is enabled
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
is_writing_setup = cfg.get("is_writing_setup", False)
|
||||
is_report_structure_setup = cfg.get("is_report_structure_setup", False)
|
||||
todo_list_middleware = _create_todo_list_middleware(is_plan_mode)
|
||||
@@ -574,9 +574,10 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
# actually injected. Default off → no behavior change for normal chats.
|
||||
thinking_force_disabled = bool(cfg.get("thinking_force_disabled", False))
|
||||
reasoning_effort = cfg.get("reasoning_effort", None)
|
||||
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
subagent_enabled = cfg.get("subagent_enabled", False)
|
||||
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
|
||||
is_plan_mode = cfg.get("is_plan_mode", False)
|
||||
disable_tools = bool(cfg.get("disable_tools", False))
|
||||
subagent_enabled = cfg.get("subagent_enabled", False)
|
||||
max_concurrent_subagents = cfg.get("max_concurrent_subagents", 3)
|
||||
is_bootstrap = cfg.get("is_bootstrap", False)
|
||||
# 写作配置 Q&A:与 is_bootstrap 同构的另一种「最小化 bootstrap」——挂 setup_writing 工具、
|
||||
@@ -770,14 +771,18 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
# Default lead agent (unchanged behavior)
|
||||
is_notebook_mode = bool(cfg.get("notebook_search_space_id"))
|
||||
notebook_source_ids: list[int] = cfg.get("notebook_source_ids") or []
|
||||
default_tools = get_available_tools(
|
||||
model_name=model_name,
|
||||
groups=agent_config.tool_groups if agent_config else None,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
default_tools = (
|
||||
[]
|
||||
if disable_tools
|
||||
else get_available_tools(
|
||||
model_name=model_name,
|
||||
groups=agent_config.tool_groups if agent_config else None,
|
||||
subagent_enabled=subagent_enabled,
|
||||
is_scheduled_run=cfg.get("is_scheduled_run", False),
|
||||
excluded_tools=requested_excluded_tools,
|
||||
allow_agent_orchestration=agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID,
|
||||
app_config=resolved_app_config,
|
||||
)
|
||||
)
|
||||
if agent_id == FORCED_RESEARCH_AGENT_ID:
|
||||
default_tools = filter_forced_research_tools(default_tools)
|
||||
|
||||
+7
-1
@@ -71,7 +71,13 @@ class WorkflowConfig(BaseModel):
|
||||
max_loop_iterations: int = Field(default=20, ge=1)
|
||||
max_parallelism: int = Field(default=16, ge=1)
|
||||
run_timeout_seconds: int = Field(default=3600, ge=1)
|
||||
node_timeout_seconds: int = Field(default=600, ge=1)
|
||||
# LangGraph counts a model → tool turn as roughly two graph steps. Deep
|
||||
# research agents therefore routinely exceed a chat-sized limit of 60.
|
||||
agent_recursion_limit: int = Field(default=250, ge=20, le=1000)
|
||||
# Deep Research report nodes are durable jobs and can legitimately need
|
||||
# longer than a normal chat/tool step. Individual graph node timeouts stay
|
||||
# opt-in, but the system cap must not silently kill those report jobs.
|
||||
node_timeout_seconds: int = Field(default=1800, ge=1)
|
||||
max_concurrent_runs_per_user: int = Field(default=5, ge=1)
|
||||
lease_ttl_seconds: int = Field(default=60, ge=10)
|
||||
embed: WorkflowEmbedConfig = Field(default_factory=WorkflowEmbedConfig)
|
||||
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
"""Create durable pre-run workflow planning sessions and proposals.
|
||||
|
||||
Revision ID: 20260831_01
|
||||
Revises: 20260829_02
|
||||
Create Date: 2026-08-31
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
|
||||
revision = "20260831_01"
|
||||
down_revision = "20260829_02"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _large_text_type() -> sa.types.TypeEngine:
|
||||
bind = op.get_bind()
|
||||
return LONGTEXT() if bind.dialect.name == "mysql" else sa.Text()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
text_type = _large_text_type()
|
||||
|
||||
if not inspector.has_table("workflow_planning_sessions"):
|
||||
op.create_table(
|
||||
"workflow_planning_sessions",
|
||||
sa.Column("id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("workflow_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("owner_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("query", sa.Text(), nullable=False),
|
||||
sa.Column("status", sa.String(length=32), nullable=False, server_default="proposed"),
|
||||
sa.Column("source_draft_revision", sa.Integer(), nullable=True),
|
||||
sa.Column("selected_proposal_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("confirmed_run_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_workflow_planning_sessions_workflow_id", "workflow_planning_sessions", ["workflow_id"])
|
||||
op.create_index("ix_workflow_planning_sessions_owner_id", "workflow_planning_sessions", ["owner_id"])
|
||||
op.create_index("ix_workflow_planning_sessions_status", "workflow_planning_sessions", ["status"])
|
||||
op.create_index("ix_workflow_planning_sessions_confirmed_run_id", "workflow_planning_sessions", ["confirmed_run_id"])
|
||||
|
||||
if not inspector.has_table("workflow_planning_proposals"):
|
||||
op.create_table(
|
||||
"workflow_planning_proposals",
|
||||
sa.Column("id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("session_id", sa.String(length=64), sa.ForeignKey("workflow_planning_sessions.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("position", sa.Integer(), nullable=False),
|
||||
sa.Column("status", sa.String(length=32), nullable=False, server_default="proposed"),
|
||||
sa.Column("strategy", sa.String(length=64), nullable=False, server_default="collaborative"),
|
||||
sa.Column("title", sa.String(length=255), nullable=False),
|
||||
sa.Column("summary", sa.Text(), nullable=False, server_default=""),
|
||||
sa.Column("rationale", sa.Text(), nullable=False, server_default=""),
|
||||
sa.Column("estimated_duration_seconds", sa.Integer(), nullable=True),
|
||||
sa.Column("graph_json", text_type, nullable=False),
|
||||
sa.Column("canvas_schema_json", text_type, nullable=False),
|
||||
sa.Column("validation_json", text_type, nullable=False),
|
||||
sa.Column("graph_revision", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.UniqueConstraint("session_id", "position", name="uq_workflow_planning_proposals_position"),
|
||||
)
|
||||
op.create_index("ix_workflow_planning_proposals_session_id", "workflow_planning_proposals", ["session_id"])
|
||||
op.create_index("ix_workflow_planning_proposals_status", "workflow_planning_proposals", ["status"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
if inspector.has_table("workflow_planning_proposals"):
|
||||
op.drop_table("workflow_planning_proposals")
|
||||
if inspector.has_table("workflow_planning_sessions"):
|
||||
op.drop_table("workflow_planning_sessions")
|
||||
+6
@@ -57,6 +57,10 @@ from deerflow.persistence.workflow_runs.model import (
|
||||
WorkflowRunArtifactRow,
|
||||
WorkflowRunRow,
|
||||
)
|
||||
from deerflow.persistence.workflow_planning.model import (
|
||||
WorkflowPlanningProposalRow,
|
||||
WorkflowPlanningSessionRow,
|
||||
)
|
||||
from deerflow.persistence.workflows.model import WorkflowDefinitionRow, WorkflowVersionRow
|
||||
from deerflow.persistence.llm_metrics.model import LlmCallMetricRow
|
||||
from deerflow.persistence.llmwiki.model import LlmWikiConversationDepositRow, LlmWikiKnowledgeBaseRow
|
||||
@@ -142,6 +146,8 @@ __all__ = [
|
||||
"WorkflowRunArtifactRow",
|
||||
"WorkflowRunEventRow",
|
||||
"WorkflowRunRow",
|
||||
"WorkflowPlanningProposalRow",
|
||||
"WorkflowPlanningSessionRow",
|
||||
"WorkflowVersionRow",
|
||||
"LlmCallMetricRow",
|
||||
"LlmWikiKnowledgeBaseRow",
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
"""Planning-session persistence public API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.workflow_planning.base import (
|
||||
WorkflowPlanningConflictError,
|
||||
WorkflowPlanningProposalNotFoundError,
|
||||
WorkflowPlanningSessionNotFoundError,
|
||||
WorkflowPlanningStore,
|
||||
)
|
||||
from deerflow.persistence.workflow_planning.memory import MemoryWorkflowPlanningStore
|
||||
from deerflow.persistence.workflow_planning.model import (
|
||||
WorkflowPlanningProposalRow,
|
||||
WorkflowPlanningSessionRow,
|
||||
)
|
||||
from deerflow.persistence.workflow_planning.sql import SqlWorkflowPlanningStore
|
||||
|
||||
__all__ = [
|
||||
"MemoryWorkflowPlanningStore",
|
||||
"SqlWorkflowPlanningStore",
|
||||
"WorkflowPlanningConflictError",
|
||||
"WorkflowPlanningProposalNotFoundError",
|
||||
"WorkflowPlanningProposalRow",
|
||||
"WorkflowPlanningSessionNotFoundError",
|
||||
"WorkflowPlanningSessionRow",
|
||||
"WorkflowPlanningStore",
|
||||
"make_workflow_planning_store",
|
||||
]
|
||||
|
||||
|
||||
def make_workflow_planning_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryWorkflowPlanningStore()
|
||||
return SqlWorkflowPlanningStore(session_factory)
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
"""Durable planning-session storage for conversational workflow proposals.
|
||||
|
||||
A planning session exists *before* a workflow run. It holds several proposed
|
||||
graphs, lets the owner select and edit one, and records the final run once it
|
||||
has been confirmed. Keeping this state outside ``workflow_runs`` means a
|
||||
user can explore plans without creating/cancelling fake executions.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class WorkflowPlanningSessionNotFoundError(Exception):
|
||||
def __init__(self, session_id: str) -> None:
|
||||
super().__init__(f"workflow planning session not found: {session_id}")
|
||||
self.session_id = session_id
|
||||
|
||||
|
||||
class WorkflowPlanningProposalNotFoundError(Exception):
|
||||
def __init__(self, session_id: str, proposal_id: str) -> None:
|
||||
super().__init__(f"workflow planning proposal not found: {session_id}/{proposal_id}")
|
||||
self.session_id = session_id
|
||||
self.proposal_id = proposal_id
|
||||
|
||||
|
||||
class WorkflowPlanningConflictError(Exception):
|
||||
"""Raised for stale graph edits or a session that has already been confirmed."""
|
||||
|
||||
def __init__(self, session_id: str, message: str, *, current_revision: int | None = None) -> None:
|
||||
super().__init__(message)
|
||||
self.session_id = session_id
|
||||
self.current_revision = current_revision
|
||||
|
||||
|
||||
class WorkflowPlanningStore(ABC):
|
||||
@abstractmethod
|
||||
async def create_session(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Persist a new session with its initial list of proposals."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_session(self, session_id: str) -> dict[str, Any] | None:
|
||||
"""Return a session with proposals ordered by ``position``."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str | None,
|
||||
validation: dict[str, Any] | None,
|
||||
expected_revision: int,
|
||||
) -> dict[str, Any]:
|
||||
"""CAS-update an editable proposal graph and return the session."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def select_proposal(self, session_id: str, proposal_id: str) -> dict[str, Any]:
|
||||
"""Select a proposal. Selection is reversible until confirmation."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def confirm_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
run_id: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Bind the selected proposal to its newly-created durable run."""
|
||||
raise NotImplementedError
|
||||
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
"""In-memory planning-session store for tests and no-DB deployments."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.workflow_planning.base import (
|
||||
WorkflowPlanningConflictError,
|
||||
WorkflowPlanningProposalNotFoundError,
|
||||
WorkflowPlanningSessionNotFoundError,
|
||||
WorkflowPlanningStore,
|
||||
)
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(UTC).isoformat()
|
||||
|
||||
|
||||
class MemoryWorkflowPlanningStore(WorkflowPlanningStore):
|
||||
def __init__(self) -> None:
|
||||
self._sessions: dict[str, dict[str, Any]] = {}
|
||||
self._proposals: dict[str, dict[str, Any]] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
def _session(self, session_id: str) -> dict[str, Any]:
|
||||
session = self._sessions.get(session_id)
|
||||
if session is None:
|
||||
raise WorkflowPlanningSessionNotFoundError(session_id)
|
||||
return session
|
||||
|
||||
def _proposal(self, session_id: str, proposal_id: str) -> dict[str, Any]:
|
||||
proposal = self._proposals.get(proposal_id)
|
||||
if proposal is None or proposal["session_id"] != session_id:
|
||||
raise WorkflowPlanningProposalNotFoundError(session_id, proposal_id)
|
||||
return proposal
|
||||
|
||||
def _public(self, session: dict[str, Any]) -> dict[str, Any]:
|
||||
proposals = [p for p in self._proposals.values() if p["session_id"] == session["id"]]
|
||||
proposals.sort(key=lambda item: item["position"])
|
||||
return copy.deepcopy({**session, "proposals": proposals})
|
||||
|
||||
async def create_session(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
async with self._lock:
|
||||
now = _now()
|
||||
session_id = str(data.get("id") or f"wps_{uuid.uuid4().hex[:16]}")
|
||||
session = {
|
||||
"id": session_id,
|
||||
"workflow_id": str(data["workflow_id"]),
|
||||
"owner_id": str(data["owner_id"]),
|
||||
"query": str(data["query"]),
|
||||
"status": "proposed",
|
||||
"source_draft_revision": data.get("source_draft_revision"),
|
||||
"selected_proposal_id": None,
|
||||
"confirmed_run_id": None,
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._sessions[session_id] = session
|
||||
for position, raw in enumerate(data.get("proposals") or []):
|
||||
proposal_id = str(raw.get("id") or f"wpp_{uuid.uuid4().hex[:16]}")
|
||||
self._proposals[proposal_id] = {
|
||||
"id": proposal_id,
|
||||
"session_id": session_id,
|
||||
"position": int(raw.get("position", position)),
|
||||
"status": "proposed",
|
||||
"strategy": str(raw.get("strategy") or "collaborative"),
|
||||
"title": str(raw.get("title") or "候选流程"),
|
||||
"summary": str(raw.get("summary") or ""),
|
||||
"rationale": str(raw.get("rationale") or ""),
|
||||
"estimated_duration_seconds": raw.get("estimated_duration_seconds"),
|
||||
"graph": copy.deepcopy(raw.get("graph") or {}),
|
||||
"canvas_schema": str(raw.get("canvas_schema") or "{}"),
|
||||
"validation": copy.deepcopy(raw.get("validation") or {}),
|
||||
"graph_revision": int(raw.get("graph_revision") or 0),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
return self._public(session)
|
||||
|
||||
async def get_session(self, session_id: str) -> dict[str, Any] | None:
|
||||
async with self._lock:
|
||||
session = self._sessions.get(session_id)
|
||||
return self._public(session) if session is not None else None
|
||||
|
||||
async def update_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str | None,
|
||||
validation: dict[str, Any] | None,
|
||||
expected_revision: int,
|
||||
) -> dict[str, Any]:
|
||||
async with self._lock:
|
||||
session = self._session(session_id)
|
||||
if session["status"] == "confirmed":
|
||||
raise WorkflowPlanningConflictError(session_id, "候选流程已经确认,不能继续编辑")
|
||||
proposal = self._proposal(session_id, proposal_id)
|
||||
if proposal["graph_revision"] != expected_revision:
|
||||
raise WorkflowPlanningConflictError(
|
||||
session_id,
|
||||
"候选流程已被其他修改更新,请刷新后重试",
|
||||
current_revision=proposal["graph_revision"],
|
||||
)
|
||||
proposal["graph"] = copy.deepcopy(graph)
|
||||
if canvas_schema is not None:
|
||||
proposal["canvas_schema"] = canvas_schema
|
||||
if validation is not None:
|
||||
proposal["validation"] = copy.deepcopy(validation)
|
||||
proposal["graph_revision"] += 1
|
||||
proposal["updated_at"] = _now()
|
||||
session["updated_at"] = proposal["updated_at"]
|
||||
return self._public(session)
|
||||
|
||||
async def select_proposal(self, session_id: str, proposal_id: str) -> dict[str, Any]:
|
||||
async with self._lock:
|
||||
session = self._session(session_id)
|
||||
if session["status"] == "confirmed":
|
||||
raise WorkflowPlanningConflictError(session_id, "规划会话已经确认")
|
||||
self._proposal(session_id, proposal_id)
|
||||
for proposal in self._proposals.values():
|
||||
if proposal["session_id"] == session_id:
|
||||
proposal["status"] = "selected" if proposal["id"] == proposal_id else "proposed"
|
||||
proposal["updated_at"] = _now()
|
||||
session["selected_proposal_id"] = proposal_id
|
||||
session["status"] = "selected"
|
||||
session["updated_at"] = _now()
|
||||
return self._public(session)
|
||||
|
||||
async def confirm_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
run_id: str,
|
||||
) -> dict[str, Any]:
|
||||
async with self._lock:
|
||||
session = self._session(session_id)
|
||||
self._proposal(session_id, proposal_id)
|
||||
if session["status"] == "confirmed":
|
||||
if session["selected_proposal_id"] == proposal_id and session["confirmed_run_id"] == run_id:
|
||||
return self._public(session)
|
||||
raise WorkflowPlanningConflictError(session_id, "规划会话已经确认到另一条运行")
|
||||
if session["selected_proposal_id"] != proposal_id:
|
||||
raise WorkflowPlanningConflictError(session_id, "请先选择该候选流程")
|
||||
session["status"] = "confirmed"
|
||||
session["confirmed_run_id"] = run_id
|
||||
session["updated_at"] = _now()
|
||||
for proposal in self._proposals.values():
|
||||
if proposal["session_id"] == session_id:
|
||||
proposal["status"] = "confirmed" if proposal["id"] == proposal_id else "proposed"
|
||||
proposal["updated_at"] = session["updated_at"]
|
||||
return self._public(session)
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
"""ORM rows for pre-run workflow planning sessions and candidate graphs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import ForeignKey, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy import text as sa_text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
|
||||
|
||||
|
||||
class WorkflowPlanningSessionRow(Base):
|
||||
__tablename__ = "workflow_planning_sessions"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
workflow_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
owner_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
query: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
# proposed | selected | confirmed | expired
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="proposed", index=True)
|
||||
source_draft_revision: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
selected_proposal_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
confirmed_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
)
|
||||
|
||||
|
||||
class WorkflowPlanningProposalRow(Base):
|
||||
__tablename__ = "workflow_planning_proposals"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("session_id", "position", name="uq_workflow_planning_proposals_position"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
session_id: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
ForeignKey("workflow_planning_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
position: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
# proposed | selected | confirmed
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="proposed", index=True)
|
||||
strategy: Mapped[str] = mapped_column(String(64), nullable=False, default="collaborative")
|
||||
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
summary: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
rationale: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
estimated_duration_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
graph_json: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
||||
# The opaque Coze document is kept beside the executable graph so an
|
||||
# editable candidate never needs to overwrite the user's workflow draft.
|
||||
canvas_schema_json: Mapped[str] = mapped_column(PortableLongText(), default="{}", nullable=False)
|
||||
validation_json: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
||||
graph_revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa_text("0"))
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
)
|
||||
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
"""SQLAlchemy planning-session repository."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.workflow_planning.base import (
|
||||
WorkflowPlanningConflictError,
|
||||
WorkflowPlanningProposalNotFoundError,
|
||||
WorkflowPlanningSessionNotFoundError,
|
||||
WorkflowPlanningStore,
|
||||
)
|
||||
from deerflow.persistence.workflow_planning.model import (
|
||||
WorkflowPlanningProposalRow,
|
||||
WorkflowPlanningSessionRow,
|
||||
)
|
||||
|
||||
|
||||
def _iso(value: datetime | None) -> str | None:
|
||||
return value.isoformat() if isinstance(value, datetime) else None
|
||||
|
||||
|
||||
def _proposal_dict(row: WorkflowPlanningProposalRow) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"session_id": row.session_id,
|
||||
"position": row.position,
|
||||
"status": row.status,
|
||||
"strategy": row.strategy,
|
||||
"title": row.title,
|
||||
"summary": row.summary or "",
|
||||
"rationale": row.rationale or "",
|
||||
"estimated_duration_seconds": row.estimated_duration_seconds,
|
||||
"graph": row.graph_json or {},
|
||||
"canvas_schema": row.canvas_schema_json or "{}",
|
||||
"validation": row.validation_json or {},
|
||||
"graph_revision": row.graph_revision,
|
||||
"created_at": _iso(row.created_at),
|
||||
"updated_at": _iso(row.updated_at),
|
||||
}
|
||||
|
||||
|
||||
def _session_dict(row: WorkflowPlanningSessionRow, proposals: list[WorkflowPlanningProposalRow]) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"workflow_id": row.workflow_id,
|
||||
"owner_id": row.owner_id,
|
||||
"query": row.query,
|
||||
"status": row.status,
|
||||
"source_draft_revision": row.source_draft_revision,
|
||||
"selected_proposal_id": row.selected_proposal_id,
|
||||
"confirmed_run_id": row.confirmed_run_id,
|
||||
"created_at": _iso(row.created_at),
|
||||
"updated_at": _iso(row.updated_at),
|
||||
"proposals": [_proposal_dict(proposal) for proposal in proposals],
|
||||
}
|
||||
|
||||
|
||||
class SqlWorkflowPlanningStore(WorkflowPlanningStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def _proposals(self, session: AsyncSession, session_id: str, *, lock: bool = False) -> list[WorkflowPlanningProposalRow]:
|
||||
stmt = (
|
||||
select(WorkflowPlanningProposalRow)
|
||||
.where(WorkflowPlanningProposalRow.session_id == session_id)
|
||||
.order_by(WorkflowPlanningProposalRow.position.asc())
|
||||
)
|
||||
if lock:
|
||||
stmt = stmt.with_for_update()
|
||||
result = await session.execute(stmt)
|
||||
return list(result.scalars())
|
||||
|
||||
async def create_session(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
session_row = WorkflowPlanningSessionRow(
|
||||
id=str(data.get("id") or f"wps_{uuid.uuid4().hex[:16]}"),
|
||||
workflow_id=str(data["workflow_id"]),
|
||||
owner_id=str(data["owner_id"]),
|
||||
query=str(data["query"]),
|
||||
status="proposed",
|
||||
source_draft_revision=data.get("source_draft_revision"),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
proposals: list[WorkflowPlanningProposalRow] = []
|
||||
for position, raw in enumerate(data.get("proposals") or []):
|
||||
proposals.append(
|
||||
WorkflowPlanningProposalRow(
|
||||
id=str(raw.get("id") or f"wpp_{uuid.uuid4().hex[:16]}"),
|
||||
session_id=session_row.id,
|
||||
position=int(raw.get("position", position)),
|
||||
status="proposed",
|
||||
strategy=str(raw.get("strategy") or "collaborative"),
|
||||
title=str(raw.get("title") or "候选流程"),
|
||||
summary=str(raw.get("summary") or ""),
|
||||
rationale=str(raw.get("rationale") or ""),
|
||||
estimated_duration_seconds=raw.get("estimated_duration_seconds"),
|
||||
graph_json=raw.get("graph") or {},
|
||||
canvas_schema_json=str(raw.get("canvas_schema") or "{}"),
|
||||
validation_json=raw.get("validation") or {},
|
||||
graph_revision=int(raw.get("graph_revision") or 0),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(session_row)
|
||||
# These rows only carry scalar foreign-key values, not an ORM
|
||||
# relationship. Flush the parent explicitly before staging its
|
||||
# proposals so SQLite/MySQL/Postgres all see the referenced
|
||||
# planning-session row during the child insert.
|
||||
await session.flush()
|
||||
session.add_all(proposals)
|
||||
await session.flush()
|
||||
result = _session_dict(session_row, proposals)
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
async def get_session(self, session_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowPlanningSessionRow, session_id)
|
||||
if row is None:
|
||||
return None
|
||||
return _session_dict(row, await self._proposals(session, session_id))
|
||||
|
||||
async def update_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str | None,
|
||||
validation: dict[str, Any] | None,
|
||||
expected_revision: int,
|
||||
) -> dict[str, Any]:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowPlanningSessionRow, session_id, with_for_update=True)
|
||||
if row is None:
|
||||
raise WorkflowPlanningSessionNotFoundError(session_id)
|
||||
if row.status == "confirmed":
|
||||
raise WorkflowPlanningConflictError(session_id, "候选流程已经确认,不能继续编辑")
|
||||
proposals = await self._proposals(session, session_id, lock=True)
|
||||
proposal = next((item for item in proposals if item.id == proposal_id), None)
|
||||
if proposal is None:
|
||||
raise WorkflowPlanningProposalNotFoundError(session_id, proposal_id)
|
||||
if proposal.graph_revision != expected_revision:
|
||||
raise WorkflowPlanningConflictError(
|
||||
session_id,
|
||||
"候选流程已被其他修改更新,请刷新后重试",
|
||||
current_revision=proposal.graph_revision,
|
||||
)
|
||||
proposal.graph_json = graph
|
||||
if canvas_schema is not None:
|
||||
proposal.canvas_schema_json = canvas_schema
|
||||
if validation is not None:
|
||||
proposal.validation_json = validation
|
||||
proposal.graph_revision += 1
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.flush()
|
||||
result = _session_dict(row, proposals)
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
async def select_proposal(self, session_id: str, proposal_id: str) -> dict[str, Any]:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowPlanningSessionRow, session_id, with_for_update=True)
|
||||
if row is None:
|
||||
raise WorkflowPlanningSessionNotFoundError(session_id)
|
||||
if row.status == "confirmed":
|
||||
raise WorkflowPlanningConflictError(session_id, "规划会话已经确认")
|
||||
proposals = await self._proposals(session, session_id, lock=True)
|
||||
if not any(proposal.id == proposal_id for proposal in proposals):
|
||||
raise WorkflowPlanningProposalNotFoundError(session_id, proposal_id)
|
||||
now = datetime.now(UTC)
|
||||
row.status = "selected"
|
||||
row.selected_proposal_id = proposal_id
|
||||
row.updated_at = now
|
||||
for proposal in proposals:
|
||||
proposal.status = "selected" if proposal.id == proposal_id else "proposed"
|
||||
proposal.updated_at = now
|
||||
await session.flush()
|
||||
result = _session_dict(row, proposals)
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
async def confirm_proposal(
|
||||
self,
|
||||
session_id: str,
|
||||
proposal_id: str,
|
||||
*,
|
||||
run_id: str,
|
||||
) -> dict[str, Any]:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowPlanningSessionRow, session_id, with_for_update=True)
|
||||
if row is None:
|
||||
raise WorkflowPlanningSessionNotFoundError(session_id)
|
||||
proposals = await self._proposals(session, session_id, lock=True)
|
||||
if not any(proposal.id == proposal_id for proposal in proposals):
|
||||
raise WorkflowPlanningProposalNotFoundError(session_id, proposal_id)
|
||||
if row.status == "confirmed":
|
||||
if row.selected_proposal_id == proposal_id and row.confirmed_run_id == run_id:
|
||||
return _session_dict(row, proposals)
|
||||
raise WorkflowPlanningConflictError(session_id, "规划会话已经确认到另一条运行")
|
||||
if row.selected_proposal_id != proposal_id:
|
||||
raise WorkflowPlanningConflictError(session_id, "请先选择该候选流程")
|
||||
now = datetime.now(UTC)
|
||||
row.status = "confirmed"
|
||||
row.confirmed_run_id = run_id
|
||||
row.updated_at = now
|
||||
for proposal in proposals:
|
||||
proposal.status = "confirmed" if proposal.id == proposal_id else "proposed"
|
||||
proposal.updated_at = now
|
||||
await session.flush()
|
||||
result = _session_dict(row, proposals)
|
||||
await session.commit()
|
||||
return result
|
||||
@@ -45,6 +45,7 @@ WorkflowErrorCode = Literal[
|
||||
"WORKFLOW_SQL_FAILED",
|
||||
"WORKFLOW_CODE_SANDBOX_FAILED",
|
||||
"WORKFLOW_TRANSFORM_FAILED",
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"WORKFLOW_CONDITION_FAILED",
|
||||
"WORKFLOW_MERGE_FAILED",
|
||||
"WORKFLOW_HUMAN_INPUT_INVALID",
|
||||
@@ -89,6 +90,7 @@ ALL_WORKFLOW_ERROR_CODES: tuple[WorkflowErrorCode, ...] = (
|
||||
"WORKFLOW_SQL_FAILED",
|
||||
"WORKFLOW_CODE_SANDBOX_FAILED",
|
||||
"WORKFLOW_TRANSFORM_FAILED",
|
||||
"WORKFLOW_DEEP_RESEARCH_FAILED",
|
||||
"WORKFLOW_CONDITION_FAILED",
|
||||
"WORKFLOW_MERGE_FAILED",
|
||||
"WORKFLOW_HUMAN_INPUT_INVALID",
|
||||
|
||||
@@ -23,7 +23,10 @@ from deerflow.workflows.errors import WorkflowError
|
||||
|
||||
_TEMPLATE_RE = re.compile(r"\{\{\s*([^{}]+?)\s*\}\}")
|
||||
_ROOT_KEYS = ("inputs", "nodes", "run", "loop", "env")
|
||||
_PATH_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_\-]*|\[\d+\])*$")
|
||||
# Coze canvas node ids are commonly numeric (for example ``100001``). They
|
||||
# are still dictionary keys, not Python identifiers, so permit a numeric
|
||||
# dotted segment while keeping roots and normal field names restricted.
|
||||
_PATH_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(?:\.(?:[A-Za-z_][A-Za-z0-9_\-]*|\d+)|\[\d+\])*$")
|
||||
_COMPARATORS = ("==", "!=", ">=", "<=", ">", "<", " in ", " contains ")
|
||||
_MAX_RENDER_CHARS = 200_000
|
||||
|
||||
@@ -45,7 +48,7 @@ def resolve_path(path: str, state: dict[str, Any]) -> Any:
|
||||
if root not in _ROOT_KEYS:
|
||||
raise _invalid(expr, f"根节点必须是 {'/'.join(_ROOT_KEYS)} 之一")
|
||||
current: Any = state
|
||||
for segment in re.findall(r"[A-Za-z_][A-Za-z0-9_\-]*|\[\d+\]", expr):
|
||||
for segment in re.findall(r"[A-Za-z_][A-Za-z0-9_\-]*|\d+|\[\d+\]", expr):
|
||||
if current is None:
|
||||
return None
|
||||
if segment.startswith("["):
|
||||
@@ -234,7 +237,7 @@ def evaluate_condition(expression: str, state: dict[str, Any]) -> bool:
|
||||
return _truthy(_operand(expr, state))
|
||||
|
||||
|
||||
_NODES_IDENT_RE = re.compile(r"(?:\{\{\s*)?nodes\.([A-Za-z_][A-Za-z0-9_\-]*)")
|
||||
_NODES_IDENT_RE = re.compile(r"(?:\{\{\s*)?nodes\.([A-Za-z_0-9][A-Za-z0-9_\-]*)")
|
||||
|
||||
|
||||
def collect_referenced_nodes(value: Any) -> set[str]:
|
||||
|
||||
@@ -11,6 +11,8 @@ from deerflow.workflows.nodes.agents import (
|
||||
from deerflow.workflows.nodes.base import ACTIVE_PORTS, NodeExecutor, NodeRegistry
|
||||
from deerflow.workflows.nodes.core import (
|
||||
ConditionNodeExecutor,
|
||||
DeepResearchWriteNodeExecutor,
|
||||
EvidenceNormalizerNodeExecutor,
|
||||
LoopNodeExecutor,
|
||||
MergeNodeExecutor,
|
||||
OutputNodeExecutor,
|
||||
@@ -29,6 +31,8 @@ __all__ = [
|
||||
"AgentNodeExecutor",
|
||||
"CodeNodeExecutor",
|
||||
"ConditionNodeExecutor",
|
||||
"DeepResearchWriteNodeExecutor",
|
||||
"EvidenceNormalizerNodeExecutor",
|
||||
"HttpNodeExecutor",
|
||||
"HumanInputNodeExecutor",
|
||||
"LoopNodeExecutor",
|
||||
@@ -52,6 +56,8 @@ def build_default_registry(config: WorkflowConfig | None = None) -> 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())
|
||||
|
||||
+72
-2
@@ -25,6 +25,8 @@ 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:
|
||||
@@ -116,6 +118,60 @@ def _safe_tool_args(args: Any) -> dict[str, Any]:
|
||||
return out
|
||||
|
||||
|
||||
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)."""
|
||||
|
||||
@@ -258,9 +314,16 @@ class AgentNodeExecutor(_AgentInvocation):
|
||||
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 "")
|
||||
prompt = render_template(template, ctx.state()).strip()
|
||||
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,
|
||||
@@ -295,7 +358,14 @@ class SkillNodeExecutor(_AgentInvocation):
|
||||
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 "")
|
||||
prompt = render_template(template, ctx.state()).strip() or f"请使用技能 {skill_names[0]} 完成任务。"
|
||||
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,
|
||||
|
||||
+240
-1
@@ -7,7 +7,7 @@ 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.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
|
||||
@@ -141,6 +141,243 @@ class TransformNodeExecutor:
|
||||
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."""
|
||||
|
||||
@@ -230,6 +467,8 @@ class LoopNodeExecutor:
|
||||
|
||||
__all__ = [
|
||||
"ConditionNodeExecutor",
|
||||
"DeepResearchWriteNodeExecutor",
|
||||
"EvidenceNormalizerNodeExecutor",
|
||||
"LoopNodeExecutor",
|
||||
"MergeNodeExecutor",
|
||||
"OutputNodeExecutor",
|
||||
|
||||
@@ -52,6 +52,11 @@ class HumanInputNodeExecutor:
|
||||
)
|
||||
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":
|
||||
|
||||
+4
@@ -75,6 +75,10 @@ class WorkflowRuntimeDeps:
|
||||
run_sql: Callable[..., Awaitable[dict[str, Any]]] | None = None
|
||||
run_code: Callable[..., Awaitable[dict[str, Any]]] | None = None
|
||||
run_subworkflow: Callable[..., Awaitable[dict[str, Any]]] | None = None
|
||||
# Starts (or reattaches to) a durable Deep Research report-writing job.
|
||||
# The app adapter owns its session/job/event stores; workflow nodes only
|
||||
# receive the final report plus safe progress callbacks.
|
||||
run_deep_research: Callable[..., Awaitable[dict[str, Any]]] | None = None
|
||||
save_artifact: Callable[..., Awaitable[dict[str, Any]]] | None = None
|
||||
resolve_credential: Callable[[str], Awaitable[dict[str, Any] | None]] | None = None
|
||||
resolve_data_source: Callable[[str], Awaitable[dict[str, Any] | None]] | None = None
|
||||
|
||||
@@ -24,6 +24,8 @@ NodeType = Literal[
|
||||
"sql_read",
|
||||
"code",
|
||||
"transform",
|
||||
"evidence_normalizer",
|
||||
"deep_research_write",
|
||||
"condition",
|
||||
"merge",
|
||||
"human_input",
|
||||
@@ -40,6 +42,8 @@ ALL_NODE_TYPES: tuple[NodeType, ...] = (
|
||||
"sql_read",
|
||||
"code",
|
||||
"transform",
|
||||
"evidence_normalizer",
|
||||
"deep_research_write",
|
||||
"condition",
|
||||
"merge",
|
||||
"human_input",
|
||||
@@ -222,6 +226,7 @@ class ResumeRunRequest(BaseModel):
|
||||
class AgentNodeConfig(BaseModel):
|
||||
agent_id: str = Field(alias="agentId")
|
||||
prompt_template: str = Field(default="", alias="promptTemplate")
|
||||
input_bindings: dict[str, Any] = Field(default_factory=dict, alias="inputBindings")
|
||||
response_mode: AgentResponseMode = Field(default="text", alias="responseMode")
|
||||
response_schema: dict[str, Any] = Field(default_factory=dict, alias="responseSchema")
|
||||
thread_mode: AgentThreadMode = Field(default="isolated_per_node", alias="threadMode")
|
||||
@@ -236,6 +241,7 @@ class SkillNodeConfig(BaseModel):
|
||||
agent_id: str | None = Field(default=None, alias="agentId")
|
||||
skill_names: list[str] = Field(default_factory=list, alias="skillNames")
|
||||
prompt_template: str = Field(default="", alias="promptTemplate")
|
||||
input_bindings: dict[str, Any] = Field(default_factory=dict, alias="inputBindings")
|
||||
# callable_skill fields (phase 4+; accepted in schema, not executed in phase 0)
|
||||
entry: str | None = None
|
||||
input_schema: dict[str, Any] = Field(default_factory=dict, alias="inputSchema")
|
||||
|
||||
@@ -188,6 +188,7 @@ def validate_workflow_graph(
|
||||
)
|
||||
)
|
||||
|
||||
predecessors = _predecessors(graph)
|
||||
for node in graph.nodes:
|
||||
if node.type == "condition":
|
||||
branches = node.config.get("branches") or []
|
||||
@@ -234,6 +235,104 @@ def validate_workflow_graph(
|
||||
)
|
||||
)
|
||||
|
||||
if node.type == "evidence_normalizer":
|
||||
raw_sources = node.config.get("sources")
|
||||
if raw_sources is not None and not isinstance(raw_sources, list):
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="证据归一化节点的 sources 必须是数组",
|
||||
node_id=node.id,
|
||||
details={"field": "config.sources"},
|
||||
)
|
||||
)
|
||||
source_ids: list[str] = []
|
||||
if isinstance(raw_sources, list):
|
||||
for item in raw_sources:
|
||||
source_id = str(item.get("nodeId") or item.get("node_id") or "").strip() if isinstance(item, dict) else ""
|
||||
if not source_id:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="证据归一化 sources 的每项都需要 nodeId",
|
||||
node_id=node.id,
|
||||
details={"field": "config.sources"},
|
||||
)
|
||||
)
|
||||
continue
|
||||
source_ids.append(source_id)
|
||||
else:
|
||||
source_ids = [edge.source for edge in graph.edges if edge.target == node.id]
|
||||
if not source_ids:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="证据归一化节点至少需要一个上游研究节点",
|
||||
node_id=node.id,
|
||||
details={"field": "config.sources"},
|
||||
)
|
||||
)
|
||||
ancestors = _ancestors(node.id, predecessors)
|
||||
for source_id in source_ids:
|
||||
if source_id not in graph.node_map():
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_NODE_NOT_FOUND",
|
||||
message=f"证据归一化引用了不存在的节点: {source_id}",
|
||||
node_id=node.id,
|
||||
details={"referencedNodeId": source_id},
|
||||
)
|
||||
)
|
||||
elif source_id not in ancestors:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_EXPRESSION_INVALID",
|
||||
message=f"证据归一化只能引用上游节点,{source_id} 不在 {node.id} 的上游",
|
||||
node_id=node.id,
|
||||
details={"referencedNodeId": source_id},
|
||||
)
|
||||
)
|
||||
|
||||
if node.type == "deep_research_write":
|
||||
raw_evidence = node.config.get("evidenceBinding") or node.config.get("evidence_binding")
|
||||
if raw_evidence is None:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="深度研究写作节点必须绑定 Evidence Pack",
|
||||
node_id=node.id,
|
||||
details={"field": "config.evidenceBinding"},
|
||||
)
|
||||
)
|
||||
elif not isinstance(raw_evidence, (str, dict)):
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="深度研究写作节点的 Evidence Pack 绑定格式无效",
|
||||
node_id=node.id,
|
||||
details={"field": "config.evidenceBinding"},
|
||||
)
|
||||
)
|
||||
raw_config = node.config.get("researchConfig") or node.config.get("research_config") or {}
|
||||
if not isinstance(raw_config, dict):
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="深度研究写作节点的 researchConfig 必须是对象",
|
||||
node_id=node.id,
|
||||
details={"field": "config.researchConfig"},
|
||||
)
|
||||
)
|
||||
elif raw_config.get("mode") == "multi_agent":
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
code="WORKFLOW_SCHEMA_INVALID",
|
||||
message="深度研究写作节点暂不支持 multi_agent;请在工作流中显式使用 human_input 节点",
|
||||
node_id=node.id,
|
||||
details={"field": "config.researchConfig.mode"},
|
||||
)
|
||||
)
|
||||
|
||||
if resource_checker is not None:
|
||||
verdict = resource_checker(node.type, node.config or {})
|
||||
if verdict:
|
||||
|
||||
Reference in new issue
Block a user