fix(ai写作): 技能检索稳定接收数据(复用参考文献提取+截断抢救+上限早停)
- 复用主智能体「参考文献」模式的提取逻辑(deerflow.runtime.references)解析技能JSON,字段并集兼容 - bash输出中间截断时逐条抢救完整记录对象,不再因大JSON解析失败而全丢 - 所有退出路径(超时/递归上限/异常)都从tool输出收割,不依赖模型最终汇总 - 达到max_materials上限即停止驱动模型,修复步骤条爆100+条/素材超额 - 步骤条实时显示每次检索获得多少条数据 - 内网兜底:与技能并行预跑、响应解析兼容两套接口字段、0结果换简单检索词重试、超时放宽到10分钟 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
893f1e6933
commit
854de9b43c
7 files changed
+340
-162
No files matched your search
+186
-107
@@ -70,108 +70,152 @@ _MATERIAL_OUTPUT_CONTRACT = (
|
||||
)
|
||||
|
||||
|
||||
# 技能 stdout 里「结果列表」常见的字段名(不同技能各异,做并集兼容)
|
||||
_RESULT_LIST_KEYS = ("results", "data", "items", "materials", "documents", "list", "hits")
|
||||
_TITLE_KEYS = ("title", "m_title", "name", "doc_title")
|
||||
_CONTENT_KEYS = ("content", "content_preview", "page_content", "text", "summary", "abstract")
|
||||
_SOURCE_KEYS = ("source", "source_category", "source1", "source_name", "kb_name")
|
||||
_DATE_KEYS = ("publish_time", "published_at", "m_publish", "date", "create_time")
|
||||
def _scan_balanced_json_objects(text: str) -> list:
|
||||
"""从可能被**中间截断**的文本里扫出所有「括号平衡且完整」的 ``{...}`` 子串(含各嵌套层级)。
|
||||
|
||||
|
||||
def _try_load_json(text: str):
|
||||
"""尽量从技能 stdout 解析出 JSON:先整体 loads,失败再截取首个平衡的 {…}/[…]。
|
||||
|
||||
技能脚本的输出常带尾巴(如 ``...}\n\nExit Code: 1``),整体 loads 会失败,故再用
|
||||
「首个左括号 → 最后一个对应右括号」截取主体重试。
|
||||
根因:bash 工具对超过 ~20000 字的输出做**中间截断**(插入「... [middle truncated …] ...」),
|
||||
使整段大 JSON(如 count=30 的技能返回)无法解析;但被截断点**两侧的单条结果对象**通常仍是
|
||||
完整合法的 JSON,可逐个抢救出来。带字符串状态机(正确处理引号内的 {}/转义),逐个返回闭合的
|
||||
对象子串,调用方再逐个 json.loads + 字段识别。
|
||||
"""
|
||||
import json as _json
|
||||
|
||||
text = (text or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
return _json.loads(text)
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
for open_c, close_c in (("{", "}"), ("[", "]")):
|
||||
start = text.find(open_c)
|
||||
end = text.rfind(close_c)
|
||||
if 0 <= start < end:
|
||||
try:
|
||||
return _json.loads(text[start : end + 1])
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
return None
|
||||
out: list = []
|
||||
stack: list[int] = []
|
||||
in_str = False
|
||||
esc = False
|
||||
for i, ch in enumerate(text):
|
||||
if in_str:
|
||||
if esc:
|
||||
esc = False
|
||||
elif ch == "\\":
|
||||
esc = True
|
||||
elif ch == '"':
|
||||
in_str = False
|
||||
continue
|
||||
if ch == '"':
|
||||
in_str = True
|
||||
elif ch == "{":
|
||||
stack.append(i)
|
||||
elif ch == "}":
|
||||
if stack:
|
||||
s = stack.pop()
|
||||
frag = text[s : i + 1]
|
||||
try:
|
||||
parsed = _json.loads(frag)
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
if isinstance(parsed, dict):
|
||||
out.append(parsed)
|
||||
return out
|
||||
|
||||
|
||||
def _extract_result_items(obj) -> list:
|
||||
"""从解析出的 JSON 里取「结果数组」:顶层就是数组则直接用,否则按常见字段名找。"""
|
||||
if isinstance(obj, list):
|
||||
return obj
|
||||
if isinstance(obj, dict):
|
||||
for key in _RESULT_LIST_KEYS:
|
||||
v = obj.get(key)
|
||||
if isinstance(v, list):
|
||||
return v
|
||||
return []
|
||||
def _describe_tool_output(raw: str, skill_name: str) -> str:
|
||||
"""从单次技能输出里提炼一句步骤条文案:**检索词 + 本次命中条数**(用户要求实时显示每次拿到多少条)。
|
||||
|
||||
返回空串 = 这次工具输出不是可识别的检索结果(如 read_file 读 SKILL.md),不发步骤。
|
||||
"""
|
||||
import re as _re
|
||||
|
||||
def _pick(d: dict, keys: tuple) -> str:
|
||||
for k in keys:
|
||||
v = d.get(k)
|
||||
if isinstance(v, str) and v.strip():
|
||||
return v.strip()
|
||||
if v not in (None, "") and not isinstance(v, (dict, list)):
|
||||
return str(v).strip()
|
||||
return ""
|
||||
from deerflow.runtime import references as _ref
|
||||
|
||||
if not isinstance(raw, str) or not raw.strip():
|
||||
return ""
|
||||
payload = _ref._parse_json_like(raw)
|
||||
query = ""
|
||||
if isinstance(payload, dict):
|
||||
query = str(payload.get("query") or "").strip()
|
||||
# 明确失败(超时/报错):也给一条「0 条 + 原因」,让用户看到这次没出数据
|
||||
if payload.get("success") is False:
|
||||
err = str(payload.get("error") or "").strip()
|
||||
q = f"「{query}」" if query else ""
|
||||
return f"技能「{skill_name}」检索{q}未获得数据(0 条){(':' + err) if err else ''}"
|
||||
|
||||
count: int | None = None
|
||||
if isinstance(payload, dict) and isinstance(payload.get("count"), int):
|
||||
count = payload["count"]
|
||||
items = _ref._first_list(payload) if payload is not None else []
|
||||
if count is None and items:
|
||||
count = len(items)
|
||||
if count is None:
|
||||
# 整段没解析出来(最常见:bash 输出被中间截断)→ 从头部正则抢救 query/count,
|
||||
# 再退化为「抢救出的完整记录对象」计数。
|
||||
if not query:
|
||||
m = _re.search(r'"query"\s*:\s*"([^"]{1,80})"', raw)
|
||||
query = m.group(1).strip() if m else ""
|
||||
m2 = _re.search(r'"count"\s*:\s*(\d+)', raw)
|
||||
if m2:
|
||||
count = int(m2.group(1))
|
||||
else:
|
||||
salvaged = _scan_balanced_json_objects(raw)
|
||||
n = sum(1 for o in salvaged if o.get("content") or o.get("content_preview") or o.get("title") or o.get("m_title"))
|
||||
count = n if n > 0 else None
|
||||
if count is None:
|
||||
return "" # 非检索类输出,不发步骤
|
||||
q = f"「{query}」" if query else ""
|
||||
return f"技能「{skill_name}」检索{q}获得 {count} 条数据"
|
||||
|
||||
|
||||
def _harvest_tool_results(tool_outputs: list, skill_name: str, limit: int) -> list:
|
||||
"""**直接从技能 stdout 收割真实检索结果**(不依赖模型最后的 JSON 汇总)。
|
||||
|
||||
关键容错:一次写作会对同一技能跑多个检索词,部分检索词可能超时(``success:false``),
|
||||
但成功的那些(``success:true``)拿回的真实数据**必须照单全收**,不能因为别的检索词没出
|
||||
数据就整段丢弃。本函数逐条解析技能输出、跳过 ``success:false`` 的、把结果数组映射成
|
||||
SearchResult,按 url/recUuid/title 去重,截断到 limit。字段名做并集兼容(适配 knowledge-
|
||||
search-v2 的 ``{success,query,count,results:[{recUuid,title,url,content_preview,...}]}``
|
||||
及内网 ES 的 ``{m_title,content,recUuid,...}`` 等多种技能输出)。
|
||||
**复用主智能体「参考文献」模式同一套久经考验的提取逻辑**(`deerflow.runtime.references`):
|
||||
那套逻辑在 ChatPage/AgentChatPage 已 100% 稳定地把技能 JSON 解析成参考文献——它对结果列表
|
||||
字段(results/data/items/documents/...)、单条字段(title/m_title、content/page_content/
|
||||
content_preview/snippet/...、recUuid/uuid/docId/...)都做了超大并集兼容,并尊重每个技能的
|
||||
展示配置。ai 写作直接复用它,确保和参考文献模式「解析能力完全一致」。
|
||||
|
||||
在此基础上再叠加两层本地容错:
|
||||
- 跳过 ``success:false`` 的输出(某检索词超时不影响其它成功的);
|
||||
- 整段 JSON 解析不出结果时(最常见:bash 输出被**中间截断**),退化为「逐个抢救完整的
|
||||
记录对象」(`_scan_balanced_json_objects`),截断点两侧的单条记录仍能救回。
|
||||
"""
|
||||
from ..search.base import SearchResult
|
||||
from deerflow.runtime import references as _ref
|
||||
|
||||
tool_context = {"skill_name": skill_name, "tool_name": skill_name}
|
||||
harvested: list = []
|
||||
seen: set[str] = set()
|
||||
for raw in tool_outputs:
|
||||
obj = _try_load_json(raw)
|
||||
if obj is None:
|
||||
if not isinstance(raw, str):
|
||||
continue
|
||||
payload = _ref._parse_json_like(raw)
|
||||
# 明确标记失败的(超时 / 报错)跳过,但不影响其它成功的输出
|
||||
if isinstance(obj, dict) and obj.get("success") is False:
|
||||
if isinstance(payload, dict) and payload.get("success") is False:
|
||||
continue
|
||||
for it in _extract_result_items(obj):
|
||||
items = _ref._first_list(payload) if payload is not None else []
|
||||
if not items:
|
||||
# 整段解析不出结果(bash 中间截断把大 JSON 剪断)→ 逐个抢救完整的记录对象
|
||||
items = _scan_balanced_json_objects(raw)
|
||||
for idx, it in enumerate(items):
|
||||
if not isinstance(it, dict):
|
||||
continue
|
||||
# 内网 ES 风格:真实字段藏在 metadata 里
|
||||
md = it.get("metadata") if isinstance(it.get("metadata"), dict) else {}
|
||||
merged = {**md, **it} if md else it
|
||||
content = _pick(merged, _CONTENT_KEYS)
|
||||
title = _pick(merged, _TITLE_KEYS)
|
||||
# 用参考文献模式的归一化(含字段并集 + 每技能展示配置),与右侧参考文献完全一致
|
||||
norm = _ref._normalize_reference_item(
|
||||
it, len(harvested) + 1, payload=payload, tool_context=tool_context
|
||||
)
|
||||
if not norm:
|
||||
continue
|
||||
content = str(norm.get("content") or norm.get("snippet") or "").strip()
|
||||
title = str(norm.get("title") or "").strip()
|
||||
if not content and not title:
|
||||
continue
|
||||
url = _pick(merged, ("url",))
|
||||
rec = _pick(merged, ("recUuid", "rec_uuid", "id"))
|
||||
raw_rec = norm.get("raw") if isinstance(norm.get("raw"), dict) else {}
|
||||
rec = str(raw_rec.get("recUuid") or raw_rec.get("uuid") or raw_rec.get("docId") or "").strip()
|
||||
url = str(norm.get("url") or "").strip()
|
||||
key = url or rec or title
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
try:
|
||||
score = float(merged.get("relevance_score") or 0.0)
|
||||
score = float(norm.get("score"))
|
||||
except (TypeError, ValueError):
|
||||
score = 0.0
|
||||
score = 0.75
|
||||
harvested.append(SearchResult(
|
||||
title=title or "(无标题)",
|
||||
content=content[:500],
|
||||
source=_pick(merged, _SOURCE_KEYS) or skill_name,
|
||||
published_at=_pick(merged, _DATE_KEYS),
|
||||
source=str(norm.get("source") or skill_name).strip() or skill_name,
|
||||
published_at=str(norm.get("time") or "").strip(),
|
||||
url=url,
|
||||
relevance_score=score or 0.75,
|
||||
source_type="skill",
|
||||
@@ -304,7 +348,10 @@ async def run_skill_via_real_agent(
|
||||
logger.warning("skill real-agent[%s]: init failed: %s", skill_name, e)
|
||||
return [], f"运行时初始化失败: {e}"
|
||||
|
||||
run_config: dict = {"recursion_limit": _REAL_AGENT_MAX_TURNS}
|
||||
# recursion_limit 计的是 graph 超级步(每个「模型→工具」回合≈2 步),不能直接等于 max_turns,
|
||||
# 否则模型做了 N 个检索词后还没来得及写最终汇总就被 GraphRecursionError 截断(实测 10 个
|
||||
# 检索词≈20 步 > 16,结果汇总丢失)。给到 ~2×turns + 余量,让模型正常情况下能写完汇总。
|
||||
run_config: dict = {"recursion_limit": _REAL_AGENT_MAX_TURNS * 2 + 4}
|
||||
run_context: dict = {}
|
||||
if thread_id:
|
||||
run_config["configurable"] = {"thread_id": thread_id}
|
||||
@@ -330,6 +377,7 @@ async def run_skill_via_real_agent(
|
||||
|
||||
async def _drive() -> None:
|
||||
nonlocal final_text
|
||||
reached_limit = False
|
||||
try:
|
||||
async for chunk in agent.astream(
|
||||
init_state, config=run_config, context=run_context, stream_mode="values"
|
||||
@@ -352,6 +400,18 @@ async def run_skill_via_real_agent(
|
||||
out = _content_to_text(m.content)
|
||||
if out.strip():
|
||||
tool_outputs.append(out)
|
||||
# 实时步骤条:这次检索拿到多少条数据(含失败/截断的诚实计数)
|
||||
desc = _describe_tool_output(out, skill_name)
|
||||
if desc:
|
||||
step_queue.put_nowait(desc)
|
||||
# **早停**:已收割素材达上限就停止驱动模型——否则弱模型会无限换检索词
|
||||
# (步骤条爆 100+ 条、素材远超上限)。停掉嵌套 astream 即不再检索。
|
||||
if max_results and len(_harvest_tool_results(tool_outputs, skill_name, max_results)) >= max_results:
|
||||
step_queue.put_nowait(f"已获取 {max_results} 条素材,达到上限,停止本技能检索")
|
||||
reached_limit = True
|
||||
break
|
||||
if reached_limit:
|
||||
break
|
||||
finally:
|
||||
step_queue.put_nowait(None) # sentinel:跑完/出错都让 pump 收尾
|
||||
|
||||
@@ -365,10 +425,62 @@ async def run_skill_via_real_agent(
|
||||
if emit_query:
|
||||
emit_query(item) # 本协程上下文调用 writer → custom 事件实时刷给前端
|
||||
|
||||
def _finalize() -> list:
|
||||
"""把「模型最终汇总的 materials」与「技能 stdout 直接收割的结果」合并去重。
|
||||
|
||||
**关键**:无论本次运行是正常结束、超时、还是抛异常,只要已经收到过技能的工具输出,
|
||||
就必须把这些真实结果收割下来——绝不能因为模型还没来得及汇总、或整段被切断,就把已经
|
||||
拿到的数据整段丢掉(用户实测:技能明明检到了 30+ 条,却因没汇总而全丢)。
|
||||
"""
|
||||
# 1) 模型最终汇总的 materials(它可能已按相关度筛选/去重)
|
||||
model_results: list = []
|
||||
parsed = parse_json(final_text or "", {"materials": []})
|
||||
raw_materials = parsed.get("materials")
|
||||
if isinstance(raw_materials, list):
|
||||
for m in raw_materials:
|
||||
if not isinstance(m, dict):
|
||||
continue
|
||||
content = str(m.get("content") or "").strip()
|
||||
title = str(m.get("title") or "").strip()
|
||||
if not content and not title:
|
||||
continue
|
||||
model_results.append(SearchResult(
|
||||
title=title or "(无标题)",
|
||||
content=content[:500],
|
||||
source=str(m.get("source") or skill_name).strip() or skill_name,
|
||||
published_at=str(m.get("published_at") or "").strip(),
|
||||
url=str(m.get("url") or "").strip(),
|
||||
relevance_score=0.8,
|
||||
source_type="skill",
|
||||
))
|
||||
|
||||
# 2) **直接从技能 stdout 收割真实检索结果**——哪怕模型最后没汇总、或别的检索词超时,
|
||||
# 成功检索词拿回的数据也照单全收。
|
||||
harvested = _harvest_tool_results(tool_outputs, skill_name, max_results)
|
||||
|
||||
# 3) 合并去重(模型汇总优先、技能原始结果补齐),按 url/recUuid/title 去重,截断到上限。
|
||||
merged: list = []
|
||||
seen_keys: set[str] = set()
|
||||
for r in model_results + harvested:
|
||||
key = (r.url or "").strip() or (r.recUuid or "").strip() or (r.title or "").strip()
|
||||
if key and key in seen_keys:
|
||||
continue
|
||||
if key:
|
||||
seen_keys.add(key)
|
||||
merged.append(r)
|
||||
if len(merged) >= max_results:
|
||||
break
|
||||
return merged
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(_pump(), timeout=max(timeout_s, _REAL_AGENT_TIMEOUT_SECONDS))
|
||||
except asyncio.TimeoutError:
|
||||
task.cancel()
|
||||
# 超时也要收割已拿到的技能数据:有就返回(无错误),实在没有才报超时。
|
||||
salvaged = _finalize()
|
||||
if salvaged:
|
||||
logger.info("skill real-agent[%s]: timed out but salvaged %d results", skill_name, len(salvaged))
|
||||
return salvaged, ""
|
||||
return [], f"技能检索超时(>{max(timeout_s, _REAL_AGENT_TIMEOUT_SECONDS)}s)"
|
||||
|
||||
# pump 正常结束(收到 sentinel)后等后台任务收尾,拿 final_text / 抛出的异常
|
||||
@@ -376,47 +488,14 @@ async def run_skill_via_real_agent(
|
||||
await task
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("skill real-agent[%s]: astream failed: %s", skill_name, e, exc_info=True)
|
||||
# 递归上限 / 中途异常同样要收割已拿到的技能数据,不整段丢。
|
||||
salvaged = _finalize()
|
||||
if salvaged:
|
||||
logger.info("skill real-agent[%s]: errored but salvaged %d results", skill_name, len(salvaged))
|
||||
return salvaged, ""
|
||||
return [], f"技能检索失败: {e}"
|
||||
|
||||
# 1) 模型最终汇总的 materials(它可能已按相关度筛选/去重)
|
||||
model_results: list = []
|
||||
parsed = parse_json(final_text or "", {"materials": []})
|
||||
raw_materials = parsed.get("materials")
|
||||
if isinstance(raw_materials, list):
|
||||
for m in raw_materials:
|
||||
if not isinstance(m, dict):
|
||||
continue
|
||||
content = str(m.get("content") or "").strip()
|
||||
title = str(m.get("title") or "").strip()
|
||||
if not content and not title:
|
||||
continue
|
||||
model_results.append(SearchResult(
|
||||
title=title or "(无标题)",
|
||||
content=content[:500],
|
||||
source=str(m.get("source") or skill_name).strip() or skill_name,
|
||||
published_at=str(m.get("published_at") or "").strip(),
|
||||
url=str(m.get("url") or "").strip(),
|
||||
relevance_score=0.8,
|
||||
source_type="skill",
|
||||
))
|
||||
|
||||
# 2) **直接从技能 stdout 收割真实检索结果**——哪怕模型最后没汇总、或别的检索词超时,
|
||||
# 成功检索词拿回的数据也照单全收(用户诉求:拿到的数据必须接收,不能整段丢)。
|
||||
harvested = _harvest_tool_results(tool_outputs, skill_name, max_results)
|
||||
|
||||
# 3) 合并去重(模型汇总优先、技能原始结果补齐),按 url/recUuid/title 去重,截断到上限。
|
||||
merged: list = []
|
||||
seen_keys: set[str] = set()
|
||||
for r in model_results + harvested:
|
||||
key = (r.url or "").strip() or (r.recUuid or "").strip() or (r.title or "").strip()
|
||||
if key and key in seen_keys:
|
||||
continue
|
||||
if key:
|
||||
seen_keys.add(key)
|
||||
merged.append(r)
|
||||
if len(merged) >= max_results:
|
||||
break
|
||||
return merged, ""
|
||||
return _finalize(), ""
|
||||
|
||||
|
||||
def _read_skill_body(skill) -> str:
|
||||
|
||||
+73
-30
@@ -767,6 +767,60 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
# 早停上限:通常为 max_materials;用户显式要「再追加 N 条」时放宽到 N,确保凑够
|
||||
collect_limit = max(max_materials, requested_append_count or 0)
|
||||
|
||||
# ── 内网检索接口:与技能检索**并行**预跑 ──
|
||||
# 用户诉求:调用技能的同时就把内网检索接口跑起来;技能没拿到结果时,直接用这里
|
||||
# 预跑好的内网结果兜底(省去"技能全失败后再串行等一遍内网"的时间)。需在 config.yaml
|
||||
# 的 ai_writing.intranet_search_url 配地址才启用;技能拿到结果则取消这个任务、不浪费。
|
||||
intranet_url = (ai_writing_config.get("intranet_search_url") or "").strip()
|
||||
intranet_task: "asyncio.Task | None" = None
|
||||
intranet_holder: dict = {}
|
||||
|
||||
async def _run_intranet_search() -> list[SearchResult]:
|
||||
# 全程吞异常:并行内网任务**绝不**能把主流程带挂(实测内网接口可能瞬间失败)。
|
||||
# 失败只记 search_errors + 日志,返回空列表,交由技能结果或空结果文案兜底。
|
||||
try:
|
||||
from ..search.intranet_stub import IntranetSearchProvider
|
||||
|
||||
intranet = IntranetSearchProvider(
|
||||
base_url=intranet_url,
|
||||
knowledge_base_name=ai_writing_config.get("intranet_knowledge_base", "samples"),
|
||||
# 兜底接口超时可在 config.yaml 的 ai_writing.intranet_search_timeout 调大,
|
||||
# 默认 600s(10 分钟),给内网慢检索留足时间。
|
||||
timeout=ai_writing_config.get("intranet_search_timeout", 600),
|
||||
)
|
||||
intranet_holder["provider"] = intranet
|
||||
fb_keywords = keywords[:keyword_count] or [intent]
|
||||
gathered = await asyncio.gather(
|
||||
*[intranet.search(kw, max_results=per_skill) for kw in fb_keywords],
|
||||
return_exceptions=True,
|
||||
)
|
||||
collected: list[SearchResult] = []
|
||||
for res in gathered:
|
||||
if isinstance(res, Exception):
|
||||
logger.warning("[intranet-parallel] one keyword failed: %s", res)
|
||||
search_errors.append(f"{type(res).__name__}: {res}")
|
||||
continue
|
||||
collected.extend(res or [])
|
||||
return collected
|
||||
except asyncio.CancelledError:
|
||||
raise # 取消要正常传播(技能已拿到结果时主动取消)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("[intranet-parallel] intranet fallback crashed: %s", e, exc_info=True)
|
||||
search_errors.append(f"内网兜底异常:{type(e).__name__}: {e}")
|
||||
return []
|
||||
|
||||
if intranet_url:
|
||||
intranet_task = asyncio.create_task(_run_intranet_search())
|
||||
|
||||
async def _cancel_intranet_task() -> None:
|
||||
"""技能已拿到素材:取消并行的内网检索任务,避免无谓占用 / 未取回异常告警。"""
|
||||
if intranet_task is not None and not intranet_task.done():
|
||||
intranet_task.cancel()
|
||||
try:
|
||||
await intranet_task
|
||||
except BaseException: # noqa: BLE001 — CancelledError / 任务内异常都吞掉
|
||||
pass
|
||||
|
||||
for sk in ordered:
|
||||
added, error_detail = await _execute_skill(sk)
|
||||
tried_names.add(sk.name)
|
||||
@@ -802,45 +856,34 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
# 兜底已拿到素材即止,不再无谓尝试后续技能
|
||||
break
|
||||
|
||||
# ── 最终兜底:技能(含兜底技能)都没检索到 → 直连内网知识库接口(ES)兜一把 ──
|
||||
# 用户诉求:技能因网络/配置连不上而 0 条时,至少用内网 ES 接口直检,避免素材全空。
|
||||
# 需在 config.yaml 的 ai_writing 段配 intranet_search_url 才启用;接口契约见
|
||||
# IntranetSearchProvider(POST {query,knowledge_base_name,ts} → [{page_content,metadata}])。
|
||||
# ── 最终兜底:技能(含兜底技能)都没检索到 → 用并行预跑的内网知识库接口结果兜底 ──
|
||||
# 用户诉求:调用技能时已同时跑了内网检索接口;技能因网络/配置连不上而 0 条时,直接用
|
||||
# 那份并行结果,避免素材全空、也省去串行等待。需在 config.yaml 配 intranet_search_url。
|
||||
if not new_results:
|
||||
intranet_url = (ai_writing_config.get("intranet_search_url") or "").strip()
|
||||
if intranet_url:
|
||||
from ..search.intranet_stub import IntranetSearchProvider
|
||||
|
||||
if intranet_task is not None:
|
||||
_emit_skill_step({
|
||||
"type": "fallback",
|
||||
"message": "技能均未检索到素材,改为直连内网知识库接口兜底检索…",
|
||||
"message": "技能均未检索到素材,使用并行预跑的内网知识库接口结果兜底…",
|
||||
})
|
||||
intranet = IntranetSearchProvider(
|
||||
base_url=intranet_url,
|
||||
knowledge_base_name=ai_writing_config.get("intranet_knowledge_base", "samples"),
|
||||
# 兜底接口超时可在 config.yaml 的 ai_writing.intranet_search_timeout 调大,
|
||||
# 默认 600s(10 分钟),给内网慢检索留足时间。
|
||||
timeout=ai_writing_config.get("intranet_search_timeout", 600),
|
||||
)
|
||||
fb_keywords = keywords[:keyword_count] or [intent]
|
||||
fb_gathered = await asyncio.gather(
|
||||
*[intranet.search(kw, max_results=per_skill) for kw in fb_keywords],
|
||||
return_exceptions=True,
|
||||
)
|
||||
fb_added = 0
|
||||
for fb_results in fb_gathered:
|
||||
if isinstance(fb_results, Exception):
|
||||
search_errors.append(f"{type(fb_results).__name__}: {fb_results}")
|
||||
continue
|
||||
fb_added += _collect(fb_results)
|
||||
search_errors.extend(intranet.errors)
|
||||
# 让下方 notice 拿到内网接口的原始响应体(解析失败时便于定位)
|
||||
provider = intranet
|
||||
try:
|
||||
fb_results = await intranet_task
|
||||
except BaseException as e: # noqa: BLE001 — 任务内异常/取消都按"无结果"处理
|
||||
fb_results = []
|
||||
search_errors.append(f"{type(e).__name__}: {e}")
|
||||
fb_added = _collect(fb_results)
|
||||
intranet = intranet_holder.get("provider")
|
||||
if intranet is not None:
|
||||
search_errors.extend(getattr(intranet, "errors", []))
|
||||
# 让下方 notice 拿到内网接口的原始响应体(解析失败时便于定位)
|
||||
provider = intranet
|
||||
_emit_skill_step({
|
||||
"type": "skill_result",
|
||||
"message": f"内网知识库接口兜底检索到 {fb_added} 条新素材",
|
||||
"matched_count": fb_added,
|
||||
})
|
||||
else:
|
||||
# 技能已拿到素材:取消并行的内网检索,不再浪费内网接口调用
|
||||
await _cancel_intranet_task()
|
||||
|
||||
# 合并去重
|
||||
# - 首次检索 / 普通 re_search(无输入,用户只想重出):旧素材丢弃,覆盖式替换
|
||||
|
||||
Reference in new issue
Block a user