From ddba32ef500d271a9e6f461178282c03964f417a Mon Sep 17 00:00:00 2001 From: anor Date: Fri, 27 Mar 2026 17:15:46 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20admin=20=D0=B2=D0=B8=D0=B4=D0=B8=D1=82?= =?UTF-8?q?=20=D1=87=D1=83=D0=B6=D0=B8=D0=B5=20=D1=81=D0=B5=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20/=3Ftab=3DUUID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_history() ищет по username+tab_id. Если пусто и role=admin, fallback на get_history_any_user(tab_id) — без фильтра по username. Co-Authored-By: Claude Opus 4.6 (1M context) --- db.py | 11 +++++++++++ main.py | 11 +++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index 3bd1bc8..d4e4d13 100644 --- a/db.py +++ b/db.py @@ -190,6 +190,17 @@ def list_invites() -> list[dict]: return [dict(r) for r in rows] +def get_history_any_user(tab_id: str, limit: int = 100) -> list[dict]: + """Get chat history for a tab regardless of username (admin view).""" + with _conn() as conn: + rows = conn.execute( + "SELECT role, content, timestamp FROM messages " + "WHERE tab_id = ? ORDER BY id DESC LIMIT ?", + (tab_id, limit), + ).fetchall() + return [dict(r) for r in reversed(rows)] + + def list_sessions(username: str) -> list[dict]: """List all chat sessions for a user with last message preview.""" with _conn() as conn: diff --git a/main.py b/main.py index 321daf3..d05c623 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse from fastapi.staticfiles import StaticFiles from auth import authenticate, verify_token -from db import get_history, init_db, list_sessions, save_message +from db import get_history, get_history_any_user, init_db, list_sessions, save_message # Backend: "claude" (Docker sandbox), "glm" (GLM-5 + CAG), "auto" (route by task) AI_BACKEND = os.environ.get("AI_BACKEND", "claude") @@ -217,7 +217,14 @@ async def chat(request: Request): async def history(request: Request): user = _get_user(request) tab_id = request.headers.get("x-tab-id", "default") - return get_history(user, tab_id) + # Try own history first; if empty and admin — try any user's history for this tab + result = get_history(user, tab_id) + if not result: + from auth import get_token_role + token = request.headers.get("authorization", "").removeprefix("Bearer ").strip() + if get_token_role(token) == "admin": + result = get_history_any_user(tab_id) + return result @app.get("/api/sessions")