feat(roundtable): taskId 会商记录独立存储(不分权) + 第三步生成过程对话/步骤条入库
taskId 深链会商存储独立化: - 新增独立表 roundtable_task_drafts + 路由 /api/roundtable-task-drafts,只按 task_id 归属、完全不按 user 分权(created_by 仅审计),与个人 roundtable_drafts 物理分离; 个人草稿表恢复严格按用户隔离(去掉原 task 放权)。 - 后台研讨作业按 taskId 共享:roundtable_jobs 加 task_id,get/get_by_draft/stream/ resume/cancel 支持 ?task_id= 跨用户读取与控制,作业报告写回 task-draft 存储。 - 前端:新增 api/task-drafts.ts 客户端,useDraftPersistence / useRoundtableDrafts / roundtable-jobs / DispatchChainJobModal / RoundtablePlanningPage 按 taskId 路由读写。 - 迁移 20260618_01 + column_additions_mysql.sql(jobs.task_id) + 数据搬迁脚本 scripts/migrate_roundtable_task_drafts.py(把历史 task_id 行迁出个人表)。 第三步聊天记录像第二步一样存全: - 总结报告 / 经典看板 / 大屏 三条流的生成过程对话(中间文字 + 步骤条 + stub)落库, 加载草稿整段回显,而非只剩最终 md / 流程图 / HTML。DraftStep3Snapshot 扩展 dialogues/seatStubMessages 槽位;存稿走页面 render 期 ref,恢复走各 hook hydrateSaved (旧草稿无字段则回退单条「历史记录」气泡)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
500a562bb6
commit
a8da6c2632
29 files changed
+1191
-263
No files matched your search
@@ -63,6 +63,7 @@ from app.gateway.routers import (
|
||||
roundtable_draft_shares,
|
||||
roundtable_drafts,
|
||||
roundtable_jobs,
|
||||
roundtable_task_drafts,
|
||||
runs,
|
||||
scheduled_tasks,
|
||||
sensitive_words,
|
||||
@@ -1334,6 +1335,9 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# Roundtable-planning drafts + recommendation history at /api/roundtable-drafts
|
||||
app.include_router(roundtable_drafts.router)
|
||||
|
||||
# Task-scoped roundtable drafts (taskId 深链聊天记录,独立存、不分权) at /api/roundtable-task-drafts
|
||||
app.include_router(roundtable_task_drafts.router)
|
||||
|
||||
# Step3 流程图结构化抽取 + 校验 (SSE) at /api/flow-extract
|
||||
app.include_router(flow_extract.router)
|
||||
|
||||
|
||||
@@ -223,6 +223,11 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.roundtable_draft_store = make_roundtable_draft_store(sf)
|
||||
|
||||
# Task-scoped roundtable drafts (taskId 深链聊天记录,独立存、不按 user 分权)。
|
||||
from deerflow.persistence.roundtable_task_drafts import make_roundtable_task_draft_store
|
||||
|
||||
app.state.roundtable_task_draft_store = make_roundtable_task_draft_store(sf)
|
||||
|
||||
from deerflow.persistence.roundtable_draft_shares import make_roundtable_draft_share_store
|
||||
|
||||
app.state.roundtable_draft_share_store = make_roundtable_draft_share_store(sf)
|
||||
@@ -302,6 +307,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
app.state.roundtable_job_store,
|
||||
gateway_factory=gateway_factory,
|
||||
draft_store=getattr(app.state, "roundtable_draft_store", None),
|
||||
# task 作业把最终报告写回**独立的** task-draft 存储(不分权)。
|
||||
task_draft_store=getattr(app.state, "roundtable_task_draft_store", None),
|
||||
)
|
||||
else:
|
||||
app.state.roundtable_job_executor = None
|
||||
|
||||
@@ -168,6 +168,9 @@ class StartParams:
|
||||
coordinator_name: str = "roundtable-coordinator"
|
||||
# 关联草稿:作业完成后把 step3 报告写回该草稿,使「查看报告」可用。
|
||||
draft_id: str | None = None
|
||||
# 外部任务 id(taskId 深链会商)。非空 = task 作业:draft_id 指向独立的
|
||||
# roundtable_task_drafts 表,研讨/报告写回 task-draft 存储(不分权)。
|
||||
task_id: str | None = None
|
||||
# 是否为「续跑」(resume 澄清)。首启=False → 真网关建全新线程(不复用前端线程,
|
||||
# 避免与前端本地研讨的活跃 run 撞 409);续跑=True → 复用作业自己的线程。
|
||||
is_resume: bool = False
|
||||
@@ -192,13 +195,19 @@ GatewayFactory = Callable[[StartParams], object]
|
||||
|
||||
|
||||
class RoundtableJobExecutor:
|
||||
def __init__(self, store, *, gateway_factory: GatewayFactory, draft_store=None) -> None:
|
||||
def __init__(self, store, *, gateway_factory: GatewayFactory, draft_store=None, task_draft_store=None) -> None:
|
||||
self._store = store
|
||||
self._gateway_factory = gateway_factory
|
||||
# 草稿仓储(可选):作业完成后把 step3 写回草稿,供前端「查看报告」。
|
||||
self._draft_store = draft_store
|
||||
# task 草稿仓储(独立、不分权):task 作业(params.task_id 非空)写回这里而非个人草稿。
|
||||
self._task_draft_store = task_draft_store
|
||||
self._tasks: dict[str, asyncio.Task] = {}
|
||||
|
||||
def _draft_store_for(self, params: StartParams):
|
||||
"""task 作业 → 独立 task-draft 存储;普通作业 → 个人草稿存储。"""
|
||||
return self._task_draft_store if params.task_id else self._draft_store
|
||||
|
||||
def start_job(self, params: StartParams) -> bool:
|
||||
"""启动一个后台编排任务。同一 job 已在跑则忽略(幂等)。返回是否新启动。"""
|
||||
if params.job_id in self._tasks and not self._tasks[params.job_id].done():
|
||||
@@ -238,7 +247,7 @@ class RoundtableJobExecutor:
|
||||
self._store,
|
||||
job_id=params.job_id,
|
||||
agents=params.agents,
|
||||
on_dialogue=_on_dialogue if (self._draft_store and params.draft_id) else None,
|
||||
on_dialogue=_on_dialogue if (self._draft_store_for(params) and params.draft_id) else None,
|
||||
)
|
||||
result = await run_orchestration(
|
||||
gateway=gateway,
|
||||
@@ -294,7 +303,8 @@ class RoundtableJobExecutor:
|
||||
读现有 step2 → 按 run_id=``job-<jobId>`` upsert(保留其它手动 run)→ 设为 activeRun
|
||||
→ update_draft。前端加载草稿即可像手动跑一样 hydrate 出对话。
|
||||
"""
|
||||
if self._draft_store is None or not params.draft_id:
|
||||
draft_store = self._draft_store_for(params)
|
||||
if draft_store is None or not params.draft_id:
|
||||
return
|
||||
idx_by_agent = {a.agent_id: i for i, a in enumerate(params.agents)}
|
||||
s2_dialogues = [_to_step2_dialogue(d, idx_by_agent) for d in dialogues]
|
||||
@@ -303,7 +313,7 @@ class RoundtableJobExecutor:
|
||||
)
|
||||
run_id = f"job-{params.job_id}"
|
||||
try:
|
||||
draft = await self._draft_store.get_draft(params.draft_id, params.user_id)
|
||||
draft = await draft_store.get_draft(params.draft_id, params.user_id)
|
||||
except Exception: # noqa: BLE001
|
||||
draft = None
|
||||
step2 = dict((draft or {}).get("step2") or {})
|
||||
@@ -352,7 +362,7 @@ class RoundtableJobExecutor:
|
||||
if has_report:
|
||||
update_kwargs["step3"] = effective_step3
|
||||
try:
|
||||
await self._draft_store.update_draft(params.draft_id, params.user_id, **update_kwargs)
|
||||
await draft_store.update_draft(params.draft_id, params.user_id, **update_kwargs)
|
||||
except Exception: # noqa: BLE001 — 写草稿失败不影响作业本身
|
||||
logger.warning("write run to draft %s failed", params.draft_id, exc_info=True)
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ A draft is one full roundtable session (Step 1 intent + Step 2 multi-agent
|
||||
roundtable). Bound to the requesting user (not the browser), so it follows the
|
||||
account across devices/sessions — replacing the old ``localStorage`` storage.
|
||||
|
||||
This store is **strictly per-user**. taskId 深链聊天记录 (无界嵌入抽屉) live in a
|
||||
separate, unpartitioned store — see ``/api/roundtable-task-drafts``
|
||||
(``roundtable_task_drafts`` table) — so the two never mix.
|
||||
|
||||
Recommendation history is bound to a draft: the recommend dialog shows "last
|
||||
recommendation for this session" on open, while still letting the user
|
||||
re-analyze (which appends a new history row).
|
||||
@@ -181,45 +185,6 @@ async def create_draft(request: Request, body: DraftCreateRequest) -> DraftRespo
|
||||
return DraftResponse(**row)
|
||||
|
||||
|
||||
@router.get("/by-task/{task_id}/latest", response_model=DraftResponse)
|
||||
async def get_latest_draft_by_task(request: Request, task_id: str) -> DraftResponse:
|
||||
"""按外部任务 id 取最新一条草稿(**不按 user 分权**);无则 404。
|
||||
|
||||
无界嵌入抽屉打开时调用:若该任务已有聊天记录,回显最新一条。
|
||||
"""
|
||||
store = _get_store(request)
|
||||
row = await store.get_latest_draft_by_task(task_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="No draft for this task")
|
||||
return DraftResponse(**row)
|
||||
|
||||
|
||||
@router.get("/by-task/{task_id}", response_model=DraftListResponse)
|
||||
async def list_drafts_by_task(request: Request, task_id: str) -> DraftListResponse:
|
||||
"""按外部任务 id 列出该任务全部草稿(轻量 meta,newest first,**不按 user 分权**)。
|
||||
|
||||
taskId 深链模式下历史下拉用:一个 taskId 可有多条会商记录;同 `list_drafts`
|
||||
一样用最近后台作业状态富化(job store 缺失/失败时静默退化为无状态)。
|
||||
"""
|
||||
store = _get_store(request)
|
||||
rows = await store.list_drafts_by_task(task_id)
|
||||
job_store = getattr(request.app.state, "roundtable_job_store", None)
|
||||
if job_store is not None and rows:
|
||||
try:
|
||||
status_map = await job_store.status_map_by_drafts([r.get("id") for r in rows])
|
||||
except AttributeError:
|
||||
status_map = {}
|
||||
except Exception: # noqa: BLE001 — 富化失败不能拖垮列表
|
||||
logger.warning("roundtable draft by-task list: job status enrichment failed", exc_info=True)
|
||||
status_map = {}
|
||||
for r in rows:
|
||||
info = status_map.get(r.get("id"))
|
||||
if info:
|
||||
r["status"] = info.get("status")
|
||||
r["job_id"] = info.get("jobId")
|
||||
return DraftListResponse(drafts=[DraftMetaResponse(**r) for r in rows])
|
||||
|
||||
|
||||
@router.get("/{draft_id}", response_model=DraftResponse)
|
||||
async def get_draft(request: Request, draft_id: str) -> DraftResponse:
|
||||
store = _get_store(request)
|
||||
|
||||
@@ -55,6 +55,9 @@ class JobAgent(BaseModel):
|
||||
|
||||
class StartJobRequest(BaseModel):
|
||||
draftId: str | None = Field(default=None, max_length=64)
|
||||
# 外部任务 id(taskId 深链会商)。非空 = task 作业:按 task_id 共享、不按 user 分权,
|
||||
# 其 draft_id 指向独立的 roundtable_task_drafts 表(最终报告写回那里)。
|
||||
taskId: str | None = Field(default=None, max_length=128)
|
||||
intent: str = ""
|
||||
agents: list[JobAgent] = Field(default_factory=list)
|
||||
model: str | None = None
|
||||
@@ -191,8 +194,10 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
|
||||
# 时直接返回它、不再新建。前端的「手动挂起按钮 / 离开页面自动挂起 / beforeunload 刷新
|
||||
# 兜底」可能对同一研讨多次触发本端点 —— 客户端去重只能挡住同页内的重复,跨触发源 /
|
||||
# 跨标签页的并发只能靠这里兜住,否则同一研讨会被双跑。
|
||||
task_id = (body.taskId or "").strip() or None
|
||||
if body.draftId:
|
||||
existing = await store.get_by_draft(body.draftId, user_id=user_id)
|
||||
# task 作业按 task_id 去重(跨用户共享);个人作业按 user 去重。
|
||||
existing = await store.get_by_draft(body.draftId, user_id=user_id, task_id=task_id)
|
||||
if existing is not None and (existing.get("status") or "") not in _TERMINAL:
|
||||
return _to_response(existing)
|
||||
|
||||
@@ -202,6 +207,7 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
|
||||
id=job_id,
|
||||
draft_id=body.draftId,
|
||||
user_id=user_id,
|
||||
task_id=task_id,
|
||||
orchestration_mode=body.orchestrationMode,
|
||||
coordinator_name=body.coordinatorName,
|
||||
thread_ids=body.threadIds,
|
||||
@@ -223,6 +229,7 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
|
||||
thread_ids=body.threadIds,
|
||||
coordinator_name=body.coordinatorName,
|
||||
draft_id=body.draftId,
|
||||
task_id=task_id,
|
||||
chain=body.chain,
|
||||
orchestration_plan=body.orchestrationPlan,
|
||||
seat_thinking_enabled=body.thinking_enabled,
|
||||
@@ -234,26 +241,30 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
|
||||
|
||||
|
||||
@router.get("", response_model=RoundtableJobResponse | None)
|
||||
async def get_job_by_draft(request: Request, draft_id: str) -> RoundtableJobResponse | None:
|
||||
async def get_job_by_draft(request: Request, draft_id: str, task_id: str | None = None) -> RoundtableJobResponse | None:
|
||||
# task_id 非空 = task 作业查询(按 task 共享、不分权);否则按 user 分权。
|
||||
store = _get_store(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
row = await store.get_by_draft(draft_id, user_id=user_id)
|
||||
row = await store.get_by_draft(draft_id, user_id=user_id, task_id=tid)
|
||||
return _to_response(row) if row is not None else None
|
||||
|
||||
|
||||
@router.get("/{job_id}", response_model=RoundtableJobResponse)
|
||||
async def get_job(request: Request, job_id: str) -> RoundtableJobResponse:
|
||||
async def get_job(request: Request, job_id: str, task_id: str | None = None) -> RoundtableJobResponse:
|
||||
store = _get_store(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
row = await store.get(job_id, user_id=user_id)
|
||||
row = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@router.get("/{job_id}/stream")
|
||||
async def stream_job(request: Request, job_id: str) -> StreamingResponse:
|
||||
async def stream_job(request: Request, job_id: str, task_id: str | None = None) -> StreamingResponse:
|
||||
store = _get_store(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
def _frame(payload: dict[str, Any]) -> str:
|
||||
@@ -263,7 +274,7 @@ async def stream_job(request: Request, job_id: str) -> StreamingResponse:
|
||||
for _ in range(_MAX_STREAM_TICKS):
|
||||
if await request.is_disconnected():
|
||||
return
|
||||
row = await store.get(job_id, user_id=user_id)
|
||||
row = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
if row is None:
|
||||
yield _frame({"event": "error", "detail": "Job not found"})
|
||||
return
|
||||
@@ -280,12 +291,15 @@ async def stream_job(request: Request, job_id: str) -> StreamingResponse:
|
||||
|
||||
|
||||
@router.post("/{job_id}/resume", response_model=RoundtableJobResponse)
|
||||
async def resume_job(request: Request, job_id: str, body: ResumeJobRequest) -> RoundtableJobResponse:
|
||||
async def resume_job(
|
||||
request: Request, job_id: str, body: ResumeJobRequest, task_id: str | None = None
|
||||
) -> RoundtableJobResponse:
|
||||
store = _get_store(request)
|
||||
executor = _get_executor(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
row = await store.get(job_id, user_id=user_id)
|
||||
row = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
if (row.get("status") or "") != "awaiting_input":
|
||||
@@ -295,14 +309,18 @@ async def resume_job(request: Request, job_id: str, body: ResumeJobRequest) -> R
|
||||
if not answer:
|
||||
raise HTTPException(status_code=400, detail="answer is required")
|
||||
|
||||
# task 作业不分权:放开写入归属校验(user_id=None);并以**原作业所有者**身份续跑,
|
||||
# 保证编排上下文 / 写回 task-draft 用一致的 user 上下文。
|
||||
write_user_id = None if tid else user_id
|
||||
run_user_id = row.get("user_id") or user_id
|
||||
updated = await store.update_progress(
|
||||
job_id, user_id=user_id, status="running", pending_clarification=None
|
||||
job_id, user_id=write_user_id, status="running", pending_clarification=None
|
||||
)
|
||||
|
||||
executor.start_job(
|
||||
StartParams(
|
||||
job_id=job_id,
|
||||
user_id=user_id,
|
||||
user_id=run_user_id,
|
||||
agents=_seat_refs_from_nodes(row.get("dispatch_chain")),
|
||||
seed_message=_RESUME_PREFIX + answer,
|
||||
model=None,
|
||||
@@ -311,6 +329,7 @@ async def resume_job(request: Request, job_id: str, body: ResumeJobRequest) -> R
|
||||
thread_ids=row.get("thread_ids"),
|
||||
coordinator_name=row.get("coordinator_name") or "roundtable-coordinator",
|
||||
draft_id=row.get("draft_id"),
|
||||
task_id=tid or row.get("task_id"),
|
||||
is_resume=True,
|
||||
chain=row.get("chain"),
|
||||
orchestration_plan=row.get("orchestration_plan"),
|
||||
@@ -359,15 +378,17 @@ async def admin_cleanup(request: Request, body: CleanupRequest) -> CleanupRespon
|
||||
|
||||
|
||||
@router.post("/{job_id}/cancel", response_model=RoundtableJobResponse)
|
||||
async def cancel_job(request: Request, job_id: str) -> RoundtableJobResponse:
|
||||
async def cancel_job(request: Request, job_id: str, task_id: str | None = None) -> RoundtableJobResponse:
|
||||
store = _get_store(request)
|
||||
executor = _get_executor(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
row = await store.get(job_id, user_id=user_id)
|
||||
row = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
|
||||
executor.cancel(job_id)
|
||||
updated = await store.update_progress(job_id, user_id=user_id, status="cancelled")
|
||||
# task 作业不分权:放开写入归属校验。
|
||||
updated = await store.update_progress(job_id, user_id=(None if tid else user_id), status="cancelled")
|
||||
return _to_response(updated or row)
|
||||
@@ -0,0 +1,187 @@
|
||||
"""CRUD API for **task-scoped** roundtable-planning drafts (聊天记录).
|
||||
|
||||
This is the dedicated store for roundtable sessions opened via a taskId deep-link
|
||||
(无界嵌入抽屉). It is physically and semantically separate from the per-user
|
||||
``/api/roundtable-drafts`` store:
|
||||
|
||||
* records are keyed **only by ``task_id``** (one task → many records);
|
||||
* there is **no user 分权** — any authenticated caller may read/write/delete a
|
||||
task's records (the deep-link audience is shared); ``created_by`` is recorded
|
||||
for audit only.
|
||||
|
||||
Routes (prefix ``/api/roundtable-task-drafts``):
|
||||
GET "/by-task/{task_id}" list all drafts for a task (lightweight)
|
||||
GET "/by-task/{task_id}/latest" newest draft for a task (full)
|
||||
POST "" create (``task_id`` required)
|
||||
GET "/{draft_id}" full draft (incl. step1/step2/step3)
|
||||
PUT "/{draft_id}" partial update
|
||||
DELETE "/{draft_id}" delete
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/roundtable-task-drafts", tags=["roundtable-task-drafts"])
|
||||
|
||||
|
||||
# ── schemas ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TaskDraftMetaResponse(BaseModel):
|
||||
id: str
|
||||
task_id: str
|
||||
title: str = ""
|
||||
furthest_step: int = 1
|
||||
created_at: datetime | str | None = None
|
||||
updated_at: datetime | str | None = None
|
||||
# Latest background-job status for this draft (history-dropdown loading badge).
|
||||
status: str | None = None
|
||||
job_id: str | None = None
|
||||
|
||||
|
||||
class TaskDraftListResponse(BaseModel):
|
||||
drafts: list[TaskDraftMetaResponse]
|
||||
|
||||
|
||||
class TaskDraftResponse(TaskDraftMetaResponse):
|
||||
step1: dict[str, Any] | None = None
|
||||
step2: dict[str, Any] | None = None
|
||||
step3: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class TaskDraftCreateRequest(BaseModel):
|
||||
id: str | None = Field(default=None, max_length=64)
|
||||
task_id: str = Field(max_length=128)
|
||||
title: str = Field(default="", max_length=512)
|
||||
furthest_step: int = Field(default=1, ge=1, le=3)
|
||||
step1: dict[str, Any] | None = None
|
||||
step2: dict[str, Any] | None = None
|
||||
step3: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class TaskDraftUpdateRequest(BaseModel):
|
||||
title: str | None = Field(default=None, max_length=512)
|
||||
furthest_step: int | None = Field(default=None, ge=1, le=3)
|
||||
step1: dict[str, Any] | None = None
|
||||
step2: dict[str, Any] | None = None
|
||||
step3: dict[str, Any] | None = None
|
||||
|
||||
|
||||
# ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _current_user_id(request: Request) -> str:
|
||||
user = getattr(request.state, "user", None)
|
||||
if user is not None:
|
||||
return str(user.id)
|
||||
return get_effective_user_id()
|
||||
|
||||
|
||||
def _get_store(request: Request):
|
||||
store = getattr(request.app.state, "roundtable_task_draft_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="Roundtable task draft store not available")
|
||||
return store
|
||||
|
||||
|
||||
async def _enrich_status(request: Request, rows: list[dict[str, Any]]) -> None:
|
||||
"""Attach the latest background-job status/jobId per draft (best-effort).
|
||||
|
||||
Uses the job store's draft-id keyed lookup (no user filter) — a task's drafts
|
||||
span users. Failure or a missing job store silently degrades to no status.
|
||||
"""
|
||||
job_store = getattr(request.app.state, "roundtable_job_store", None)
|
||||
if job_store is None or not rows:
|
||||
return
|
||||
try:
|
||||
status_map = await job_store.status_map_by_drafts([r.get("id") for r in rows])
|
||||
except AttributeError:
|
||||
status_map = {}
|
||||
except Exception: # noqa: BLE001 — 富化失败不能拖垮列表
|
||||
logger.warning("roundtable task draft list: job status enrichment failed", exc_info=True)
|
||||
status_map = {}
|
||||
for r in rows:
|
||||
info = status_map.get(r.get("id"))
|
||||
if info:
|
||||
r["status"] = info.get("status")
|
||||
r["job_id"] = info.get("jobId")
|
||||
|
||||
|
||||
# ── routes ───────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.get("/by-task/{task_id}", response_model=TaskDraftListResponse)
|
||||
async def list_drafts_by_task(request: Request, task_id: str) -> TaskDraftListResponse:
|
||||
"""All drafts for a task, newest-first (一个 taskId 多条记录,不分权)。"""
|
||||
store = _get_store(request)
|
||||
rows = await store.list_drafts_by_task(task_id)
|
||||
await _enrich_status(request, rows)
|
||||
return TaskDraftListResponse(drafts=[TaskDraftMetaResponse(**r) for r in rows])
|
||||
|
||||
|
||||
@router.get("/by-task/{task_id}/latest", response_model=TaskDraftResponse)
|
||||
async def get_latest_draft_by_task(request: Request, task_id: str) -> TaskDraftResponse:
|
||||
"""Newest draft for a task; 404 if none. 回显最新一条聊天记录用。"""
|
||||
store = _get_store(request)
|
||||
row = await store.get_latest_draft_by_task(task_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="No draft for this task")
|
||||
return TaskDraftResponse(**row)
|
||||
|
||||
|
||||
@router.post("", response_model=TaskDraftResponse, status_code=201)
|
||||
async def create_draft(request: Request, body: TaskDraftCreateRequest) -> TaskDraftResponse:
|
||||
store = _get_store(request)
|
||||
task_id = (body.task_id or "").strip()
|
||||
if not task_id:
|
||||
raise HTTPException(status_code=400, detail="task_id is required")
|
||||
row = await store.create_draft(
|
||||
{
|
||||
"id": (body.id or uuid4().hex),
|
||||
"task_id": task_id,
|
||||
"title": body.title.strip(),
|
||||
"furthest_step": body.furthest_step,
|
||||
"step1": body.step1,
|
||||
"step2": body.step2,
|
||||
"step3": body.step3,
|
||||
},
|
||||
created_by=_current_user_id(request),
|
||||
)
|
||||
return TaskDraftResponse(**row)
|
||||
|
||||
|
||||
@router.get("/{draft_id}", response_model=TaskDraftResponse)
|
||||
async def get_draft(request: Request, draft_id: str) -> TaskDraftResponse:
|
||||
store = _get_store(request)
|
||||
row = await store.get_draft(draft_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Draft not found")
|
||||
return TaskDraftResponse(**row)
|
||||
|
||||
|
||||
@router.put("/{draft_id}", response_model=TaskDraftResponse)
|
||||
async def update_draft(request: Request, draft_id: str, body: TaskDraftUpdateRequest) -> TaskDraftResponse:
|
||||
store = _get_store(request)
|
||||
fields = body.model_dump(exclude_unset=True)
|
||||
row = await store.update_draft(draft_id, **fields)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Draft not found")
|
||||
return TaskDraftResponse(**row)
|
||||
|
||||
|
||||
@router.delete("/{draft_id}", status_code=204)
|
||||
async def delete_draft(request: Request, draft_id: str) -> None:
|
||||
store = _get_store(request)
|
||||
deleted = await store.delete_draft(draft_id)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=404, detail="Draft not found")
|
||||
Reference in new issue
Block a user