feat(ai-writing): 检索类型加回「知识库检索」(直连内网 ES 接口)

弱模型常识别不了配置技能(deepseek-chat 等),恢复旧的"不用技能"检索路径
作为独立检索类型 knowledge_base:绕过技能编排,直接调
ai_writing.intranet_search_url 内网知识库 ES 接口(IntranetSearchProvider)
对检索词并发检索;未配地址 → 无素材 + skill_error 告警。

- researcher.py: material_source 归一保留 knowledge_base + 新增直连内网分支
- state.py: material_source 三值注释
- 前端 AIWritingForm/WritingFormContext/types: 下拉新增「知识库检索」
- tests/test_ai_writing_knowledge_base.py: 直连/未配/不编排技能 3 用例
- backend CLAUDE.md: 检索类型段更新为三值

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
fengxiangdebaoziandClaude Opus 4.8 committed 2026-06-26 10:04:57 +08:00
1 parent a400989f64
commit 3d60651491
7 files changed
+182 -16

No files matched your search

@@ -40,11 +40,12 @@ const INPUT_CLASS =
const DEFAULT_KEYWORD_COUNT = 1;
const DEFAULT_AUDIENCE = '普通读者';
type MaterialSource = 'general' | 'notebook';
type MaterialSource = 'general' | 'knowledge_base' | 'notebook';
const MAX_INTENT_LENGTH = 500;
const MATERIAL_SOURCE_OPTIONS: Array<{ value: MaterialSource; label: string; desc: string }> = [
{ value: 'general', label: '通用检索', desc: '用素材收集专家配置的技能检索(默认:知识库检索)' },
{ value: 'knowledge_base', label: '知识库检索', desc: '直连内网知识库检索接口,不走技能编排' },
{ value: 'notebook', label: '我的空间', desc: '从空间中检索素材' },
];
@@ -37,8 +37,8 @@ export interface WritingFormValue {
/** 是否在成稿里加参考文献(正文 [n] 角标 + 文末「参考文献」列表,像论文)。默认开。 */
addReferences: boolean;
setAddReferences: (v: boolean) => void;
materialSource: 'general' | 'notebook';
setMaterialSource: (v: 'general' | 'notebook') => void;
materialSource: 'general' | 'knowledge_base' | 'notebook';
setMaterialSource: (v: 'general' | 'knowledge_base' | 'notebook') => void;
/** 不收集素材,直接用通用知识写作:勾选后跳过素材检索阶段(含素材确认暂停点)。默认关。 */
skipResearch: boolean;
setSkipResearch: (v: boolean) => void;
@@ -75,7 +75,7 @@ export function WritingFormProvider({
initialDocumentIds,
}: {
children: ReactNode;
initialMaterialSource?: 'general' | 'notebook';
initialMaterialSource?: 'general' | 'knowledge_base' | 'notebook';
initialNotebookId?: number | null;
initialDocumentIds?: number[];
}) {
@@ -88,7 +88,7 @@ export function WritingFormProvider({
const [audience, setAudience] = useState('普通读者');
const [strictMode, setStrictMode] = useState(false);
const [addReferences, setAddReferences] = useState(true);
const [materialSource, setMaterialSource] = useState<'general' | 'notebook'>(initialMaterialSource ?? 'general');
const [materialSource, setMaterialSource] = useState<'general' | 'knowledge_base' | 'notebook'>(initialMaterialSource ?? 'general');
const [skipResearch, setSkipResearch] = useState(false);
const [selectedNotebookId, setSelectedNotebookId] = useState<number | null>(initialNotebookId ?? null);
const [selectedDocumentIds, setSelectedDocumentIds] = useState<number[]>(initialDocumentIds ?? []);
@@ -105,8 +105,10 @@ export function WritingFormProvider({
if (payload.target_audience?.trim()) setAudience(payload.target_audience.trim());
if (payload.writing_mode === 'strict') setStrictMode(true);
else if (payload.writing_mode === 'loose') setStrictMode(false);
// 旧值 skill / knowledge_base 已合并为通用检索 general
if (payload.material_source === 'skill' || payload.material_source === 'knowledge_base' || payload.material_source === 'general') {
// knowledge_base=知识库检索(直连内网 ES)保留;旧值 skill 归一为通用检索 general
if (payload.material_source === 'knowledge_base') {
setMaterialSource('knowledge_base');
} else if (payload.material_source === 'skill' || payload.material_source === 'general') {
setMaterialSource('general');
}
if (payload.outline?.trim()) {
@@ -265,8 +265,9 @@ export interface AIWritingRequest {
writingMode: "strict" | "loose";
/** 是否在成稿里加参考文献(正文 [n] 角标 + 文末「参考文献」列表,像论文)。默认 true。 */
addReferences?: boolean;
/** 检索类型:general=通用检索(素材收集专家按配置技能检索,默认知识库检索技能)/ notebook=我的空间 */
materialSource: "general" | "notebook";
/** 检索类型:general=通用检索(素材收集专家按配置技能检索,默认知识库检索技能)/
* knowledge_base=知识库检索(直连内网知识库 ES 接口,不走技能编排)/ notebook=我的空间 */
materialSource: "general" | "knowledge_base" | "notebook";
/** 不收集素材,直接用通用知识写作:勾选后跳过整个素材检索流程(含素材确认暂停点),
* 直接进入大纲规划;后端强制 loose 写作模式。默认 false。 */
skipResearch?: boolean;
+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 exactly two values: `general` (通用检索, default) and `notebook` (我的空间); legacy `knowledge_base`/`skill` are normalized to `general` by the researcher node. 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 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-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 "检不出".
@@ -1,12 +1,15 @@
"""素材收集专家节点:关键词提取 → 配置技能编排检索(含笔记) → 相关度排序。
检索类型(material_source)只有两种:
检索类型(material_source)有三种:
- ``general``(通用检索,默认):按素材收集专家 agent(ai-writing-researcher)
config.yaml 配置的技能顺序编排检索。内置「知识库检索」技能
(knowledge-base-search)走原生 web/intranet 关键词并发检索;其它技能用
其 description 生成定向 query 后调真实 provider 检索。
- ``knowledge_base``(知识库检索):**直连内网知识库 ES 接口**
(``ai_writing.intranet_search_url`` 配的 ``IntranetSearchProvider``),
不编排任何技能、也不跑通用 provider。未配置接口地址时本次无素材并给出告警。
- ``notebook``(我的空间):仅用笔记 API 素材。
旧值 ``knowledge_base`` / ``skill`` 一律按 ``general`` 兼容处理。
旧值 ``skill`` 仍按 ``general`` 兼容处理。
PAUSE-1 素材确认卡的单输入框:用户用自然语言描述想补充检索的内容
(如「检索 A 的数据」)时,让模型从已配置技能中按相关度匹配,逐个检索,
@@ -414,9 +417,10 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
if requested_append_count is not None:
effective_user_query = residual_topic or intent
# 用户在写作表单选择的检索类型:general(通用检索,按 agent 配置技能编排)/
# notebook(我的空间)。旧值 knowledge_base / skill 一律按 general 兼容。
# knowledge_base(知识库检索,直连内网 ES 接口)/ notebook(我的空间)。
# 旧值 skill 一律按 general 兼容。
raw_source = (state.get("material_source") or "general").strip()
material_source = "notebook" if raw_source == "notebook" else "general"
material_source = raw_source if raw_source in ("notebook", "knowledge_base") else "general"
# 用户指定的检索关键词数量(1~10),决定生成多少个检索词、检索多少个方向
keyword_count = _resolve_keyword_count(state.get("keyword_count"))
@@ -560,6 +564,58 @@ 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:
from ..search.intranet_stub import IntranetSearchProvider
provider = IntranetSearchProvider(
base_url=intranet_url,
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),
)
# 用户输入框追加(is_user_query_search)时 keywords 已是 [用户原话],全部检索;
# 首次检索按 keyword_count 截断检索词。
search_keywords = keywords if is_user_query_search else keywords[:keyword_count]
total = len(search_keywords)
per_keyword = _per_keyword_limit(max_materials, total)
_emit(f"知识库检索:直连内网接口检索 {total} 个方向…")
_emit_skill_step({
"type": "invoke_skill",
"message": f"使用「{_KNOWLEDGE_SKILL_DISPLAY}」直连内网接口检索 {total} 个方向:{('、'.join(search_keywords[:3]))[:60]}",
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
})
gathered = await asyncio.gather(
*[provider.search(kw, max_results=per_keyword) for kw in search_keywords],
return_exceptions=True,
)
for results in gathered:
if isinstance(results, Exception):
logger.warning("[knowledge-base] search failed: %s", results)
search_errors.append(f"{type(results).__name__}: {results}")
continue
new_results.extend(results or [])
search_errors.extend(getattr(provider, "errors", []))
_emit_skill_step({
"type": "skill_result",
"message": f"「{_KNOWLEDGE_SKILL_DISPLAY}」(内网接口)检索到 {len(new_results)} 条素材",
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
"matched_count": len(new_results),
})
elif is_append_only:
# ── 兼容旧版追加关键词模式 ──
# 直接对用户给的关键词做原生并发检索(不走技能编排),结果追加到旧素材。
@@ -141,8 +141,9 @@ class AIWritingState(TypedDict):
# 是否在成稿里加参考文献:正文引用素材处加角标 [n],文末按出现顺序生成「参考文献」列表(像论文)。
add_references: bool
# 检索类型:general=通用检索(按素材收集专家 agent 配置的技能编排,默认)/
# notebook=我的空间(仅笔记检索)。旧值 knowledge_base / skill 由 researcher
# 节点按 general 兼容处理。
# knowledge_base=知识库检索(直连 ai_writing.intranet_search_url 内网 ES 接口,
# 不走技能编排——弱模型识别不了技能时用这条)/ notebook=我的空间(仅笔记检索)。
# 旧值 skill 由 researcher 节点按 general 兼容处理。
material_source: str
# 跳过素材收集:用户在初始表单勾选「不收集素材,直接用通用知识写作」时为 True。
# researcher 节点在**首次**进入(无任何 pause 干预)时短路,跳过整个检索流程直接
@@ -0,0 +1,105 @@
"""researcher_node:检索类型 `knowledge_base`(知识库检索)= 直连内网 ES 接口。
弱模型识别不了技能时用这条路:不编排任何技能、不跑通用 provider,
直接调 ai_writing.intranet_search_url 内网知识库 ES 接口检索。
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
from deerflow.agents.ai_writing.nodes import researcher as R
from deerflow.agents.ai_writing.search.base import SearchResult
class _FakeIntranet:
instances: list = []
def __init__(self, **kw):
self.errors = []
self.raw_responses = []
self.kw = kw
_FakeIntranet.instances.append(self)
async def search(self, query, max_results=5, **_):
return [SearchResult(
title=f"内网-{query}", content="内网正文", source="ES",
published_at="", url=f"http://es/{query}", source_type="intranet",
)]
class _SkillProviderShouldNotRun:
"""knowledge_base 模式不应触碰通用 provider —— 调到就让测试爆。"""
errors: list = []
raw_responses: list = []
async def search(self, query, max_results=5, **_): # pragma: no cover - 不应被调用
raise AssertionError("knowledge_base 模式不应调用通用 provider / 技能检索")
def _patch(monkeypatch, intranet_url="http://es/api"):
_FakeIntranet.instances = []
cfg = {
"max_materials": 20,
"intranet_search_url": intranet_url,
"intranet_knowledge_base": "samples",
}
monkeypatch.setattr(R, "get_ai_writing_config", lambda: cfg)
monkeypatch.setattr(R, "use_builtin_agent", lambda *_: False)
monkeypatch.setattr(R, "safe_get_stream_writer", lambda: (lambda *a, **k: None))
monkeypatch.setattr(R, "use_skill_search_agent", lambda: False)
async def fake_llm_json(*a, **k):
return {"keywords": ["特朗普竞选"]}
monkeypatch.setattr(R, "llm_json", fake_llm_json)
# knowledge_base 模式不该加载/编排技能,但仍打桩以防意外路径
monkeypatch.setattr(R, "_load_configured_skills",
lambda: [SimpleNamespace(name="knowledge-search-v2", description="内网检索")])
monkeypatch.setattr(R, "get_search_provider", lambda _cfg: _SkillProviderShouldNotRun())
monkeypatch.setattr(
"deerflow.agents.ai_writing.search.intranet_stub.IntranetSearchProvider",
_FakeIntranet,
)
def _state(material_source="knowledge_base"):
return {
"user_intent": "分析特朗普近期动向",
"article_type": "政治分析",
"target_audience": "普通读者",
"keyword_count": 1,
"material_source": material_source,
}
@pytest.mark.asyncio
async def test_knowledge_base_hits_intranet_directly(monkeypatch):
_patch(monkeypatch)
out = await R.researcher_node(_state(), {"configurable": {}})
mats = out["material_package"]["materials"]
assert mats, "知识库检索应返回内网素材"
assert all(m["source_type"] == "intranet" for m in mats)
assert mats[0]["title"] == "内网-特朗普竞选"
assert _FakeIntranet.instances, "应实例化 IntranetSearchProvider 直连内网接口"
# 走素材确认暂停点
assert out["status"] == "awaiting_material_confirm"
@pytest.mark.asyncio
async def test_knowledge_base_no_url_empty(monkeypatch):
"""未配置内网接口地址:无素材,且不实例化 provider。"""
_patch(monkeypatch, intranet_url="")
out = await R.researcher_node(_state(), {"configurable": {}})
assert out["material_package"]["materials"] == []
assert _FakeIntranet.instances == [], "无地址时不应实例化内网 provider"
@pytest.mark.asyncio
async def test_knowledge_base_does_not_orchestrate_skills(monkeypatch):
"""knowledge_base 不编排技能:通用 provider 一旦被调用就会 raise。"""
_patch(monkeypatch)
# 不抛异常即证明没走 _SkillProviderShouldNotRun
out = await R.researcher_node(_state(), {"configurable": {}})
assert out["material_package"]["materials"]