deepseek-proxy / server /tab_manager.py
adityaxess's picture
Deploy 24/7 DeepSeek Proxy Server
aedcabb
Raw History Blame Contribute Delete
2.62 kB
"""
Tab & Session State Manager for DeepSeek Server.
Manages active chat session tab, session switching, and conversation turn pointers.
"""
import asyncio
from server.cdp_client import create_new_chat_session, fetch_previous_chat_sessions
class TabManager:
def __init__(self):
self.active_session_id = None
# Maps chat_session_id -> last assistant parent_message_id
self.session_pointers = {}
async def initialize(self, use_new_tab: bool = True, selected_session_id: str = None):
"""
Initializes tab state.
If use_new_tab is True, creates a new chat session.
Otherwise selects chosen session_id or defaults to most recent.
"""
if use_new_tab or not selected_session_id:
session_id = await create_new_chat_session()
self.active_session_id = session_id
self.session_pointers[session_id] = None
print(f"πŸ†• Initialized NEW Chat Session Tab: {session_id}")
return session_id
else:
self.active_session_id = selected_session_id
if selected_session_id not in self.session_pointers:
self.session_pointers[selected_session_id] = None
print(f"πŸ”„ Switched to PREVIOUS Chat Session Tab: {selected_session_id}")
return selected_session_id
async def get_active_session(self) -> str:
"""Returns the current active chat session ID. Creates one if none active."""
if not self.active_session_id:
await self.initialize(use_new_tab=True)
return self.active_session_id
async def set_active_session(self, session_id: str):
"""Sets the active chat session ID."""
self.active_session_id = session_id
if session_id not in self.session_pointers:
self.session_pointers[session_id] = None
print(f"πŸ“Œ Active Tab set to: {session_id}")
return session_id
async def list_available_tabs(self):
"""Returns list of previous chat sessions from DeepSeek API."""
sessions = await fetch_previous_chat_sessions()
return sessions
def get_parent_message_id(self, session_id: str):
"""Returns last recorded parent_message_id for the given session."""
return self.session_pointers.get(session_id)
def update_parent_message_id(self, session_id: str, message_id: int):
"""Updates last recorded parent_message_id for the session."""
if message_id is not None:
self.session_pointers[session_id] = message_id
# Global singleton instance
tab_manager = TabManager()