feat(server): report Z.ai coding quota through OpenCode - #11178
kolbydayley wants to merge 1 commit into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
|
||
| const PROVIDER_ID = "zai-coding-plan"; | ||
| const CODING_URL = "https://api.z.ai/api/coding/paas/v4"; | ||
| const QUOTA_URL = "https://api.z.ai/api/monitor/usage/quota/limit"; |
There was a problem hiding this comment.
🟠 High Layers/zaiUsageLimits.ts:17
Current Z.ai Coding Plan keys receive 401 from /api/monitor/usage/quota/limit, so this request is converted to probeFailed and no quota windows are published. Use /api/monitor/usage, which serves the live quota response.
| const QUOTA_URL = "https://api.z.ai/api/monitor/usage/quota/limit"; | |
| const QUOTA_URL = "https://api.z.ai/api/monitor/usage"; |
🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/provider/Layers/zaiUsageLimits.ts around line 17:
Current Z.ai Coding Plan keys receive `401` from `/api/monitor/usage/quota/limit`, so this request is converted to `probeFailed` and no quota windows are published. Use `/api/monitor/usage`, which serves the live quota response.
| }); | ||
|
|
||
| function quotaWindow(limit: typeof Limit.Type): ServerProviderUsageWindow | undefined { | ||
| if (limit.type !== "TOKENS_LIMIT" && limit.type !== "TIME_LIMIT") return undefined; |
There was a problem hiding this comment.
🟠 High Layers/zaiUsageLimits.ts:32
quotaWindow discards CREDIT_LIMIT rows, so current Z.ai Coding Plan responses containing the five-hour and weekly allowances produce no supported windows and are reported as unsupported instead of displaying the account's limits. Include CREDIT_LIMIT as a supported limit type.
- if (limit.type !== "TOKENS_LIMIT" && limit.type !== "TIME_LIMIT") return undefined;
+ if (limit.type !== "TOKENS_LIMIT" && limit.type !== "TIME_LIMIT" && limit.type !== "CREDIT_LIMIT") return undefined;🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/provider/Layers/zaiUsageLimits.ts around line 32:
`quotaWindow` discards `CREDIT_LIMIT` rows, so current Z.ai Coding Plan responses containing the five-hour and weekly allowances produce no supported windows and are reported as `unsupported` instead of displaying the account's limits. Include `CREDIT_LIMIT` as a supported limit type.
ApprovabilityVerdict: Not approved Macroscope's review found this PR not approvable — This PR adds a new production Z.ai quota integration, including authenticated outbound requests and user-facing usage windows in OpenCode provider snapshots. The implementation also has unresolved high-severity findings concerning the live endpoint and response limit types, so the behavior and credential flow need human review. Not approved because:
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2e55b98288
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| installed: true, | ||
| version, | ||
| status: connectedCount > 0 ? "ready" : "warning", | ||
| ...(usageLimits ? { usageLimits } : {}), |
There was a problem hiding this comment.
Scope Z.ai quota to Z.ai-backed models
When an OpenCode instance has another upstream connected alongside Z.ai, this attaches Z.ai's quota to the entire OpenCode provider snapshot. Both web and mobile offer /usage-limits and build its report using only the OpenCode driver/instance (packages/shared/src/usageLimits.ts:639-649), without inspecting the selected model slug, so a thread using an Anthropic, OpenAI, or other non-Z.ai model through that instance is shown Z.ai quota as the current model's limits. Publish or advertise these limits with upstream-model scope instead.
Useful? React with 👍 / 👎.
| const usageLimits = yield* readOpenCodeZaiUsageLimits( | ||
| inventoryExit.value.inventory.providerList, | ||
| checkedAt, | ||
| ); |
There was a problem hiding this comment.
Deduplicate the account-wide Z.ai subscription
When the same Z.ai account is connected through two environments or OpenCode instances, these account-wide windows carry no stable account identity: the resulting OpenCode auth object supplies only status/type, so collectLimitAccounts falls back to ${environmentId}:${provider.instanceId} at packages/shared/src/usageLimits.ts:257-258 and pools each copy as a separate account. This can double the displayed available quota even though both snapshots describe the same subscription; provide an identity usable for deduplication or otherwise prevent duplicate Z.ai subscriptions from being summed.
AGENTS.md reference: AGENTS.md:L74-L74
Useful? React with 👍 / 👎.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (6)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughOpenCode provider checks now retrieve Z.ai Coding Plan usage limits through the Z.ai quota endpoint. The limits are mapped into provider snapshots, unavailable probes preserve provider readiness, credentials remain excluded from snapshots, and documentation describes the reported quota windows. ChangesZ.ai quota reporting
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant OpenCodeDriver
participant OpenCodeProvider
participant HttpClient
participant ZAIQuotaEndpoint
OpenCodeDriver->>OpenCodeProvider: checkProvider
OpenCodeProvider->>HttpClient: request Z.ai usage limits
HttpClient->>ZAIQuotaEndpoint: send bearer-authenticated quota request
ZAIQuotaEndpoint-->>HttpClient: return quota response
HttpClient-->>OpenCodeProvider: return mapped usage limits
OpenCodeProvider-->>OpenCodeDriver: publish provider snapshot
Suggested reviewers: Merge Risk: ⚪ Minimal · up to The quota integration has no substantiated merge-blocking issue and is ready for normal validation and merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 5 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
Thanks for the PR. We're not taking changes to the orchestration and provider layers right now: that part of the server is being rewritten for V2, and merging into the current code would either conflict with or be thrown away by that work. Closing for now. If this is still an issue once V2 lands, please reopen (or open a fresh PR against the new code) and we'll take a proper look. |
What Changed
OpenCode users authenticated with Z.AI Coding Plan can run GLM turns, but their provider snapshot never includes the subscription quota. This adds Z.ai's coding and MCP quota windows to the existing
usageLimitssnapshot during the OpenCode status probe, so the existing Limits view and/usage-limitscommand can consume them.The reader uses the credential OpenCode resolved for that server, including configured remote OpenCode servers. It calls Z.ai's read-only quota endpoint, keeps coding and MCP windows distinct, preserves provider reset timestamps, and bounds the request to five seconds. A failed quota request leaves the coding provider ready and uses the existing last-good-quota behavior. Custom endpoints, plugin-managed auth, and unavailable credentials are reported as unsupported instead of guessing a host-local account.
Why
This fills the Z.ai subscription-headroom gap using the existing OpenCode integration and wire contract. It adds no provider driver, configuration setting, database migration, client schema, or polling process. The endpoint is the one used by Z.ai's official usage plugin.
Related to #5539 and discussion #8148. Historical OpenCode token/cost reporting remains in #8456; this PR does not duplicate or depend on that work. These limits are account-wide, including consumption outside T3 Code.
Validation
git diff --check.No frontend components or layouts changed; the existing cross-client Limits contract carries the new data. No installed app or production state was modified for this contribution.
Checklist
Built with GPT-6 in the Codex harness through T3 Code.
Summary by CodeRabbit