auth_zai_kimi.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Kimi Code and Z.AI endpoint auto-detection, LM Studio base-URL normalization.
  2. Re-exported from ``hermes_cli/auth.py`` (patch targets unchanged); origin helpers are imported
  3. lazily per function so ``hermes_cli.auth.<helper>`` patches still intercept and no cycle forms.
  4. """
  5. from __future__ import annotations
  6. import logging
  7. import hashlib
  8. import time
  9. from typing import Dict, Optional
  10. from hermes_cli.auth_constants import httpx
  11. logger = logging.getLogger("hermes_cli.auth")
  12. # In-process negative cache for Z.AI endpoint detection, keyed by key hash: a failed probe is not
  13. # retried for this long (a success persists to auth.json instead).
  14. _ZAI_PROBE_FAILURE_TTL_SECONDS = 300
  15. _zai_probe_failed_until: dict[str, float] = {}
  16. # "sk-kimi-" keys only work on api.kimi.com/coding; legacy moonshot keys use the old default.
  17. # NO /v1 suffix: the anthropic SDK appends "/v1/messages" itself ("/coding/v1" would 404).
  18. KIMI_CODE_BASE_URL = "https://api.kimi.com/coding"
  19. def _resolve_kimi_base_url(api_key: str, default_url: str, env_override: str) -> str:
  20. """Kimi base URL from the key prefix; an explicit KIMI_BASE_URL always wins."""
  21. if env_override:
  22. return env_override
  23. if api_key and api_key.startswith("sk-kimi-"):
  24. return KIMI_CODE_BASE_URL
  25. return default_url
  26. # Z.AI bills general/coding plans and global/China endpoints separately ("Insufficient balance" on
  27. # the wrong one), so probe once and cache. Candidate models are tried in order: newer coding-plan
  28. # accounts may only have recent GLM slugs, older ones still glm-4.7.
  29. _ZAI_CODING_PROBE_MODELS = ["glm-5.3", "glm-5.3-flash", "glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-4.7"]
  30. ZAI_ENDPOINTS = [
  31. # (id, base_url, probe_models, label)
  32. ("global", "https://api.z.ai/api/paas/v4", ["glm-5"], "Global"),
  33. ("cn", "https://open.bigmodel.cn/api/paas/v4", ["glm-5"], "China"),
  34. ("coding-global", "https://api.z.ai/api/coding/paas/v4", _ZAI_CODING_PROBE_MODELS, "Global (Coding Plan)"),
  35. ("coding-cn", "https://open.bigmodel.cn/api/coding/paas/v4", _ZAI_CODING_PROBE_MODELS, "China (Coding Plan)"),
  36. ]
  37. def _probe_single_zai_endpoint(api_key: str, endpoint: tuple, timeout: float) -> Optional[dict[str, str]]:
  38. """Probe one Z.AI endpoint, trying its candidate models in order; None when none succeeds."""
  39. ep_id, base_url, probe_models, label = endpoint
  40. for model in probe_models:
  41. try:
  42. resp = httpx.post(
  43. f"{base_url}/chat/completions",
  44. headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
  45. json={"model": model, "stream": False, "max_tokens": 1, "messages": [{"role": "user", "content": "ping"}]},
  46. timeout=timeout,
  47. )
  48. if resp.status_code == 200:
  49. logger.debug("Z.AI endpoint probe: %s (%s) model=%s OK", ep_id, base_url, model)
  50. return {"id": ep_id, "base_url": base_url, "model": model, "label": label}
  51. logger.debug("Z.AI endpoint probe: %s model=%s returned %s", ep_id, model, resp.status_code)
  52. except Exception as exc:
  53. logger.debug("Z.AI endpoint probe: %s model=%s failed: %s", ep_id, model, exc)
  54. return None
  55. def detect_zai_endpoint(api_key: str, timeout: float = 8.0) -> Optional[dict[str, str]]:
  56. """Probe z.ai endpoints in parallel; first working one in ZAI_ENDPOINTS priority order, or None."""
  57. from concurrent.futures import ThreadPoolExecutor, as_completed
  58. # No `with`: it would join ALL probes on exit, defeating the early return below.
  59. pool = ThreadPoolExecutor(max_workers=len(ZAI_ENDPOINTS))
  60. try:
  61. futures = {pool.submit(_probe_single_zai_endpoint, api_key, ep, timeout): ep[0] for ep in ZAI_ENDPOINTS}
  62. by_id = {ep_id: f for f, ep_id in futures.items()}
  63. results: dict[str, dict[str, str]] = {}
  64. def _first_ready(require_done: bool) -> Optional[dict[str, str]]:
  65. # Walk endpoints in PRIORITY order; a lower-priority success only wins once every
  66. # higher-priority probe has finished without success.
  67. for ep in ZAI_ENDPOINTS:
  68. if require_done and not by_id[ep[0]].done():
  69. return None # a higher-priority probe is still in flight
  70. if ep[0] in results:
  71. return results[ep[0]]
  72. return None
  73. for future in as_completed(futures):
  74. try:
  75. result = future.result()
  76. if result is not None:
  77. results[futures[future]] = result
  78. except Exception:
  79. pass
  80. winner = _first_ready(require_done=True)
  81. if winner is not None:
  82. return winner
  83. return _first_ready(require_done=False)
  84. finally:
  85. pool.shutdown(wait=False)
  86. def _resolve_zai_base_url(api_key: str, default_url: str, env_override: str) -> str:
  87. """Z.AI base URL by probing endpoints; an explicit GLM_BASE_URL always wins.
  88. The detected endpoint is cached in provider state (auth.json) keyed on a hash of the API key so
  89. subsequent starts skip the probe.
  90. """
  91. from hermes_cli.auth import _auth_store_lock, _load_auth_store, _load_provider_state, _save_auth_store, _store_provider_state, detect_zai_endpoint
  92. if env_override:
  93. return env_override
  94. # No key -> don't probe (N×M 401s); auxiliary-client auto-detection hits this for everyone.
  95. if not api_key:
  96. return default_url
  97. key_hash = hashlib.sha256(api_key.encode()).hexdigest()[:16]
  98. state = _load_provider_state(_load_auth_store(), "zai") or {}
  99. cached = state.get("detected_endpoint")
  100. if isinstance(cached, dict) and cached.get("base_url") and cached.get("key_hash", "") == key_hash:
  101. logger.debug("Z.AI: using cached endpoint %s", cached["base_url"])
  102. return cached["base_url"]
  103. # Only a success is persisted, so a failing key (429/401 on every endpoint) would re-run the
  104. # four chat-completion probes on every credential-pool load — dozens of times per picker open.
  105. if _zai_probe_failed_until.get(key_hash, 0.0) > time.time():
  106. return default_url
  107. # Probe — may take up to ~8s per endpoint.
  108. detected = detect_zai_endpoint(api_key)
  109. if not (detected and detected.get("base_url")):
  110. logger.debug("Z.AI: probe failed, falling back to default %s", default_url)
  111. _zai_probe_failed_until[key_hash] = time.time() + _ZAI_PROBE_FAILURE_TTL_SECONDS
  112. return default_url
  113. detected_endpoint = {
  114. "base_url": detected["base_url"], "endpoint_id": detected.get("id", ""),
  115. "model": detected.get("model", ""), "label": detected.get("label", ""),
  116. "key_hash": key_hash,
  117. }
  118. # Persist failure must not break resolution; worst case the next start re-probes.
  119. try:
  120. with _auth_store_lock():
  121. auth_store = _load_auth_store() # reload under lock to avoid overwriting concurrent changes
  122. state_under_lock = _load_provider_state(auth_store, "zai") or {}
  123. state_under_lock["detected_endpoint"] = detected_endpoint
  124. # set_active=False: runs from credential-pool env seeding; must not flip active provider.
  125. _store_provider_state(auth_store, "zai", state_under_lock, set_active=False)
  126. _save_auth_store(auth_store)
  127. except Exception as exc:
  128. logger.warning("Z.AI: could not persist detected endpoint (%s); will re-probe next start", exc)
  129. logger.info("Z.AI: auto-detected endpoint %s (%s)", detected["label"], detected["base_url"])
  130. return detected["base_url"]
  131. def _normalize_lmstudio_runtime_base_url(base_url: str) -> str:
  132. """Return the OpenAI-compatible LM Studio runtime base URL.
  133. LM Studio's native management API lives under ``/api/v1`` while its OpenAI-compatible chat
  134. endpoint lives under ``/v1``; users paste either form, so normalize before the SDK appends
  135. ``/chat/completions``.
  136. """
  137. root = str(base_url or "").strip().rstrip("/")
  138. for suffix in ("/api/v1", "/api", "/v1"):
  139. if root.endswith(suffix):
  140. root = root[: -len(suffix)].rstrip("/")
  141. break
  142. return (root or "http://127.0.0.1:1234") + "/v1"