feat: refine ai writing search flow

This commit is contained in:
董宏凯 committed 2026-07-01 18:00:14 +08:00
1 parent 025e43f4c5
commit d21dbeb214
15 files changed
+260 -113

No files matched your search

@@ -204,6 +204,37 @@ async def intent_parser_node(state: AIWritingState, config: RunnableConfig) -> d
if not isinstance(preconfigured_wc, int) or preconfigured_wc <= 0:
preconfigured_wc = 800
if state.get("skip_intent_parser"):
writer = safe_get_stream_writer()
final_article_type, final_target_audience, final_word_count, final_refined_intent = _resolve_intent_fields(
state, fallback_result, fallback_result, valid_keys, default_key
)
writer({
"type": "intent_ready",
"article_type": final_article_type,
"target_audience": final_target_audience,
"word_count_target": final_word_count,
"refined_intent": final_refined_intent,
})
progress_events = [{
"type": "step_done",
"agent_name": "系统",
"message": "意图解析完成(已使用已确认配置)",
}]
updates: dict = {
"status": "researching",
"progress_events": progress_events,
"article_type": final_article_type,
"target_audience": final_target_audience,
"word_count_target": final_word_count,
}
persisted_model = (configurable.get("model_name") or "").strip()
if persisted_model:
updates["model_name"] = persisted_model
if final_refined_intent != state["user_intent"]:
updates["user_intent"] = final_refined_intent
return updates
user = _USER_TEMPLATE.format(
user_intent=state["user_intent"],
preconfigured_article_type=fallback_type,
@@ -428,8 +428,11 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
# 用户在写作表单选择的检索类型:general(通用检索,按 agent 配置技能编排)/
# knowledge_base(知识库检索,直连内网 ES 接口)/ notebook(我的空间)。
# 旧值 skill 一律按 general 兼容。
raw_source = (state.get("material_source") or "general").strip()
raw_source = (intervention.get("material_source") or state.get("material_source") or "general").strip()
material_source = raw_source if raw_source in ("notebook", "knowledge_base") else "general"
notebook_search_space_id: int | None = state.get("notebook_search_space_id")
if material_source == "notebook" and not notebook_search_space_id:
material_source = "general"
# 用户指定的检索关键词数量(1~10),决定生成多少个检索词、检索多少个方向
keyword_count = _resolve_keyword_count(state.get("keyword_count"))
@@ -486,7 +489,12 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
# 用户输入分支:把整段自然语言作为唯一检索词,尊重原意,不再做关键词拆分。
# 若用户只说了「再追加 N 条素材」,effective_user_query 已回退为写作意图。
keywords: list[str] = [effective_user_query]
_emit(f"按你的输入「{effective_user_query[:30]}」匹配技能检索…")
if material_source == "knowledge_base":
_emit(f"按你的输入「{effective_user_query[:30]}」进行知识库检索…")
elif material_source == "notebook":
_emit(f"按你的输入「{effective_user_query[:30]}」检索我的空间…")
else:
_emit(f"按你的输入「{effective_user_query[:30]}」匹配技能检索…")
elif is_append_only:
# 兼容旧版追加模式:直接用用户填的关键词,不再调 LLM
keywords = _clean_keywords(extra_keywords, intent)
@@ -530,10 +538,9 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
keywords = list(dict.fromkeys(keywords + extra_keywords))
# Step 1.5: 笔记检索(如果传入了 notebook_search_space_id)
notebook_search_space_id: int | None = state.get("notebook_search_space_id")
notebook_materials: list[Material] = []
if notebook_search_space_id:
if material_source == "notebook" and notebook_search_space_id:
notebook_source_ids: list[int] = state.get("notebook_source_ids") or []
notebook_token: str = cfg.get("notebook_token") or ""
_emit("正在检索笔记内容…")
@@ -1132,7 +1139,12 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
# 进度事件文案:用户输入 / 追加模式下让用户看到差异化提示
if is_user_query_search:
step_start_msg = f"按你的输入匹配技能检索并追加:{user_query[:40]}"
if material_source == "knowledge_base":
step_start_msg = f"按你的输入进行知识库检索并追加:{user_query[:40]}"
elif material_source == "notebook":
step_start_msg = f"按你的输入检索我的空间并追加:{user_query[:40]}"
else:
step_start_msg = f"按你的输入匹配技能检索并追加:{user_query[:40]}"
elif is_append_only:
step_start_msg = f"追加检索关键词:{', '.join(keywords[:3])}"
elif skill_steps:
@@ -186,7 +186,11 @@ async def _stream_sample_analysis(
]
full_text = ""
try:
model = get_model(config, model_name=model_name, disable_thinking=True)
# Keep thinking enabled for the sample analyst. Reasoning models may
# stream it through additional_kwargs.reasoning_content, while some
# local models emit literal <think>...</think> in content; both paths
# are split below just like intent/outline/draft streaming.
model = get_model(config, model_name=model_name, disable_thinking=False)
parser = ThinkTagParser()
async for chunk in model.astream(messages):
reasoning = reasoning_delta(chunk)