| 134 | |
| 135 | |
| 136 | async def _initialize_browser_and_page(): |
| 137 | from playwright.async_api import async_playwright |
| 138 | |
| 139 | state.logger.info("Starting Playwright...") |
| 140 | state.playwright_manager = await async_playwright().start() |
| 141 | state.is_playwright_ready = True |
| 142 | state.logger.info("Playwright started.") |
| 143 | |
| 144 | ws_endpoint = get_environment_variable("CAMOUFOX_WS_ENDPOINT") |
| 145 | launch_mode = get_environment_variable("LAUNCH_MODE", "unknown") |
| 146 | |
| 147 | if not ws_endpoint and launch_mode != "direct_debug_no_browser": |
| 148 | raise ValueError("CAMOUFOX_WS_ENDPOINT environment variable is missing.") |
| 149 | |
| 150 | if ws_endpoint: |
| 151 | state.logger.info(f"Connecting to browser at: {ws_endpoint}") |
| 152 | state.browser_instance = await state.playwright_manager.firefox.connect( |
| 153 | ws_endpoint, timeout=30000 |
| 154 | ) |
| 155 | state.is_browser_connected = True |
| 156 | state.logger.info(f"Connected to browser: {state.browser_instance.version}") |
| 157 | |
| 158 | state.page_instance, state.is_page_ready = await _initialize_page_logic( |
| 159 | state.browser_instance |
| 160 | ) |
| 161 | if state.is_page_ready: |
| 162 | await _handle_initial_model_state_and_storage(state.page_instance) |
| 163 | await enable_temporary_chat_mode(state.page_instance) |
| 164 | state.logger.info("Page initialized successfully.") |
| 165 | else: |
| 166 | state.logger.error("Page initialization failed.") |
| 167 | state.page_instance = None |
| 168 | state.is_page_ready = False |
| 169 | state.current_ai_studio_model_id = None |
| 170 | |
| 171 | if not state.model_list_fetch_event.is_set(): |
| 172 | state.model_list_fetch_event.set() |
| 173 | |
| 174 | |
| 175 | async def _shutdown_resources(): |