import logging from typing import Any, Dict, List, Optional from database.view_manager import IdentifierType logger = logging.getLogger(__name__) MAX_HISTORY_MESSAGES = 10 class ContextManager: def __init__(self) -> None: self._active_identifiers: Dict[IdentifierType, str] = {} self._last_database_response: Optional[str] = None def get_active_identifier(self, id_type: IdentifierType) -> Optional[str]: if id_type is None: raise TypeError("id_type must not be None") return self._active_identifiers.get(id_type) def set_active_identifier(self, id_type: IdentifierType, value: Optional[str]) -> None: if not value or not value.strip(): return self._active_identifiers[id_type] = value.strip() self._last_database_response = None def clear_all_identifiers(self) -> None: self._active_identifiers.clear() self._last_database_response = None def get_active_credit_file_id(self) -> Optional[str]: return self._active_identifiers.get(IdentifierType.CREDIT_FILE_ID) def set_active_credit_file_id(self, credit_file_id: Optional[str]) -> None: if credit_file_id and credit_file_id.strip(): self.set_active_identifier(IdentifierType.CREDIT_FILE_ID, credit_file_id) def get_last_database_response(self) -> Optional[str]: return self._last_database_response def set_last_database_response(self, response: Optional[str]) -> None: self._last_database_response = response @property def active_identifiers(self) -> Dict[IdentifierType, str]: return self._active_identifiers.copy() _context_manager_instance: Optional[ContextManager] = None def get_context_manager() -> ContextManager: global _context_manager_instance if _context_manager_instance is None: _context_manager_instance = ContextManager() return _context_manager_instance def reset_context_manager() -> None: global _context_manager_instance _context_manager_instance = None