2323)
2424from models import ClientDisconnectedError
2525from .operations import save_error_snapshot , _wait_for_response_completion , _get_final_response_content
26+ from .initialization import enable_temporary_chat_mode
2627
2728class PageController :
2829 """封装了与AI Studio页面交互的所有操作。"""
@@ -572,6 +573,8 @@ async def clear_chat_history(self, check_client_disconnected: Callable):
572573 if can_attempt_clear :
573574 await self ._execute_chat_clear (clear_chat_button_locator , confirm_button_locator , overlay_locator , check_client_disconnected )
574575 await self ._verify_chat_cleared (check_client_disconnected )
576+ self .logger .info (f"[{ self .req_id } ] 聊天已清空,重新启用 '临时聊天' 模式..." )
577+ await enable_temporary_chat_mode (self .page )
575578
576579 except Exception as e_clear :
577580 self .logger .error (f"[{ self .req_id } ] 清空聊天过程中发生错误: { e_clear } " )
@@ -580,21 +583,78 @@ async def clear_chat_history(self, check_client_disconnected: Callable):
580583 raise
581584
582585 async def _execute_chat_clear (self , clear_chat_button_locator , confirm_button_locator , overlay_locator , check_client_disconnected : Callable ):
583- """执行清空聊天操作 (适配新版UI,无确认对话框)"""
584- self .logger .info (f"[{ self .req_id } ] 点击\" 清空聊天\" 按钮: { CLEAR_CHAT_BUTTON_SELECTOR } " )
585-
586+ """执行清空聊天操作"""
587+ overlay_initially_visible = False
586588 try :
589+ if await overlay_locator .is_visible (timeout = 1000 ):
590+ overlay_initially_visible = True
591+ self .logger .info (f"[{ self .req_id } ] 清空聊天确认遮罩层已可见。直接点击\" 继续\" 。" )
592+ except TimeoutError :
593+ self .logger .info (f"[{ self .req_id } ] 清空聊天确认遮罩层初始不可见 (检查超时或未找到)。" )
594+ overlay_initially_visible = False
595+ except Exception as e_vis_check :
596+ self .logger .warning (f"[{ self .req_id } ] 检查遮罩层可见性时发生错误: { e_vis_check } 。假定不可见。" )
597+ overlay_initially_visible = False
598+
599+ await self ._check_disconnect (check_client_disconnected , "清空聊天 - 初始遮罩层检查后" )
600+
601+ if overlay_initially_visible :
602+ self .logger .info (f"[{ self .req_id } ] 点击\" 继续\" 按钮 (遮罩层已存在): { CLEAR_CHAT_CONFIRM_BUTTON_SELECTOR } " )
603+ await confirm_button_locator .click (timeout = CLICK_TIMEOUT_MS )
604+ else :
605+ self .logger .info (f"[{ self .req_id } ] 点击\" 清空聊天\" 按钮: { CLEAR_CHAT_BUTTON_SELECTOR } " )
587606 await clear_chat_button_locator .click (timeout = CLICK_TIMEOUT_MS )
588607 await self ._check_disconnect (check_client_disconnected , "清空聊天 - 点击\" 清空聊天\" 后" )
589-
590- await asyncio .sleep (0.5 )
591-
592- self .logger .info (f"[{ self .req_id } ] ✅ '清空聊天'按钮已点击。新版UI无确认步骤,操作完成。" )
593608
594- except Exception as e :
595- self .logger .error (f"[{ self .req_id } ] 点击'清空聊天'按钮时发生错误: { e } " )
596- raise
597-
609+ try :
610+ self .logger .info (f"[{ self .req_id } ] 等待清空聊天确认遮罩层出现: { OVERLAY_SELECTOR } " )
611+ await expect_async (overlay_locator ).to_be_visible (timeout = WAIT_FOR_ELEMENT_TIMEOUT_MS )
612+ self .logger .info (f"[{ self .req_id } ] 清空聊天确认遮罩层已出现。" )
613+ except TimeoutError :
614+ error_msg = f"等待清空聊天确认遮罩层超时 (点击清空按钮后)。请求 ID: { self .req_id } "
615+ self .logger .error (error_msg )
616+ await save_error_snapshot (f"clear_chat_overlay_timeout_{ self .req_id } " )
617+ raise Exception (error_msg )
618+
619+ await self ._check_disconnect (check_client_disconnected , "清空聊天 - 遮罩层出现后" )
620+ self .logger .info (f"[{ self .req_id } ] 点击\" 继续\" 按钮 (在对话框中): { CLEAR_CHAT_CONFIRM_BUTTON_SELECTOR } " )
621+ await confirm_button_locator .click (timeout = CLICK_TIMEOUT_MS )
622+
623+ await self ._check_disconnect (check_client_disconnected , "清空聊天 - 点击\" 继续\" 后" )
624+
625+ # 等待对话框消失
626+ max_retries_disappear = 3
627+ for attempt_disappear in range (max_retries_disappear ):
628+ try :
629+ self .logger .info (f"[{ self .req_id } ] 等待清空聊天确认按钮/对话框消失 (尝试 { attempt_disappear + 1 } /{ max_retries_disappear } )..." )
630+ await expect_async (confirm_button_locator ).to_be_hidden (timeout = CLEAR_CHAT_VERIFY_TIMEOUT_MS )
631+ await expect_async (overlay_locator ).to_be_hidden (timeout = 1000 )
632+ self .logger .info (f"[{ self .req_id } ] ✅ 清空聊天确认对话框已成功消失。" )
633+ break
634+ except TimeoutError :
635+ self .logger .warning (f"[{ self .req_id } ] ⚠️ 等待清空聊天确认对话框消失超时 (尝试 { attempt_disappear + 1 } /{ max_retries_disappear } )。" )
636+ if attempt_disappear < max_retries_disappear - 1 :
637+ await asyncio .sleep (1.0 )
638+ await self ._check_disconnect (check_client_disconnected , f"清空聊天 - 重试消失检查 { attempt_disappear + 1 } 前" )
639+ continue
640+ else :
641+ error_msg = f"达到最大重试次数。清空聊天确认对话框未消失。请求 ID: { self .req_id } "
642+ self .logger .error (error_msg )
643+ await save_error_snapshot (f"clear_chat_dialog_disappear_timeout_{ self .req_id } " )
644+ raise Exception (error_msg )
645+ except ClientDisconnectedError :
646+ self .logger .info (f"[{ self .req_id } ] 客户端在等待清空确认对话框消失时断开连接。" )
647+ raise
648+ except Exception as other_err :
649+ self .logger .warning (f"[{ self .req_id } ] 等待清空确认对话框消失时发生其他错误: { other_err } " )
650+ if attempt_disappear < max_retries_disappear - 1 :
651+ await asyncio .sleep (1.0 )
652+ continue
653+ else :
654+ raise
655+
656+ await self ._check_disconnect (check_client_disconnected , f"清空聊天 - 消失检查尝试 { attempt_disappear + 1 } 后" )
657+
598658 async def _verify_chat_cleared (self , check_client_disconnected : Callable ):
599659 """验证聊天已清空"""
600660 last_response_container = self .page .locator (RESPONSE_CONTAINER_SELECTOR ).last
0 commit comments