641 lines
25 KiB
Python
641 lines
25 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 = ""
|
||
|
||
|
||
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
|
||
|
||
|
||
class RenameSessionRequest(BaseModel):
|
||
title: str
|
||
|
||
|
||
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] = {}
|
||
|
||
|
||
# ── 工具函数 ──────────────────────────────────────────────────────────────────
|
||
|
||
def _sse(event: str, data: dict) -> str:
|
||
return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False)}\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 "")
|
||
|
||
|
||
# ── 路由 ──────────────────────────────────────────────────────────────────────
|
||
|
||
@router.post("/start", response_model=StartWritingResponse)
|
||
async def start_writing(body: StartWritingRequest, request: Request):
|
||
"""启动写作流程,返回 session_id。"""
|
||
from deerflow.agents.ai_writing.graph import AI_WRITING_GRAPH
|
||
from app.gateway.deps import get_ai_writing_session_store, get_article_type_store, get_current_user
|
||
|
||
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,
|
||
"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)
|
||
|
||
asyncio.create_task(_stream_graph_segment(session_id, initial_state, config, AI_WRITING_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):
|
||
"""保存会话的完整对话时间线,供历史会话回显出与实时对话一致的富时间线。"""
|
||
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}
|
||
user_id = await get_current_user(request)
|
||
updated = await repo.update(session_id, user_id=user_id, transcript=body.transcript)
|
||
if updated is None:
|
||
raise HTTPException(status_code=404, detail="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 AI_WRITING_GRAPH
|
||
from app.gateway.deps import get_current_user
|
||
|
||
if session_id not in _event_queues:
|
||
raise HTTPException(status_code=404, detail="Session not found")
|
||
|
||
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,
|
||
}
|
||
|
||
# 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)
|
||
|
||
asyncio.create_task(
|
||
_stream_graph_segment(session_id, Command(resume=intervention), config, AI_WRITING_GRAPH)
|
||
)
|
||
return {"message": "已提交,流程恢复中"}
|
||
|
||
|
||
@router.get("/{session_id}/state")
|
||
async def get_state(session_id: str):
|
||
"""查询当前流程状态快照。"""
|
||
from deerflow.agents.ai_writing.graph import AI_WRITING_GRAPH
|
||
|
||
config = _make_config(session_id)
|
||
snapshot = await AI_WRITING_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 _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 _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:
|
||
yield _sse("error", {"message": "Session not found"})
|
||
return
|
||
|
||
queue = _event_queues[session_id]
|
||
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)
|