Skip to content

fix: Zhipu quota host + multi-window limits; OpenAI OAuth auto-refresh - #29

Open
wirsbf wants to merge 2 commits into
vbgate:mainfrom
wirsbf:fix/zhipu-host-openai-refresh
Open

wirsbf wants to merge 2 commits into
vbgate:mainfrom
wirsbf:fix/zhipu-host-openai-refresh

Conversation

@wirsbf

@wirsbf wirsbf commented Sep 26, 2026

Copy link
Copy Markdown

Problems

  1. Zhipu quota always fails with code 1000 身份验证失败: ZHIPU_QUOTA_QUERY_URL points to https://bigmodel.cn — that is the web-console origin (cookie auth). The API origin for the same path is https://open.bigmodel.cn (matches Z.ai's official usage-query plugin, zai-org/zai-coding-plugins, and community tools tokn / cc-switch / CodexBar).
  2. New plans render nothing/wrong: formatter only reads the first TOKENS_LIMIT. Since the 2026-07-30 credit revision plans report CREDIT_LIMIT and return two windows — 5-hour rolling (unit==3) + weekly (unit==6) — so weekly usage was invisible and credit plans showed nothing.
  3. OpenAI dead-ends on expiry: with an expired access token the tool errors out even though auth.json holds a valid refresh token.

Changes

  • zhipu.ts: correct host; Accept-Language header; render all windows (5h tokens/credits + weekly + MCP TIME_LIMIT), plan level in the title, precise percentage from currentValue/usage when available (server percentage is floor'ed); on auth failure, one retry against the other host (keys are host-specific — a zhipuai key only works on open.bigmodel.cn, a z.ai key only on api.z.ai).
  • openai.ts: refresh via POST https://auth.openai.com/oauth/token (grant_type=refresh_token, client_id app_EMoamEEZ73f0CkXaXp7hrann — the same public client OpenCode and codex-rs use), on expiry or HTTP 401; the rotated refresh token is persisted atomically to auth.json (temp+rename, mode 0600, other providers preserved); refresh failures return an actionable re-login hint.
  • Google ENOENT skip intentionally omitted — covered by fix: skip Google quota check when Antigravity is not configured #21.

Evidence

  • Endpoint/auth contract cross-verified against zai-org/zai-coding-plugins (official), agentic-rs/tokn quota.rs, farion1231/cc-switch coding_plan.rs, steipete/CodexBar zai.js; Authorization: <raw token> primary (Bearer also accepted).
  • Refresh flow mirrors sst/opencode packages/opencode/src/plugin/openai/codex.ts (same client_id, form-encoded body, rotated rt persisted back).
  • Live-tested before porting: correct host + key returns level + both windows; expired lineage returns clean invalid_refresh_token + re-login message.

Testing

  • npm run typecheck, npm run build, npm run lint all pass.
  • Offline fetch-stub test renders legacy (single TOKENS_LIMIT) and new (CREDIT_LIMIT 5h + weekly + TIME_LIMIT, level) fixtures correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant