fix(ai写作): 恢复内网直连检索(知识库检索)空地址回退默认端点

knowledge_base 检索类型遇到空 intranet_search_url 会短路报「未配置」、返回 0
素材,导致「内网直连检索不能用」。旧版 get_search_provider(search_provider=
intranet) 在空地址时由 IntranetSearchProvider 回退内置默认内网端点,所以不配
地址也能直连。本次去掉该短路,空地址原样下传由 provider 回退默认端点,恢复旧逻辑。

- researcher.py: knowledge_base 分支无条件构造 IntranetSearchProvider
- test_ai_writing_knowledge_base.py: 空地址用例改为断言回退默认端点仍出素材
- backend CLAUDE.md: 检索类型段说明更新

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
董宏凯andClaude Opus 4.8 committed 2026-06-26 22:13:19 +08:00
1 parent 89232d7521
commit 0089af94d3
3 files changed
+23 -25

No files matched your search

+1 -1
View File
@@ -273,7 +273,7 @@ The AI-writing graph (`packages/harness/deerflow/agents/ai_writing/`) has four s
Seeding mirrors the roundtable pattern: verified `config.yaml` + `SOUL.md` ship in `app/gateway/routers/_ai_writing_seed_assets/<id>/`; `_ai_writing_seed.py::ensure_ai_writing_functional_agents()` copies them to `.deer-flow/agents/` when missing (never overwrites local edits), called from the startup lifespan (before `_sync_legacy_agents`, so the agents appear in `/api/agents` on first boot) and self-heals from `GET /api/ai-writing/sessions` + `GET /api/ai-writing/article-types`. Tests: `tests/test_ai_writing_seed.py`, `tests/test_ai_writing_agent_runner.py`.
**Skill-driven retrieval (检索类型).** `material_source` has three values: `general` (通用检索, default), `knowledge_base` (知识库检索), and `notebook` (我的空间); legacy `skill` is normalized to `general` by the researcher node. **`knowledge_base` 直连内网 ES 接口** — it bypasses skill orchestration entirely and calls `IntranetSearchProvider` over `ai_writing.intranet_search_url` (concurrent over the generated keywords; `intranet_knowledge_base`/`intranet_search_timeout` apply); an unconfigured url → no materials + a `skill_error` notice. This is the "弱模型识别不了技能" escape hatch — the old no-skill retrieval path restored as an explicit option (weak models like deepseek-chat often fail to invoke configured skills). Tests: `tests/test_ai_writing_knowledge_base.py`. General retrieval is orchestrated from the **skills configured on the `ai-writing-researcher` agent** (`config.yaml → skills`, editable from the agent management page): the built-in `knowledge-base-search` skill (知识库检索) runs the native keyword fan-out web/intranet search (the old default path), while any other configured skill searches with a `description`-targeted query; skills run in configured order with early-stop at `max_materials`, and an empty/unresolvable skills config falls back to the built-in knowledge search so retrieval always works. The skill itself is seeded from `_ai_writing_seed_assets/skills/knowledge-base-search/SKILL.md` into `skills/public/` (never overwrites), and a pre-existing researcher `config.yaml` lacking the `skills` key is patched in place with the default (explicit `skills: []` is respected). **PAUSE-1 input append flow**: when the user types natural language into the material-confirm input (`re_search`/legacy `enable_skill_search` + `user_query`), the node matches the most relevant configured skill via `SkillPicker(candidates=…)` (skipped when only one skill is configured), searches with the user's words, and **appends** the new materials to the existing package (URL-dedup, original order preserved, no LLM re-rank/truncation, 100-item hard cap). **Skill-failure fault tolerance**: a configured skill that times out / raises / has all its keyword searches fail (vs. legitimately returning 0 results) no longer just silently moves on — the researcher node emits a `skill_search_step` of type `skill_error` (carrying the error `reason`), and if the configured skills collected **no** new materials at all it runs a `fallback` pass over other available skills (`_fallback_candidate_skills`: the built-in `knowledge-base-search` native path first, then other enabled catalog skills not yet tried) instead of erroring out. The frontend `SkillStepsCard` renders `skill_error` (rose) and `fallback` (amber) so the timeline shows "技能报错 → 改用其他技能搜集". Tests: `tests/test_ai_writing_material_source.py`.
**Skill-driven retrieval (检索类型).** `material_source` has three values: `general` (通用检索, default), `knowledge_base` (知识库检索), and `notebook` (我的空间); legacy `skill` is normalized to `general` by the researcher node. **`knowledge_base` 直连内网 ES 接口(= 旧版「内网直连检索」)** — it bypasses skill orchestration entirely and calls `IntranetSearchProvider` over `ai_writing.intranet_search_url` (concurrent over the generated keywords; `intranet_knowledge_base`/`intranet_search_timeout` apply). **An empty/unconfigured `intranet_search_url` does NOT disable it** — the empty string is passed straight to `IntranetSearchProvider`, which falls back to its built-in default endpoint (`intranet_stub._DEFAULT_ENDPOINT`), exactly like the legacy `get_search_provider(search_provider=intranet)` path. So 内网直连检索 works out of the box without configuring an address (configure `intranet_search_url` only to point at a different endpoint). This is the "弱模型识别不了技能" escape hatch — the old no-skill retrieval path restored as an explicit option (weak models like deepseek-chat often fail to invoke configured skills). Tests: `tests/test_ai_writing_knowledge_base.py`. General retrieval is orchestrated from the **skills configured on the `ai-writing-researcher` agent** (`config.yaml → skills`, editable from the agent management page): the built-in `knowledge-base-search` skill (知识库检索) runs the native keyword fan-out web/intranet search (the old default path), while any other configured skill searches with a `description`-targeted query; skills run in configured order with early-stop at `max_materials`, and an empty/unresolvable skills config falls back to the built-in knowledge search so retrieval always works. The skill itself is seeded from `_ai_writing_seed_assets/skills/knowledge-base-search/SKILL.md` into `skills/public/` (never overwrites), and a pre-existing researcher `config.yaml` lacking the `skills` key is patched in place with the default (explicit `skills: []` is respected). **PAUSE-1 input append flow**: when the user types natural language into the material-confirm input (`re_search`/legacy `enable_skill_search` + `user_query`), the node matches the most relevant configured skill via `SkillPicker(candidates=…)` (skipped when only one skill is configured), searches with the user's words, and **appends** the new materials to the existing package (URL-dedup, original order preserved, no LLM re-rank/truncation, 100-item hard cap). **Skill-failure fault tolerance**: a configured skill that times out / raises / has all its keyword searches fail (vs. legitimately returning 0 results) no longer just silently moves on — the researcher node emits a `skill_search_step` of type `skill_error` (carrying the error `reason`), and if the configured skills collected **no** new materials at all it runs a `fallback` pass over other available skills (`_fallback_candidate_skills`: the built-in `knowledge-base-search` native path first, then other enabled catalog skills not yet tried) instead of erroring out. The frontend `SkillStepsCard` renders `skill_error` (rose) and `fallback` (amber) so the timeline shows "技能报错 → 改用其他技能搜集". Tests: `tests/test_ai_writing_material_source.py`.
**Skill-call timeout** is hardcoded **300s (5 min)** — `researcher.py` `_SKILL_SEARCH_TIMEOUT_SECONDS` (per-skill wrapper) and `_skill_agent.py` `_REAL_AGENT_TIMEOUT_SECONDS` (whole real-agent skill run). The intranet ES fallback (`IntranetSearchProvider`) gets a longer **600s (10 min)** default (`intranet_stub.py`; overridable via `ai_writing.intranet_search_timeout`) — that interface is the slow one, so our own fallback timeout is generous to avoid being blamed for "检不出".
@@ -5,9 +5,10 @@
config.yaml 配置的技能顺序编排检索。内置「知识库检索」技能
(knowledge-base-search)走原生 web/intranet 关键词并发检索;其它技能用
其 description 生成定向 query 后调真实 provider 检索。
- ``knowledge_base``(知识库检索):**直连内网知识库 ES 接口**
(``ai_writing.intranet_search_url`` 配的 ``IntranetSearchProvider``),
不编排任何技能、也不跑通用 provider。未配置接口地址时本次无素材并给出告警。
- ``knowledge_base``(知识库检索 / 内网直连检索):**直连内网知识库 ES 接口**
(``IntranetSearchProvider``),不编排任何技能、也不跑通用 provider。接口地址取
``ai_writing.intranet_search_url``,**留空时回退 provider 内置默认内网端点**——与旧版
``get_search_provider(search_provider=intranet)`` 一致,即便没配地址内网直连仍可用。
- ``notebook``(我的空间):仅用笔记 API 素材。
旧值 ``skill`` 仍按 ``general`` 兼容处理。
@@ -568,29 +569,22 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
# 仅使用 Step 1.5 已拉取的 notebook_materials。
_emit(f"仅使用笔记素材,跳过技能检索({len(notebook_materials)} 条)")
elif material_source == "knowledge_base":
# ── 知识库检索:直连内网 ES 接口 ──
# 用户在表单选了「知识库检索」→ 跳过技能编排、跳过通用 provider,
# 直接调 config.yaml 配的 ai_writing.intranet_search_url 内网知识库 ES 接口
# (IntranetSearchProvider)对检索词并发检索。未配置地址 → 无素材 + 告警。
intranet_url = (ai_writing_config.get("intranet_search_url") or "").strip()
if not intranet_url:
_emit("未配置内网知识库检索接口(ai_writing.intranet_search_url),本次未检索到素材")
search_errors.append("未配置 ai_writing.intranet_search_url,知识库检索不可用")
_emit_skill_step({
"type": "skill_error",
"message": "「知识库检索」未配置内网接口地址(ai_writing.intranet_search_url),无法检索",
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
"reason": "intranet_search_url 为空",
})
else:
# ── 知识库检索:直连内网 ES 接口(恢复旧版「内网直连检索」可用逻辑)──
# 用户在表单选了「知识库检索」→ 跳过技能编排、跳过通用 provider,直接调内网知识库
# ES 接口(IntranetSearchProvider)对检索词并发检索。接口地址优先取 config.yaml 的
# ai_writing.intranet_search_url;**留空时回退到 IntranetSearchProvider 内置默认端点**
# (历史上一直可用的内网 ES 地址),与旧版 get_search_provider(search_provider=intranet)
# 完全一致——所以即便没配地址,内网直连依然可用,不再短路成「未配置 → 无素材」。
from ..search.intranet_stub import IntranetSearchProvider
intranet_url = (ai_writing_config.get("intranet_search_url") or "").strip()
provider = IntranetSearchProvider(
base_url=intranet_url,
base_url=intranet_url, # 空串 → provider 内部回退默认内网端点(旧逻辑)
knowledge_base_name=ai_writing_config.get("intranet_knowledge_base", "samples"),
# 内网慢检索超时可在 config.yaml 的 ai_writing.intranet_search_timeout 调大,默认 600s。
timeout=ai_writing_config.get("intranet_search_timeout", 600),
)
endpoint_label = intranet_url or getattr(provider, "endpoint", "内置默认内网端点")
# 用户输入框追加(is_user_query_search)时 keywords 已是 [用户原话],全部检索;
# 首次检索按 keyword_count 截断检索词。
search_keywords = keywords if is_user_query_search else keywords[:keyword_count]
@@ -599,7 +593,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
_emit(f"知识库检索:直连内网接口检索 {total} 个方向…")
_emit_skill_step({
"type": "invoke_skill",
"message": f"使用「{_KNOWLEDGE_SKILL_DISPLAY}」直连内网接口检索 {total} 个方向:{('、'.join(search_keywords[:3]))[:60]}",
"message": f"使用「{_KNOWLEDGE_SKILL_DISPLAY}」直连内网接口({endpoint_label})检索 {total} 个方向:{('、'.join(search_keywords[:3]))[:60]}",
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
})
gathered = await asyncio.gather(
@@ -88,12 +88,16 @@ async def test_knowledge_base_hits_intranet_directly(monkeypatch):
@pytest.mark.asyncio
async def test_knowledge_base_no_url_empty(monkeypatch):
"""未配置内网接口地址:无素材,且不实例化 provider。"""
async def test_knowledge_base_no_url_falls_back_to_default_endpoint(monkeypatch):
"""未配置接口地址:仍实例化 IntranetSearchProvider(base_url 传空串,由 provider 内部
回退默认内网端点),内网直连依然可用——恢复旧版 search_provider=intranet 的行为。"""
_patch(monkeypatch, intranet_url="")
out = await R.researcher_node(_state(), {"configurable": {}})
assert out["material_package"]["materials"] == []
assert _FakeIntranet.instances == [], "无地址时不应实例化内网 provider"
mats = out["material_package"]["materials"]
assert mats, "无地址时应回退默认内网端点,仍返回素材"
assert all(m["source_type"] == "intranet" for m in mats)
assert _FakeIntranet.instances, "应实例化 IntranetSearchProvider 直连内网接口"
assert _FakeIntranet.instances[0].kw.get("base_url") == "", "空地址应原样下传,由 provider 回退默认端点"
@pytest.mark.asyncio