Add intranet retrieval diagnostics for AI writing
This commit is contained in:
1 parent
bcd656fb49
commit
4fce6f9104
4 files changed
+236
-22
No files matched your search
@@ -19,16 +19,33 @@ class _FakeIntranet:
|
||||
def __init__(self, **kw):
|
||||
self.errors = []
|
||||
self.raw_responses = []
|
||||
self.calls = []
|
||||
self.kw = kw
|
||||
_FakeIntranet.instances.append(self)
|
||||
|
||||
async def search(self, query, max_results=5, **_):
|
||||
self.calls.append({"query": query, "status": "ok", "ok": True, "count": 1, "elapsed_ms": 12})
|
||||
return [SearchResult(
|
||||
title=f"内网-{query}", content="内网正文", source="ES",
|
||||
published_at="", url=f"http://es/{query}", source_type="intranet",
|
||||
)]
|
||||
|
||||
|
||||
class _EmptyIntranet(_FakeIntranet):
|
||||
async def search(self, query, max_results=5, **_):
|
||||
self.calls.append({"query": query, "status": "empty", "ok": True, "count": 0, "elapsed_ms": 9})
|
||||
return []
|
||||
|
||||
|
||||
class _FailingIntranet(_FakeIntranet):
|
||||
async def search(self, query, max_results=5, **_):
|
||||
msg = f"ConnectError: cannot connect for {query}"
|
||||
self.errors.append(msg)
|
||||
self.raw_responses.append("{\"success\": false, \"error\": \"cannot connect\"}")
|
||||
self.calls.append({"query": query, "status": "error", "ok": False, "count": 0, "elapsed_ms": 3, "error": msg})
|
||||
return []
|
||||
|
||||
|
||||
class _SkillProviderShouldNotRun:
|
||||
"""knowledge_base 模式不应触碰通用 provider —— 调到就让测试爆。"""
|
||||
errors: list = []
|
||||
@@ -38,7 +55,7 @@ class _SkillProviderShouldNotRun:
|
||||
raise AssertionError("knowledge_base 模式不应调用通用 provider / 技能检索")
|
||||
|
||||
|
||||
def _patch(monkeypatch, intranet_url="http://es/api"):
|
||||
def _patch(monkeypatch, intranet_url="http://es/api", intranet_cls=_FakeIntranet):
|
||||
_FakeIntranet.instances = []
|
||||
cfg = {
|
||||
"max_materials": 20,
|
||||
@@ -60,7 +77,7 @@ def _patch(monkeypatch, intranet_url="http://es/api"):
|
||||
monkeypatch.setattr(R, "get_search_provider", lambda _cfg: _SkillProviderShouldNotRun())
|
||||
monkeypatch.setattr(
|
||||
"deerflow.agents.ai_writing.search.intranet_stub.IntranetSearchProvider",
|
||||
_FakeIntranet,
|
||||
intranet_cls,
|
||||
)
|
||||
|
||||
|
||||
@@ -85,6 +102,14 @@ async def test_knowledge_base_hits_intranet_directly(monkeypatch):
|
||||
assert _FakeIntranet.instances, "应实例化 IntranetSearchProvider 直连内网接口"
|
||||
# 走素材确认暂停点
|
||||
assert out["status"] == "awaiting_material_confirm"
|
||||
dbg = out["material_package"].get("intranet_debug")
|
||||
assert dbg["mode"] == "knowledge_base"
|
||||
assert dbg["status"] == "ok"
|
||||
assert dbg["endpoint"] == "http://es/api"
|
||||
assert dbg["result_count"] == 1
|
||||
assert dbg["queries"][0]["query"] == "特朗普竞选"
|
||||
assert dbg["queries"][0]["count"] == 1
|
||||
assert dbg["calls"][0]["status"] == "ok"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -107,3 +132,34 @@ async def test_knowledge_base_does_not_orchestrate_skills(monkeypatch):
|
||||
# 不抛异常即证明没走 _SkillProviderShouldNotRun
|
||||
out = await R.researcher_node(_state(), {"configurable": {}})
|
||||
assert out["material_package"]["materials"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_knowledge_base_empty_result_is_visible(monkeypatch):
|
||||
"""接口成功但 0 条也要在返回里能看出来,而不是只显示"无素材"。"""
|
||||
_patch(monkeypatch, intranet_cls=_EmptyIntranet)
|
||||
out = await R.researcher_node(_state(), {"configurable": {}})
|
||||
pkg = out["material_package"]
|
||||
assert pkg["materials"] == []
|
||||
dbg = pkg.get("intranet_debug")
|
||||
assert dbg["status"] == "empty"
|
||||
assert dbg["result_count"] == 0
|
||||
assert dbg["queries"][0]["count"] == 0
|
||||
assert dbg["calls"][0]["status"] == "empty"
|
||||
steps = pkg["skill_search_steps"]
|
||||
assert any(step["type"] == "skill_result" and "0 条" in step["message"] for step in steps)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_knowledge_base_failure_is_visible(monkeypatch):
|
||||
"""provider 吞掉异常返回 [] 时,前端仍应看到报错步骤与 notice。"""
|
||||
_patch(monkeypatch, intranet_cls=_FailingIntranet)
|
||||
out = await R.researcher_node(_state(), {"configurable": {}})
|
||||
pkg = out["material_package"]
|
||||
dbg = pkg.get("intranet_debug")
|
||||
assert dbg["status"] == "error"
|
||||
assert dbg["errors"]
|
||||
assert dbg["raw_responses"]
|
||||
steps = pkg["skill_search_steps"]
|
||||
assert any(step["type"] == "skill_error" for step in steps)
|
||||
assert any(event["type"] == "notice" and event.get("raw") for event in out["progress_events"])
|
||||
Reference in new issue
Block a user