# routes/session_routes.py import re import json import uuid from datetime import datetime from fastapi import APIRouter, Form, HTTPException, Response, Request import logging from core.session_manager import SessionManager from core.models import ChatMessage from src.request_models import SessionResponse from core.database import Session as DbSession, SessionLocal, Document, GalleryImage from src.auth_helpers import get_current_user def _verify_session_owner(request: Request, session_id: str): """Verify the current user owns the session. Raises 404 if not.""" user = get_current_user(request) if not user: raise HTTPException(403, "Authentication required") db = SessionLocal() try: row = db.query(DbSession.owner).filter(DbSession.id == session_id).first() if not row: raise HTTPException(404, f"Session {session_id} not found") if row.owner != user: raise HTTPException(404, f"Session {session_id} not found") finally: db.close() logger = logging.getLogger(__name__) router = APIRouter(prefix="/api", tags=["sessions"]) def _pick_endpoint_for_sort(): """Pick model endpoint for auto-sort LLM call — uses utility endpoint setting, falls back to default.""" from src.endpoint_resolver import resolve_endpoint # Try utility endpoint first (what the user configured for background tasks) url, model, headers = resolve_endpoint("utility") if url and model: return url, model, headers # Fall back to task endpoint try: from src.task_endpoint import resolve_task_endpoint url, model, headers = resolve_task_endpoint() if url and model: return url, model, headers except Exception: pass # Fall back to default url, model, headers = resolve_endpoint("default") if url and model: return url, model, headers return None, None, None def setup_session_routes(session_manager: SessionManager, config: dict, webhook_manager=None): """Setup session routes with the provided manager and config""" REQUEST_TIMEOUT = config.get("REQUEST_TIMEOUT", 20) OPENAI_API_KEY = config.get("OPENAI_API_KEY") SESSIONS_FILE = config.get("SESSIONS_FILE") @router.get("/sessions") def list_sessions(request: Request): user = get_current_user(request) # Lazy purge: incognito sessions are ephemeral by design — wipe leftovers # from the DB and session_manager so they vanish on the next page refresh. # BUT: skip sessions that were created within the last 10 minutes. # Without that guard, the purge nukes the active "Nobody" session on the # very first /api/sessions call after creation, killing the in-flight # chat. The frontend's own _cleanupIncognitoSessions handler knows which # session is current and won't delete the live one — this server-side # purge exists only to catch ghosts the frontend missed (tab close, # crash). Only clean up rows old enough to be definitely orphaned. try: from datetime import datetime as _dt, timedelta as _td _cutoff = _dt.utcnow() - _td(minutes=10) _purge_db = SessionLocal() try: from core.database import ChatMessage as _DbMsg _ghosts = _purge_db.query(DbSession).filter( DbSession.name.in_(("Nobody", "Incognito")), DbSession.created_at < _cutoff, ).all() for _g in _ghosts: _purge_db.query(_DbMsg).filter(_DbMsg.session_id == _g.id).delete() _purge_db.delete(_g) if hasattr(session_manager, "delete_session"): try: session_manager.delete_session(_g.id) except Exception: pass if _ghosts: _purge_db.commit() finally: _purge_db.close() except Exception: pass user_sessions = session_manager.get_sessions_for_user(user) # Fetch folder info from DB for each session db = SessionLocal() try: folder_map = {} token_map = {} important_map = {} created_map = {} updated_map = {} last_msg_map = {} mode_map = {} msg_count_map = {} rows = db.query(DbSession.id, DbSession.folder, DbSession.total_input_tokens, DbSession.total_output_tokens, DbSession.is_important, DbSession.created_at, DbSession.updated_at, DbSession.last_message_at, DbSession.mode, DbSession.message_count).filter(DbSession.archived == False).all() for row in rows: folder_map[row.id] = row.folder token_map[row.id] = (row.total_input_tokens or 0) + (row.total_output_tokens or 0) important_map[row.id] = row.is_important or False created_map[row.id] = row.created_at.isoformat() if row.created_at else None updated_map[row.id] = row.updated_at.isoformat() if row.updated_at else None # Fall back to updated_at then created_at so sessions that # predate the column (or have no messages) still sort sanely. last_msg_map[row.id] = ( row.last_message_at.isoformat() if row.last_message_at else (row.updated_at.isoformat() if row.updated_at else (row.created_at.isoformat() if row.created_at else None)) ) mode_map[row.id] = row.mode msg_count_map[row.id] = row.message_count or 0 # Sessions with active documents that have content from sqlalchemy import func doc_session_ids = set( r[0] for r in db.query(Document.session_id) .filter(Document.is_active == True, Document.current_content != None, func.trim(Document.current_content) != "") .distinct().all() ) img_session_ids = set( r[0] for r in db.query(GalleryImage.session_id) .filter(GalleryImage.session_id != None) .distinct().all() ) finally: db.close() sessions = [{"id": s.id, "name": s.name, "model": s.model, "endpoint_url": s.endpoint_url, "rag": s.rag, "archived": s.archived, "folder": folder_map.get(s.id), "total_tokens": token_map.get(s.id, 0), "is_important": important_map.get(s.id, False), "created_at": created_map.get(s.id), "updated_at": updated_map.get(s.id), "last_message_at": last_msg_map.get(s.id), "has_documents": s.id in doc_session_ids, "has_images": s.id in img_session_ids, "mode": mode_map.get(s.id), "message_count": msg_count_map.get(s.id, 0)} for s in user_sessions.values() if not s.archived and (s.name or "").strip() not in ("Nobody", "Incognito")] return sessions @router.post("/session", response_model=SessionResponse) def create_session( request: Request, name: str = Form(""), endpoint_url: str = Form(""), model: str = Form(""), rag: str = Form(None), skip_validation: str = Form(None), api_key: str = Form(""), endpoint_id: str = Form(""), ): skip_val = str(skip_validation).lower() == "true" if not endpoint_url and not skip_val: raise HTTPException(400, "endpoint_url is required (choose from /api/models)") model_to_use = model if skip_val: # skip_validation = trust the caller and do NOT probe /v1/models. # Used for custom endpoints AND for bare placeholder sessions with no # model at all (e.g. an email reply draft just needs a session to live # in). Probing here was 400-ing those with "Cannot reach /v1/models". pass elif not model_to_use: from src.llm_core import list_model_ids ids = list_model_ids(endpoint_url, timeout=REQUEST_TIMEOUT, headers={"Authorization": f"Bearer {api_key}"} if api_key.strip() else None) if not ids: raise HTTPException(400, "Cannot reach /v1/models") # Default to the first CHAT model — endpoints often list embedding/ # tts/whisper models first (e.g. text-embedding-ada-002), which # can't hold a conversation. _NON_CHAT = ("text-embedding", "embedding", "tts-", "whisper", "text-moderation", "moderation-", "dall-e", "rerank") chat_ids = [m for m in ids if not any(p in m.lower() for p in _NON_CHAT)] model_to_use = (chat_ids or ids)[0] else: from src.llm_core import list_model_ids import os as _os req_base = _os.path.basename(model_to_use.rstrip("/")) avail = list_model_ids(endpoint_url, timeout=REQUEST_TIMEOUT, headers={"Authorization": f"Bearer {api_key}"} if api_key.strip() else None) if not avail: raise HTTPException(400, "Cannot reach /v1/models") if model_to_use not in avail: found = None for a in avail: if _os.path.basename(a.rstrip("/")) == req_base: found = a break if not found: raise HTTPException(400, f"Model not found at server. Available: {', '.join(avail)}") model_to_use = found sid = str(uuid.uuid4()) user = get_current_user(request) session = session_manager.create_session( session_id=sid, name=name or "", endpoint_url=endpoint_url or "", model=model_to_use, rag=str(rag).lower() == "true" if rag else False, owner=user, ) # Set auth headers for custom API-key endpoints resolved_key = api_key.strip() if api_key else "" resolved_base = endpoint_url if not resolved_key and endpoint_id and endpoint_id.strip(): from core.database import ModelEndpoint _db = SessionLocal() try: ep = _db.query(ModelEndpoint).filter(ModelEndpoint.id == endpoint_id.strip()).first() if ep and ep.api_key: resolved_key = ep.api_key resolved_base = ep.base_url finally: _db.close() if resolved_key: from src.endpoint_resolver import build_headers session.headers = build_headers(resolved_key, resolved_base) session_manager.save_sessions() # Fire webhook (sync-safe) if webhook_manager: webhook_manager.fire_and_forget("session.created", { "session_id": sid, "name": session.name, "model": model_to_use, }) # Fire event for automation tasks from src.event_bus import fire_event fire_event("session_created", user) return SessionResponse( id=sid, name=session.name, model=model_to_use, rag=str(rag).lower() == "true" if rag else False, archived=False ) @router.patch("/session/{sid}") def rename_session( request: Request, sid: str, name: str = Form(None), folder: str = Form(None), model: str = Form(None), endpoint_url: str = Form(None), endpoint_id: str = Form(None), ): _verify_session_owner(request, sid) try: session = session_manager.get_session(sid) except KeyError: raise HTTPException(404, f"Session {sid} not found") result = {"id": sid} if name is not None: session_manager.update_session_name(sid, name) result["name"] = name # Update folder assignment if folder is not None: db = SessionLocal() try: db_session = db.query(DbSession).filter(DbSession.id == sid).first() if db_session: db_session.folder = folder if folder else None db_session.updated_at = datetime.utcnow() db.commit() result["folder"] = folder if folder else None finally: db.close() # Switch model/endpoint mid-session if model is not None and endpoint_url is not None: if endpoint_id: from core.database import ModelEndpoint _db = SessionLocal() try: ep = _db.query(ModelEndpoint).filter(ModelEndpoint.id == endpoint_id).first() if not ep: raise HTTPException(400, "Model endpoint no longer exists") finally: _db.close() session.model = model session.endpoint_url = endpoint_url # Update auth headers from the endpoint's stored API key if endpoint_id: _db = SessionLocal() try: ep = _db.query(ModelEndpoint).filter(ModelEndpoint.id == endpoint_id).first() if ep and ep.api_key: from src.endpoint_resolver import build_headers session.headers = build_headers(ep.api_key, ep.base_url) finally: _db.close() # Persist to DB db = SessionLocal() try: db_session = db.query(DbSession).filter(DbSession.id == sid).first() if db_session: db_session.model = model db_session.endpoint_url = endpoint_url db_session.updated_at = datetime.utcnow() db.commit() finally: db.close() result["model"] = model result["endpoint_url"] = endpoint_url return result @router.post("/session/{sid}/inject_messages") async def inject_messages(request: Request, sid: str): """Bulk-inject messages into a session's history (for group chat sync).""" _verify_session_owner(request, sid) try: sess = session_manager.get_session(sid) except KeyError: raise HTTPException(404, f"Session {sid} not found") body = await request.json() messages = body.get("messages", []) from core.models import ChatMessage for m in messages: sess.add_message(ChatMessage(m["role"], m["content"], metadata=m.get("metadata"))) session_manager.save_sessions() return {"ok": True, "count": len(messages)} @router.post("/session/{sid}/delete") def delete_session_beacon(request: Request, sid: str): """Delete session via POST (for navigator.sendBeacon on page close).""" return delete_session(request, sid) @router.post("/sessions/bulk-delete") async def bulk_delete_sessions(request: Request): """Delete multiple sessions (for compare cleanup via sendBeacon).""" from core.database import ChatMessage as _CM try: body = await request.json() ids = body.get("ids", []) except Exception: ids = [] for sid in ids: try: _verify_session_owner(request, sid) session_manager.delete_session(sid) db = SessionLocal() try: db.query(_CM).filter(_CM.session_id == sid).delete() db.query(DbSession).filter(DbSession.id == sid).delete() db.commit() except Exception: db.rollback() finally: db.close() except Exception: pass return {"deleted": len(ids)} @router.delete("/session/{sid}") def delete_session(request: Request, sid: str): """Permanently delete a session and all its messages.""" _verify_session_owner(request, sid) try: # Block deletion of starred/favorited sessions db = SessionLocal() try: db_sess = db.query(DbSession).filter(DbSession.id == sid).first() if db_sess and db_sess.is_important: raise HTTPException( status_code=403, detail={"error": "SESSION_STARRED", "message": "Unstar the session before deleting it"} ) finally: db.close() # Delete the session and all its messages if session_manager.delete_session(sid): return {"status": "deleted"} else: raise HTTPException(404, "Session not found") except HTTPException: raise except Exception as e: logger.error(f"Error deleting session {sid}: {e}") raise HTTPException( status_code=500, detail={ "error": "SESSION_DELETE_ERROR", "message": "Failed to delete session" } ) @router.delete("/sessions/all") def delete_all_sessions(request: Request): """Admin only: permanently delete ALL sessions and their messages.""" from core.middleware import require_admin require_admin(request) db = SessionLocal() try: from core.database import ChatMessage as DbChatMessage count = db.query(DbSession).count() db.query(DbChatMessage).delete() db.query(DbSession).delete() db.commit() session_manager.sessions.clear() logger.info(f"Admin deleted all {count} sessions") return {"status": "deleted", "count": count} except Exception as e: db.rollback() logger.error(f"Error deleting all sessions: {e}") raise HTTPException(500, "Failed to delete sessions") finally: db.close() @router.post("/session/{sid}/archive") def archive_session(request: Request, sid: str): """Archive a session, keeping its data but removing it from active sessions.""" _verify_session_owner(request, sid) try: # First check if session exists session_manager.get_session(sid) # Archive the session db = SessionLocal() try: db_session = db.query(DbSession).filter(DbSession.id == sid).first() if db_session: db_session.archived = True db_session.updated_at = datetime.utcnow() db.commit() # Update in memory if it exists if sid in session_manager.sessions: session_manager.sessions[sid].archived = True logger.info(f"Archived session {sid}") return {"status": "archived"} else: raise HTTPException(404, f"Session {sid} not found") except HTTPException: raise except Exception as e: db.rollback() logger.error(f"Error archiving session {sid}: {e}") raise HTTPException(500, "Failed to archive session") finally: db.close() except KeyError: raise HTTPException(404, f"Session '{sid}' not found") @router.post("/session/{sid}/unarchive") def unarchive_session(request: Request, sid: str): """Restore an archived session back to the active session list.""" _verify_session_owner(request, sid) db = SessionLocal() try: db_session = db.query(DbSession).filter(DbSession.id == sid).first() if not db_session: raise HTTPException(404, f"Session {sid} not found") db_session.archived = False db_session.updated_at = datetime.utcnow() db.commit() # Reload into session manager so it appears in the active list try: if sid in session_manager.sessions: session_manager.sessions[sid].archived = False else: session_manager._load_session_from_db(sid) except Exception: pass # Non-fatal — session will load on next access return {"status": "unarchived"} except HTTPException: raise except Exception as e: db.rollback() logger.error(f"Error unarchiving session {sid}: {e}") raise HTTPException(500, "Failed to unarchive session") finally: db.close() @router.get("/sessions/archived") def list_archived_sessions(request: Request, search: str = "", offset: int = 0, limit: int = 20, sort: str = "recent", model: str = ""): """List archived sessions for the archive browser.""" user = get_current_user(request) db = SessionLocal() try: q = db.query(DbSession).filter(DbSession.archived == True) if not user: raise HTTPException(403, "Authentication required") q = q.filter(DbSession.owner == user) if search: safe_search = search.replace('%', r'\%').replace('_', r'\_') q = q.filter(DbSession.name.ilike(f"%{safe_search}%", escape='\\')) if model: q = q.filter(DbSession.model.ilike(f"%{model}")) total = q.count() sort_map = { "recent": DbSession.updated_at.desc(), "oldest": DbSession.updated_at.asc(), "most-messages": DbSession.message_count.desc().nulls_last(), "alpha": DbSession.name.asc(), } order = sort_map.get(sort, DbSession.updated_at.desc()) rows = q.order_by(order).offset(offset).limit(limit).all() sessions = [] for s in rows: sessions.append({ "id": s.id, "name": s.name, "model": s.model, "message_count": s.message_count or 0, "created_at": s.created_at.isoformat() if s.created_at else None, "updated_at": s.updated_at.isoformat() if s.updated_at else None, "is_important": s.is_important, }) return {"sessions": sessions, "total": total} finally: db.close() @router.get("/history/{sid}") def get_history(request: Request, sid: str): _verify_session_owner(request, sid) try: session = session_manager.get_session(sid) except KeyError: raise HTTPException(404, f"Session {sid} not found") return {"history": [msg.to_dict() for msg in session.history]} @router.get("/session/{sid}/export") def export_session(request: Request, sid: str, fmt: str = "md", filename: str = ""): """Export conversation history as a downloadable file. Supported formats: md (markdown), txt (plain text), json, html """ _verify_session_owner(request, sid) try: session = session_manager.get_session(sid) except KeyError: raise HTTPException(404, f"Session {sid} not found") safe_name = re.sub(r'[^\w\-_]', '_', session.name) timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') if fmt == "json": import json as _json data = { "name": session.name, "model": session.model, "exported": datetime.now().isoformat(), "messages": [{"role": m.role, "content": m.content} for m in session.history], } out_name = filename or f"conversation_{safe_name}_{timestamp}.json" return Response( content=_json.dumps(data, indent=2, ensure_ascii=False), media_type="application/json", headers={"Content-Disposition": f"attachment; filename={out_name}"}, ) if fmt == "txt": lines = [] for m in session.history: lines.append(f"[{m.role.upper()}]") lines.append(m.content) lines.append("") out_name = filename or f"conversation_{safe_name}_{timestamp}.txt" return Response( content="\n".join(lines), media_type="text/plain", headers={"Content-Disposition": f"attachment; filename={out_name}"}, ) if fmt == "html": html_parts = [ "
", f"