Skip to content

Commit 2d57a20

Browse files
authored
Refactor enable_temporary_chat_mode function
Refactor enable_temporary_chat_mode to improve button visibility handling and streamline the process of enabling temporary chat mode.
1 parent e28f64e commit 2d57a20

1 file changed

Lines changed: 34 additions & 14 deletions

File tree

  • browser_utils/initialization

browser_utils/initialization/core.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -563,30 +563,50 @@ async def signal_camoufox_shutdown() -> None: # pragma: no cover
563563
async def enable_temporary_chat_mode(page: AsyncPage) -> None: # pragma: no cover
564564
"""
565565
Check and enable "Temporary chat" mode in the AI Studio interface.
566-
This is an independent UI operation and should be called after the page is fully stable.
566+
Supports both direct UI visibility and collapsed menu visibility.
567567
"""
568-
try:
569-
incognito_button_locator = page.locator(
570-
'button[aria-label="Temporary chat toggle"], button[aria-label="Toggle temporary chat"]'
571-
)
568+
incognito_selector = 'button[aria-label="Temporary chat toggle"], button[aria-label="Toggle temporary chat"]'
569+
menu_trigger_selector = 'button[aria-label="View more actions"]'
570+
571+
incognito_locator = page.locator(incognito_selector)
572+
menu_trigger = page.locator(menu_trigger_selector)
572573

573-
await incognito_button_locator.wait_for(state="visible", timeout=10000)
574+
try:
575+
# Fast path
576+
logger.debug("[UI] Searching for temporary chat button (Fast path)")
577+
try:
578+
await incognito_locator.wait_for(state="visible", timeout=3000)
579+
except Exception:
580+
# Fallback
581+
logger.debug("[UI] Button not visible, attempting to open menu")
582+
if await menu_trigger.is_visible():
583+
await menu_trigger.click()
584+
# Wait for the menu item to appear
585+
await incognito_locator.wait_for(state="visible", timeout=5000)
586+
else:
587+
logger.warning("[UI] Neither button nor menu trigger found")
588+
return
574589

575-
button_classes = await incognito_button_locator.get_attribute("class")
590+
# Status Check
591+
button_classes = await incognito_locator.get_attribute("class") or ""
576592

577-
if button_classes and "ms-button-active" in button_classes:
593+
if "ms-button-active" in button_classes:
578594
logger.debug("[UI] Temporary chat mode already active")
579595
else:
580-
await incognito_button_locator.click(timeout=5000, force=True)
596+
logger.debug("[UI] Enabling temporary chat mode")
597+
await incognito_locator.click(timeout=5000, force=True)
581598
await asyncio.sleep(1)
582599

583-
updated_classes = await incognito_button_locator.get_attribute("class")
584-
if updated_classes and "ms-button-active" in updated_classes:
585-
logger.debug("[UI] Temporary chat mode enabled")
586-
else:
587-
logger.warning("[UI] Failed to enable temporary chat mode")
600+
# Recovery: Close menu if still expanded
601+
# Checking aria-expanded is more precise than a boolean flag
602+
if await menu_trigger.get_attribute("aria-expanded") == "true":
603+
logger.debug("[UI] Closing menu to restore UI state")
604+
await page.keyboard.press("Escape")
588605

589606
except asyncio.CancelledError:
590607
raise
591608
except Exception as e:
592609
logger.warning(f"[UI] Error in temporary chat mode: {e}")
610+
# Final safety attempt to clear any stuck UI
611+
if await menu_trigger.get_attribute("aria-expanded") == "true":
612+
await page.keyboard.press("Escape")

0 commit comments

Comments
 (0)