feat(roundtable): 会商前端调用报错收集 + 诊断页用户名/范围切换

- 前端调用报错收集(用户实际遇到的报错,服务端诊断看不到的也收):新增
  POST /api/roundtable-diagnostics/report(非 admin,scope="client")+ 前端上报器
  roundtable-planning/api/diagnostics-report.ts。streamMultiAgent(第二步席位 +
  第三步报告/结构化/绘制/大屏)覆盖 HTTP 错误(502)、SSE error 帧、worker error 帧
  (模型没调用成功)、fetch 网络不可达、流式中途中断,reported 去重;initMultiAgent /
  intent / recommend / 后台作业接口同步上报。
- 诊断页:每条报错显示上报用户名(👤,user_id→邮箱@前缀,后端 best-effort 解析);
  第一个筛选改为「后台挂起 / 前台调用」切换(前台调用=foreground,client,后端 scope
  支持逗号多值),阶段下拉随范围联动(后台→二三步;前台→一二三步)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
董宏凯andClaude Opus 4.8 committed 2026-06-19 23:45:34 +08:00
1 parent 4a403c95c5
commit 762d3620c4
12 files changed
+433 -18

No files matched your search

@@ -18,6 +18,8 @@ from typing import Any
from fastapi import APIRouter, HTTPException, Query, Request
from pydantic import BaseModel, Field
from deerflow.runtime.user_context import get_effective_user_id
router = APIRouter(prefix="/api/roundtable-diagnostics", tags=["roundtable-diagnostics"])
@@ -28,6 +30,16 @@ def _get_store(request: Request):
return store
def _resolve_user_id(request: Request) -> str | None:
user = getattr(request.state, "user", None)
if user is not None:
return str(user.id)
try:
return get_effective_user_id()
except Exception: # noqa: BLE001
return None
async def _require_admin(request: Request) -> None:
"""放行 admin / auth-disabled,其余 403(与 roundtable_jobs / task_buttons 同套语义)。"""
from app.gateway.deps import get_optional_user_from_request
@@ -55,6 +67,7 @@ class DiagnosticItem(BaseModel):
agentName: str | None = None
cycle: int | None = None
createdAt: str | None = None
userName: str | None = None
class DiagnosticListResponse(BaseModel):
@@ -81,6 +94,22 @@ class CleanupResponse(BaseModel):
elapsed_seconds: float
class ReportRequest(BaseModel):
"""前端上报一条会商调用报错(任意登录用户可调,**非 admin**)。"""
stage: str = "client"
event: str = ""
level: str = "error"
message: str | None = None
detail: Any = None
jobId: str | None = None
draftId: str | None = None
taskId: str | None = None
agentId: str | None = None
agentName: str | None = None
cycle: int | None = None
def _to_item(row: dict[str, Any]) -> DiagnosticItem:
return DiagnosticItem(
id=row["id"],
@@ -98,9 +127,48 @@ def _to_item(row: dict[str, Any]) -> DiagnosticItem:
agentName=row.get("agent_name"),
cycle=row.get("cycle"),
createdAt=row.get("created_at"),
userName=row.get("_user_name"),
)
def _username_from_email(email: str | None, uid: str | None) -> str | None:
"""展示用用户名:邮箱 ``@`` 前缀(与个人页一致);无邮箱回退到短 uid。"""
if email:
return email.split("@", 1)[0] if "@" in email else email
if uid:
return uid[:8]
return None
async def _enrich_user_names(request: Request, items: list[dict[str, Any]]) -> None:
"""把每条诊断的 user_id 解析成用户名(邮箱 @ 前缀),写进 dict 的 ``_user_name``。
Best-effort + 批量去重缓存:解析不到就回退短 uid,绝不因此让列表查询失败。
"""
try:
from app.gateway.deps import get_local_provider
provider = get_local_provider()
except Exception: # noqa: BLE001
provider = None
cache: dict[str, str | None] = {}
for it in items:
uid = it.get("user_id")
if not uid:
it["_user_name"] = None
continue
if uid not in cache:
email: str | None = None
if provider is not None:
try:
user = await provider.get_user(uid)
email = getattr(user, "email", None) if user else None
except Exception: # noqa: BLE001
email = None
cache[uid] = _username_from_email(email, uid)
it["_user_name"] = cache[uid]
def _parse_dt(raw: str | None) -> datetime | None:
if not raw:
return None
@@ -130,8 +198,13 @@ async def list_diagnostics(
) -> DiagnosticListResponse:
await _require_admin(request)
store = _get_store(request)
# scope 支持逗号分隔多值(前端「前台调用」= foreground,client 两种 scope 合并查)。
scope_param: str | list[str] | None = None
if scope:
parts = [s.strip() for s in scope.split(",") if s.strip()]
scope_param = parts if len(parts) > 1 else (parts[0] if parts else None)
items, total = await store.list_logs(
scope=scope or None,
scope=scope_param,
stage=stage or None,
level=level or None,
event=event or None,
@@ -145,9 +218,41 @@ async def list_diagnostics(
limit=limit,
offset=offset,
)
await _enrich_user_names(request, items)
return DiagnosticListResponse(items=[_to_item(r) for r in items], total=total, limit=limit, offset=offset)
@router.post("/report")
async def report_client_error(request: Request, body: ReportRequest) -> dict[str, bool]:
"""前端把一次会商调用的报错上报上来(HTTP 502/网络失败/SSE error 帧等用户实际遇到的)。
**不 admin-gated**——任何登录用户在遇到报错时都应能上报;写入 scope="client" 以区分
「服务端观测到的(foreground)」与「前端上报的(client)」。best-effort,写失败也返回 ok。
"""
store = getattr(request.app.state, "roundtable_diagnostic_store", None)
if store is None:
return {"ok": False}
try:
await store.record(
scope="client",
stage=body.stage or "client",
level=body.level or "error",
event=body.event or "",
message=body.message,
detail=body.detail,
job_id=body.jobId,
draft_id=body.draftId,
task_id=body.taskId,
user_id=_resolve_user_id(request),
agent_id=body.agentId,
agent_name=body.agentName,
cycle=body.cycle,
)
except Exception: # noqa: BLE001 — 上报失败不影响前端
return {"ok": False}
return {"ok": True}
@router.get("/facets", response_model=FacetsResponse)
async def get_facets(request: Request) -> FacetsResponse:
await _require_admin(request)