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(
|
||||
|
||||
@@ -17,14 +17,16 @@ import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.gateway.routers import workflow_data_sources, workflow_embed, workflow_resources
|
||||
from app.gateway.routers import workflow_data_sources, workflow_embed, workflow_planning, workflow_resources
|
||||
from app.gateway.routers import workflow_runs as runs_router
|
||||
from app.gateway.workflow_agent_runner import WorkflowAgentRunner
|
||||
from app.gateway.workflow_dispatcher import WorkflowRunDispatcher
|
||||
from app.gateway.workflow_executor import WorkflowRunExecutor
|
||||
from app.gateway.workflow_live_hub import WorkflowLiveHub
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.persistence.workflow_data_sources import MemoryWorkflowDataSourceStore
|
||||
from deerflow.persistence.workflow_events import MemoryWorkflowEventStore
|
||||
from deerflow.persistence.workflow_planning import MemoryWorkflowPlanningStore
|
||||
from deerflow.persistence.workflow_runs import MemoryWorkflowRunStore
|
||||
from deerflow.persistence.workflows import MemoryWorkflowStore
|
||||
|
||||
@@ -60,11 +62,13 @@ FAIL_GRAPH: dict[str, Any] = {
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "title", "value": "{{ inputs.topic }}"}]},
|
||||
},
|
||||
{"id": "boom", "type": "code", "config": {"source": "output['x'] = 1"}},
|
||||
{"id": "boom", "type": "transform", "config": {"operations": [{"op": "not_a_real_op"}]}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"title": "{{ nodes.boom.data.title }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "shape"},
|
||||
{"id": "e2", "source": "shape", "target": "boom"},
|
||||
{"id": "e3", "source": "boom", "target": "end"},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -87,6 +91,62 @@ PAUSE_GRAPH: dict[str, Any] = {
|
||||
],
|
||||
}
|
||||
|
||||
PLANNING_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-planning",
|
||||
"name": "planning",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "research",
|
||||
"type": "agent",
|
||||
"name": "研究员",
|
||||
"config": {"agentId": "default", "promptTemplate": "收集事实"},
|
||||
},
|
||||
{
|
||||
"id": "writer",
|
||||
"type": "agent",
|
||||
"name": "分析师",
|
||||
"config": {"agentId": "default", "promptTemplate": "综合结论"},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.writer.data.text }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "p1", "source": "start", "target": "research"},
|
||||
{"id": "p2", "source": "research", "target": "writer"},
|
||||
{"id": "p3", "source": "writer", "target": "end"},
|
||||
],
|
||||
}
|
||||
|
||||
FEEDBACK_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-feedback",
|
||||
"name": "feedback",
|
||||
"inputSchema": {"type": "object", "required": ["topic"], "properties": {"topic": {"type": "string"}}},
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "shape",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "brief", "value": "{{ inputs.topic }} 的基础材料"}]},
|
||||
},
|
||||
{
|
||||
"id": "writer",
|
||||
"type": "agent",
|
||||
"config": {
|
||||
"agentId": "default",
|
||||
"promptTemplate": "请基于 {{ nodes.shape.data.brief }} 写出分析。",
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.writer.data.text }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "feedback-1", "source": "start", "target": "shape"},
|
||||
{"id": "feedback-2", "source": "shape", "target": "writer"},
|
||||
{"id": "feedback-3", "source": "writer", "target": "end"},
|
||||
],
|
||||
}
|
||||
|
||||
TYPED_INPUT_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-typed",
|
||||
@@ -165,7 +225,7 @@ async def harness(monkeypatch: pytest.MonkeyPatch):
|
||||
async def _optional(_request=None):
|
||||
return None
|
||||
|
||||
for module in (runs_router, workflow_data_sources, workflow_embed, workflow_resources):
|
||||
for module in (runs_router, workflow_data_sources, workflow_embed, workflow_planning, workflow_resources):
|
||||
monkeypatch.setattr(module, "get_app_config", lambda: _Cfg())
|
||||
monkeypatch.setattr(module, "get_current_user", _user)
|
||||
if hasattr(module, "get_optional_user_from_request"):
|
||||
@@ -174,6 +234,7 @@ async def harness(monkeypatch: pytest.MonkeyPatch):
|
||||
app = FastAPI()
|
||||
app.state.workflow_store = MemoryWorkflowStore()
|
||||
app.state.workflow_run_store = MemoryWorkflowRunStore()
|
||||
app.state.workflow_planning_store = MemoryWorkflowPlanningStore()
|
||||
app.state.workflow_event_store = MemoryWorkflowEventStore()
|
||||
app.state.workflow_data_source_store = MemoryWorkflowDataSourceStore()
|
||||
app.state.workflow_live_hub = WorkflowLiveHub()
|
||||
@@ -197,6 +258,7 @@ async def harness(monkeypatch: pytest.MonkeyPatch):
|
||||
)
|
||||
app.include_router(workflow_data_sources.router)
|
||||
app.include_router(workflow_embed.router)
|
||||
app.include_router(workflow_planning.router)
|
||||
app.include_router(workflow_resources.router)
|
||||
app.include_router(runs_router.router)
|
||||
|
||||
@@ -246,6 +308,445 @@ async def test_missing_required_input_is_rejected_before_queueing(harness: Harne
|
||||
assert await harness.runs.list_runs(limit=10) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_planning_session_selects_an_editable_candidate_then_confirms_one_durable_run(
|
||||
harness: Harness,
|
||||
) -> None:
|
||||
async def planner(**_kwargs: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"recommendedStrategy": "parallel_research",
|
||||
"titles": {"parallel_research": "多角度调研方案"},
|
||||
}
|
||||
|
||||
harness.app.state.workflow_proposal_planner = planner
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "planner", "owner_id": "user-a", "draft_graph": PLANNING_GRAPH},
|
||||
)
|
||||
|
||||
created = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/planning-sessions",
|
||||
json={"query": "分析本周销售下滑并给出改进方案"},
|
||||
)
|
||||
assert created.status_code == 200, created.text
|
||||
session = created.json()
|
||||
assert session["status"] == "proposed"
|
||||
assert [proposal["strategy"] for proposal in session["proposals"]] == [
|
||||
"parallel_research",
|
||||
"collaborative",
|
||||
"quick_answer",
|
||||
]
|
||||
assert session["proposals"][0]["title"] == "多角度调研方案"
|
||||
assert all(proposal["validation"]["valid"] for proposal in session["proposals"])
|
||||
parallel_graph = session["proposals"][0]["graph"]
|
||||
evidence = next(node for node in parallel_graph["nodes"] if node["type"] == "evidence_normalizer")
|
||||
synthesis = next(node for node in parallel_graph["nodes"] if node["id"] == "writer")
|
||||
report = next(node for node in parallel_graph["nodes"] if node["type"] == "deep_research_write")
|
||||
assert evidence["config"]["sources"] == [{"nodeId": "research", "label": "研究员"}]
|
||||
assert synthesis["config"]["inputBindings"]["evidencePack"] == "{{ nodes.__parallel_evidence__.data.evidencePack }}"
|
||||
assert report["config"]["evidenceBinding"] == "{{ nodes.__parallel_evidence__.data.evidencePack }}"
|
||||
assert report["config"]["researchConfig"]["mode"] == "detailed"
|
||||
assert [role["name"] for role in session["proposals"][0]["roles"]] == ["研究员", "分析师"]
|
||||
assert session["proposals"][0]["steps"] == ["研究员", "证据归一化", "分析师", "深度研究报告写作"]
|
||||
|
||||
selected = session["proposals"][0]
|
||||
selected_response = await harness.client.post(
|
||||
f"/api/workflows/planning-sessions/{session['planningSessionId']}/proposals/{selected['proposalId']}/select",
|
||||
)
|
||||
assert selected_response.status_code == 200, selected_response.text
|
||||
selected_session = selected_response.json()
|
||||
assert selected_session["status"] == "selected"
|
||||
assert selected_session["selectedProposalId"] == selected["proposalId"]
|
||||
|
||||
started = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={
|
||||
"planningSessionId": session["planningSessionId"],
|
||||
"proposalId": selected["proposalId"],
|
||||
"idempotencyKey": "confirm-proposal-1",
|
||||
},
|
||||
)
|
||||
assert started.status_code == 200, started.text
|
||||
run = started.json()
|
||||
assert run["created"] is True
|
||||
assert run["executionMode"] == "planned_workflow"
|
||||
assert run["planningSessionId"] == session["planningSessionId"]
|
||||
|
||||
# A duplicate confirmation never creates a second execution. The planning
|
||||
# session is the durable source of truth, not a front-end disabled button.
|
||||
duplicate = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={
|
||||
"planningSessionId": session["planningSessionId"],
|
||||
"proposalId": selected["proposalId"],
|
||||
"idempotencyKey": "confirm-proposal-1",
|
||||
},
|
||||
)
|
||||
assert duplicate.status_code == 200, duplicate.text
|
||||
assert duplicate.json()["runId"] == run["runId"]
|
||||
assert duplicate.json()["created"] is False
|
||||
|
||||
persisted = await harness.client.get(
|
||||
f"/api/workflows/planning-sessions/{session['planningSessionId']}",
|
||||
)
|
||||
assert persisted.status_code == 200
|
||||
assert persisted.json()["status"] == "confirmed"
|
||||
assert persisted.json()["confirmedRunId"] == run["runId"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_planning_session_streams_real_controller_milestones_before_result(
|
||||
harness: Harness,
|
||||
) -> None:
|
||||
async def planner(**_kwargs: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"recommendedStrategy": "quick_answer",
|
||||
"strategyOrder": ["quick_answer", "collaborative"],
|
||||
"agentSelections": {"quick_answer": ["default"], "collaborative": ["default"]},
|
||||
}
|
||||
|
||||
harness.app.state.workflow_proposal_planner = planner
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "stream-planner", "owner_id": "user-a", "draft_graph": PLANNING_GRAPH},
|
||||
)
|
||||
|
||||
async with harness.client.stream(
|
||||
"POST",
|
||||
f"/api/workflows/{definition['id']}/planning-sessions/stream",
|
||||
json={"query": "请评估本周销售下滑原因"},
|
||||
) as response:
|
||||
assert response.status_code == 200
|
||||
payload = "".join([chunk async for chunk in response.aiter_text()])
|
||||
|
||||
assert "event: progress" in payload
|
||||
assert '"phase": "connection"' in payload
|
||||
assert '"phase": "catalog"' in payload
|
||||
assert '"phase": "controller"' in payload
|
||||
assert '"phase": "selection"' in payload
|
||||
assert '"phase": "assembly"' in payload
|
||||
assert '"phase": "validation"' in payload
|
||||
assert '"phase": "complete"' in payload
|
||||
assert "event: result" in payload
|
||||
assert ": stream-flush" in payload
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dedicated_workflow_controller_has_no_tools_or_thinking(
|
||||
harness: Harness,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
captured: dict[str, Any] = {}
|
||||
|
||||
async def fake_run_agent(_self: Any, **kwargs: Any) -> dict[str, Any]:
|
||||
captured.update(kwargs)
|
||||
return {
|
||||
"text": (
|
||||
'{"recommendedStrategy":"quick_answer",'
|
||||
'"agentSelections":{"collaborative":["default"],'
|
||||
'"quick_answer":["default"]}}'
|
||||
)
|
||||
}
|
||||
|
||||
monkeypatch.setattr(WorkflowAgentRunner, "run_agent", fake_run_agent)
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "controller-boundary", "owner_id": "user-a", "draft_graph": PLANNING_GRAPH},
|
||||
)
|
||||
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/planning-sessions",
|
||||
json={"query": "快速确认这份材料是否有缺口"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.text
|
||||
assert captured["agent_id"] == "workflow-planner"
|
||||
assert captured["disable_tools"] is True
|
||||
assert captured["thinking_enabled"] is False
|
||||
assert captured["force_disable_thinking"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_planner_uses_visible_agent_catalog_when_canvas_is_only_start_and_end(
|
||||
harness: Harness,
|
||||
) -> None:
|
||||
class VisibleAgentStore:
|
||||
async def list_visible(self, _user_id: str) -> list[dict[str, str]]:
|
||||
return [
|
||||
{
|
||||
"id": "agent-research",
|
||||
"name": "行业研究员",
|
||||
"description": "收集行业数据、案例和不确定性。",
|
||||
},
|
||||
{
|
||||
"id": "agent-writer",
|
||||
"name": "策略分析师",
|
||||
"description": "比较证据并形成策略建议。",
|
||||
},
|
||||
{
|
||||
"id": "workflow-planner",
|
||||
"name": "工作流总控",
|
||||
"description": "不应被选作正式工作流执行角色。",
|
||||
},
|
||||
]
|
||||
|
||||
async def get_extras_for(self, _agent_ids: list[str]) -> dict[str, dict[str, list[str]]]:
|
||||
return {
|
||||
"agent-research": {"skills": ["web-search"]},
|
||||
"agent-writer": {"skills": []},
|
||||
}
|
||||
|
||||
captured: dict[str, Any] = {}
|
||||
|
||||
async def planner(**kwargs: Any) -> dict[str, Any]:
|
||||
captured.update(kwargs)
|
||||
return {
|
||||
"recommendedStrategy": "parallel_research",
|
||||
"strategyOrder": ["parallel_research", "collaborative", "quick_answer"],
|
||||
"agentSelections": {
|
||||
"parallel_research": ["agent-research", "agent-writer"],
|
||||
"collaborative": ["agent-research", "agent-writer"],
|
||||
"quick_answer": ["agent-research"],
|
||||
},
|
||||
"titles": {"parallel_research": "双角色调研报告"},
|
||||
}
|
||||
|
||||
harness.app.state.agent_store = VisibleAgentStore()
|
||||
harness.app.state.workflow_proposal_planner = planner
|
||||
resource_response = await harness.client.get("/api/workflows/resources/agents?keyword=总控")
|
||||
assert resource_response.status_code == 200
|
||||
assert resource_response.json() == {"agents": []}
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{
|
||||
"name": "empty-canvas-planner",
|
||||
"owner_id": "user-a",
|
||||
"draft_graph": {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-empty-planning",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {}}},
|
||||
],
|
||||
"edges": [{"id": "start-to-end", "source": "start", "target": "end"}],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/planning-sessions",
|
||||
json={"query": "研究新能源乘用车市场,并给出落地建议"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.text
|
||||
assert [item["agentId"] for item in captured["agents"]] == [
|
||||
"agent-research",
|
||||
"agent-writer",
|
||||
]
|
||||
session = response.json()
|
||||
assert [proposal["strategy"] for proposal in session["proposals"]] == [
|
||||
"parallel_research",
|
||||
"collaborative",
|
||||
"quick_answer",
|
||||
]
|
||||
parallel = session["proposals"][0]
|
||||
assert parallel["title"] == "双角色调研报告"
|
||||
assert [role["agentId"] for role in parallel["roles"]] == [
|
||||
"agent-research",
|
||||
"agent-writer",
|
||||
]
|
||||
assert all(proposal["validation"]["valid"] for proposal in session["proposals"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_planning_store_flushes_the_session_before_its_candidates() -> None:
|
||||
"""A durable planning session can insert its child proposals with FK checks on."""
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.workflow_planning import SqlWorkflowPlanningStore
|
||||
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
||||
try:
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(Base.metadata.create_all)
|
||||
store = SqlWorkflowPlanningStore(async_sessionmaker(engine, expire_on_commit=False))
|
||||
|
||||
created = await store.create_session(
|
||||
{
|
||||
"id": "wps-parent",
|
||||
"workflow_id": "wf-parent",
|
||||
"owner_id": "user-a",
|
||||
"query": "生成一份市场分析",
|
||||
"proposals": [
|
||||
{
|
||||
"id": "wpp-child",
|
||||
"strategy": "collaborative",
|
||||
"title": "协同分析",
|
||||
"graph": {"nodes": [], "edges": []},
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
assert created["id"] == "wps-parent"
|
||||
assert [proposal["id"] for proposal in created["proposals"]] == ["wpp-child"]
|
||||
persisted = await store.get_session("wps-parent")
|
||||
assert persisted is not None
|
||||
assert persisted["proposals"][0]["session_id"] == "wps-parent"
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_start_rejects_unbound_nodes_before_queueing(harness: Harness) -> None:
|
||||
graph = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-unbound",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "agent", "type": "agent", "config": {"agentId": "default", "promptTemplate": ""}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.agent.data.text }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "agent"},
|
||||
{"id": "e2", "source": "agent", "target": "end"},
|
||||
],
|
||||
}
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "unbound", "owner_id": "user-a", "draft_graph": graph},
|
||||
)
|
||||
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={"target": "draft", "inputs": {}},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
detail = response.json()["detail"]
|
||||
assert detail["code"] == "WORKFLOW_RESOURCE_MISSING"
|
||||
assert detail["nodeId"] == "agent"
|
||||
assert await harness.runs.list_runs(limit=10) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_directed_agent_task_projects_one_bound_agent_into_a_durable_run(harness: Harness) -> None:
|
||||
class VisibleAgentStore:
|
||||
async def list_visible(self, _user_id: str) -> list[dict[str, str]]:
|
||||
return [{"id": "agent-research"}]
|
||||
|
||||
async def get_extras_for(self, _agent_ids: list[str]) -> dict[str, dict[str, list[str]]]:
|
||||
return {}
|
||||
|
||||
harness.app.state.agent_store = VisibleAgentStore()
|
||||
graph = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-directed",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "100002",
|
||||
"type": "agent",
|
||||
"name": "市场研究员",
|
||||
"config": {
|
||||
"agentId": "agent-research",
|
||||
"promptTemplate": "分析用户的市场研究任务",
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.100002.data.text }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "100002"},
|
||||
{"id": "e2", "source": "100002", "target": "end"},
|
||||
],
|
||||
}
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "directed", "owner_id": "user-a", "draft_graph": graph},
|
||||
)
|
||||
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={
|
||||
"target": "draft",
|
||||
"input": {"query": "分析新能源行业趋势"},
|
||||
"targetAgentId": "agent-research",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.text
|
||||
run = response.json()
|
||||
assert run["executionMode"] == "agent_task"
|
||||
assert run["targetAgentId"] == "agent-research"
|
||||
assert run["targetAgentName"] == "市场研究员"
|
||||
stored = await harness.runs.get_run(run["runId"])
|
||||
assert stored is not None
|
||||
projected = stored["context"]["draftGraph"]
|
||||
assert [node["id"] for node in projected["nodes"]] == [
|
||||
"__agent_task_start__",
|
||||
"100002",
|
||||
"__agent_task_output__",
|
||||
]
|
||||
agent = projected["nodes"][1]
|
||||
assert agent["config"]["inputBindings"] == {"workflowInput": "{{ inputs }}"}
|
||||
assert projected["nodes"][2]["config"]["mapping"] == {
|
||||
"text": "{{ nodes.100002.data.text }}"
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_directed_agent_task_rejects_agents_not_bound_on_the_canvas(harness: Harness) -> None:
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "not-bound", "owner_id": "user-a", "draft_graph": REPORT_GRAPH},
|
||||
)
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={"target": "draft", "input": {"query": "你好"}, "targetAgentId": "agent-missing"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_AGENT_TARGET_NOT_BOUND"
|
||||
assert await harness.runs.list_runs(limit=10) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_draft_human_resume_preserves_the_execution_graph_snapshot(harness: Harness) -> None:
|
||||
definition = await harness.app.state.workflow_store.create_definition(
|
||||
{"name": "draft-pause", "owner_id": "user-a", "draft_graph": PAUSE_GRAPH},
|
||||
)
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{definition['id']}/runs",
|
||||
json={"target": "draft", "inputs": {}},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
run_id = response.json()["runId"]
|
||||
|
||||
await harness.drive()
|
||||
paused = await harness.runs.get_run(run_id)
|
||||
assert paused is not None and paused["status"] == "awaiting_input"
|
||||
assert [node["id"] for node in paused["context"]["draftGraph"]["nodes"]] == [
|
||||
"start",
|
||||
"ask",
|
||||
"end",
|
||||
]
|
||||
|
||||
resumed = await harness.client.post(
|
||||
f"/api/workflows/runs/{run_id}/resume",
|
||||
json={
|
||||
"resumeToken": paused["resume_token"],
|
||||
"action": "submit",
|
||||
"payload": {"comment": "继续执行"},
|
||||
},
|
||||
)
|
||||
assert resumed.status_code == 200, resumed.text
|
||||
await harness.drive()
|
||||
|
||||
finished = await harness.runs.get_run(run_id)
|
||||
assert finished is not None
|
||||
assert finished["status"] == "completed"
|
||||
assert finished["output"] == {"comment": "继续执行"}
|
||||
assert [node["id"] for node in finished["context"]["draftGraph"]["nodes"]] == [
|
||||
"start",
|
||||
"ask",
|
||||
"end",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_typed_input_schema_violations_return_structured_400(harness: Harness) -> None:
|
||||
"""Wrong types (not just missing keys) fail at start with field paths."""
|
||||
@@ -465,6 +966,74 @@ async def test_retry_rejected_unless_failed(harness: Harness) -> None:
|
||||
assert completed.json()["detail"]["code"] == "WORKFLOW_RUN_NOT_RETRYABLE"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_completed_agent_feedback_creates_revision_and_reuses_unaffected_nodes(
|
||||
harness: Harness,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
prompts: list[str] = []
|
||||
|
||||
async def fake_run_agent(_self: Any, **kwargs: Any) -> dict[str, Any]:
|
||||
prompt = str(kwargs["prompt"])
|
||||
prompts.append(prompt)
|
||||
text = "已根据反馈重写" if "<workflow-user-feedback>" in prompt else "初版分析"
|
||||
on_delta = kwargs.get("on_delta")
|
||||
if on_delta is not None:
|
||||
await on_delta(text)
|
||||
return {"text": text, "thread_id": "fake-workflow-agent"}
|
||||
|
||||
monkeypatch.setattr(WorkflowAgentRunner, "run_agent", fake_run_agent)
|
||||
workflow_id, version_id = await harness.publish(FEEDBACK_GRAPH, name="feedback")
|
||||
original = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
original_detail = (await harness.client.get(f"/api/workflows/runs/{original['id']}")).json()
|
||||
assert original_detail["status"] == "completed"
|
||||
assert original_detail["output"] == {"text": "初版分析"}
|
||||
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/runs/{original['id']}/feedback",
|
||||
json={
|
||||
"nodeId": "writer",
|
||||
"message": "请从成本、风险和可执行性三个维度重新分析。",
|
||||
"idempotencyKey": "writer-revision-1",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 201, response.text
|
||||
revision = response.json()
|
||||
assert revision["created"] is True
|
||||
assert revision["revisionOfRunId"] == original["id"]
|
||||
assert revision["retryOfRunId"] == original["id"]
|
||||
assert revision["affectedNodeIds"] == ["end", "writer"]
|
||||
assert revision["reusedNodeIds"] == ["shape", "start"]
|
||||
assert revision["feedback"]["targetNodeId"] == "writer"
|
||||
assert revision["feedback"]["reusedNodeIds"] == ["shape", "start"]
|
||||
|
||||
before_drive = (await harness.client.get(f"/api/workflows/runs/{revision['id']}")).json()
|
||||
assert {row["nodeId"] for row in before_drive["nodeRuns"]} == {"start", "shape"}
|
||||
|
||||
await harness.drive()
|
||||
completed = (await harness.client.get(f"/api/workflows/runs/{revision['id']}")).json()
|
||||
assert completed["status"] == "completed"
|
||||
assert completed["output"] == {"text": "已根据反馈重写"}
|
||||
assert {row["nodeId"] for row in completed["nodeRuns"]} == {"start", "shape", "writer", "end"}
|
||||
assert "<workflow-user-feedback>" in prompts[-1]
|
||||
assert "成本、风险和可执行性" in prompts[-1]
|
||||
|
||||
duplicate = await harness.client.post(
|
||||
f"/api/workflows/runs/{original['id']}/feedback",
|
||||
json={
|
||||
"nodeId": "writer",
|
||||
"message": "请从成本、风险和可执行性三个维度重新分析。",
|
||||
"idempotencyKey": "writer-revision-1",
|
||||
},
|
||||
)
|
||||
assert duplicate.status_code == 201
|
||||
assert duplicate.json()["id"] == revision["id"]
|
||||
assert duplicate.json()["created"] is False
|
||||
assert duplicate.json()["reused"] is True
|
||||
assert (await harness.client.get(f"/api/workflows/runs/{original['id']}")).json()["output"] == {"text": "初版分析"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_last_event_id_beats_after_query(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
|
||||
Reference in new issue
Block a user