feat(roundtable): 深链策划流程小图 + 流程图弹窗横向/缩略图/深蓝适配

- Step1 消息列表头部:深链 taskId 模式取到目的树后,左侧标题换成「策划流程」
  彩色层级小图(任务→目的→…→叙事,每级配色对齐流程图节点 + 类型小图标),
  点击打开任务目的流程图弹窗;仅第一步、有数据时出现。
- TaskPurposeDialog 流程图:横向(LR)布局 + 右下角缩略图(MiniMap)。
- FlowGraphPanel 深蓝主题适配(随 ThemeContext):容器/头部/画布点阵/连线/图例/
  小地图/节点详情弹窗 + 节点配色(FLOW_NODE_TYPE_META_DARK),浅色红主题不受影响。
- 圆桌侧栏深色兜底:neutral 家族 + 米色 hex(#FAF6F5/#ECE7E4/#E8D0CC) 映射到深蓝
  token(关系图背板、第N步 tag 等不再泛白)。

附带提交工作区其余暂存改动(AI 写作等),不含 config.yaml。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
董宏凯andClaude Opus 4.8 committed 2026-06-18 17:39:19 +08:00
1 parent 85cca36ddb
commit 03b6fb724c
40 files changed
+2167 -119

No files matched your search

@@ -271,6 +271,12 @@ Seeding mirrors the roundtable pattern: verified `config.yaml` + `SOUL.md` ship
**Skill-driven retrieval (检索类型).** `material_source` has exactly two values: `general` (通用检索, default) and `notebook` (我的空间); legacy `knowledge_base`/`skill` are normalized to `general` by the researcher node. General retrieval is orchestrated from the **skills configured on the `ai-writing-researcher` agent** (`config.yaml → skills`, editable from the agent management page): the built-in `knowledge-base-search` skill (知识库检索) runs the native keyword fan-out web/intranet search (the old default path), while any other configured skill searches with a `description`-targeted query; skills run in configured order with early-stop at `max_materials`, and an empty/unresolvable skills config falls back to the built-in knowledge search so retrieval always works. The skill itself is seeded from `_ai_writing_seed_assets/skills/knowledge-base-search/SKILL.md` into `skills/public/` (never overwrites), and a pre-existing researcher `config.yaml` lacking the `skills` key is patched in place with the default (explicit `skills: []` is respected). **PAUSE-1 input append flow**: when the user types natural language into the material-confirm input (`re_search`/legacy `enable_skill_search` + `user_query`), the node matches the most relevant configured skill via `SkillPicker(candidates=…)` (skipped when only one skill is configured), searches with the user's words, and **appends** the new materials to the existing package (URL-dedup, original order preserved, no LLM re-rank/truncation, 100-item hard cap). **Skill-failure fault tolerance**: a configured skill that times out / raises / has all its keyword searches fail (vs. legitimately returning 0 results) no longer just silently moves on — the researcher node emits a `skill_search_step` of type `skill_error` (carrying the error `reason`), and if the configured skills collected **no** new materials at all it runs a `fallback` pass over other available skills (`_fallback_candidate_skills`: the built-in `knowledge-base-search` native path first, then other enabled catalog skills not yet tried) instead of erroring out. The frontend `SkillStepsCard` renders `skill_error` (rose) and `fallback` (amber) so the timeline shows "技能报错 → 改用其他技能搜集". Tests: `tests/test_ai_writing_material_source.py`.
**Skill-call timeout** is `AI_WRITING_SKILL_SEARCH_TIMEOUT_SECONDS` (env, default **120s**; `researcher.py` `_SKILL_SEARCH_TIMEOUT_SECONDS`); the DuckDuckGo provider's internal cap was raised 30s→120s so it doesn't undercut it.
**Agentic skill execution (`ai_writing.researcher_skill_agent`, default false).** By default a configured non-native skill is *not* run agent-style — the node just turns its `description` into a query for the generic search provider. With this flag on, the directed-skill branch instead runs `nodes/_skill_agent.py::run_skill_search_agent`: it injects the skill's **SKILL.md** (read from `skill.skill_file`) alongside the researcher SOUL as the system prompt, binds a `search` tool to the researcher model, and runs a bounded (`_MAX_TURNS=4`) tool-call loop so the model drives the retrieval per the skill's instructions; the `SearchResult`s the tool returns are harvested directly as materials (no fuzzy text extraction). Requires a tool-calling model; if the model can't tool-call (returns no `tool_calls`) or the agent yields nothing, `_execute_skill` falls back to the legacy description→query search so materials are never empty. The built-in `knowledge-base-search` native concurrent path is unchanged. Tests: `tests/test_ai_writing_skill_agent.py`.
**Background suspend (后台挂起).** A writing session can be handed off to the server to auto-drive the remaining flow to finalize, surviving page leave / refresh / account switch. `POST /api/ai-writing/sessions/{id}/background` (owner-checked, idempotent) starts `AIWritingJobExecutor` (`app/gateway/ai_writing_job_executor.py`), wired in `deps.py` as `app.state.ai_writing_job_executor`. The executor **drives the same `ai_writing` graph in-process** — it builds `make_ai_writing_graph()`, attaches the shared `app.state.checkpointer` (the very saver the runtime worker uses, so it continues the real thread state the frontend already wrote), sets the owning user via `set_current_user`, and loops `aget_state` → `ainvoke(Command(resume=…))` until `status==done`. **Auto-action per interrupt**: material/outline confirm → `confirm`; section_help → every blocked section `loose` (continue with general knowledge, never re-loop to research); draft_confirm → `to_editor` until reviewed, then `finalize` (on pass, or when `revision_count >= max_revisions`); review_confirm → `accept_review` (revise & re-review until pass or budget exhausted, then forced finalize). To avoid racing a still-running runtime worker (suspend mid-node), the driver **waits for an actionable state** (`_wait_for_actionable`): it only acts when the graph is at an interrupt / done, or when the checkpoint stops advancing (no worker → abandoned/restart). No new table — the graph checkpoint + `ai_writing_sessions` already hold all state; `status="background"` marks 挂起中 (surfaced in the history dropdown), nodes write `draft`/`review_result`/`status=done` as usual, so the user reopens the finished article from history. Per-user concurrency cap `AI_WRITING_BACKGROUND_SLOTS_PER_USER` (default 2, same SQLite-lock rationale as the roundtable executor). Startup `reconcile` (in `deps.py`) relaunches drivers for sessions left `background` after a restart (`AIWritingSessionRepository.list_by_status`). **Progress flowchart popup**: `GET /api/ai-writing/sessions/{id}/progress` (owner-checked) reads the graph checkpoint via `executor.get_progress` and derives a canonical pipeline `stage` key (`intent/research/material_confirm/outline/outline_confirm/draft/draft_confirm/review/done`, from the pending interrupt's `pause_point` or `snapshot.next` node) + `note` (素材不足求助 / 编辑打回) + review progress + `isRunning`. Frontend: 「后台挂起」 button + auto-suspend on route unmount / `beforeunload` (`AIWritingPanel.tsx`, `suspendAIWritingToBackground[Keepalive]` in `api/ai-writing-sessions.ts`); 挂起中 badge in `AIWritingHistoryDropdown.tsx`, and **clicking a 挂起中 history record opens a flowchart dialog** (`AIWritingFlowDialog.tsx` polls `getAIWritingProgress` every 2.5s, renders the fixed vertical pipeline `AIWritingFlowChart.tsx` lit by the live `stage`) so the user sees how far the background run has gotten without reopening/taking over the session. **Transcript consistency**: the rich history timeline (`AIWritingHistoryView` → `AIWritingTimeline`) replays `transcript = {progressEvents, completedInterventions}`, normally assembled+saved client-side. A background run has no client, so the executor rebuilds it at the end (`_save_transcript`/`_build_transcript_from_checkpoint`) **entirely from the final checkpoint** — both `progressEvents` and `completedInterventions` are derived from the same set of `resumed` events (every pause-resume emits exactly one), so they are always 1:1 aligned. For each `resumed`: classify its message → `(pausePoint, action)` (`_classify_resumed`, matched against `graph.py`'s resume messages), insert an `await_user` (with the pause prompt) before it, and build a **rich** intervention card enriched from final state (material_confirm→`materials`, outline_confirm→`outline`, draft_confirm→`draftTitle`/`draftWordCount`/`reviewResult`, review_confirm→`reviewResult`); `materials_ready` is inserted before the first material-confirm. (Earlier code merged the frontend's pre-suspend `completedInterventions` with auto ones and aligned by index — but the frontend's last save can predate its most recent decision, so the counts drifted and cards rendered off-by-one with empty data; deriving everything from the checkpoint root-fixes both.) Raw (snake_case) events **and** intervention data are normalized on load by the **exported** `normalizeProgressDict` / `normalize{Material,Outline,ReviewResult}` (idempotent on already-camelCase live transcripts — one normalize path for both sources), so reopening a background-completed session shows the same cards as a frontend-driven one. Tests: `tests/test_ai_writing_background.py`.
### Admin Leaderboard Snapshots (`/api/admin/users/leaderboard*`)
The admin ????? is served from **precomputed per-day snapshots** in `admin_leaderboard_daily_stats` (keyed by `(stat_date, settings_hash)`), not by aggregating raw rows on every request. A background scheduler inside the Gateway (`_leaderboard_snapshot_scheduler_loop` in `app/gateway/app.py`, singleton file-locked) ticks every `ADMIN_LEADERBOARD_BACKGROUND_INTERVAL_SECONDS` (default 300s): it prewarms recent days, then builds any `queued`/`error`/stale-`running` day. `GET /leaderboard` merges the per-day snapshots for the requested range. **Today** is rebuilt whenever its snapshot is older than `ADMIN_LEADERBOARD_TODAY_TTL_SECONDS` (default 43200s = half a day, so the leaderboard refreshes ~twice daily); a past day, once `ready`, is treated as fresh forever.
@@ -0,0 +1,546 @@
"""AI 写作「后台挂起」执行器。
把 AI 写作 LangGraph 图作为**后台 asyncio 任务**驱动到定稿,survive 发起请求的
生命周期(离开页面 / 刷新 / 切换账号都不打断)。
与圆桌后台作业(``roundtable_job_executor``)的根本区别:AI 写作的编排**就是
LangGraph 图本身**(4 个 ``interrupt()`` 干预点),所以这里不另起编排引擎,而是
直接驱动同一张图——用 app 启动时创建的**共享 checkpointer**(``app.state.checkpointer``,
即运行时 worker 跑该图用的同一个 saver,见 ``runtime/runs/worker.py`` 的
``agent.checkpointer = ctx.checkpointer``)续上前端已经写到 checkpoint 的线程状态,
在每个 ``interrupt()`` 处用「默认放行」动作自动 resume,一路推进到定稿:
素材确认 → confirm
大纲确认 → confirm
缺素材求助 → 全部「通用知识续写」(loose,不回检索、不丢章节)
草稿确认 → 未审过先 to_editor,审过(通过/用完次数)再 finalize
编辑打回 → accept_review(按编辑意见重写),直到通过或用完 max_revisions
图节点本就把进度落库(``writer_draft`` 写 draft、``editor`` 写 review_result、
``pause_*`` finalize 时 ``_mark_session_done`` 写 status=done),所以后台跑完用户
从历史里重新打开会话即可看到成稿——这里无需额外的产物表。
"""
from __future__ import annotations
import asyncio
import logging
import os
from langgraph.types import Command
from deerflow.runtime.user_context import AUTO, reset_current_user, set_current_user
logger = logging.getLogger(__name__)
def _slots_per_user() -> int:
"""每个用户同时真正在跑的后台写作上限(默认 2)。
根因与圆桌同源:checkpointer 多为单连接 SQLite(一把 asyncio.Lock 串行全进程
checkpoint 读写),后台作业无并发上限会把多张图的模型轮次压到同一把锁 + 同一
离线模型端点,拖慢前台。按用户限流后随便挂多少都能建,只是同时真跑的 ≤ N。
前台实时路径走 LangGraph 运行时 worker,不经这里,永不受限。
"""
raw = os.getenv("AI_WRITING_BACKGROUND_SLOTS_PER_USER", "").strip()
if not raw:
return 2
try:
return max(1, int(raw))
except ValueError:
return 2
# 每用户一把信号量(懒建)。键为 user_id(None → "default")。
_user_semaphores: dict[str, asyncio.Semaphore] = {}
def _user_semaphore(user_id: str | None) -> asyncio.Semaphore:
key = user_id or "default"
sem = _user_semaphores.get(key)
if sem is None:
sem = asyncio.Semaphore(_slots_per_user())
_user_semaphores[key] = sem
return sem
class _JobUser:
"""最小 CurrentUser:满足 user_context 的 ``.id`` 结构协议。"""
def __init__(self, user_id: str | None) -> None:
self.id = user_id
# 后台运行中的会话状态值(历史下拉据此显示「挂起中」徽标)。
STATUS_BACKGROUND = "background"
# 单个会话最多自动推进的步数(resume / 续跑各算一步)。安全上限,防图拓扑异常死循环。
# 正常一篇:素材→大纲→草稿→审核(最多 max_revisions 轮)→定稿,远小于这个数。
_MAX_DRIVE_STEPS = 60
# 「等待可操作态」轮询:用户在节点执行中(如素材搜集)点后台挂起时,运行时 worker
# 可能还在跑那个节点。此时**绝不能**并发 ainvoke 同一线程(会撞坏 checkpoint),先观察:
# - 出现 interrupt / 走到 done → 立刻接管;
# - 停在节点间且 checkpoint 连续 _INFLIGHT_MAX_STABLE 次不再推进 → 判定无 worker 在跑
# (被遗弃 / 进程重启续跑),可安全自驱。
_INFLIGHT_POLL_SECONDS = 1.5
_INFLIGHT_MAX_STABLE = 3
# 等待极长节点(LLM 慢)的轮询上限,≈ _INFLIGHT_MAX_WAIT_POLLS * _INFLIGHT_POLL_SECONDS 秒。
_INFLIGHT_MAX_WAIT_POLLS = 800
# 进度弹窗:把「下一个待执行节点 / 当前 interrupt 暂停点」映射成前端流程图的规范化阶段 key。
_NODE_TO_STAGE = {
"intent_parser": "intent",
"researcher": "research",
"pause_material": "material_confirm",
"writer_outline": "outline",
"pause_outline": "outline_confirm",
"writer_draft": "draft",
"pause_section_help": "draft",
"pause_draft": "draft_confirm",
"editor": "review",
"pause_review": "review",
}
_PAUSE_TO_STAGE = {
"material_confirm": "material_confirm",
"outline_confirm": "outline_confirm",
"section_help": "draft",
"draft_confirm": "draft_confirm",
"review_confirm": "review",
}
# 暂停点附带的细化说明(流水线主链之外的分支)。
_STAGE_NOTE = {
"section_help": "素材不足求助",
"review_confirm": "编辑打回",
}
# resumed 事件 message → (pause_point, action)。与 graph.py 各 pause 节点 emit 的 resumed
# 文案严格对齐(见 graph.py pause_material/outline/draft/section_help/review)。从 checkpoint
# 重建 await_user/干预卡时据此分类——await_user 与 completedInterventions 都源自同一批 resumed
# 事件,永远 1:1 对齐(不再依赖前端可能滞后的已存 interventions,根治错位 off-by-one)。
def _classify_resumed(message: str) -> tuple[str | None, str | None]:
m = message or ""
if "确认素材" in m:
return "material_confirm", "confirm"
if "匹配技能检索" in m or "重新检索" in m or "重新搜索" in m:
return "material_confirm", "re_search"
if "确认大纲" in m:
return "outline_confirm", "confirm"
if "重新规划大纲" in m:
return "outline_confirm", "re_outline"
if "提交编辑审核" in m:
return "draft_confirm", "to_editor"
if "强制定稿" in m:
return "review_confirm", "force_finalize"
if "确认定稿" in m:
return "draft_confirm", "finalize"
if "按用户意见重写" in m:
return "draft_confirm", "user_revise"
if "按编辑意见重写" in m:
return "review_confirm", "accept_review"
if "补充检索素材" in m:
return "section_help", "supplement"
if "继续写作" in m:
return "section_help", "loose"
return None, None
# 暂停提示文案(await_user 的 message)——对齐 graph.py 各 interrupt() 的 message,
# 让历史回显里暂停点显示与实时一致的提示语。
_PAUSE_MESSAGES = {
"material_confirm": "素材收集专家已完成素材搜集,请确认素材后继续。",
"outline_confirm": "作家已完成大纲规划,请确认或编辑大纲后继续。",
"draft_confirm": "草稿已完成,请确认或给出修改意见。",
"section_help": "部分章节缺少足够素材支撑写作,请决定如何处理。",
"review_confirm": "编辑认为文章需要修改,请决定下一步操作。",
}
def _build_transcript_from_checkpoint(values: dict) -> tuple[list[dict], list[dict]]:
"""从最终 checkpoint 重建 (progressEvents, completedInterventions),二者自洽 1:1 对齐。
每条 ``resumed`` 事件 = 一个已处理的暂停点:在其前插一条 ``await_user``(带暂停提示
文案),并据 resumed 文案分类出 pausePoint+action 生成**带富数据**的干预卡(素材列表/
大纲/草稿标题字数/审核结果取自最终 state;snake_case 字段由前端归一化)。素材确认前
再插一条 ``materials_ready``。其余里程碑(intent_ready/step_*/outline_ready/draft_ready/
review_ready/notice/error)节点本就 append 进 progress_events,原位即正确顺序。
时间线靠 ``await_user`` 锚点按序取 completedInterventions 渲染干预卡——这里两者都源自
同一批 resumed,数量与顺序天然一致,根治了「卡片错位 / 只显示标题没数据」的 bug。
"""
raw_events = list(values.get("progress_events") or [])
pkg = values.get("material_package") or {}
outline = values.get("current_outline")
draft = values.get("current_draft") or {}
review = values.get("review_result")
status = values.get("status")
events: list[dict] = []
interventions: list[dict] = []
materials_inserted = not pkg
for ev in raw_events:
if isinstance(ev, dict) and ev.get("type") == "resumed":
pp, action = _classify_resumed(ev.get("message") or "")
if pp == "material_confirm" and not materials_inserted:
events.append({
"type": "materials_ready",
"materials": pkg.get("materials") or [],
"keywords": pkg.get("keywords") or [],
"summary": pkg.get("summary") or "",
})
materials_inserted = True
events.append({"type": "await_user", "pause_point": pp, "message": _PAUSE_MESSAGES.get(pp, "")})
rec: dict = {"pausePoint": pp, "action": action}
if pp == "material_confirm":
rec["materials"] = pkg.get("materials") or []
elif pp == "outline_confirm":
if outline:
rec["outline"] = outline
elif pp == "draft_confirm":
rec["draftTitle"] = draft.get("title") or ""
rec["draftWordCount"] = int(draft.get("word_count") or 0)
if review:
rec["reviewResult"] = review
elif pp == "review_confirm":
if review:
rec["reviewResult"] = review
interventions.append(rec)
events.append(ev)
if not materials_inserted:
# 没有 material_confirm 暂停(罕见)但有素材包 → 插到第一条 outline_ready 前,否则附末尾。
mr = {"type": "materials_ready", "materials": pkg.get("materials") or [],
"keywords": pkg.get("keywords") or [], "summary": pkg.get("summary") or ""}
idx = next((i for i, e in enumerate(events) if isinstance(e, dict) and e.get("type") == "outline_ready"), None)
events.insert(idx, mr) if idx is not None else events.append(mr)
if status == "done" and not any(isinstance(e, dict) and e.get("type") == "done" for e in events):
events.append({"type": "done", "message": "写作完成"})
return events, interventions
def _checkpoint_id(snapshot) -> str | None:
"""取快照的 checkpoint_id,用于判断图状态是否在被(worker)推进。"""
cfg = getattr(snapshot, "config", None) or {}
try:
return (cfg.get("configurable") or {}).get("checkpoint_id")
except AttributeError:
return None
def _auto_resume_payload(pause_point: str | None, interrupt_value: dict, values: dict) -> dict:
"""给定当前 ``interrupt()`` 暂停点 + 图状态,返回「默认放行」的 resume 入参。
设计目标:一路推进到定稿且**不绕回检索 / 不无限重审**。判定逻辑见模块 docstring。
"""
if pause_point == "material_confirm":
# 不传 approvedMaterialIds → 节点默认采用全部素材。
return {"action": "confirm"}
if pause_point == "outline_confirm":
# 不传 editedOutline → 节点采用当前大纲。
return {"action": "confirm"}
if pause_point == "section_help":
# 严格模式缺素材求助:全部章节按「通用知识续写」(loose)处理——既不回检索
# (supplement 会绕回 researcher 再触发暂停),也不删章节,直接续写到底。
blocked = (interrupt_value or {}).get("blocked_sections") or values.get("blocked_sections") or []
decisions = []
for b in blocked:
title = (b or {}).get("section_title") or (b or {}).get("sectionTitle") or ""
if title:
decisions.append({"sectionTitle": title, "decision": "loose"})
return {"action": "section_help", "sectionDecisions": decisions}
if pause_point == "draft_confirm":
review = values.get("review_result")
rev = int(values.get("revision_count") or 0)
max_rev = int(values.get("max_revisions") or 3)
if not review:
# 还没经过编辑审核 → 先送审。
return {"action": "to_editor"}
if (review or {}).get("verdict") == "pass":
# 审核通过 → 定稿。
return {"action": "finalize"}
if rev >= max_rev:
# 用完修改次数仍未通过(after_review 强制落到草稿确认)→ 接受现稿定稿。
return {"action": "finalize"}
# 刚按编辑意见改过、review 还是旧的不通过结果 → 再次送审,拿新一轮审核。
return {"action": "to_editor"}
if pause_point == "review_confirm":
# 编辑打回(仍有修改额度)→ 按编辑意见重写,重写完会再次 to_editor 复审。
return {"action": "accept_review"}
# 未知暂停点:保守放行。
return {"action": "confirm"}
def _pending_interrupt(snapshot) -> dict | None:
"""从状态快照里取第一个待处理 ``interrupt()`` 的 value(dict),无则 None。"""
for task in getattr(snapshot, "tasks", None) or ():
for intr in getattr(task, "interrupts", None) or ():
value = getattr(intr, "value", None)
if isinstance(value, dict):
return value
return {"value": value}
return None
class AIWritingJobExecutor:
"""驱动 AI 写作图在后台跑到定稿。按 thread_id 幂等、按用户限流。"""
def __init__(self, app) -> None:
# 闭包持有 app,运行时再读 ``app.state.checkpointer`` / session store——它们在
# lifespan 里创建,运行时读保证拿到最新实例(与 worker 用同一个 checkpointer)。
self._app = app
self._tasks: dict[str, asyncio.Task] = {}
# ── 公开 API ──────────────────────────────────────────────────────────────
def start(self, *, thread_id: str, user_id: str | None) -> bool:
"""启动一个后台驱动任务。同一 thread 已在跑则忽略(幂等)。返回是否新启动。"""
existing = self._tasks.get(thread_id)
if existing is not None and not existing.done():
return False
task = asyncio.create_task(self._run(thread_id, user_id))
self._tasks[thread_id] = task
task.add_done_callback(lambda t, tid=thread_id: self._tasks.pop(tid, None))
return True
def is_running(self, thread_id: str) -> bool:
task = self._tasks.get(thread_id)
return task is not None and not task.done()
async def get_progress(self, thread_id: str) -> dict:
"""读取该写作线程当前所处阶段(从 graph checkpoint 推导),供「查看进度」弹窗。
返回规范化的 ``stage`` key(见 ``_NODE_TO_STAGE`` / ``_PAUSE_TO_STAGE``,与前端
流程图阶段一一对应)+ 可选 ``note`` + 审核进度。无 checkpoint / 读失败 → stage=None。
"""
empty = {"stage": None, "note": None, "revisionCount": 0, "maxRevisions": 3, "reviewVerdict": None}
checkpointer = self._checkpointer()
if checkpointer is None:
return empty
from deerflow.agents.ai_writing.graph import make_ai_writing_graph
graph = make_ai_writing_graph()
graph.checkpointer = checkpointer
try:
snapshot = await graph.aget_state({"configurable": {"thread_id": thread_id}})
except Exception: # noqa: BLE001 — 读不到 checkpoint 不致命
return empty
values = getattr(snapshot, "values", None) or {}
review = values.get("review_result") or {}
out = {
"stage": None,
"note": None,
"revisionCount": int(values.get("revision_count") or 0),
"maxRevisions": int(values.get("max_revisions") or 3),
"reviewVerdict": review.get("verdict"),
}
if not values:
return out # 还没建 checkpoint
if values.get("status") == "done":
out["stage"] = "done"
return out
interrupt_value = _pending_interrupt(snapshot)
if interrupt_value is not None:
pp = interrupt_value.get("pause_point")
out["stage"] = _PAUSE_TO_STAGE.get(pp, pp)
out["note"] = _STAGE_NOTE.get(pp)
return out
next_nodes = list(getattr(snapshot, "next", None) or ())
if next_nodes:
node = next_nodes[0]
out["stage"] = _NODE_TO_STAGE.get(node, node)
return out
# 无 interrupt、无 next → 已结束。
out["stage"] = "done"
return out
def cancel(self, thread_id: str) -> bool:
task = self._tasks.get(thread_id)
if task is None or task.done():
return False
task.cancel()
return True
async def await_job(self, thread_id: str) -> None:
"""等待某后台任务结束(测试用;生产不调用)。"""
task = self._tasks.get(thread_id)
if task is not None:
try:
await task
except (Exception, asyncio.CancelledError): # noqa: BLE001
pass
def reconcile(self, *, sessions: list[dict]) -> int:
"""启动时把仍标记为 ``background`` 的会话重新拉起驱动(进程重启续跑)。
``sessions`` 为 ``[{id, user_id}, ...]``。返回拉起的任务数。
"""
launched = 0
for s in sessions:
tid = s.get("id")
if not tid:
continue
if self.start(thread_id=tid, user_id=s.get("user_id")):
launched += 1
if launched:
logger.info("[ai-writing-bg] reconcile relaunched %d background session(s)", launched)
return launched
# ── 内部 ────────────────────────────────────────────────────────────────
def _checkpointer(self):
return getattr(self._app.state, "checkpointer", None)
def _store(self):
return getattr(self._app.state, "ai_writing_session_store", None)
async def _set_status(self, thread_id: str, status: str) -> None:
store = self._store()
if store is None:
return
try:
await store.update(thread_id, user_id=AUTO, status=status)
except Exception: # noqa: BLE001 — 落库失败不致命
logger.warning("[ai-writing-bg] set status=%s for %s failed", status, thread_id, exc_info=True)
async def _run(self, thread_id: str, user_id: str | None) -> None:
token = set_current_user(_JobUser(user_id))
try:
checkpointer = self._checkpointer()
if checkpointer is None:
logger.warning("[ai-writing-bg] no checkpointer; cannot drive %s", thread_id)
return
await self._set_status(thread_id, STATUS_BACKGROUND)
from deerflow.agents.ai_writing.graph import make_ai_writing_graph
graph = make_ai_writing_graph()
# 与运行时 worker 同款:把共享 checkpointer 挂到图上,续上同一线程的 checkpoint。
graph.checkpointer = checkpointer
config = {"configurable": {"thread_id": thread_id}, "recursion_limit": 100}
# 按用户限流:同用户最多 N 篇后台写作同时真跑(见 _slots_per_user)。
async with _user_semaphore(user_id):
done, _interventions = await self._drive(graph, config)
# 把后台跑出的对话时间线写进 transcript(与前端直接写作的存取一致)。
await self._save_transcript(thread_id, graph, config)
if not done:
# 图已停在非 done 终态(无 interrupt 也无 next):兜底标 done,避免会话
# 永久卡在「挂起中」。正常路径下 finalize 已由节点 _mark_session_done 写 done。
await self._mark_terminal(thread_id, graph, config)
except asyncio.CancelledError:
logger.info("[ai-writing-bg] job %s cancelled", thread_id)
raise
except Exception: # noqa: BLE001 — 顶层兜底
logger.exception("[ai-writing-bg] job %s crashed", thread_id)
await self._set_status(thread_id, "error")
finally:
reset_current_user(token)
async def _wait_for_actionable(self, graph, config) -> tuple[object, str]:
"""等到图处于可安全操作的状态,返回 ``(snapshot, action)``。
action ∈ {``interrupt``(有干预点待 resume), ``done``(已结束),
``push``(停在节点间且无 worker 在推进,可自驱)}。
见 ``_INFLIGHT_*`` 注释:避免与运行时 worker 并发跑同一线程。
"""
last_ckpt: object = object()
stable = 0
for _ in range(_INFLIGHT_MAX_WAIT_POLLS):
snapshot = await graph.aget_state(config)
if (getattr(snapshot, "values", None) or {}).get("status") == "done":
return snapshot, "done"
if _pending_interrupt(snapshot) is not None:
return snapshot, "interrupt"
if not getattr(snapshot, "next", None):
return snapshot, "done" # 无中断、无待执行 → 图已结束
ckpt = _checkpoint_id(snapshot)
if ckpt != last_ckpt:
# checkpoint 在变 → 有 worker 正推进该节点,继续等它到下一个干预点。
last_ckpt = ckpt
stable = 0
else:
stable += 1
if stable >= _INFLIGHT_MAX_STABLE:
return snapshot, "push" # 连续不动 → 无 worker,自己推进
await asyncio.sleep(_INFLIGHT_POLL_SECONDS)
# 等待超时(极长节点)→ 保守按可推进处理。
return await graph.aget_state(config), "push"
async def _drive(self, graph, config) -> tuple[bool, list[dict]]:
"""推进图直到定稿(status==done)或无可推进。
返回 ``(是否走到 done, 本次自动处理的 interventions)``。每个干预点记录
``{pausePoint, action}``,供重建 transcript 时与 ``await_user`` 对齐。
"""
thread_id = (config.get("configurable") or {}).get("thread_id")
interventions: list[dict] = []
for _ in range(_MAX_DRIVE_STEPS):
snapshot, action = await self._wait_for_actionable(graph, config)
values = getattr(snapshot, "values", None) or {}
if action == "done":
return values.get("status") == "done", interventions
if action == "interrupt":
interrupt_value = _pending_interrupt(snapshot) or {}
pause_point = interrupt_value.get("pause_point")
payload = _auto_resume_payload(pause_point, interrupt_value, values)
interventions.append({"pausePoint": pause_point, "action": payload.get("action")})
logger.info("[ai-writing-bg] auto-resume thread=%s pause=%s action=%s",
thread_id, pause_point, payload.get("action"))
await graph.ainvoke(Command(resume=payload), config)
continue
# action == "push":停在节点间且无 worker 在跑 → 继续推进。
await graph.ainvoke(None, config)
logger.warning("[ai-writing-bg] drive hit max steps for thread=%s", thread_id)
return False, interventions
async def _save_transcript(self, thread_id: str, graph, config) -> None:
"""把后台跑出的对话时间线写进 ``transcript`` —— 与前端直接写作的存取完全一致。
完全从最终 checkpoint 自洽重建(``_build_transcript_from_checkpoint``):progressEvents
与 completedInterventions 都源自同一批 ``resumed`` 事件,永远 1:1 对齐——不再依赖前端
可能滞后的已存 interventions(那是「卡片错位」bug 的根因)。
"""
store = self._store()
if store is None:
return
try:
snapshot = await graph.aget_state(config)
except Exception: # noqa: BLE001
return
values = getattr(snapshot, "values", None) or {}
events, interventions = _build_transcript_from_checkpoint(values)
transcript = {"progressEvents": events, "completedInterventions": interventions}
try:
await store.update(
thread_id, user_id=AUTO, transcript=transcript, completed_interventions=interventions
)
except Exception: # noqa: BLE001 — transcript 是锦上添花,存失败不影响作业
logger.warning("[ai-writing-bg] save transcript for %s failed", thread_id, exc_info=True)
async def _mark_terminal(self, thread_id: str, graph, config) -> None:
try:
snapshot = await graph.aget_state(config)
status = (getattr(snapshot, "values", None) or {}).get("status")
except Exception: # noqa: BLE001
status = None
# 走到 done 由节点写过了;否则若图已停止(无 next)兜底标 done,停在中途则保留。
if status == "done":
return
try:
snapshot = await graph.aget_state(config)
if not getattr(snapshot, "next", None):
await self._set_status(thread_id, "done")
except Exception: # noqa: BLE001
logger.warning("[ai-writing-bg] mark terminal for %s failed", thread_id, exc_info=True)
@@ -8,6 +8,7 @@ Initialization is handled directly in ``app.py`` via :class:`AsyncExitStack`.
from __future__ import annotations
import asyncio
import logging
from collections.abc import AsyncGenerator, Callable
from contextlib import AsyncExitStack, asynccontextmanager
@@ -172,6 +173,24 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
# 老 /start /stream /resume 路由仍走 request.app.state,互不影响。
_register_ai_writing_default_store(app.state.ai_writing_session_store)
# AI 写作「后台挂起」执行器:把写作图作为后台任务驱动到定稿(离页/刷新/切号不打断)。
# 用 app.state.checkpointer(与运行时 worker 同一份 saver)续上线程 checkpoint。
from app.gateway.ai_writing_job_executor import AIWritingJobExecutor
app.state.ai_writing_job_executor = AIWritingJobExecutor(app)
# 进程重启续跑:把仍标记为 background 的会话重新拉起驱动(best-effort,不阻塞启动)。
if app.state.ai_writing_session_store is not None:
from app.gateway.ai_writing_job_executor import STATUS_BACKGROUND
async def _reconcile_ai_writing_background() -> None:
try:
sessions = await app.state.ai_writing_session_store.list_by_status(STATUS_BACKGROUND)
app.state.ai_writing_job_executor.reconcile(sessions=sessions)
except Exception: # noqa: BLE001
logger.warning("[ai-writing-bg] startup reconcile failed", exc_info=True)
asyncio.create_task(_reconcile_ai_writing_background())
from deerflow.persistence.article_types import make_article_type_store
app.state.article_type_store = make_article_type_store(sf)
@@ -473,6 +492,11 @@ def get_ai_writing_session_store(request: Request):
return getattr(request.app.state, "ai_writing_session_store", None)
def get_ai_writing_job_executor(request: Request):
"""Return the AI writing background-suspend executor, or None when unavailable."""
return getattr(request.app.state, "ai_writing_job_executor", None)
def get_article_type_store(request: Request):
"""Return the article type store, or None when not configured."""
return getattr(request.app.state, "article_type_store", None)
@@ -233,6 +233,98 @@ async def save_transcript(session_id: str, body: SaveTranscriptRequest, request:
# 会话不存在或不属于当前用户:同样不抛 500,返回 success=False 即可
return {"success": False, "reason": "session_not_found"}
return {"success": True}
class BackgroundResponse(BaseModel):
# 是否新拉起了后台任务(False = 已在后台跑,幂等返回)
started: bool
# 会话当前状态(background = 挂起中)
status: str
@router.post("/sessions/{session_id}/background", response_model=BackgroundResponse)
async def suspend_to_background(session_id: str, request: Request):
"""把这篇写作转入「后台挂起」:后端自动驱动剩余流程到定稿。
前端在用户点「后台挂起」或离开页面时调用。幂等:同一会话已在后台跑则直接返回。
驱动逻辑见 ``app.gateway.ai_writing_job_executor``(素材/大纲自动确认、缺素材
通用知识续写、草稿送审、审核不过自动重写直到通过或用完次数再定稿)。
"""
from app.gateway.deps import (
get_ai_writing_job_executor,
get_ai_writing_session_store,
get_current_user,
)
executor = get_ai_writing_job_executor(request)
if executor is None:
raise HTTPException(status_code=503, detail="Background executor not available")
repo = get_ai_writing_session_store(request)
if repo is None:
raise HTTPException(status_code=503, detail="Session store not available")
user_id = await get_current_user(request)
# 鉴权 + 存在性:会话必须属于当前用户(get 已带 user_id 过滤)。
record = await repo.get(session_id, user_id=user_id)
if record is None:
raise HTTPException(status_code=404, detail="Session not found")
if record.get("status") == "done":
# 已定稿,无需后台驱动。
return BackgroundResponse(started=False, status="done")
started = executor.start(thread_id=session_id, user_id=user_id)
return BackgroundResponse(started=started, status="background")
class ProgressResponse(BaseModel):
# 业务表 status(background = 挂起中 / done / error / in_progress)
sessionStatus: str
# 规范化阶段 key(intent/research/material_confirm/outline/outline_confirm/draft/draft_confirm/review/done),
# 从 graph checkpoint 推导;None = 还没建 checkpoint。
stage: Optional[str] = None
# 阶段细化说明(素材不足求助 / 编辑打回)
note: Optional[str] = None
revisionCount: int = 0
maxRevisions: int = 3
reviewVerdict: Optional[str] = None
# 后台驱动任务是否在本进程在跑(重启后可能为 False 但 status 仍 background,由 reconcile 续起)
isRunning: bool = False
@router.get("/sessions/{session_id}/progress", response_model=ProgressResponse)
async def get_session_progress(session_id: str, request: Request):
"""读取一篇写作的实际进度(当前阶段),供「查看进度」流程图弹窗轮询。
阶段从 graph checkpoint 推导,反映后端真实推进位置;鉴权要求会话属于当前用户。
"""
from app.gateway.deps import (
get_ai_writing_job_executor,
get_ai_writing_session_store,
get_current_user,
)
repo = get_ai_writing_session_store(request)
if repo is None:
raise HTTPException(status_code=503, detail="Session store not available")
user_id = await get_current_user(request)
record = await repo.get(session_id, user_id=user_id)
if record is None:
raise HTTPException(status_code=404, detail="Session not found")
executor = get_ai_writing_job_executor(request)
progress = await executor.get_progress(session_id) if executor is not None else {}
return ProgressResponse(
sessionStatus=str(record.get("status") or "in_progress"),
stage=progress.get("stage") or None,
note=progress.get("note"),
revisionCount=int(progress.get("revisionCount") or 0),
maxRevisions=int(progress.get("maxRevisions") or 3),
reviewVerdict=progress.get("reviewVerdict"),
isRunning=bool(executor.is_running(session_id)) if executor is not None else False,
)
@router.get("/article-types", response_model=list[ArticleTypeResponse])
async def list_article_types(request: Request):
"""列出所有文章类型,按 sort_order 升序排列。"""
@@ -169,7 +169,19 @@ async def pause_outline_confirm(state: AIWritingState) -> dict:
async def pause_draft_confirm(state: AIWritingState, config: RunnableConfig) -> dict:
"""PAUSE-3:草稿确认"""
draft = state["current_draft"]
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": "草稿已完成,请确认或给出修改意见。",
@@ -308,7 +320,15 @@ def after_outline_pause(state: AIWritingState) -> str:
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"
@@ -377,7 +397,7 @@ def make_ai_writing_graph(config=None):
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"})
{"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,
@@ -126,9 +126,11 @@ def resolve_agent_model_name(
传入,如 researcher 的 keyword_model/rank_model)> ``state.model_name``
(用户写作表单选的主模型)> configurable.model_name。
strict=True(大纲/写作/审核):全部为空抛 ValueError(与 get_writing_model
口径一致——不静默回退默认模型);strict=False(researcher 辅助任务):
返回 None,由 create_chat_model 回退 config.yaml 的 models[0]。
全部为空时**不再抛错**(旧实现 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 "",
@@ -140,7 +142,7 @@ def resolve_agent_model_name(
if isinstance(name, str) and name.strip():
return name.strip()
if strict:
raise ValueError("未指定写作模型(model_name 为空):请在写作表单选择模型后重试")
logger.warning("写作模型未指定(state/configurable/agent.model 均为空),回退默认模型")
return None
@@ -0,0 +1,177 @@
"""像「智能体调用技能」那样用一个技能检索素材。
背景:素材收集专家(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
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 = ""
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=timeout_s
)
except asyncio.TimeoutError:
last_error = f"检索超时(>{timeout_s}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, ""
@@ -74,24 +74,30 @@ def get_model(config: RunnableConfig, model_name: str | None = None):
return create_chat_model(name=name)
def get_writing_model(config: RunnableConfig, state: dict):
"""大纲 / 写作 / 审核专用:严格使用用户在表单选定的模型,**不回退默认**。
def resolve_writing_model_name(config: RunnableConfig, state: dict) -> tuple[str | None, bool]:
"""解析大纲 / 写作 / 审核阶段使用的模型名,返回 ``(name, used_default)``。
用户模型在 intent_parser(首次 invocation,configurable.model_name 可用)阶段
捕获进 ``state['model_name']``,经 checkpoint 持久化。这三个节点都在
「确认素材 / 大纲」resume 之后才运行,而 resume 是新 invocation、其 config 不再
带 model_name —— 所以必须从 state 读,不能从 configurable 读(否则回退到 config
列表第一个模型,常是推理模型,既影响质量也会让 reasoning_content 走不出流式)。
优先级:``state['model_name']``(用户在表单选定,由 intent_parser 在首次
invocation 捕获并经 checkpoint 持久化)> ``configurable.model_name``(同一
invocation 内直跑、或前端在 resume 时一并回传的兜底)。
缺失视为配置错误直接抛 ValueError —— 由各节点的 try/except 兜底成 status:error,
而不是静默回退默认模型。
interrupt/resume 是新 invocation,其 config 默认不带原 model_name,所以正常情况
要从 state 读。两者皆空(历史会话 / 首轮捕获就为空 / 前端没传)时不再视为致命
错误,而是返回 ``(None, True)``:调用方据此回退 config.yaml 的 ``models[0]`` 并向
用户提示「未选模型,已用默认模型」,保证写作流程不被一个空模型名硬卡住。
"""
name = ((state or {}).get("model_name") or "").strip()
if not name:
# 兜底再看一眼 configurable(首次同 invocation 内直跑到这里的极端情况)
name = (config.get("configurable", {}).get("model_name", "") or "").strip()
if not name:
raise ValueError("未指定写作模型(model_name 为空):请在写作表单选择模型后重试")
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)
@@ -40,6 +40,7 @@ from ._agent_runner import (
run_writing_agent,
use_builtin_agent,
)
from ._skill_agent import run_skill_search_agent, use_skill_search_agent
from ._utils import get_ai_writing_config, llm_json, safe_get_stream_writer
logger = logging.getLogger(__name__)
@@ -83,10 +84,21 @@ _MAX_MAX_MATERIALS = 100
_DEFAULT_KEYWORD_COUNT = 6
_MAX_KEYWORD_COUNT = 10
# 单个技能的 web 检索超时(秒)。技能检索是顺序循环,
# DuckDuckGo 等 provider 偶发限速/挂起会把整段"技能检索过程"拖到几十秒看起来卡死;
# 超时即放弃该技能、emit 0 条结果继续下一个,保证时间线持续推进。
_SKILL_SEARCH_TIMEOUT_SECONDS = 25
# 单个技能的检索超时(秒)。技能检索是顺序循环,provider 偶发限速/挂起会把整段
# "技能检索过程"拖住;超时即放弃该技能、emit 0 条结果继续下一个,保证时间线持续推进。
# 默认 120s(≥2 分钟):内网检索 provider 本身就允许 timeout=120,之前 25s 会过早放弃
# 慢但有效的检索。可用环境变量 AI_WRITING_SKILL_SEARCH_TIMEOUT_SECONDS 覆盖。
def _env_int(name: str, default: int) -> int:
raw = (os.getenv(name) or "").strip()
if raw:
try:
return max(1, int(raw))
except ValueError:
pass
return default
_SKILL_SEARCH_TIMEOUT_SECONDS = _env_int("AI_WRITING_SKILL_SEARCH_TIMEOUT_SECONDS", 120)
# 内置「知识库检索」技能名:素材收集专家 agent 的默认技能。researcher 对它
# 特殊处理——不用 description 拼 query,而是对 Step 1 生成的全部检索词做
@@ -607,8 +619,34 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
error_detail = ";".join(dict.fromkeys(errs))[:200]
return added, error_detail
else:
# 定向技能检索:user_query 模式优先用用户原话;非知识库技能再
# 附上 description 让 query 贴合该技能的领域。
# ── 智能体式技能调用(config ai_writing.researcher_skill_agent 打开时)──
# 把技能 SKILL.md 注入 + 绑定 search 工具,让模型按技能说明驱动检索,
# 收割工具返回的 SearchResult 作为素材(与「智能体调用技能」逻辑一致)。
if use_skill_search_agent():
def _emit_q(q: str, _name=sk.name) -> None:
_emit_skill_step({
"type": "invoke_skill",
"message": f"素材收集专家用技能「{_name}」检索:{q[:60]}",
"skill_name": _name,
})
agent_results, agent_err = await run_skill_search_agent(
skill=sk,
intent=intent,
keywords=keywords,
user_query=user_query if is_user_query_search else "",
config=config,
state=state,
model_name_fallback=keyword_model,
max_results=per_skill,
timeout_s=_SKILL_SEARCH_TIMEOUT_SECONDS,
emit_query=_emit_q,
)
if agent_results:
return _collect(agent_results), ""
# 弱模型不会工具调用 / 智能体检索失败 → 回退直连检索,保证素材不空。
# 定向技能检索(默认路径 / 智能体模式回退):user_query 模式优先用用户原话;
# 非知识库技能再附上 description 让 query 贴合该技能的领域。
if is_user_query_search:
skill_query = user_query if is_knowledge else f"{user_query} {sk.description or ''}".strip()
else:
@@ -21,7 +21,7 @@ from ._agent_runner import (
stream_writing_agent,
use_builtin_agent,
)
from ._utils import get_writing_model, _content_to_text, count_words, reasoning_delta, ThinkTagParser, safe_get_stream_writer
from ._utils import resolve_writing_model_name, _content_to_text, count_words, reasoning_delta, ThinkTagParser, safe_get_stream_writer
logger = logging.getLogger(__name__)
@@ -209,11 +209,18 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
use_agent = use_builtin_agent("writer")
agent_label = "作家"
model = None
# 未选模型回退默认时给用户一条非致命提示(最终随草稿一并返回)
model_default_notice: dict | None = None
if use_agent:
try:
agent_cfg, _soul = load_writing_agent(WRITER_AGENT_ID)
# 模型守卫提前到循环外:模型名缺失/配错与旧路径同样转成 status:error
resolve_agent_model_name(agent_cfg, state, config)
# 模型解析提前到循环外:返回 None 表示 state/configurable/agent.model 全空,
# 后续 create_chat_model 会回退默认模型,这里据此准备一条用户提示。
if resolve_agent_model_name(agent_cfg, state, config) is None:
model_default_notice = {
"type": "notice", "agent_name": agent_display_name(WRITER_AGENT_ID, "作家"),
"title": "模型提示", "detail": "未检测到所选写作模型,已使用默认模型继续写作。",
}
agent_label = agent_display_name(WRITER_AGENT_ID, "作家")
except WritingAgentUnavailable as e:
logger.warning("writer builtin agent unavailable, falling back to legacy prompts: %s", e)
@@ -231,7 +238,14 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
# 模型创建守卫:模型名配错时 create_chat_model 抛 ValueError,
# 不拦截会硬崩溃整个图;此处转成可恢复的 status:error。
try:
model = get_writing_model(config, state)
model_name, used_default = resolve_writing_model_name(config, state)
if used_default:
model_default_notice = {
"type": "notice", "agent_name": "作家",
"title": "模型提示", "detail": "未检测到所选写作模型,已使用默认模型继续写作。",
}
from deerflow.models import create_chat_model
model = create_chat_model(name=model_name)
except Exception as e:
logger.error("draft model creation failed: %s", e, exc_info=True)
return {
@@ -449,6 +463,8 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
# 分节写作出现模型异常时,以非致命 notice 提示用户,草稿仍照常产出
notices: list[dict] = []
if model_default_notice:
notices.append(model_default_notice)
if section_errors:
notices.append({
"type": "notice",
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
class DuckDuckGoSearchProvider(BaseSearchProvider):
"""使用 DuckDuckGo 进行互联网搜索,无需 API Key。"""
def __init__(self, region: str = "cn-zh", max_timeout: int = 30):
def __init__(self, region: str = "cn-zh", max_timeout: int = 120):
self.region = region
self.max_timeout = max_timeout
# 收集本次运行的检索异常,供 researcher 节点取出并提示用户
@@ -207,6 +207,21 @@ class AIWritingSessionRepository:
await session.commit()
return True
async def list_by_status(self, status: str, *, limit: int = 500) -> list[dict]:
"""列出指定 status 的会话(仅 id + user_id,用于后台驱动重启续跑 reconcile)。
不按 user_id 过滤——进程级 reconcile 要把所有用户「挂起中」的会话都拉起。
"""
stmt = (
select(AIWritingSessionRow.id, AIWritingSessionRow.user_id)
.where(AIWritingSessionRow.status == status)
.order_by(AIWritingSessionRow.updated_at.asc())
.limit(limit)
)
async with self._sf() as session:
result = await session.execute(stmt)
return [{"id": row[0], "user_id": row[1]} for row in result.all()]
async def list_older_than(
self,
cutoff: datetime,