fix(zai): recognize CREDIT_LIMIT quota entries for credit-based plan tiers - #240
fix(zai): recognize CREDIT_LIMIT quota entries for credit-based plan tiers#240digivisions wants to merge 2 commits into
Conversation
Credit-based GLM plan tiers (e.g. Coding Lite) return quota entries with type CREDIT_LIMIT rather than TOKENS_LIMIT. The parser only matched TOKENS_LIMIT/TIME_LIMIT tuples and hit the default continue for anything else, so every entry was skipped and the probe failed with "No recognized quota types found". Map CREDIT_LIMIT using the same unit semantics as TOKENS_LIMIT (3 = session, 6 = weekly, 7 = monthly) and preserve unknown units via modelSpecific instead of dropping them.
📝 WalkthroughWalkthroughThe Zai usage probe now parses ChangesZai credit quota support
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@Sources/Infrastructure/Zai/ZaiUsageProbe.swift`:
- Around line 318-319: Update the unit-7 handling in the quota mapping switch to
use .timeLimit("Monthly") for both TOKENS_LIMIT and CREDIT_LIMIT, preserving the
monthly duration and persisted key. Add a regression test covering CREDIT_LIMIT
with unit 7 and asserting the monthly time-limit behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 231f9f01-11bb-4772-ba00-d28115613cca
📒 Files selected for processing (2)
Sources/Infrastructure/Zai/ZaiUsageProbe.swiftTests/InfrastructureTests/Zai/ZaiUsageProbeParsingTests.swift
| case ("TOKENS_LIMIT", 7), ("CREDIT_LIMIT", 7): | ||
| quotaType = .modelSpecific("Monthly") |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Map unit 7 to the monthly time quota.
Line 319 creates modelSpecific("Monthly"). That type has a seven-day duration and the persisted key model:Monthly. Use .timeLimit("Monthly") so the quota has the 30-day monthly duration and the time:Monthly key. Add a CREDIT_LIMIT unit 7 regression test.
Proposed fix
case ("TOKENS_LIMIT", 7), ("CREDIT_LIMIT", 7):
- quotaType = .modelSpecific("Monthly")
+ quotaType = .timeLimit("Monthly")📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| case ("TOKENS_LIMIT", 7), ("CREDIT_LIMIT", 7): | |
| quotaType = .modelSpecific("Monthly") | |
| case ("TOKENS_LIMIT", 7), ("CREDIT_LIMIT", 7): | |
| quotaType = .timeLimit("Monthly") |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@Sources/Infrastructure/Zai/ZaiUsageProbe.swift` around lines 318 - 319,
Update the unit-7 handling in the quota mapping switch to use
.timeLimit("Monthly") for both TOKENS_LIMIT and CREDIT_LIMIT, preserving the
monthly duration and persisted key. Add a regression test covering CREDIT_LIMIT
with unit 7 and asserting the monthly time-limit behavior.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #240 +/- ##
==========================================
- Coverage 83.13% 82.98% -0.15%
==========================================
Files 123 123
Lines 9704 9708 +4
==========================================
- Hits 8067 8056 -11
- Misses 1637 1652 +15
🚀 New features to boost your workflow:
|
Problem
On credit-based GLM plan tiers (
"level": "lite"), the Z.ai provider failswith
Z.ai Unavailable — Failed to parse output: No recognized quota types found,even though the account is valid and
/api/monitor/usage/quota/limitreturns HTTP 200with complete quota data.
Root cause
ZaiUsageProbe.parseQuotaLimitResponseswitches on(limit.type, limit.unit)andonly matches
TIME_LIMITandTOKENS_LIMIT. Credit-based tiers report the sameinformation under
CREDIT_LIMIT, so every entry hits thedefault: continue,quotasends up empty, and the guard throws.Actual response from a
liteaccount (values redacted for privacy):{ "code": 200, "data": { "limits": [ { "type": "CREDIT_LIMIT", "unit": 3, "number": 5, "usage": 2000, "currentValue": 0, "remaining": 2000, "percentage": 0 }, { "type": "CREDIT_LIMIT", "unit": 6, "number": 1, "usage": 10000, "currentValue": 2004, "remaining": 7995, "percentage": 20, "nextResetTime": 1786112351998 } ], "level": "lite" }, "success": true }The
unitsemantics are identical toTOKENS_LIMIT—3is the rolling 5-hourwindow and
6is the rolling multi-day window — so the existing mapping from #181applies unchanged.
Fix
Add
CREDIT_LIMITto each existing case rather than duplicating the mapping, plus acatch-all so unrecognised units surface as
Credits (unit N)instead of being droppedsilently — mirroring the existing
Tokens (unit N)behaviour.Tests
Five new cases in
ZaiUsageProbeParsingTests: full credit-plan response,unit=3→session,
unit=6→ weekly, unknown-unit preservation, and a mixedTOKENS_LIMIT/CREDIT_LIMIT/TIME_LIMITresponse.tuist test Infrastructurepasses (22 tests).
Verified end-to-end against a live
liteaccount: probe now logsZai probe success: 2 quotas foundand both windows render in the menu bar.No behaviour change for token-based plans.
Summary by CodeRabbit
Bug Fixes
Tests