Resolve the latest X-FE-Version value from chat.z.ai. The lookup order is: 1. Cached value within TTL. 2. Remote fetch from chat.z.ai. Raises: Exception: If unable to fetch the version from the remote source.
(force_refresh: bool = False)
| 62 | |
| 63 | |
| 64 | def get_latest_fe_version(force_refresh: bool = False) -> str: |
| 65 | """ |
| 66 | Resolve the latest X-FE-Version value from chat.z.ai. |
| 67 | |
| 68 | The lookup order is: |
| 69 | 1. Cached value within TTL. |
| 70 | 2. Remote fetch from chat.z.ai. |
| 71 | |
| 72 | Raises: |
| 73 | Exception: If unable to fetch the version from the remote source. |
| 74 | """ |
| 75 | global _cached_version, _cached_at |
| 76 | |
| 77 | if _should_use_cache(force_refresh): |
| 78 | return _cached_version |
| 79 | |
| 80 | try: |
| 81 | headers = {"User-Agent": get_random_user_agent("chrome")} |
| 82 | except Exception: |
| 83 | headers = { |
| 84 | "User-Agent": ( |
| 85 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " |
| 86 | "AppleWebKit/537.36 (KHTML, like Gecko) " |
| 87 | "Chrome/120.0.0.0 Safari/537.36" |
| 88 | ) |
| 89 | } |
| 90 | |
| 91 | try: |
| 92 | with httpx.Client(timeout=10.0, follow_redirects=True) as client: |
| 93 | response = client.get(FE_VERSION_SOURCE_URL, headers=headers) |
| 94 | response.raise_for_status() |
| 95 | version = _extract_version(response.text) |
| 96 | if version: |
| 97 | if version != _cached_version: |
| 98 | _logger.info(f"[Z.AI] Detected X-FE-Version update: {version}") |
| 99 | _cached_version = version |
| 100 | _cached_at = time.time() |
| 101 | return version |
| 102 | |
| 103 | _logger.error("[Z.AI] Unable to locate X-FE-Version in landing page") |
| 104 | raise Exception("Unable to locate X-FE-Version in landing page") |
| 105 | except Exception as exc: |
| 106 | _logger.error(f"[Z.AI] Failed to fetch X-FE-Version from {FE_VERSION_SOURCE_URL}: {exc}") |
| 107 | raise Exception(f"Failed to fetch X-FE-Version: {exc}") |
| 108 | |
| 109 | |
| 110 | def refresh_fe_version() -> str: |
no test coverage detected