feat(ai写作): 自带大纲/会商及研究模板等前后端改动

提交工作区中 AI 写作(表单/时间线/历史/上下文/流式 hook/类型)、
研究模板配置页、lead_agent、researcher/graph/state 及 gateway/config
的进行中改动。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
fengxiangdebaoziandClaude Opus 4.8 committed 2026-06-24 16:15:39 +08:00
1 parent d2c76100f2
commit e9c8d1cea2
18 files changed
+287 -50

No files matched your search

@@ -62,6 +62,17 @@ async def pause_material_confirm(state: AIWritingState) -> dict:
approved_ids = set(user_input.get("approvedMaterialIds") or [m["id"] for m in pkg["materials"]])
filtered = [m for m in pkg["materials"] if m["id"] in approved_ids]
updated_pkg = {**pkg, "materials": filtered}
# 用户自带大纲:researcher 已在「搜集素材」阶段解析大纲并归类素材 → 跳过大纲生成步,
# 仅把被用户取消勾选的素材从各章节 material_ids 里剔除,直接进入大纲确认。
prebuilt = state.get("current_outline")
if prebuilt and (state.get("user_outline_raw") or "").strip():
return {
"material_package": updated_pkg,
"current_outline": _prune_outline_material_ids(prebuilt, approved_ids),
"last_user_intervention": user_input,
"status": "awaiting_outline_confirm",
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "已采用你提供的大纲并归类素材,请确认大纲"}],
}
return {
"material_package": updated_pkg,
"last_user_intervention": user_input,
@@ -90,6 +101,21 @@ async def pause_material_confirm(state: AIWritingState) -> dict:
}
def _prune_outline_material_ids(outline: dict, approved_ids: set) -> dict:
"""把大纲各章节 material_ids 里、用户在素材确认卡取消勾选的素材编号剔除掉。
用户自带大纲时素材归类发生在「搜集素材」阶段(researcher),早于素材确认;
用户在确认卡里去掉某些素材后,用这个函数把对应编号从大纲剔除,保持一致。
"""
if not outline:
return outline
sections = []
for s in outline.get("sections", []):
kept = [mid for mid in (s.get("material_ids") or []) if mid in approved_ids]
sections.append({**s, "material_ids": kept})
return {**outline, "sections": sections}
def _normalize_outline(outline: dict) -> dict:
"""将前端回传的 camelCase 大纲统一为后端 snake_case 格式。"""
if not outline:
@@ -318,7 +344,13 @@ def after_researcher(state: AIWritingState) -> str:
def after_material_pause(state: AIWritingState) -> str:
return "researcher" if state["status"] == "researching" else "writer_outline"
status = state["status"]
if status == "researching":
return "researcher"
# 用户自带大纲:researcher 已在搜集阶段整理好大纲 → 跳过 writer_outline 生成步,直接确认大纲。
if status == "awaiting_outline_confirm":
return "pause_outline"
return "writer_outline"
def after_outline_pause(state: AIWritingState) -> str:
@@ -396,9 +428,9 @@ def make_ai_writing_graph(config=None):
g.set_entry_point("intent_parser")
g.add_edge("intent_parser", "researcher")
g.add_conditional_edges("researcher", after_researcher,
{"pause_material": "pause_material", "writer_draft": "writer_draft"})
{"pause_material": "pause_material", "writer_draft": "writer_draft", "writer_outline": "writer_outline"})
g.add_conditional_edges("pause_material", after_material_pause,
{"researcher": "researcher", "writer_outline": "writer_outline"})
{"researcher": "researcher", "writer_outline": "writer_outline", "pause_outline": "pause_outline"})
g.add_edge("writer_outline", "pause_outline")
g.add_conditional_edges("pause_outline", after_outline_pause,
{"writer_outline": "writer_outline", "writer_draft": "writer_draft"})
@@ -399,6 +399,22 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
def _emit(message: str) -> None:
writer({"type": "research_progress", "message": message})
# 跳过素材收集:用户在初始表单勾选「不收集素材,直接用通用知识写作」。
# 仅在**首次**进入(无任何 pause 干预 = intervention 为空)时短路,避免后续
# 「补充检索 / 按输入追加」等再入研究节点时被误跳过。直接产出空素材包、强制
# loose 写作模式(无素材可严格依据),并跳过素材确认暂停点直奔大纲规划。
if state.get("skip_research") and not intervention:
empty_pkg = MaterialPackage(keywords=[], materials=[], summary="用户选择不收集素材,直接用通用知识写作")
return {
"material_package": empty_pkg,
"writing_mode": "loose",
"status": "writing_outline",
"progress_events": [
{"type": "step_start", "agent_name": researcher_label, "message": "已跳过素材收集,直接用通用知识写作"},
{"type": "materials_ready", "agent_name": researcher_label, "message": "未收集素材(通用知识写作)"},
],
}
# Step 1: 提取关键词
kw_result: dict = {} # 走"用户输入 / 追加"分支时跳过 LLM 调用,notice 拼接需要兜底值
if is_user_query_search:
@@ -856,13 +872,36 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
step_start_msg = f"通用检索(技能编排 {len(skill_steps)} 步):{', '.join(keywords[:3])}"
else:
step_start_msg = f"搜索关键词:{', '.join(keywords[:3])}"
progress_events: list[dict] = [
{"type": "step_start", "agent_name": researcher_label, "message": step_start_msg},
*notices,
{"type": "materials_ready", "agent_name": researcher_label, "message": summary},
]
extra_updates: dict = {}
# 用户自带大纲:在「搜集素材」阶段顺便解析大纲并把素材归类到各章节,
# 从而跳过 writer_outline 的大纲生成步(见 graph after_material_pause 路由)。
# 补充检索(is_supplement)直接回写作、沿用原大纲,不在此整理。
if not is_supplement and (state.get("user_outline_raw") or "").strip():
from .writer_outline import build_user_outline_with_materials
try:
organized = await build_user_outline_with_materials(state, config, pkg)
except Exception:
logger.warning("researcher: 整理用户大纲失败(非致命),回退到大纲生成步", exc_info=True)
organized = None
if organized:
extra_updates["current_outline"] = organized
progress_events.append({
"type": "outline_ready",
"agent_name": researcher_label,
"message": f"已按你提供的大纲整理素材,共 {len(organized.get('sections', []))} 个章节",
})
return {
"material_package": pkg,
# 补充检索:直接回写作(沿用原大纲);常规检索:进入素材确认暂停点
"status": "writing_draft" if is_supplement else "awaiting_material_confirm",
"progress_events": [
{"type": "step_start", "agent_name": researcher_label, "message": step_start_msg},
*notices,
{"type": "materials_ready", "agent_name": researcher_label, "message": summary},
],
**extra_updates,
"progress_events": progress_events,
}
@@ -144,6 +144,10 @@ class AIWritingState(TypedDict):
# notebook=我的空间(仅笔记检索)。旧值 knowledge_base / skill 由 researcher
# 节点按 general 兼容处理。
material_source: str
# 跳过素材收集:用户在初始表单勾选「不收集素材,直接用通用知识写作」时为 True。
# researcher 节点在**首次**进入(无任何 pause 干预)时短路,跳过整个检索流程直接
# 去大纲规划;强制 loose 写作模式(没有素材可严格依据)。
skip_research: bool
# 用户在写作表单选定的主模型。大纲/写作/审核严格使用它(不回退默认)。
# 由 intent_parser 在首次 invocation 从 configurable.model_name 捕获进 state,
# 经 checkpoint 持久化 —— interrupt/resume 是新 invocation、其 config 不带原