Fix z.ai 5-hour token quota discarded when API returns multiple TOKENS_LIMIT entries#632
Open
takumi3488 wants to merge 4 commits intosteipete:mainfrom
Open
Fix z.ai 5-hour token quota discarded when API returns multiple TOKENS_LIMIT entries#632takumi3488 wants to merge 4 commits intosteipete:mainfrom
takumi3488 wants to merge 4 commits intosteipete:mainfrom
Conversation
The z.ai API can return two TOKENS_LIMIT entries — one for the 5-hour window (unit:3/number:5) and one for the weekly window (unit:6/number:1). Previously the second entry silently overwrote the first, discarding the 5-hour quota entirely. Changes: - Add ZaiLimitUnit.weeks (rawValue 6) with windowMinutes = n×7×24×60 - Add ZaiUsageSnapshot.sessionTokenLimit for the shorter-window entry - Rewrite parseUsageSnapshot to collect all TOKENS_LIMIT entries and sort by windowMinutes: shorter → sessionTokenLimit (tertiary), longer → tokenLimit (primary), preserving the existing display unchanged for APIs that return only one TOKENS_LIMIT - Map sessionTokenLimit to UsageSnapshot.tertiary in toUsageSnapshot() - Enable supportsOpus + opusLabel "5-hour" in ZaiProviderDescriptor so MenuCardView/MenuDescriptor/CLIRenderer render the new tertiary row - Wire zaiSessionDetail text into the tertiary card metric - Add ZaiThreeLimitTests covering 3-entry parsing, unit:6 enum, and backward-compatible 2-entry fallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The z.ai API returns up to three quota entries — a 5-hour
TOKENS_LIMIT, a weeklyTOKENS_LIMIT, and aTIME_LIMIT(MCP). The parse loop used a singletokenLimitvariable, so the second entry silently overwrote the first and the 5-hour quota was lost.ZaiLimitUnit.weeks(rawValue 6) so the weekly entry gets a correctwindowMinutesTOKENS_LIMITentries and sort by window size: shorter window →sessionTokenLimit(tertiary slot), longer →tokenLimit(primary slot)sessionTokenLimittoUsageSnapshot.tertiaryand enablesupportsOpus/opusLabel: "5-hour"in the descriptor so MenuCardView, MenuDescriptor, and CLI all render the new rowzaiSessionDetailtext into the tertiary card metricTOKENS_LIMIT+TIME_LIMIT,sessionTokenLimitis nil and the tertiary row is suppressedValidation
swift buildpassesZaiThreeLimitTests: 3-entry parse,unit:6enum, and 2-entry backward-compat fallbackZaiMenuCardTests, a new test inCodexPresentationCharacterizationTests, and a new test inCLISnapshotTestscovering the tertiary row at all three rendering layers