1007 lines
42 KiB
Python
1007 lines
42 KiB
Python
"""AI 写作路由 —— 独立于原有路由,前缀 /api/ai-writing/
|
||
|
||
POST /api/ai-writing/start 启动写作流程,返回 session_id
|
||
GET /api/ai-writing/{id}/stream SSE 流(进度/干预/草稿事件)
|
||
POST /api/ai-writing/{id}/resume 用户干预响应,恢复流程
|
||
GET /api/ai-writing/{id}/state 查询当前流程状态
|
||
GET /api/ai-writing/sessions 列出用户的会话历史
|
||
GET /api/ai-writing/sessions/{id} 获取单个历史会话
|
||
PATCH /api/ai-writing/sessions/{id} 重命名会话标题
|
||
DELETE /api/ai-writing/sessions/{id} 删除会话
|
||
GET /api/ai-writing/article-types 列出所有文章类型
|
||
POST /api/ai-writing/article-types 新增文章类型
|
||
PATCH /api/ai-writing/article-types/{id} 更新文章类型
|
||
DELETE /api/ai-writing/article-types/{id} 删除文章类型
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import json
|
||
import logging
|
||
import uuid
|
||
from collections.abc import AsyncGenerator
|
||
from datetime import UTC, datetime
|
||
from typing import Optional
|
||
|
||
from fastapi import APIRouter, HTTPException, Request
|
||
from fastapi.responses import StreamingResponse
|
||
from langgraph.types import Command
|
||
from pydantic import BaseModel, Field
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
router = APIRouter(prefix="/api/ai-writing", tags=["ai-writing"])
|
||
|
||
# ── 请求/响应模型 ─────────────────────────────────────────────────────────────
|
||
|
||
class StartWritingRequest(BaseModel):
|
||
user_intent: str
|
||
article_type: str = "analysis"
|
||
target_audience: str = "普通读者"
|
||
word_count_target: int = 800
|
||
model_name: str = ""
|
||
# 素材检索关键词数量:用户指定用几个关键词检索(1~10,默认 6)
|
||
keyword_count: int = 6
|
||
# 写作模式:strict=严格依据素材 / loose=可结合通用知识(默认 loose)
|
||
writing_mode: str = "loose"
|
||
|
||
|
||
class StartWritingResponse(BaseModel):
|
||
session_id: str
|
||
message: str = "写作流程已启动"
|
||
|
||
|
||
class ResumeRequest(BaseModel):
|
||
action: str
|
||
pause_point: str = ""
|
||
approved_material_ids: list[str] = Field(default_factory=list)
|
||
extra_keywords: list[str] = Field(default_factory=list)
|
||
edited_outline: Optional[dict] = None
|
||
outline_feedback: str = ""
|
||
user_revision_notes: str = ""
|
||
override_verdict: bool = False
|
||
selected_issue_indices: Optional[list[int]] = None
|
||
# 素材不足求助点:每个被阻塞章节的处理决策
|
||
# [{"sectionTitle": "...", "decision": "supplement|delete|loose"}]
|
||
section_decisions: list[dict] = Field(default_factory=list)
|
||
|
||
|
||
class RenameSessionRequest(BaseModel):
|
||
title: str
|
||
|
||
|
||
class AdminCleanupRequest(BaseModel):
|
||
"""手动清理接口的可选覆盖参数。
|
||
|
||
全部留空 → 用 config.yaml 的 ai_writing.cleanup 段默认值;显式传值
|
||
→ 覆盖配置(一般用于运维想验证一个临时阈值,例如清掉「最近 1 小时
|
||
以前的」会话来模拟整轮调度)。
|
||
"""
|
||
retention_days: Optional[int] = Field(default=None, ge=0, description="保留多少天以内的会话;0 = 不保留")
|
||
only_finished: Optional[bool] = Field(default=None, description="True 时只清 done/error 状态")
|
||
batch_size: Optional[int] = Field(default=None, ge=1, le=10000, description="单批最多删多少行")
|
||
|
||
|
||
class AdminCleanupResponse(BaseModel):
|
||
deleted_db: int
|
||
deleted_checkpoints: int
|
||
# 因「仍在跑」被保护掉的会话数。永远跳过 _event_queues 里活跃的会话,
|
||
# 即使 retention_days=0 也不会秒杀用户正在写的稿。
|
||
skipped_active: int = 0
|
||
elapsed_seconds: float
|
||
params: dict
|
||
|
||
|
||
class SaveTranscriptRequest(BaseModel):
|
||
# 前端组装的完整对话时间线(progressEvents + completedInterventions)
|
||
transcript: dict
|
||
|
||
|
||
class ArticleTypeRequest(BaseModel):
|
||
key: str
|
||
label: str
|
||
description: Optional[str] = None
|
||
prompt_hint: Optional[str] = None
|
||
default_word_count: int = 800
|
||
sort_order: int = 0
|
||
|
||
|
||
class UpdateArticleTypeRequest(BaseModel):
|
||
key: Optional[str] = None
|
||
label: Optional[str] = None
|
||
description: Optional[str] = None
|
||
prompt_hint: Optional[str] = None
|
||
default_word_count: Optional[int] = None
|
||
sort_order: Optional[int] = None
|
||
|
||
|
||
class ArticleTypeResponse(BaseModel):
|
||
id: str
|
||
key: str
|
||
label: str
|
||
description: Optional[str] = None
|
||
prompt_hint: Optional[str] = None
|
||
default_word_count: int
|
||
sort_order: int
|
||
created_at: str
|
||
updated_at: str
|
||
|
||
|
||
class SessionResponse(BaseModel):
|
||
id: str
|
||
user_id: Optional[str] = None
|
||
title: str
|
||
user_intent: Optional[str] = None
|
||
status: str
|
||
draft_title: Optional[str] = None
|
||
draft_markdown: Optional[str] = None
|
||
completed_interventions: Optional[list] = None
|
||
review_result: Optional[dict] = None
|
||
transcript: Optional[dict] = None
|
||
created_at: str
|
||
updated_at: str
|
||
|
||
|
||
# ── 内部状态 ──────────────────────────────────────────────────────────────────
|
||
|
||
# session_id → asyncio.Queue
|
||
_event_queues: dict[str, asyncio.Queue] = {}
|
||
|
||
# session_id → model_name(resume 时复用)
|
||
_session_model_names: dict[str, str] = {}
|
||
|
||
# session_id → AIWritingSessionRepository(for DB saves)
|
||
_session_repos: dict[str, object] = {}
|
||
|
||
# session_id → user_id (str | None, already resolved)
|
||
_session_user_ids: dict[str, str | None] = {}
|
||
|
||
# session_id → accumulated intervention records
|
||
_session_interventions: dict[str, list] = {}
|
||
|
||
# session_id → True if session ended with error
|
||
_session_error_flags: dict[str, bool] = {}
|
||
|
||
# session_id → valid_article_types(list[dict] with key/label)
|
||
_session_article_types: dict[str, list[dict]] = {}
|
||
|
||
# session_id → pending await_user event tuple for SSE reconnect re-delivery
|
||
# asyncio.Queue is consume-once; if the old SSE generator dequeued the await_user event
|
||
# before the client disconnected, the event is lost from the queue. We save a copy here
|
||
# so that a new SSE connection (e.g., after user clicks "继续") can re-inject it.
|
||
_session_pending_pause: dict[str, tuple] = {}
|
||
|
||
# Serializes session recovery so that a concurrent /stream + /resume on the same
|
||
# lost session don't each rebuild a different queue object.
|
||
_recovery_lock = asyncio.Lock()
|
||
|
||
# 在飞的 graph 推进任务(每次 /start 和 /resume 都会塞一个进来)。
|
||
# Lifespan shutdown 时会等这些 task 跑到下一个 interrupt / 结束,再关闭
|
||
# checkpointer 连接 —— 否则中途关 connection,task 内部的 sqlite 写入会抛
|
||
# 错,导致 LLM 已经生成但还没落 checkpoint 的进度丢失。
|
||
_inflight_graph_tasks: set[asyncio.Task] = set()
|
||
|
||
|
||
# ── 工具函数 ──────────────────────────────────────────────────────────────────
|
||
|
||
def _sse(event: str, data: dict) -> str:
|
||
"""组装一帧 SSE。
|
||
|
||
JSON 序列化对 progress_events 来说理论上不会失败(节点里塞的都是 str/dict
|
||
/list/int 这类基础类型),但一旦塞进了 datetime / bytes / 自定义对象,
|
||
``json.dumps`` 会抛 TypeError → SSE 生成器爆掉 → 客户端再也收不到任何
|
||
事件,包括关键的 await_user 和 done。
|
||
|
||
这里用 ``default=str`` 兜底任何「带 __str__」的对象;如果连 ``str()``
|
||
都炸了,最后还兜一次手写一条 error 帧推出去 —— 写作流程「能继续」比
|
||
「漂亮地序列化」更重要。
|
||
"""
|
||
try:
|
||
payload = json.dumps(data, ensure_ascii=False, default=str)
|
||
except Exception as exc:
|
||
logger.exception("ai_writing: SSE payload 序列化失败 event=%s", event)
|
||
payload = json.dumps(
|
||
{
|
||
"session_id": data.get("session_id") if isinstance(data, dict) else None,
|
||
"message": f"事件序列化失败(已降级): {type(exc).__name__}",
|
||
},
|
||
ensure_ascii=False,
|
||
)
|
||
event = "notice" # 不要硬塞回原 event,万一是 error 会污染语义
|
||
return f"event: {event}\ndata: {payload}\n\n"
|
||
|
||
|
||
def _ai_writing_config() -> dict:
|
||
"""读取 config.yaml 的 ai_writing 段(含素材检索 provider 配置)。"""
|
||
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:
|
||
return {}
|
||
|
||
|
||
def _make_config(session_id: str, model_name: str = "", valid_article_types: list[dict] | None = None) -> dict:
|
||
return {
|
||
"configurable": {
|
||
"thread_id": session_id,
|
||
"model_name": model_name or _session_model_names.get(session_id, ""),
|
||
"ai_writing_config": _ai_writing_config(),
|
||
# 把 SSE 队列注入,供草稿/大纲节点直接推流式 chunk
|
||
"sse_queue": _event_queues.get(session_id),
|
||
"sse_session_id": session_id,
|
||
# 动态文章类型列表,intent_parser 从此处读取有效枚举
|
||
"valid_article_types": valid_article_types or _session_article_types.get(session_id),
|
||
}
|
||
}
|
||
|
||
|
||
def _truncate_title(text: str, max_len: int = 50) -> str:
|
||
text = text.strip()
|
||
return text[:max_len] + ("…" if len(text) > max_len else "")
|
||
|
||
|
||
def _spawn_graph_task(coro) -> asyncio.Task:
|
||
"""启一个 graph 推进 task 并登记到 _inflight_graph_tasks。
|
||
|
||
用 ``add_done_callback`` 在完成时自动反登记 —— 不管 task 正常结束、抛
|
||
异常、还是被 cancel,都会清理;这样 _inflight_graph_tasks 不会泄漏。
|
||
"""
|
||
task = asyncio.create_task(coro)
|
||
_inflight_graph_tasks.add(task)
|
||
task.add_done_callback(_inflight_graph_tasks.discard)
|
||
return task
|
||
|
||
|
||
async def wait_for_inflight_graph_tasks(timeout: float) -> tuple[int, int]:
|
||
"""Lifespan shutdown 入口:阻塞等待所有 in-flight graph task 收尾。
|
||
|
||
每个 ``_stream_graph_segment`` 跑到下一个 ``interrupt()`` 或 ``END`` 就
|
||
会自然返回;通常只需要等几秒到几十秒。超时后剩下的会被 ``cancel()``,
|
||
但 cancel 完仍要等它们 ack 退出(带极短的 grace),否则下一步关
|
||
checkpointer 时还会撞上活的协程。
|
||
|
||
Returns:
|
||
(drained, cancelled): 自然结束的数量、被 cancel 强制结束的数量
|
||
"""
|
||
if not _inflight_graph_tasks:
|
||
return 0, 0
|
||
|
||
pending = set(_inflight_graph_tasks)
|
||
initial = len(pending)
|
||
logger.info("ai_writing: 等待 %d 个 in-flight graph task 收尾(timeout=%.1fs)", initial, timeout)
|
||
|
||
done, still_running = await asyncio.wait(pending, timeout=timeout)
|
||
drained = len(done)
|
||
|
||
cancelled = 0
|
||
if still_running:
|
||
logger.warning(
|
||
"ai_writing: 仍有 %d 个 task 超时未结束,强制 cancel", len(still_running)
|
||
)
|
||
for t in still_running:
|
||
t.cancel()
|
||
cancelled += 1
|
||
# 给 cancel 一点 ack 时间,避免下游关连接时撞活协程
|
||
await asyncio.wait(still_running, timeout=2.0)
|
||
|
||
logger.info("ai_writing: in-flight 收尾完成 drained=%d cancelled=%d", drained, cancelled)
|
||
return drained, cancelled
|
||
|
||
|
||
# ── 路由 ──────────────────────────────────────────────────────────────────────
|
||
|
||
@router.post("/start", response_model=StartWritingResponse)
|
||
async def start_writing(body: StartWritingRequest, request: Request):
|
||
"""启动写作流程,返回 session_id。
|
||
|
||
**失败兜底契约**:本接口除非 Pydantic 校验 body 失败(422),其他任何
|
||
后端组件(DB / checkpointer / 文章类型仓库 / 用户解析)失败都不会导致
|
||
/start 返回 5xx。哪怕兜底走的是「极简内存模式」也要先把 session_id
|
||
交给前端,让它能起 SSE 接到 error 事件,避免 UI 卡死在 loading 状态。
|
||
"""
|
||
from deerflow.agents.ai_writing.graph import get_ai_writing_graph
|
||
from app.gateway.deps import get_ai_writing_session_store, get_article_type_store, get_current_user
|
||
|
||
# get_ai_writing_graph() 内部已对 sqlite/postgres 失败做了 InMemorySaver
|
||
# 降级,理论上永不抛。但 build_ai_writing_graph 拼图结构也可能因下游
|
||
# 节点 import 失败抛错 —— 这种情况 graph 完全不可用,必须早 fail,前端
|
||
# 弹错处理。
|
||
try:
|
||
graph = await get_ai_writing_graph()
|
||
except Exception as e:
|
||
logger.exception("ai_writing: graph 构建彻底失败,无法启动")
|
||
raise HTTPException(
|
||
status_code=503,
|
||
detail=f"AI 写作服务暂不可用: {type(e).__name__}",
|
||
) from e
|
||
|
||
session_id = str(uuid.uuid4())
|
||
queue: asyncio.Queue = asyncio.Queue()
|
||
_event_queues[session_id] = queue
|
||
_session_model_names[session_id] = body.model_name
|
||
_session_interventions[session_id] = []
|
||
|
||
# 加载动态文章类型列表,注入给 intent_parser 节点
|
||
article_type_store = get_article_type_store(request)
|
||
valid_article_types: list[dict] | None = None
|
||
if article_type_store is not None:
|
||
try:
|
||
records = await article_type_store.list()
|
||
if records:
|
||
valid_article_types = [{"key": r["key"], "label": r["label"]} for r in records]
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to load article types for session=%s", session_id)
|
||
_session_article_types[session_id] = valid_article_types or []
|
||
|
||
# 持久化会话记录(DB 可能未配置,则跳过)
|
||
repo = get_ai_writing_session_store(request)
|
||
user_id = await get_current_user(request)
|
||
if repo is not None:
|
||
try:
|
||
title = _truncate_title(body.user_intent)
|
||
await repo.create(
|
||
id=session_id,
|
||
title=title,
|
||
user_intent=body.user_intent,
|
||
user_id=user_id,
|
||
)
|
||
_session_repos[session_id] = repo
|
||
_session_user_ids[session_id] = user_id
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to create session record: session=%s", session_id)
|
||
|
||
initial_state = {
|
||
"user_intent": body.user_intent,
|
||
"article_type": body.article_type,
|
||
"target_audience": body.target_audience,
|
||
"word_count_target": body.word_count_target,
|
||
# 规整到 [1, 10],非法值回退默认 6
|
||
"keyword_count": max(1, min(10, body.keyword_count)),
|
||
# 写作模式,非法值回退 loose
|
||
"writing_mode": body.writing_mode if body.writing_mode in ("strict", "loose") else "loose",
|
||
"revision_count": 0,
|
||
"max_revisions": 3,
|
||
"status": "researching",
|
||
"drafts": [],
|
||
"progress_events": [],
|
||
"material_package": None,
|
||
"current_outline": None,
|
||
"current_draft": None,
|
||
"review_result": None,
|
||
"last_user_intervention": None,
|
||
}
|
||
config = _make_config(session_id, body.model_name, valid_article_types=valid_article_types)
|
||
|
||
_spawn_graph_task(_stream_graph_segment(session_id, initial_state, config, graph))
|
||
return StartWritingResponse(session_id=session_id)
|
||
|
||
|
||
@router.get("/sessions", response_model=list[SessionResponse])
|
||
async def list_sessions(request: Request):
|
||
"""列出当前用户的 AI 写作历史会话。"""
|
||
from app.gateway.deps import get_ai_writing_session_store, get_current_user
|
||
|
||
repo = get_ai_writing_session_store(request)
|
||
if repo is None:
|
||
return []
|
||
user_id = await get_current_user(request)
|
||
return await repo.list(user_id=user_id)
|
||
|
||
|
||
@router.get("/sessions/{session_id}", response_model=SessionResponse)
|
||
async def get_session(session_id: str, request: Request):
|
||
"""获取单个历史会话。"""
|
||
from app.gateway.deps import 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")
|
||
return record
|
||
|
||
|
||
@router.patch("/sessions/{session_id}", response_model=SessionResponse)
|
||
async def rename_session(session_id: str, body: RenameSessionRequest, request: Request):
|
||
"""重命名会话标题。"""
|
||
from app.gateway.deps import 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)
|
||
updated = await repo.update(session_id, user_id=user_id, title=body.title.strip())
|
||
if updated is None:
|
||
raise HTTPException(status_code=404, detail="Session not found")
|
||
return updated
|
||
|
||
|
||
@router.delete("/sessions/{session_id}")
|
||
async def delete_session(session_id: str, request: Request):
|
||
"""删除历史会话。"""
|
||
from app.gateway.deps import 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)
|
||
deleted = await repo.delete(session_id, user_id=user_id)
|
||
if not deleted:
|
||
raise HTTPException(status_code=404, detail="Session not found")
|
||
return {"success": True}
|
||
|
||
|
||
@router.put("/sessions/{session_id}/transcript")
|
||
async def save_transcript(session_id: str, body: SaveTranscriptRequest, request: Request):
|
||
"""保存会话的完整对话时间线,供历史会话回显出与实时对话一致的富时间线。
|
||
|
||
时间线持久化属于「锦上添花」功能:即便保存失败(如内网部署缺少 transcript
|
||
列、或 MySQL TEXT 列超长),也绝不能阻断写作流程。任何异常都吞掉并返回
|
||
success=False,由前端忽略即可。
|
||
"""
|
||
from app.gateway.deps import get_ai_writing_session_store, get_current_user
|
||
|
||
repo = get_ai_writing_session_store(request)
|
||
if repo is None:
|
||
return {"success": False, "reason": "store_unavailable"}
|
||
try:
|
||
user_id = await get_current_user(request)
|
||
updated = await repo.update(session_id, user_id=user_id, transcript=body.transcript)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to save transcript: session=%s", session_id)
|
||
return {"success": False, "reason": "save_failed"}
|
||
if updated is None:
|
||
# 会话不存在或不属于当前用户:同样不抛 500,返回 success=False 即可
|
||
return {"success": False, "reason": "session_not_found"}
|
||
return {"success": True}
|
||
|
||
|
||
@router.get("/article-types", response_model=list[ArticleTypeResponse])
|
||
async def list_article_types(request: Request):
|
||
"""列出所有文章类型,按 sort_order 升序排列。"""
|
||
from app.gateway.deps import get_article_type_store
|
||
|
||
store = get_article_type_store(request)
|
||
if store is None:
|
||
return []
|
||
return await store.list()
|
||
|
||
|
||
@router.post("/article-types", response_model=ArticleTypeResponse, status_code=201)
|
||
async def create_article_type(body: ArticleTypeRequest, request: Request):
|
||
"""新增文章类型。key 必须全局唯一。"""
|
||
from app.gateway.deps import get_article_type_store
|
||
|
||
store = get_article_type_store(request)
|
||
if store is None:
|
||
raise HTTPException(status_code=503, detail="Article type store not available")
|
||
existing = await store.get_by_key(body.key)
|
||
if existing is not None:
|
||
raise HTTPException(status_code=409, detail=f"Article type key '{body.key}' already exists")
|
||
return await store.create(
|
||
key=body.key,
|
||
label=body.label,
|
||
description=body.description,
|
||
prompt_hint=body.prompt_hint,
|
||
default_word_count=body.default_word_count,
|
||
sort_order=body.sort_order,
|
||
)
|
||
|
||
|
||
@router.patch("/article-types/{article_type_id}", response_model=ArticleTypeResponse)
|
||
async def update_article_type(article_type_id: str, body: UpdateArticleTypeRequest, request: Request):
|
||
"""更新文章类型字段(只传需要修改的字段)。"""
|
||
from app.gateway.deps import get_article_type_store
|
||
|
||
store = get_article_type_store(request)
|
||
if store is None:
|
||
raise HTTPException(status_code=503, detail="Article type store not available")
|
||
# 如果修改了 key,检查唯一性
|
||
if body.key is not None:
|
||
existing = await store.get_by_key(body.key)
|
||
if existing is not None and existing["id"] != article_type_id:
|
||
raise HTTPException(status_code=409, detail=f"Article type key '{body.key}' already exists")
|
||
|
||
_UNSET = object.__new__(object)
|
||
kwargs = {}
|
||
for field in ("key", "label", "description", "prompt_hint", "default_word_count", "sort_order"):
|
||
val = getattr(body, field)
|
||
if val is not None:
|
||
kwargs[field] = val
|
||
|
||
updated = await store.update(article_type_id, **kwargs)
|
||
if updated is None:
|
||
raise HTTPException(status_code=404, detail="Article type not found")
|
||
return updated
|
||
|
||
|
||
@router.delete("/article-types/{article_type_id}")
|
||
async def delete_article_type(article_type_id: str, request: Request):
|
||
"""删除文章类型。"""
|
||
from app.gateway.deps import get_article_type_store
|
||
|
||
store = get_article_type_store(request)
|
||
if store is None:
|
||
raise HTTPException(status_code=503, detail="Article type store not available")
|
||
deleted = await store.delete(article_type_id)
|
||
if not deleted:
|
||
raise HTTPException(status_code=404, detail="Article type not found")
|
||
return {"success": True}
|
||
|
||
|
||
@router.get("/{session_id}/stream")
|
||
async def stream_progress(session_id: str):
|
||
"""SSE 流:推送进度事件、干预请求、草稿内容。"""
|
||
return StreamingResponse(
|
||
_sse_generator(session_id),
|
||
media_type="text/event-stream",
|
||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||
)
|
||
|
||
|
||
@router.post("/{session_id}/resume")
|
||
async def resume_writing(session_id: str, body: ResumeRequest, request: Request):
|
||
"""用户提交干预决策,恢复暂停的流程。"""
|
||
from deerflow.agents.ai_writing.graph import get_ai_writing_graph
|
||
from app.gateway.deps import get_current_user
|
||
|
||
graph = await get_ai_writing_graph()
|
||
|
||
if session_id not in _event_queues:
|
||
# 内存会话注册表丢失(进程内清理后重连等)。先尝试从 checkpointer 恢复,
|
||
# 让用户能在干预点继续,而不是直接 404 中断整个写作流程。
|
||
recovery = await _recover_session(session_id, request)
|
||
if recovery != "interrupted":
|
||
logger.warning(
|
||
"ai_writing: resume on unrecoverable session=%s (recovery=%s)", session_id, recovery
|
||
)
|
||
raise HTTPException(
|
||
status_code=404,
|
||
detail="会话状态已丢失或流程已结束,无法继续,请重新发起写作。",
|
||
)
|
||
|
||
config = _make_config(session_id)
|
||
intervention = {
|
||
"action": body.action,
|
||
"approvedMaterialIds": body.approved_material_ids,
|
||
"extraKeywords": body.extra_keywords,
|
||
"editedOutline": body.edited_outline,
|
||
"outlineFeedback": body.outline_feedback,
|
||
"userRevisionNotes": body.user_revision_notes,
|
||
"overrideVerdict": body.override_verdict,
|
||
"selectedIssueIndices": body.selected_issue_indices,
|
||
"sectionDecisions": body.section_decisions,
|
||
}
|
||
|
||
# Record this intervention for session history
|
||
record = {
|
||
"pausePoint": body.pause_point,
|
||
"action": body.action,
|
||
"timestamp": datetime.now(UTC).isoformat(),
|
||
}
|
||
if session_id in _session_interventions:
|
||
_session_interventions[session_id].append(record)
|
||
repo = _session_repos.get(session_id)
|
||
user_id = _session_user_ids.get(session_id)
|
||
if repo is not None:
|
||
try:
|
||
await repo.update(
|
||
session_id,
|
||
user_id=user_id,
|
||
completed_interventions=_session_interventions[session_id],
|
||
)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to save intervention: session=%s", session_id)
|
||
|
||
# Clear pending pause so SSE reconnect won't re-inject the stale await_user event
|
||
_session_pending_pause.pop(session_id, None)
|
||
|
||
_spawn_graph_task(
|
||
_stream_graph_segment(session_id, Command(resume=intervention), config, graph)
|
||
)
|
||
return {"message": "已提交,流程恢复中"}
|
||
|
||
|
||
@router.get("/{session_id}/state")
|
||
async def get_state(session_id: str):
|
||
"""查询当前流程状态快照。"""
|
||
from deerflow.agents.ai_writing.graph import get_ai_writing_graph
|
||
|
||
graph = await get_ai_writing_graph()
|
||
config = _make_config(session_id)
|
||
snapshot = await graph.aget_state(config)
|
||
if not snapshot or not snapshot.values:
|
||
raise HTTPException(status_code=404, detail="Session not found")
|
||
return snapshot.values
|
||
|
||
|
||
# ── 管理接口 ──────────────────────────────────────────────────────────────────
|
||
|
||
async def _require_admin(request: Request) -> None:
|
||
"""放行 admin / auth-disabled,其余 403。
|
||
|
||
与 scheduled_tasks._require_admin 同套语义:auth_disabled 模式下
|
||
get_optional_user_from_request 返回 None,此时放行(开发环境一致体验)。
|
||
"""
|
||
from app.gateway.deps import get_optional_user_from_request
|
||
|
||
user = await get_optional_user_from_request(request)
|
||
if user is None:
|
||
return
|
||
if getattr(user, "system_role", None) != "admin":
|
||
raise HTTPException(status_code=403, detail="AI 写作清理管理接口仅限管理员")
|
||
|
||
|
||
def _resolve_cleanup_params(override: AdminCleanupRequest) -> dict:
|
||
"""合并 config.yaml 默认值 + 请求显式覆盖。
|
||
|
||
显式传 0 / False 也算覆盖(用 ``is not None`` 判定),方便测「保留 0 天 =
|
||
全删」这种极端场景。
|
||
"""
|
||
from deerflow.config import get_app_config
|
||
|
||
raw = getattr(get_app_config(), "ai_writing", None) or {}
|
||
cfg = (raw.get("cleanup") if isinstance(raw, dict) else None) or {}
|
||
|
||
return {
|
||
"retention_days": (
|
||
override.retention_days if override.retention_days is not None
|
||
else int(cfg.get("retention_days", 7))
|
||
),
|
||
"only_finished": (
|
||
override.only_finished if override.only_finished is not None
|
||
else bool(cfg.get("delete_only_finished", False))
|
||
),
|
||
"batch_size": (
|
||
override.batch_size if override.batch_size is not None
|
||
else int(cfg.get("batch_size", 500))
|
||
),
|
||
}
|
||
|
||
|
||
@router.post("/admin/cleanup", response_model=AdminCleanupResponse)
|
||
async def admin_cleanup(
|
||
body: AdminCleanupRequest,
|
||
request: Request,
|
||
) -> AdminCleanupResponse:
|
||
"""**仅管理员** 手动触发一次会话清理(不等凌晨 3 点定时调度)。
|
||
|
||
用途:
|
||
- 验证 cleanup 配置改动是否生效
|
||
- 内网紧急清理(磁盘满 / MySQL 行数过多)
|
||
- 调试:传 ``retention_days: 0`` 一刷拉清空,方便重新跑回归
|
||
|
||
所有参数留空 → 沿用 config.yaml 的 ``ai_writing.cleanup`` 段默认。显式
|
||
传值会覆盖配置但**不修改 config.yaml**(一次性参数,下次定时调度还是用
|
||
配置文件的值)。
|
||
"""
|
||
import time
|
||
|
||
from app.gateway.ai_writing_cleanup import run_cleanup_once
|
||
|
||
await _require_admin(request)
|
||
params = _resolve_cleanup_params(body)
|
||
|
||
started = time.monotonic()
|
||
try:
|
||
result = await run_cleanup_once(**params)
|
||
except Exception as e:
|
||
logger.exception("ai_writing admin cleanup failed: params=%s", params)
|
||
# 抛 500 是合理的 —— 管理员触发清理失败需要看到失败原因,跟「业务
|
||
# 接口被失败拖累」是两回事
|
||
raise HTTPException(
|
||
status_code=500,
|
||
detail=f"清理执行失败: {type(e).__name__}: {e}",
|
||
) from e
|
||
|
||
elapsed = time.monotonic() - started
|
||
logger.info(
|
||
"ai_writing admin cleanup done: params=%s deleted_db=%d deleted_cp=%d elapsed=%.2fs",
|
||
params,
|
||
result["deleted_db"],
|
||
result["deleted_checkpoints"],
|
||
elapsed,
|
||
)
|
||
return AdminCleanupResponse(
|
||
deleted_db=result["deleted_db"],
|
||
deleted_checkpoints=result["deleted_checkpoints"],
|
||
skipped_active=result.get("skipped_active", 0),
|
||
elapsed_seconds=round(elapsed, 3),
|
||
params=params,
|
||
)
|
||
|
||
|
||
# ── 内部辅助 ──────────────────────────────────────────────────────────────────
|
||
|
||
async def _stream_graph_segment(
|
||
session_id: str,
|
||
input_or_command,
|
||
config: dict,
|
||
graph,
|
||
):
|
||
"""运行一个 Graph 片段(初始运行或 resume),将输出推入队列。
|
||
遇到 interrupt 时停止(不关闭队列),等待下次 resume。
|
||
Graph 结束时关闭队列(放入 None 哨兵)。
|
||
"""
|
||
queue = _event_queues.get(session_id)
|
||
if queue is None:
|
||
return
|
||
|
||
reached_end = False
|
||
is_error = False
|
||
try:
|
||
async for chunk in graph.astream(input_or_command, config=config, stream_mode="updates"):
|
||
node_name = next(iter(chunk))
|
||
node_output = chunk[node_name]
|
||
|
||
# interrupt() 触发
|
||
if node_name == "__interrupt__":
|
||
# node_output 是 (Interrupt(...),) 元组
|
||
interrupt_list = node_output if isinstance(node_output, (list, tuple)) else [node_output]
|
||
for intr in interrupt_list:
|
||
interrupt_data = intr.value if hasattr(intr, "value") else intr
|
||
event_tuple = ("await_user", interrupt_data)
|
||
await queue.put(event_tuple)
|
||
# Save for SSE reconnect re-delivery: if the old generator consumed this
|
||
# event before the client disconnected, a new connection would block forever.
|
||
_session_pending_pause[session_id] = event_tuple
|
||
return # 等待 resume,不关闭队列
|
||
|
||
if not isinstance(node_output, dict):
|
||
continue
|
||
|
||
# 推送 progress_events
|
||
for evt in node_output.get("progress_events") or []:
|
||
await queue.put((_event_type(evt), {**evt, "session_id": session_id}))
|
||
|
||
# 素材就绪
|
||
if pkg := node_output.get("material_package"):
|
||
await queue.put(("materials_ready", {
|
||
"session_id": session_id,
|
||
"keywords": pkg.get("keywords", []),
|
||
"materials": pkg.get("materials", []),
|
||
"summary": pkg.get("summary", ""),
|
||
}))
|
||
|
||
# 大纲就绪
|
||
if outline := node_output.get("current_outline"):
|
||
await queue.put(("outline_ready", {"session_id": session_id, "outline": outline}))
|
||
|
||
# 草稿就绪 — 保存到 DB
|
||
if draft := node_output.get("current_draft"):
|
||
draft_title = draft.get("title", "")
|
||
draft_markdown = draft.get("full_markdown", "")
|
||
await queue.put(("draft_ready", {
|
||
"session_id": session_id,
|
||
"title": draft_title,
|
||
"markdown": draft_markdown,
|
||
"revision_count": draft.get("revision_count", 0),
|
||
}))
|
||
repo = _session_repos.get(session_id)
|
||
user_id = _session_user_ids.get(session_id)
|
||
if repo is not None:
|
||
try:
|
||
await repo.update(
|
||
session_id,
|
||
user_id=user_id,
|
||
status="draft_ready",
|
||
title=draft_title or _truncate_title(
|
||
next(iter(_session_interventions.get(session_id, [{}])), {}).get("action", "草稿"),
|
||
),
|
||
draft_title=draft_title,
|
||
draft_markdown=draft_markdown,
|
||
)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to save draft: session=%s", session_id)
|
||
|
||
# 编辑审核就绪 — 保存到 DB
|
||
if review := node_output.get("review_result"):
|
||
await queue.put(("review_ready", {"session_id": session_id, "review_result": review}))
|
||
repo = _session_repos.get(session_id)
|
||
user_id = _session_user_ids.get(session_id)
|
||
if repo is not None:
|
||
try:
|
||
await repo.update(
|
||
session_id,
|
||
user_id=user_id,
|
||
status="review_ready",
|
||
review_result=review,
|
||
)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to save review: session=%s", session_id)
|
||
|
||
# 状态 done:Graph 结束
|
||
if node_output.get("status") == "done":
|
||
reached_end = True
|
||
|
||
# astream 正常结束 = Graph 执行到 END
|
||
reached_end = True
|
||
await queue.put(("done", {"session_id": session_id, "message": "写作完成"}))
|
||
|
||
except Exception as e:
|
||
logger.exception("ai_writing graph segment failed: session=%s", session_id)
|
||
await queue.put(("error", {"session_id": session_id, "message": str(e)}))
|
||
is_error = True
|
||
reached_end = True
|
||
finally:
|
||
if reached_end:
|
||
# 更新最终状态到 DB
|
||
repo = _session_repos.get(session_id)
|
||
user_id = _session_user_ids.get(session_id)
|
||
if repo is not None:
|
||
try:
|
||
await repo.update(
|
||
session_id,
|
||
user_id=user_id,
|
||
status="error" if is_error else "done",
|
||
)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to update final status: session=%s", session_id)
|
||
|
||
await queue.put(None) # 哨兵,通知 SSE 生成器关闭
|
||
_event_queues.pop(session_id, None)
|
||
_session_model_names.pop(session_id, None)
|
||
_session_repos.pop(session_id, None)
|
||
_session_user_ids.pop(session_id, None)
|
||
_session_interventions.pop(session_id, None)
|
||
_session_error_flags.pop(session_id, None)
|
||
_session_article_types.pop(session_id, None)
|
||
_session_pending_pause.pop(session_id, None)
|
||
|
||
|
||
def _event_type(evt: dict) -> str:
|
||
t = evt.get("type", "progress")
|
||
# 过滤掉已在上方单独推送的事件类型,避免重复
|
||
if t in ("materials_ready", "outline_ready", "draft_ready", "review_ready", "await_user",
|
||
"draft_chunk", "outline_chunk"):
|
||
return "step_done"
|
||
return t
|
||
|
||
|
||
async def _load_graph_snapshot(session_id: str):
|
||
"""读取 AI 写作 Graph 在该 session 上的状态快照(失败/无状态返回 None)。
|
||
|
||
Graph 的 checkpointer 现在按 config.yaml 持久化到磁盘(默认 SQLite),
|
||
所以容器重启后只要数据卷还在,仍然能恢复出来。
|
||
"""
|
||
try:
|
||
from deerflow.agents.ai_writing.graph import get_ai_writing_graph
|
||
|
||
graph = await get_ai_writing_graph()
|
||
snapshot = await graph.aget_state(_make_config(session_id))
|
||
except Exception:
|
||
logger.exception("ai_writing: aget_state failed during recovery: session=%s", session_id)
|
||
return None
|
||
if not snapshot or not getattr(snapshot, "values", None):
|
||
return None
|
||
return snapshot
|
||
|
||
|
||
def _extract_interrupt(snapshot) -> dict | None:
|
||
"""从快照里取出当前挂起的 interrupt 负载(即 await_user 的数据)。"""
|
||
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 None
|
||
|
||
|
||
async def _recover_session(session_id: str, request: Request | None = None) -> str:
|
||
"""内存会话注册表丢失时,尝试从 checkpointer 重建会话。
|
||
|
||
返回:
|
||
- "interrupted" —— 图正停在某个干预点,已重建队列并预置 await_user 事件,可继续 resume
|
||
- "finished" —— 图已完成,已重建队列并预置 done 事件(+草稿)
|
||
- "unrecoverable" —— 无可恢复状态(进程/容器重启过,或 session 从未存在)
|
||
|
||
幂等:内存里已有队列时直接当作 "interrupted" 返回。整段在 `_recovery_lock`
|
||
下执行,避免并发的 /stream 与 /resume 各自重建出不同的队列对象。
|
||
"""
|
||
async with _recovery_lock:
|
||
if session_id in _event_queues:
|
||
return "interrupted"
|
||
|
||
snapshot = await _load_graph_snapshot(session_id)
|
||
if snapshot is None:
|
||
return "unrecoverable"
|
||
|
||
queue: asyncio.Queue = asyncio.Queue()
|
||
_event_queues[session_id] = queue
|
||
_session_interventions.setdefault(session_id, [])
|
||
_session_model_names.setdefault(session_id, "")
|
||
_session_article_types.setdefault(session_id, [])
|
||
|
||
# 尽量恢复 DB 仓库引用,使后续 resume 仍能持久化草稿/审核结果
|
||
if request is not None and session_id not in _session_repos:
|
||
try:
|
||
from app.gateway.deps import get_ai_writing_session_store, get_current_user
|
||
|
||
repo = get_ai_writing_session_store(request)
|
||
if repo is not None:
|
||
_session_repos[session_id] = repo
|
||
_session_user_ids[session_id] = await get_current_user(request)
|
||
except Exception:
|
||
logger.exception("ai_writing: failed to restore repo during recovery: session=%s", session_id)
|
||
|
||
interrupt_val = _extract_interrupt(snapshot)
|
||
if interrupt_val is not None:
|
||
event = ("await_user", {**interrupt_val, "session_id": session_id})
|
||
await queue.put(event)
|
||
_session_pending_pause[session_id] = event
|
||
logger.info("ai_writing: recovered interrupted session from checkpointer: session=%s", session_id)
|
||
return "interrupted"
|
||
|
||
values = snapshot.values or {}
|
||
if not getattr(snapshot, "next", None) or values.get("status") == "done":
|
||
draft = values.get("current_draft") or {}
|
||
if draft.get("full_markdown"):
|
||
await queue.put(("draft_ready", {
|
||
"session_id": session_id,
|
||
"title": draft.get("title", ""),
|
||
"markdown": draft.get("full_markdown", ""),
|
||
"revision_count": draft.get("revision_count", 0),
|
||
}))
|
||
await queue.put(("done", {"session_id": session_id, "message": "写作已完成"}))
|
||
await queue.put(None) # 哨兵:让 SSE 生成器读完即关闭
|
||
logger.info("ai_writing: recovered finished session from checkpointer: session=%s", session_id)
|
||
return "finished"
|
||
|
||
# 既不在干预点、也未结束:上次运行多半被中断在某个节点,无法续推
|
||
await queue.put(("error", {
|
||
"session_id": session_id,
|
||
"message": "写作流程已中断,请重新发起写作。",
|
||
}))
|
||
await queue.put(None)
|
||
logger.warning("ai_writing: session checkpoint stuck mid-flow, not resumable: session=%s", session_id)
|
||
return "finished"
|
||
|
||
|
||
async def _sse_generator(session_id: str) -> AsyncGenerator[str, None]:
|
||
"""从队列消费事件,推送为 SSE 帧。"""
|
||
# 等待队列创建(最多 5 秒)
|
||
for _ in range(50):
|
||
if session_id in _event_queues:
|
||
break
|
||
await asyncio.sleep(0.1)
|
||
else:
|
||
# 内存里没有这个会话队列。不要直接报「Session not found」——
|
||
# 先尝试从 checkpointer 恢复(图可能仍停在某个干预点 / 已完成)。
|
||
recovery = await _recover_session(session_id)
|
||
if recovery == "unrecoverable":
|
||
yield _sse("connected", {"session_id": session_id})
|
||
yield _sse("error", {
|
||
"session_id": session_id,
|
||
"message": "会话状态已丢失(服务可能已重启),请重新发起写作。",
|
||
})
|
||
return
|
||
# 恢复成功:_event_queues[session_id] 已重建并预置事件,继续走正常消费逻辑。
|
||
|
||
queue = _event_queues.get(session_id)
|
||
if queue is None:
|
||
# 极端兜底:恢复后又被并发清理
|
||
yield _sse("connected", {"session_id": session_id})
|
||
yield _sse("error", {"session_id": session_id, "message": "会话状态已丢失,请重新发起写作。"})
|
||
return
|
||
yield _sse("connected", {"session_id": session_id})
|
||
|
||
# Re-inject the saved await_user event if the queue is empty and a pending pause exists.
|
||
# This covers the case where the old SSE generator dequeued the await_user event and
|
||
# yielded it to a disconnecting client — the event is then lost from the consume-once queue.
|
||
if queue.empty() and session_id in _session_pending_pause:
|
||
await queue.put(_session_pending_pause[session_id])
|
||
|
||
while True:
|
||
item = await queue.get()
|
||
if item is None:
|
||
break
|
||
event_type, data = item
|
||
yield _sse(event_type, data)
|
||
await asyncio.sleep(0)
|