Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions agent/auxiliary_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,27 @@ def _resolve_api_key_provider() -> Tuple[Optional[OpenAI], Optional[str]]:
continue

raw_base_url = _pool_runtime_base_url(entry, pconfig.inference_base_url) or pconfig.inference_base_url
# Z.AI manual-pool entries may carry a base_url baked in at
# `hermes auth add` time, before the user knew the right
# endpoint for Coding Plan keys. Re-resolve through
# `_resolve_zai_base_url` so the cached `detected_endpoint`
# from auth.json wins when available, and the probe runs on
# first use otherwise. This brings manual-pool parity with
# the env-seeded path fixed in commit 9e844160f.
if provider_id == "zai":
try:
from hermes_cli.auth import _resolve_zai_base_url as _zrb
_zai_token = _pool_runtime_api_key(entry) or ""
raw_base_url = _zrb(
_zai_token,
raw_base_url,
os.getenv("GLM_BASE_URL", "").strip().rstrip("/"),
) or raw_base_url
except Exception:
logger.debug(
"Z.AI: base_url re-resolution failed, using entry.base_url=%s",
raw_base_url,
)
base_url = _to_openai_base_url(raw_base_url)
model = _get_aux_model_for_provider(provider_id) or None
if model is None:
Expand Down Expand Up @@ -2833,11 +2854,44 @@ def _is_payment_error(exc: Exception) -> bool:
Vertex AI "quota exceeded") are functionally equivalent to credit
exhaustion — the provider cannot serve the request until the quota
resets — and should trigger the same provider-fallback logic.

EXCEPTION: Z.AI GLM Coding Plan errors are per-key rolling quotas
(e.g. 1113 "Insufficient balance or no resource package", 1308
"Usage limit reached for 5 hour"), not wallet-billing exhaustion.
The credential pool rotation layer should handle them — a single
exhausted key must rotate, not trigger a global provider fallback
that takes the whole pool offline. These codes are therefore
explicitly excluded from payment-error classification when they
originate from ``api.z.ai`` (including the ``/api/coding/paas/v4``
endpoint used for Coding Plan subscriptions).
"""
status = getattr(exc, "status_code", None)
if status == 402:
# HTTP 402 is unambiguous payment-required — classify as payment
# error regardless of message body. Z.AI's 1113/1308 surface as
# status 429 / 400 with code fields, not bare 402, so this guard
# does not interfere with the Coding Plan exclusion below.
return True
err_lower = str(exc).lower()
# Z.AI Coding Plan per-key rolling quotas are NOT payment errors.
# These errors should trigger pool rotation (try next key) rather
# than provider fallback (which takes the whole pool offline).
# Codes 1113 ("Insufficient balance or no resource package") and
# 1308 ("Usage limit reached for 5 hour") are rolling quotas, not
# billing issues.
if (
"api.z.ai" in err_lower
or "z.ai/api/coding" in err_lower
or ("zhipuai" in err_lower and "coding" in err_lower)
):
if "1113" in err_lower:
return False
if "1308" in err_lower:
return False
if "no resource package" in err_lower:
return False
if "usage limit reached" in err_lower:
return False
# OpenRouter and other providers include "credits" or "afford" in 402 bodies,
# but sometimes wrap them in 429 or other codes.
# Daily quota exhaustion from Bedrock, Vertex AI, and similar providers
Expand Down Expand Up @@ -5420,6 +5474,14 @@ def _finalize(resolved_provider: str, sync_client: Any, default_model: Optional[
# while the OpenAI wire handles it correctly.
if requested == "zai" and not resolved_base_url:
zai_openai_urls = [
# Z.AI GLM Coding Plan endpoints first: subscription keys
# (id.secret.* format) authenticate here and return 1113
# "no resource package" on the metered endpoint. Probing
# coding first lets vision helpers pick the right route
# without going through credential-pool rotation.
"https://api.z.ai/api/coding/paas/v4",
"https://open.bigmodel.cn/api/coding/paas/v4",
# Metered fallbacks for non-Coding-Plan keys.
"https://open.bigmodel.cn/api/paas/v4",
"https://api.z.ai/api/paas/v4",
]
Expand Down
91 changes: 77 additions & 14 deletions hermes_cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,37 +620,68 @@ def _resolve_api_key_provider_secret(
# may only have access to recent models (glm-5.1, glm-5v-turbo) while older
# ones still use glm-4.7.

# Order chosen by audited key-popularity across 8 production keys:
# anthropic-global (6/8) > coding-global > anthropic-cn (4/8)
# > coding-cn > global > cn.
# The "format" field drives detect_zai_endpoint() body shape selection.
ZAI_ENDPOINTS = [
# (id, base_url, probe_models, label)
("global", "https://api.z.ai/api/paas/v4", ["glm-5"], "Global"),
("cn", "https://open.bigmodel.cn/api/paas/v4", ["glm-5"], "China"),
("coding-global", "https://api.z.ai/api/coding/paas/v4", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-4.7"], "Global (Coding Plan)"),
("coding-cn", "https://open.bigmodel.cn/api/coding/paas/v4", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-4.7"], "China (Coding Plan)"),
("anthropic-global", "https://api.z.ai/api/anthropic", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-5", "glm-4.7"], "Global (Anthropic wire)"),
("coding-global", "https://api.z.ai/api/coding/paas/v4", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-5", "glm-4.7"], "Global (Coding Plan)"),
("anthropic-cn", "https://open.bigmodel.cn/api/anthropic", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-5", "glm-4.7"], "China (Anthropic wire)"),
("coding-cn", "https://open.bigmodel.cn/api/coding/paas/v4", ["glm-5.2", "glm-5.1", "glm-5v-turbo", "glm-5", "glm-4.7"], "China (Coding Plan)"),
("global", "https://api.z.ai/api/paas/v4", ["glm-5"], "Global"),
("cn", "https://open.bigmodel.cn/api/paas/v4", ["glm-5"], "China"),
]


def _zai_probe_path(ep_id: str, base_url: str) -> str:
"""Return the request path for a given Z.AI endpoint, wire-format-aware."""
if ep_id.startswith("anthropic-"):
# Anthropic Messages API path on /api/anthropic
return f"{base_url}/v1/messages"
return f"{base_url}/chat/completions"


def _zai_probe_body(ep_id: str, model: str) -> dict:
"""Return the request body for a given Z.AI endpoint, wire-format-aware."""
if ep_id.startswith("anthropic-"):
return {
"model": model,
"max_tokens": 1,
"messages": [{"role": "user", "content": "ping"}],
}
return {
"model": model,
"stream": False,
"max_tokens": 1,
"messages": [{"role": "user", "content": "ping"}],
}


def detect_zai_endpoint(api_key: str, timeout: float = 8.0) -> Optional[Dict[str, str]]:
"""Probe z.ai endpoints to find one that accepts this API key.

Returns {"id": ..., "base_url": ..., "model": ..., "label": ...} for the
first working endpoint, or None if all fail. For endpoints with multiple
candidate models, tries each in order and returns the first that succeeds.

Supports two wire formats:
- OpenAI chat completions (default, /api/paas/v4 and /api/coding/paas/v4)
- Anthropic Messages (anthropic-* endpoints, /api/anthropic/v1/messages)
"""
for ep_id, base_url, probe_models, label in ZAI_ENDPOINTS:
for model in probe_models:
try:
resp = httpx.post(
f"{base_url}/chat/completions",
_zai_probe_path(ep_id, base_url),
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
# Anthropic wire requires this version header.
**({"anthropic-version": "2023-06-01"} if ep_id.startswith("anthropic-") else {}),
},
json={
"model": model,
"stream": False,
"max_tokens": 1,
"messages": [{"role": "user", "content": "ping"}],
},
json=_zai_probe_body(ep_id, model),
timeout=timeout,
)
if resp.status_code == 200:
Expand All @@ -667,16 +698,48 @@ def detect_zai_endpoint(api_key: str, timeout: float = 8.0) -> Optional[Dict[str
return None


def _configured_zai_base_url() -> str:
"""Return ``model.base_url`` when ``config.yaml`` explicitly selects Z.AI.

Lets users deliberately pick the standard vs Coding Plan endpoint in
config.yaml without setting ``GLM_BASE_URL`` in every profile env file.
Only consulted when ``model.provider`` is a Z.AI alias — prevents a
global ``model.base_url`` for some other provider from leaking into
Z.AI resolution (#58088).
"""
try:
from hermes_cli.config import load_config

cfg = load_config() or {}
except Exception:
return ""
model_cfg = cfg.get("model") if isinstance(cfg, dict) else {}
if not isinstance(model_cfg, dict):
return ""
provider = str(model_cfg.get("provider") or "").strip().lower()
if provider not in {"zai", "glm", "z-ai", "z.ai", "zhipu"}:
return ""
return str(model_cfg.get("base_url") or "").strip().rstrip("/")


def _resolve_zai_base_url(api_key: str, default_url: str, env_override: str) -> str:
"""Return the correct Z.AI base URL by probing endpoints.

If the user has explicitly set GLM_BASE_URL, that always wins.
Otherwise, probe the candidate endpoints to find one that accepts the
key. The detected endpoint is cached in provider state (auth.json) keyed
Precedence (highest first):
1. ``GLM_BASE_URL`` env var (explicit override)
2. ``model.base_url`` from config.yaml when ``model.provider`` is Z.AI
3. cached ``detected_endpoint`` in auth.json (key-hash keyed)
4. live probe of all candidate endpoints
5. ``default_url`` (registry fallback)

The detected endpoint is cached in provider state (auth.json) keyed
on a hash of the API key so subsequent starts skip the probe.
"""
if env_override:
return env_override
config_override = _configured_zai_base_url()
if config_override:
return config_override

# No API key set → don't probe (would fire N×M HTTPS requests with an
# empty Bearer token, all returning 401). This path is hit during
Expand Down
Loading