import json import os from bridge.agent_wrapper import AgentWrapper class SessionManager: def __init__(self, bot_client): self.bot = bot_client self.active_sessions = {} # Maps thread_id (int) -> AgentWrapper self.channel_mappings = {} # Maps channel_id (int) -> project_path (str) self.load_config() def load_config(self): config_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "config.json") if os.path.exists(config_path): try: with open(config_path, "r") as f: data = json.load(f) # Convert channel keys to integers self.channel_mappings = {int(k): v for k, v in data.get("channels", {}).items()} except Exception as e: print(f"Error loading config.json: {e}") def get_project_path(self, channel_id: int) -> str: """ Returns the mapped project path for a given channel, or the default workspace if not mapped. """ return self.channel_mappings.get(channel_id, os.getenv("DEFAULT_WORKSPACE_PATH", "/Users/keira/Documents/GitHub")) def get_or_create_session(self, thread_id: int, channel_id: int) -> AgentWrapper: if thread_id in self.active_sessions: return self.active_sessions[thread_id] project_path = self.get_project_path(channel_id) session = AgentWrapper( project_path=project_path, thread_id=thread_id, bot_client=self.bot ) self.active_sessions[thread_id] = session return session def close_session(self, thread_id: int): if thread_id in self.active_sessions: del self.active_sessions[thread_id]