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
+95
-9
@@ -115,6 +115,31 @@ def _fallback_knowledge_skill() -> SimpleNamespace:
|
||||
return SimpleNamespace(name=KNOWLEDGE_SEARCH_SKILL_NAME, description=_KNOWLEDGE_SKILL_FALLBACK_DESC)
|
||||
|
||||
|
||||
def _unique_limited(values: list, limit: int) -> list:
|
||||
"""Stable de-dup for short diagnostic lists."""
|
||||
return list(dict.fromkeys(values or []))[:limit]
|
||||
|
||||
|
||||
def _intranet_query_summary(rows: list[dict], limit: int = 5) -> str:
|
||||
"""Compact per-query stats for the visible skill step message."""
|
||||
parts: list[str] = []
|
||||
for row in rows[:limit]:
|
||||
query = str(row.get("query") or "")[:24] or "(空)"
|
||||
elapsed_ms = row.get("elapsed_ms")
|
||||
try:
|
||||
elapsed = f"{float(elapsed_ms) / 1000:.1f}s" if elapsed_ms is not None else ""
|
||||
except (TypeError, ValueError):
|
||||
elapsed = ""
|
||||
if row.get("error"):
|
||||
reason = str(row.get("error"))[:80]
|
||||
parts.append(f"{query}: 失败{f'/{elapsed}' if elapsed else ''}({reason})")
|
||||
else:
|
||||
parts.append(f"{query}: {int(row.get('count') or 0)} 条{f'/{elapsed}' if elapsed else ''}")
|
||||
if len(rows) > limit:
|
||||
parts.append(f"另 {len(rows) - limit} 个方向")
|
||||
return ";".join(parts)
|
||||
|
||||
|
||||
def _load_configured_skills() -> list:
|
||||
"""解析素材收集专家 agent(ai-writing-researcher)配置的技能列表。
|
||||
|
||||
@@ -594,6 +619,20 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
timeout=_intranet_timeout_seconds(ai_writing_config),
|
||||
)
|
||||
endpoint_label = intranet_url or getattr(provider, "endpoint", "内置默认内网端点")
|
||||
intranet_debug = {
|
||||
"mode": "knowledge_base",
|
||||
"configured": bool(intranet_url),
|
||||
"endpoint": endpoint_label,
|
||||
"knowledge_base": ai_writing_config.get("intranet_knowledge_base", "samples"),
|
||||
"timeout": _intranet_timeout_seconds(ai_writing_config),
|
||||
"status": "running",
|
||||
"queries": [],
|
||||
"calls": [],
|
||||
"errors": [],
|
||||
"raw_responses": [],
|
||||
"result_count": 0,
|
||||
"used_as_fallback": False,
|
||||
}
|
||||
# 用户输入框追加(is_user_query_search)时 keywords 已是 [用户原话],全部检索;
|
||||
# 首次检索按 keyword_count 截断检索词。
|
||||
search_keywords = keywords if is_user_query_search else keywords[:keyword_count]
|
||||
@@ -605,20 +644,65 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
"message": f"使用「{_KNOWLEDGE_SKILL_DISPLAY}」直连内网接口({endpoint_label})检索 {total} 个方向:{('、'.join(search_keywords[:3]))[:60]}",
|
||||
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
|
||||
})
|
||||
|
||||
async def _search_one_keyword(kw: str) -> tuple[str, list[SearchResult], str, int]:
|
||||
started = time.monotonic()
|
||||
try:
|
||||
results = await provider.search(kw, max_results=per_keyword)
|
||||
return kw, list(results or []), "", int((time.monotonic() - started) * 1000)
|
||||
except Exception as e: # noqa: BLE001 — provider 理论上会吞异常,这里兜住意外冒泡
|
||||
logger.warning("[knowledge-base] keyword=%r search failed: %s", kw, e)
|
||||
return kw, [], f"{type(e).__name__}: {e}", int((time.monotonic() - started) * 1000)
|
||||
|
||||
gathered = await asyncio.gather(
|
||||
*[provider.search(kw, max_results=per_keyword) for kw in search_keywords],
|
||||
*[_search_one_keyword(kw) 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}")
|
||||
for item in gathered:
|
||||
if isinstance(item, Exception):
|
||||
logger.warning("[knowledge-base] search task failed: %s", item)
|
||||
message = f"{type(item).__name__}: {item}"
|
||||
search_errors.append(message)
|
||||
intranet_debug["queries"].append({"query": "(unknown)", "count": 0, "error": message})
|
||||
continue
|
||||
kw, results, error, elapsed_ms = item
|
||||
row = {"query": kw, "count": len(results or []), "elapsed_ms": elapsed_ms}
|
||||
if error:
|
||||
row["error"] = error
|
||||
search_errors.append(error)
|
||||
intranet_debug["queries"].append(row)
|
||||
new_results.extend(results or [])
|
||||
search_errors.extend(getattr(provider, "errors", []))
|
||||
provider_errors = _unique_limited(getattr(provider, "errors", []) or [], 5)
|
||||
search_errors.extend(provider_errors)
|
||||
intranet_debug["errors"] = provider_errors
|
||||
intranet_debug["raw_responses"] = _unique_limited(getattr(provider, "raw_responses", []) or [], 3)
|
||||
intranet_debug["calls"] = list(getattr(provider, "calls", []) or [])[:20]
|
||||
intranet_debug["result_count"] = len(new_results)
|
||||
if search_errors and new_results:
|
||||
intranet_debug["status"] = "partial"
|
||||
elif search_errors:
|
||||
intranet_debug["status"] = "error"
|
||||
elif new_results:
|
||||
intranet_debug["status"] = "ok"
|
||||
else:
|
||||
intranet_debug["status"] = "empty"
|
||||
|
||||
query_summary = _intranet_query_summary(intranet_debug["queries"])
|
||||
if search_errors:
|
||||
uniq_errors = _unique_limited(search_errors, 3)
|
||||
_emit_skill_step({
|
||||
"type": "skill_error",
|
||||
"message": f"「{_KNOWLEDGE_SKILL_DISPLAY}」内网接口调用异常:{';'.join(uniq_errors)}",
|
||||
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
|
||||
"reason": ";".join(uniq_errors),
|
||||
})
|
||||
result_message = (
|
||||
f"「{_KNOWLEDGE_SKILL_DISPLAY}」(内网接口)调用完成,检索到 {len(new_results)} 条素材"
|
||||
+ (f"({query_summary})" if query_summary else "")
|
||||
)
|
||||
_emit_skill_step({
|
||||
"type": "skill_result",
|
||||
"message": f"「{_KNOWLEDGE_SKILL_DISPLAY}」(内网接口)检索到 {len(new_results)} 条素材",
|
||||
"message": result_message,
|
||||
"skill_name": KNOWLEDGE_SEARCH_SKILL_NAME,
|
||||
"matched_count": len(new_results),
|
||||
})
|
||||
@@ -846,6 +930,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
"debug_force": intranet_debug_force,
|
||||
"status": "pending" if intranet_url else "not_configured",
|
||||
"queries": [], # 每个检索词:{query, count, error?}
|
||||
"calls": [], # provider 每次真实 HTTP 调用,含简化词重试
|
||||
"errors": [], # provider 聚合的报错
|
||||
"raw_responses": [], # 解析失败时的原始响应片段
|
||||
"result_count": 0, # 内网总命中条数
|
||||
@@ -880,8 +965,9 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
continue
|
||||
intranet_debug["queries"].append({"query": kw, "count": len(res or [])})
|
||||
collected.extend(res or [])
|
||||
intranet_debug["errors"] = list(dict.fromkeys(getattr(intranet, "errors", [])))[:5]
|
||||
intranet_debug["raw_responses"] = list(dict.fromkeys(getattr(intranet, "raw_responses", [])))[:3]
|
||||
intranet_debug["errors"] = _unique_limited(getattr(intranet, "errors", []) or [], 5)
|
||||
intranet_debug["raw_responses"] = _unique_limited(getattr(intranet, "raw_responses", []) or [], 3)
|
||||
intranet_debug["calls"] = list(getattr(intranet, "calls", []) or [])[:20]
|
||||
intranet_debug["result_count"] = len(collected)
|
||||
intranet_debug["status"] = "ran"
|
||||
return collected
|
||||
|
||||
Reference in new issue
Block a user