MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / ServerState

Class ServerState

api_utils/server_state.py:36–102  ·  view source on GitHub ↗

Centralized container for all server state. This class holds all mutable state that needs to be shared across modules. Using a class allows for better organization and easier testing (state can be reset).

Source from the content-addressed store, hash-verified

34
35
36class ServerState:
37 """
38 Centralized container for all server state.
39
40 This class holds all mutable state that needs to be shared across modules.
41 Using a class allows for better organization and easier testing (state can be reset).
42 """
43
44 def __init__(self) -> None:
45 """Initialize all state variables with default values."""
46 self.reset()
47
48 def reset(self) -> None:
49 """Reset all state to initial values. Useful for testing."""
50 # --- Stream Queue ---
51 self.STREAM_QUEUE: Optional[multiprocessing.Queue] = None
52 self.STREAM_PROCESS: Optional[multiprocessing.Process] = None
53
54 # --- Playwright/Browser State ---
55 self.playwright_manager: Optional["AsyncPlaywright"] = None
56 self.browser_instance: Optional["AsyncBrowser"] = None
57 self.page_instance: Optional["AsyncPage"] = None
58 self.is_playwright_ready: bool = False
59 self.is_browser_connected: bool = False
60 self.is_page_ready: bool = False
61 self.is_initializing: bool = False
62
63 # --- Proxy Configuration ---
64 self.PLAYWRIGHT_PROXY_SETTINGS: Optional[Dict[str, str]] = None
65
66 # --- Model State ---
67 self.global_model_list_raw_json: Optional[str] = None
68 self.parsed_model_list: List[Dict[str, Any]] = []
69 self.model_list_fetch_event: Event = asyncio.Event()
70 self.current_ai_studio_model_id: Optional[str] = None
71 self.current_auth_profile_path: Optional[str] = None
72 self.model_switching_lock: Lock = Lock()
73 self.excluded_model_ids: Set[str] = set()
74
75 # --- Request Processing State ---
76 self.request_queue: "Optional[Queue[QueueItem]]" = None
77 self.processing_lock: Optional[Lock] = None
78 self.worker_task: "Optional[Task[None]]" = None
79
80 # --- Parameter Cache ---
81 self.page_params_cache: Dict[str, Any] = {}
82 self.params_cache_lock: Lock = Lock()
83
84 # --- Debug Logging State ---
85 self.console_logs: List[Dict[str, Any]] = []
86 self.network_log: Dict[str, List[Dict[str, Any]]] = {
87 "requests": [],
88 "responses": [],
89 }
90
91 # --- Logging ---
92 self.logger: logging.Logger = logging.getLogger("AIStudioProxyServer")
93 self.log_ws_manager: Optional["WebSocketConnectionManager"] = None

Callers 2

fresh_stateFunction · 0.90
server_state.pyFile · 0.85

Calls

no outgoing calls

Tested by 1

fresh_stateFunction · 0.72