Skip to content

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
takumi3488:feat/zai-three-quota-display
Open

Fix z.ai 5-hour token quota discarded when API returns multiple TOKENS_LIMIT entries#632
takumi3488 wants to merge 4 commits intosteipete:mainfrom
takumi3488:feat/zai-three-quota-display

Conversation

@takumi3488
Copy link
Copy Markdown

@takumi3488 takumi3488 commented Apr 1, 2026

Summary

The z.ai API returns up to three quota entries — a 5-hour TOKENS_LIMIT, a weekly TOKENS_LIMIT, and a TIME_LIMIT (MCP). The parse loop used a single tokenLimit variable, so the second entry silently overwrote the first and the 5-hour quota was lost.

  • Add ZaiLimitUnit.weeks (rawValue 6) so the weekly entry gets a correct windowMinutes
  • Collect all TOKENS_LIMIT entries and sort by window size: shorter window → sessionTokenLimit (tertiary slot), longer → tokenLimit (primary slot)
  • Map sessionTokenLimit to UsageSnapshot.tertiary and enable supportsOpus/opusLabel: "5-hour" in the descriptor so MenuCardView, MenuDescriptor, and CLI all render the new row
  • Wire zaiSessionDetail text into the tertiary card metric
  • Backwards-compatible: when the API returns only one TOKENS_LIMIT + TIME_LIMIT, sessionTokenLimit is nil and the tertiary row is suppressed

Validation

  • swift build passes
  • Added ZaiThreeLimitTests: 3-entry parse, unit:6 enum, and 2-entry backward-compat fallback
  • Added ZaiMenuCardTests, a new test in CodexPresentationCharacterizationTests, and a new test in CLISnapshotTests covering the tertiary row at all three rendering layers

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
@takumi3488 takumi3488 changed the title feat(zai): display 5-hour token quota as tertiary row Fix z.ai 5-hour token quota discarded when API returns multiple TOKENS_LIMIT entries Apr 1, 2026
@takumi3488 takumi3488 marked this pull request as draft April 1, 2026 05:50
@takumi3488 takumi3488 marked this pull request as ready for review April 1, 2026 06:00
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