feat: add AI writing chat workspace

This commit is contained in:
董宏凯 committed 2026-07-02 13:56:46 +08:00
1 parent 54190d7a83
commit 845c87e3ee
13 files changed
+2622

No files matched your search

@@ -327,6 +327,91 @@ async def save_transcript(session_id: str, body: SaveTranscriptRequest, request:
return {"success": True}
# ── 意图解析(对话驱动写作台) ────────────────────────────────────────────────
class IntentParseRequest(BaseModel):
# 用户在底部智能输入框发的自由文本
text: str
# 前端当前 WritingStatus(仅参考;暂停点以 checkpoint 里真实 interrupt 为准)
status: Optional[str] = None
# 前端认为的当前暂停点(checkpoint 读取失败时的兜底)
pause_point: Optional[str] = None
class IntentParseResponse(BaseModel):
intent: str
pause_point: Optional[str] = None
action: Optional[str] = None
# 驼峰键(approvedMaterialIds / userQuery / outlineFeedback / userRevisionNotes /
# overrideVerdict / selectedIssueIndices / sectionDecisions),前端可直接喂 submitIntervention
payload: dict = Field(default_factory=dict)
need_research: bool = False
research_query: str = ""
answer: str = ""
confidence: float = 0.0
clarify: str = ""
@router.post("/sessions/{session_id}/intent", response_model=IntentParseResponse)
async def parse_session_intent(session_id: str, body: IntentParseRequest, request: Request):
"""对话驱动写作台:把用户自由文本翻译成结构化写作指令(或问答/澄清)。
读该会话 graph checkpoint 的素材/大纲/草稿/审核摘要 + 当前 pending interrupt
(权威暂停点),交给 ``deerflow.agents.ai_writing.intent_router`` 的 LLM 意图
路由。动作白名单 / 高代价置信度门槛 / payload 清洗都在 intent_router 内完成,
这里只做鉴权与 state 读取。模型失败不抛 5xx —— intent_router 会降级成 clarify。
"""
from app.gateway.ai_writing_job_executor import _pending_interrupt
from app.gateway.deps import get_ai_writing_session_store, get_current_user
from deerflow.agents.ai_writing.intent_router import parse_user_intent
text = (body.text or "").strip()
if not text:
raise HTTPException(status_code=400, detail="text 不能为空")
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")
# 读 checkpoint:暂停点以 pending interrupt 为权威(frontend 声明只作读取失败兜底)。
values: dict = {}
interrupt_value: Optional[dict] = None
pause_point: Optional[str] = None
checkpoint_ok = False
_app_state = getattr(getattr(request, "app", None), "state", None)
checkpointer = getattr(_app_state, "checkpointer", None)
if checkpointer is not None:
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": session_id}})
values = getattr(snapshot, "values", None) or {}
interrupt_value = _pending_interrupt(snapshot)
checkpoint_ok = bool(values)
if interrupt_value:
pause_point = interrupt_value.get("pause_point")
except Exception:
logger.warning("ai_writing intent: read checkpoint failed for %s", session_id, exc_info=True)
if pause_point is None and not checkpoint_ok:
pause_point = (body.pause_point or "").strip() or None
result = await parse_user_intent(
text=text,
pause_point=pause_point,
values=values,
interrupt_value=interrupt_value,
model_name=str(values.get("model_name") or "") or None,
)
return IntentParseResponse(**result)
class BackgroundResponse(BaseModel):
# 是否新拉起了后台任务(False = 已在后台跑,幂等返回)
started: bool