初始化
This commit is contained in:
1 parent
34474bd5fc
commit
b638b541d0
3008 files changed
+837699
No files matched your search
+3
@@ -0,0 +1,3 @@
|
||||
from .graph import make_ai_writing_graph
|
||||
|
||||
__all__ = ["make_ai_writing_graph"]
|
||||
+463
@@ -0,0 +1,463 @@
|
||||
"""AI 写作 LangGraph Graph,含 4 个用户干预点。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langgraph.graph import END, StateGraph
|
||||
from langgraph.types import interrupt
|
||||
|
||||
from .nodes.editor import editor_node
|
||||
from .nodes.intent_parser import intent_parser_node
|
||||
from .nodes.researcher import researcher_node
|
||||
from .nodes.sample_analyzer import sample_analyzer_node
|
||||
from .nodes.writer_draft import writer_draft_node
|
||||
from .nodes.writer_outline import writer_outline_node
|
||||
from .state import AIWritingState
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _mark_session_done(config: RunnableConfig | None) -> None:
|
||||
"""把业务表行 status 写成 ``done``。
|
||||
|
||||
pause_draft_confirm action=finalize / pause_review_confirm force_finalize 时调,
|
||||
让 LangGraph Server 路径下新会话有正确终态(老路径靠 ``_stream_graph_segment``
|
||||
finally 块写终态,新路径完全不走它)。失败不影响 graph 流程。
|
||||
"""
|
||||
if not config:
|
||||
return
|
||||
thread_id = (config.get("configurable") or {}).get("thread_id")
|
||||
if not thread_id:
|
||||
return
|
||||
from deerflow.persistence.ai_writing_sessions import get_default_store
|
||||
from deerflow.runtime.user_context import AUTO
|
||||
|
||||
repo = get_default_store()
|
||||
if repo is None:
|
||||
return
|
||||
try:
|
||||
await repo.update(thread_id, user_id=AUTO, status="done")
|
||||
except Exception:
|
||||
logger.exception("ai_writing: mark_session_done failed (non-fatal) thread_id=%s", thread_id)
|
||||
|
||||
# ── 干预节点 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
async def pause_material_confirm(state: AIWritingState) -> dict:
|
||||
"""PAUSE-1:素材确认"""
|
||||
pkg = state["material_package"]
|
||||
user_input: dict = interrupt({
|
||||
"pause_point": "material_confirm",
|
||||
"message": "素材收集专家已完成素材搜集,请确认素材后继续。",
|
||||
"materials": pkg["materials"],
|
||||
"keywords": pkg["keywords"],
|
||||
"summary": pkg["summary"],
|
||||
})
|
||||
|
||||
action = user_input.get("action", "confirm")
|
||||
|
||||
# 单输入框文本:可能是技能名 / 来源 / 检索意图,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"]])
|
||||
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,
|
||||
"status": "writing_outline",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "用户已确认素材,开始规划大纲"}],
|
||||
}
|
||||
|
||||
# enable_skill_search:旧版「启用技能检索」按钮的兼容入口——通用检索现在
|
||||
# 本来就按 agent 配置技能编排,researcher 把它按 user_query 追加检索处理
|
||||
if action == "enable_skill_search":
|
||||
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, "material_source": material_source},
|
||||
"material_source": material_source,
|
||||
"status": "researching",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": msg}],
|
||||
}
|
||||
|
||||
# re_search:带 user_query = 在配置技能中匹配检索并**追加**素材;
|
||||
# 不带输入 = 重新生成检索词、覆盖式重检(兼容旧版 extraKeywords 数组追加)
|
||||
extra_kws = user_input.get("extraKeywords") or []
|
||||
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, "material_source": material_source},
|
||||
"material_source": material_source,
|
||||
"status": "researching",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": msg}],
|
||||
}
|
||||
|
||||
|
||||
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:
|
||||
return outline
|
||||
sections = []
|
||||
for s in outline.get("sections", []):
|
||||
sections.append({
|
||||
"section_title": s.get("section_title") or s.get("sectionTitle", ""),
|
||||
"key_points": s.get("key_points") or s.get("keyPoints", []),
|
||||
"material_ids": s.get("material_ids") or s.get("materialIds", []),
|
||||
})
|
||||
return {"title": outline.get("title", ""), "sections": sections}
|
||||
|
||||
|
||||
def _check_outline_material_support(outline: dict) -> str:
|
||||
"""轻量启发式:统计未关联任何素材的章节,返回预警文案(空串=无需预警)。
|
||||
|
||||
仅做提示、不拦截流程;真正的素材不足会在写作节点被检出并触发求助暂停。
|
||||
"""
|
||||
sections = (outline or {}).get("sections", [])
|
||||
weak = [
|
||||
(s.get("section_title") or s.get("sectionTitle") or "")
|
||||
for s in sections
|
||||
if not (s.get("material_ids") or s.get("materialIds"))
|
||||
]
|
||||
weak = [t for t in weak if t]
|
||||
if not weak:
|
||||
return ""
|
||||
return (
|
||||
f"有 {len(weak)} 个章节未关联检索素材({('、'.join(weak))})。"
|
||||
"严格依据素材模式下,这些章节在写作时可能因素材不足而暂停向你求助。"
|
||||
)
|
||||
|
||||
|
||||
async def pause_outline_confirm(state: AIWritingState) -> dict:
|
||||
"""PAUSE-2:大纲确认"""
|
||||
user_input: dict = interrupt({
|
||||
"pause_point": "outline_confirm",
|
||||
"message": "作家已完成大纲规划,请确认或编辑大纲后继续。",
|
||||
"outline": state["current_outline"],
|
||||
})
|
||||
|
||||
action = user_input.get("action", "confirm")
|
||||
|
||||
if action == "confirm":
|
||||
raw_outline = user_input.get("editedOutline") or state["current_outline"]
|
||||
edited = _normalize_outline(raw_outline) if raw_outline else state["current_outline"]
|
||||
events: list[dict] = [{"type": "resumed", "agent_name": "系统", "message": "用户已确认大纲,开始写作"}]
|
||||
# 阶段四:严格模式下对大纲做轻量素材充分性预警(仅提示,不拦截流程)
|
||||
if state.get("writing_mode") == "strict":
|
||||
warning = _check_outline_material_support(edited)
|
||||
if warning:
|
||||
events.append({
|
||||
"type": "notice", "agent_name": "系统",
|
||||
"title": "素材充分性提示", "detail": warning,
|
||||
})
|
||||
return {
|
||||
"current_outline": edited,
|
||||
"last_user_intervention": user_input,
|
||||
"status": "writing_draft",
|
||||
"progress_events": events,
|
||||
}
|
||||
|
||||
# re_outline
|
||||
feedback = user_input.get("outlineFeedback", "")
|
||||
updates: dict = {
|
||||
"last_user_intervention": {**user_input, "_feedback": feedback},
|
||||
"status": "writing_outline",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": f"重新规划大纲({feedback[:30]})"}],
|
||||
}
|
||||
# 用户在卡片里手动改过大纲 → 以手动版作为本次重规划的编辑基准
|
||||
raw_edited = user_input.get("editedOutline")
|
||||
if raw_edited:
|
||||
updates["current_outline"] = _normalize_outline(raw_edited)
|
||||
return updates
|
||||
|
||||
|
||||
async def pause_draft_confirm(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
"""PAUSE-3:草稿确认"""
|
||||
draft = state.get("current_draft")
|
||||
# 防御:草稿缺失(写作节点异常未产出)时不进入确认暂停,转 error 优雅结束,
|
||||
# 避免 draft["title"] 在 None 上崩溃。正常路径下 after_writer_draft 已拦截
|
||||
# status:error,这里是兜底第二道防线(如 editor 异常等边角情形)。
|
||||
if not draft:
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": [{
|
||||
"type": "error", "agent_name": "作家",
|
||||
"title": "草稿缺失", "detail": "草稿尚未生成,无法进入确认环节,请重试。",
|
||||
"message": "草稿生成失败",
|
||||
}],
|
||||
}
|
||||
user_input: dict = interrupt({
|
||||
"pause_point": "draft_confirm",
|
||||
"message": "草稿已完成,请确认或给出修改意见。",
|
||||
"draft_title": draft["title"],
|
||||
"revision_count": draft["revision_count"],
|
||||
"review_result": state.get("review_result"),
|
||||
})
|
||||
|
||||
action = user_input.get("action", "to_editor")
|
||||
|
||||
if action == "finalize":
|
||||
await _mark_session_done(config)
|
||||
return {
|
||||
"last_user_intervention": user_input,
|
||||
"status": "done",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "用户确认定稿"}],
|
||||
}
|
||||
|
||||
if action == "user_revise":
|
||||
notes = user_input.get("userRevisionNotes", "")
|
||||
return {
|
||||
"last_user_intervention": {**user_input, "_user_notes": notes},
|
||||
"revision_count": state.get("revision_count", 0) + 1,
|
||||
"status": "revising",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": f"按用户意见重写({notes[:30]})"}],
|
||||
}
|
||||
|
||||
# to_editor:用户可能在「您的修改意见」里填了关注点再点「让编辑继续审核」,
|
||||
# 把它带给编辑节点,让三维度审查重点核查用户在意的方面(不再被丢弃)。
|
||||
editor_notes = (user_input.get("userRevisionNotes") or "").strip()
|
||||
msg = (
|
||||
f"提交编辑审核(已附带你的修改意见:{editor_notes[:30]})"
|
||||
if editor_notes else "提交编辑审核"
|
||||
)
|
||||
return {
|
||||
"last_user_intervention": {**user_input, "_editor_notes": editor_notes},
|
||||
"status": "reviewing",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": msg}],
|
||||
}
|
||||
|
||||
|
||||
async def pause_section_help(state: AIWritingState) -> dict:
|
||||
"""素材不足求助点:严格模式下作家发现章节缺素材,让用户逐章决定如何处理。
|
||||
|
||||
用户对每个被阻塞章节选择:
|
||||
- supplement:补充检索素材(整体回到检索流程重走)
|
||||
- delete:删除该章节
|
||||
- loose:按通用知识续写该章节
|
||||
"""
|
||||
blocked = state.get("blocked_sections") or []
|
||||
user_input: dict = interrupt({
|
||||
"pause_point": "section_help",
|
||||
"message": "部分章节缺少足够素材支撑写作,请决定如何处理。",
|
||||
"blocked_sections": blocked,
|
||||
})
|
||||
|
||||
# sectionDecisions: [{"sectionTitle": "...", "decision": "supplement|delete|loose"}]
|
||||
by_title: dict = {}
|
||||
for d in user_input.get("sectionDecisions") or []:
|
||||
title = d.get("sectionTitle") or d.get("section_title")
|
||||
if title:
|
||||
by_title[title] = d.get("decision", "loose")
|
||||
|
||||
# 任一章节选「补充检索」→ 整体回到检索,重走「检索 → 素材确认 → 大纲 → 写作」
|
||||
supplement_reasons = [
|
||||
b["reason"] for b in blocked if by_title.get(b["section_title"]) == "supplement"
|
||||
]
|
||||
if supplement_reasons:
|
||||
return {
|
||||
# supplement_search:补充检索(保留已有素材、追加新素材),完成后直接回写作、不重排大纲
|
||||
"last_user_intervention": {**user_input, "action": "supplement_search", "_extra_keywords": supplement_reasons},
|
||||
"status": "researching",
|
||||
"blocked_sections": None,
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "补充检索素材中…"}],
|
||||
}
|
||||
|
||||
# 否则:删除标记章节、记录「通用知识续写」章节,直接回写作
|
||||
outline = state.get("current_outline") or {"title": "", "sections": []}
|
||||
kept, loose_titles = [], []
|
||||
for s in outline.get("sections", []):
|
||||
title = s.get("section_title") or s.get("sectionTitle", "")
|
||||
decision = by_title.get(title)
|
||||
if decision == "delete":
|
||||
continue
|
||||
if decision == "loose":
|
||||
loose_titles.append(title)
|
||||
kept.append(s)
|
||||
# 防御:用户把所有章节都删了 → 保留原大纲并全部按通用知识写,避免空文章
|
||||
if not kept:
|
||||
kept = outline.get("sections", [])
|
||||
loose_titles = [s.get("section_title") or s.get("sectionTitle", "") for s in kept]
|
||||
return {
|
||||
"current_outline": {**outline, "sections": kept},
|
||||
"last_user_intervention": {**user_input, "_loose_sections": loose_titles},
|
||||
"status": "writing_draft",
|
||||
"blocked_sections": None,
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "已按你的选择继续写作…"}],
|
||||
}
|
||||
|
||||
|
||||
async def pause_review_confirm(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
"""PAUSE-4:编辑打回确认"""
|
||||
user_input: dict = interrupt({
|
||||
"pause_point": "review_confirm",
|
||||
"message": "编辑认为文章需要修改,请决定下一步操作。",
|
||||
"review_result": state["review_result"],
|
||||
"revision_count": state.get("revision_count", 0),
|
||||
"max_revisions": state.get("max_revisions", 3),
|
||||
})
|
||||
|
||||
if user_input.get("overrideVerdict") or user_input.get("action") == "force_finalize":
|
||||
await _mark_session_done(config)
|
||||
return {
|
||||
"last_user_intervention": user_input,
|
||||
"status": "done",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "用户强制定稿"}],
|
||||
}
|
||||
|
||||
action = user_input.get("action", "accept_review")
|
||||
extra_notes = user_input.get("userRevisionNotes", "")
|
||||
return {
|
||||
"last_user_intervention": {**user_input, "_extra_notes": extra_notes, "action": action},
|
||||
"revision_count": state.get("revision_count", 0) + 1,
|
||||
"status": "revising",
|
||||
"progress_events": [{"type": "resumed", "agent_name": "系统", "message": "按编辑意见重写"}],
|
||||
}
|
||||
|
||||
|
||||
# ── 条件路由 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
def after_researcher(state: AIWritingState) -> str:
|
||||
"""检索完成后:补充检索(素材不足求助)直接回写作,否则去素材确认。"""
|
||||
return "writer_draft" if state.get("status") == "writing_draft" else "pause_material"
|
||||
|
||||
|
||||
def after_material_pause(state: AIWritingState) -> str:
|
||||
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:
|
||||
return "writer_outline" if state["status"] == "writing_outline" else "writer_draft"
|
||||
|
||||
|
||||
def after_writer_draft(state: AIWritingState) -> str:
|
||||
"""写作完成后:素材不足则去求助暂停点,否则去草稿确认。
|
||||
|
||||
写作节点兜底成 ``status:error``(如模型创建失败)时直接结束本次 run —— 否则会
|
||||
流到 pause_draft,而此时 ``current_draft`` 为 None,``pause_draft_confirm`` 取
|
||||
``draft["title"]`` 会二次崩溃('NoneType' object is not subscriptable)。错误事件
|
||||
已由写作节点 emit,run 优雅结束即可,用户重试不受影响。
|
||||
"""
|
||||
if state["status"] == "error":
|
||||
return "end"
|
||||
return "pause_section_help" if state["status"] == "awaiting_section_help" else "pause_draft"
|
||||
|
||||
|
||||
def after_section_help_pause(state: AIWritingState) -> str:
|
||||
"""求助暂停点之后:补充检索回 researcher,否则回写作。"""
|
||||
return "researcher" if state["status"] == "researching" else "writer_draft"
|
||||
|
||||
|
||||
def after_draft_pause(state: AIWritingState) -> str:
|
||||
status = state["status"]
|
||||
if status == "done":
|
||||
return "end"
|
||||
if status == "revising":
|
||||
return "writer_draft"
|
||||
return "editor"
|
||||
|
||||
|
||||
def after_review(state: AIWritingState) -> str:
|
||||
review = state.get("review_result") or {}
|
||||
if review.get("verdict") == "pass":
|
||||
return "pause_draft"
|
||||
rev_count = state.get("revision_count", 0)
|
||||
max_rev = state.get("max_revisions", 3)
|
||||
if rev_count >= max_rev:
|
||||
return "pause_draft" # 超出上限,强制到草稿确认
|
||||
return "pause_review"
|
||||
|
||||
|
||||
def after_review_pause(state: AIWritingState) -> str:
|
||||
return "end" if state["status"] == "done" else "writer_draft"
|
||||
|
||||
|
||||
|
||||
# ── LangGraph Server 入口 ────────────────────────────────────────────────────
|
||||
#
|
||||
# compile 出裸 graph(不带 checkpointer),由 LangGraph Server 注入主 checkpointer
|
||||
# (langgraph.json 里 ``make_checkpointer`` 配的那个)。AI 写作的 thread 状态与
|
||||
# 主聊天共享同一份 saver —— LangGraph schema 用 ``(thread_id, checkpoint_ns)`` 做
|
||||
# 主键,UUID 不会撞。
|
||||
def make_ai_writing_graph(config=None):
|
||||
"""LangGraph Server graph factory:不接收 checkpointer,由 Server 注入。
|
||||
|
||||
Args:
|
||||
config: LangGraph Server 调用工厂时注入的 RunnableConfig(暂未消费)。
|
||||
"""
|
||||
g = StateGraph(AIWritingState)
|
||||
|
||||
g.add_node("sample_analyzer", sample_analyzer_node)
|
||||
g.add_node("intent_parser", intent_parser_node)
|
||||
g.add_node("researcher", researcher_node)
|
||||
g.add_node("pause_material", pause_material_confirm)
|
||||
g.add_node("writer_outline", writer_outline_node)
|
||||
g.add_node("pause_outline", pause_outline_confirm)
|
||||
g.add_node("writer_draft", writer_draft_node)
|
||||
g.add_node("pause_section_help", pause_section_help)
|
||||
g.add_node("pause_draft", pause_draft_confirm)
|
||||
g.add_node("editor", editor_node)
|
||||
g.add_node("pause_review", pause_review_confirm)
|
||||
|
||||
g.set_entry_point("sample_analyzer")
|
||||
g.add_edge("sample_analyzer", "intent_parser")
|
||||
g.add_edge("intent_parser", "researcher")
|
||||
g.add_conditional_edges("researcher", after_researcher,
|
||||
{"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", "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"})
|
||||
g.add_conditional_edges("writer_draft", after_writer_draft,
|
||||
{"pause_section_help": "pause_section_help", "pause_draft": "pause_draft", "end": END})
|
||||
g.add_conditional_edges("pause_section_help", after_section_help_pause,
|
||||
{"researcher": "researcher", "writer_draft": "writer_draft"})
|
||||
g.add_conditional_edges("pause_draft", after_draft_pause,
|
||||
{"end": END, "writer_draft": "writer_draft", "editor": "editor"})
|
||||
g.add_conditional_edges("editor", after_review,
|
||||
{"pause_draft": "pause_draft", "pause_review": "pause_review"})
|
||||
g.add_conditional_edges("pause_review", after_review_pause,
|
||||
{"end": END, "writer_draft": "writer_draft"})
|
||||
|
||||
return g.compile()
|
||||
+573
@@ -0,0 +1,573 @@
|
||||
"""对话驱动写作台的意图路由:用户自由文本 → 结构化 action/payload。
|
||||
|
||||
供 Gateway 的 ``POST /api/ai-writing/sessions/{id}/intent`` 调用:读该会话的
|
||||
graph checkpoint state(素材/大纲/草稿/审核摘要)+ 当前暂停点,用 LLM 把用户的
|
||||
一句话翻译成 ``UserInterventionPayload`` 可直接消费的指令;或识别为提问(直接
|
||||
带回 answer)、控制指令、需澄清。
|
||||
|
||||
核心安全边界(避免"原逻辑不支持硬套用"):
|
||||
- action 白名单严格对齐 graph.py 各 pause 节点真实接受的动作(见
|
||||
``ALLOWED_ACTIONS``),LLM 输出不在白名单内 → 一律降级为 clarify;
|
||||
- 高代价动作(finalize / force_finalize / 无查询词的覆盖式 re_search)要求
|
||||
confidence ≥ 0.85,不够 → 降级 clarify;
|
||||
- 无暂停点(运行中/已结束)时不允许输出任何 action;
|
||||
- payload 逐 key 白名单清洗,杜绝 LLM 编造字段污染 resume 载荷。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from .prompts.intent_router_prompts import (
|
||||
INTENT_ROUTER_SYSTEM,
|
||||
INTENT_ROUTER_USER_TEMPLATE,
|
||||
NO_PAUSE_GUIDE,
|
||||
STAGE_ACTION_GUIDES,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 各暂停点合法动作 —— 与 graph.py 的 pause_material_confirm / pause_outline_confirm /
|
||||
# pause_draft_confirm / pause_review_confirm / pause_section_help 严格对齐。
|
||||
# 注意 material_confirm 的 enable_skill_search 是旧按钮兼容入口,语义 = 带
|
||||
# userQuery 的 re_search,意图层统一输出 re_search,不再暴露旧动作。
|
||||
ALLOWED_ACTIONS: dict[str, set[str]] = {
|
||||
"material_confirm": {"confirm", "re_search"},
|
||||
"outline_confirm": {"confirm", "re_outline"},
|
||||
"draft_confirm": {"to_editor", "finalize", "user_revise"},
|
||||
"review_confirm": {"accept_review", "force_finalize"},
|
||||
"section_help": {"section_help"},
|
||||
}
|
||||
|
||||
# 高代价动作的置信度下限(misfire 代价大:结束流程 / 丢弃现有素材)。
|
||||
HIGH_COST_CONFIDENCE = 0.85
|
||||
# 普通动作的置信度下限。
|
||||
ACTION_CONFIDENCE = 0.5
|
||||
|
||||
_SECTION_DECISIONS = {"supplement", "loose", "delete"}
|
||||
|
||||
_THINK_RE = re.compile(r"<think>.*?</think>", re.S)
|
||||
_WRITING_ACTION_VERB_RE = re.compile(
|
||||
r"(新增|增加|添加|加入|加上|补充|补上|补个|补一个|生成|撰写|写一|写个|写成|扩写|缩写|精简|改写|润色|调整|修改|重写|删除|删掉|拆成|合并|挪到|移动|规划|重新规划|设计|安排)"
|
||||
)
|
||||
_WRITING_ACTION_TARGET_RE = re.compile(
|
||||
r"(第[一二三四五六七八九十\d]+[章节段]|章节|章|小节|段落|段|结尾|开头|标题|大纲|内容|案例|表格|总结章|总结章节|结论|摘要|观点|部分)"
|
||||
)
|
||||
_INFO_QUESTION_RE = re.compile(r"(哪些|什么|为什么|是什么|有哪些|多少|是否)")
|
||||
|
||||
|
||||
def _fallback_intent() -> dict:
|
||||
return {
|
||||
"intent": "clarify",
|
||||
"action": None,
|
||||
"payload": {},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 0.0,
|
||||
"clarify": "我没有理解你的意思,能换个说法再试一次吗?也可以直接点击上方卡片里的按钮操作。",
|
||||
}
|
||||
|
||||
|
||||
def _looks_like_writing_change_request(text: str) -> bool:
|
||||
text = (text or "").strip()
|
||||
if _INFO_QUESTION_RE.search(text):
|
||||
return False
|
||||
return bool(_WRITING_ACTION_VERB_RE.search(text) and _WRITING_ACTION_TARGET_RE.search(text))
|
||||
|
||||
|
||||
def _rule_based_intent(text: str, pause_point: str | None) -> dict | None:
|
||||
"""Deterministic guard for writing-operation requests that look like Q&A."""
|
||||
if not _looks_like_writing_change_request(text):
|
||||
return None
|
||||
if pause_point == "outline_confirm":
|
||||
return {
|
||||
"intent": "revise_outline",
|
||||
"action": "re_outline",
|
||||
"payload": {"outlineFeedback": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
if pause_point == "draft_confirm":
|
||||
return {
|
||||
"intent": "revise_draft",
|
||||
"action": "user_revise",
|
||||
"payload": {"userRevisionNotes": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
if pause_point == "review_confirm":
|
||||
return {
|
||||
"intent": "accept_review_with_user_notes",
|
||||
"action": "accept_review",
|
||||
"payload": {"userRevisionNotes": text.strip()},
|
||||
"need_research": False,
|
||||
"research_query": "",
|
||||
"answer": "",
|
||||
"confidence": 1.0,
|
||||
"clarify": "",
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
# ── 上下文快照 ────────────────────────────────────────────────────────────────
|
||||
|
||||
def _shorten(text: str, limit: int) -> str:
|
||||
text = (text or "").strip().replace("\n", " ")
|
||||
return text if len(text) <= limit else text[: limit - 1] + "…"
|
||||
|
||||
|
||||
def _materials_block(values: dict, limit: int = 30) -> str:
|
||||
pkg = values.get("material_package") or {}
|
||||
materials = pkg.get("materials") or []
|
||||
if not materials:
|
||||
return "(当前没有素材)"
|
||||
lines = []
|
||||
for i, m in enumerate(materials[:limit]):
|
||||
mid = m.get("id", "")
|
||||
title = _shorten(str(m.get("title") or ""), 60)
|
||||
source = _shorten(str(m.get("source") or ""), 20)
|
||||
excerpt = _shorten(str(m.get("content") or ""), 80)
|
||||
lines.append(f"{i + 1}. [id={mid}] {title}(来源:{source}){excerpt}")
|
||||
if len(materials) > limit:
|
||||
lines.append(f"…共 {len(materials)} 条,仅列出前 {limit} 条")
|
||||
keywords = pkg.get("keywords") or []
|
||||
head = f"检索关键词:{'、'.join(str(k) for k in keywords[:10])}\n" if keywords else ""
|
||||
return head + "\n".join(lines)
|
||||
|
||||
|
||||
def _outline_block(values: dict) -> str:
|
||||
outline = values.get("current_outline") or {}
|
||||
sections = outline.get("sections") or []
|
||||
if not sections:
|
||||
return "(尚无大纲)"
|
||||
lines = [f"文章标题:{outline.get('title') or ''}"]
|
||||
for i, s in enumerate(sections):
|
||||
title = s.get("section_title") or s.get("sectionTitle") or ""
|
||||
points = s.get("key_points") or s.get("keyPoints") or []
|
||||
point_titles = []
|
||||
for p in points[:6]:
|
||||
if isinstance(p, dict):
|
||||
point_titles.append(str(p.get("point_title") or p.get("pointTitle") or ""))
|
||||
else:
|
||||
point_titles.append(str(p))
|
||||
suffix = f"(要点:{_shorten(';'.join(t for t in point_titles if t), 120)})" if point_titles else ""
|
||||
lines.append(f"第{i + 1}章 {title}{suffix}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _draft_block(values: dict, body_limit: int = 4000) -> str:
|
||||
draft = values.get("current_draft") or {}
|
||||
markdown = str(draft.get("full_markdown") or draft.get("fullMarkdown") or "")
|
||||
if not markdown:
|
||||
return "(尚无草稿)"
|
||||
title = draft.get("title") or ""
|
||||
word_count = draft.get("word_count") or draft.get("wordCount") or ""
|
||||
revision = draft.get("revision_count") or draft.get("revisionCount") or 0
|
||||
body = markdown if len(markdown) <= body_limit else markdown[:body_limit] + "\n…(正文过长已截断)"
|
||||
return f"草稿标题:《{title}》 修改次数:{revision} 字数:{word_count}\n--- 草稿正文 ---\n{body}"
|
||||
|
||||
|
||||
def _review_block(values: dict) -> str:
|
||||
review = values.get("review_result") or {}
|
||||
if not review:
|
||||
return "(尚无编辑审核结果)"
|
||||
notes = review.get("revision_notes") or review.get("revisionNotes") or []
|
||||
lines = [
|
||||
f"编辑结论:{review.get('verdict', '')}(总分 {review.get('overall_score', review.get('overallScore', ''))},"
|
||||
f"事实 {review.get('fact_score', review.get('factScore', ''))} / 逻辑 {review.get('logic_score', review.get('logicScore', ''))} / "
|
||||
f"语言 {review.get('language_score', review.get('languageScore', ''))})",
|
||||
]
|
||||
for i, n in enumerate(notes):
|
||||
issue = _shorten(str(n.get("issue") or ""), 80)
|
||||
suggestion = _shorten(str(n.get("suggestion") or ""), 80)
|
||||
lines.append(f"意见[index={i}] [{n.get('category', '')}/{n.get('severity', '')}] {issue} → {suggestion}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _blocked_block(values: dict, interrupt_value: dict | None) -> str:
|
||||
blocked = (interrupt_value or {}).get("blocked_sections") or values.get("blocked_sections") or []
|
||||
if not blocked:
|
||||
return ""
|
||||
lines = ["受阻章节(素材不足):"]
|
||||
for b in blocked:
|
||||
title = b.get("section_title") or b.get("sectionTitle") or ""
|
||||
reason = _shorten(str(b.get("reason") or ""), 80)
|
||||
lines.append(f"- {title}:{reason}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def build_context_snapshot(values: dict, pause_point: str | None, interrupt_value: dict | None = None) -> str:
|
||||
"""把 checkpoint state 压成 LLM 可读的中文上下文快照。
|
||||
|
||||
按当前阶段裁剪重点:素材阶段给全量素材清单;大纲阶段给大纲;草稿/审核阶段给
|
||||
草稿正文(截断)+ 审核意见(带 index,供 selectedIssueIndices 映射)。
|
||||
"""
|
||||
status = str(values.get("status") or "")
|
||||
intent = _shorten(str(values.get("user_intent") or ""), 200)
|
||||
parts = [f"写作主题:{intent}", f"流程状态:{status or '未知'}"]
|
||||
|
||||
if pause_point == "material_confirm":
|
||||
parts.append("素材清单:\n" + _materials_block(values))
|
||||
outline = _outline_block(values)
|
||||
if outline != "(尚无大纲)":
|
||||
parts.append("已有大纲:\n" + outline)
|
||||
elif pause_point == "outline_confirm":
|
||||
parts.append("当前大纲:\n" + _outline_block(values))
|
||||
parts.append("素材清单(供判断 need_research):\n" + _materials_block(values, limit=20))
|
||||
elif pause_point in ("draft_confirm", "review_confirm"):
|
||||
parts.append(_draft_block(values))
|
||||
parts.append("编辑审核:\n" + _review_block(values))
|
||||
parts.append("素材清单(供判断 need_research):\n" + _materials_block(values, limit=15))
|
||||
elif pause_point == "section_help":
|
||||
blocked = _blocked_block(values, interrupt_value)
|
||||
if blocked:
|
||||
parts.append(blocked)
|
||||
parts.append("当前大纲:\n" + _outline_block(values))
|
||||
else:
|
||||
# 运行中 / 已结束:给全景概要,支撑 ask_meta / ask_content
|
||||
parts.append("素材清单:\n" + _materials_block(values, limit=15))
|
||||
outline = _outline_block(values)
|
||||
if outline != "(尚无大纲)":
|
||||
parts.append("大纲:\n" + outline)
|
||||
draft = _draft_block(values, body_limit=2500)
|
||||
if draft != "(尚无草稿)":
|
||||
parts.append(draft)
|
||||
review = _review_block(values)
|
||||
if review != "(尚无编辑审核结果)":
|
||||
parts.append("编辑审核:\n" + review)
|
||||
|
||||
return "\n\n".join(p for p in parts if p)
|
||||
|
||||
|
||||
# ── 结果清洗 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
def _clean_payload(raw: Any, action: str, values: dict, interrupt_value: dict | None) -> dict:
|
||||
"""按动作白名单清洗 payload(键名统一驼峰,供前端直接喂 submitIntervention)。"""
|
||||
p = raw if isinstance(raw, dict) else {}
|
||||
out: dict[str, Any] = {}
|
||||
|
||||
def _str(key: str) -> str:
|
||||
v = p.get(key)
|
||||
return v.strip() if isinstance(v, str) else ""
|
||||
|
||||
if action == "confirm":
|
||||
ids = p.get("approvedMaterialIds") or p.get("approved_material_ids")
|
||||
if isinstance(ids, list) and ids:
|
||||
known = {str(m.get("id")) for m in ((values.get("material_package") or {}).get("materials") or [])}
|
||||
kept = [str(i) for i in ids if not known or str(i) in known]
|
||||
if kept:
|
||||
out["approvedMaterialIds"] = kept
|
||||
elif action == "re_search":
|
||||
query = _str("userQuery") or _str("user_query")
|
||||
if query:
|
||||
out["userQuery"] = query
|
||||
out["approvedMaterialIds"] = []
|
||||
elif action == "re_outline":
|
||||
feedback = _str("outlineFeedback") or _str("outline_feedback")
|
||||
if feedback:
|
||||
out["outlineFeedback"] = feedback
|
||||
elif action in ("user_revise", "to_editor", "accept_review"):
|
||||
notes = _str("userRevisionNotes") or _str("user_revision_notes")
|
||||
if notes:
|
||||
out["userRevisionNotes"] = notes
|
||||
indices = p.get("selectedIssueIndices") or p.get("selected_issue_indices")
|
||||
if isinstance(indices, list):
|
||||
review = values.get("review_result") or {}
|
||||
total = len(review.get("revision_notes") or review.get("revisionNotes") or [])
|
||||
cleaned = sorted({int(i) for i in indices if isinstance(i, (int, float)) and 0 <= int(i) < max(total, 0)})
|
||||
if cleaned:
|
||||
out["selectedIssueIndices"] = cleaned
|
||||
# user_revise 必须有意见原文,没有就说明 LLM 输出不完整 → 由上层降级 clarify
|
||||
elif action == "force_finalize":
|
||||
out["overrideVerdict"] = True
|
||||
elif action == "section_help":
|
||||
blocked = (interrupt_value or {}).get("blocked_sections") or values.get("blocked_sections") or []
|
||||
blocked_titles = [
|
||||
str(b.get("section_title") or b.get("sectionTitle") or "") for b in blocked
|
||||
]
|
||||
raw_decisions = p.get("sectionDecisions") or p.get("section_decisions") or []
|
||||
by_title: dict[str, str] = {}
|
||||
for d in raw_decisions if isinstance(raw_decisions, list) else []:
|
||||
if not isinstance(d, dict):
|
||||
continue
|
||||
title = str(d.get("sectionTitle") or d.get("section_title") or "")
|
||||
decision = str(d.get("decision") or "")
|
||||
if title and decision in _SECTION_DECISIONS:
|
||||
by_title[title] = decision
|
||||
# 覆盖全部受阻章节;LLM 漏掉的按多数倾向补齐,无倾向用 loose(与后台挂起默认一致)
|
||||
majority = "loose"
|
||||
if by_title:
|
||||
counts: dict[str, int] = {}
|
||||
for v in by_title.values():
|
||||
counts[v] = counts.get(v, 0) + 1
|
||||
majority = max(counts, key=lambda k: counts[k])
|
||||
out["sectionDecisions"] = [
|
||||
{"sectionTitle": t, "decision": by_title.get(t, majority)} for t in blocked_titles if t
|
||||
]
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def normalize_intent_result(raw: dict, pause_point: str | None, values: dict, interrupt_value: dict | None = None) -> dict:
|
||||
"""LLM 原始输出 → 安全的意图契约(动作白名单 / 置信度门槛 / payload 清洗)。"""
|
||||
intent = str(raw.get("intent") or "unknown").strip() or "unknown"
|
||||
action = raw.get("action")
|
||||
action = action.strip() if isinstance(action, str) and action.strip() else None
|
||||
answer = str(raw.get("answer") or "").strip()
|
||||
clarify = str(raw.get("clarify") or "").strip()
|
||||
need_research = bool(raw.get("need_research"))
|
||||
research_query = str(raw.get("research_query") or "").strip()
|
||||
try:
|
||||
confidence = max(0.0, min(1.0, float(raw.get("confidence") or 0.0)))
|
||||
except (TypeError, ValueError):
|
||||
confidence = 0.0
|
||||
|
||||
def _clarified(message: str) -> dict:
|
||||
return {
|
||||
"intent": intent if intent != "unknown" else "clarify",
|
||||
"pause_point": pause_point,
|
||||
"action": None,
|
||||
"payload": {},
|
||||
"need_research": need_research,
|
||||
"research_query": research_query,
|
||||
"answer": answer,
|
||||
"confidence": confidence,
|
||||
"clarify": clarify or message,
|
||||
}
|
||||
|
||||
# clarify 非空 / 问答类 → 不执行动作
|
||||
if clarify:
|
||||
return _clarified(clarify)
|
||||
if action is None:
|
||||
return {
|
||||
"intent": intent,
|
||||
"pause_point": pause_point,
|
||||
"action": None,
|
||||
"payload": {},
|
||||
"need_research": need_research,
|
||||
"research_query": research_query,
|
||||
"answer": answer,
|
||||
"confidence": confidence,
|
||||
"clarify": "",
|
||||
}
|
||||
|
||||
# 无暂停点:图没有可 resume 的 interrupt,任何动作都执行不了
|
||||
allowed = ALLOWED_ACTIONS.get(pause_point or "")
|
||||
if not allowed:
|
||||
return _clarified("当前流程正在自动执行或已结束,没有等待确认的步骤,暂时不能执行该操作。")
|
||||
|
||||
if action not in allowed:
|
||||
return _clarified(
|
||||
f"这个操作在当前阶段做不了(可用:{'、'.join(sorted(allowed))})。请换一种说法,或等流程走到对应阶段再提。"
|
||||
)
|
||||
|
||||
payload = _clean_payload(raw.get("payload"), action, values, interrupt_value)
|
||||
|
||||
# user_revise 没带意见原文 = 输出不完整,宁可反问
|
||||
if action == "user_revise" and not payload.get("userRevisionNotes"):
|
||||
return _clarified("你希望怎么修改草稿?请具体说明(例如:第三段太啰嗦,精简一下)。")
|
||||
|
||||
# 高代价动作置信度门槛
|
||||
is_high_cost = action in ("finalize", "force_finalize") or (
|
||||
action == "re_search" and not payload.get("userQuery")
|
||||
)
|
||||
threshold = HIGH_COST_CONFIDENCE if is_high_cost else ACTION_CONFIDENCE
|
||||
if confidence < threshold:
|
||||
hint = {
|
||||
"finalize": "确认要定稿并结束本次写作吗?",
|
||||
"force_finalize": "确认忽略编辑意见、强制定稿吗?",
|
||||
"re_search": "重新搜索会重新生成检索词并覆盖现有素材,确认吗?(若只想补充素材,请描述想补充的内容)",
|
||||
}.get(action, "我理解得不够确定,能再说得具体一点吗?")
|
||||
return _clarified(hint)
|
||||
|
||||
return {
|
||||
"intent": intent,
|
||||
"pause_point": pause_point,
|
||||
"action": action,
|
||||
"payload": payload,
|
||||
"need_research": need_research,
|
||||
"research_query": research_query,
|
||||
"answer": answer,
|
||||
"confidence": confidence,
|
||||
"clarify": "",
|
||||
}
|
||||
|
||||
|
||||
# ── LLM 调用 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
def _build_messages(text: str, pause_point: str | None, values: dict, interrupt_value: dict | None):
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
|
||||
stage_guide = STAGE_ACTION_GUIDES.get(pause_point or "", NO_PAUSE_GUIDE)
|
||||
context = build_context_snapshot(values, pause_point, interrupt_value)
|
||||
user = INTENT_ROUTER_USER_TEMPLATE.format(stage_guide=stage_guide, context=context, text=text.strip())
|
||||
return [SystemMessage(content=INTENT_ROUTER_SYSTEM), HumanMessage(content=user)]
|
||||
|
||||
|
||||
async def parse_user_intent(
|
||||
*,
|
||||
text: str,
|
||||
pause_point: str | None,
|
||||
values: dict,
|
||||
interrupt_value: dict | None = None,
|
||||
model_name: str | None = None,
|
||||
model: Any | None = None,
|
||||
) -> dict:
|
||||
"""用户自由文本 → 意图契约 dict(snake_case 顶层字段 + 驼峰 payload)。
|
||||
|
||||
``model`` 参数仅供测试注入 fake;生产走 ``create_chat_model`` + 配置模型
|
||||
容错链(与 intent_parser 同套 fallback_chain)。任何模型层失败都不抛给调用方,
|
||||
降级为 clarify(意图解析是辅助能力,绝不能把写作页面搞挂)。
|
||||
"""
|
||||
from .nodes._utils import _content_to_text, parse_json
|
||||
|
||||
fallback = _fallback_intent()
|
||||
rule = _rule_based_intent(text, pause_point)
|
||||
if rule is not None:
|
||||
return normalize_intent_result(rule, pause_point, values, interrupt_value)
|
||||
|
||||
messages = _build_messages(text, pause_point, values, interrupt_value)
|
||||
|
||||
raw_text = ""
|
||||
if model is not None:
|
||||
try:
|
||||
resp = await model.ainvoke(messages)
|
||||
raw_text = _content_to_text(getattr(resp, "content", "") or "")
|
||||
except Exception:
|
||||
logger.exception("intent_router: injected model failed")
|
||||
else:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
from .nodes._model_fallback import classify_exception, fallback_chain
|
||||
|
||||
chain = fallback_chain((model_name or "").strip() or None) or [None]
|
||||
for candidate in chain:
|
||||
try:
|
||||
m = create_chat_model(name=candidate) if candidate else create_chat_model()
|
||||
resp = await m.ainvoke(messages)
|
||||
raw_text = _content_to_text(getattr(resp, "content", "") or "")
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001 — 换下一个模型继续
|
||||
logger.warning(
|
||||
"intent_router: model=%s failed (reason=%s): %s",
|
||||
candidate, classify_exception(exc), exc,
|
||||
)
|
||||
continue
|
||||
|
||||
raw_text = _THINK_RE.sub("", raw_text or "").strip()
|
||||
if not raw_text:
|
||||
return normalize_intent_result(fallback, pause_point, values, interrupt_value)
|
||||
|
||||
parsed = parse_json(raw_text, fallback)
|
||||
return normalize_intent_result(parsed, pause_point, values, interrupt_value)
|
||||
|
||||
|
||||
async def stream_user_intent(
|
||||
*,
|
||||
text: str,
|
||||
pause_point: str | None,
|
||||
values: dict,
|
||||
interrupt_value: dict | None = None,
|
||||
model_name: str | None = None,
|
||||
model: Any | None = None,
|
||||
) -> AsyncIterator[dict]:
|
||||
"""流式解析用户自由文本。
|
||||
|
||||
事件契约:
|
||||
- ``{"type": "chunk", "chunk": "...", "is_thinking": bool}``
|
||||
- ``{"type": "result", "result": normalized_intent}``
|
||||
|
||||
解析阶段仍以结构化 JSON 为最终产物;流式输出用于前端实时展示模型的
|
||||
思考/解析过程。``<think>...</think>`` 内容和 provider reasoning 字段都会作为
|
||||
``is_thinking=True`` 分流,正文 JSON 只参与最终解析。
|
||||
"""
|
||||
from .nodes._utils import ThinkTagParser, _content_to_text, parse_json, reasoning_delta
|
||||
|
||||
fallback = _fallback_intent()
|
||||
rule = _rule_based_intent(text, pause_point)
|
||||
if rule is not None:
|
||||
yield {
|
||||
"type": "result",
|
||||
"result": normalize_intent_result(rule, pause_point, values, interrupt_value),
|
||||
}
|
||||
return
|
||||
|
||||
messages = _build_messages(text, pause_point, values, interrupt_value)
|
||||
|
||||
def content_events(parser: ThinkTagParser, raw_parts: list[str], delta: str) -> list[dict]:
|
||||
events = []
|
||||
for is_thinking, part in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
raw_parts.append(part)
|
||||
events.append({"type": "chunk", "chunk": part, "is_thinking": is_thinking})
|
||||
return events
|
||||
|
||||
async def run_model(m: Any) -> AsyncIterator[dict]:
|
||||
parser = ThinkTagParser()
|
||||
raw_parts: list[str] = []
|
||||
if hasattr(m, "astream"):
|
||||
async for chunk in m.astream(messages):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
yield {"type": "chunk", "chunk": reasoning, "is_thinking": True}
|
||||
content = _content_to_text(getattr(chunk, "content", "") or "")
|
||||
if content:
|
||||
for event in content_events(parser, raw_parts, content):
|
||||
yield event
|
||||
else:
|
||||
resp = await m.ainvoke(messages)
|
||||
content = _content_to_text(getattr(resp, "content", "") or "")
|
||||
if content:
|
||||
for event in content_events(parser, raw_parts, content):
|
||||
yield event
|
||||
yield {"type": "_raw_text", "raw_text": "".join(raw_parts)}
|
||||
|
||||
raw_text = ""
|
||||
if model is not None:
|
||||
try:
|
||||
async for event in run_model(model):
|
||||
if event["type"] == "_raw_text":
|
||||
raw_text = str(event.get("raw_text") or "")
|
||||
else:
|
||||
yield event
|
||||
except Exception:
|
||||
raw_text = ""
|
||||
logger.exception("intent_router: injected stream model failed")
|
||||
else:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
from .nodes._model_fallback import classify_exception, fallback_chain
|
||||
|
||||
chain = fallback_chain((model_name or "").strip() or None) or [None]
|
||||
for candidate in chain:
|
||||
try:
|
||||
m = create_chat_model(name=candidate) if candidate else create_chat_model()
|
||||
async for event in run_model(m):
|
||||
if event["type"] == "_raw_text":
|
||||
raw_text = str(event.get("raw_text") or "")
|
||||
else:
|
||||
yield event
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001 — 换下一个模型继续
|
||||
raw_text = ""
|
||||
logger.warning(
|
||||
"intent_router: stream model=%s failed (reason=%s): %s",
|
||||
candidate, classify_exception(exc), exc,
|
||||
)
|
||||
continue
|
||||
|
||||
raw_text = _THINK_RE.sub("", raw_text or "").strip()
|
||||
if not raw_text:
|
||||
result = normalize_intent_result(fallback, pause_point, values, interrupt_value)
|
||||
else:
|
||||
parsed = parse_json(raw_text, fallback)
|
||||
result = normalize_intent_result(parsed, pause_point, values, interrupt_value)
|
||||
yield {"type": "result", "result": result}
|
||||
Whitespace-only changes.
+279
@@ -0,0 +1,279 @@
|
||||
"""AI 写作「内置功能型智能体」运行助手。
|
||||
|
||||
四个写作阶段(素材收集/大纲/写作/审核)的图节点在 ``config.yaml →
|
||||
ai_writing.use_builtin_agents.<stage>`` 打开时,不再使用 ``prompts/*.py`` 的
|
||||
硬编码提示词直调 LLM,而是改为:
|
||||
|
||||
1. 从 ``.deer-flow/agents/<agent-id>/`` 加载该内置 agent 的 **SOUL.md**
|
||||
(人设 + 工作流 + 结构化输出契约)作为 system prompt;
|
||||
2. 模型按 ``agent config.yaml 的 model > 节点显式回退模型 > state.model_name``
|
||||
的优先级解析(admin 可在智能体管理页给某阶段单独配模型);
|
||||
3. 流式调用该模型,chunk 通过调用方传入的回调透传到 LangGraph ``custom``
|
||||
通道(前端时间线打字机不回归);
|
||||
4. 按 SOUL 规定的 marker(如 ``[OUTLINE_READY]``)+ ```json``` 块解析结构化
|
||||
输出,解析复用 ``_utils.parse_json`` 多级容错。
|
||||
|
||||
设计取舍(对应 ai-writing-内置智能体改造.md §3.2 / §7):
|
||||
|
||||
- **编排控制权留在节点**:writer 的逐章循环、researcher 的检索编排、editor 的
|
||||
三维度循环都不交给 agent——agent 只承担「单次结构化产出」,这样干预点/逐章
|
||||
流式/严格模式求助逻辑全部原样保留。
|
||||
- **不经过 lead agent 运行时**:本模块位于 harness 层,不能 import ``app.*``
|
||||
(intent.py 的 loopback 机制在 app 层),也无需为单轮结构化调用付出
|
||||
thread/checkpointer/中间件链的成本。SOUL + per-agent 模型已覆盖本场景的
|
||||
可配置性诉求;researcher 的「挂载技能」(Phase 3)再考虑接入完整运行时。
|
||||
- **agent 目录缺失时抛 ``WritingAgentUnavailable``**:由节点捕获后回退旧的
|
||||
硬编码 prompt 路径,种子器(app 层 ``_ai_writing_seed.py``)失败不阻断写作。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ._utils import ThinkTagParser, _content_to_text, parse_json, reasoning_delta
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ── 内置写作 agent 的稳定 id(与 app 层种子器 _ai_writing_seed.py 对齐)──────────
|
||||
RESEARCHER_AGENT_ID = "ai-writing-researcher"
|
||||
OUTLINER_AGENT_ID = "ai-writing-outliner"
|
||||
WRITER_AGENT_ID = "ai-writing-writer"
|
||||
EDITOR_AGENT_ID = "ai-writing-editor"
|
||||
|
||||
# ── SOUL 规定的结构化输出 marker(契约见各 agent 的 SOUL.md)─────────────────────
|
||||
KEYWORDS_READY_MARKER = "[KEYWORDS_READY]"
|
||||
MATERIALS_RANKED_MARKER = "[MATERIALS_RANKED]"
|
||||
OUTLINE_READY_MARKER = "[OUTLINE_READY]"
|
||||
REVIEW_READY_MARKER = "[REVIEW_READY]"
|
||||
|
||||
# flag 名(config.yaml ai_writing.use_builtin_agents.<stage>)→ agent id
|
||||
STAGE_AGENT_IDS = {
|
||||
"researcher": RESEARCHER_AGENT_ID,
|
||||
"outliner": OUTLINER_AGENT_ID,
|
||||
"writer": WRITER_AGENT_ID,
|
||||
"editor": EDITOR_AGENT_ID,
|
||||
}
|
||||
|
||||
|
||||
class WritingAgentUnavailable(RuntimeError):
|
||||
"""内置写作 agent 的 SOUL.md 缺失/为空,无法按 agent 模式运行。
|
||||
|
||||
节点捕获后应回退到旧的硬编码 prompt 路径(等价于 flag 关闭),不让写作
|
||||
流程因 agent 目录被删而中断。
|
||||
"""
|
||||
|
||||
|
||||
def use_builtin_agent(stage: str) -> bool:
|
||||
"""读 ``config.yaml → ai_writing.use_builtin_agents.<stage>`` 开关。
|
||||
|
||||
缺失/非法一律视为 False(默认走旧节点逻辑)。config 带 mtime 热加载,
|
||||
改完 config.yaml 新会话即生效。
|
||||
"""
|
||||
from ._utils import get_ai_writing_config
|
||||
|
||||
flags = get_ai_writing_config().get("use_builtin_agents")
|
||||
if not isinstance(flags, dict):
|
||||
return False
|
||||
return bool(flags.get(stage, False))
|
||||
|
||||
|
||||
def load_writing_agent(agent_id: str):
|
||||
"""加载内置写作 agent 的 (config, soul);SOUL 缺失抛 WritingAgentUnavailable。"""
|
||||
from deerflow.config.agents_config import load_agent_config, load_agent_soul
|
||||
|
||||
soul = load_agent_soul(agent_id)
|
||||
if not soul:
|
||||
raise WritingAgentUnavailable(
|
||||
f"内置写作智能体 '{agent_id}' 的 SOUL.md 缺失或为空"
|
||||
"(目录可能被删除,重启 Gateway 或重新打开写作页可自动补建)"
|
||||
)
|
||||
try:
|
||||
agent_cfg = load_agent_config(agent_id)
|
||||
except ValueError:
|
||||
# config.yaml 解析失败不致命:SOUL 仍可用,模型回退节点/表单选择。
|
||||
logger.warning("load_writing_agent: config.yaml of '%s' is invalid, ignoring", agent_id)
|
||||
agent_cfg = None
|
||||
return agent_cfg, soul
|
||||
|
||||
|
||||
def agent_display_name(agent_id: str, default: str) -> str:
|
||||
"""取 agent 的展示名(progress_events.agent_name 用),加载失败回退默认值。"""
|
||||
try:
|
||||
from deerflow.config.agents_config import load_agent_config
|
||||
|
||||
cfg = load_agent_config(agent_id)
|
||||
name = (cfg.name or "").strip() if cfg else ""
|
||||
return name or default
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def resolve_agent_model_name(
|
||||
agent_cfg,
|
||||
state: dict,
|
||||
config: RunnableConfig,
|
||||
*,
|
||||
explicit_fallback: str | None = None,
|
||||
strict: bool = True,
|
||||
) -> str | None:
|
||||
"""解析本次 agent 调用使用的模型名。
|
||||
|
||||
优先级:agent config.yaml 的 ``model`` > ``explicit_fallback``(节点显式
|
||||
传入,如 researcher 的 keyword_model/rank_model)> ``state.model_name``
|
||||
(用户写作表单选的主模型)> configurable.model_name。
|
||||
|
||||
全部为空时**不再抛错**(旧实现 strict=True 抛 ValueError 会把「未选模型」直接打成
|
||||
草稿失败,且后续 pause_draft 还会因 current_draft 为空二次崩溃):统一返回 None,
|
||||
由 create_chat_model 回退 config.yaml 的 ``models[0]``。``strict`` 仅用于区分日志
|
||||
级别——大纲/写作/审核阶段缺模型属异常配置,记 warning,调用方据 None 向用户提示
|
||||
「已用默认模型」。
|
||||
"""
|
||||
candidates = (
|
||||
(getattr(agent_cfg, "model", None) or "") if agent_cfg else "",
|
||||
explicit_fallback or "",
|
||||
(state or {}).get("model_name") or "",
|
||||
(config.get("configurable", {}) or {}).get("model_name") or "",
|
||||
)
|
||||
for name in candidates:
|
||||
if isinstance(name, str) and name.strip():
|
||||
return name.strip()
|
||||
if strict:
|
||||
logger.warning("写作模型未指定(state/configurable/agent.model 均为空),回退默认模型")
|
||||
return None
|
||||
|
||||
|
||||
def parse_marker_json(text: str, marker: str, fallback: dict) -> dict:
|
||||
"""从 agent 输出中按「marker + ```json``` 块」契约解析 JSON,多级降级。
|
||||
|
||||
1. ``marker\\n```json {...} ``` ``(SOUL 规定的标准形态,正则仿
|
||||
intent.py::_INTENT_READY_PATTERN);
|
||||
2. marker 存在但没包代码块 → 取 marker 之后的全部文本交给 parse_json;
|
||||
3. marker 缺失(弱模型漏输出)→ 整段文本交给 parse_json;
|
||||
4. 全部失败 → fallback。
|
||||
"""
|
||||
if not text or not text.strip():
|
||||
return fallback
|
||||
|
||||
pattern = re.compile(re.escape(marker) + r"\s*```(?:json)?\s*(\{.*?\})\s*```", re.DOTALL)
|
||||
m = pattern.search(text)
|
||||
if m:
|
||||
result = parse_json(m.group(1), fallback)
|
||||
if result is not fallback:
|
||||
return result
|
||||
|
||||
idx = text.find(marker)
|
||||
if idx != -1:
|
||||
tail = text[idx + len(marker):]
|
||||
result = parse_json(tail, fallback)
|
||||
if result is not fallback:
|
||||
return result
|
||||
|
||||
return parse_json(text, fallback)
|
||||
|
||||
|
||||
async def stream_writing_agent(
|
||||
*,
|
||||
agent_id: str,
|
||||
user_payload: str,
|
||||
state: dict,
|
||||
config: RunnableConfig,
|
||||
on_text: Callable[[str, bool], None] | None = None,
|
||||
explicit_model_fallback: str | None = None,
|
||||
strict_model: bool = True,
|
||||
disable_thinking: bool = False,
|
||||
) -> str:
|
||||
"""在「SOUL 作 system prompt + 单条 Human 输入」契约下流式运行一个写作 agent。
|
||||
|
||||
Args:
|
||||
agent_id: 内置写作 agent id(STAGE_AGENT_IDS 的值之一)。
|
||||
user_payload: 本阶段输入(意图/素材/大纲/草稿等拼成的一段文本)。
|
||||
on_text: 每个流式片段回调 ``(text, is_thinking)``,调用方在回调里把
|
||||
chunk 包装成本阶段的 custom 事件(outline_chunk / draft_chunk /
|
||||
review_chunk ...)推给前端;None = 不透传(如 researcher 的辅助任务)。
|
||||
explicit_model_fallback / strict_model: 见 resolve_agent_model_name。
|
||||
disable_thinking: True 时强制关闭模型思考(force_disable_thinking),用于
|
||||
素材收集/大纲整理这类不需要思考的阶段(GLM5 等爱出 <think>)。关不掉的
|
||||
模型仍由下方 ThinkTagParser 把思考分流到思考框,不会泄进正文。
|
||||
|
||||
Returns:
|
||||
累积的**正文**文本(<think> 思考与 reasoning_content 已剥离),供
|
||||
parse_marker_json / 章节拼装使用。
|
||||
|
||||
Raises:
|
||||
WritingAgentUnavailable: SOUL 缺失(调用方回退旧 prompt 路径)。
|
||||
其余模型异常原样上抛,由调用方按既有 try/except 兜底成 status:error。
|
||||
"""
|
||||
agent_cfg, soul = load_writing_agent(agent_id)
|
||||
model_name = resolve_agent_model_name(
|
||||
agent_cfg, state, config,
|
||||
explicit_fallback=explicit_model_fallback,
|
||||
strict=strict_model,
|
||||
)
|
||||
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
model = create_chat_model(name=model_name, force_disable_thinking=disable_thinking)
|
||||
|
||||
full_text = ""
|
||||
parser = ThinkTagParser()
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=soul),
|
||||
HumanMessage(content=user_payload),
|
||||
]):
|
||||
# 推理模型的思考走 reasoning_content(content 为空):作为思考实时推送
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning and on_text is not None:
|
||||
on_text(reasoning, True)
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
if on_text is not None:
|
||||
on_text(text, is_thinking)
|
||||
return full_text
|
||||
|
||||
|
||||
async def run_writing_agent(
|
||||
*,
|
||||
agent_id: str,
|
||||
user_payload: str,
|
||||
marker: str,
|
||||
fallback: dict,
|
||||
state: dict,
|
||||
config: RunnableConfig,
|
||||
on_text: Callable[[str, bool], None] | None = None,
|
||||
explicit_model_fallback: str | None = None,
|
||||
strict_model: bool = True,
|
||||
disable_thinking: bool = False,
|
||||
) -> dict:
|
||||
"""跑一个写作 agent 并按 marker 契约解析其结构化输出(失败回 fallback)。
|
||||
|
||||
模型调用异常时与 ``_utils.llm_json`` 同口径:返回
|
||||
``{**fallback, "_llm_error": str(e)}``,不上抛(SOUL 缺失除外——
|
||||
WritingAgentUnavailable 原样上抛,由节点切回旧 prompt 路径)。
|
||||
|
||||
disable_thinking: 透传给 stream_writing_agent,强制关闭思考(见其文档)。
|
||||
"""
|
||||
try:
|
||||
full_text = await stream_writing_agent(
|
||||
agent_id=agent_id,
|
||||
user_payload=user_payload,
|
||||
state=state,
|
||||
config=config,
|
||||
on_text=on_text,
|
||||
explicit_model_fallback=explicit_model_fallback,
|
||||
strict_model=strict_model,
|
||||
disable_thinking=disable_thinking,
|
||||
)
|
||||
except WritingAgentUnavailable:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("run_writing_agent(%s) failed: %s", agent_id, e, exc_info=True)
|
||||
return {**fallback, "_llm_error": str(e)}
|
||||
return parse_marker_json(full_text, marker, fallback)
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
"""AI 写作模型自动容错(按 ``config.yaml models[]`` 顺序换下一个模型继续)。
|
||||
|
||||
与多智能体会商 ``app/gateway/roundtable_model_fallback.py`` 同思路,但**只放在 harness 层**
|
||||
避免 ``app→harness`` 单向依赖被反向打破:
|
||||
|
||||
- ``fallback_chain(current)``:候选模型链 ``[当前, …其余]``(按 config.yaml 顺序,去重保序);
|
||||
- ``classify_exception(exc)``:把异常归到一个用户可读的失败原因码;
|
||||
- ``reason_text(reason)``:原因码 → 简短中文(用于「『X』模型 <原因>,已切换『Y』继续」);
|
||||
- ``emit_model_switch_event(writer, ...)``:把 ``model_switch`` 推到 LangGraph custom 通道
|
||||
供前端时间线展示;
|
||||
- ``build_switch_progress_event(...)``:节点 return 的 ``progress_events`` 里同样塞一份
|
||||
(走 updates 通道,前端 reducer 据此刷新缓冲并落到时间线)。
|
||||
|
||||
总开关 / 上限沿用会商的 env:
|
||||
- ``ROUNDTABLE_MODEL_FALLBACK``:默认开;``0/false/off/no`` 关闭(链只含当前模型);
|
||||
- ``ROUNDTABLE_MODEL_FALLBACK_MAX``:候选数上限(含当前),0/未设 = 不限。
|
||||
|
||||
放在 ai_writing 节点目录下而非 harness/agents/middlewares:本模块不是中间件,
|
||||
且仅服务 ai_writing 的几个流式节点;如果未来圆桌也想下沉合并,再统一拎出去。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Callable
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_REASON_TEXT: dict[str, str] = {
|
||||
"quota": "额度不足/账单异常",
|
||||
"auth": "鉴权失败/无访问权限",
|
||||
"busy": "服务繁忙",
|
||||
"transient": "网络/连接异常",
|
||||
"upstream": "上游响应异常",
|
||||
"config": "模型未配置或配置异常",
|
||||
"exception": "调用异常",
|
||||
"generic": "调用失败",
|
||||
}
|
||||
|
||||
|
||||
def reason_text(reason: str | None) -> str:
|
||||
"""原因码 → 简短中文(未知码 → 「调用失败」)。"""
|
||||
if not reason:
|
||||
return "调用失败"
|
||||
return _REASON_TEXT.get(reason, "调用失败")
|
||||
|
||||
|
||||
_QUOTA_PATTERNS = ("insufficient_quota", "quota", "billing", "credit", "payment",
|
||||
"余额不足", "超出限额", "额度不足", "欠费")
|
||||
_AUTH_PATTERNS = ("authentication", "unauthorized", "invalid api key", "invalid_api_key",
|
||||
"permission", "forbidden", "access denied", "无权", "未授权")
|
||||
_BUSY_PATTERNS = ("server busy", "temporarily unavailable", "try again later", "please retry",
|
||||
"please try again", "overloaded", "high demand", "rate limit",
|
||||
"负载较高", "服务繁忙", "稍后重试", "请稍后重试")
|
||||
_UPSTREAM_PATTERNS = ("bad_response_status_code", "bad response status",
|
||||
"expecting ',' delimiter", "expecting value", "expecting property name",
|
||||
"unexpected character", "unterminated string", "jsondecodeerror")
|
||||
_TRANSIENT_EXC_NAMES = {"APITimeoutError", "APIConnectionError", "InternalServerError",
|
||||
"ReadError", "RemoteProtocolError", "ReadTimeout", "ConnectTimeout", "TimeoutError"}
|
||||
|
||||
|
||||
def classify_exception(exc: BaseException) -> str:
|
||||
"""异常 → 失败原因码(quota/auth/busy/transient/upstream/config/generic/exception)。
|
||||
|
||||
与 ``LLMErrorHandlingMiddleware._classify_error`` 同口径,简化版:不区分是否可重试
|
||||
(ai_writing 的容错只换模型,不在同模型上重试)。``create_chat_model`` 抛
|
||||
``ValueError("Model X not found in config")`` 这类配置缺失另归 ``config``。
|
||||
"""
|
||||
name = exc.__class__.__name__
|
||||
detail = str(exc).lower()
|
||||
|
||||
if isinstance(exc, ValueError) and "not found in config" in detail:
|
||||
return "config"
|
||||
if any(p in detail for p in _QUOTA_PATTERNS):
|
||||
return "quota"
|
||||
if any(p in detail for p in _AUTH_PATTERNS):
|
||||
return "auth"
|
||||
if name in _TRANSIENT_EXC_NAMES:
|
||||
return "transient"
|
||||
if any(p in detail for p in _BUSY_PATTERNS):
|
||||
return "busy"
|
||||
if any(p in detail for p in _UPSTREAM_PATTERNS):
|
||||
return "upstream"
|
||||
return "exception"
|
||||
|
||||
|
||||
def _fallback_enabled() -> bool:
|
||||
raw = (os.getenv("ROUNDTABLE_MODEL_FALLBACK", "1") or "1").strip().lower()
|
||||
return raw not in {"0", "false", "no", "off"}
|
||||
|
||||
|
||||
def _max_attempts() -> int:
|
||||
try:
|
||||
return max(0, int(os.getenv("ROUNDTABLE_MODEL_FALLBACK_MAX", "0") or "0"))
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def _config_model_names() -> list[str]:
|
||||
try:
|
||||
from deerflow.config import get_app_config
|
||||
cfg = get_app_config()
|
||||
return [m.name for m in (cfg.models or []) if getattr(m, "name", None)]
|
||||
except Exception: # noqa: BLE001 — 拿不到配置(测试桩)时退化为空
|
||||
logger.debug("ai_writing fallback_chain: get_app_config().models unavailable", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
def fallback_chain(current: str | None) -> list[str]:
|
||||
"""``[当前模型, …config.yaml 其余模型]``(去重保序)。
|
||||
|
||||
- ``current`` 为空 → 链头落到 ``models[0]``;
|
||||
- ``current`` 在配置内 → 排第一,余者按配置顺序补齐;
|
||||
- ``current`` 不在配置内 → 仍排第一(让 ``create_chat_model`` 在第一轮按原行为抛,
|
||||
之后再换模型),再补齐全部配置模型;
|
||||
- 总开关关闭 → 只返回头一个;
|
||||
- 拿不到配置 → ``[current]``(current 为空则空列表)。
|
||||
"""
|
||||
all_names = _config_model_names()
|
||||
cur = (current or "").strip()
|
||||
ordered: list[str] = []
|
||||
if cur:
|
||||
ordered.append(cur)
|
||||
for n in all_names:
|
||||
if n and n not in ordered:
|
||||
ordered.append(n)
|
||||
if not ordered:
|
||||
return [cur] if cur else []
|
||||
if not _fallback_enabled():
|
||||
return ordered[:1]
|
||||
cap = _max_attempts()
|
||||
if cap > 0:
|
||||
ordered = ordered[:cap]
|
||||
return ordered
|
||||
|
||||
|
||||
def build_switch_progress_event(
|
||||
*,
|
||||
agent_name: str,
|
||||
stage: str,
|
||||
failed_model: str,
|
||||
next_model: str,
|
||||
reason: str,
|
||||
section_index: int | None = None,
|
||||
section_title: str | None = None,
|
||||
) -> dict:
|
||||
"""构造一条 ``model_switch`` ``progress_event`` 字典(供节点 return 时塞进 ``progress_events`` 数组)。
|
||||
|
||||
``stage`` ∈ {intent, outline, draft, review}:前端 reducer 据此清掉对应阶段的流式缓冲,
|
||||
避免半段输出被下一个模型的输出粘在一起。
|
||||
"""
|
||||
fm = (failed_model or "").strip() or "当前模型"
|
||||
nm = (next_model or "").strip() or "下一个模型"
|
||||
rt = reason_text(reason)
|
||||
detail = f"「{agent_name}」所用模型「{fm}」{rt},已自动切换到「{nm}」继续。"
|
||||
payload: dict[str, Any] = {
|
||||
"type": "model_switch",
|
||||
"agent_name": agent_name,
|
||||
"title": "模型容错",
|
||||
"detail": detail,
|
||||
"message": detail,
|
||||
"stage": stage,
|
||||
"failed_model": fm,
|
||||
"next_model": nm,
|
||||
"reason": reason,
|
||||
}
|
||||
if section_index is not None:
|
||||
payload["section_index"] = section_index
|
||||
if section_title:
|
||||
payload["section_title"] = section_title
|
||||
return payload
|
||||
|
||||
|
||||
def emit_model_switch_event(
|
||||
writer: Callable[[dict], None] | None,
|
||||
*,
|
||||
agent_name: str,
|
||||
stage: str,
|
||||
failed_model: str,
|
||||
next_model: str,
|
||||
reason: str,
|
||||
section_index: int | None = None,
|
||||
section_title: str | None = None,
|
||||
) -> None:
|
||||
"""把 ``model_switch`` 事件推到 LangGraph custom 通道(前端 useStream onCustomEvent 收)。"""
|
||||
if writer is None:
|
||||
return
|
||||
payload = build_switch_progress_event(
|
||||
agent_name=agent_name, stage=stage,
|
||||
failed_model=failed_model, next_model=next_model, reason=reason,
|
||||
section_index=section_index, section_title=section_title,
|
||||
)
|
||||
try:
|
||||
writer(payload)
|
||||
except Exception: # noqa: BLE001 — writer 在无 runtime 时是 no-op,理论不抛
|
||||
logger.debug("emit_model_switch_event: writer raised", exc_info=True)
|
||||
+671
@@ -0,0 +1,671 @@
|
||||
"""像「智能体调用技能」那样用一个技能检索素材。
|
||||
|
||||
背景:素材收集专家(ai-writing-researcher)是内置智能体,但此前它「用技能检索」只是
|
||||
拿技能的 ``description`` 拼一个 query 丢给通用 web/intranet 检索 provider——技能自己
|
||||
SKILL.md 里写的检索说明 / 策略并没有真正跑起来,和真实智能体「读 SKILL.md → 按说明
|
||||
调工具检索」的逻辑不一致。
|
||||
|
||||
本模块把两者对齐:把技能 **SKILL.md 全文**连同素材收集专家 SOUL 注入 system prompt,
|
||||
给模型绑定一个 ``search`` 工具,跑一个**有界的工具调用循环**——模型按技能说明自行决定
|
||||
检索词与轮次(就像智能体读了技能后驱动检索),我们直接**收割工具返回的 SearchResult**
|
||||
作为素材(无需再从自由文本里抽取,检索 provider 的结果本就是结构化素材)。
|
||||
|
||||
仅在 ``config.yaml → ai_writing.researcher_skill_agent`` 打开时启用(默认 false,走旧的
|
||||
description→检索 直连路径)。弱模型不会工具调用 / 检索失败时,调用方回退旧路径,保证
|
||||
素材不为空。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ._agent_runner import (
|
||||
RESEARCHER_AGENT_ID,
|
||||
WritingAgentUnavailable,
|
||||
load_writing_agent,
|
||||
resolve_agent_model_name,
|
||||
)
|
||||
from ._utils import get_ai_writing_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 工具调用循环上限(每轮 = 一次模型调用,可含多个 search 调用)。
|
||||
_MAX_TURNS = 4
|
||||
|
||||
|
||||
def use_skill_search_agent() -> bool:
|
||||
"""读 ``config.yaml → ai_writing.researcher_skill_agent`` 开关(默认 false,mtime 热加载)。"""
|
||||
cfg = get_ai_writing_config()
|
||||
return bool(cfg.get("researcher_skill_agent", False)) if isinstance(cfg, dict) else False
|
||||
|
||||
|
||||
# ── 用真实智能体运行时跑技能(和「别的智能体调用技能」完全一致)────────────────────
|
||||
#
|
||||
# 旧的 run_skill_search_agent 只给模型绑了一个写死路由到 DDG 的通用 search 工具,技能
|
||||
# SKILL.md 里要用的真实工具(read_file / bash / 内网检索工具 / MCP)根本不存在,所以
|
||||
# 内网技能跑不出东西。本函数改为复用 **SubagentExecutor**(与 lead agent / 子智能体同一套
|
||||
# 运行时):加载技能 SKILL.md(skills 白名单)+ 全套工具 + 懒加载沙箱,让技能按其说明真正
|
||||
# 检索,再让模型把检索到的真实素材按 JSON 契约输出,我们解析成结构化素材。
|
||||
#
|
||||
# 单次工具调用循环上限(防跑飞)。
|
||||
_REAL_AGENT_MAX_TURNS = 16
|
||||
|
||||
# 真实智能体跑技能:整段运行(读 SKILL.md + 多轮工具调用)的超时上限,**硬编码 5 分钟**。
|
||||
# 内网检索很慢、技能常做多次检索,单次直连检索的 120s 不够整段用;5 分钟足够整段跑完
|
||||
# (含模型对超时检索词自行换简单词重试),不依赖 env(部署方不一定会配环境变量)。
|
||||
_REAL_AGENT_TIMEOUT_SECONDS = 300
|
||||
|
||||
_MATERIAL_OUTPUT_CONTRACT = (
|
||||
"你的任务:用上面挂载的检索技能(<skill> 里的 SKILL.md)按它的说明**实际执行检索**,"
|
||||
"收集与「写作意图」相关的真实素材。\n"
|
||||
"\n"
|
||||
"【第一步 · 必须真正执行技能,绝不能跳过】\n"
|
||||
"- 你**必须先实际调用工具去检索**,再谈输出。技能通常自带脚本——按 SKILL.md 的说明用 **bash** 运行它,"
|
||||
"例如:`cd <技能目录> && python3 scripts/xxx.py \"检索词\"`(具体脚本名/参数以 SKILL.md 为准),"
|
||||
"或调用它说明里的内网接口 / MCP 工具。\n"
|
||||
"- **严禁不调用任何工具就直接输出 JSON**,更不许凭记忆编造素材。哪怕你“觉得”知道答案,也必须真的去检索。\n"
|
||||
"- 对每个检索方向各执行一次;可多次调用工具覆盖不同角度。\n"
|
||||
"- 某个检索词返回 0 条或报「请求超时」时**不要放弃**:换成更核心、更短的检索词再试一次"
|
||||
"(去掉「竞选/诉讼/政策/动向」等后缀,只留核心实体,如「特朗普竞选」→「特朗普」)。\n"
|
||||
"\n"
|
||||
"【第二步 · 检索完成后再整理输出】\n"
|
||||
"- 把工具**实际返回**的真实素材整理成一个 JSON 对象(不要解释、不要 markdown 代码块、用英文双引号):\n"
|
||||
'{"materials": [{"title": "标题", "content": "素材正文摘录(尽量保留原文, <=500字)", "source": "来源", "url": "原文链接(可空)", "published_at": "日期(可空)"}]}\n'
|
||||
"- content 必须来自工具实际返回的内容;只要有任何检索词拿到数据就**全部收下**,不要因别的检索词没结果而丢弃。\n"
|
||||
"- 只有当你**确实执行了检索、且所有检索词(含简化重试)都没有结果**时,才输出 {\"materials\": []}。"
|
||||
)
|
||||
|
||||
|
||||
def _scan_balanced_json_objects(text: str) -> list:
|
||||
"""从可能被**中间截断**的文本里扫出所有「括号平衡且完整」的 ``{...}`` 子串(含各嵌套层级)。
|
||||
|
||||
根因:bash 工具对超过 ~20000 字的输出做**中间截断**(插入「... [middle truncated …] ...」),
|
||||
使整段大 JSON(如 count=30 的技能返回)无法解析;但被截断点**两侧的单条结果对象**通常仍是
|
||||
完整合法的 JSON,可逐个抢救出来。带字符串状态机(正确处理引号内的 {}/转义),逐个返回闭合的
|
||||
对象子串,调用方再逐个 json.loads + 字段识别。
|
||||
"""
|
||||
import json as _json
|
||||
|
||||
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 _describe_tool_output(raw: str, skill_name: str) -> str:
|
||||
"""从单次技能输出里提炼一句步骤条文案:**检索词 + 本次命中条数**(用户要求实时显示每次拿到多少条)。
|
||||
|
||||
返回空串 = 这次工具输出不是可识别的检索结果(如 read_file 读 SKILL.md),不发步骤。
|
||||
"""
|
||||
import re as _re
|
||||
|
||||
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 汇总)。
|
||||
|
||||
**复用主智能体「参考文献」模式同一套久经考验的提取逻辑**(`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:
|
||||
if not isinstance(raw, str):
|
||||
continue
|
||||
payload = _ref._parse_json_like(raw)
|
||||
# 明确标记失败的(超时 / 报错)跳过,但不影响其它成功的输出
|
||||
if isinstance(payload, dict) and payload.get("success") is False:
|
||||
continue
|
||||
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
|
||||
# 用参考文献模式的归一化(含字段并集 + 每技能展示配置),与右侧参考文献完全一致
|
||||
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
|
||||
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(norm.get("score"))
|
||||
except (TypeError, ValueError):
|
||||
score = 0.75
|
||||
harvested.append(SearchResult(
|
||||
title=title or "(无标题)",
|
||||
content=content[:500],
|
||||
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",
|
||||
recUuid=rec,
|
||||
))
|
||||
if len(harvested) >= limit:
|
||||
return harvested
|
||||
return harvested
|
||||
|
||||
|
||||
def _summarize_tool_args(args: dict) -> str:
|
||||
"""把工具调用参数压成一句简短摘要,放进步骤条文案(取最能代表本次调用的字段)。"""
|
||||
if isinstance(args, dict):
|
||||
for key in ("command", "query", "path", "file_path", "pattern", "name", "input", "url"):
|
||||
v = args.get(key)
|
||||
if isinstance(v, str) and v.strip():
|
||||
return v.strip()[:80]
|
||||
try:
|
||||
import json as _json
|
||||
|
||||
return _json.dumps(args, ensure_ascii=False)[:80]
|
||||
except Exception: # noqa: BLE001
|
||||
return str(args)[:80]
|
||||
return str(args)[:80]
|
||||
|
||||
|
||||
async def run_skill_via_real_agent(
|
||||
*,
|
||||
skill,
|
||||
intent: str,
|
||||
keywords: list[str],
|
||||
user_query: str,
|
||||
config: RunnableConfig,
|
||||
state: dict,
|
||||
model_name_fallback: str | None,
|
||||
max_results: int,
|
||||
timeout_s: int,
|
||||
emit_query=None,
|
||||
) -> tuple[list, str]:
|
||||
"""用真实智能体运行时(SubagentExecutor)跑一个检索技能,收割结构化素材。
|
||||
|
||||
与「别的智能体调用技能」一致:技能 SKILL.md + 全套工具 + 沙箱都齐全,技能的真实检索
|
||||
逻辑(读文件 / 调内网接口 / MCP)能真正执行。模型最终按 JSON 契约吐出素材,解析成
|
||||
SearchResult。返回 ``(results, error_detail)``:results 为空且 error 非空 = 该技能失败
|
||||
(researcher 据此回退直连检索)。
|
||||
"""
|
||||
from langchain_core.messages import AIMessage, ToolMessage
|
||||
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.subagents.config import SubagentConfig
|
||||
from deerflow.subagents.executor import SubagentExecutor
|
||||
from deerflow.tools import get_available_tools
|
||||
|
||||
from ..search.base import SearchResult
|
||||
from ._agent_runner import RESEARCHER_AGENT_ID, WritingAgentUnavailable, load_writing_agent
|
||||
from ._utils import _content_to_text, parse_json
|
||||
|
||||
skill_name = getattr(skill, "name", "") or "技能"
|
||||
|
||||
try:
|
||||
agent_cfg, soul = load_writing_agent(RESEARCHER_AGENT_ID)
|
||||
except WritingAgentUnavailable:
|
||||
agent_cfg, soul = None, ""
|
||||
model_name = resolve_agent_model_name(
|
||||
agent_cfg, state, config, explicit_fallback=model_name_fallback, strict=False
|
||||
)
|
||||
|
||||
try:
|
||||
app_config = get_app_config()
|
||||
tools = get_available_tools(
|
||||
model_name=model_name, subagent_enabled=False, include_mcp=True, app_config=app_config
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("skill real-agent[%s]: build tools failed: %s", skill_name, e)
|
||||
return [], f"工具初始化失败: {e}"
|
||||
|
||||
# 技能在容器内的目录:注入 SKILL.md 时模型只有内容、不知道脚本在哪,会用相对路径
|
||||
# (python3 scripts/xxx.py)在错误的 CWD 下跑 → No such file。把绝对目录告诉它,让它
|
||||
# 先 cd 进去再执行(与「别的智能体」里 skill_view/read_file 拿到 location 后的行为一致)。
|
||||
try:
|
||||
base_path = getattr(getattr(app_config, "skills", None), "container_path", None) or "/mnt/skills"
|
||||
except Exception: # noqa: BLE001
|
||||
base_path = "/mnt/skills"
|
||||
try:
|
||||
skill_dir = skill.get_container_path(base_path)
|
||||
except Exception: # noqa: BLE001
|
||||
skill_dir = ""
|
||||
try:
|
||||
skill_file_path = skill.get_container_file_path(base_path)
|
||||
except Exception: # noqa: BLE001
|
||||
skill_file_path = f"{skill_dir}/SKILL.md" if skill_dir else ""
|
||||
skill_loc_hint = (
|
||||
f"\n\n【技能目录(重要)】技能「{skill_name}」在容器内的目录是:{skill_dir}\n"
|
||||
f"- 技能说明文件是:{skill_file_path}\n"
|
||||
f"- 运行该技能自带的脚本时,**务必先 `cd {skill_dir}` 再执行**"
|
||||
f"(例如 `cd {skill_dir} && python3 scripts/xxx.py ...`),或对脚本用绝对路径;\n"
|
||||
"- **不要用相对路径**(如 `python3 scripts/xxx.py`),否则当前目录不对会报 No such file。"
|
||||
) if skill_dir else ""
|
||||
|
||||
system_prompt = (soul + "\n\n" if soul else "") + _MATERIAL_OUTPUT_CONTRACT + skill_loc_hint
|
||||
|
||||
task_lines = [f"写作意图:{intent or '(无)'}"]
|
||||
if user_query:
|
||||
task_lines.append(f"用户补充检索意图:{user_query}")
|
||||
if keywords:
|
||||
task_lines.append(f"可参考的检索方向:{', '.join(keywords[:8])}")
|
||||
task_lines.append(f"目标素材数量约 {max_results} 条。")
|
||||
if skill_file_path or skill_dir:
|
||||
task_lines.extend([
|
||||
"",
|
||||
"【必须使用技能(弱模型也必须遵守)】",
|
||||
f"- 当前指定技能:{skill_name}",
|
||||
f"- SKILL.md 路径:{skill_file_path or '(见上方 <skill> 内容)'}",
|
||||
f"- 技能目录:{skill_dir or '(未知)'}",
|
||||
"- 你的下一步必须先调用 `read_file` 读取这个 SKILL.md(即使上方已有技能摘要,也要读取完整说明),"
|
||||
"再按 SKILL.md 的脚本/接口流程获取真实数据。",
|
||||
"- 如果 SKILL.md 提到 `scripts/*.py`,必须调用 `bash` 实际运行脚本;优先使用 "
|
||||
f"`cd {skill_dir or '<技能目录>'} && python scripts/xxx.py ...` 或 "
|
||||
f"`cd {skill_dir or '<技能目录>'} && python3 scripts/xxx.py ...`。",
|
||||
"- 禁止第一轮就直接输出 JSON;没有执行过 read_file 和技能要求的脚本/工具前,不能输出 `materials`。",
|
||||
])
|
||||
# 弱模型强提示:明确要求「先动手执行技能脚本」,避免它直接吐空 JSON 不检索。
|
||||
task_lines.append(
|
||||
f"现在请**立刻开始执行检索**:按上面挂载技能的 SKILL.md,用 bash 运行它的脚本"
|
||||
f"(如 `cd {skill_dir or '<技能目录>'} && python3 scripts/...py \"检索词\"`)实际检索,"
|
||||
"先执行工具拿到真实数据,再整理输出 JSON;不要不检索就直接输出。"
|
||||
)
|
||||
task = "\n".join(task_lines)
|
||||
|
||||
sub_cfg = SubagentConfig(
|
||||
name=f"ai-writing-skill-{skill_name}",
|
||||
description="AI 写作素材检索",
|
||||
system_prompt=system_prompt,
|
||||
tools=None, # 继承全套工具,技能按 SKILL.md 自行取用
|
||||
disallowed_tools=["task"], # 素材检索不需要再派生子任务
|
||||
skills=[skill_name], # 仅加载这个技能的 SKILL.md
|
||||
model=model_name or "inherit",
|
||||
max_turns=_REAL_AGENT_MAX_TURNS,
|
||||
# 整段运行硬编码至少 5 分钟(_REAL_AGENT_TIMEOUT_SECONDS),内网慢检索不被过早切断;
|
||||
# 调用方传入更大的 timeout_s 时以更大者为准。
|
||||
timeout_seconds=max(timeout_s, _REAL_AGENT_TIMEOUT_SECONDS),
|
||||
)
|
||||
|
||||
# 复用 SubagentExecutor 的 agent/state 构建(模型 + 中间件 + 沙箱 + 技能注入),但**不**走
|
||||
# 它阻塞式的 execute()。
|
||||
thread_id = (config.get("configurable") or {}).get("thread_id")
|
||||
try:
|
||||
executor = SubagentExecutor(
|
||||
config=sub_cfg, tools=tools, app_config=app_config,
|
||||
parent_model=model_name, thread_id=thread_id,
|
||||
)
|
||||
agent = executor._create_agent()
|
||||
init_state = await executor._build_initial_state(task)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("skill real-agent[%s]: init failed: %s", skill_name, e)
|
||||
return [], f"运行时初始化失败: {e}"
|
||||
|
||||
# recursion_limit 计的是 graph 超级步(每个「模型→工具」回合≈2 步),不能直接等于 max_turns,
|
||||
# 否则模型做了 N 个检索词后还没来得及写最终汇总就被 GraphRecursionError 截断(实测 10 个
|
||||
# 检索词≈20 步 > 16,结果汇总丢失)。给到 ~2×turns + 余量,让模型正常情况下能写完汇总。
|
||||
run_config: dict = {
|
||||
"recursion_limit": _REAL_AGENT_MAX_TURNS * 2 + 4,
|
||||
"configurable": {"agent_id": RESEARCHER_AGENT_ID},
|
||||
}
|
||||
run_context: dict = {"agent_id": RESEARCHER_AGENT_ID, "agent_name": RESEARCHER_AGENT_ID}
|
||||
if thread_id:
|
||||
run_config["configurable"]["thread_id"] = thread_id
|
||||
run_context["thread_id"] = thread_id
|
||||
if app_config is not None:
|
||||
run_context["app_config"] = app_config
|
||||
|
||||
# 首步:技能开始(SKILL.md 已注入,给一条起始进度)
|
||||
if emit_query:
|
||||
emit_query(f"读取技能「{skill_name}」的 SKILL.md,开始执行检索…")
|
||||
|
||||
# ── 实时步骤的关键 ──
|
||||
# 嵌套 agent.astream 跑在**后台任务**里,只把工具调用塞进队列;真正的 emit_query(writer)
|
||||
# 由本协程(researcher 节点自身的协程)从队列里取出来调——和 draft_chunk 同一上下文。
|
||||
# 这样 custom 事件能被 LangGraph **实时**刷给前端 SSE;若在嵌套 astream 里直接 emit,
|
||||
# 事件会被攒到整段跑完才一次性吐出(后端在跑、前端却一直 loading)。
|
||||
final_text = ""
|
||||
# 技能每次工具调用的 stdout(如 search.py 打印的 {success,results} JSON),用于**直接收割**
|
||||
# 真实检索结果,不依赖模型最后是否把它们汇总进 {materials}。
|
||||
tool_outputs: list[str] = []
|
||||
seen_msg_ids: set[str] = set()
|
||||
step_queue: asyncio.Queue = asyncio.Queue()
|
||||
|
||||
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"
|
||||
):
|
||||
for m in chunk.get("messages") or []:
|
||||
mid = getattr(m, "id", None)
|
||||
if not mid or mid in seen_msg_ids:
|
||||
continue
|
||||
seen_msg_ids.add(mid)
|
||||
for tc in (getattr(m, "tool_calls", None) or []):
|
||||
tname = (tc.get("name") or "").strip() or "工具"
|
||||
step_queue.put_nowait(
|
||||
f"技能「{skill_name}」调用 {tname}:{_summarize_tool_args(tc.get('args') or {})}"
|
||||
)
|
||||
if isinstance(m, AIMessage):
|
||||
text = _content_to_text(m.content)
|
||||
if text.strip():
|
||||
final_text = text
|
||||
elif isinstance(m, ToolMessage):
|
||||
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 收尾
|
||||
|
||||
task = asyncio.create_task(_drive())
|
||||
|
||||
async def _pump() -> None:
|
||||
while True:
|
||||
item = await step_queue.get()
|
||||
if item is None:
|
||||
break
|
||||
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 / 抛出的异常
|
||||
try:
|
||||
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}"
|
||||
|
||||
return _finalize(), ""
|
||||
|
||||
|
||||
def _read_skill_body(skill) -> str:
|
||||
"""读技能 SKILL.md 全文;失败回退 name+description。"""
|
||||
try:
|
||||
return skill.skill_file.read_text(encoding="utf-8")
|
||||
except Exception: # noqa: BLE001
|
||||
return f"# 技能:{getattr(skill, 'name', '')}\n{getattr(skill, 'description', '') or ''}"
|
||||
|
||||
|
||||
async def run_skill_search_agent(
|
||||
*,
|
||||
skill,
|
||||
intent: str,
|
||||
keywords: list[str],
|
||||
user_query: str,
|
||||
config: RunnableConfig,
|
||||
state: dict,
|
||||
model_name_fallback: str | None,
|
||||
max_results: int,
|
||||
timeout_s: int,
|
||||
emit_query=None,
|
||||
) -> tuple[list, str]:
|
||||
"""像智能体一样按技能 SKILL.md 检索素材。
|
||||
|
||||
Returns ``(results, error_detail)``:results 为收割到的 SearchResult 列表(调用方再
|
||||
去全局去重/合并);error_detail 非空且 results 为空 = 该技能整体失败(触发兜底)。
|
||||
模型不支持工具调用 / 始终不发起检索时返回 ``([], "")``,调用方据此回退旧检索路径。
|
||||
"""
|
||||
from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage
|
||||
from langchain_core.tools import tool
|
||||
|
||||
from ..search import get_search_provider
|
||||
|
||||
skill_name = getattr(skill, "name", "") or "技能"
|
||||
skill_body = _read_skill_body(skill)
|
||||
|
||||
try:
|
||||
agent_cfg, soul = load_writing_agent(RESEARCHER_AGENT_ID)
|
||||
except WritingAgentUnavailable:
|
||||
agent_cfg, soul = None, ""
|
||||
|
||||
model_name = resolve_agent_model_name(
|
||||
agent_cfg, state, config, explicit_fallback=model_name_fallback, strict=False
|
||||
)
|
||||
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
try:
|
||||
model = create_chat_model(name=model_name)
|
||||
except Exception as e: # noqa: BLE001
|
||||
return [], f"模型创建失败: {e}"
|
||||
|
||||
@tool
|
||||
def search(query: str) -> str: # noqa: ARG001 — 仅用于给模型生成 schema,真正检索在循环里执行
|
||||
"""根据 query 检索素材。一次只传一个检索词;可多次调用以覆盖不同角度。"""
|
||||
return ""
|
||||
|
||||
try:
|
||||
model_tools = model.bind_tools([search])
|
||||
except Exception as e: # noqa: BLE001 — 模型不支持工具调用
|
||||
logger.info("skill agent: model lacks tool-calling, fallback. err=%s", e)
|
||||
return [], ""
|
||||
|
||||
provider = get_search_provider(get_ai_writing_config())
|
||||
|
||||
system = (
|
||||
(soul + "\n\n" if soul else "")
|
||||
+ "你现在挂载了下面这个检索技能,请**严格按它的说明**来检索素材。\n"
|
||||
"通过调用 search 工具执行检索(一次一个检索词,可多次调用覆盖不同角度/来源);\n"
|
||||
"收集到足够素材后直接回复「检索完成」即可,不要编造任何内容。\n\n"
|
||||
f"=== 技能:{skill_name} ===\n{skill_body}"
|
||||
)
|
||||
task_lines = [f"写作意图:{intent or '(无)'}"]
|
||||
if user_query:
|
||||
task_lines.append(f"用户补充:{user_query}")
|
||||
if keywords:
|
||||
task_lines.append(f"可参考的检索方向:{', '.join(keywords[:8])}")
|
||||
task_lines.append(f"请按上面技能的说明检索相关素材(目标约 {max_results} 条)。")
|
||||
messages: list = [SystemMessage(content=system), HumanMessage(content="\n".join(task_lines))]
|
||||
|
||||
results: list = []
|
||||
seen_urls: set[str] = set()
|
||||
last_error = ""
|
||||
search_timeout = max(timeout_s, _REAL_AGENT_TIMEOUT_SECONDS)
|
||||
|
||||
for _turn in range(_MAX_TURNS):
|
||||
try:
|
||||
ai = await model_tools.ainvoke(messages)
|
||||
except Exception as e: # noqa: BLE001
|
||||
last_error = f"模型调用失败: {e}"
|
||||
logger.warning("skill agent[%s] model invoke failed: %s", skill_name, e)
|
||||
break
|
||||
messages.append(ai)
|
||||
tool_calls = getattr(ai, "tool_calls", None) or []
|
||||
if not tool_calls:
|
||||
break # 模型不再检索(回复「检索完成」或纯文本)
|
||||
for tc in tool_calls:
|
||||
tc_id = tc.get("id") or ""
|
||||
query = ((tc.get("args") or {}).get("query") or "").strip()
|
||||
if not query:
|
||||
messages.append(ToolMessage(content="query 为空,已跳过", tool_call_id=tc_id))
|
||||
continue
|
||||
if emit_query:
|
||||
emit_query(query)
|
||||
try:
|
||||
found = await asyncio.wait_for(
|
||||
provider.search(query, max_results=max_results), timeout=search_timeout
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
last_error = f"检索超时(>{search_timeout}s)"
|
||||
messages.append(ToolMessage(content=last_error, tool_call_id=tc_id))
|
||||
continue
|
||||
except Exception as e: # noqa: BLE001
|
||||
last_error = str(e)
|
||||
messages.append(ToolMessage(content=f"检索失败: {e}", tool_call_id=tc_id))
|
||||
continue
|
||||
added = 0
|
||||
for r in found or []:
|
||||
url = getattr(r, "url", None)
|
||||
if url and url in seen_urls:
|
||||
continue
|
||||
if url:
|
||||
seen_urls.add(url)
|
||||
results.append(r)
|
||||
added += 1
|
||||
# 给模型一个简短回执,让它据此决定是否继续换角度检索。
|
||||
sample = ";".join((getattr(r, "title", "") or "")[:40] for r in (found or [])[:5]) or "无结果"
|
||||
messages.append(
|
||||
ToolMessage(content=f"检索到 {added} 条新素材。标题样例:{sample}", tool_call_id=tc_id)
|
||||
)
|
||||
if len(results) >= max_results:
|
||||
break
|
||||
|
||||
if not results and last_error:
|
||||
return [], last_error
|
||||
return results, ""
|
||||
+341
@@ -0,0 +1,341 @@
|
||||
"""节点共用工具:JSON 解析容错、LLM 创建。
|
||||
|
||||
弱模型经常输出不规范 JSON(中文标点、结尾多余逗号、被 markdown 包裹、
|
||||
夹带解释文字、嵌套对象)。parse_json 用多级降级策略尽量抢救出结构化结果。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_ai_writing_config() -> dict:
|
||||
"""读 ``config.yaml`` 的 ``ai_writing`` 段。
|
||||
|
||||
阶段 7 之前由路由层 ``_make_config`` 把这份 dict 注入到 ``configurable.ai_writing_config``
|
||||
给节点使用。阶段 7 删冗余后 ``_make_config`` 和注入路径全部消失(前端 ``stream.submit``
|
||||
的 config 只传 ``model_name``)—— 节点改成自己直接从 ``get_app_config()`` 读,跨边界
|
||||
更干净,``config.yaml`` 改完也不用重启前端 / 重发起会话。
|
||||
|
||||
返回字段(按使用方):
|
||||
- researcher:``keyword_model`` / ``rank_model`` / ``max_materials`` /
|
||||
``search_provider`` 等
|
||||
- editor:``pass_threshold``
|
||||
"""
|
||||
try:
|
||||
from deerflow.config import get_app_config
|
||||
|
||||
aw = getattr(get_app_config(), "ai_writing", None)
|
||||
if isinstance(aw, dict):
|
||||
return aw
|
||||
return dict(aw) if aw else {}
|
||||
except Exception:
|
||||
logger.exception("ai_writing: 读取 app config 失败,使用空配置")
|
||||
return {}
|
||||
|
||||
|
||||
def safe_get_stream_writer():
|
||||
"""返回一个 stream writer callable;在没有 LangGraph runtime 上下文时退化成 no-op。
|
||||
|
||||
AI 写作节点用它把流式 chunk 推到 LangGraph 原生 ``custom`` 通道。
|
||||
场景:
|
||||
|
||||
- LangGraph Server 路径(``client.runs.stream`` 订阅了 ``stream_mode=["custom", ...]``)
|
||||
→ 真实 writer,事件透传给 SDK 消费方。
|
||||
- 老 ``/start /stream /resume`` 路径(``graph.astream(stream_mode="updates")``)
|
||||
→ 真实 writer 仍可调用,只是没人订阅 custom 通道,事件被静默丢弃。
|
||||
这是阶段 2「硬切到 get_stream_writer」的预期行为:老前端 EventSource 不再收到
|
||||
token-level chunk,但整体进度卡(draft_ready / review_ready 这些 state 级事件)
|
||||
仍正常 —— 阶段 3 前端切到 useStream 后立刻补齐。
|
||||
- 单测里直接调节点函数(无 LangGraph runtime) → 返回 no-op,调用方零分支。
|
||||
"""
|
||||
try:
|
||||
from langgraph.config import get_stream_writer # 延迟 import,便于单测打补丁
|
||||
|
||||
return get_stream_writer()
|
||||
except Exception:
|
||||
return lambda _payload: None
|
||||
|
||||
|
||||
def get_model(config: RunnableConfig, model_name: str | None = None, *, disable_thinking: bool = False):
|
||||
"""创建对话模型。
|
||||
|
||||
model_name 显式传入时优先使用(如素材收集专家的检索词/排序专用快模型),
|
||||
否则回退到 configurable.model_name(用户在写作表单里选的主模型)。
|
||||
|
||||
disable_thinking=True 时强制关闭模型的思考/推理(force_disable_thinking):
|
||||
对 OpenAI 兼容网关注入 GLM ``thinking.type=disabled`` / Qwen·vLLM
|
||||
``enable_thinking=False`` 三种禁用形态,用于素材收集、大纲整理这类不需要思考的
|
||||
结构化任务(GLM5 等内网模型尤其爱出 <think>)。关不掉的模型由各调用方的
|
||||
ThinkTagParser 兜底,照常把思考分流到思考框。
|
||||
"""
|
||||
from deerflow.models import create_chat_model
|
||||
name = model_name or config.get("configurable", {}).get("model_name", "") or None
|
||||
return create_chat_model(name=name, force_disable_thinking=disable_thinking)
|
||||
|
||||
|
||||
def resolve_writing_model_name(config: RunnableConfig, state: dict) -> tuple[str | None, bool]:
|
||||
"""解析大纲 / 写作 / 审核阶段使用的模型名,返回 ``(name, used_default)``。
|
||||
|
||||
优先级:``state['model_name']``(用户在表单选定,由 intent_parser 在首次
|
||||
invocation 捕获并经 checkpoint 持久化)> ``configurable.model_name``(同一
|
||||
invocation 内直跑、或前端在 resume 时一并回传的兜底)。
|
||||
|
||||
interrupt/resume 是新 invocation,其 config 默认不带原 model_name,所以正常情况
|
||||
要从 state 读。两者皆空(历史会话 / 首轮捕获就为空 / 前端没传)时不再视为致命
|
||||
错误,而是返回 ``(None, True)``:调用方据此回退 config.yaml 的 ``models[0]`` 并向
|
||||
用户提示「未选模型,已用默认模型」,保证写作流程不被一个空模型名硬卡住。
|
||||
"""
|
||||
name = ((state or {}).get("model_name") or "").strip()
|
||||
if not name:
|
||||
name = (config.get("configurable", {}).get("model_name", "") or "").strip()
|
||||
if name:
|
||||
return name, False
|
||||
logger.warning("写作模型未指定(state/configurable 均为空),回退默认模型")
|
||||
return None, True
|
||||
|
||||
|
||||
def get_writing_model(config: RunnableConfig, state: dict):
|
||||
"""大纲 / 写作 / 审核:解析用户选定模型,缺失时回退默认模型(见 resolve_writing_model_name)。"""
|
||||
name, _ = resolve_writing_model_name(config, state)
|
||||
from deerflow.models import create_chat_model
|
||||
return create_chat_model(name=name)
|
||||
|
||||
|
||||
# 弱模型常把结构性标点写成中文全角符号,仅在常规解析失败后作为修复手段
|
||||
_FULLWIDTH_FIX = {
|
||||
"“": '"', # 左双引号 “
|
||||
"”": '"', # 右双引号 ”
|
||||
"‘": "'", # 左单引号 ‘
|
||||
"’": "'", # 右单引号 ’
|
||||
":": ":", # 全角冒号 :
|
||||
",": ",", # 全角逗号 ,
|
||||
"(": "(", # 全角左括号 (
|
||||
")": ")", # 全角右括号 )
|
||||
"{": "{", # 全角左花括号 {
|
||||
"}": "}", # 全角右花括号 }
|
||||
"[": "[", # 全角左方括号 [
|
||||
"]": "]", # 全角右方括号 ]
|
||||
"【": "[", # 黑方括号 【
|
||||
"】": "]", # 黑方括号 】
|
||||
}
|
||||
|
||||
|
||||
def _extract_balanced(text: str) -> str | None:
|
||||
"""从文本中提取第一个括号平衡的 {...} 块(支持嵌套,忽略字符串内的括号)。"""
|
||||
start = text.find("{")
|
||||
if start == -1:
|
||||
return None
|
||||
depth = 0
|
||||
in_str = False
|
||||
escape = False
|
||||
for i in range(start, len(text)):
|
||||
ch = text[i]
|
||||
if escape:
|
||||
escape = False
|
||||
continue
|
||||
if ch == "\\":
|
||||
escape = True
|
||||
continue
|
||||
if ch == '"':
|
||||
in_str = not in_str
|
||||
continue
|
||||
if in_str:
|
||||
continue
|
||||
if ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return text[start:i + 1]
|
||||
return None
|
||||
|
||||
|
||||
def _repair_json(text: str) -> str:
|
||||
"""尽力修复弱模型常见的 JSON 错误:全角标点、结尾多余逗号。"""
|
||||
for bad, good in _FULLWIDTH_FIX.items():
|
||||
text = text.replace(bad, good)
|
||||
# 去掉对象/数组结尾多余的逗号: ,} 或 ,]
|
||||
text = re.sub(r",(\s*[}\]])", r"\1", text)
|
||||
return text
|
||||
|
||||
|
||||
def _try_load(text: str) -> dict | None:
|
||||
try:
|
||||
result = json.loads(text)
|
||||
except Exception:
|
||||
return None
|
||||
return result if isinstance(result, dict) else None
|
||||
|
||||
|
||||
def parse_json(text: str, fallback: dict) -> dict:
|
||||
"""从 LLM 响应中提取 JSON 对象,多级降级,失败时返回 fallback。"""
|
||||
if not text or not text.strip():
|
||||
return fallback
|
||||
text = text.strip()
|
||||
|
||||
# 1. 直接解析
|
||||
if (r := _try_load(text)) is not None:
|
||||
return r
|
||||
|
||||
# 2. markdown 代码块内容
|
||||
m = re.search(r"```(?:json)?\s*(.+?)\s*```", text, re.DOTALL)
|
||||
if m:
|
||||
fenced = m.group(1).strip()
|
||||
if (r := _try_load(fenced)) is not None:
|
||||
return r
|
||||
text = fenced # 用代码块内容继续后续步骤
|
||||
|
||||
# 3. 平衡括号提取(处理夹带解释文字、嵌套对象的情况)
|
||||
candidate = _extract_balanced(text)
|
||||
if candidate:
|
||||
if (r := _try_load(candidate)) is not None:
|
||||
return r
|
||||
# 4. 修复后再试
|
||||
if (r := _try_load(_repair_json(candidate))) is not None:
|
||||
return r
|
||||
|
||||
# 5. 整体修复后兜底
|
||||
if (r := _try_load(_repair_json(text))) is not None:
|
||||
return r
|
||||
|
||||
logger.warning("JSON parse failed, using fallback. raw=%r", text[:200])
|
||||
return fallback
|
||||
|
||||
|
||||
def reasoning_delta(chunk) -> str:
|
||||
"""提取流式 chunk 的增量「推理内容」。
|
||||
|
||||
推理模型(deepseek-reasoner / GLM 等)会把思考过程放在
|
||||
``additional_kwargs.reasoning_content`` 增量字段里,而 ``content`` 为空 ——
|
||||
只读 content 的流式循环会把这些 chunk 全部跳过,前端就看不到任何实时输出。
|
||||
各写作节点用本函数把推理增量当作 ``is_thinking=True`` 推给前端的思考框。
|
||||
|
||||
兼容不同 provider 的字段名:``reasoning_content``(主流)/ ``reasoning``。
|
||||
"""
|
||||
ak = getattr(chunk, "additional_kwargs", None) or {}
|
||||
val = ak.get("reasoning_content") or ak.get("reasoning") or ""
|
||||
return val if isinstance(val, str) else ""
|
||||
|
||||
|
||||
def _content_to_text(content) -> str:
|
||||
"""将 LLM 响应内容统一为字符串(部分模型返回结构化 content 块列表)。"""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if isinstance(content, list):
|
||||
return "".join(
|
||||
part.get("text", "") if isinstance(part, dict) else str(part)
|
||||
for part in content
|
||||
)
|
||||
return str(content)
|
||||
|
||||
|
||||
# 中文常见标点:参与「字数」统计,与 Word 字数统计 / 用户直觉口径对齐
|
||||
_CJK_PUNCTUATION = ",。、;:?!“”‘’()《》【】「」『』〈〉—…·~"
|
||||
|
||||
|
||||
def count_words(text: str) -> int:
|
||||
"""统计中文语境下的「字数」。
|
||||
|
||||
口径:汉字逐字计 + 中文标点逐个计 + 连续数字/英文各算 1 字。
|
||||
不计入空白、markdown 语法符号(# * - 等)与 ASCII 标点。
|
||||
前后端共用同一口径(前端 utils/wordCount.ts 实现一致)。
|
||||
"""
|
||||
if not text:
|
||||
return 0
|
||||
cjk = len(re.findall(r"[一-鿿]", text))
|
||||
cjk_punct = sum(1 for c in text if c in _CJK_PUNCTUATION)
|
||||
alnum_runs = len(re.findall(r"[A-Za-z0-9]+", text))
|
||||
return cjk + cjk_punct + alnum_runs
|
||||
|
||||
|
||||
class ThinkTagParser:
|
||||
"""流式 <think>...</think> 标签解析器。
|
||||
|
||||
模型可能在任意 chunk 边界截断标签,通过尾部缓冲处理跨 chunk 边界情况。
|
||||
调用 feed(chunk) 返回 list[tuple[bool, str]],每项为 (is_thinking, text)。
|
||||
"""
|
||||
|
||||
_OPEN_TAG = "<think>"
|
||||
_CLOSE_TAG = "</think>"
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._in_think = False
|
||||
self._buf = "" # 潜在标签的部分匹配缓冲
|
||||
|
||||
def feed(self, chunk: str) -> list[tuple[bool, str]]:
|
||||
"""处理一个流式 chunk,返回 (is_thinking, text) 片段列表。"""
|
||||
self._buf += chunk
|
||||
result: list[tuple[bool, str]] = []
|
||||
|
||||
while self._buf:
|
||||
if self._in_think:
|
||||
tag = self._CLOSE_TAG
|
||||
idx = self._buf.find(tag)
|
||||
if idx != -1:
|
||||
if idx > 0:
|
||||
result.append((True, self._buf[:idx]))
|
||||
self._buf = self._buf[idx + len(tag):]
|
||||
self._in_think = False
|
||||
else:
|
||||
partial = self._partial_suffix(self._buf, tag)
|
||||
if partial:
|
||||
result.append((True, self._buf[:-partial]))
|
||||
self._buf = self._buf[-partial:]
|
||||
else:
|
||||
result.append((True, self._buf))
|
||||
self._buf = ""
|
||||
break
|
||||
else:
|
||||
tag = self._OPEN_TAG
|
||||
idx = self._buf.find(tag)
|
||||
if idx != -1:
|
||||
if idx > 0:
|
||||
result.append((False, self._buf[:idx]))
|
||||
self._buf = self._buf[idx + len(tag):]
|
||||
self._in_think = True
|
||||
else:
|
||||
partial = self._partial_suffix(self._buf, tag)
|
||||
if partial:
|
||||
result.append((False, self._buf[:-partial]))
|
||||
self._buf = self._buf[-partial:]
|
||||
else:
|
||||
result.append((False, self._buf))
|
||||
self._buf = ""
|
||||
break
|
||||
|
||||
return [(is_t, t) for (is_t, t) in result if t]
|
||||
|
||||
@staticmethod
|
||||
def _partial_suffix(text: str, tag: str) -> int:
|
||||
"""返回 text 尾部与 tag 前缀匹配的最长长度(>0 说明可能是部分标签)。"""
|
||||
max_check = min(len(tag) - 1, len(text))
|
||||
for length in range(max_check, 0, -1):
|
||||
if tag.startswith(text[-length:]):
|
||||
return length
|
||||
return 0
|
||||
|
||||
|
||||
async def llm_json(system: str, user: str, fallback: dict, config: RunnableConfig, model_name: str | None = None, *, disable_thinking: bool = False) -> dict:
|
||||
"""调用 LLM,解析 JSON 输出,失败用 fallback。
|
||||
|
||||
model_name 可覆盖默认模型(如检索词/排序使用配置的专用快模型)。
|
||||
disable_thinking=True 时强制关闭思考(见 get_model),用于检索词/素材排序这类
|
||||
结构化抽取任务,省掉 GLM5 等模型的 <think> 思考开销。
|
||||
"""
|
||||
messages = [SystemMessage(content=system), HumanMessage(content=user)]
|
||||
try:
|
||||
# get_model 放进 try:模型名在 config.yaml 找不到时 create_chat_model 会抛
|
||||
# ValueError,若留在 try 外会逐层上抛使整个图硬崩溃。
|
||||
model = get_model(config, model_name=model_name, disable_thinking=disable_thinking)
|
||||
resp = await model.ainvoke(messages)
|
||||
return parse_json(_content_to_text(resp.content), fallback)
|
||||
except Exception as e:
|
||||
logger.error("llm_json failed: %s", e, exc_info=True)
|
||||
return {**fallback, "_llm_error": str(e)}
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
"""编辑节点:三项独立 LLM 审查(事实/逻辑/语言),逐 token 流式推送以支持打字机效果。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Callable
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..state import AIWritingState, ReviewResult, ReviewIssue
|
||||
from ..prompts.editor_prompts import (
|
||||
EDITOR_SYSTEM, FACT_REVIEW_USER, LOGIC_REVIEW_USER, LANGUAGE_REVIEW_USER,
|
||||
USER_FOCUS_BLOCK,
|
||||
)
|
||||
from ._agent_runner import (
|
||||
EDITOR_AGENT_ID,
|
||||
REVIEW_READY_MARKER,
|
||||
WritingAgentUnavailable,
|
||||
agent_display_name,
|
||||
load_writing_agent,
|
||||
resolve_agent_model_name,
|
||||
run_writing_agent,
|
||||
use_builtin_agent,
|
||||
)
|
||||
from ._model_fallback import (
|
||||
build_switch_progress_event,
|
||||
classify_exception,
|
||||
emit_model_switch_event,
|
||||
fallback_chain,
|
||||
)
|
||||
from ._utils import get_ai_writing_config, parse_json, resolve_writing_model_name, _content_to_text, reasoning_delta, ThinkTagParser, safe_get_stream_writer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_DEFAULT_PASS_THRESHOLD = 75
|
||||
|
||||
|
||||
def _build_user_focus(state: AIWritingState) -> str:
|
||||
"""从干预数据取用户在「编辑审核」时附带的修改意见,拼成注入审查提示词的关注块。
|
||||
|
||||
仅当用户点「让编辑继续审核」且填了意见(graph.py `to_editor` 分支 stash 的
|
||||
``_editor_notes``)时返回非空块;否则返回空串,三维度审查保持原样。
|
||||
"""
|
||||
intervention = state.get("last_user_intervention") or {}
|
||||
notes = (intervention.get("_editor_notes") or "").strip()
|
||||
if not notes:
|
||||
return ""
|
||||
return USER_FOCUS_BLOCK.format(notes=notes)
|
||||
|
||||
|
||||
def _materials_summary(pkg: dict) -> str:
|
||||
lines = []
|
||||
for m in (pkg.get("materials") or [])[:6]:
|
||||
lines.append(f"[{m['id']}] {m['title']}: {m['content'][:200]}")
|
||||
return "\n".join(lines) or "(无素材)"
|
||||
|
||||
|
||||
def _parse_issues(raw_issues: list, category: str) -> list[ReviewIssue]:
|
||||
issues = []
|
||||
for item in (raw_issues or []):
|
||||
issues.append(ReviewIssue(
|
||||
category=category,
|
||||
severity=item.get("severity", "minor"),
|
||||
location=item.get("location", ""),
|
||||
issue=item.get("issue", ""),
|
||||
suggestion=item.get("suggestion", ""),
|
||||
))
|
||||
return issues
|
||||
|
||||
|
||||
async def _stream_one_review(
|
||||
model, user_prompt: str, category: str,
|
||||
writer: Callable[[dict], None],
|
||||
) -> str:
|
||||
"""流式执行单项审查,逐 chunk 经 LangGraph custom 通道推送 review_chunk 事件,返回累积的正文文本(用于 JSON 解析)。"""
|
||||
full_text = "" # 仅累积正文(非思考),供后续 JSON 解析
|
||||
parser = ThinkTagParser()
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=EDITOR_SYSTEM),
|
||||
HumanMessage(content=user_prompt),
|
||||
]):
|
||||
# 推理模型的思考走 reasoning_content(content 为空):作为思考实时推送
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
writer({"type": "review_chunk", "category": category, "text": reasoning, "is_thinking": True})
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
writer({
|
||||
"type": "review_chunk",
|
||||
"category": category,
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
return full_text
|
||||
|
||||
|
||||
async def _save_review_to_repo(config: RunnableConfig, review: ReviewResult) -> None:
|
||||
"""把 review_result 异步落到业务表(best-effort,写失败不影响流程)。
|
||||
|
||||
``user_id`` 走 ``resolve_user_id(AUTO)`` 读 contextvar(与 intent_parser 建行、
|
||||
writer_draft 写草稿同套规则)。
|
||||
"""
|
||||
cfg = config.get("configurable", {}) or {}
|
||||
thread_id = cfg.get("thread_id")
|
||||
if not thread_id:
|
||||
return
|
||||
from deerflow.persistence.ai_writing_sessions import get_default_store
|
||||
from deerflow.runtime.user_context import AUTO
|
||||
|
||||
repo = get_default_store()
|
||||
if repo is None:
|
||||
return
|
||||
try:
|
||||
await repo.update(
|
||||
thread_id,
|
||||
user_id=AUTO,
|
||||
status="review_ready",
|
||||
review_result=dict(review),
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("editor: save_review failed (non-fatal) thread_id=%s", thread_id)
|
||||
|
||||
|
||||
async def editor_node(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
draft = state.get("current_draft")
|
||||
if not draft:
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": [{"type": "error", "agent_name": "编辑", "message": "草稿为空,无法审核"}],
|
||||
}
|
||||
|
||||
article = draft["full_markdown"]
|
||||
pkg = state.get("material_package") or {}
|
||||
mats = _materials_summary(pkg)
|
||||
|
||||
cfg = config.get("configurable", {})
|
||||
writer = safe_get_stream_writer()
|
||||
# 阶段 7 后从 app config 直接读,不走 configurable 注入
|
||||
ai_writing_config = get_ai_writing_config()
|
||||
pass_threshold = int(ai_writing_config.get("pass_threshold", _DEFAULT_PASS_THRESHOLD))
|
||||
|
||||
# 用户在草稿确认时附带的修改意见 → 注入三维度审查,让编辑重点核查(空则不影响原审查)
|
||||
user_focus = _build_user_focus(state)
|
||||
|
||||
REVIEW_TASKS = [
|
||||
("fact", FACT_REVIEW_USER.format(materials_summary=mats, article=article, user_focus=user_focus)),
|
||||
("logic", LOGIC_REVIEW_USER.format(article=article, user_focus=user_focus)),
|
||||
("language", LANGUAGE_REVIEW_USER.format(article=article, user_focus=user_focus)),
|
||||
]
|
||||
|
||||
# 内置 agent 模式(config.yaml ai_writing.use_builtin_agents.editor):
|
||||
# 三维度循环仍由节点编排(review_chunk / review_item_done 流式形态不变),
|
||||
# 每个维度跑一次 ai-writing-editor agent(SOUL 作 system prompt,输出
|
||||
# [REVIEW_READY] + {"score","issues"})。agent 目录缺失则静默回退旧路径。
|
||||
use_agent = use_builtin_agent("editor")
|
||||
agent_label = "编辑"
|
||||
model = None
|
||||
if use_agent:
|
||||
try:
|
||||
agent_cfg, _soul = load_writing_agent(EDITOR_AGENT_ID)
|
||||
# 模型守卫提前到循环外:模型名缺失/配错时与旧路径同样转成 status:error
|
||||
resolve_agent_model_name(agent_cfg, state, config)
|
||||
agent_label = agent_display_name(EDITOR_AGENT_ID, "编辑")
|
||||
except WritingAgentUnavailable as e:
|
||||
logger.warning("editor builtin agent unavailable, falling back to legacy prompts: %s", e)
|
||||
use_agent = False
|
||||
except Exception as e:
|
||||
logger.error("editor agent model resolution failed: %s", e, exc_info=True)
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": [{
|
||||
"type": "error", "agent_name": agent_label,
|
||||
"title": "模型错误", "detail": str(e), "message": "审核失败",
|
||||
}],
|
||||
}
|
||||
# 旧路径下模型由 fallback_chain 在每维度审查前按候选顺序解析;模型挂掉自动换下一个继续。
|
||||
editor_chain: list[str] = []
|
||||
current_model_name: str | None = None
|
||||
if not use_agent:
|
||||
initial_name, _ud = resolve_writing_model_name(config, state)
|
||||
editor_chain = fallback_chain(initial_name)
|
||||
current_model_name = editor_chain[0] if editor_chain else None
|
||||
from deerflow.models import create_chat_model
|
||||
results: dict[str, dict] = {}
|
||||
review_errors: list[str] = [] # 分项审查的模型异常,供完成后非致命提示
|
||||
switch_notices: list[dict] = [] # 模型容错切换记录,最终塞进 progress_events 走时间线
|
||||
|
||||
_CATEGORY_LABEL = {"fact": "事实性", "logic": "逻辑性", "language": "语言文字"}
|
||||
|
||||
for category, user_prompt in REVIEW_TASKS:
|
||||
fallback = {"score": 80, "issues": []}
|
||||
try:
|
||||
if use_agent:
|
||||
def _on_text(text: str, is_thinking: bool, _cat: str = category) -> None:
|
||||
writer({"type": "review_chunk", "category": _cat, "text": text, "is_thinking": is_thinking})
|
||||
|
||||
res = await run_writing_agent(
|
||||
agent_id=EDITOR_AGENT_ID,
|
||||
user_payload=user_prompt,
|
||||
marker=REVIEW_READY_MARKER,
|
||||
fallback=fallback,
|
||||
state=state,
|
||||
config=config,
|
||||
on_text=_on_text,
|
||||
)
|
||||
if res.get("_llm_error"):
|
||||
review_errors.append(f"「{_CATEGORY_LABEL.get(category, category)}审查」{res['_llm_error']}")
|
||||
else:
|
||||
# 旧路径:按 fallback_chain 试候选模型,失败就换下一个继续本维度。
|
||||
# 与 writer_draft 同思路:以「上一维度成功使用的模型」为头部。
|
||||
dim_chain: list[str] = []
|
||||
if current_model_name:
|
||||
dim_chain.append(current_model_name)
|
||||
for n in editor_chain:
|
||||
if n and n not in dim_chain:
|
||||
dim_chain.append(n)
|
||||
if not dim_chain:
|
||||
dim_chain = [None] # type: ignore[list-item]
|
||||
full_text = ""
|
||||
dim_succeeded = False
|
||||
last_dim_exc: BaseException | None = None
|
||||
for d_idx, candidate in enumerate(dim_chain):
|
||||
try:
|
||||
m = create_chat_model(name=candidate) if candidate else create_chat_model()
|
||||
full_text = await _stream_one_review(m, user_prompt, category, writer)
|
||||
dim_succeeded = True
|
||||
current_model_name = candidate or current_model_name
|
||||
break
|
||||
except Exception as e:
|
||||
last_dim_exc = e
|
||||
reason = classify_exception(e)
|
||||
logger.error(
|
||||
"editor review [%s] failed on model=%s (reason=%s): %s",
|
||||
category, candidate, reason, e, exc_info=True,
|
||||
)
|
||||
next_candidate = dim_chain[d_idx + 1] if d_idx + 1 < len(dim_chain) else None
|
||||
if next_candidate:
|
||||
emit_model_switch_event(
|
||||
writer, agent_name=agent_label, stage="review",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
)
|
||||
switch_notices.append(build_switch_progress_event(
|
||||
agent_name=agent_label, stage="review",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
))
|
||||
continue
|
||||
if not dim_succeeded:
|
||||
assert last_dim_exc is not None
|
||||
raise last_dim_exc
|
||||
res = parse_json(full_text, fallback)
|
||||
except Exception as e:
|
||||
logger.warning("editor review [%s] failed: %s", category, e)
|
||||
res = fallback
|
||||
review_errors.append(f"「{_CATEGORY_LABEL.get(category, category)}审查」{type(e).__name__}: {e}")
|
||||
|
||||
results[category] = res
|
||||
|
||||
# 这一项审查完成 —— 推送结构化结果(前端据此把流式文本替换为评分/意见)
|
||||
issues = _parse_issues(res.get("issues", []), category)
|
||||
writer({
|
||||
"type": "review_item_done",
|
||||
"category": category,
|
||||
"score": int(res.get("score", 80)),
|
||||
"issues": [dict(iss) for iss in issues],
|
||||
})
|
||||
|
||||
# 汇总
|
||||
fact_score = int(results["fact"].get("score", 80))
|
||||
logic_score = int(results["logic"].get("score", 80))
|
||||
lang_score = int(results["language"].get("score", 80))
|
||||
overall_score = (fact_score + logic_score + lang_score) // 3
|
||||
|
||||
all_issues: list[ReviewIssue] = (
|
||||
_parse_issues(results["fact"].get("issues", []), "fact") +
|
||||
_parse_issues(results["logic"].get("issues", []), "logic") +
|
||||
_parse_issues(results["language"].get("issues", []), "language")
|
||||
)
|
||||
all_issues.sort(key=lambda x: (0 if x["severity"] == "major" else 1))
|
||||
|
||||
verdict = "pass" if overall_score >= pass_threshold else "reject"
|
||||
|
||||
review = ReviewResult(
|
||||
verdict=verdict,
|
||||
fact_score=fact_score,
|
||||
logic_score=logic_score,
|
||||
language_score=lang_score,
|
||||
overall_score=overall_score,
|
||||
pass_threshold=pass_threshold,
|
||||
revision_notes=all_issues,
|
||||
)
|
||||
|
||||
# 阶段 2:节点内并存 DB 写入(路由层同样也写一遍,阶段 7 删冗余时移除路由那份)
|
||||
await _save_review_to_repo(config, review)
|
||||
|
||||
# 分项审查出现模型异常时,以非致命 notice 提示用户,审核结论仍照常产出
|
||||
notices: list[dict] = []
|
||||
if review_errors:
|
||||
notices.append({
|
||||
"type": "notice",
|
||||
"agent_name": agent_label,
|
||||
"title": "模型错误",
|
||||
"detail": "部分审查项调用失败,已按默认分兜底。原因:" + ";".join(review_errors[:3]),
|
||||
})
|
||||
|
||||
return {
|
||||
"review_result": review,
|
||||
"progress_events": [
|
||||
{"type": "step_start", "agent_name": agent_label, "message": "开始审核"},
|
||||
*switch_notices,
|
||||
*notices,
|
||||
{
|
||||
"type": "review_ready",
|
||||
"agent_name": agent_label,
|
||||
"message": f"审核完成,综合得分 {overall_score},结论:{'通过' if verdict == 'pass' else '打回修改'}",
|
||||
},
|
||||
],
|
||||
}
|
||||
+346
@@ -0,0 +1,346 @@
|
||||
"""意图解析节点:将用户自然语言意图结构化为写作配置。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
|
||||
from ..state import AIWritingState
|
||||
from ._model_fallback import (
|
||||
build_switch_progress_event,
|
||||
classify_exception,
|
||||
emit_model_switch_event,
|
||||
fallback_chain,
|
||||
)
|
||||
from ._utils import (
|
||||
ThinkTagParser,
|
||||
_content_to_text,
|
||||
parse_json,
|
||||
reasoning_delta,
|
||||
safe_get_stream_writer,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _truncate_title(text: str, max_len: int = 50) -> str:
|
||||
"""与老 /start 路由里 _truncate_title 行为一致:UTF 字符截断 + 省略号。"""
|
||||
text = (text or "").strip()
|
||||
if len(text) <= max_len:
|
||||
return text
|
||||
return text[:max_len].rstrip() + "…"
|
||||
|
||||
|
||||
async def _upsert_business_row(configurable: dict, state: AIWritingState) -> None:
|
||||
"""在 intent_parser 入口幂等创建 ai_writing_sessions 业务表行。
|
||||
|
||||
LangGraph Server 路径下没有 /start 路由建行,必须节点自建,否则列表页查不到、
|
||||
transcript / draft / review 后续写入都会因 update 找不到行而静默丢数据。
|
||||
resume 路径下 thread_id 已经有行,upsert_session 只刷新 updated_at。
|
||||
|
||||
user_id 走 ``resolve_user_id(AUTO)``:FastAPI 请求线程把它写到 contextvar,
|
||||
asyncio.create_task 继承父任务的 context,所以这里能拿到当前用户;
|
||||
contextvar 没设时(测试 / 离线场景)抛 RuntimeError,try/except 兜底跳过。
|
||||
"""
|
||||
from deerflow.persistence.ai_writing_sessions import get_default_store
|
||||
from deerflow.runtime.user_context import AUTO
|
||||
|
||||
thread_id = configurable.get("thread_id")
|
||||
if not thread_id:
|
||||
return
|
||||
repo = get_default_store()
|
||||
if repo is None:
|
||||
return
|
||||
user_intent = (state.get("user_intent") or "").strip()
|
||||
try:
|
||||
await repo.upsert_session(
|
||||
id=thread_id,
|
||||
title=_truncate_title(user_intent) or "AI 写作",
|
||||
user_intent=user_intent,
|
||||
user_id=AUTO,
|
||||
status="in_progress",
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("intent_parser: upsert_session failed (non-fatal) thread_id=%s", thread_id)
|
||||
|
||||
# 兜底类型列表,当 config 中未注入 valid_article_types 时使用
|
||||
_DEFAULT_ARTICLE_TYPES: list[dict] = [
|
||||
{"key": "news", "label": "新闻资讯"},
|
||||
{"key": "analysis", "label": "分析报道"},
|
||||
{"key": "feature", "label": "深度特稿"},
|
||||
{"key": "report", "label": "调研报告"},
|
||||
]
|
||||
|
||||
_SYSTEM_TEMPLATE = """你是一名写作助手。
|
||||
你的任务:分析用户的写作意图,提取结构化的写作配置。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 "。
|
||||
- article_type 只能是以下之一:{type_options}。
|
||||
|
||||
【完整示例】
|
||||
输入意图:帮我写一篇面向行业专家的深度文章,分析今年新能源汽车出口为什么大涨,大概两千字
|
||||
正确输出:
|
||||
{{"article_type": "feature", "target_audience": "行业专家", "word_count_target": 2000, "refined_intent": "分析今年新能源汽车出口高速增长的原因"}}"""
|
||||
|
||||
_USER_TEMPLATE = """用户写作意图:{user_intent}
|
||||
|
||||
用户在表单中已指定写作配置(以下三项必须原样输出,不要自行修改):
|
||||
- 文章类型(article_type):{preconfigured_article_type}
|
||||
- 目标读者(target_audience):{preconfigured_audience}
|
||||
- 目标字数(word_count_target):{preconfigured_word_count}
|
||||
|
||||
请输出以下 JSON,article_type / target_audience / word_count_target 必须与上述一致,只需根据用户意图精炼 refined_intent:
|
||||
{{{{
|
||||
"article_type": "{preconfigured_article_type}",
|
||||
"target_audience": "{preconfigured_audience}",
|
||||
"word_count_target": {preconfigured_word_count},
|
||||
"refined_intent": "精炼后的写作意图(一句话)"
|
||||
}}}}"""
|
||||
|
||||
|
||||
def _build_type_options(types: list[dict]) -> str:
|
||||
"""将类型列表格式化为提示词中的枚举说明,如 news(新闻资讯)/ analysis(分析报道)。"""
|
||||
return " / ".join(f'{t["key"]}({t["label"]})' for t in types)
|
||||
|
||||
|
||||
def _coerce_word_count(raw) -> int | None:
|
||||
"""稳健地把模型返回的字数字段转成正整数;无法解析时返回 None。
|
||||
|
||||
弱模型(尤其老模型被陌生词带偏时)常返回「约800字」「两千」「800.0」
|
||||
这类非纯数字形式,直接 int() 会抛 ValueError/TypeError 使整个图崩溃。
|
||||
此处用「提取首个数字串」策略容错,解析不出就返回 None 让调用方保留默认值。
|
||||
"""
|
||||
if raw is None:
|
||||
return None
|
||||
if isinstance(raw, bool): # bool 是 int 子类,需先排除
|
||||
return None
|
||||
if isinstance(raw, (int, float)):
|
||||
value = int(raw)
|
||||
return value if value > 0 else None
|
||||
if not isinstance(raw, str):
|
||||
# dict / list 等结构化类型:str() 化后可能误含数字,直接判为无法解析
|
||||
return None
|
||||
m = re.search(r"\d[\d,]*", raw)
|
||||
if not m:
|
||||
return None
|
||||
try:
|
||||
value = int(m.group().replace(",", ""))
|
||||
except ValueError:
|
||||
return None
|
||||
return value if value > 0 else None
|
||||
|
||||
|
||||
def _resolve_intent_fields(
|
||||
state: AIWritingState,
|
||||
result: dict,
|
||||
fallback_result: dict,
|
||||
valid_keys: list[str],
|
||||
default_key: str,
|
||||
) -> tuple[str, str, int, str]:
|
||||
"""合并 LLM 解析结果与用户表单预填字段;预填字段优先(见 AI_WRITING_BACKEND_DEV_ZH §intent_parser)。"""
|
||||
fallback_type = fallback_result["article_type"]
|
||||
if fallback_type not in valid_keys:
|
||||
fallback_type = default_key
|
||||
|
||||
raw_type = result.get("article_type", "")
|
||||
llm_type = raw_type if raw_type in valid_keys else fallback_type
|
||||
|
||||
preconfigured_type = (state.get("article_type") or "").strip()
|
||||
if preconfigured_type:
|
||||
# 用户/配置助手已明确指定文章类型(含**列表外的自定义/新类型**)→ 原样尊重,
|
||||
# 不再因不在默认表里被丢弃回退成默认(否则自定义/新类型在写作时会被还原成默认)。
|
||||
final_article_type = preconfigured_type
|
||||
else:
|
||||
final_article_type = llm_type
|
||||
|
||||
preconfigured_audience = (state.get("target_audience") or "").strip()
|
||||
if preconfigured_audience:
|
||||
final_target_audience = preconfigured_audience
|
||||
else:
|
||||
final_target_audience = result.get("target_audience") or fallback_result["target_audience"]
|
||||
|
||||
preconfigured_wc = state.get("word_count_target")
|
||||
if isinstance(preconfigured_wc, int) and preconfigured_wc > 0:
|
||||
final_word_count = preconfigured_wc
|
||||
else:
|
||||
word_count = _coerce_word_count(result.get("word_count_target"))
|
||||
final_word_count = word_count if word_count is not None else fallback_result["word_count_target"]
|
||||
|
||||
final_refined_intent = result.get("refined_intent") or state["user_intent"]
|
||||
return final_article_type, final_target_audience, final_word_count, final_refined_intent
|
||||
|
||||
|
||||
async def intent_parser_node(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
# 从 configurable 读取动态类型列表,未配置时退回默认值
|
||||
configurable = (config or {}).get("configurable", {})
|
||||
valid_article_types: list[dict] = configurable.get("valid_article_types") or _DEFAULT_ARTICLE_TYPES
|
||||
valid_keys: list[str] = [t["key"] for t in valid_article_types]
|
||||
default_key: str = valid_keys[0] if valid_keys else "analysis"
|
||||
|
||||
# 业务表 upsert(LangGraph Server 路径下没有 /start 路由建行,节点入口自建;
|
||||
# resume 时幂等不重复建。失败不阻断写作流程)。
|
||||
await _upsert_business_row(configurable, state)
|
||||
|
||||
type_options = _build_type_options(valid_article_types)
|
||||
system = _SYSTEM_TEMPLATE.format(type_options=type_options)
|
||||
|
||||
fallback_type = state.get("article_type", default_key)
|
||||
if fallback_type not in valid_keys:
|
||||
fallback_type = default_key
|
||||
|
||||
fallback_result = {
|
||||
"article_type": fallback_type,
|
||||
"target_audience": state.get("target_audience", "普通读者"),
|
||||
"word_count_target": state.get("word_count_target", 800),
|
||||
"refined_intent": state["user_intent"],
|
||||
}
|
||||
|
||||
preconfigured_wc = fallback_result["word_count_target"]
|
||||
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,
|
||||
preconfigured_audience=fallback_result["target_audience"],
|
||||
preconfigured_word_count=preconfigured_wc,
|
||||
)
|
||||
|
||||
# 流式调用:与 writer_outline / writer_draft 同套机制 —— LLM 逐字吐出 JSON,
|
||||
# 通过 ``intent_chunk`` 推给前端打字机;完成后 ``parse_json`` 解析结构化结果,
|
||||
# 再通过 ``intent_ready`` 推一份结构化字段给前端结果卡片。
|
||||
#
|
||||
# 模型自动容错:失败时按 fallback_chain 顺序换下一个模型重试本环节。每次切换
|
||||
# 都通过 ``model_switch`` custom 事件 + ``progress_events`` 同步告诉前端,前端
|
||||
# 清掉本环节已累积的 intent_chunk 缓冲,避免半段输出和下个模型的输出拼接错乱。
|
||||
writer = safe_get_stream_writer()
|
||||
initial_model = (configurable.get("model_name") or "").strip() or None
|
||||
chain = fallback_chain(initial_model)
|
||||
progress_events: list[dict] = []
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
full_text = ""
|
||||
success_model: str | None = None
|
||||
last_error: BaseException | None = None
|
||||
for idx, candidate in enumerate(chain or [None]):
|
||||
full_text = "" # 每次重试都从头累积,避免不同模型的输出粘在一起
|
||||
parser = ThinkTagParser()
|
||||
try:
|
||||
model = create_chat_model(name=candidate) if candidate else create_chat_model()
|
||||
async for chunk in model.astream(
|
||||
[SystemMessage(content=system), HumanMessage(content=user)]
|
||||
):
|
||||
# 推理模型的思考走 reasoning_content(content 为空):作为思考实时推送
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
writer({"type": "intent_chunk", "text": reasoning, "is_thinking": True})
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
writer({
|
||||
"type": "intent_chunk",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
success_model = candidate or ""
|
||||
break
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
reason = classify_exception(exc)
|
||||
logger.error(
|
||||
"intent_parser streaming failed on model=%s (reason=%s): %s",
|
||||
candidate, reason, exc, exc_info=True,
|
||||
)
|
||||
next_candidate = chain[idx + 1] if idx + 1 < len(chain) else None
|
||||
if next_candidate:
|
||||
emit_model_switch_event(
|
||||
writer, agent_name="系统", stage="intent",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
)
|
||||
progress_events.append(build_switch_progress_event(
|
||||
agent_name="系统", stage="intent",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
))
|
||||
continue
|
||||
# 链尾仍失败:保留 fallback 默认值,向用户报一条非致命 notice,流程继续。
|
||||
progress_events.append({
|
||||
"type": "notice",
|
||||
"agent_name": "系统",
|
||||
"title": "模型错误",
|
||||
"detail": (
|
||||
f"意图解析失败,已按默认配置兜底。原因:{type(exc).__name__}: {exc}"
|
||||
),
|
||||
})
|
||||
|
||||
result = parse_json(full_text, fallback_result) if full_text else fallback_result
|
||||
|
||||
final_article_type, final_target_audience, final_word_count, final_refined_intent = _resolve_intent_fields(
|
||||
state, 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.append({"type": "step_done", "agent_name": "系统", "message": "意图解析完成"})
|
||||
|
||||
updates: dict = {"status": "researching", "progress_events": progress_events}
|
||||
# 捕获用户表单选定的主模型进 state(仅本节点能可靠拿到 configurable.model_name —
|
||||
# 它运行在首次 invocation;后续大纲/写作/审核在 resume 后跑,config 已不带它)。
|
||||
# 大纲/写作/审核经 get_writing_model 严格读 state['model_name'],不回退默认。
|
||||
# 容错切换后用实际成功的模型作为后续阶段起点,避免每个阶段都重复经历同一次失败。
|
||||
persisted_model = (success_model or initial_model or "").strip()
|
||||
if persisted_model:
|
||||
updates["model_name"] = persisted_model
|
||||
_ = last_error # 链尾失败已生成 notice,这里保留变量供调试时断点查看
|
||||
updates["article_type"] = final_article_type
|
||||
updates["target_audience"] = final_target_audience
|
||||
updates["word_count_target"] = final_word_count
|
||||
if final_refined_intent != state["user_intent"]:
|
||||
updates["user_intent"] = final_refined_intent
|
||||
|
||||
return updates
|
||||
+1337
File diff suppressed because it is too large.
Load diff
+279
@@ -0,0 +1,279 @@
|
||||
"""样文仿写:分析用户上传/粘贴的样文,提取可复用的文风画像。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..state import AIWritingState, SampleStyleProfile
|
||||
from ._utils import (
|
||||
ThinkTagParser,
|
||||
_content_to_text,
|
||||
count_words,
|
||||
get_model,
|
||||
parse_json,
|
||||
reasoning_delta,
|
||||
safe_get_stream_writer,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_MAX_SAMPLE_ANALYSIS_CHARS = 12000
|
||||
|
||||
SAMPLE_ANALYZE_SYSTEM = """你是一名资深中文编辑和文风分析师。
|
||||
你的任务:阅读用户提供的样文,提取「可用于仿写的文风画像」,供后续写作模型模仿其结构、语气和表达节奏。
|
||||
|
||||
【重要边界】
|
||||
- 你不是改写样文本身,而是分析它的写作风格。
|
||||
- 不要复述样文的具体事实、数字、人名、机构名,除非用于概括文章结构。
|
||||
- 后续写作必须避免直接照抄样文,因此你要明确指出哪些表达方式可以学、哪些原文内容不能复制。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号。
|
||||
- 字段结构:
|
||||
{
|
||||
"summary": "一句话概括样文风格",
|
||||
"article_type_guess": "推测文章类型",
|
||||
"tone": "语气/立场/情绪特征",
|
||||
"structure": ["篇章结构特征1", "篇章结构特征2"],
|
||||
"language_features": ["遣词造句特征1", "修辞/表达特征2"],
|
||||
"sentence_style": "句式、长短句、节奏特征",
|
||||
"opening_style": "开头方式",
|
||||
"ending_style": "结尾方式",
|
||||
"imitation_guidance": ["后续仿写应遵守的建议1", "建议2"],
|
||||
"avoid_copying": ["不得复制的内容/风险1", "风险2"],
|
||||
"sample_outline": ["样文结构骨架1", "样文结构骨架2"]
|
||||
}"""
|
||||
|
||||
SAMPLE_ANALYZE_USER = """样文文件名:{filename}
|
||||
样文字数:约 {word_count} 字
|
||||
|
||||
样文内容(可能已截断):
|
||||
{sample_text}
|
||||
|
||||
请提取可用于后续仿写的文风画像,只输出 JSON。"""
|
||||
|
||||
|
||||
def _is_imitate_mode(state: AIWritingState) -> bool:
|
||||
mode = (state.get("writing_mode_type") or "").strip().lower()
|
||||
return mode == "imitate"
|
||||
|
||||
|
||||
def _compact_list(values: object, limit: int = 6) -> list[str]:
|
||||
if not isinstance(values, list):
|
||||
return []
|
||||
out: list[str] = []
|
||||
for item in values:
|
||||
text = str(item or "").strip()
|
||||
if text:
|
||||
out.append(text[:120])
|
||||
if len(out) >= limit:
|
||||
break
|
||||
return out
|
||||
|
||||
|
||||
def _normalize_profile(raw: dict, sample_text: str) -> SampleStyleProfile:
|
||||
"""规整 LLM 输出,避免缺字段影响后续提示词。"""
|
||||
first_lines = [ln.strip() for ln in sample_text.splitlines() if ln.strip()][:5]
|
||||
fallback_outline = first_lines or ["起承转合清晰", "围绕主题分段展开"]
|
||||
profile: SampleStyleProfile = {
|
||||
"summary": str(raw.get("summary") or "样文表达较为完整,结构清晰,适合提取篇章节奏与措辞习惯。")[:200],
|
||||
"article_type_guess": str(raw.get("article_type_guess") or raw.get("articleTypeGuess") or "")[:80],
|
||||
"tone": str(raw.get("tone") or "稳健、清晰")[:120],
|
||||
"structure": _compact_list(raw.get("structure"), 8) or ["先交代背景或问题,再分层展开,最后收束观点。"],
|
||||
"language_features": _compact_list(raw.get("language_features") or raw.get("languageFeatures"), 8) or ["表达相对凝练,注重段落之间的衔接。"],
|
||||
"sentence_style": str(raw.get("sentence_style") or raw.get("sentenceStyle") or "长短句结合,段落节奏平稳。")[:160],
|
||||
"opening_style": str(raw.get("opening_style") or raw.get("openingStyle") or "开头直接引入主题。")[:160],
|
||||
"ending_style": str(raw.get("ending_style") or raw.get("endingStyle") or "结尾总结全文并给出判断或展望。")[:160],
|
||||
"imitation_guidance": _compact_list(raw.get("imitation_guidance") or raw.get("imitationGuidance"), 8) or ["模仿篇章结构、语气和句式节奏,不搬运样文事实。"],
|
||||
"avoid_copying": _compact_list(raw.get("avoid_copying") or raw.get("avoidCopying"), 8) or ["不得连续照抄样文原句或复用样文中的专有事实。"],
|
||||
"sample_outline": _compact_list(raw.get("sample_outline") or raw.get("sampleOutline"), 10) or fallback_outline,
|
||||
}
|
||||
return profile
|
||||
|
||||
|
||||
def _fallback_profile(sample_text: str) -> SampleStyleProfile:
|
||||
"""模型失败时的确定性兜底画像。"""
|
||||
paragraphs = [p.strip() for p in re.split(r"\n\s*\n+", sample_text) if p.strip()]
|
||||
avg_len = round(sum(len(p) for p in paragraphs) / max(len(paragraphs), 1))
|
||||
return {
|
||||
"summary": "已根据样文段落和篇幅提取基础仿写约束。",
|
||||
"article_type_guess": "",
|
||||
"tone": "保持样文的正式程度和表达密度。",
|
||||
"structure": [
|
||||
f"样文约 {len(paragraphs)} 个自然段,平均每段约 {avg_len} 字。",
|
||||
"仿写时保留相近的段落推进节奏。",
|
||||
],
|
||||
"language_features": ["保留样文的用词正式程度、连接方式和论述密度。"],
|
||||
"sentence_style": "参照样文长短句比例,避免明显口语化或突兀转换。",
|
||||
"opening_style": "参照样文开头的切入方式,但改写为新主题。",
|
||||
"ending_style": "参照样文结尾的收束方式,形成新主题下的总结。",
|
||||
"imitation_guidance": ["学习样文结构和语气,所有事实内容以用户新要求为准。"],
|
||||
"avoid_copying": ["不要连续复用样文原句,不要照搬样文专有事实、案例和数据。"],
|
||||
"sample_outline": [p[:80] for p in paragraphs[:8]] or ["围绕主题分层展开"],
|
||||
}
|
||||
|
||||
|
||||
def render_sample_style_section(state: AIWritingState) -> str:
|
||||
"""把文风画像渲染成可注入大纲/写作提示词的约束片段。"""
|
||||
if not _is_imitate_mode(state):
|
||||
return ""
|
||||
profile = state.get("sample_style_profile") or {}
|
||||
if not profile:
|
||||
return ""
|
||||
strength = (state.get("imitate_strength") or "medium").strip().lower()
|
||||
strength_label = {
|
||||
"light": "轻度:只参考语气和部分表达节奏,结构可按新主题调整。",
|
||||
"medium": "中度:同时参考语气、段落节奏和篇章推进方式。",
|
||||
"strong": "高度:尽量贴近样文结构骨架、标题组织和表达密度,但仍不得照抄原文。",
|
||||
}.get(strength, "中度:同时参考语气、段落节奏和篇章推进方式。")
|
||||
preserve = "是" if state.get("preserve_sample_structure") else "否"
|
||||
compact = {
|
||||
"summary": profile.get("summary", ""),
|
||||
"tone": profile.get("tone", ""),
|
||||
"structure": profile.get("structure", []),
|
||||
"language_features": profile.get("language_features", []),
|
||||
"sentence_style": profile.get("sentence_style", ""),
|
||||
"opening_style": profile.get("opening_style", ""),
|
||||
"ending_style": profile.get("ending_style", ""),
|
||||
"imitation_guidance": profile.get("imitation_guidance", []),
|
||||
"avoid_copying": profile.get("avoid_copying", []),
|
||||
"sample_outline": profile.get("sample_outline", []),
|
||||
}
|
||||
return (
|
||||
"\n\n【样文仿写要求】\n"
|
||||
f"- 仿写强度:{strength_label}\n"
|
||||
f"- 是否尽量沿用样文结构:{preserve}\n"
|
||||
"- 仿写目标:学习样文的篇章结构、语气、段落节奏、句式和表达密度;新文章主题、事实与论点必须服务用户的新要求。\n"
|
||||
"- 严禁:不得连续照抄样文原句,不得搬运样文中的专有事实、数据、人物、机构或案例。\n"
|
||||
"文风画像 JSON:\n"
|
||||
f"{json.dumps(compact, ensure_ascii=False)}"
|
||||
)
|
||||
|
||||
|
||||
async def _stream_sample_analysis(
|
||||
*,
|
||||
filename: str,
|
||||
sample_text: str,
|
||||
clipped: str,
|
||||
fallback: dict,
|
||||
config: RunnableConfig,
|
||||
model_name: str | None,
|
||||
) -> dict:
|
||||
writer = safe_get_stream_writer()
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": (
|
||||
f"读取样文《{filename}》,约 {count_words(sample_text)} 字;"
|
||||
f"本轮提取前 {len(clipped)} 字用于文风画像。\n"
|
||||
"正在识别文章类型、结构骨架、语气、句式节奏、仿写建议和避抄边界...\n\n"
|
||||
),
|
||||
"is_thinking": False,
|
||||
})
|
||||
|
||||
messages = [
|
||||
SystemMessage(content=SAMPLE_ANALYZE_SYSTEM),
|
||||
HumanMessage(content=SAMPLE_ANALYZE_USER.format(
|
||||
filename=filename,
|
||||
word_count=count_words(sample_text),
|
||||
sample_text=clipped,
|
||||
)),
|
||||
]
|
||||
full_text = ""
|
||||
try:
|
||||
# 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)
|
||||
if reasoning:
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": reasoning,
|
||||
"is_thinking": True,
|
||||
})
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
return parse_json(full_text, fallback) if full_text.strip() else fallback
|
||||
except Exception as exc:
|
||||
logger.error("sample_analyzer streaming failed: %s", exc, exc_info=True)
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": "\n样文分析模型异常,已改用基础画像兜底。\n",
|
||||
"is_thinking": False,
|
||||
})
|
||||
return {**fallback, "_llm_error": str(exc)}
|
||||
|
||||
|
||||
async def sample_analyzer_node(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
if not _is_imitate_mode(state):
|
||||
return {}
|
||||
|
||||
sample_text = (state.get("sample_text") or "").strip()
|
||||
filename = (state.get("sample_filename") or "粘贴文本").strip()
|
||||
if not sample_text:
|
||||
return {
|
||||
"writing_mode_type": "normal",
|
||||
"status": "researching",
|
||||
"progress_events": [{
|
||||
"type": "notice",
|
||||
"agent_name": "系统",
|
||||
"title": "样文为空",
|
||||
"detail": "未收到可分析的样文内容,已按普通写作继续。",
|
||||
}],
|
||||
}
|
||||
|
||||
clipped = sample_text[:_MAX_SAMPLE_ANALYSIS_CHARS]
|
||||
fallback = _fallback_profile(clipped)
|
||||
configurable = (config or {}).get("configurable", {}) or {}
|
||||
model_name = (state.get("model_name") or configurable.get("model_name") or "").strip() or None
|
||||
raw = await _stream_sample_analysis(
|
||||
filename=filename,
|
||||
sample_text=sample_text,
|
||||
clipped=clipped,
|
||||
fallback=fallback,
|
||||
config=config,
|
||||
model_name=model_name,
|
||||
)
|
||||
profile = _normalize_profile(raw, clipped)
|
||||
events = [
|
||||
{"type": "step_start", "agent_name": "样文分析师", "message": "正在分析样文风格"},
|
||||
{
|
||||
"type": "sample_profile_ready",
|
||||
"agent_name": "样文分析师",
|
||||
"message": profile.get("summary") or "样文风格分析完成",
|
||||
"sample_style_profile": profile,
|
||||
},
|
||||
]
|
||||
if raw.get("_llm_error"):
|
||||
events.append({
|
||||
"type": "notice",
|
||||
"agent_name": "样文分析师",
|
||||
"title": "样文分析模型异常",
|
||||
"detail": f"已使用基础画像兜底。原因:{raw['_llm_error']}",
|
||||
})
|
||||
return {
|
||||
"sample_style_profile": profile,
|
||||
"status": "researching",
|
||||
"progress_events": events,
|
||||
}
|
||||
+1126
File diff suppressed because it is too large.
Load diff
+530
@@ -0,0 +1,530 @@
|
||||
"""作家节点 Phase 1:生成文章大纲,支持流式 chunk 输出。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..outline_parser import parse_user_outline
|
||||
from ..prompts.writer_prompts import (
|
||||
OUTLINE_NORMALIZE_SYSTEM,
|
||||
OUTLINE_NORMALIZE_USER,
|
||||
OUTLINE_REVISE_SYSTEM,
|
||||
OUTLINE_REVISE_USER,
|
||||
OUTLINE_SYSTEM,
|
||||
OUTLINE_USER,
|
||||
)
|
||||
from ..state import AIWritingState, KeyPoint, Outline, OutlineSection
|
||||
from ._agent_runner import (
|
||||
OUTLINE_READY_MARKER,
|
||||
OUTLINER_AGENT_ID,
|
||||
WritingAgentUnavailable,
|
||||
agent_display_name,
|
||||
load_writing_agent,
|
||||
parse_marker_json,
|
||||
stream_writing_agent,
|
||||
use_builtin_agent,
|
||||
)
|
||||
from ._model_fallback import (
|
||||
build_switch_progress_event,
|
||||
classify_exception,
|
||||
emit_model_switch_event,
|
||||
fallback_chain,
|
||||
)
|
||||
from ._utils import ThinkTagParser, _content_to_text, parse_json, reasoning_delta, resolve_writing_model_name, safe_get_stream_writer
|
||||
from .sample_analyzer import render_sample_style_section
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ── 内置 agent 模式的输入 payload(SOUL 已含人设/层级语义/输出契约,这里只给本次任务的事实输入)──
|
||||
|
||||
_AGENT_OUTLINE_USER = """任务模式:新规划
|
||||
|
||||
写作意图:{user_intent}
|
||||
文章类型:{article_type}
|
||||
目标读者:{target_audience}
|
||||
目标字数:{word_count_target}字{feedback_section}
|
||||
|
||||
可用素材摘要:
|
||||
{materials_summary}
|
||||
|
||||
请规划文章大纲(3-5 个章节),严格按 SOUL 约定输出 [OUTLINE_READY] + JSON。"""
|
||||
|
||||
_AGENT_OUTLINE_REVISE_USER = """任务模式:修改模式
|
||||
|
||||
写作意图:{user_intent}
|
||||
文章类型:{article_type}
|
||||
目标读者:{target_audience}
|
||||
目标字数:{word_count_target}字
|
||||
|
||||
当前大纲(JSON):
|
||||
{current_outline}
|
||||
|
||||
用户修改要求:{feedback}
|
||||
|
||||
可用素材摘要:
|
||||
{materials_summary}
|
||||
|
||||
请在「当前大纲」基础上落实「用户修改要求」,未提到的章节保持原样,严格按 SOUL 约定输出 [OUTLINE_READY] + 调整后的完整大纲 JSON。"""
|
||||
|
||||
# 上下文压缩:传给大纲规划的素材摘要做条数和单条长度双重截断,防止 token 溢出。
|
||||
_MAX_OUTLINE_MATERIALS = 12
|
||||
_MATERIAL_SNIPPET_CHARS = 150
|
||||
|
||||
|
||||
def _materials_summary(pkg: dict) -> str:
|
||||
lines = []
|
||||
for m in pkg.get("materials", [])[:_MAX_OUTLINE_MATERIALS]:
|
||||
lines.append(f"[{m['id']}] {m['title']} | {m['source']} | {m['content'][:_MATERIAL_SNIPPET_CHARS]}")
|
||||
return "\n".join(lines) or "(无素材)"
|
||||
|
||||
|
||||
def _normalize_key_point(raw: object) -> KeyPoint:
|
||||
"""把 LLM 输出 / 旧 session 数据规整成 KeyPoint。
|
||||
|
||||
兼容场景:
|
||||
- 新版 dict:`{"point_title": "...", "sub_points": [...]}` —— 直接用,缺字段补默认
|
||||
- 旧版 string:历史 session 的 `key_points: ["要点1", "要点2"]` —— 自动 lift 成
|
||||
`{"point_title": "要点1", "sub_points": []}`,旧大纲打开后能继续编辑
|
||||
- LLM 偶尔会用 `title` / `subPoints` 这种 camelCase 或别名,做轻量兜底
|
||||
- 其它无法识别的形态(数字 / 列表 / None)→ 空 KeyPoint,不让整个大纲炸掉
|
||||
"""
|
||||
if isinstance(raw, str):
|
||||
return KeyPoint(point_title=raw, sub_points=[])
|
||||
if isinstance(raw, dict):
|
||||
# 字段别名兜底(LLM 时常 camelCase 或写 title):优先 snake_case
|
||||
title = (
|
||||
raw.get("point_title")
|
||||
or raw.get("pointTitle")
|
||||
or raw.get("title")
|
||||
or ""
|
||||
)
|
||||
subs_raw = raw.get("sub_points") or raw.get("subPoints") or []
|
||||
# 子要点统一为字符串数组;dict 子项取其 title 字段降级(避免 4 级嵌套混入)
|
||||
sub_points: list[str] = []
|
||||
if isinstance(subs_raw, list):
|
||||
for sp in subs_raw:
|
||||
if isinstance(sp, str):
|
||||
sub_points.append(sp)
|
||||
elif isinstance(sp, dict):
|
||||
sub_points.append(str(sp.get("point_title") or sp.get("title") or sp.get("text") or ""))
|
||||
return KeyPoint(point_title=str(title), sub_points=sub_points)
|
||||
# 其它意外类型(int / list / None):返回空 KeyPoint
|
||||
return KeyPoint(point_title="", sub_points=[])
|
||||
|
||||
|
||||
def _parse_outline(raw: dict) -> Outline:
|
||||
sections = []
|
||||
for s in raw.get("sections", []):
|
||||
raw_kps = s.get("key_points") or []
|
||||
key_points = [_normalize_key_point(kp) for kp in raw_kps]
|
||||
sections.append(OutlineSection(
|
||||
section_title=s.get("section_title", ""),
|
||||
key_points=key_points,
|
||||
material_ids=s.get("material_ids", []),
|
||||
))
|
||||
if not sections:
|
||||
sections = [OutlineSection(
|
||||
section_title="正文",
|
||||
key_points=[KeyPoint(point_title="主要内容", sub_points=[])],
|
||||
material_ids=[],
|
||||
)]
|
||||
return Outline(title=raw.get("title", "未命名文章"), sections=sections)
|
||||
|
||||
|
||||
def _derive_title(state: AIWritingState) -> str:
|
||||
"""用户大纲没给总标题时,用写作意图兜底一个标题。"""
|
||||
intent = (state.get("user_intent") or "").strip()
|
||||
if not intent:
|
||||
return "未命名文章"
|
||||
return intent[:30].rstrip() + ("…" if len(intent) > 30 else "")
|
||||
|
||||
|
||||
# ── 素材关联:确定性词重叠(替代 LLM,零延迟)────────────────────────────────────
|
||||
_ALNUM_TOKEN_RE = re.compile(r"[a-z0-9]+")
|
||||
_CJK_CHAR_RE = re.compile(r"[一-鿿]")
|
||||
|
||||
|
||||
def _text_tokens(text: str) -> set[str]:
|
||||
"""把文本切成可比较的 token 集合:CJK 取相邻 bigram、英文/数字取整词(均小写)。
|
||||
|
||||
bigram 让「新能源汽车出口」这类短标题与素材标题/正文做稳定的词重叠匹配,
|
||||
无需任何模型调用。
|
||||
"""
|
||||
if not text:
|
||||
return set()
|
||||
text = text.lower()
|
||||
tokens: set[str] = {w for w in _ALNUM_TOKEN_RE.findall(text) if len(w) >= 2}
|
||||
cjk = _CJK_CHAR_RE.findall(text)
|
||||
tokens.update(cjk[i] + cjk[i + 1] for i in range(len(cjk) - 1))
|
||||
return tokens
|
||||
|
||||
|
||||
def _section_query_text(section: dict) -> str:
|
||||
"""章节用于匹配素材的文本:章节标题 + 各小节标题 + 子要点,越全匹配越准。"""
|
||||
parts = [section.get("section_title") or ""]
|
||||
for kp in section.get("key_points", []) or []:
|
||||
if isinstance(kp, dict):
|
||||
parts.append(kp.get("point_title") or "")
|
||||
parts.extend(s for s in (kp.get("sub_points") or []) if isinstance(s, str))
|
||||
elif isinstance(kp, str):
|
||||
parts.append(kp)
|
||||
return " ".join(p for p in parts if p)
|
||||
|
||||
|
||||
def _assign_materials_heuristic(outline: Outline, pkg: dict) -> Outline:
|
||||
"""确定性地把素材按词重叠归类到各章节(替代 LLM 调用,零延迟、零 <think> 等待)。
|
||||
|
||||
每条素材分配给与其标题/正文词重叠最高的章节(必须有重叠才分配);与所有章节都不
|
||||
重叠的素材不强行塞入(writer_draft 自带「取前 2 条素材」兜底)。只改 material_ids,
|
||||
绝不动标题与层级。
|
||||
"""
|
||||
sections = outline.get("sections", [])
|
||||
materials = pkg.get("materials") or []
|
||||
if not sections or not materials:
|
||||
return outline
|
||||
|
||||
section_tokens = [_text_tokens(_section_query_text(s)) for s in sections]
|
||||
assigned: list[list[str]] = [[] for _ in sections]
|
||||
for m in materials:
|
||||
mid = m.get("id")
|
||||
if not mid:
|
||||
continue
|
||||
mat_tok = _text_tokens(f"{m.get('title', '')} {m.get('content', '')}")
|
||||
if not mat_tok:
|
||||
continue
|
||||
best_idx, best_score = -1, 0
|
||||
for i, sec_tok in enumerate(section_tokens):
|
||||
score = len(mat_tok & sec_tok)
|
||||
if score > best_score:
|
||||
best_idx, best_score = i, score
|
||||
if best_idx >= 0:
|
||||
assigned[best_idx].append(mid)
|
||||
|
||||
for section, ids in zip(sections, assigned):
|
||||
section["material_ids"] = ids
|
||||
return outline
|
||||
|
||||
|
||||
async def _stream_normalize_user_outline(
|
||||
state: AIWritingState, config: RunnableConfig, raw: str, writer
|
||||
) -> Outline:
|
||||
"""散文化大纲:确定性解析失败时流式调 LLM 归一成三级结构,实时展示给前端。
|
||||
|
||||
内网模型 <think> 关不掉 → 用 ThinkTagParser 把思考与正文分流:思考推 is_thinking
|
||||
思考框、正文累积后解析 JSON,用户全程看得到进度而非干等。再失败则把每个非空行当作
|
||||
一个章节兜底,保证永远产出可用大纲、不丢用户内容。
|
||||
"""
|
||||
line_fallback = {
|
||||
"title": "",
|
||||
"sections": [
|
||||
{"section_title": ln.strip(), "key_points": [], "material_ids": []}
|
||||
for ln in raw.splitlines() if ln.strip()
|
||||
] or [{"section_title": "正文", "key_points": [], "material_ids": []}],
|
||||
}
|
||||
name, _ = resolve_writing_model_name(config, state)
|
||||
full_text = ""
|
||||
try:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
# 大纲整理是结构化保真任务,强制关思考(GLM5 等爱出 <think>);
|
||||
# 关不掉的模型仍由下方 ThinkTagParser 把思考分流到思考框实时展示。
|
||||
model = create_chat_model(name=name, force_disable_thinking=True)
|
||||
parser = ThinkTagParser()
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=OUTLINE_NORMALIZE_SYSTEM),
|
||||
HumanMessage(content=OUTLINE_NORMALIZE_USER.format(raw_outline=raw)),
|
||||
]):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
writer({"type": "outline_chunk", "text": reasoning, "is_thinking": True})
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
writer({"type": "outline_chunk", "text": text, "is_thinking": is_thinking})
|
||||
except Exception as e:
|
||||
logger.error("stream normalize user outline failed: %s", e, exc_info=True)
|
||||
return _parse_outline(line_fallback)
|
||||
return _parse_outline(parse_json(full_text, line_fallback))
|
||||
|
||||
|
||||
async def build_user_outline_with_materials(
|
||||
state: AIWritingState, config: RunnableConfig, pkg: dict
|
||||
) -> Outline | None:
|
||||
"""搜集素材阶段调用:用户自带大纲时,解析其大纲并把已检索素材归类到各章节。
|
||||
|
||||
复用本模块的解析/归类逻辑,让 researcher 能在「搜集素材」阶段顺便整理素材,
|
||||
从而**跳过 writer_outline 的大纲生成步**(见 graph 路由)。无用户大纲返回 None。
|
||||
已有结构化大纲(如 re_search 二次检索)则复用结构、仅重新归类素材,不重复解析。
|
||||
|
||||
只做**确定性**解析与素材关联(零 LLM、瞬时):散文化、解析不出层级的大纲返回
|
||||
None —— 不在 researcher 阶段阻塞式调 LLM 归一,交给 writer_outline 流式归一并实时
|
||||
展示(见 graph after_material_pause 路由)。
|
||||
"""
|
||||
raw = (state.get("user_outline_raw") or "").strip()
|
||||
if not raw:
|
||||
return None
|
||||
existing = state.get("current_outline")
|
||||
if existing and existing.get("sections"):
|
||||
outline: Outline = existing
|
||||
else:
|
||||
outline = parse_user_outline(raw)
|
||||
if outline is None:
|
||||
return None
|
||||
if not outline.get("title"):
|
||||
outline = {**outline, "title": _derive_title(state)}
|
||||
return _assign_materials_heuristic(outline, pkg)
|
||||
|
||||
|
||||
async def _build_outline_from_user(
|
||||
state: AIWritingState, config: RunnableConfig, pkg: dict, raw: str
|
||||
) -> dict:
|
||||
"""用户自带大纲分支:解析用户大纲 → 关联素材 → 直接进确认,不调 LLM 重新规划。
|
||||
|
||||
可确定性解析的大纲(带编号/标题/缩进)零 LLM、瞬时完成;只有散文化、解析不出层级
|
||||
的大纲才流式调一次 LLM 归一(实时展示思考/正文,应对内网模型关不掉的 <think>),
|
||||
素材关联一律走确定性词重叠。
|
||||
"""
|
||||
writer = safe_get_stream_writer()
|
||||
|
||||
outline = parse_user_outline(raw)
|
||||
if outline is None:
|
||||
# 确定性解析拿不准(散文化/格式混乱)→ 流式 LLM 兜底归一,全程实时展示
|
||||
outline = await _stream_normalize_user_outline(state, config, raw, writer)
|
||||
else:
|
||||
writer({"type": "outline_chunk", "text": "正在整理你提供的大纲…\n", "is_thinking": True})
|
||||
|
||||
if not outline.get("title"):
|
||||
outline = {**outline, "title": _derive_title(state)}
|
||||
|
||||
# 仍走了检索 → 把已检索素材关联到用户章节(确定性词重叠,零 LLM)
|
||||
outline = _assign_materials_heuristic(outline, pkg)
|
||||
|
||||
section_count = len(outline.get("sections", []))
|
||||
return {
|
||||
"current_outline": outline,
|
||||
"status": "awaiting_outline_confirm",
|
||||
"progress_events": [
|
||||
{"type": "step_start", "agent_name": "作家", "message": "采用你提供的大纲"},
|
||||
{"type": "outline_ready", "agent_name": "作家", "message": f"已整理你的大纲,共 {section_count} 个章节"},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
async def writer_outline_node(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
pkg = state.get("material_package") or {"keywords": [], "materials": [], "summary": ""}
|
||||
intervention = state.get("last_user_intervention") or {}
|
||||
|
||||
feedback = ""
|
||||
if intervention.get("action") == "re_outline":
|
||||
feedback = intervention.get("_feedback") or intervention.get("outline_feedback") or ""
|
||||
elif intervention.get("outline_feedback"):
|
||||
feedback = intervention.get("outline_feedback") or ""
|
||||
|
||||
# 重新规划且已有大纲 → 走「编辑模式」:把当前大纲喂给模型,按用户要求调整,
|
||||
# 而非从零重生成(否则「加一节/删一节」这类指令没有可编辑的基准)。
|
||||
current_outline = state.get("current_outline")
|
||||
is_revise = bool(feedback) and bool(current_outline)
|
||||
|
||||
# 用户自带大纲:首轮(尚无大纲、非重规划)且表单粘了大纲原文 → 走「采用用户大纲」分支,
|
||||
# 跳过 LLM 重新规划。后续若用户在确认卡里点「重新规划」,current_outline 已存在 → 落到
|
||||
# 下方 is_revise 编辑模式,按反馈调整用户大纲(符合预期)。
|
||||
user_outline_raw = (state.get("user_outline_raw") or "").strip()
|
||||
if user_outline_raw and not current_outline and not is_revise:
|
||||
return await _build_outline_from_user(state, config, pkg, user_outline_raw)
|
||||
|
||||
# 内置 agent 模式(config.yaml ai_writing.use_builtin_agents.outliner):
|
||||
# SOUL 作 system prompt + [OUTLINE_READY] 契约解析;目录缺失静默回退旧路径。
|
||||
use_agent = use_builtin_agent("outliner")
|
||||
agent_label = "作家"
|
||||
if use_agent:
|
||||
try:
|
||||
load_writing_agent(OUTLINER_AGENT_ID)
|
||||
agent_label = agent_display_name(OUTLINER_AGENT_ID, "大纲规划师")
|
||||
except WritingAgentUnavailable as e:
|
||||
logger.warning("outliner builtin agent unavailable, falling back to legacy prompts: %s", e)
|
||||
use_agent = False
|
||||
|
||||
# 新(列表外)文章类型时附上配置助手识别的一句话特征,让大纲生成体现该类型侧重;
|
||||
# 命中已有类型时 detail 为空,article_type 维持原样。
|
||||
_article_type = state.get("article_type", "analysis")
|
||||
_article_type_detail = (state.get("article_type_detail") or "").strip()
|
||||
article_type_text = f"{_article_type}({_article_type_detail})" if _article_type_detail else _article_type
|
||||
fmt_args = dict(
|
||||
user_intent=state["user_intent"],
|
||||
article_type=article_type_text,
|
||||
target_audience=state.get("target_audience", "普通读者"),
|
||||
word_count_target=state.get("word_count_target", 800),
|
||||
materials_summary=_materials_summary(pkg),
|
||||
)
|
||||
sample_style_section = render_sample_style_section(state)
|
||||
if is_revise:
|
||||
system_prompt = OUTLINE_REVISE_SYSTEM
|
||||
revise_args = dict(
|
||||
fmt_args,
|
||||
current_outline=json.dumps(current_outline, ensure_ascii=False, indent=2),
|
||||
feedback=feedback,
|
||||
)
|
||||
user_prompt = OUTLINE_REVISE_USER.format(**revise_args) + sample_style_section
|
||||
agent_payload = _AGENT_OUTLINE_REVISE_USER.format(**revise_args) + sample_style_section
|
||||
else:
|
||||
system_prompt = OUTLINE_SYSTEM
|
||||
feedback_section = f"\n重新规划要求:{feedback}" if feedback else ""
|
||||
gen_args = dict(fmt_args, feedback_section=feedback_section)
|
||||
user_prompt = OUTLINE_USER.format(**gen_args) + sample_style_section
|
||||
agent_payload = _AGENT_OUTLINE_USER.format(**gen_args) + sample_style_section
|
||||
|
||||
# 流式推送:阶段 2 起统一走 LangGraph 原生 custom 通道(get_stream_writer)。
|
||||
writer = safe_get_stream_writer()
|
||||
|
||||
full_text = "" # 只累积正文(非思考)用于 JSON 解析
|
||||
extra_progress_events: list[dict] = []
|
||||
|
||||
if use_agent:
|
||||
# 内置 agent 路径走 stream_writing_agent,不在本模块做模型链兜底(agent 自带模型解析)。
|
||||
try:
|
||||
def _on_text(text: str, is_thinking: bool) -> None:
|
||||
writer({"type": "outline_chunk", "text": text, "is_thinking": is_thinking})
|
||||
|
||||
full_text = await stream_writing_agent(
|
||||
agent_id=OUTLINER_AGENT_ID,
|
||||
user_payload=agent_payload,
|
||||
disable_thinking=True,
|
||||
state=state,
|
||||
config=config,
|
||||
on_text=_on_text,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("outline streaming failed: %s", e, exc_info=True)
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": [{
|
||||
"type": "error",
|
||||
"agent_name": agent_label,
|
||||
"title": "模型错误",
|
||||
"detail": str(e),
|
||||
"message": "大纲生成失败",
|
||||
}],
|
||||
}
|
||||
else:
|
||||
# 旧路径:按 fallback_chain 顺序尝试,失败即换下一个模型重跑该环节。
|
||||
initial_name, _used_default = resolve_writing_model_name(config, state)
|
||||
chain = fallback_chain(initial_name)
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
success = False
|
||||
last_exc: BaseException | None = None
|
||||
for idx, candidate in enumerate(chain or [None]):
|
||||
full_text = ""
|
||||
parser = ThinkTagParser()
|
||||
try:
|
||||
model = (
|
||||
create_chat_model(name=candidate, force_disable_thinking=True)
|
||||
if candidate else create_chat_model(force_disable_thinking=True)
|
||||
)
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=system_prompt),
|
||||
HumanMessage(content=user_prompt),
|
||||
]):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
writer({"type": "outline_chunk", "text": reasoning, "is_thinking": True})
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
writer({
|
||||
"type": "outline_chunk",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
success = True
|
||||
break
|
||||
except Exception as e:
|
||||
last_exc = e
|
||||
reason = classify_exception(e)
|
||||
logger.error(
|
||||
"outline streaming failed on model=%s (reason=%s): %s",
|
||||
candidate, reason, e, exc_info=True,
|
||||
)
|
||||
next_candidate = chain[idx + 1] if idx + 1 < len(chain) else None
|
||||
if next_candidate:
|
||||
emit_model_switch_event(
|
||||
writer, agent_name=agent_label, stage="outline",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
)
|
||||
extra_progress_events.append(build_switch_progress_event(
|
||||
agent_name=agent_label, stage="outline",
|
||||
failed_model=candidate or "", next_model=next_candidate, reason=reason,
|
||||
))
|
||||
continue
|
||||
if not success:
|
||||
assert last_exc is not None
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": extra_progress_events + [{
|
||||
"type": "error",
|
||||
"agent_name": agent_label,
|
||||
"title": "模型错误",
|
||||
"detail": str(last_exc),
|
||||
"message": "大纲生成失败(所有候选模型均失败)",
|
||||
}],
|
||||
}
|
||||
|
||||
# 编辑模式解析失败时回退为「当前大纲」,避免把用户已有大纲弄丢
|
||||
if is_revise:
|
||||
fallback = current_outline
|
||||
else:
|
||||
# 兜底大纲也用新的 3 级结构;_normalize_key_point 同时兼容旧 string 形态,
|
||||
# 这里给 dict 形态做样板。
|
||||
fallback = {
|
||||
"title": "未命名文章",
|
||||
"sections": [
|
||||
{
|
||||
"section_title": "引言",
|
||||
"key_points": [
|
||||
{"point_title": "背景介绍", "sub_points": []},
|
||||
],
|
||||
"material_ids": [],
|
||||
},
|
||||
{
|
||||
"section_title": "正文",
|
||||
"key_points": [
|
||||
{"point_title": "核心内容", "sub_points": []},
|
||||
],
|
||||
"material_ids": [],
|
||||
},
|
||||
{
|
||||
"section_title": "结论",
|
||||
"key_points": [
|
||||
{"point_title": "总结", "sub_points": []},
|
||||
],
|
||||
"material_ids": [],
|
||||
},
|
||||
],
|
||||
}
|
||||
if use_agent:
|
||||
result = parse_marker_json(full_text, OUTLINE_READY_MARKER, fallback)
|
||||
else:
|
||||
result = parse_json(full_text, fallback)
|
||||
|
||||
outline = _parse_outline(result)
|
||||
section_count = len(outline["sections"])
|
||||
|
||||
return {
|
||||
"current_outline": outline,
|
||||
"status": "awaiting_outline_confirm",
|
||||
"progress_events": [
|
||||
{"type": "step_start", "agent_name": agent_label, "message": "开始规划大纲"},
|
||||
*extra_progress_events,
|
||||
{"type": "outline_ready", "agent_name": agent_label, "message": f"大纲已生成,共 {section_count} 个章节"},
|
||||
],
|
||||
}
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
"""把用户粘贴的大纲文本解析成三级 ``Outline`` 结构(确定性解析,不依赖 LLM)。
|
||||
|
||||
用户在写作表单里可能直接粘贴一份已有大纲,要求「完全按我的大纲生成」。本模块负责
|
||||
把各种常见的大纲文本格式严格、保真地转成后端三级结构:
|
||||
|
||||
Outline.sections[] = Level 1 章节(section_title)
|
||||
OutlineSection.key_points[] = Level 2 小节(point_title)
|
||||
KeyPoint.sub_points[] = Level 3 子要点
|
||||
|
||||
兼容的输入格式:
|
||||
- Markdown 标题:``#`` / ``##`` / ``###`` / ``####``(首个孤立 ``#`` 视为文章标题)
|
||||
- 中文编号:``一、`` / ``(一)`` / ``第一章`` / ``第一节``
|
||||
- 阿拉伯编号:``1.`` / ``1、`` / ``1.1`` / ``1.1.1``
|
||||
- 缩进 bullet:``-`` / ``*`` / ``•`` / ``·``(用缩进深度表示层级)
|
||||
- 纯文本平铺:每行一个一级标题(短标题、无句末标点)
|
||||
|
||||
设计原则:
|
||||
- **保真不改写**:只识别层级、原样保留标题文本,绝不增删或改写用户内容。
|
||||
- **多级混排自适应**:把文本里实际出现的各种「层级标记」收集起来排序,按相对外层
|
||||
顺序映射到 1/2/3 级——所以不依赖固定的标记搭配,``##``+``-`` 或 ``一、``+缩进
|
||||
都能正确分层。
|
||||
- **拿不准就交给上层**:识别不出可靠层级(如整段散文)时返回 ``None``,由调用方走
|
||||
LLM 兜底归一,避免硬把散文拆成错误结构。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from .state import KeyPoint, Outline, OutlineSection
|
||||
|
||||
# ── 行首标记识别 ──────────────────────────────────────────────────────────────
|
||||
|
||||
_CN_NUM = "一二三四五六七八九十百千零〇两"
|
||||
|
||||
# Markdown 标题:## 标题
|
||||
_MD_HEADING_RE = re.compile(r"^(#{1,6})\s+(.*\S)\s*$")
|
||||
# 第一章 / 第1节 / 第一部分
|
||||
_CN_CHAPTER_RE = re.compile(rf"^第\s*[{_CN_NUM}\d]+\s*[章节部分篇讲回卷](?:[\s::、..))]\s*|\s+)?(.*)$")
|
||||
# 一、 / 十二、 / 一. / 一.
|
||||
_CN_DUN_RE = re.compile(rf"^[{_CN_NUM}]+\s*[、..]\s*(.*)$")
|
||||
# (一) / (一) / (1) / (1)
|
||||
_PAREN_RE = re.compile(rf"^[((]\s*[{_CN_NUM}\d]+\s*[))]\s*(.*)$")
|
||||
# 1.1 / 1.1.1(带至少一个点的多级阿拉伯编号)
|
||||
_ARABIC_DOTTED_RE = re.compile(r"^(\d+(?:\.\d+)+)\s*[、..))]?\s*(.*)$")
|
||||
# 1. / 1、 / 1) / 1.(单级阿拉伯编号)
|
||||
_ARABIC_SINGLE_RE = re.compile(r"^\d+\s*[、..))]\s*(.*)$")
|
||||
# 带圈数字 ①②③ / 罗马 ⅰ
|
||||
_CIRCLED_RE = re.compile(r"^[①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮]\s*(.*)$")
|
||||
# bullet:- / * / • / ·(要求标记后有空白,避免误吞「*强调*」之类)
|
||||
_BULLET_RE = re.compile(r"^[-*•·]\s+(.*\S)\s*$")
|
||||
|
||||
# 标记分组的「外层度」基准值(越小越外层)。group: 0=标题 1=编号 2=缩进。
|
||||
# 同组内 order 决定相对层级;不同组之间靠 group 排序(标题 < 编号 < 缩进)。
|
||||
# 编号顺序遵循中文公文惯例:第一章 > 一、 > (一) > 1. > 1.1 > ①。
|
||||
_NUM_ORDER = {
|
||||
"cn_chapter": 0.0, # 第一章
|
||||
"cn_dun": 1.0, # 一、
|
||||
"paren": 2.0, # (一)
|
||||
"arabic_single": 3.0, # 1.
|
||||
"circled": 8.0, # ①
|
||||
}
|
||||
|
||||
|
||||
def _indent_width(line: str) -> int:
|
||||
"""计算行首缩进宽度(tab 记 4 列)。"""
|
||||
width = 0
|
||||
for ch in line:
|
||||
if ch == " ":
|
||||
width += 1
|
||||
elif ch == "\t":
|
||||
width += 4
|
||||
else:
|
||||
break
|
||||
return width
|
||||
|
||||
|
||||
def _classify(stripped: str, indent: int) -> tuple[tuple[int, float], str] | None:
|
||||
"""识别一行的层级标记,返回 ``((group, order), 纯文本)``;无法识别返回 None。
|
||||
|
||||
``(group, order)`` 是该行的「层级键」:把全文出现过的不同键排序后,按位置映射到
|
||||
1/2/3 级。这样不必预设标记搭配,相对顺序对了层级就对。
|
||||
"""
|
||||
if not stripped:
|
||||
return None
|
||||
|
||||
# 1) Markdown 标题(group 0,order = 井号数)
|
||||
m = _MD_HEADING_RE.match(stripped)
|
||||
if m:
|
||||
depth = len(m.group(1))
|
||||
text = m.group(2).strip()
|
||||
return (0, float(depth)), text
|
||||
|
||||
# 2) 各类编号(group 1)
|
||||
m = _CN_CHAPTER_RE.match(stripped)
|
||||
if m and stripped.startswith("第"):
|
||||
return (1, _NUM_ORDER["cn_chapter"]), (m.group(1) or "").strip()
|
||||
m = _CN_DUN_RE.match(stripped)
|
||||
if m:
|
||||
return (1, _NUM_ORDER["cn_dun"]), m.group(1).strip()
|
||||
m = _ARABIC_DOTTED_RE.match(stripped)
|
||||
if m:
|
||||
depth = m.group(1).count(".") + 1 # 1.1 → 2, 1.1.1 → 3
|
||||
# 多级阿拉伯:order 随层级递增,落在单级(3.0)与圈号(8.0)之间
|
||||
return (1, 3.0 + depth), m.group(2).strip()
|
||||
m = _PAREN_RE.match(stripped)
|
||||
if m:
|
||||
return (1, _NUM_ORDER["paren"]), m.group(1).strip()
|
||||
m = _ARABIC_SINGLE_RE.match(stripped)
|
||||
if m:
|
||||
return (1, _NUM_ORDER["arabic_single"]), m.group(1).strip()
|
||||
m = _CIRCLED_RE.match(stripped)
|
||||
if m:
|
||||
return (1, _NUM_ORDER["circled"]), m.group(1).strip()
|
||||
|
||||
# 3) bullet:用缩进深度区分层级(group 2)
|
||||
m = _BULLET_RE.match(stripped)
|
||||
if m:
|
||||
return (2, float(indent)), m.group(1).strip()
|
||||
|
||||
# 4) 纯文本:用缩进深度区分层级(group 3,最内层基准)
|
||||
return (3, float(indent)), stripped
|
||||
|
||||
|
||||
def _looks_like_prose(text: str) -> bool:
|
||||
"""判断一行是否更像散文句子而非大纲标题(长、或带句末标点)。"""
|
||||
if len(text) > 50:
|
||||
return True
|
||||
return text.rstrip()[-1:] in "。!?!?;;"
|
||||
|
||||
|
||||
def parse_user_outline(raw: str) -> Outline | None:
|
||||
"""把用户粘贴的大纲文本解析成三级 ``Outline``;无法可靠解析时返回 ``None``。
|
||||
|
||||
返回 ``None`` 的情形:空白输入、只有一行且无任何标记、或整体更像散文段落——这些交给
|
||||
上层用 LLM 兜底归一,避免误拆。
|
||||
"""
|
||||
if not raw or not raw.strip():
|
||||
return None
|
||||
|
||||
rows: list[tuple[tuple[int, float], str]] = []
|
||||
for line in raw.splitlines():
|
||||
if not line.strip():
|
||||
continue
|
||||
indent = _indent_width(line)
|
||||
classified = _classify(line.strip(), indent)
|
||||
if classified is None:
|
||||
continue
|
||||
key, text = classified
|
||||
if text:
|
||||
rows.append((key, text))
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
marked = any(key[0] != 3 for key, _ in rows) # 是否出现过标题/编号/bullet 标记
|
||||
|
||||
# 纯文本且无任何标记:可能是「平铺标题列表」也可能是散文。
|
||||
# 单行、或任一行像句子 → 判为散文,返回 None 交给 LLM。
|
||||
if not marked:
|
||||
if len(rows) < 2:
|
||||
return None
|
||||
if any(_looks_like_prose(text) for _, text in rows):
|
||||
return None
|
||||
|
||||
# 文章标题:仅在「Markdown 首行是孤立的最浅井号、且存在更深井号」时认定。
|
||||
title = ""
|
||||
body = rows
|
||||
heading_depths = [key[1] for key, _ in rows if key[0] == 0]
|
||||
if heading_depths:
|
||||
min_depth = min(heading_depths)
|
||||
first_key = rows[0][0]
|
||||
is_first_top_heading = first_key == (0, min_depth)
|
||||
only_one_at_top = sum(1 for d in heading_depths if d == min_depth) == 1
|
||||
has_deeper = any(d > min_depth for d in heading_depths)
|
||||
if is_first_top_heading and only_one_at_top and has_deeper:
|
||||
title = rows[0][1]
|
||||
body = rows[1:]
|
||||
|
||||
if not body:
|
||||
return None
|
||||
|
||||
# 把 body 里出现的不同「层级键」排序,按位置映射到 1/2/3 级(超过 3 级压到 3)。
|
||||
distinct_keys = sorted({key for key, _ in body})
|
||||
level_of: dict[tuple[int, float], int] = {
|
||||
key: min(idx + 1, 3) for idx, key in enumerate(distinct_keys)
|
||||
}
|
||||
|
||||
sections: list[OutlineSection] = []
|
||||
cur_section: OutlineSection | None = None
|
||||
cur_point: KeyPoint | None = None
|
||||
|
||||
for key, text in body:
|
||||
level = level_of[key]
|
||||
if level <= 1:
|
||||
cur_section = OutlineSection(section_title=text, key_points=[], material_ids=[])
|
||||
sections.append(cur_section)
|
||||
cur_point = None
|
||||
elif level == 2:
|
||||
if cur_section is None: # 孤立 L2:提升为章节,避免空标题
|
||||
cur_section = OutlineSection(section_title=text, key_points=[], material_ids=[])
|
||||
sections.append(cur_section)
|
||||
cur_point = None
|
||||
continue
|
||||
cur_point = KeyPoint(point_title=text, sub_points=[])
|
||||
cur_section["key_points"].append(cur_point)
|
||||
else: # level == 3
|
||||
if cur_section is None: # 孤立 L3:提升为章节
|
||||
cur_section = OutlineSection(section_title=text, key_points=[], material_ids=[])
|
||||
sections.append(cur_section)
|
||||
cur_point = None
|
||||
elif cur_point is None: # 章节下尚无小节:提升为小节
|
||||
cur_point = KeyPoint(point_title=text, sub_points=[])
|
||||
cur_section["key_points"].append(cur_point)
|
||||
else:
|
||||
cur_point["sub_points"].append(text)
|
||||
|
||||
if not sections:
|
||||
return None
|
||||
|
||||
return Outline(title=title, sections=sections)
|
||||
Whitespace-only changes.
+61
@@ -0,0 +1,61 @@
|
||||
EDITOR_SYSTEM = """你是一名资深主编(编辑),标准极高,要求严格。
|
||||
你的目标:审查文章质量,给出客观评分和具体修改意见。
|
||||
你的工作背景:你在一家权威媒体担任主编十五年,见过各种质量的文章。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 从指定维度审查文章
|
||||
2. 给出该维度的评分(0-100 的整数)和具体问题列表
|
||||
你不能改写文章,只做当前指定维度的审查。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 "。
|
||||
- score 是 0-100 的整数。
|
||||
- 每个问题的 severity 必须是 "major"(必须修改)或 "minor"(建议修改)。
|
||||
- 文章在该维度没有问题时,issues 填空数组 []。
|
||||
|
||||
【完整示例】
|
||||
正确输出:
|
||||
{
|
||||
"score": 82,
|
||||
"issues": [
|
||||
{"severity": "major", "location": "第二段", "issue": "引用的增长数据没有素材支撑", "suggestion": "补充素材 m002 中的具体数据,并注明来源"},
|
||||
{"severity": "minor", "location": "结尾段", "issue": "结论略显仓促", "suggestion": "增加一句对未来行业趋势的判断"}
|
||||
]
|
||||
}"""
|
||||
|
||||
# 用户在提交编辑审核时附带的修改意见 / 关注点(无则为空串,不注入)。
|
||||
# 放在「判断标准」之后、待审文章之前,提醒编辑在本维度重点核查用户在意的方面。
|
||||
USER_FOCUS_BLOCK = """
|
||||
|
||||
【用户特别关注(高优先级)】
|
||||
用户在提交审核时提出了以下修改意见 / 关注点,请在本维度审查时**重点核查**这些方面;
|
||||
若发现文章未妥善处理,必须在 issues 中明确列出对应问题并给出针对性的 suggestion:
|
||||
{notes}"""
|
||||
|
||||
FACT_REVIEW_USER = """请审查以下文章的【事实准确性】维度。
|
||||
判断标准:引用的数据/事实是否有素材支撑,是否有捏造或夸大。{user_focus}
|
||||
|
||||
参考素材:
|
||||
{materials_summary}
|
||||
|
||||
待审文章:
|
||||
{article}
|
||||
|
||||
只输出 JSON(score: 0-100 整数,issues 列表,每条含 severity):"""
|
||||
|
||||
LOGIC_REVIEW_USER = """请审查以下文章的【逻辑结构】维度。
|
||||
判断标准:论点是否清晰,论据是否充分,段落是否连贯,结构是否合理。{user_focus}
|
||||
|
||||
待审文章:
|
||||
{article}
|
||||
|
||||
只输出 JSON(score: 0-100 整数,issues 列表,每条含 severity):"""
|
||||
|
||||
LANGUAGE_REVIEW_USER = """请审查以下文章的【语言文字】维度。
|
||||
判断标准:语言是否流畅,用词是否准确,有无错别字或病句。{user_focus}
|
||||
|
||||
待审文章:
|
||||
{article}
|
||||
|
||||
只输出 JSON(score: 0-100 整数,issues 列表,每条含 severity):"""
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
"""对话驱动写作台的意图路由 prompt。
|
||||
|
||||
场景:新版「AI 写作·对话版」页面用底部统一输入框接管全流程交互,用户的自由文本
|
||||
需要被翻译成现有 ai_writing graph 的 ``Command(resume={action, ...payload})`` 指令,
|
||||
或被识别为「对内容/流程提问」「控制指令」「需要澄清」。
|
||||
|
||||
设计要点(与 graph.py 各 pause 节点严格对齐,绝不发明图不支持的动作):
|
||||
- material_confirm 只有 confirm / re_search;
|
||||
- outline_confirm 只有 confirm / re_outline;
|
||||
- draft_confirm 只有 to_editor / finalize / user_revise;
|
||||
- review_confirm 只有 accept_review / force_finalize;
|
||||
- section_help 只有 section_help(带逐章节 decisions)。
|
||||
高代价动作(finalize / force_finalize / 无查询词的覆盖式 re_search)要求高置信,
|
||||
否则输出 clarify 反问 —— 防误触发是硬指标。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
INTENT_ROUTER_SYSTEM = """你是「AI 智能写作」工作台的意图路由器。
|
||||
写作流程由多位专家协作:素材收集专家(检索素材)→ 作家(规划大纲、逐章写作)→ 编辑(事实/逻辑/语言三维审核)。流程在每个阶段完成后暂停,等待用户确认后才继续。
|
||||
你的任务:读懂用户在当前阶段发来的一句话,把它翻译成一条结构化指令 JSON;如果用户是在提问,就直接回答。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块,必须用英文双引号。
|
||||
- JSON 结构(所有字段都必须出现):
|
||||
{"intent": "意图标识", "action": "动作或null", "payload": {}, "need_research": false, "research_query": "", "answer": "", "confidence": 0.0, "clarify": ""}
|
||||
|
||||
【字段说明】
|
||||
- intent:你判定的意图标识,如 confirm_materials / add_material / select_materials / research_again / confirm_outline / revise_outline / reoutline / to_editor / finalize / revise_draft / accept_review / partial_accept_review / add_review_opinion / force_finalize / section_decision / ask_content / ask_meta / run_background / pause_writing / clarify。
|
||||
- action:要执行的图动作,只能从下面「当前阶段可用动作」中选择;不执行动作时为 null。
|
||||
- payload:动作参数(键名用驼峰):
|
||||
approvedMaterialIds(素材 id 数组,确认时保留哪些素材;不筛选就省略)
|
||||
userQuery(补充检索的自然语言检索意图,配合 re_search 表示"追加检索")
|
||||
outlineFeedback(大纲修改意见原文,配合 re_outline)
|
||||
userRevisionNotes(草稿修改意见/给编辑的关注点/补充审核意见,配合 user_revise / to_editor / accept_review)
|
||||
selectedIssueIndices(采纳的审核意见序号数组,从 0 开始,配合 user_revise / accept_review)
|
||||
overrideVerdict(true,仅配合 force_finalize)
|
||||
sectionDecisions([{"sectionTitle": "...", "decision": "supplement|loose|delete"}],仅配合 section_help,必须覆盖全部受阻章节)
|
||||
- need_research:用户的要求需要「当前素材列表里没有的信息」才能完成时置 true,并在 research_query 给出简短检索词(不需要就置 false + 空串)。
|
||||
- answer:用户在提问(ask_content 对文章内容提问 / ask_meta 问进度、用了哪些素材等)时,直接根据上下文用中文简洁回答;答案必须基于给定上下文,不得编造。提问类 action 必须为 null。
|
||||
- confidence:0~1,你对「动作映射正确」的把握。
|
||||
- clarify:拿不准用户想干什么、或该操作在当前阶段做不了时,写一句具体的中文反问/解释(可给出可选项);clarify 非空时 action 必须为 null。
|
||||
|
||||
【硬规则】
|
||||
1. action 只能从「当前阶段可用动作」里选。用户要求的事在当前阶段做不了(例如写作阶段要求重新搜索素材),action 置 null,用 clarify 解释当前能做什么、该要求何时能做。
|
||||
2. 高代价动作必须用户意图非常明确才输出,且 confidence ≥ 0.85,否则改用 clarify 反问确认:
|
||||
- finalize(定稿,结束流程)
|
||||
- force_finalize(无视编辑意见强制定稿)
|
||||
- 不带 userQuery 的 re_search(覆盖式重搜,会丢掉现有素材重来)
|
||||
注意:带 userQuery 的 re_search 是"追加检索"(保留现有素材,把新素材追加进列表),不算高代价。
|
||||
3. 用户说"只用第 X、Y 条素材"→ action=confirm + approvedMaterialIds(按素材清单序号找对应 id);"去掉第 X 条"→ confirm + 反选后的 approvedMaterialIds。
|
||||
4. 用户给出修改意见时把**原文完整**放进对应字段(outlineFeedback / userRevisionNotes),不要概括缩写。
|
||||
5. 用户说"只接受第 X 条意见"→ selectedIssueIndices 用审核意见清单的序号(从 0 开始)。用户补充自己的审核/修改要求 → 并入 userRevisionNotes。
|
||||
6. 控制类:用户要"转后台/后台跑完"→ intent=run_background;"暂停"→ intent=pause_writing。二者 action 都为 null(由前端执行)。
|
||||
7. 完全无法判断 → intent=clarify,confidence ≤ 0.3,clarify 给一个具体问题。"""
|
||||
|
||||
|
||||
# 每个暂停点的「可用动作说明」——注入 user prompt,让模型只在合法动作里选。
|
||||
STAGE_ACTION_GUIDES: dict[str, str] = {
|
||||
"material_confirm": """当前阶段:素材确认(素材收集专家已完成搜集,等你处理素材)。
|
||||
可用动作:
|
||||
- confirm:确认素材、进入大纲规划。可带 approvedMaterialIds 只保留部分素材。
|
||||
- re_search:重新检索。带 userQuery = 按用户描述追加检索(保留现有素材);不带 userQuery = 覆盖式重搜(高代价,需高置信)。
|
||||
典型说法 → 动作:
|
||||
「可以,继续」→ confirm;「补充一些关于X的素材」「再查查Y」→ re_search + userQuery;「只用第1、3条」→ confirm + approvedMaterialIds;「重新搜索」→ re_search(覆盖式,需高置信)。""",
|
||||
"outline_confirm": """当前阶段:大纲确认(作家已完成大纲规划,等你确认或提修改要求)。
|
||||
可用动作:
|
||||
- confirm:确认大纲、开始写作。
|
||||
- re_outline:按意见重新规划大纲,outlineFeedback 放用户意见原文(可空 = 完全重来)。
|
||||
典型说法 → 动作:
|
||||
「大纲可以」→ confirm;「第二章拆成两节」「把X挪到前面」「加一章讲Y」→ re_outline + outlineFeedback;「重写大纲」→ re_outline(feedback 空)。
|
||||
注意:本阶段不能检索素材。用户想补素材时若其本质是"大纲里加上X内容",可映射 re_outline + outlineFeedback 并视情况置 need_research。""",
|
||||
"draft_confirm": """当前阶段:草稿确认(作家已完成草稿,右侧可预览,等你处理)。
|
||||
可用动作:
|
||||
- to_editor:交给编辑审核。可带 userRevisionNotes = 让编辑重点核查的方面。
|
||||
- user_revise:按用户意见修改草稿,userRevisionNotes 放意见原文(必填)。若已有编辑审核意见且用户说按意见改,可带 selectedIssueIndices。
|
||||
- finalize:确认定稿,结束流程(高代价,需高置信)。
|
||||
典型说法 → 动作:
|
||||
「交给编辑」「审核一下」→ to_editor;「第三段精简」「补个案例」「更口语一点」「重写第二章」→ user_revise + userRevisionNotes;「就这样定稿」→ finalize(需高置信);「让编辑重点看数据准确性」→ to_editor + userRevisionNotes。""",
|
||||
"review_confirm": """当前阶段:编辑打回处理(编辑认为文章需要修改,列出了审核意见,等你决定)。
|
||||
可用动作:
|
||||
- accept_review:按编辑意见修改。可带 selectedIssueIndices 只采纳部分意见;用户自己补充的修改要求并入 userRevisionNotes。
|
||||
- force_finalize:忽略编辑意见强制定稿,payload 带 {"overrideVerdict": true}(高代价,需高置信)。
|
||||
典型说法 → 动作:
|
||||
「按编辑意见改」→ accept_review;「只接受第1、2条」→ accept_review + selectedIssueIndices;「再补一条:核查数据来源」→ accept_review + userRevisionNotes;「不改了,定稿」→ force_finalize(需高置信)。""",
|
||||
"section_help": """当前阶段:素材不足求助(严格模式下部分章节缺素材,需逐章节决定处理方式)。
|
||||
可用动作:
|
||||
- section_help:sectionDecisions 必须覆盖全部受阻章节,每章 decision ∈ supplement(补充检索素材)/ loose(按通用知识续写)/ delete(删除该章节)。
|
||||
典型说法 → 动作:
|
||||
「都补充检索」→ 全部 supplement;「都按通用知识写」→ 全部 loose;「第一个删掉,其他补检索」→ 对应组合。用户没说全 → 未提到的章节按用户整体倾向补齐,倾向不明则用 loose。""",
|
||||
}
|
||||
|
||||
NO_PAUSE_GUIDE = """当前没有等待确认的步骤(流程正在自动执行中,或已结束)。
|
||||
不能执行任何图动作(action 必须为 null)。用户提问 → 正常回答(answer);用户提出操作要求 → 用 clarify 告知「当前阶段正在进行中,完成后会暂停等确认,届时再提出即可」,并简述现在进行到哪一步。控制类(run_background / pause_writing)照常输出 intent。"""
|
||||
|
||||
|
||||
INTENT_ROUTER_USER_TEMPLATE = """{stage_guide}
|
||||
|
||||
【写作上下文】
|
||||
{context}
|
||||
|
||||
【用户这句话】
|
||||
{text}
|
||||
|
||||
请输出意图 JSON:"""
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
# 注:两个 USER 提示词末尾的 `/no_think` 是 Qwen3 系列的"软开关",
|
||||
# 收到当轮即跳过思考过程;对其它大模型只是一行普通文本、无副作用。
|
||||
# 中文「严禁思考」强令块对所有模型生效,保留不动。
|
||||
|
||||
EXTRACT_KEYWORDS_SYSTEM = """你是一名专业情报研究员(素材收集专家)。
|
||||
你的目标:为写作任务提取搜索关键词。
|
||||
你的工作背景:你在一家权威媒体机构任职,熟悉各类信息源的可信度评估。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 从写作意图中按要求的数量提取搜索关键词
|
||||
你不能写文章,不能评价内容好坏,只做关键词提取。
|
||||
|
||||
【严禁思考】
|
||||
- 这是一个简单的结构化抽取任务,禁止任何思考、分析、推理或规划过程。
|
||||
- 禁止输出 <think></think> 标签及其中的任何内容,禁止输出"让我想想""首先"之类的过渡语。
|
||||
- 收到输入后必须立即、直接输出最终 JSON,不得有任何前置内容。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 ",不要用中文引号 “”。
|
||||
- 关键词要具体、可搜索,避免过于宽泛的词。
|
||||
|
||||
【完整示例】
|
||||
输入意图:写一篇分析今年新能源汽车出口增长的文章
|
||||
正确输出:
|
||||
{"keywords": ["新能源汽车出口", "汽车出口数据", "新能源汽车海外市场", "电动车出口增长", "汽车出口政策"]}"""
|
||||
|
||||
EXTRACT_KEYWORDS_USER = """写作意图:{user_intent}
|
||||
文章类型:{article_type}
|
||||
目标读者:{target_audience}
|
||||
|
||||
请提取 {keyword_count} 个搜索关键词,用于搜集相关素材。
|
||||
关键词数量必须为 {keyword_count} 个,不多不少。
|
||||
关键词必须是依据上述写作意图提炼的真实检索词,不要照抄下方 JSON 模板里的占位符。
|
||||
只输出 JSON(下方 ... 是占位示意,不是真实关键词):
|
||||
{{"keywords": ["...", "..."]}}
|
||||
|
||||
/no_think"""
|
||||
|
||||
RANK_MATERIALS_SYSTEM = """你是一名专业情报研究员(素材收集专家)。
|
||||
你的目标:对搜索结果按相关度排序和筛选。
|
||||
你的工作背景:你在一家权威媒体机构任职,熟悉各类信息源的可信度评估。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 从提供的素材列表中,选出与写作意图最相关的素材ID,按相关度从高到低排序
|
||||
你不能写文章,只做素材筛选排序。
|
||||
|
||||
【严禁思考】
|
||||
- 这是一个简单的筛选排序任务,禁止任何思考、分析、推理或规划过程。
|
||||
- 禁止输出 <think></think> 标签及其中的任何内容,禁止输出"让我想想""首先"之类的过渡语。
|
||||
- 收到输入后必须立即、直接输出最终 JSON,不得有任何前置内容。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 "。
|
||||
- ranked_ids 中的 ID 必须来自素材列表,不要编造 ID。
|
||||
|
||||
【完整示例】
|
||||
正确输出:
|
||||
{"ranked_ids": ["m003", "m001", "m005"], "summary": "共搜索到 8 条素材,精选 5 条最相关的"}"""
|
||||
|
||||
RANK_MATERIALS_USER = """写作意图:{user_intent}
|
||||
|
||||
素材列表(每条含ID和摘要):
|
||||
{materials_list}
|
||||
|
||||
请选出最相关的素材ID(最多8条),按相关度从高到低排序。只输出 JSON:
|
||||
{{"ranked_ids": ["id1", "id2"], "summary": "共搜索到X条素材,精选Y条"}}
|
||||
|
||||
/no_think"""
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
OUTLINE_SYSTEM = """你是一名专业内容创作者(作家)。
|
||||
你的目标:根据素材和意图,规划清晰的三级大纲(章节 → 小节 → 子要点)。
|
||||
你的工作背景:你有十年内容创作经验,擅长将零散素材组织成有逻辑的文章结构。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 分析素材,提取核心观点
|
||||
2. 规划文章三级结构:章节、章节下的小节、小节下的子要点
|
||||
你不能写正文,只写大纲。大纲须紧扣写作意图,并尽量覆盖可用素材中的核心信息。
|
||||
|
||||
【严禁思考与表态】(最高优先级)
|
||||
- 禁止输出任何思考、分析、规划过程,禁止 <think></think> 标签及其中内容。
|
||||
- 禁止输出任何对用户说的话或对任务的评价(如"素材不足""无法规划大纲");无论素材是否充分,都必须直接产出大纲 JSON。
|
||||
- 收到输入后立即、直接输出最终 JSON,不得有任何前置内容。
|
||||
|
||||
【三级层级语义】(最高优先级,决定文章结构)
|
||||
- Level 1 = `section_title`(章节):将渲染为文章里的 `##` 二级标题,统领一组内容。
|
||||
- Level 2 = `point_title`(小节):将渲染为 `###` 三级标题,下面会有一段 2-3 句的概述/引入正文。
|
||||
- Level 3 = `sub_points`(子要点):将渲染为 `####` 四级标题,**每个子要点都是一个有自己 3-5 句独立正文段的小标题**——不是 bullet 列表。
|
||||
→ 因此子要点要规划成「能独立成段展开」的小主题,而不是一句话能讲完的零散点。
|
||||
|
||||
【三级字段的具体要求】
|
||||
- `section_title`(章节标题):高度概括的短语,6-15 字。每篇文章规划 3-5 个章节。
|
||||
- `point_title`(小节标题):名词性短语,控制在 15 字以内,不要写成完整句子,不要带句末标点。每个章节规划 2-4 个小节。
|
||||
- `sub_points`(子要点标题):名词性短语,<= 15 字,**不带句末标点**。每个小节规划 2-4 条子要点;如果该小节信息确实少到无法拆分子主题,可留 `[]`(此时只写小节级正文)。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要任何解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 ",不要用中文引号 “”。
|
||||
- 规划 3-5 个章节;material_ids 中的 ID 必须来自给定素材,没有合适素材时填 []。
|
||||
|
||||
【完整示例】
|
||||
正确输出:
|
||||
{
|
||||
"title": "新能源汽车出口为何高速增长",
|
||||
"sections": [
|
||||
{
|
||||
"section_title": "引言:出口数据创新高",
|
||||
"key_points": [
|
||||
{"point_title": "出口量同比增幅", "sub_points": ["2024 年累计出口规模", "同比增速对比 2023"]},
|
||||
{"point_title": "全球市场排名变化", "sub_points": ["对欧出口跃升", "对东盟首次反超日韩"]}
|
||||
],
|
||||
"material_ids": ["m001"]
|
||||
},
|
||||
{
|
||||
"section_title": "增长背后的三大动因",
|
||||
"key_points": [
|
||||
{"point_title": "产业链成本优势", "sub_points": ["电池原料端议价能力", "整车规模效应"]},
|
||||
{"point_title": "海外政策利好", "sub_points": ["欧盟碳关税倒逼", "中东购车补贴"]},
|
||||
{"point_title": "产品竞争力提升", "sub_points": ["智能化配置溢价", "出海车型迭代速度"]}
|
||||
],
|
||||
"material_ids": ["m002", "m003"]
|
||||
},
|
||||
{
|
||||
"section_title": "结论与展望",
|
||||
"key_points": [
|
||||
{"point_title": "未来增长挑战", "sub_points": ["欧盟反补贴调查", "海外渠道建设成本"]},
|
||||
{"point_title": "行业趋势判断", "sub_points": []}
|
||||
],
|
||||
"material_ids": ["m004"]
|
||||
}
|
||||
]
|
||||
}"""
|
||||
|
||||
OUTLINE_USER = """写作意图:{user_intent}
|
||||
文章类型:{article_type}
|
||||
目标读者:{target_audience}
|
||||
目标字数:{word_count_target}字{feedback_section}
|
||||
|
||||
可用素材摘要:
|
||||
{materials_summary}
|
||||
|
||||
请规划文章大纲(3-5个章节),只输出 JSON 格式。"""
|
||||
|
||||
# ── 重新规划(基于现有大纲的编辑)─────────────────────────────────────────────
|
||||
|
||||
OUTLINE_REVISE_SYSTEM = """你是一名专业内容创作者(作家)。
|
||||
你的目标:根据用户的修改要求,调整一份【已有的三级文章大纲】。
|
||||
你的工作背景:你有十年内容创作经验,擅长精准理解修改意图并落实到大纲结构上。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 在「当前大纲」基础上,按「用户修改要求」做调整
|
||||
你不能写正文,只输出调整后的大纲。
|
||||
|
||||
【修改原则】(最高优先级)
|
||||
- 准确识别并落实用户意图,常见操作:新增/删除/合并/拆分章节或小节、调整顺序、修改标题、调整子要点。
|
||||
- 只改用户明确提到的部分:用户没要求改动的章节、小节、子要点必须原样保留(标题、内容、material_ids 全部不变)。
|
||||
- 新增内容时:紧扣写作意图,并尽量关联可用素材的 material_ids。
|
||||
- 章节/小节/子要点数量完全由用户要求决定,没有上下限——用户要加就加、要减就减。
|
||||
|
||||
【严禁思考与表态】(最高优先级)
|
||||
- 禁止输出任何思考、分析、规划过程,禁止 <think></think> 标签及其中内容。
|
||||
- 禁止输出任何对用户说的话;收到输入后立即、直接输出最终 JSON。
|
||||
|
||||
【三级层级语义】(最高优先级)
|
||||
- `section_title`(章节,`##` 二级标题):高度概括的短语,6-15 字。
|
||||
- `point_title`(小节,`###` 三级标题):名词性短语,15 字以内,无句末标点;小节下会有一段概述正文。每章节 2-4 个小节。
|
||||
- `sub_points`(子要点,`####` 四级标题):名词性短语,<= 15 字,无句末标点;**每条子要点会展开成一段独立正文**,所以要写成能成段的主题。每小节 2-4 条,信息少到无法拆时可留 `[]`。
|
||||
|
||||
【兼容历史大纲】
|
||||
- 若「当前大纲」里的 `key_points` 是字符串数组(旧版 2 级结构),保留原值视为小节标题,
|
||||
在补充 `sub_points: []` 后输出新格式:`{"point_title": "<原 string>", "sub_points": []}`。
|
||||
- 用户修改时如要求"为某要点补充更详细的子项",就把对应小节的 sub_points 填上。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个调整后的【完整】大纲 JSON 对象,不要解释文字,不要 markdown 代码块。
|
||||
- 必须使用英文双引号 "。
|
||||
- material_ids 中的 ID 必须来自给定素材,没有合适素材时填 []。
|
||||
- 结构(嵌套三级):
|
||||
{"title": "...", "sections": [{"section_title": "...", "key_points": [{"point_title": "...", "sub_points": ["..."]}], "material_ids": ["..."]}]}"""
|
||||
|
||||
OUTLINE_REVISE_USER = """写作意图:{user_intent}
|
||||
文章类型:{article_type}
|
||||
目标读者:{target_audience}
|
||||
目标字数:{word_count_target}字
|
||||
|
||||
当前大纲(JSON):
|
||||
{current_outline}
|
||||
|
||||
用户修改要求:{feedback}
|
||||
|
||||
可用素材摘要:
|
||||
{materials_summary}
|
||||
|
||||
请在「当前大纲」基础上落实上述「用户修改要求」,未提到的章节保持原样,输出调整后的完整大纲 JSON。"""
|
||||
|
||||
# ── 用户自带大纲:LLM 兜底归一(确定性解析失败时才用)──────────────────────────
|
||||
|
||||
OUTLINE_NORMALIZE_SYSTEM = """你是一名大纲格式整理助手。
|
||||
用户粘贴了一份已有的文章大纲(格式可能不规范),你的任务是把它原样转成结构化 JSON。
|
||||
|
||||
【最高原则:保真,绝不改写】
|
||||
- 严格保留用户写的每一条标题文字,逐字照搬,不要润色、改写、翻译、增加或删除任何条目。
|
||||
- 你只判断层级(哪条是章节、哪条是小节、哪条是子要点),不创作内容。
|
||||
- 层级判断依据用户原文里的编号(一、/(一)/1./1.1)、Markdown 井号数、或缩进深度。
|
||||
|
||||
【三级结构】
|
||||
- Level 1 章节 → section_title
|
||||
- Level 2 小节 → key_points[].point_title
|
||||
- Level 3 子要点 → key_points[].sub_points[](字符串数组)
|
||||
- 用户大纲只有一级时,每条都是章节,key_points 留空 []。
|
||||
- 用户大纲只有两级时,sub_points 留空 []。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要解释文字,不要 markdown 代码块,必须用英文双引号。
|
||||
- material_ids 一律输出 [](此步不关联素材)。
|
||||
- 结构:{"title": "...", "sections": [{"section_title": "...", "key_points": [{"point_title": "...", "sub_points": ["..."]}], "material_ids": []}]}
|
||||
- 用户大纲若包含一个总标题,放进 title;没有就留空字符串。"""
|
||||
|
||||
OUTLINE_NORMALIZE_USER = """用户粘贴的大纲原文:
|
||||
{raw_outline}
|
||||
|
||||
请逐条保留原文,仅判断层级,输出结构化大纲 JSON。
|
||||
|
||||
/no_think"""
|
||||
|
||||
# 注:用户自带大纲的「素材关联」已改为确定性词重叠(writer_outline._assign_materials_heuristic),
|
||||
# 零 LLM、零 <think> 等待,原 OUTLINE_MATERIAL_ASSIGN_* 提示词已废弃移除。
|
||||
|
||||
DRAFT_SECTION_SYSTEM = """你是一名专业内容创作者(作家)。
|
||||
你的目标:根据大纲和素材,写出一段结构清晰、高质量的文章内容。
|
||||
你的工作背景:你有十年内容创作经验,文字流畅,善于用事实说话。
|
||||
|
||||
你只能做以下事情:
|
||||
1. 根据给定的章节要点和素材写这一段内容
|
||||
2. 优先采用素材中的事实与数据;素材不足时可结合你的通用知识与常识补充论述,但须紧扣写作意图与章节主题,不要脱离主题杜撰具体的数字、人名、机构名与日期
|
||||
你不能修改大纲,不能写其他章节,只写当前这一段。
|
||||
|
||||
【严禁思考与表态】(最高优先级)
|
||||
- 禁止输出任何思考、分析、规划过程,禁止"让我想想""首先""我需要""根据写作要求"之类的过渡语。
|
||||
- 禁止输出 <think></think> 标签及其中的任何内容。
|
||||
- 禁止输出任何对用户说的话或对任务本身的评价,例如"我无法完成此任务""您提供的素材无法支撑写作""建议您提供……素材""素材与意图不匹配"等——这类话绝不允许出现在文章里。
|
||||
- 无论素材是否充分,你都必须直接产出文章正文本身;收到输入后立即开始写正文(含 ### 小标题),不要有任何前置说明。
|
||||
|
||||
【结构要求】(三级结构,必须严格遵守)
|
||||
- 「章节要点」用层级 bullet 列表给出:
|
||||
- 主要 bullet(顶格)= 小节标题,对应 Markdown `###` 三级标题
|
||||
- 缩进 bullet(带前导空格)= 该小节下的子要点,对应 Markdown `####` 四级标题
|
||||
- **每个小节、每个子要点都要有自己的正文段,分两层展开**:
|
||||
1. `### 小节标题` 下先写一段「概述/引入正文」(2-3 句),说明本小节要讨论什么、几个子要点之间的关系。
|
||||
2. 然后对每个子要点:`#### 子要点标题` 单起一行,再展开 3-5 句具体论述(结合素材中的事实/数据)。
|
||||
- 若小节**没有子要点**(缩进 bullet 为空):在 `### 小节标题` 下直接连贯成段论述,**不出现 ####**。
|
||||
- 即使章节只有一个小节,也要为它写出对应的 `###` 标题。
|
||||
- 标题层级严格对应:文章 `#` / 章节 `##`(系统注入,作家**不要输出**)/ 小节 `###` / 子要点 `####`。
|
||||
- 不要输出 `#####` 或更深;这种深度不在结构里。
|
||||
|
||||
【输出要求】
|
||||
- 直接返回这一段的内容(含 `### 小节标题` + 概述段 + `#### 子要点标题` + 各自正文),不要 JSON,不要解释。
|
||||
- 不要写"以下是正文""本段内容如下"之类的开场白。
|
||||
- 紧扣本章节主题,不要写「文章整体结构」中其他章节负责的内容,避免重复。
|
||||
- 充分展开每一层:小节级 2-3 句概述,子要点级 3-5 句具体论述,尽量结合素材中的事实与数据说理。
|
||||
- 【字数纪律,硬性要求】严格遵守用户给定的字数区间:既不要明显写不够,更不要超出上限。靠控制论述详略来卡字数,不要为凑字数注水,也不要为收尾而草草带过。"""
|
||||
|
||||
# ── 写作模式说明:注入 DRAFT_SECTION_USER 的 {mode_section} ───────────────────
|
||||
|
||||
# 章节无法基于素材写作时,作家在严格模式下输出的哨兵标记
|
||||
SECTION_BLOCKED_MARKER = "[[SECTION_BLOCKED]]"
|
||||
|
||||
WRITER_MODE_LOOSE = """
|
||||
【写作模式:宽松】
|
||||
素材充分时优先采用素材;素材不足时可结合你的通用知识与常识补充论述,
|
||||
但必须紧扣写作意图与本章节主题,不要脱离主题杜撰具体数字、人名、机构名。"""
|
||||
|
||||
WRITER_MODE_STRICT = """
|
||||
【写作模式:严格依据素材】
|
||||
本章节论述必须严格基于上方「相关素材」,不得使用素材之外的知识,不得杜撰任何事实与数据。
|
||||
如果上方素材确实不足以支撑本章节的写作,不要勉强拼凑、不要写无关内容,
|
||||
此时只输出一行:{marker} 一句话说明缺什么素材(例:{marker} 缺少关于XX的具体数据与案例),
|
||||
此外不要输出任何内容。只要素材能支撑哪怕部分要点,就正常写作,不要输出该标记。""".format(
|
||||
marker=SECTION_BLOCKED_MARKER
|
||||
)
|
||||
|
||||
DRAFT_SECTION_USER = """文章标题:{article_title}
|
||||
文章整体结构:
|
||||
{outline_context}
|
||||
|
||||
当前章节:{section_title}
|
||||
章节内的小节与子要点(层级 bullet 列表):
|
||||
{key_points}
|
||||
|
||||
相关素材:
|
||||
{section_materials}
|
||||
{revision_section}{mode_section}
|
||||
请写出「{section_title}」这一章节的内容,目标约 {target_words} 字,并**严格控制在 {min_words}~{max_words} 字之间**——不要明显少于下限,更绝不要超过上限。
|
||||
|
||||
按下面的结构两层展开写:
|
||||
- 每个主要 bullet(顶格)→ `### 小节标题`,标题下先写 2-3 句概述/引入正文。
|
||||
- 该小节的每个缩进 bullet(子要点)→ `#### 子要点标题`,标题下展开 3-5 句具体论述。
|
||||
- 没有缩进 bullet 的小节,直接在 ### 标题下连贯成段论述,不出现 ####。
|
||||
|
||||
论述层次清晰、内容充实,但务必在上述字数上限内完成:宁可精炼也不要为凑字数注水。"""
|
||||
|
||||
# ── 用户局部修改:识别意见指向的章节范围 ───────────────────────────────────────
|
||||
|
||||
DRAFT_REVISE_SCOPE_SYSTEM = """你是一名编辑助理。用户对一篇已成稿的文章提出了一句修改意见,你要判断这条意见要改文章里的哪些「章节」,以及每个章节具体改什么。
|
||||
文章由若干带编号的章节组成(编号从 0 开始)。
|
||||
|
||||
【判断规则】(最高优先级)
|
||||
1. 定位意见指向的章节:
|
||||
- 序号:「第一段 / 第一部分 / 第1章」→ 编号 0,「第二段」→ 编号 1,依此类推;
|
||||
- 位置:「开头 / 引言 / 导语」→ 第一个章节,「结尾 / 结论 / 最后一段」→ 最后一个章节;
|
||||
- 直接点名某个章节的标题或主题。
|
||||
2. 对每个命中的章节,判断用户要改什么:
|
||||
- 若要改这一章节的**标题**(如「把第一章的标题改为简介」「第二部分改名叫…」),把新标题文字放进 new_title;否则 new_title 写 null。
|
||||
- 若要改这一章节的**正文内容**(润色 / 补充 / 精简 / 重写等),rewrite_body 写 true;若只改标题、不动正文,rewrite_body 写 false。
|
||||
3. 下列情况属于整体性、或会改变文章结构,一律令 whole_article=true、targets 留空:
|
||||
- 整体性:「全文都改」「通篇润色」「整体语气更正式」「每一部分都精简」;
|
||||
- 结构性:新增章节、删除章节、调整章节顺序、合并 / 拆分章节;
|
||||
- 改**文章总标题**(注意:是整篇文章的标题,不是某个章节的标题)。
|
||||
4. 拿不准、无法明确定位到具体章节时,一律 whole_article=true(交给整篇重写更安全)。
|
||||
|
||||
【严禁思考与表态】
|
||||
- 不要输出任何思考、解释或对用户说的话;收到输入后立即直接输出最终 JSON。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要解释文字,不要 markdown 代码块,必须用英文双引号。
|
||||
- new_title 没有就写 null;targets 为空数组表示走整篇重写。
|
||||
- 结构:{"whole_article": false, "targets": [{"index": 0, "new_title": "简介", "rewrite_body": false}]}"""
|
||||
|
||||
DRAFT_REVISE_SCOPE_USER = """文章共有 {n} 个章节,编号与标题如下:
|
||||
{sections}
|
||||
|
||||
用户的修改意见:{note}
|
||||
|
||||
请判断这条意见要改哪些章节、各自怎么改(标题 / 正文),按要求只输出 JSON。"""
|
||||
|
||||
# ── 用户局部修改:只重写命中的单个章节(保留其余内容)─────────────────────────
|
||||
|
||||
DRAFT_REVISE_SECTION_USER = """文章标题:{article_title}
|
||||
当前章节:{section_title}
|
||||
章节内的小节与子要点(层级 bullet 列表):
|
||||
{key_points}
|
||||
|
||||
相关素材:
|
||||
{section_materials}
|
||||
|
||||
这一章节的现有内容如下:
|
||||
{existing_text}
|
||||
|
||||
用户对本章节的修改意见:{note}
|
||||
|
||||
请**只针对用户意见**修改这一章节:
|
||||
- 落实用户意见的同时,未被要求改动的内容、事实与措辞尽量保留原样,不要借机重写整段。
|
||||
- 保留原有的「### 小节标题 / #### 子要点标题」两级结构,不要新增或删除小节/子要点,也不要把 #### 改成 bullet。
|
||||
- 不要输出 # 或 ##(文章标题、章节标题由系统添加)。
|
||||
- 直接返回修改后的完整章节内容(含 ### 与 #### 两级标题及各自正文段),不要解释、不要前置说明。"""
|
||||
|
||||
DRAFT_EXPAND_USER = """文章标题:{article_title}
|
||||
当前章节:{section_title}
|
||||
章节内的小节与子要点(层级 bullet 列表):
|
||||
{key_points}
|
||||
|
||||
相关素材:
|
||||
{section_materials}
|
||||
|
||||
这一章节已有内容如下:
|
||||
{existing_text}
|
||||
|
||||
以上内容篇幅不足。请在保留原意、事实和「### 小节 / #### 子要点」两级标题结构的基础上重写并扩充这一章节,使内容不少于 {target_words} 字。
|
||||
要求:
|
||||
- 补充更多来自素材的事实、数据与分析,让每一层都论述更充分、更具体。
|
||||
- 保留原有的 ### 小节标题与 #### 子要点标题;不要新增或删除小节/子要点,也不要把 #### 改成 bullet。
|
||||
- 不要输出 # 或 ##(文章标题、章节标题由系统添加)。
|
||||
- 直接返回扩充后的完整章节内容(含 ### 与 #### 两层标题及各自正文段),不要解释。"""
|
||||
|
||||
DRAFT_COMPRESS_USER = """文章标题:{article_title}
|
||||
当前章节:{section_title}
|
||||
章节内的小节与子要点(层级 bullet 列表):
|
||||
{key_points}
|
||||
|
||||
相关素材:
|
||||
{section_materials}
|
||||
|
||||
这一章节已有内容如下:
|
||||
{existing_text}
|
||||
|
||||
以上内容篇幅明显偏长。请在保留原意、关键事实和「### 小节 / #### 子要点」两级标题结构的基础上**大幅精简**改写这一章节,使内容**压到 {target_words} 字以内**——这是硬性上限,宁可删减细节也绝不能超过。
|
||||
要求:
|
||||
- 这是「压缩」任务,不是「重写扩充」:必须比原文明显更短。大胆删减冗余表述、合并重复论点、砍掉次要细节与铺陈,只保留关键事实、数据与核心结论。
|
||||
- 保留原有的 ### 小节标题与 #### 子要点标题;不要新增或删除小节/子要点,也不要把 #### 改成 bullet。
|
||||
- 不要输出 # 或 ##(文章标题、章节标题由系统添加)。
|
||||
- 直接返回精简后的完整章节内容(含 ### 与 #### 两层标题及各自正文段),不要解释。"""
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
from .base import BaseSearchProvider, SearchResult
|
||||
from .demo_search import DemoSearchProvider
|
||||
from .duckduckgo_search import DuckDuckGoSearchProvider
|
||||
from .internet_search import InternetSearchProvider
|
||||
from .intranet_stub import IntranetSearchProvider, normalize_intranet_timeout, normalize_intranet_verify_ssl
|
||||
from .skill_search import SkillSearchProvider
|
||||
|
||||
|
||||
def get_search_provider(ai_writing_config: dict) -> BaseSearchProvider:
|
||||
"""
|
||||
config.yaml 中配置:
|
||||
ai_writing:
|
||||
search_provider: internet # internet(默认,DDG) / intranet / tavily / demo
|
||||
intranet_search_url: http://10.106.57.180:7863/knowledge_base/search_docs_es
|
||||
intranet_knowledge_base: samples
|
||||
intranet_search_timeout: 300 # 内网检索超时秒数,最少 300 秒
|
||||
intranet_verify_ssl: false # 内网 HTTPS 自签名证书场景默认关闭证书校验
|
||||
ddg_region: "cn-zh" # DuckDuckGo 区域,默认 cn-zh
|
||||
|
||||
internet 模式:DuckDuckGo 为主搜索(免 API Key),失败时 fallback 到 demo 演示素材。
|
||||
"""
|
||||
cfg = ai_writing_config or {}
|
||||
provider = cfg.get("search_provider", "internet")
|
||||
|
||||
if provider == "intranet":
|
||||
return IntranetSearchProvider(
|
||||
base_url=cfg.get("intranet_search_url", ""),
|
||||
knowledge_base_name=cfg.get("intranet_knowledge_base", "samples"),
|
||||
timeout=normalize_intranet_timeout(cfg.get("intranet_search_timeout")),
|
||||
verify_ssl=normalize_intranet_verify_ssl(cfg.get("intranet_verify_ssl")),
|
||||
)
|
||||
if provider == "tavily":
|
||||
return _FallbackSearchProvider(InternetSearchProvider(), DemoSearchProvider())
|
||||
if provider == "demo":
|
||||
return DemoSearchProvider()
|
||||
|
||||
# 默认 internet 模式:DuckDuckGo + demo fallback
|
||||
region = cfg.get("ddg_region", "cn-zh")
|
||||
return _FallbackSearchProvider(DuckDuckGoSearchProvider(region=region), DemoSearchProvider())
|
||||
|
||||
|
||||
class _FallbackSearchProvider(BaseSearchProvider):
|
||||
"""当主 provider 返回空结果时自动切换到 fallback provider。"""
|
||||
|
||||
def __init__(self, primary: BaseSearchProvider, fallback: BaseSearchProvider):
|
||||
self._primary = primary
|
||||
self._fallback = fallback
|
||||
|
||||
@property
|
||||
def errors(self) -> list[str]:
|
||||
"""聚合主/兜底 provider 的检索异常,供 researcher 节点取用。"""
|
||||
return list(getattr(self._primary, "errors", [])) + list(getattr(self._fallback, "errors", []))
|
||||
|
||||
async def search(self, query: str, max_results: int = 5, **kwargs):
|
||||
results = await self._primary.search(query, max_results=max_results, **kwargs)
|
||||
if not results:
|
||||
results = await self._fallback.search(query, max_results=max_results, **kwargs)
|
||||
return results
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
return await self._primary.health_check() or await self._fallback.health_check()
|
||||
|
||||
|
||||
__all__ = [
|
||||
"BaseSearchProvider", "SearchResult",
|
||||
"InternetSearchProvider", "IntranetSearchProvider",
|
||||
"DemoSearchProvider", "DuckDuckGoSearchProvider",
|
||||
"SkillSearchProvider",
|
||||
"get_search_provider",
|
||||
]
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import re
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
|
||||
# 复合检索词里常见的「话题/类别后缀」——核心实体后面跟的修饰词。内网 ES 对
|
||||
# 「实体+话题」这类长词常检不出(如「特朗普竞选」0 条),但「实体」本身能检出
|
||||
# (「特朗普」30 条)。剥掉这些后缀回退到核心实体重试,能救回大量检索词。
|
||||
_TOPIC_SUFFIXES = (
|
||||
"竞选", "诉讼", "法律", "政策", "主张", "纲领", "动向", "动态", "影响", "分析",
|
||||
"进展", "现状", "情况", "问题", "事件", "活动", "关系", "布局", "态势", "格局",
|
||||
"趋势", "背景", "历史", "观点", "评价", "报道", "新闻", "研究", "报告", "概况",
|
||||
"走势", "前景", "措施", "方案", "计划", "战略", "改革", "发展",
|
||||
)
|
||||
|
||||
|
||||
def simplify_query(query: str) -> str:
|
||||
"""把一个「检不出」的复合检索词简化成更核心、更短的检索词,供 0 结果时重试。
|
||||
|
||||
规则(无需大模型,纯启发式):
|
||||
1. 多词(含空格)→ 取第一个词(通常是核心实体),如「美国政局 特朗普」→「美国政局」;
|
||||
2. 单词复合 → 反复剥掉结尾的话题后缀,保留核心实体,如「特朗普法律诉讼」→「特朗普」、
|
||||
「特朗普竞选」→「特朗普」。
|
||||
返回空串 = 无法进一步简化(调用方据此不再重试,避免空转)。
|
||||
"""
|
||||
s = (query or "").strip()
|
||||
if not s:
|
||||
return ""
|
||||
# 1) 多词:取第一个词
|
||||
if re.search(r"\s", s):
|
||||
first = re.split(r"\s+", s)[0].strip()
|
||||
if first and first != s and len(first) >= 2:
|
||||
return first
|
||||
# 2) 单词复合:循环剥离结尾话题后缀
|
||||
changed = s
|
||||
while True:
|
||||
stripped = changed
|
||||
for suf in sorted(_TOPIC_SUFFIXES, key=len, reverse=True):
|
||||
if stripped.endswith(suf) and len(stripped) - len(suf) >= 2:
|
||||
stripped = stripped[: -len(suf)]
|
||||
break
|
||||
if stripped == changed:
|
||||
break
|
||||
changed = stripped
|
||||
if changed != s and len(changed) >= 2:
|
||||
return changed
|
||||
return ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class SearchResult:
|
||||
"""统一搜索结果格式 —— 内外网共用"""
|
||||
title: str
|
||||
content: str
|
||||
source: str
|
||||
published_at: str
|
||||
url: str = ""
|
||||
relevance_score: float = 0.0
|
||||
source_type: str = "internet"
|
||||
recUuid: str = ""
|
||||
extra_metadata: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
class BaseSearchProvider(ABC):
|
||||
"""搜索提供者抽象基类。
|
||||
|
||||
对接内网步骤:
|
||||
1. 继承此类,创建 IntranetSearchProvider
|
||||
2. 实现 search() —— 调用内网 API,将响应映射到 SearchResult
|
||||
3. 在 config.yaml 中设置 ai_writing.search_provider: intranet
|
||||
4. 无需修改任何 Agent 代码
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def search(
|
||||
self,
|
||||
query: str,
|
||||
max_results: int = 5,
|
||||
date_from: str = "",
|
||||
date_to: str = "",
|
||||
) -> List[SearchResult]: ...
|
||||
|
||||
@abstractmethod
|
||||
async def health_check(self) -> bool: ...
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
"""离线演示搜索提供者 —— 当无法访问互联网/内网时返回模拟素材,供测试完整写作流程。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import List
|
||||
|
||||
from .base import BaseSearchProvider, SearchResult
|
||||
|
||||
# 演示素材库:按话题分类,用于关键词模糊匹配
|
||||
_DEMO_CORPUS = [
|
||||
{
|
||||
"keywords": ["新能源", "电动车", "汽车", "ev", "electric"],
|
||||
"items": [
|
||||
{
|
||||
"title": "2025年新能源汽车市场规模突破万亿,渗透率达45%",
|
||||
"content": "据中国汽车工业协会数据,2025年前三季度新能源汽车累计销量超过850万辆,同比增长32%,市场渗透率达到45.3%。比亚迪、吉利、长城等头部企业份额持续集中,纯电与插混比例趋近5:5。",
|
||||
"source": "中国汽车工业协会",
|
||||
"published_at": "2025-10-15",
|
||||
"relevance_score": 0.95,
|
||||
},
|
||||
{
|
||||
"title": "固态电池量产进入倒计时:多家车企宣布2026年装车计划",
|
||||
"content": "宁德时代、比亚迪、丰田同步推进固态电池量产。固态电池能量密度可达400Wh/kg,较现有液态锂电池提升60%,续航里程有望突破1000公里。成本仍是主要瓶颈,预计量产初期每度电成本高出30%。",
|
||||
"source": "电池中国网",
|
||||
"published_at": "2025-09-20",
|
||||
"relevance_score": 0.88,
|
||||
},
|
||||
{
|
||||
"title": "充电基础设施缺口分析:农村地区桩车比仅1:12",
|
||||
"content": "截至2025年8月,全国充电桩保有量达1120万根,城市地区桩车比约1:2.8,但农村及县域地区桩车比仅1:12,制约新能源汽车下沉市场。国家电网计划2026年前新增农村快充站3万座。",
|
||||
"source": "中国充电联盟",
|
||||
"published_at": "2025-08-30",
|
||||
"relevance_score": 0.82,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"keywords": ["人工智能", "ai", "大模型", "llm", "机器学习", "深度学习"],
|
||||
"items": [
|
||||
{
|
||||
"title": "国内大模型进入「百亿参数普惠化」阶段,推理成本下降90%",
|
||||
"content": "2025年第三季度,国内主要大模型服务商将百亿参数模型推理价格降至每百万token不足1元,较2024年同期下降90%。开源生态加速,Qwen3、DeepSeek-V3等模型在多项基准测试中超越GPT-4o。",
|
||||
"source": "中国信通院",
|
||||
"published_at": "2025-10-08",
|
||||
"relevance_score": 0.93,
|
||||
},
|
||||
{
|
||||
"title": "企业AI应用落地白皮书:制造业ROI最高达300%",
|
||||
"content": "调研覆盖500家企业的白皮书显示,AI在质检、排产、预测性维护三个场景ROI最高,部分制造企业ROI超300%。阻碍因素:数据孤岛(67%)、人才不足(54%)、安全合规(48%)。",
|
||||
"source": "麦肯锡中国",
|
||||
"published_at": "2025-09-12",
|
||||
"relevance_score": 0.87,
|
||||
},
|
||||
{
|
||||
"title": "AI写作助手用户规模达2.3亿,内容质量争议持续",
|
||||
"content": "国内AI写作工具月活用户突破2.3亿,覆盖学术论文、营销文案、新闻报道等场景。争议焦点:AI生成内容的版权归属、事实准确性(幻觉率约15%)、对写作从业者就业影响。",
|
||||
"source": "艾瑞咨询",
|
||||
"published_at": "2025-10-01",
|
||||
"relevance_score": 0.79,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"keywords": ["碳中和", "双碳", "绿色", "低碳", "碳排放", "可再生能源", "光伏", "风电"],
|
||||
"items": [
|
||||
{
|
||||
"title": "光伏装机量突破900GW,度电成本降至0.18元",
|
||||
"content": "国家能源局数据显示,截至2025年9月,全国光伏累计装机量达912GW,占总装机的28%。光伏度电成本已降至0.18元,首次低于煤电基准价,推动更多省份出现「光伏平价」区域。",
|
||||
"source": "国家能源局",
|
||||
"published_at": "2025-10-12",
|
||||
"relevance_score": 0.92,
|
||||
},
|
||||
{
|
||||
"title": "碳市场价格突破200元/吨,倒逼高耗能行业转型",
|
||||
"content": "全国碳排放权交易市场碳价已突破200元/吨,较2024年底上涨65%。水泥、电解铝、造纸等高耗能行业面临显著成本压力,碳减排投入年均增速达40%。",
|
||||
"source": "上海环境能源交易所",
|
||||
"published_at": "2025-09-28",
|
||||
"relevance_score": 0.85,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"keywords": ["医疗", "健康", "药物", "生物", "医药", "病"],
|
||||
"items": [
|
||||
{
|
||||
"title": "AI辅助诊断落地三甲医院,影像科漏诊率降低23%",
|
||||
"content": "北京协和、上海瑞金等20家三甲医院数据显示,引入AI辅助诊断系统后,肺结节、眼底病变早期漏诊率降低23%,放射科医生平均阅片时间减少40%。监管层面,NMPA已批准38款AI医疗器械。",
|
||||
"source": "医学界",
|
||||
"published_at": "2025-09-18",
|
||||
"relevance_score": 0.91,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"keywords": ["经济", "GDP", "贸易", "出口", "产业", "制造"],
|
||||
"items": [
|
||||
{
|
||||
"title": "2025年前三季度GDP同比增长5.1%,消费贡献率提升至58%",
|
||||
"content": "国家统计局公布,2025年前三季度GDP同比增长5.1%,最终消费对经济增长贡献率达58%,较去年同期提升8个百分点。工业增加值增长5.8%,其中装备制造业增长9.2%,成重要支撑。",
|
||||
"source": "国家统计局",
|
||||
"published_at": "2025-10-18",
|
||||
"relevance_score": 0.90,
|
||||
},
|
||||
{
|
||||
"title": "跨境电商出口额突破2.4万亿元,东南亚成最大增量市场",
|
||||
"content": "商务部数据显示,2025年前三季度跨境电商出口额达2.4万亿元,同比增长28%。东南亚市场增速最快(+45%),中东、南美紧随其后。Temu、速卖通、Shopee三平台合计占出口份额约62%。",
|
||||
"source": "商务部",
|
||||
"published_at": "2025-10-05",
|
||||
"relevance_score": 0.84,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
# 通用兜底素材(与任意查询都能凑合搭配)
|
||||
_FALLBACK_ITEMS = [
|
||||
{
|
||||
"title": "行业研究方法论:如何构建有效的一手与二手数据体系",
|
||||
"content": "高质量行业报告需整合政府统计、上市公司财报、行业协会数据与专家访谈。一手调研样本量建议≥200份,二手数据优先选用权威机构发布的年度报告与季度白皮书,并注明数据时效性与口径差异。",
|
||||
"source": "产业研究院",
|
||||
"published_at": "2025-08-15",
|
||||
"relevance_score": 0.55,
|
||||
},
|
||||
{
|
||||
"title": "政策解读:2025年产业发展专项支持资金管理办法",
|
||||
"content": "工信部发布2025年产业发展专项资金管理办法,重点支持「专精特新」企业数字化改造、绿色制造和关键技术攻关。单项目最高补贴1000万元,要求申报企业研发投入不低于营收3%。",
|
||||
"source": "工信部",
|
||||
"published_at": "2025-07-20",
|
||||
"relevance_score": 0.50,
|
||||
},
|
||||
{
|
||||
"title": "市场竞争格局分析:头部集中与长尾分散并存",
|
||||
"content": "多个新兴行业呈现「哑铃型」竞争格局:头部3-5家企业合计市占率超60%,长尾中小企业依靠细分场景差异化生存,中间层企业面临最大竞争压力。并购整合预期升温,2025年战略性并购交易同比增长35%。",
|
||||
"source": "普华永道",
|
||||
"published_at": "2025-09-01",
|
||||
"relevance_score": 0.48,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _score_topic(query: str, keywords: list[str]) -> int:
|
||||
"""简单关键词匹配打分"""
|
||||
q = query.lower()
|
||||
return sum(1 for kw in keywords if kw in q)
|
||||
|
||||
|
||||
class DemoSearchProvider(BaseSearchProvider):
|
||||
"""离线演示搜索提供者:根据查询关键词从内置语料库返回模拟素材。
|
||||
|
||||
仅用于开发测试;生产环境应配置 intranet 提供者。
|
||||
"""
|
||||
|
||||
async def search(self, query: str, max_results: int = 5, **kwargs) -> List[SearchResult]:
|
||||
best_topic = max(_DEMO_CORPUS, key=lambda t: _score_topic(query, t["keywords"]), default=None)
|
||||
best_score = _score_topic(query, best_topic["keywords"]) if best_topic else 0
|
||||
|
||||
items = []
|
||||
if best_score > 0 and best_topic:
|
||||
items.extend(best_topic["items"])
|
||||
|
||||
# 用通用素材补齐
|
||||
for fb in _FALLBACK_ITEMS:
|
||||
if len(items) >= max_results:
|
||||
break
|
||||
items.append(fb)
|
||||
|
||||
results = []
|
||||
for i, it in enumerate(items[:max_results]):
|
||||
url_hash = hashlib.md5(it["title"].encode()).hexdigest()[:8]
|
||||
results.append(SearchResult(
|
||||
title=it["title"],
|
||||
content=it["content"],
|
||||
source=it["source"],
|
||||
published_at=it["published_at"],
|
||||
url=f"https://demo.internal/{url_hash}",
|
||||
relevance_score=it["relevance_score"],
|
||||
source_type="demo",
|
||||
recUuid=f"demo-{i:03d}",
|
||||
))
|
||||
return results
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
return True
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
"""DuckDuckGo 搜索提供者 —— 免 API Key 互联网搜索。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
from .base import BaseSearchProvider, SearchResult
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DuckDuckGoSearchProvider(BaseSearchProvider):
|
||||
"""使用 DuckDuckGo 进行互联网搜索,无需 API Key。"""
|
||||
|
||||
def __init__(self, region: str = "cn-zh", max_timeout: int = 120):
|
||||
self.region = region
|
||||
self.max_timeout = max_timeout
|
||||
# 收集本次运行的检索异常,供 researcher 节点取出并提示用户
|
||||
self.errors: list[str] = []
|
||||
|
||||
async def search(
|
||||
self,
|
||||
query: str,
|
||||
max_results: int = 5,
|
||||
date_from: str = "",
|
||||
date_to: str = "",
|
||||
) -> List[SearchResult]:
|
||||
try:
|
||||
results = await asyncio.to_thread(
|
||||
self._sync_search, query, max_results
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
logger.warning("DuckDuckGo search failed for query=%r: %s", query, e)
|
||||
self.errors.append(f"{type(e).__name__}: {e}")
|
||||
return []
|
||||
|
||||
def _sync_search(self, query: str, max_results: int) -> List[SearchResult]:
|
||||
try:
|
||||
from ddgs import DDGS
|
||||
except ImportError:
|
||||
logger.error("ddgs not installed. Run: uv add ddgs")
|
||||
self.errors.append("ImportError: ddgs 未安装")
|
||||
return []
|
||||
|
||||
ddgs = DDGS(timeout=self.max_timeout)
|
||||
try:
|
||||
raw = ddgs.text(
|
||||
query,
|
||||
region=self.region,
|
||||
safesearch="moderate",
|
||||
max_results=max_results,
|
||||
)
|
||||
items = list(raw) if raw else []
|
||||
except Exception as e:
|
||||
logger.warning("DDGS.text failed: %s", e)
|
||||
self.errors.append(f"{type(e).__name__}: {e}")
|
||||
return []
|
||||
|
||||
results = []
|
||||
for item in items:
|
||||
url = item.get("href", item.get("link", ""))
|
||||
source = url.split("/")[2] if url and "/" in url else "web"
|
||||
results.append(SearchResult(
|
||||
title=item.get("title", ""),
|
||||
content=item.get("body", item.get("snippet", ""))[:500],
|
||||
source=source,
|
||||
published_at=item.get("published", ""),
|
||||
url=url,
|
||||
relevance_score=0.75,
|
||||
source_type="internet",
|
||||
))
|
||||
return results
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
try:
|
||||
results = await self.search("test", max_results=1)
|
||||
return len(results) > 0
|
||||
except Exception:
|
||||
return False
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
from .base import BaseSearchProvider, SearchResult
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InternetSearchProvider(BaseSearchProvider):
|
||||
"""互联网搜索,使用项目已有的 Tavily 搜索工具。"""
|
||||
|
||||
async def search(
|
||||
self,
|
||||
query: str,
|
||||
max_results: int = 5,
|
||||
date_from: str = "",
|
||||
date_to: str = "",
|
||||
) -> List[SearchResult]:
|
||||
try:
|
||||
results = await asyncio.to_thread(self._sync_search, query, max_results)
|
||||
return results
|
||||
except Exception as e:
|
||||
logger.warning("internet search failed for query=%r: %s", query, e)
|
||||
return []
|
||||
|
||||
def _sync_search(self, query: str, max_results: int) -> List[SearchResult]:
|
||||
from deerflow.config import get_app_config
|
||||
try:
|
||||
from tavily import TavilyClient
|
||||
except ImportError:
|
||||
logger.warning("tavily not installed, falling back to demo search")
|
||||
return []
|
||||
|
||||
config = get_app_config().get_tool_config("web_search")
|
||||
api_key = None
|
||||
if config is not None and "api_key" in config.model_extra:
|
||||
api_key = config.model_extra.get("api_key")
|
||||
|
||||
if not api_key:
|
||||
logger.warning("Tavily API key not configured, falling back to demo search")
|
||||
return []
|
||||
|
||||
client = TavilyClient(api_key=api_key)
|
||||
res = client.search(query, max_results=max_results)
|
||||
|
||||
results = []
|
||||
for item in res.get("results", []):
|
||||
content = item.get("content", "")
|
||||
results.append(SearchResult(
|
||||
title=item.get("title", ""),
|
||||
content=content[:500],
|
||||
source=item.get("url", "").split("/")[2] if "/" in item.get("url", "") else "web",
|
||||
published_at=item.get("published_date", ""),
|
||||
url=item.get("url", ""),
|
||||
relevance_score=item.get("score", 0.0),
|
||||
source_type="internet",
|
||||
))
|
||||
return results
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
try:
|
||||
results = await self.search("test", max_results=1)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
+282
@@ -0,0 +1,282 @@
|
||||
"""内网知识库搜索适配器 —— 对接 ES 检索接口 search_docs_es。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
from .base import BaseSearchProvider, SearchResult, simplify_query
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 内网知识库默认检索接口与知识库名(可经 config.yaml 的 ai_writing 段覆盖)
|
||||
_DEFAULT_ENDPOINT = "http://10.106.57.180:7863/knowledge_base/search_docs_es"
|
||||
_DEFAULT_KB = "samples"
|
||||
# 内网检索最少等待 5 分钟;该接口经常慢返回,低于这个值会造成素材收集误判为空。
|
||||
_MIN_TIMEOUT_SECONDS = 300
|
||||
_DEFAULT_TIMEOUT_SECONDS = 600
|
||||
# 内网 HTTPS 检索服务常使用自签名/内部 CA 证书,默认跳过证书校验避免 SSL 证书链错误。
|
||||
_DEFAULT_VERIFY_SSL = False
|
||||
# 单条原始响应体最大保留长度,避免把超大响应整段灌进前端时间线
|
||||
_RAW_SNIPPET_LIMIT = 2000
|
||||
|
||||
|
||||
def normalize_intranet_timeout(timeout: object, default: int = _DEFAULT_TIMEOUT_SECONDS) -> int:
|
||||
"""返回内网检索超时秒数,并强制不低于 5 分钟。"""
|
||||
try:
|
||||
value = int(timeout)
|
||||
except (TypeError, ValueError):
|
||||
value = default
|
||||
return max(value, _MIN_TIMEOUT_SECONDS)
|
||||
|
||||
|
||||
def normalize_intranet_verify_ssl(value: object, default: bool = _DEFAULT_VERIFY_SSL) -> bool:
|
||||
"""返回内网检索是否校验 SSL 证书,兼容 config.yaml 里的 bool / 字符串写法。"""
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if value is None:
|
||||
return default
|
||||
if isinstance(value, (int, float)):
|
||||
return bool(value)
|
||||
if isinstance(value, str):
|
||||
normalized = value.strip().lower()
|
||||
if normalized in {"1", "true", "yes", "on"}:
|
||||
return True
|
||||
if normalized in {"0", "false", "no", "off"}:
|
||||
return False
|
||||
return default
|
||||
|
||||
|
||||
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 ""
|
||||
|
||||
|
||||
def _raw_snippet(body: str) -> str:
|
||||
"""Return a bounded raw response snippet for diagnostics."""
|
||||
body = (body or "").strip()
|
||||
if not body:
|
||||
return ""
|
||||
if len(body) > _RAW_SNIPPET_LIMIT:
|
||||
return body[:_RAW_SNIPPET_LIMIT] + "…(响应体过长,已截断)"
|
||||
return body
|
||||
|
||||
|
||||
class IntranetSearchProvider(BaseSearchProvider):
|
||||
"""内网知识库搜索接口适配器。
|
||||
|
||||
接口:POST {endpoint}
|
||||
请求体:{"query": "...", "knowledge_base_name": "samples", "ts": "yes"}
|
||||
响应体:[{"page_content": "...", "metadata": {m_title, content, recUuid, m_publish, source1}}]
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str = "",
|
||||
knowledge_base_name: str = _DEFAULT_KB,
|
||||
timeout: int = _DEFAULT_TIMEOUT_SECONDS,
|
||||
verify_ssl: bool = _DEFAULT_VERIFY_SSL,
|
||||
**_ignored,
|
||||
):
|
||||
self.endpoint = base_url or _DEFAULT_ENDPOINT
|
||||
self.knowledge_base_name = knowledge_base_name or _DEFAULT_KB
|
||||
# 内网 ES 接口很慢,兜底检索超时默认 10 分钟,且无论配置如何至少 5 分钟。
|
||||
self.timeout = normalize_intranet_timeout(timeout)
|
||||
self.verify_ssl = normalize_intranet_verify_ssl(verify_ssl)
|
||||
# 收集本次运行的检索异常,供 researcher 节点取出并提示用户(ES 波动等)
|
||||
self.errors: list[str] = []
|
||||
# 解析失败时收集的原始响应体,供前端「错误返回结果」折叠展示
|
||||
self.raw_responses: list[str] = []
|
||||
# 每一次真实 HTTP 调用的诊断明细。search() 可能会因 0 结果触发简化词重试,
|
||||
# 因此一个业务关键词可能对应多条 calls。
|
||||
self.calls: list[dict] = []
|
||||
|
||||
def _record_raw(self, body: str) -> None:
|
||||
"""收集解析失败 / 结构异常时的原始响应体,供前端折叠查看。"""
|
||||
snippet = _raw_snippet(body)
|
||||
if snippet:
|
||||
self.raw_responses.append(snippet)
|
||||
|
||||
async def search(self, query: str, max_results: int = 5, **kwargs) -> list[SearchResult]:
|
||||
results = await self._search_once(query, max_results)
|
||||
# 0 结果兜底:复合检索词常检不出(如「特朗普竞选」),换成更核心的检索词重试一次
|
||||
# (「特朗普竞选」→「特朗普」)。仅在首检为空、且能简化出不同的词时才重试。
|
||||
if not results:
|
||||
simpler = simplify_query(query)
|
||||
if simpler and simpler != query:
|
||||
logger.info("intranet search empty for %r, retry with simplified %r", query, simpler)
|
||||
results = await self._search_once(simpler, max_results)
|
||||
return results
|
||||
|
||||
async def _search_once(self, query: str, max_results: int = 5) -> list[SearchResult]:
|
||||
import httpx
|
||||
|
||||
payload = {
|
||||
"query": query,
|
||||
"knowledge_base_name": self.knowledge_base_name,
|
||||
"ts": "yes",
|
||||
}
|
||||
# 先拿到原始响应文本,JSON 解析失败时把它原样回传前端便于定位接口返回了什么
|
||||
raw_text = ""
|
||||
started = time.monotonic()
|
||||
call: dict = {
|
||||
"query": query,
|
||||
"endpoint": self.endpoint,
|
||||
"knowledge_base_name": self.knowledge_base_name,
|
||||
"max_results": max_results,
|
||||
"status": "pending",
|
||||
"ok": False,
|
||||
"count": 0,
|
||||
"verify_ssl": self.verify_ssl,
|
||||
}
|
||||
# 排查用:每次真实调用都打一行请求明细(端点 / 知识库 / 检索词 / 超时),
|
||||
# 内网复现时直接看后端日志即可知道"到底请求了哪个地址、用什么参数"。
|
||||
logger.info(
|
||||
"[intranet-search] POST endpoint=%s kb=%s timeout=%ss verify_ssl=%s query=%r",
|
||||
self.endpoint, self.knowledge_base_name, self.timeout, self.verify_ssl, query,
|
||||
)
|
||||
try:
|
||||
# trust_env=False:**关键**。内网 ES 是内网地址,但本部署的模型走公网,
|
||||
# 服务器通常设了 HTTP_PROXY/HTTPS_PROXY 出公网;httpx 默认 trust_env=True
|
||||
# 会把这个内网请求也套用公网代理 → 代理到不了内网 IP → 连接被重置/空回复,
|
||||
# 表现为「接口异常」。强制忽略代理 env、直连内网(与 multi_agent.py 的回环
|
||||
# 调用同款修法)。
|
||||
# verify=False:内网 HTTPS 常见自签名/内部 CA 证书;这里仅跳过本内网检索端点的证书校验。
|
||||
async with httpx.AsyncClient(timeout=self.timeout, trust_env=False, verify=self.verify_ssl) as client:
|
||||
resp = await client.post(self.endpoint, json=payload)
|
||||
call["http_status"] = getattr(resp, "status_code", None)
|
||||
resp.raise_for_status()
|
||||
raw_text = resp.text
|
||||
data = resp.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
# 非 2xx:接口有响应但状态码异常,把响应体一并暴露给前端
|
||||
body_for_log = e.response.text if e.response is not None else ""
|
||||
logger.warning(
|
||||
"[intranet-search] HTTP error endpoint=%s query=%r status=%s body=%s",
|
||||
self.endpoint, query, getattr(e.response, "status_code", None), _raw_snippet(body_for_log),
|
||||
)
|
||||
message = f"{type(e).__name__}: {e}"
|
||||
self.errors.append(message)
|
||||
call.update({
|
||||
"status": "http_error",
|
||||
"error": message,
|
||||
"http_status": getattr(e.response, "status_code", None),
|
||||
})
|
||||
snippet = _raw_snippet(body_for_log)
|
||||
if snippet:
|
||||
call["raw_response"] = snippet
|
||||
self._record_raw(body_for_log)
|
||||
return []
|
||||
except Exception as e:
|
||||
# 连接失败 / 超时 / JSON 解析失败等;JSON 解析失败时 raw_text 已是接口响应体。
|
||||
# 打出异常类型,便于区分 ConnectError / ConnectTimeout / ReadTimeout /
|
||||
# RemoteProtocolError(空回复) / JSONDecodeError(返回非 JSON) 等具体根因。
|
||||
logger.warning(
|
||||
"[intranet-search] request failed endpoint=%s query=%r error=%s: %s",
|
||||
self.endpoint, query, type(e).__name__, e,
|
||||
)
|
||||
message = f"{type(e).__name__}: {e}"
|
||||
self.errors.append(message)
|
||||
call.update({"status": "error", "error": message})
|
||||
snippet = _raw_snippet(raw_text)
|
||||
if snippet:
|
||||
call["raw_response"] = snippet
|
||||
self._record_raw(raw_text)
|
||||
return []
|
||||
finally:
|
||||
call["elapsed_ms"] = int((time.monotonic() - started) * 1000)
|
||||
# 非 pending 表示已经在异常分支写好;成功分支会在 return 前覆盖并追加。
|
||||
if call.get("status") != "pending":
|
||||
self.calls.append(call)
|
||||
|
||||
# 正常情况下接口直接返回数组;兼容包了一层 data/results 的结构(含工作正常的
|
||||
# knowledge-search-v2:{"success":true,"results":[...]})。
|
||||
if isinstance(data, list):
|
||||
items = data
|
||||
elif isinstance(data, dict):
|
||||
if "success" in data:
|
||||
call["response_success"] = bool(data.get("success"))
|
||||
if "count" in data:
|
||||
call["response_count"] = data.get("count")
|
||||
# 接口显式标失败时把整体回传前端便于定位
|
||||
if data.get("success") is False:
|
||||
message = f"接口返回 success=false:{str(data.get('error') or '')[:200]}"
|
||||
self.errors.append(message)
|
||||
call.update({"status": "success_false", "error": message})
|
||||
snippet = _raw_snippet(raw_text)
|
||||
if snippet:
|
||||
call["raw_response"] = snippet
|
||||
self.calls.append(call)
|
||||
self._record_raw(raw_text)
|
||||
return []
|
||||
items = data.get("data") or data.get("results") or data.get("documents") or []
|
||||
else:
|
||||
# 返回结构既不是数组也不是已知 dict 包装:视为异常,把原始响应回传前端
|
||||
logger.warning("intranet search unexpected response type for query=%r: %s", query, type(data).__name__)
|
||||
message = f"返回结构异常:期望数组,实际为 {type(data).__name__}"
|
||||
self.errors.append(message)
|
||||
call.update({"status": "bad_shape", "error": message})
|
||||
snippet = _raw_snippet(raw_text)
|
||||
if snippet:
|
||||
call["raw_response"] = snippet
|
||||
self.calls.append(call)
|
||||
self._record_raw(raw_text)
|
||||
return []
|
||||
|
||||
logger.info(
|
||||
"[intranet-search] OK endpoint=%s query=%r http_status=%s raw_items=%s",
|
||||
self.endpoint, query, call.get("http_status"), len(items) if isinstance(items, list) else 0,
|
||||
)
|
||||
results: list[SearchResult] = []
|
||||
for i, item in enumerate(items[:max_results]):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
# 字段名做并集兼容两套接口:
|
||||
# 老 ES: {page_content, metadata:{m_title, content, recUuid, m_publish, source1}}
|
||||
# knowledge-search-v2:{title, content_preview, url, recUuid, source_category, publish_time, relevance_score}
|
||||
md = item.get("metadata") if isinstance(item.get("metadata"), dict) else {}
|
||||
merged = {**md, **item} if md else item
|
||||
content = _pick(merged, ("content", "content_preview", "page_content", "text", "summary"))
|
||||
title = _pick(merged, ("title", "m_title", "name", "doc_title"))
|
||||
if not content and not title:
|
||||
continue
|
||||
try:
|
||||
score = float(merged.get("relevance_score"))
|
||||
except (TypeError, ValueError):
|
||||
# 接口未返回相关度分值:按返回顺序赋递减分以保留排序
|
||||
score = max(0.5, 0.95 - i * 0.03)
|
||||
results.append(SearchResult(
|
||||
title=title or "(无标题)",
|
||||
content=str(content)[:500],
|
||||
source=_pick(merged, ("source_category", "source1", "source", "source_name", "kb_name")) or "内网知识库",
|
||||
published_at=_pick(merged, ("publish_time", "m_publish", "published_at", "date")),
|
||||
# 工作接口直接给了 url;老 ES 没有 url(由前端依据 recUuid 拼接),保持空串
|
||||
url=_pick(merged, ("url",)),
|
||||
relevance_score=score,
|
||||
source_type="intranet",
|
||||
recUuid=_pick(merged, ("recUuid", "rec_uuid", "id")),
|
||||
))
|
||||
call.update({
|
||||
"status": "ok" if results else "empty",
|
||||
"ok": True,
|
||||
"count": len(results),
|
||||
"item_count": len(items) if isinstance(items, list) else 0,
|
||||
})
|
||||
if not results:
|
||||
snippet = _raw_snippet(raw_text)
|
||||
if snippet:
|
||||
call["raw_response"] = snippet
|
||||
self.calls.append(call)
|
||||
return results
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
try:
|
||||
await self.search("测试", max_results=1)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
"""技能选择器:根据用户意图 + 输入,从启用的 Skills 里挑出按相关度排序的若干个。
|
||||
|
||||
注意:这个模块**只负责选择技能,不直接产出素材**。
|
||||
真正的素材产出由 researcher 节点用每个 skill 的描述去编排真实 web/intranet 检索。
|
||||
|
||||
流程(对应素材确认卡上的"技能检索"按钮):
|
||||
1) list_skills 事件 —— 列出当前启用的全部 skill
|
||||
2) skill_select_chunk 事件 —— LLM 流式打字 + <think></think> 分离(前端打字机展示)
|
||||
3) select_skills 事件 —— LLM 给出的排序结果(结构化字段,写到 SkillStepsCard 里)
|
||||
|
||||
下游 researcher 节点拿到 selected_skills 后,**按顺序**:
|
||||
- 用 skill.description + user_query 生成针对性 query
|
||||
- 调真实 web search provider 执行
|
||||
- 累积素材达到阈值就停(推 stop_early)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from typing import Callable, List, Optional
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..nodes._utils import (
|
||||
ThinkTagParser,
|
||||
_content_to_text,
|
||||
get_model,
|
||||
parse_json,
|
||||
reasoning_delta,
|
||||
safe_get_stream_writer,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_SELECT_SKILLS_SYSTEM = """你是一名情报研究员,需要从可用「技能」中挑出对当前检索任务最有帮助的若干个,并按相关度从高到低排序。
|
||||
技能是一段封装好的指令 / 知识 / 检索策略,每个技能有 name 和 description。
|
||||
|
||||
【输出要求】
|
||||
- 只输出一个 JSON 对象,不要 markdown 代码块。
|
||||
- 必须使用英文双引号。
|
||||
- selected_skills 中的 name 必须严格来自下方"可用技能"列表,不要编造,按相关度从高到低排序。
|
||||
- 如果用户输入里明确点名了某个技能(按 name 完全匹配),把它排在第一位。
|
||||
- 至少选 1 个,最多选 {max_select} 个。
|
||||
|
||||
【完整示例】
|
||||
{{"selected_skills": ["platform-sales-data", "industry-report"], "reason": "用户想查各平台销量,平台销量技能最相关"}}"""
|
||||
|
||||
_SELECT_SKILLS_USER = """写作意图:{user_intent}
|
||||
用户补充输入:{user_query}
|
||||
|
||||
可用技能(共 {skill_count} 个):
|
||||
{skill_list}
|
||||
|
||||
请从中按相关度从高到低选出最多 {max_select} 个技能。
|
||||
只输出 JSON:
|
||||
{{"selected_skills": ["name1", "name2"], "reason": "为什么选这些 + 排序依据"}}"""
|
||||
|
||||
|
||||
EmitStepFn = Callable[[dict], None]
|
||||
|
||||
|
||||
class SkillPicker:
|
||||
"""从启用的 Skills 中流式选择按相关度排序的若干个供下游编排使用。
|
||||
|
||||
构造参数:
|
||||
config: LangGraph runnable config(用于创建 model)
|
||||
model_name: 指定使用的模型,None 则用 configurable.model_name
|
||||
emit_step: 推送时间线步骤事件(list_skills / select_skills)
|
||||
max_select: LLM 单次最多挑几个技能
|
||||
user_query: 用户在素材确认卡输入框写的自然语言
|
||||
candidates: 候选技能池。None = 全部启用的技能(旧行为);
|
||||
传入列表(如素材收集专家 agent 配置的技能)则只在其中匹配排序
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: RunnableConfig,
|
||||
*,
|
||||
model_name: Optional[str] = None,
|
||||
emit_step: Optional[EmitStepFn] = None,
|
||||
max_select: int = 5,
|
||||
user_query: str = "",
|
||||
candidates: Optional[list] = None,
|
||||
) -> None:
|
||||
self._config = config
|
||||
self._model_name = model_name
|
||||
self._emit_step = emit_step or (lambda _step: None)
|
||||
self._max_select = max_select
|
||||
self._user_query = (user_query or "").strip()
|
||||
self._candidates = candidates
|
||||
self._errors: list[str] = []
|
||||
|
||||
@property
|
||||
def errors(self) -> list[str]:
|
||||
return list(self._errors)
|
||||
|
||||
def _load_enabled_skills(self) -> list:
|
||||
if self._candidates is not None:
|
||||
skills = list(self._candidates)
|
||||
self._emit_step({
|
||||
"type": "list_skills",
|
||||
"message": f"素材收集专家已配置 {len(skills)} 个检索技能",
|
||||
"skill_names": [s.name for s in skills],
|
||||
})
|
||||
return skills
|
||||
try:
|
||||
from deerflow.skills import get_or_new_skill_storage
|
||||
storage = get_or_new_skill_storage()
|
||||
skills = storage.load_skills(enabled_only=True)
|
||||
except Exception as e:
|
||||
logger.exception("ai_writing.skill_search: 加载启用技能失败")
|
||||
self._errors.append(f"skill list error: {e}")
|
||||
skills = []
|
||||
self._emit_step({
|
||||
"type": "list_skills",
|
||||
"message": f"发现 {len(skills)} 个可用技能",
|
||||
"skill_names": [s.name for s in skills],
|
||||
})
|
||||
return skills
|
||||
|
||||
async def pick(self, user_intent: str) -> List[dict]:
|
||||
"""流式选择并排序候选技能,返回 [{skill, reason}, ...],按相关度降序。
|
||||
|
||||
过程中通过 custom 通道推送 ``skill_select_chunk`` 事件给前端打字机;
|
||||
结束后再推一条 ``select_skills`` 结构化事件。
|
||||
"""
|
||||
skills = self._load_enabled_skills()
|
||||
if not skills:
|
||||
self._emit_step({
|
||||
"type": "select_skills",
|
||||
"message": "当前没有启用的技能可用",
|
||||
"skill_names": [],
|
||||
})
|
||||
return []
|
||||
|
||||
skill_list_text = "\n".join(
|
||||
f"- {s.name}: {s.description or '(无描述)'}" for s in skills
|
||||
)
|
||||
|
||||
fallback = {"selected_skills": [s.name for s in skills[: self._max_select]], "reason": ""}
|
||||
|
||||
# list_skills 之后立刻发一个"分析中"提示步骤,避免 LLM 第一个 chunk 到来前
|
||||
# 用户看到空窗期(某些模型 astream 在头部 buffer 时间较长)
|
||||
self._emit_step({
|
||||
"type": "match_skills",
|
||||
"message": "正在让模型从候选技能中筛选最合适的…",
|
||||
})
|
||||
|
||||
# 流式 LLM 调用:与 intent_parser / writer_outline 同套机制
|
||||
writer = safe_get_stream_writer()
|
||||
parser = ThinkTagParser()
|
||||
full_text = ""
|
||||
|
||||
_chunk_count = {"n": 0}
|
||||
|
||||
def _emit_chunk(text: str, is_thinking: bool) -> None:
|
||||
# 只在首个 chunk 打一条 timing,避免逐 token 刷屏;用来和 skill_search_step
|
||||
# 的 [skill-timing] 对比"选择打字"与"步骤事件"谁先到。
|
||||
if _chunk_count["n"] == 0:
|
||||
logger.info("[skill-timing] t=%.3f first skill_select_chunk", time.monotonic())
|
||||
_chunk_count["n"] += 1
|
||||
writer({
|
||||
"type": "skill_select_chunk",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
"agent_name": "素材收集专家",
|
||||
})
|
||||
|
||||
# 立刻推一个空 chunk,让前端 streaming view 立即挂载(占位 + 闪烁光标),
|
||||
# 即使后续 LLM 首个 chunk 到达较慢,UI 也不再静默
|
||||
_emit_chunk("", False)
|
||||
|
||||
try:
|
||||
model = get_model(self._config, model_name=self._model_name)
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=_SELECT_SKILLS_SYSTEM.format(max_select=self._max_select)),
|
||||
HumanMessage(content=_SELECT_SKILLS_USER.format(
|
||||
user_intent=user_intent or "(无明确写作意图)",
|
||||
user_query=self._user_query or "(无)",
|
||||
skill_count=len(skills),
|
||||
skill_list=skill_list_text,
|
||||
max_select=self._max_select,
|
||||
)),
|
||||
]):
|
||||
# 推理模型的思考走 reasoning_content(content 为空):作为思考实时推送
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
_emit_chunk(reasoning, True)
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
_emit_chunk(text, is_thinking)
|
||||
except Exception as e:
|
||||
logger.exception("ai_writing.skill_search: 流式选择技能失败")
|
||||
self._errors.append(f"skill select error: {e}")
|
||||
|
||||
result = parse_json(full_text, fallback) if full_text else fallback
|
||||
|
||||
wanted_names = result.get("selected_skills") or []
|
||||
if not isinstance(wanted_names, list):
|
||||
wanted_names = []
|
||||
by_name = {s.name: s for s in skills}
|
||||
reason = result.get("reason", "") or ""
|
||||
chosen: list[dict] = []
|
||||
for name in wanted_names:
|
||||
if not isinstance(name, str):
|
||||
continue
|
||||
sk = by_name.get(name.strip())
|
||||
if sk and sk.name not in {c["skill"].name for c in chosen}:
|
||||
chosen.append({"skill": sk, "reason": reason})
|
||||
if len(chosen) >= self._max_select:
|
||||
break
|
||||
|
||||
self._emit_step({
|
||||
"type": "select_skills",
|
||||
"message": f"按相关度排序,候选 {len(chosen)} 个技能(将按顺序使用)",
|
||||
"skill_names": [c["skill"].name for c in chosen],
|
||||
"reason": reason,
|
||||
})
|
||||
return chosen
|
||||
|
||||
|
||||
# 保留旧名字让 search/__init__.py 的导出不报错;研究节点不再用它
|
||||
SkillSearchProvider = SkillPicker
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
import operator
|
||||
from typing import Annotated, List, Literal, Optional, TypedDict
|
||||
|
||||
# ── 素材 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
class Material(TypedDict, total=False):
|
||||
id: str
|
||||
title: str
|
||||
content: str # 截断到 500 字
|
||||
source: str
|
||||
published_at: str
|
||||
relevance_score: float
|
||||
source_type: str # "internet" | "intranet" | "skill"
|
||||
url: str
|
||||
recUuid: str
|
||||
# skill 类素材附带的元信息(skill_name / container_path / category),其他类型为空
|
||||
extra_metadata: dict
|
||||
|
||||
class SkillStep(TypedDict, total=False):
|
||||
"""技能检索过程的单步记录,用于前端时间线展示。
|
||||
|
||||
type=match_skills:根据用户输入流式分析有没有匹配技能(无 skill_names)
|
||||
type=list_skills:列出可用技能
|
||||
type=select_skills:LLM 从中选出的相关技能
|
||||
type=invoke_skill:实际调用某一个技能检索
|
||||
type=stop_early:达到素材阈值,提前停止后续技能调用
|
||||
"""
|
||||
type: str
|
||||
message: str
|
||||
skill_names: List[str]
|
||||
skill_name: str
|
||||
matched_count: int
|
||||
reason: str
|
||||
|
||||
class MaterialPackage(TypedDict, total=False):
|
||||
keywords: List[str]
|
||||
materials: List[Material]
|
||||
summary: str
|
||||
# 是否本次素材来自技能检索(覆盖式替换 web 检索)
|
||||
skill_search_used: bool
|
||||
# 技能检索的步骤记录(前端展示"使用了 X 技能"卡片)
|
||||
skill_search_steps: List[SkillStep]
|
||||
# 内网知识库接口诊断:端点、每个检索词的命中数/耗时/错误、原始响应片段等。
|
||||
# 用于排查 "检索很快返回 0 条" 时到底是否真正调用了内网接口。
|
||||
intranet_debug: dict
|
||||
|
||||
# ── 大纲 ──────────────────────────────────────────────────────────────────────
|
||||
# 三级结构:章节(##) → 小节(###) → 子要点(bullet -)。
|
||||
# Outline.sections[] = Level 1 章节(渲染为 ## 由系统注入)
|
||||
# OutlineSection.key_points[] = Level 2 小节(渲染为 ### 标题)
|
||||
# KeyPoint.sub_points[] = Level 3 子要点(bullet "- ...",非标题)
|
||||
#
|
||||
# 历史上 key_points 曾是 List[str](2 级):每个 str 既是小节标题又是渲染单位。
|
||||
# 升级到 3 级后,key_points 变成 List[KeyPoint],每个 KeyPoint 自带 sub_points
|
||||
# 列表;旧 session 里的 string 项由 writer_outline._parse_outline 自动 lift 成
|
||||
# {point_title: <原 string>, sub_points: []},无需迁移数据。
|
||||
|
||||
class KeyPoint(TypedDict):
|
||||
point_title: str # 小节标题(渲染为 ### 二级小标题)
|
||||
sub_points: List[str] # 子要点(可为空;非空时在小节正文里以 bullet 列表呈现)
|
||||
|
||||
class OutlineSection(TypedDict):
|
||||
section_title: str
|
||||
key_points: List[KeyPoint] # 注意:旧版是 List[str],解析层做了兼容
|
||||
material_ids: List[str]
|
||||
|
||||
class Outline(TypedDict):
|
||||
title: str
|
||||
sections: List[OutlineSection]
|
||||
|
||||
# ── 草稿 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
class DraftArticle(TypedDict):
|
||||
title: str
|
||||
outline: Outline
|
||||
full_markdown: str
|
||||
word_count: int
|
||||
revision_count: int
|
||||
|
||||
|
||||
# ── 样文仿写 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class SampleStyleProfile(TypedDict, total=False):
|
||||
summary: str
|
||||
article_type_guess: str
|
||||
tone: str
|
||||
structure: List[str]
|
||||
language_features: List[str]
|
||||
sentence_style: str
|
||||
opening_style: str
|
||||
ending_style: str
|
||||
imitation_guidance: List[str]
|
||||
avoid_copying: List[str]
|
||||
sample_outline: List[str]
|
||||
|
||||
|
||||
# ── 编辑审核 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class ReviewIssue(TypedDict):
|
||||
category: str # "fact" | "logic" | "language"
|
||||
severity: str # "major" | "minor"
|
||||
location: str
|
||||
issue: str
|
||||
suggestion: str
|
||||
|
||||
class ReviewResult(TypedDict):
|
||||
verdict: str # "pass" | "reject"
|
||||
fact_score: int
|
||||
logic_score: int
|
||||
language_score: int
|
||||
overall_score: int
|
||||
pass_threshold: int
|
||||
revision_notes: List[ReviewIssue]
|
||||
|
||||
# ── 用户干预 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class UserInterventionPayload(TypedDict, total=False):
|
||||
action: str
|
||||
approved_material_ids: List[str]
|
||||
extra_keywords: List[str]
|
||||
# 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
|
||||
override_verdict: bool
|
||||
|
||||
# ── 主状态 ────────────────────────────────────────────────────────────────────
|
||||
|
||||
WritingStatus = Literal[
|
||||
"analyzing_sample",
|
||||
"researching", "awaiting_material_confirm",
|
||||
"writing_outline", "awaiting_outline_confirm",
|
||||
"writing_draft", "awaiting_section_help", "awaiting_draft_confirm",
|
||||
"reviewing", "awaiting_review_confirm",
|
||||
"revising", "done", "error",
|
||||
]
|
||||
|
||||
|
||||
class BlockedSection(TypedDict):
|
||||
"""严格模式下素材不足、无法写作的章节。"""
|
||||
section_title: str
|
||||
reason: str
|
||||
|
||||
class AIWritingState(TypedDict):
|
||||
# 写作配置
|
||||
user_intent: str
|
||||
# 用户在写作表单粘贴的「已有大纲」原文(可选)。非空时 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
|
||||
sample_text: str
|
||||
sample_filename: str
|
||||
sample_style_profile: Optional[SampleStyleProfile]
|
||||
imitate_strength: str
|
||||
preserve_sample_structure: bool
|
||||
article_type: str
|
||||
# 当 article_type 是「已有类型库里没有」的新类型时,配置助手识别出的一句话特征说明。
|
||||
# 仅本次写作临时使用(不入库):在大纲生成时作为 hint 附在类型后,影响全文结构与侧重。
|
||||
article_type_detail: str
|
||||
target_audience: str
|
||||
word_count_target: int
|
||||
# 素材检索关键词数量(用户指定,1~10,默认 6)
|
||||
keyword_count: int
|
||||
# 写作模式:strict=严格依据素材 / loose=可结合通用知识(默认 loose)
|
||||
writing_mode: str
|
||||
# 是否在成稿里加参考文献:正文引用素材处加角标 [n],文末按出现顺序生成「参考文献」列表(像论文)。
|
||||
add_references: bool
|
||||
# 检索类型:general=通用检索(按素材收集专家 agent 配置的技能编排,默认)/
|
||||
# knowledge_base=知识库检索(直连 ai_writing.intranet_search_url 内网 ES 接口,
|
||||
# 不走技能编排——弱模型识别不了技能时用这条)/ notebook=我的空间(仅笔记检索)。
|
||||
# 旧值 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 不带原
|
||||
# model_name,存进 state 才能让 resume 后才运行的节点拿到用户模型。
|
||||
model_name: str
|
||||
# 笔记来源(可选):传入后 researcher 阶段会额外从笔记检索素材
|
||||
notebook_search_space_id: Optional[int]
|
||||
notebook_source_ids: Optional[List[int]]
|
||||
|
||||
# Agent A 输出
|
||||
material_package: Optional[MaterialPackage]
|
||||
|
||||
# Agent B 输出
|
||||
current_outline: Optional[Outline]
|
||||
drafts: Annotated[List[DraftArticle], operator.add]
|
||||
current_draft: Optional[DraftArticle]
|
||||
|
||||
# Agent C 输出
|
||||
review_result: Optional[ReviewResult]
|
||||
|
||||
# 用户干预数据(每个干预点会覆盖)
|
||||
last_user_intervention: Optional[UserInterventionPayload]
|
||||
|
||||
# 严格模式下素材不足、待用户决策的章节(写作节点产出,求助暂停点消费)
|
||||
blocked_sections: Optional[List[BlockedSection]]
|
||||
|
||||
# 流程控制
|
||||
revision_count: int
|
||||
max_revisions: int
|
||||
status: WritingStatus
|
||||
|
||||
# SSE 进度推送队列(每个节点追加)
|
||||
progress_events: Annotated[List[dict], operator.add]
|
||||
Reference in new issue
Block a user