sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -24,6 +24,7 @@ from deerflow.tools.builtins.deep_research_report_tool import (
|
||||
StreamWriterEventSink,
|
||||
build_writing_research_config,
|
||||
deep_research_report_tool,
|
||||
strip_solution_numeric_citations,
|
||||
)
|
||||
|
||||
_DEFAULT_TOPIC = "新能源汽车市场分析"
|
||||
@@ -43,6 +44,26 @@ def test_writing_research_config_defaults_to_general_outline_basic() -> None:
|
||||
assert "必须基于模型自身的领域知识" in cfg.report_instruction
|
||||
|
||||
|
||||
def test_writing_research_config_maps_workbench_card_but_keeps_basic_mode() -> None:
|
||||
cfg = build_writing_research_config(
|
||||
workbench_report_config={
|
||||
"mode": "deep",
|
||||
"custom_outline": "## 一、背景\n## 二、结论",
|
||||
"structure_mode": "fixed",
|
||||
"generate_images": True,
|
||||
"language": "en-US",
|
||||
"tone": "formal",
|
||||
}
|
||||
)
|
||||
|
||||
assert cfg.mode == "basic"
|
||||
assert cfg.custom_outline == "## 一、背景\n## 二、结论"
|
||||
assert cfg.structure_mode == "fixed"
|
||||
assert cfg.generate_images is True
|
||||
assert cfg.language == "en-US"
|
||||
assert cfg.tone == "formal"
|
||||
|
||||
|
||||
def test_writing_research_config_maps_focus_to_report_instruction() -> None:
|
||||
cfg = build_writing_research_config(focus=" 侧重对比分析,3000 字左右 ")
|
||||
|
||||
@@ -54,6 +75,65 @@ def test_writing_research_config_maps_focus_to_report_instruction() -> None:
|
||||
assert empty_focus.report_instruction == _KNOWLEDGE_WRITE_INSTRUCTION
|
||||
|
||||
|
||||
def test_solution_research_config_forbids_numbered_citations() -> None:
|
||||
cfg = build_writing_research_config(project_mode="solution")
|
||||
|
||||
assert cfg.report_instruction is not None
|
||||
assert "不得出现 [1]、[2]、【1】、【2】等数字引用标记" in cfg.report_instruction
|
||||
assert "无编号项目符号" in cfg.report_instruction
|
||||
|
||||
|
||||
def test_smart_knowledge_retrieval_reports_use_unnumbered_sources() -> None:
|
||||
assert tool_module._report_requires_unnumbered_sources(
|
||||
{"knowledge_retrieval_mode": "smart_reasoning"}
|
||||
)
|
||||
assert tool_module._report_requires_unnumbered_sources(
|
||||
{"analysis_project": {"mode": "solution"}}
|
||||
)
|
||||
assert not tool_module._report_requires_unnumbered_sources(
|
||||
{"knowledge_retrieval_mode": "classic"}
|
||||
)
|
||||
|
||||
cfg = build_writing_research_config(unnumbered_sources=True)
|
||||
assert cfg.custom_outline == GENERAL_OUTLINE
|
||||
assert cfg.structure_mode == "adaptive"
|
||||
assert cfg.report_instruction is not None
|
||||
assert "不得出现 [1]、[2]、【1】、【2】等数字引用标记" in cfg.report_instruction
|
||||
assert "无编号项目符号" in cfg.report_instruction
|
||||
|
||||
|
||||
def test_strip_solution_numeric_citations_preserves_sources_without_numbers() -> None:
|
||||
markdown = """# 方案
|
||||
|
||||
正文结论[1][2],另一项见【3】和[4],合并来源[1, 3],范围来源【2-4】。
|
||||
保留业务字段[待确认],也保留行内代码 `items[1]`。
|
||||
|
||||
```mermaid
|
||||
A[1] --> B[2]
|
||||
```
|
||||
|
||||
## 参考来源
|
||||
|
||||
[1] 来源甲. https://example.com/a
|
||||
|
||||
- 【2】 来源乙. https://example.com/b
|
||||
|
||||
[3](https://example.com/c) 来源丙
|
||||
"""
|
||||
|
||||
cleaned = strip_solution_numeric_citations(markdown)
|
||||
|
||||
assert "正文结论,另一项见和,合并来源,范围来源。" in cleaned
|
||||
assert "[待确认]" in cleaned
|
||||
assert "`items[1]`" in cleaned
|
||||
assert "A[1] --> B[2]" in cleaned
|
||||
assert "- 来源甲. https://example.com/a" in cleaned
|
||||
assert "- 来源乙. https://example.com/b" in cleaned
|
||||
assert "- <https://example.com/c> 来源丙" in cleaned
|
||||
assert "\n[1] 来源甲" not in cleaned
|
||||
assert "\n- 【2】" not in cleaned
|
||||
|
||||
|
||||
# ── 事件转发 sink ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -141,9 +221,10 @@ def _material_message(index: int, topic: str = _DEFAULT_TOPIC) -> ToolMessage:
|
||||
def _fake_runtime(
|
||||
thread_id: str = "thread-1",
|
||||
messages: list | None = None,
|
||||
context: dict | None = None,
|
||||
) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
context={"thread_id": thread_id},
|
||||
context={"thread_id": thread_id, **(context or {})},
|
||||
config={"configurable": {"thread_id": thread_id}},
|
||||
state={"messages": messages if messages is not None else [_material_message(i) for i in range(1, 4)]},
|
||||
)
|
||||
@@ -172,6 +253,15 @@ class _FakeEngine:
|
||||
return self.result
|
||||
|
||||
|
||||
class _CapturingArtifactWriter:
|
||||
def __init__(self) -> None:
|
||||
self.writes: list[tuple[str, str]] = []
|
||||
|
||||
async def write_text(self, name: str, content: str) -> str:
|
||||
self.writes.append((name, content))
|
||||
return name
|
||||
|
||||
|
||||
def _patch_tool_dependencies(monkeypatch, *, user_id: str = "u1", engine=None) -> list[dict]:
|
||||
frames, writer = _capture_writer()
|
||||
monkeypatch.setattr(tool_module, "DeepResearchEngine", lambda: engine or _FakeEngine())
|
||||
@@ -459,6 +549,86 @@ def test_tool_still_drops_old_topic_searches_before_new_request(monkeypatch) ->
|
||||
assert engine.adapters.materials.material_count == 3
|
||||
|
||||
|
||||
def test_tool_force_no_materials_skips_harvest(monkeypatch) -> None:
|
||||
_patch_tool_dependencies(monkeypatch)
|
||||
|
||||
asyncio.run(
|
||||
deep_research_report_tool.coroutine(
|
||||
_fake_runtime("thread-empty", context={"force_no_materials": True}),
|
||||
"call-empty",
|
||||
_DEFAULT_TOPIC,
|
||||
)
|
||||
)
|
||||
|
||||
engine = _FakeEngine.instances[-1]
|
||||
assert isinstance(engine.adapters.materials, StaticHarvestMaterialProvider)
|
||||
assert engine.adapters.materials.material_count == 0
|
||||
assert engine.requests[0].config.mode == "basic"
|
||||
|
||||
|
||||
def test_tool_threads_workbench_outline_into_research_config(monkeypatch) -> None:
|
||||
_patch_tool_dependencies(monkeypatch)
|
||||
|
||||
asyncio.run(
|
||||
deep_research_report_tool.coroutine(
|
||||
_fake_runtime(
|
||||
"thread-wb",
|
||||
context={
|
||||
"writing_report_config": {
|
||||
"custom_outline": "## 背景",
|
||||
"structure_mode": "fixed",
|
||||
"mode": "multi_agent",
|
||||
}
|
||||
},
|
||||
),
|
||||
"call-wb",
|
||||
_DEFAULT_TOPIC,
|
||||
)
|
||||
)
|
||||
|
||||
config = _FakeEngine.instances[-1].requests[0].config
|
||||
assert config.mode == "basic"
|
||||
assert config.custom_outline == "## 背景"
|
||||
assert config.structure_mode == "fixed"
|
||||
|
||||
|
||||
def test_tool_persists_workbench_session_before_tool_finished(monkeypatch) -> None:
|
||||
frames = _patch_tool_dependencies(monkeypatch)
|
||||
updates: list[dict] = []
|
||||
|
||||
class _Store:
|
||||
async def get(self, session_id, **kwargs):
|
||||
return {"id": session_id, "config_snapshot": {}}
|
||||
|
||||
async def update(self, session_id, **fields):
|
||||
updates.append({"session_id": session_id, **fields})
|
||||
return {"id": session_id, **fields}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.persistence.deep_research_sessions.get_default_store",
|
||||
lambda: _Store(),
|
||||
)
|
||||
|
||||
asyncio.run(
|
||||
deep_research_report_tool.coroutine(
|
||||
_fake_runtime(
|
||||
"thread-persist",
|
||||
context={"workbench_session_id": "sess-1"},
|
||||
),
|
||||
"call-persist",
|
||||
_DEFAULT_TOPIC,
|
||||
)
|
||||
)
|
||||
|
||||
assert updates
|
||||
assert updates[-1]["session_id"] == "sess-1"
|
||||
assert updates[-1]["status"] == "completed"
|
||||
assert updates[-1]["user_id"] is None
|
||||
assert "# 报告" in str(updates[-1]["report_markdown"])
|
||||
finished = next(frame for frame in frames if frame["event"]["type"] == "tool_finished")
|
||||
assert finished["event"]["payload"]["status"] == "success"
|
||||
|
||||
|
||||
def test_tool_retrieve_false_writes_with_existing_materials(monkeypatch) -> None:
|
||||
frames = _patch_tool_dependencies(monkeypatch)
|
||||
engines_before = len(_FakeEngine.instances)
|
||||
@@ -494,6 +664,88 @@ def test_tool_reports_engine_failure_without_artifacts(monkeypatch) -> None:
|
||||
assert any(f.get("event", {}).get("type") == "tool_finished" and f["event"]["payload"]["status"] == "error" for f in frames)
|
||||
|
||||
|
||||
def test_solution_tool_refuses_generation_before_requirement_confirmation() -> None:
|
||||
runtime = _fake_runtime("thread-solution-gate")
|
||||
runtime.context.update(
|
||||
{
|
||||
"analysis_project": {"mode": "solution"},
|
||||
"solution_requirements_confirmed": False,
|
||||
}
|
||||
)
|
||||
engines_before = len(_FakeEngine.instances)
|
||||
|
||||
command = asyncio.run(deep_research_report_tool.coroutine(runtime, "call-gate", "课题"))
|
||||
|
||||
assert "当前仍处于正常问答阶段" in command.update["messages"][0].content
|
||||
assert "结束问答并生成方案" in command.update["messages"][0].content
|
||||
assert "artifacts" not in command.update
|
||||
assert len(_FakeEngine.instances) == engines_before
|
||||
|
||||
|
||||
def test_solution_tool_overwrites_report_with_cleaned_citations(monkeypatch) -> None:
|
||||
engine = _FakeEngine()
|
||||
engine.result = DeepResearchResult(
|
||||
report_markdown=(
|
||||
"# 方案\n\n正文[1]和【2】。\n\n"
|
||||
"## 参考来源\n\n[1] 来源甲. https://example.com/a\n"
|
||||
"[2] 来源乙. https://example.com/b\n"
|
||||
),
|
||||
source_ids=["s1", "s2"],
|
||||
)
|
||||
artifact_writer = _CapturingArtifactWriter()
|
||||
_patch_tool_dependencies(monkeypatch, engine=engine)
|
||||
monkeypatch.setattr(
|
||||
tool_module,
|
||||
"ThreadOutputsArtifactWriter",
|
||||
lambda *args, **kwargs: artifact_writer,
|
||||
)
|
||||
runtime = _fake_runtime("thread-solution-clean")
|
||||
runtime.context.update(
|
||||
{
|
||||
"analysis_project": {"mode": "solution"},
|
||||
"solution_requirements_confirmed": True,
|
||||
}
|
||||
)
|
||||
|
||||
command = asyncio.run(deep_research_report_tool.coroutine(runtime, "call-clean", "课题"))
|
||||
|
||||
assert command.update["artifacts"] == ["/mnt/user-data/outputs/report.md"]
|
||||
assert artifact_writer.writes == [
|
||||
(
|
||||
"report.md",
|
||||
"# 方案\n\n正文和。\n\n"
|
||||
"## 参考来源\n\n- 来源甲. https://example.com/a\n"
|
||||
"- 来源乙. https://example.com/b\n",
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_ordinary_writing_tool_does_not_rewrite_numbered_citations(monkeypatch) -> None:
|
||||
engine = _FakeEngine()
|
||||
engine.result = DeepResearchResult(
|
||||
report_markdown="# 报告\n\n正文[1]。\n\n## 参考来源\n\n[1] 来源甲",
|
||||
source_ids=["s1"],
|
||||
)
|
||||
artifact_writer = _CapturingArtifactWriter()
|
||||
_patch_tool_dependencies(monkeypatch, engine=engine)
|
||||
monkeypatch.setattr(
|
||||
tool_module,
|
||||
"ThreadOutputsArtifactWriter",
|
||||
lambda *args, **kwargs: artifact_writer,
|
||||
)
|
||||
|
||||
command = asyncio.run(
|
||||
deep_research_report_tool.coroutine(
|
||||
_fake_runtime("thread-ordinary-citations"),
|
||||
"call-ordinary",
|
||||
_DEFAULT_TOPIC,
|
||||
)
|
||||
)
|
||||
|
||||
assert command.update["artifacts"] == ["/mnt/user-data/outputs/report.md"]
|
||||
assert artifact_writer.writes == []
|
||||
|
||||
|
||||
def test_tool_errors_cleanly_without_thread_id(monkeypatch) -> None:
|
||||
_patch_tool_dependencies(monkeypatch)
|
||||
runtime = SimpleNamespace(context={}, config={}, state={"messages": []})
|
||||
|
||||
Reference in new issue
Block a user