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
@@ -3,15 +3,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from app.gateway.workflow_deep_research_adapter import WorkflowDeepResearchAdapter
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.persistence.workflow_events import MemoryWorkflowEventStore
|
||||
from deerflow.persistence.workflow_runs import MemoryWorkflowRunStore
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import evaluate_condition, render_template, render_value
|
||||
from deerflow.workflows.expressions import collect_referenced_nodes, evaluate_condition, render_template, render_value
|
||||
from deerflow.workflows.nodes import build_default_registry
|
||||
from deerflow.workflows.runtime import (
|
||||
CancelToken,
|
||||
@@ -46,6 +48,7 @@ async def _run(
|
||||
*,
|
||||
deps: WorkflowRuntimeDeps | None = None,
|
||||
resume_payload: dict[str, Any] | None = None,
|
||||
env: dict[str, Any] | None = None,
|
||||
config: WorkflowConfig | None = None,
|
||||
) -> tuple[Any, list[dict[str, Any]]]:
|
||||
run_store = MemoryWorkflowRunStore()
|
||||
@@ -67,6 +70,7 @@ async def _run(
|
||||
cancel=CancelToken(),
|
||||
emit=sink.emit,
|
||||
resume_payload=resume_payload,
|
||||
env=env or {},
|
||||
)
|
||||
engine = WorkflowEngine(build_default_registry(config or WorkflowConfig()), run_store=run_store)
|
||||
result = await engine.run(ctx)
|
||||
@@ -93,6 +97,18 @@ def test_expression_rendering_and_paths() -> None:
|
||||
assert render_template("{{ nodes.nope.data.x }}", state) == ""
|
||||
|
||||
|
||||
def test_expression_supports_numeric_canvas_node_ids() -> None:
|
||||
state = {
|
||||
"inputs": {},
|
||||
"nodes": {"100002": {"data": {"text": "流式回复"}}},
|
||||
"run": {},
|
||||
"loop": {},
|
||||
"env": {},
|
||||
}
|
||||
assert render_template("{{ nodes.100002.data.text }}", state) == "流式回复"
|
||||
assert collect_referenced_nodes({"answer": "{{ nodes.100002.data.text }}"}) == {"100002"}
|
||||
|
||||
|
||||
def test_expression_rejects_unknown_roots_and_code() -> None:
|
||||
state = {"inputs": {}, "nodes": {}, "run": {}, "loop": {}, "env": {}}
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
@@ -288,6 +304,293 @@ async def test_human_input_rejects_missing_required_field() -> None:
|
||||
assert exc.value.code == "WORKFLOW_HUMAN_INPUT_INVALID"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_input_exposes_natural_language_reply_to_downstream_nodes() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "ask", "type": "human_input", "config": {"actions": ["submit"]}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"reply": "{{ nodes.ask.data.values.message }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "ask"},
|
||||
{"id": "e2", "source": "ask", "target": "end"},
|
||||
],
|
||||
)
|
||||
|
||||
result, _ = await _run(
|
||||
graph,
|
||||
resume_payload={"nodeId": "ask", "action": "submit", "values": {}, "message": "目标客户是连锁零售商"},
|
||||
)
|
||||
|
||||
assert result.output == {"reply": "目标客户是连锁零售商"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evidence_normalizer_builds_a_bounded_traceable_evidence_pack() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "sales_research",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "text", "value": "近四周新客转化率下降 12%"}]},
|
||||
},
|
||||
{
|
||||
"id": "voc_research",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "text", "value": "用户反馈显示结算流程复杂"}]},
|
||||
},
|
||||
{
|
||||
"id": "evidence",
|
||||
"type": "evidence_normalizer",
|
||||
"config": {
|
||||
"sources": [
|
||||
{"nodeId": "sales_research", "label": "销售数据"},
|
||||
{"nodeId": "voc_research", "label": "用户反馈"},
|
||||
]
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "end",
|
||||
"type": "output",
|
||||
"config": {"mapping": {"evidence": "{{ nodes.evidence.data.evidencePack }}"}},
|
||||
},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "sales_research"},
|
||||
{"id": "e2", "source": "start", "target": "voc_research"},
|
||||
{"id": "e3", "source": "sales_research", "target": "evidence"},
|
||||
{"id": "e4", "source": "voc_research", "target": "evidence"},
|
||||
{"id": "e5", "source": "evidence", "target": "end"},
|
||||
],
|
||||
)
|
||||
|
||||
result, events = await _run(graph)
|
||||
|
||||
pack = result.output["evidence"]
|
||||
assert [item["role"] for item in pack["items"]] == ["销售数据", "用户反馈"]
|
||||
assert pack["items"][0]["claim"] == "近四周新客转化率下降 12%"
|
||||
assert pack["items"][0]["source"]["uri"] == "workflow://run-1/nodes/sales_research"
|
||||
assert pack["assumptions"] == [] and pack["gaps"] == []
|
||||
assert any(
|
||||
event["event"] == "node.progress"
|
||||
and event["data"].get("phase") == "evidence.normalized"
|
||||
for event in events
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_deep_research_writer_streams_report_and_registers_markdown_artifact() -> None:
|
||||
async def run_deep_research(**kwargs: Any) -> dict[str, Any]:
|
||||
await kwargs["on_event"](
|
||||
{"type": "phase_changed", "phase": "writing", "payload": {}}
|
||||
)
|
||||
await kwargs["on_event"](
|
||||
{"type": "report_delta", "phase": "writing", "payload": {"delta": "# 储能报告"}}
|
||||
)
|
||||
assert kwargs["topic"] == "储能"
|
||||
assert kwargs["evidence_pack"]["items"][0]["role"] == "行业研究"
|
||||
return {
|
||||
"session_id": "drs_wf_1",
|
||||
"job_id": "drj_wf_1",
|
||||
"report_markdown": "# 储能报告\n\n基于已提供材料。",
|
||||
"source_count": 1,
|
||||
}
|
||||
|
||||
async def save_artifact(**kwargs: Any) -> dict[str, Any]:
|
||||
return {"id": "artifact-report", **kwargs}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "report",
|
||||
"type": "deep_research_write",
|
||||
"config": {
|
||||
"topicTemplate": "{{ inputs.topic }}",
|
||||
"evidenceBinding": {
|
||||
"items": [{"claim": "市场需求上升", "role": "行业研究"}],
|
||||
},
|
||||
"reportInstructionTemplate": "写成正式报告",
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.report.data.text }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "s-r", "source": "start", "target": "report"},
|
||||
{"id": "r-e", "source": "report", "target": "end"},
|
||||
],
|
||||
)
|
||||
|
||||
result, events = await _run(
|
||||
graph,
|
||||
{"topic": "储能"},
|
||||
deps=WorkflowRuntimeDeps(
|
||||
run_deep_research=run_deep_research,
|
||||
save_artifact=save_artifact,
|
||||
),
|
||||
)
|
||||
|
||||
assert result.output["text"].startswith("# 储能报告")
|
||||
assert result.node_results["report"]["data"]["deepResearchJobId"] == "drj_wf_1"
|
||||
assert result.node_results["report"]["artifacts"][0]["artifact_id"] == "artifact-report"
|
||||
assert any(
|
||||
event["event"] == "node.output.delta" and event["data"].get("delta") == "# 储能报告"
|
||||
for event in events
|
||||
)
|
||||
|
||||
|
||||
def test_deep_research_writer_requires_evidence_pack_and_rejects_nested_multi_agent() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "report", "type": "deep_research_write", "config": {"researchConfig": {"mode": "multi_agent"}}},
|
||||
{"id": "end", "type": "output"},
|
||||
],
|
||||
[
|
||||
{"id": "s-r", "source": "start", "target": "report"},
|
||||
{"id": "r-e", "source": "report", "target": "end"},
|
||||
],
|
||||
)
|
||||
issues = validate_workflow_graph(graph)
|
||||
assert {issue.code for issue in issues} >= {"WORKFLOW_SCHEMA_INVALID"}
|
||||
assert any("Evidence Pack" in issue.message for issue in issues)
|
||||
assert any("multi_agent" in issue.message for issue in issues)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_workflow_deep_research_adapter_reuses_durable_job_contract() -> None:
|
||||
class Sessions:
|
||||
def __init__(self) -> None:
|
||||
self.rows: dict[str, dict[str, Any]] = {}
|
||||
|
||||
async def get(self, session_id: str, *, user_id: str) -> dict[str, Any] | None:
|
||||
return self.rows.get(session_id)
|
||||
|
||||
async def create(self, **fields: Any) -> dict[str, Any]:
|
||||
row = {"status": "draft", "active_job_id": None, **fields}
|
||||
self.rows[row["id"]] = row
|
||||
return row
|
||||
|
||||
async def update(self, session_id: str, *, user_id: str, **fields: Any) -> dict[str, Any] | None:
|
||||
row = self.rows.get(session_id)
|
||||
if row is not None:
|
||||
row.update(fields)
|
||||
return row
|
||||
|
||||
async def count_active_by_user(self, *, user_id: str) -> int:
|
||||
return 0
|
||||
|
||||
class Sources:
|
||||
def __init__(self) -> None:
|
||||
self.rows: list[dict[str, Any]] = []
|
||||
|
||||
async def upsert(self, **fields: Any) -> dict[str, Any]:
|
||||
self.rows.append(fields)
|
||||
return fields
|
||||
|
||||
class Jobs:
|
||||
def __init__(self, sessions: Sessions) -> None:
|
||||
self.sessions = sessions
|
||||
self.row: dict[str, Any] | None = None
|
||||
|
||||
async def get_active_for_session(self, session_id: str, *, user_id: str) -> dict[str, Any] | None:
|
||||
return self.row if self.row and self.row["status"] not in {"completed", "failed", "cancelled"} else None
|
||||
|
||||
async def try_create_or_get_active(self, **fields: Any) -> tuple[dict[str, Any], bool]:
|
||||
self.row = {"id": fields["id"], "status": "queued", "phase": "initializing", **fields}
|
||||
return self.row, True
|
||||
|
||||
async def get(self, job_id: str, *, user_id: str) -> dict[str, Any] | None:
|
||||
assert self.row is not None and self.row["id"] == job_id
|
||||
self.row["status"] = "completed"
|
||||
session = self.sessions.rows[self.row["session_id"]]
|
||||
session.update({"status": "completed", "report_markdown": "# 可追溯报告"})
|
||||
return self.row
|
||||
|
||||
async def request_cancel(self, job_id: str, *, user_id: str) -> None:
|
||||
raise AssertionError("test should not cancel")
|
||||
|
||||
class Events:
|
||||
async def list_after(self, job_id: str, *, after: int, limit: int) -> list[dict[str, Any]]:
|
||||
return [{"seq": 1, "event_type": "report_chunk", "phase": "writing", "payload": {"delta": "# 可追溯报告"}}] if after == 0 else []
|
||||
|
||||
class Dispatcher:
|
||||
nudges = 0
|
||||
|
||||
def nudge(self) -> None:
|
||||
self.nudges += 1
|
||||
|
||||
sessions = Sessions()
|
||||
sources = Sources()
|
||||
jobs = Jobs(sessions)
|
||||
dispatcher = Dispatcher()
|
||||
app = SimpleNamespace(
|
||||
state=SimpleNamespace(
|
||||
deep_research_session_store=sessions,
|
||||
deep_research_job_store=jobs,
|
||||
deep_research_event_store=Events(),
|
||||
deep_research_source_store=sources,
|
||||
deep_research_dispatcher=dispatcher,
|
||||
deep_research_live_hub=None,
|
||||
)
|
||||
)
|
||||
events: list[dict[str, Any]] = []
|
||||
|
||||
async def on_event(event: dict[str, Any]) -> None:
|
||||
events.append(event)
|
||||
|
||||
outcome = await WorkflowDeepResearchAdapter(app).run(
|
||||
run_id="run-1",
|
||||
node_id="report",
|
||||
owner_id="user-a",
|
||||
topic="储能产业趋势",
|
||||
evidence_pack={
|
||||
"items": [
|
||||
{
|
||||
"claim": "上游研究指出储能装机需求增长。",
|
||||
"role": "行业研究",
|
||||
"source": {"nodeId": "research", "uri": "workflow://run-1/nodes/research"},
|
||||
}
|
||||
]
|
||||
},
|
||||
research_config={"mode": "detailed"},
|
||||
report_instruction="写成正式报告",
|
||||
cancel=CancelToken(),
|
||||
on_event=on_event,
|
||||
)
|
||||
|
||||
assert outcome["report_markdown"] == "# 可追溯报告"
|
||||
assert jobs.row is not None and jobs.row["input_snapshot"]["entry"] == "regenerate"
|
||||
assert jobs.row["input_snapshot"]["config"]["collection_mode"] == "legacy"
|
||||
assert sources.rows[0]["selected"] is True
|
||||
assert "工作流 Evidence Pack" in sources.rows[0]["raw_content"]
|
||||
assert dispatcher.nudges == 1
|
||||
assert any(event["type"] == "report_chunk" for event in events)
|
||||
|
||||
|
||||
def test_evidence_normalizer_rejects_non_upstream_or_unknown_sources() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "evidence", "type": "evidence_normalizer", "config": {"sources": [{"nodeId": "end"}, {"nodeId": "missing"}]}},
|
||||
{"id": "end", "type": "output"},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "evidence"},
|
||||
{"id": "e2", "source": "evidence", "target": "end"},
|
||||
],
|
||||
)
|
||||
|
||||
issues = validate_workflow_graph(graph)
|
||||
|
||||
assert {issue.code for issue in issues} >= {
|
||||
"WORKFLOW_EXPRESSION_INVALID",
|
||||
"WORKFLOW_NODE_NOT_FOUND",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_node_failure_can_be_tolerated_with_on_error_continue() -> None:
|
||||
graph = _graph(
|
||||
@@ -360,6 +663,84 @@ async def test_agent_node_streams_deltas_and_parses_json() -> None:
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_node_includes_rendered_workflow_input_bindings() -> None:
|
||||
seen: dict[str, Any] = {}
|
||||
|
||||
async def run_agent(**kwargs: Any) -> dict[str, Any]:
|
||||
seen.update(kwargs)
|
||||
return {"text": "已按任务完成"}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "writer",
|
||||
"type": "agent",
|
||||
"config": {
|
||||
"agentId": "a1",
|
||||
"promptTemplate": "请完成用户的请求。",
|
||||
"inputBindings": {
|
||||
"task": "{{ inputs.query }}",
|
||||
"allInputs": "{{ inputs }}",
|
||||
},
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.writer.data.text }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "writer"},
|
||||
{"id": "e2", "source": "writer", "target": "end"},
|
||||
],
|
||||
)
|
||||
|
||||
result, _ = await _run(
|
||||
graph,
|
||||
{"query": "分析本周销售数据", "region": "华东"},
|
||||
deps=WorkflowRuntimeDeps(run_agent=run_agent),
|
||||
)
|
||||
|
||||
assert result.output == {"text": "已按任务完成"}
|
||||
assert '"task": "分析本周销售数据"' in seen["prompt"]
|
||||
assert '"region": "华东"' in seen["prompt"]
|
||||
assert "<workflow-input>" in seen["prompt"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_node_receives_feedback_only_when_its_revision_targets_it() -> None:
|
||||
prompts: list[str] = []
|
||||
|
||||
async def run_agent(**kwargs: Any) -> dict[str, Any]:
|
||||
prompts.append(kwargs["prompt"])
|
||||
return {"text": "已修订"}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "research", "type": "agent", "config": {"agentId": "a1", "promptTemplate": "分析 {{ inputs.topic }}"}},
|
||||
{"id": "other", "type": "agent", "config": {"agentId": "a2", "promptTemplate": "不应接收反馈"}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.research.data.text }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "s-r", "source": "start", "target": "research"},
|
||||
{"id": "s-o", "source": "start", "target": "other"},
|
||||
{"id": "r-e", "source": "research", "target": "end"},
|
||||
],
|
||||
)
|
||||
await _run(
|
||||
graph,
|
||||
{"topic": "储能"},
|
||||
deps=WorkflowRuntimeDeps(run_agent=run_agent),
|
||||
env={"nodeFeedback": {"research": {"message": "请改从成本和风险角度分析"}}},
|
||||
)
|
||||
|
||||
feedback_prompt = next(prompt for prompt in prompts if "分析" in prompt)
|
||||
other_prompt = next(prompt for prompt in prompts if "不应接收反馈" in prompt)
|
||||
assert "<workflow-user-feedback>" in feedback_prompt
|
||||
assert "成本和风险" in feedback_prompt
|
||||
assert "<workflow-user-feedback>" not in other_prompt
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_capability_becomes_typed_error() -> None:
|
||||
graph = _graph(
|
||||
|
||||
Reference in new issue
Block a user