sync update from LAN repo (source dhk-1 @ 3e87be55)

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -1,10 +1,11 @@
"""素材收集专家节点:关键词提取 → 配置技能编排检索(含笔记) → 相关度排序。
"""素材收集专家节点:关键词提取 → 全量技能自由编排检索(含笔记) → 相关度排序。
检索类型(material_source)有三种:
- ``general``(通用检索,默认):按素材收集专家 agent(ai-writing-researcher)
config.yaml 配置的技能顺序编排检索。内置「知识库检索」技能
(knowledge-base-search)走原生 web/intranet 关键词并发检索;其它技能用
其 description 生成定向 query 后调真实 provider 检索。
- ``general``(通用检索,默认):像普通问答一样从**全部启用技能**里自由编排检索
(不再按 ai-writing-researcher agent config.yaml 的 skills 白名单限制)。
内置「知识库检索」技能(knowledge-base-search)保底且排最前,走原生
web/intranet 关键词并发检索;再由模型按写作意图从全部启用技能中挑最相关的
补充检索(其它技能用其 description 生成定向 query 调真实 provider)。
- ``knowledge_base``(知识库检索 / 内网直连检索):**直连内网知识库 ES 接口**
(``IntranetSearchProvider``),不编排任何技能、也不跑通用 provider。接口地址取
``ai_writing.intranet_search_url``,**留空时回退 provider 内置默认内网端点**——与旧版
@@ -13,7 +14,7 @@
旧值 ``skill`` 仍按 ``general`` 兼容处理。
PAUSE-1 素材确认卡的单输入框:用户用自然语言描述想补充检索的内容
(如「检索 A 的数据」)时,让模型从已配置技能中按相关度匹配,逐个检索,
(如「检索 A 的数据」)时,让模型从全部启用技能中按相关度匹配,逐个检索,
把新素材**追加**到现有素材列表(URL 去重、保持原有顺序、不重排不丢弃)。
"""
from __future__ import annotations
@@ -127,6 +128,23 @@ def _fallback_knowledge_skill() -> SimpleNamespace:
return SimpleNamespace(name=KNOWLEDGE_SEARCH_SKILL_NAME, description=_KNOWLEDGE_SKILL_FALLBACK_DESC)
def _knowledge_search_is_explicitly_disabled() -> bool:
"""Return whether the operator explicitly took the built-in fallback offline.
Missing catalog files and transient discovery failures keep the historical
in-memory fallback, while an explicit ``enabled: false`` must not be
reintroduced into the model-visible candidate pool.
"""
try:
from deerflow.config.extensions_config import ExtensionsConfig
state = ExtensionsConfig.from_file().skills.get(KNOWLEDGE_SEARCH_SKILL_NAME)
return state is not None and not state.enabled
except Exception:
logger.warning("researcher: 无法读取知识库检索技能开关,保留内存兜底", exc_info=True)
return False
def _unique_limited(values: list, limit: int) -> list:
"""Stable de-dup for short diagnostic lists."""
return list(dict.fromkeys(values or []))[:limit]
@@ -152,56 +170,43 @@ def _intranet_query_summary(rows: list[dict], limit: int = 5) -> str:
return ";".join(parts)
def _load_configured_skills() -> list:
"""解析素材收集专家 agent(ai-writing-researcher)配置的技能列表。
def _load_candidate_skills() -> list:
"""解析素材收集的候选技能池:**全部启用技能**,内置「知识库检索」保底且排最前。
config.yaml 的 ``skills`` 字段是权威来源(admin/用户在智能体管理页可改);
未配置 / 解析失败 / 技能文件全部缺失时,一律回退为内置「知识库检索」,
保证通用检索永远可用。返回的元素带 ``name`` / ``description`` 属性。
不再受素材收集专家 agent(ai-writing-researcher)config.yaml ``skills``
白名单限制——像普通问答一样,模型从全部启用技能里自由挑选最相关的去
检索(见下方 SkillPicker 编排)。技能目录加载失败时回退为仅内置
「知识库检索」,保证通用检索永远可用。返回的元素带 ``name`` / ``description``
属性。
"""
names: list[str] = []
try:
from deerflow.config.agents_config import load_agent_config
cfg = load_agent_config(RESEARCHER_AGENT_ID)
if cfg is not None:
names = [n.strip() for n in (cfg.skills or []) if isinstance(n, str) and n.strip()]
except Exception:
logger.warning("researcher: 读取 agent 配置技能失败,回退知识库检索", exc_info=True)
if not names:
return [_fallback_knowledge_skill()]
by_name: dict[str, object] = {}
skills: list = []
try:
from deerflow.skills.storage import get_or_new_skill_storage
by_name = {s.name: s for s in get_or_new_skill_storage().load_skills(enabled_only=False)}
skills = [s for s in get_or_new_skill_storage().load_skills(enabled_only=True) if getattr(s, "name", None)]
except Exception:
logger.warning("researcher: 加载技能目录失败", exc_info=True)
logger.warning("researcher: 加载技能目录失败,回退内置知识库检索", exc_info=True)
resolved: list = []
for n in names:
sk = by_name.get(n)
if sk is None and n == KNOWLEDGE_SEARCH_SKILL_NAME:
sk = _fallback_knowledge_skill()
if sk is None:
logger.warning("researcher: 配置的技能 '%s' 不存在于技能目录,已跳过", n)
continue
resolved.append(sk)
return resolved or [_fallback_knowledge_skill()]
# 内置「知识库检索」保底:已在目录里则挪到最前(原生多关键词并发检索始终
# 先跑);目录缺失/加载失败时补内存兜底,但显式下线时绝不补回。
knowledge = next((s for s in skills if s.name == KNOWLEDGE_SEARCH_SKILL_NAME), None)
if knowledge is not None:
skills = [knowledge, *[s for s in skills if s.name != KNOWLEDGE_SEARCH_SKILL_NAME]]
elif not _knowledge_search_is_explicitly_disabled():
skills = [_fallback_knowledge_skill(), *skills]
return skills
def _fallback_candidate_skills(tried: set[str]) -> list:
"""容错兜底:从技能目录里挑出**尚未尝试过**的可用技能,内置「知识库检索」优先。
当配置技能调用全部失败、且没收集到任何新素材时,researcher 用这些技能再试一轮,
当本轮技能调用全部失败、且没收集到任何新素材时,researcher 用这些技能再试一轮,
而不是直接报错。内置知识库检索(原生多关键词并发,最稳)排在最前,其后是技能目录里
其它已启用的技能。技能目录加载失败时只回退内置知识库检索,保证兜底链永不断。
"""
candidates: list = []
seen: set[str] = set(tried)
if KNOWLEDGE_SEARCH_SKILL_NAME not in seen:
if KNOWLEDGE_SEARCH_SKILL_NAME not in seen and not _knowledge_search_is_explicitly_disabled():
candidates.append(_fallback_knowledge_skill())
seen.add(KNOWLEDGE_SEARCH_SKILL_NAME)
try:
@@ -484,7 +489,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
is_supplement = action == "supplement_search"
# PAUSE-1 素材确认卡用户在输入框填了自然语言(如「检索 A 的数据」):
# - 跳过 LLM 重新生成检索词(节省一次调用 + 尊重用户意图)
# - 从 agent 配置的技能中匹配最相关的技能,逐个检索
# - 从全部启用技能中匹配最相关的技能,逐个检索
# - **追加**:保留 existing material_package,新素材按 URL 去重后追加在末尾
# 旧版「启用技能检索」按钮(action=enable_skill_search)按同一语义兼容处理。
is_user_query_search = action in ("re_search", "enable_skill_search") and bool(user_query)
@@ -499,7 +504,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
requested_append_count, residual_topic = _parse_append_request(user_query)
if requested_append_count is not None:
effective_user_query = residual_topic or intent
# 用户在写作表单选择的检索类型:general(通用检索,按 agent 配置技能编排)/
# 用户在写作表单选择的检索类型:general(通用检索,从全部启用技能自由编排)/
# knowledge_base(知识库检索,直连内网 ES 接口)/ notebook(我的空间)。
# 旧值 skill 一律按 general 兼容。
raw_source = (intervention.get("material_source") or state.get("material_source") or "general").strip()
@@ -640,12 +645,13 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
_emit(f"从笔记中获取到 {len(notebook_materials)} 条素材")
# Step 2: 检索
# 通用检索 = 配置技能编排。技能不是素材,是"检索策略 / 视角":
# a. 从 ai-writing-researcher agent config.yaml 读取配置技能(默认知识库检索)
# b. 「知识库检索」技能 → 对 Step 1 检索词做原生并发 web/intranet 检索
# c. 其它技能 → 用 skill.description 拼定向 query 调真实 provider
# d. user_query 模式(PAUSE-1 输入框)→ 先让模型按用户输入匹配技能再检索
# e. 累积素材到 max_materials 即 break,推 stop_early
# 通用检索 = 全量技能自由编排。技能不是素材,是"检索策略 / 视角":
# a. 候选池 = 全部启用技能(内置「知识库检索」保底且排最前,_load_candidate_skills)
# b. 池里有多个技能时让模型按相关度挑选(SkillPicker,像普通问答自由选技能)
# c. 「知识库检索」技能 → 对 Step 1 检索词做原生并发 web/intranet 检索
# d. 其它技能 → 用 skill.description 拼定向 query 调真实 provider
# e. user_query 模式(PAUSE-1 输入框)→ 按用户原话从全部启用技能中匹配再检索
# f. 累积素材到 max_materials 即 break,推 stop_early
skill_steps: list[dict] = []
search_errors: list[str] = []
@@ -794,34 +800,52 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
new_results.extend(results or [])
search_errors.extend(getattr(provider, "errors", []))
else:
# ── 通用检索:配置技能编排 ──
configured = _load_configured_skills()
# ── 通用检索:全量技能自由编排 ──
# 候选池 = 全部启用技能(内置「知识库检索」保底且排最前),不再按
# ai-writing-researcher agent config.yaml 的 skills 白名单限制——
# 像普通问答一样想用什么技能就用什么:
# a. 首次检索:知识库检索原生路径先跑(多关键词并发,最稳的基线),
# 再让模型从全部启用技能里按写作意图挑最相关的补充检索
# b. PAUSE-1 用户输入:模型按用户原话从全部启用技能中匹配排序后逐个检索
# c. 池里只有知识库检索一个技能时保持旧行为:不调 picker,直接原生检索
# d. 累积素材到 max_materials 即 break,推 stop_early
candidates = _load_candidate_skills()
provider = get_search_provider(ai_writing_config)
per_skill = max(1, max_materials)
# 用户显式要「再追加 N 条」时,单技能至少检索 N 条,保证能凑够数量(末尾再截断到 N)
if requested_append_count:
per_skill = max(per_skill, requested_append_count)
ordered = configured
if is_user_query_search and len(configured) > 1:
# 用户输入了自然语言:让模型在配置技能中按相关度匹配排序
# (技能选择走流式 LLM,推 skill_select_chunk,前端打字机展示)
# 内置知识库检索通常位于池子第一位;若管理员把所有技能下线,保持
# 空列表并继续走后面的非技能检索兜底,不能偷偷复活技能。
ordered = [candidates[0]] if candidates else []
if len(candidates) > 1:
baseline_skill = candidates[0]
# 技能选择走流式 LLM(推 skill_select_chunk,前端打字机展示);
# 首次检索不带用户原话(user_query 留空),按写作意图挑。
picker = SkillPicker(
config=config,
model_name=keyword_model,
emit_step=_emit_skill_step,
max_select=5,
user_query=effective_user_query,
candidates=configured,
user_query=effective_user_query if is_user_query_search else "",
candidates=candidates,
)
ranked = await picker.pick(user_intent=intent)
search_errors.extend(picker.errors)
ordered = [entry["skill"] for entry in ranked] or configured
if is_user_query_search:
# 用户输入模式:完全按模型匹配结果检索(不含保底顺序偏好)
ordered = [entry["skill"] for entry in ranked] or candidates
else:
# 首次/重检:知识库检索原生路径保底在前 + 模型挑中的其余技能
ordered = [baseline_skill, *[
entry["skill"] for entry in ranked if entry["skill"].name != baseline_skill.name
]]
else:
_emit_skill_step({
"type": "list_skills",
"message": f"素材收集专家已配置 {len(configured)} 个检索技能",
"skill_names": [s.name for s in configured],
"message": f"素材收集专家可用 {len(candidates)} 个检索技能",
"skill_names": [s.name for s in candidates],
})
seen_urls_new: set[str] = set()
@@ -958,7 +982,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
results, error_detail = await _timed_search(sk.name, skill_query, per_skill)
return _collect(results), error_detail
# 失败技能名(去重后用于兜底提示):记录配置技能里调用失败的那些
# 失败技能名(去重后用于兜底提示):记录本轮调用失败的那些技能
failed_skill_names: list[str] = []
tried_names: set[str] = set()
@@ -1095,7 +1119,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
})
break
# ── 容错兜底:配置技能调用失败且没收集到任何新素材 → 改用其它可用技能继续检索 ──
# ── 容错兜底:本轮技能调用失败且没收集到任何新素材 → 改用其它可用技能继续检索 ──
# (不直接报错;前端时间线靠 skill_error / fallback 步骤展示"技能报错,改用其它技能搜集")
if not new_results and failed_skill_names:
fallback_skills = _fallback_candidate_skills(tried_names)
@@ -1103,7 +1127,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
_emit_skill_step({
"type": "fallback",
"message": (
f"已配置技能({'、'.join(dict.fromkeys(failed_skill_names))})检索失败,"
f"本轮技能({'、'.join(dict.fromkeys(failed_skill_names))})检索失败,"
"改用其他技能继续搜集素材"
),
"skill_names": [_skill_display(s) for s in fallback_skills],