Spaces:
Paused
Paused
File size: 6,353 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | """
UI State Management
"""
import asyncio
import json
import logging
from playwright.async_api import Page as AsyncPage
from logging_utils import set_request_id
logger = logging.getLogger("AIStudioProxyServer")
async def _verify_ui_state_settings(page: AsyncPage, req_id: str = "unknown") -> dict:
"""
Verify if the UI state settings are correct.
Args:
page: Playwright page object.
req_id: Request ID for logging.
Returns:
dict: A dictionary containing the validation result.
"""
# Don't set lifecycle phase names as request IDs - they appear as ghost prefixes
# Only set actual request IDs (7-char alphanumerics)
if req_id not in ("initial", "set_mod", "set_model", "reload", "unknown", ""):
set_request_id(req_id)
try:
logger.debug("[State] Verifying UI state...")
# Get current localStorage settings
prefs_str = await page.evaluate(
"() => localStorage.getItem('aiStudioUserPreference')"
)
if not prefs_str:
logger.warning("localStorage.aiStudioUserPreference does not exist")
return {
"exists": False,
"isAdvancedOpen": None,
"areToolsOpen": None,
"needsUpdate": True,
"error": "localStorage not found",
}
try:
prefs = json.loads(prefs_str)
is_advanced_open = prefs.get("isAdvancedOpen")
are_tools_open = prefs.get("areToolsOpen")
# Check if update is needed
needs_update = (is_advanced_open is not True) or (
are_tools_open is not True
)
result = {
"exists": True,
"isAdvancedOpen": is_advanced_open,
"areToolsOpen": are_tools_open,
"needsUpdate": needs_update,
"prefs": prefs,
}
if needs_update:
logger.debug(
f"[State] State mismatch: adv={is_advanced_open}, tools={are_tools_open} (update needed)"
)
# No log needed when state is correct
return result
except json.JSONDecodeError as e:
logger.error(f"Failed to parse localStorage JSON: {e}")
return {
"exists": False,
"isAdvancedOpen": None,
"areToolsOpen": None,
"needsUpdate": True,
"error": f"JSON parse failed: {e}",
}
except asyncio.CancelledError:
raise
except Exception as e:
logger.error(f"Error verifying UI state settings: {e}")
return {
"exists": False,
"isAdvancedOpen": None,
"areToolsOpen": None,
"needsUpdate": True,
"error": f"Verification failed: {e}",
}
async def _force_ui_state_settings(page: AsyncPage, req_id: str = "unknown") -> bool:
"""
Forcefully set the UI state.
Args:
page: Playwright page object.
req_id: Request ID for logging.
Returns:
bool: Whether the setting was successful.
"""
try:
logger.debug("[State] Forcefully setting UI state...")
# First verify current state
current_state = await _verify_ui_state_settings(page, req_id)
if not current_state["needsUpdate"]:
logger.debug("[State] State is already correct, no update needed")
return True
# Get existing preferences or create new ones
prefs = current_state.get("prefs", {})
# Force key configurations
prefs["isAdvancedOpen"] = True
prefs["areToolsOpen"] = True
# Save to localStorage
prefs_str = json.dumps(prefs)
await page.evaluate(
"(prefsStr) => localStorage.setItem('aiStudioUserPreference', prefsStr)",
prefs_str,
)
logger.debug("[State] Set: isAdvancedOpen=true, areToolsOpen=true")
# Verify if setting was successful
verify_state = await _verify_ui_state_settings(page, req_id)
if not verify_state["needsUpdate"]:
logger.debug("[State] Setting verification successful")
return True
else:
logger.warning("UI state setting verification failed, may need to retry")
return False
except asyncio.CancelledError:
raise
except Exception as e:
logger.error(f"Error forcefully setting UI state: {e}")
return False
async def _force_ui_state_with_retry(
page: AsyncPage,
req_id: str = "unknown",
max_retries: int = 3,
retry_delay: float = 1.0,
) -> bool:
"""
Forcefully set UI state with retry mechanism.
Args:
page: Playwright page object.
req_id: Request ID for logging.
max_retries: Maximum number of retries.
retry_delay: Retry delay (seconds).
Returns:
bool: Whether the setting was ultimately successful.
"""
for attempt in range(1, max_retries + 1):
success = await _force_ui_state_settings(page, req_id)
if success:
return True
if attempt < max_retries:
logger.debug(f"[State] Retrying {attempt}/{max_retries}...")
await asyncio.sleep(retry_delay)
else:
logger.warning(f"[State] Still failed after {max_retries} attempts")
return False
async def _verify_and_apply_ui_state(page: AsyncPage, req_id: str = "unknown") -> bool:
"""
Full process of verifying and applying UI state settings.
Args:
page: Playwright page object.
req_id: Request ID for logging.
Returns:
bool: Whether the operation was successful.
"""
try:
logger.debug("[State] Starting to verify and apply UI state...")
# First verify current state
state = await _verify_ui_state_settings(page, req_id)
if state["needsUpdate"]:
logger.debug("[State] Update needed, applying forced settings...")
return await _force_ui_state_with_retry(page, req_id)
else:
return True
except asyncio.CancelledError:
raise
except Exception as e:
logger.error(f"Error during verifying and applying UI state: {e}")
return False
|