Skip to content

Commit 444f5ac

Browse files
authored
Refactor temporary chat mode button handling
1 parent 2d57a20 commit 444f5ac

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

browser_utils/models/switcher.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,41 +160,47 @@ async def switch_ai_studio_model(page: AsyncPage, model_id: str, req_id: str) ->
160160
if page_display_match:
161161
try:
162162
logger.debug("[Model] Re-enabling temporary chat mode...")
163-
incognito_button_locator = page.locator(
164-
'button[aria-label="Temporary chat toggle"], button[aria-label="Toggle temporary chat"]'
165-
)
166-
167-
await incognito_button_locator.wait_for(
168-
state="visible", timeout=5000
169-
)
170-
171-
button_classes = await incognito_button_locator.get_attribute(
172-
"class"
173-
)
174-
175-
if button_classes and "ms-button-active" in button_classes:
163+
incognito_selector = 'button[aria-label="Temporary chat toggle"], button[aria-label="Toggle temporary chat"]'
164+
menu_trigger_selector = 'button[aria-label="View more actions"]'
165+
166+
incognito_locator = page.locator(incognito_selector)
167+
menu_trigger = page.locator(menu_trigger_selector)
168+
169+
# Fast path: try to find the button directly
170+
try:
171+
await incognito_locator.wait_for(state="visible", timeout=3000)
172+
except Exception:
173+
# Fallback: check if it's inside the menu
174+
if await menu_trigger.is_visible():
175+
logger.debug("[Model] Button not visible, opening menu...")
176+
await menu_trigger.click()
177+
await incognito_locator.wait_for(state="visible", timeout=5000)
178+
179+
button_classes = await incognito_locator.get_attribute("class") or ""
180+
181+
if "ms-button-active" in button_classes:
176182
logger.debug("[Model] Temporary chat mode already active")
177183
else:
178184
logger.debug("[Model] Clicking to open temporary chat mode...")
179-
await incognito_button_locator.click(timeout=3000)
185+
await incognito_locator.click(timeout=3000, force=True)
180186
await asyncio.sleep(0.5)
181187

182-
updated_classes = await incognito_button_locator.get_attribute(
183-
"class"
184-
)
185-
if updated_classes and "ms-button-active" in updated_classes:
188+
updated_classes = await incognito_locator.get_attribute("class") or ""
189+
if "ms-button-active" in updated_classes:
186190
logger.debug("[Model] Temporary chat mode enabled")
187191
else:
188-
logger.warning(
189-
"Temporary chat mode state verification failed after click, may not have opened successfully."
190-
)
192+
logger.warning("[Model] Temporary chat mode state verification failed")
193+
194+
# Recovery: Close menu if it remains open
195+
if await menu_trigger.get_attribute("aria-expanded") == "true":
196+
await page.keyboard.press("Escape")
191197

192198
except asyncio.CancelledError:
193199
raise
194200
except Exception as e:
195-
logger.warning(
196-
f"Failed to re-enable temporary chat mode after model switching: {e}"
197-
)
201+
logger.warning(f"Failed to re-enable temporary chat mode after model switching: {e}")
202+
if await menu_trigger.get_attribute("aria-expanded") == "true":
203+
await page.keyboard.press("Escape")
198204

199205
# Invalidate function calling cache on model switch
200206
try:

0 commit comments

Comments
 (0)