@@ -274,27 +274,49 @@ async def switch_ai_studio_model(page: AsyncPage, model_id: str, req_id: str) ->
274274 if m_obj .get ("id" ) == model_id :
275275 expected_display_name_for_target_id = m_obj .get ("display_name" )
276276 break
277+
278+ try :
279+ model_name_locator = page .locator ('[data-test-id="model-name"]' )
280+ actual_displayed_model_id_on_page_raw = await model_name_locator .first .inner_text (timeout = 5000 )
281+ actual_displayed_model_id_on_page = actual_displayed_model_id_on_page_raw .strip ()
282+
283+ target_model_id = model_id
284+
285+ if actual_displayed_model_id_on_page == target_model_id :
286+ page_display_match = True
287+ logger .info (f"[{ req_id } ] ✅ 页面显示模型ID ('{ actual_displayed_model_id_on_page } ') 与期望ID ('{ target_model_id } ') 一致。" )
288+ else :
289+ page_display_match = False
290+ logger .error (f"[{ req_id } ] ❌ 页面显示模型ID ('{ actual_displayed_model_id_on_page } ') 与期望ID ('{ target_model_id } ') 不一致。" )
277291
278- if not expected_display_name_for_target_id :
279- logger .warning (f"[{ req_id } ] 无法在parsed_model_list中找到目标ID '{ model_id } ' 的显示名称,跳过页面显示名称验证。这可能不准确。" )
280- page_display_match = True
281- else :
292+ except Exception as e_disp :
293+ page_display_match = False # 读取失败则认为不匹配
294+ logger .warning (f"[{ req_id } ] 读取页面显示的当前模型ID时出错: { e_disp } 。将无法验证页面显示。" )
295+
296+ if page_display_match :
282297 try :
283- model_name_locator = page .locator ('mat-select[data-test-ms-model-selector] .model-option-content span' )
284- actual_displayed_model_name_on_page_raw = await model_name_locator .first .inner_text (timeout = 5000 )
285- actual_displayed_model_name_on_page = actual_displayed_model_name_on_page_raw .strip ()
286- normalized_actual_display = actual_displayed_model_name_on_page .lower ()
287- normalized_expected_display = expected_display_name_for_target_id .strip ().lower ()
298+ logger .info (f"[{ req_id } ] 模型切换成功,重新启用 '临时聊天' 模式..." )
299+ incognito_button_locator = page .locator ('button[aria-label="Temporary chat toggle"]' )
288300
289- if normalized_actual_display == normalized_expected_display :
290- page_display_match = True
291- logger .info (f"[{ req_id } ] ✅ 页面显示模型 ('{ actual_displayed_model_name_on_page } ') 与期望 ('{ expected_display_name_for_target_id } ') 一致。" )
301+ await incognito_button_locator .wait_for (state = "visible" , timeout = 5000 )
302+
303+ button_classes = await incognito_button_locator .get_attribute ("class" )
304+
305+ if button_classes and 'ms-button-active' in button_classes :
306+ logger .info (f"[{ req_id } ] '临时聊天' 模式已处于激活状态。" )
292307 else :
293- logger .error (f"[{ req_id } ] ❌ 页面显示模型 ('{ actual_displayed_model_name_on_page } ') 与期望 ('{ expected_display_name_for_target_id } ') 不一致。(Raw page: '{ actual_displayed_model_name_on_page_raw } ')" )
294- except Exception as e_disp :
295- logger .warning (f"[{ req_id } ] 读取页面显示的当前模型名称时出错: { e_disp } 。将无法验证页面显示。" )
296-
297- if page_display_match :
308+ logger .info (f"[{ req_id } ] '临时聊天' 模式未激活,正在点击以开启..." )
309+ await incognito_button_locator .click (timeout = 3000 )
310+ await asyncio .sleep (0.5 )
311+
312+ updated_classes = await incognito_button_locator .get_attribute ("class" )
313+ if updated_classes and 'ms-button-active' in updated_classes :
314+ logger .info (f"[{ req_id } ] ✅ '临时聊天' 模式已成功重新启用。" )
315+ else :
316+ logger .warning (f"[{ req_id } ] ⚠️ 点击后 '临时聊天' 模式状态验证失败,可能未成功重新开启。" )
317+
318+ except Exception as e :
319+ logger .warning (f"[{ req_id } ] ⚠️ 模型切换后重新启用 '临时聊天' 模式失败: { e } " )
298320 return True
299321 else :
300322 logger .error (f"[{ req_id } ] ❌ 模型切换失败,因为页面显示的模型与期望不符 (即使localStorage可能已更改)。" )
@@ -306,7 +328,7 @@ async def switch_ai_studio_model(page: AsyncPage, model_id: str, req_id: str) ->
306328 current_displayed_name_for_revert_stripped = "无法读取"
307329
308330 try :
309- model_name_locator_revert = page .locator ('mat-select [data-test-ms- model-selector] .model-option-content span ' )
331+ model_name_locator_revert = page .locator ('[data-test-id=" model-name"] ' )
310332 current_displayed_name_for_revert_raw = await model_name_locator_revert .first .inner_text (timeout = 5000 )
311333 current_displayed_name_for_revert_stripped = current_displayed_name_for_revert_raw .strip ()
312334 logger .info (f"[{ req_id } ] 恢复:页面当前显示的模型名称 (原始: '{ current_displayed_name_for_revert_raw } ', 清理后: '{ current_displayed_name_for_revert_stripped } ')" )
@@ -324,17 +346,9 @@ async def switch_ai_studio_model(page: AsyncPage, model_id: str, req_id: str) ->
324346 return False
325347
326348 model_id_to_revert_to = None
327- if parsed_model_list and current_displayed_name_for_revert_stripped != "无法读取" :
328- normalized_current_display_for_revert = current_displayed_name_for_revert_stripped .lower ()
329- for m_obj in parsed_model_list :
330- parsed_list_display_name = m_obj .get ("display_name" , "" ).strip ().lower ()
331- if parsed_list_display_name == normalized_current_display_for_revert :
332- model_id_to_revert_to = m_obj .get ("id" )
333- logger .info (f"[{ req_id } ] 恢复:页面显示名称 '{ current_displayed_name_for_revert_stripped } ' 对应模型ID: { model_id_to_revert_to } " )
334- break
335-
336- if not model_id_to_revert_to :
337- logger .warning (f"[{ req_id } ] 恢复:无法在 parsed_model_list 中找到与页面显示名称 '{ current_displayed_name_for_revert_stripped } ' 匹配的模型ID。" )
349+ if current_displayed_name_for_revert_stripped != "无法读取" :
350+ model_id_to_revert_to = current_displayed_name_for_revert_stripped
351+ logger .info (f"[{ req_id } ] 恢复:页面当前显示的ID是 '{ model_id_to_revert_to } ',将直接用于恢复。" )
338352 else :
339353 if current_displayed_name_for_revert_stripped == "无法读取" :
340354 logger .warning (f"[{ req_id } ] 恢复:因无法读取页面显示名称,故不能从 parsed_model_list 转换ID。" )
@@ -529,7 +543,7 @@ async def _set_model_from_page_display(page: AsyncPage, set_storage: bool = Fals
529543
530544 try :
531545 logger .info (" 尝试从页面显示元素读取当前模型名称..." )
532- model_name_locator = page .locator ('mat-select [data-test-ms- model-selector] .model-option-content span ' )
546+ model_name_locator = page .locator ('[data-test-id=" model-name"] ' )
533547 displayed_model_name_from_page_raw = await model_name_locator .first .inner_text (timeout = 7000 )
534548 displayed_model_name = displayed_model_name_from_page_raw .strip ()
535549 logger .info (f" 页面当前显示模型名称 (原始: '{ displayed_model_name_from_page_raw } ', 清理后: '{ displayed_model_name } ')" )
@@ -542,19 +556,10 @@ async def _set_model_from_page_display(page: AsyncPage, set_storage: bool = Fals
542556 except asyncio .TimeoutError :
543557 logger .warning (" 等待模型列表超时,可能无法准确转换显示名称为ID。" )
544558
545- if parsed_model_list :
546- for model_obj in parsed_model_list :
547- if model_obj .get ("display_name" ) and model_obj .get ("display_name" ).strip () == displayed_model_name :
548- found_model_id_from_display = model_obj .get ("id" )
549- logger .info (f" 显示名称 '{ displayed_model_name } ' 对应模型 ID: { found_model_id_from_display } " )
550- break
551-
552- if not found_model_id_from_display :
553- logger .warning (f" 未在已知模型列表中找到与显示名称 '{ displayed_model_name } ' 匹配的 ID。" )
554- else :
555- logger .warning (" 模型列表尚不可用,无法将显示名称转换为ID。" )
559+ found_model_id_from_display = displayed_model_name
560+ logger .info (f" 页面显示的直接是模型ID: '{ found_model_id_from_display } '" )
556561
557- new_model_value = found_model_id_from_display if found_model_id_from_display else displayed_model_name
562+ new_model_value = found_model_id_from_display
558563 if server .current_ai_studio_model_id != new_model_value :
559564 server .current_ai_studio_model_id = new_model_value
560565 logger .info (f" 全局 current_ai_studio_model_id 已更新为: { server .current_ai_studio_model_id } " )
0 commit comments