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

@@ -58,6 +58,16 @@ async def pause_material_confirm(state: AIWritingState) -> dict:
# 单输入框文本:可能是技能名 / 来源 / 检索意图,re_search 和 enable_skill_search 都用得到
user_query = (user_input.get("userQuery") or user_input.get("user_query") or "").strip()
requested_source = user_input.get("materialSource") or user_input.get("material_source")
fallback_source = state.get("material_source") or "general"
material_source = requested_source if requested_source in ("general", "knowledge_base", "notebook") else fallback_source
if material_source not in ("general", "knowledge_base", "notebook"):
material_source = "general"
source_label = {
"general": "通用检索",
"knowledge_base": "知识库检索",
"notebook": "我的空间",
}.get(material_source, "通用检索")
if action == "confirm":
approved_ids = set(user_input.get("approvedMaterialIds") or [m["id"] for m in pkg["materials"]])
@@ -84,9 +94,10 @@ async def pause_material_confirm(state: AIWritingState) -> dict:
# enable_skill_search:旧版「启用技能检索」按钮的兼容入口——通用检索现在
# 本来就按 agent 配置技能编排,researcher 把它按 user_query 追加检索处理
if action == "enable_skill_search":
msg = f"按输入「{user_query[:30]}」匹配技能检索并追加素材…" if user_query else "重新检索中…"
msg = f"按输入「{user_query[:30]}」使用{source_label}追加素材…" if user_query else f"使用{source_label}重新检索中…"
return {
"last_user_intervention": {**user_input, "action": "enable_skill_search", "user_query": user_query},
"last_user_intervention": {**user_input, "action": "enable_skill_search", "user_query": user_query, "material_source": material_source},
"material_source": material_source,
"status": "researching",
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": msg}],
}
@@ -94,9 +105,10 @@ async def pause_material_confirm(state: AIWritingState) -> dict:
# re_search:带 user_query = 在配置技能中匹配检索并**追加**素材;
# 不带输入 = 重新生成检索词、覆盖式重检(兼容旧版 extraKeywords 数组追加)
extra_kws = user_input.get("extraKeywords") or []
msg = f"按输入「{user_query[:30]}」匹配技能检索并追加素材…" if user_query else "重新搜索中..."
msg = f"按输入「{user_query[:30]}」使用{source_label}追加素材…" if user_query else f"使用{source_label}重新搜索中..."
return {
"last_user_intervention": {**user_input, "_extra_keywords": extra_kws, "user_query": user_query},
"last_user_intervention": {**user_input, "_extra_keywords": extra_kws, "user_query": user_query, "material_source": material_source},
"material_source": material_source,
"status": "researching",
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": msg}],
}
@@ -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)
@@ -118,6 +118,8 @@ class UserInterventionPayload(TypedDict, total=False):
# PAUSE-1 素材确认 / enable_skill_search 共用:用户在单输入框里填的自然语言
# (可能是要使用的技能名、想检索的来源、或者一段补充检索意图)。
user_query: str
# PAUSE-1 素材确认卡切换的检索方式:general / knowledge_base / notebook。
material_source: str
edited_outline: Optional[Outline]
outline_feedback: str
user_revision_notes: str
@@ -146,6 +148,8 @@ class AIWritingState(TypedDict):
# 用户在写作表单粘贴的「已有大纲」原文(可选)。非空时 writer_outline 跳过 LLM 规划,
# 直接解析这份大纲并按用户结构生成,做到「完全按我的大纲写」。留空走正常自动规划。
user_outline_raw: str
# True when setup chat has already produced structured intent fields.
skip_intent_parser: bool
# 写作入口模式:normal=普通写作 / imitate=样文仿写。仿写模式下先分析 sample_text,
# 得到 sample_style_profile,再将其注入大纲与草稿提示词。
writing_mode_type: str