deerflow-code/offline-backend-20260512/backend/app/gateway/roundtable_diag.py
2026-09-07 18:24:55 +08:00

64 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""前台(实时会商)诊断日志助手。
后台挂起路径用 ``DiagnosticsRecorder``(绑定作业上下文);前台 multi_agent 路由用本助手
从 ``request`` 取 store + 当前用户,best-effort 落一条 ``scope="foreground"`` 的诊断事件。
任何异常静默吞掉——诊断日志绝不能反过来打断会商。
"""
from __future__ import annotations
import logging
from typing import Any
from fastapi import Request
from deerflow.persistence.roundtable_diagnostics import record_diagnostic
from deerflow.runtime.user_context import get_effective_user_id
logger = logging.getLogger(__name__)
def _resolve_user_id(request: Request) -> str | None:
try:
user = getattr(request.state, "user", None)
if user is not None:
return str(user.id)
return get_effective_user_id()
except Exception: # noqa: BLE001
return None
async def record_foreground_diag(
request: Request,
*,
stage: str,
level: str,
event: str,
message: str | None = None,
detail: Any = None,
thread_id: str | None = None,
draft_id: str | None = None,
task_id: str | None = None,
agent_id: str | None = None,
agent_name: str | None = None,
) -> None:
"""从 request 落一条前台诊断事件(best-effort)。"""
try:
store = getattr(request.app.state, "roundtable_diagnostic_store", None)
await record_diagnostic(
store,
scope="foreground",
stage=stage,
level=level,
event=event,
message=message,
detail=detail,
draft_id=draft_id,
task_id=task_id,
user_id=_resolve_user_id(request),
agent_id=agent_id,
agent_name=agent_name,
)
except Exception: # noqa: BLE001 — 诊断日志绝不打断会商
logger.debug("foreground roundtable diag failed", exc_info=True)