Skip to content

refactor(cliproxy): plugin-based quota probes + GLM/xAI support#89

Merged
INONONO66 merged 4 commits into
mainfrom
refactor/quota-probes
May 28, 2026
Merged

refactor(cliproxy): plugin-based quota probes + GLM/xAI support#89
INONONO66 merged 4 commits into
mainfrom
refactor/quota-probes

Conversation

@INONONO66
Copy link
Copy Markdown
Owner

@INONONO66 INONONO66 commented May 28, 2026

Summary

Refactor the monolithic src/cliproxy/quota.ts (477 lines) into a plugin registry pattern and add two new provider quota probes.

Changes

Structural refactoring

  • Extract shared types (AuthFile, ProbeResult, ProbeWindow, ProbeFn) into quota/types.ts
  • Extract shared helpers (normalizePercent, normalizeReset, fetchJson, etc.) into quota/helpers.ts
  • Replace if/else dispatch with Map<string, ProbeFn> registry + registerProbe()
  • Split probeClaude, probeCodex, probeKimi into individual files under quota/probes/

New providers

  • GLM (probes/glm.ts): Probes api.z.ai/api/monitor/usage/quota/limit — parses TIME_LIMIT and TOKENS_LIMIT entries into standardized windows
  • xAI (probes/xai.ts): Uses xAI Management API (management-api.x.ai) — combines prepaid balance + spending limits + invoice preview into monthly spend utilization

Adding a new provider (3 steps)

  1. Create src/cliproxy/quota/probes/xxx.ts exporting a ProbeFn
  2. Add registerProbe("xxx", probeXxx) in quota/index.ts
  3. Add CLI login flag in login.ts (if OAuth-based)

File structure

src/cliproxy/quota/
├── index.ts          # Registry + refresh()
├── types.ts          # Shared types
├── helpers.ts        # Shared utils
└── probes/
    ├── claude.ts     # Anthropic OAuth usage
    ├── codex.ts      # ChatGPT wham/usage
    ├── kimi.ts       # Kimi coding usages
    ├── glm.ts        # z.ai quota/limit (new)
    └── xai.ts        # xAI Management API (new)

Verification

  • bun run typecheck
  • bun test — 246 pass, 0 fail ✅
  • bun run build

Summary by cubic

Refactored quota probing into a plugin registry and added GLM and xAI provider probes. This simplifies adding providers and enables xAI monthly spend visibility.

  • Refactors

    • Replaced monolithic src/cliproxy/quota.ts with quota/ registry (registerProbe()), plus shared types.ts and helpers.ts.
    • Split probes into quota/probes/* and dispatch via Map<string, ProbeFn>.
  • New Features

    • GLM probe (api.z.ai/api/monitor/usage/quota/limit) parsing TIME_LIMIT and TOKENS_LIMIT into standardized windows.
    • xAI probe using Management API to compute monthly spend utilization from invoice preview and limits; also surfaces prepaid balance.
    • Added glm CLI login flag in login.ts (-glm-login).

Written for commit a593290. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • New Features
    • Added support for GLM login provider.
    • Enhanced quota tracking for multiple AI providers including Claude, Codex, Kimi, GLM, and xAI.

Review Change Stack

@INONONO66 INONONO66 force-pushed the refactor/quota-probes branch from 63dd031 to a593290 Compare May 28, 2026 10:08
@INONONO66 INONONO66 merged commit a158c69 into main May 28, 2026
1 check was pending
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 07126273-2e39-4149-801f-030bc51e093b

📥 Commits

Reviewing files that changed from the base of the PR and between 558ed99 and a593290.

📒 Files selected for processing (11)
  • src/cliproxy/login.ts
  • src/cliproxy/quota.ts
  • src/cliproxy/quota/helpers.ts
  • src/cliproxy/quota/index.ts
  • src/cliproxy/quota/probes/claude.ts
  • src/cliproxy/quota/probes/codex.ts
  • src/cliproxy/quota/probes/glm.ts
  • src/cliproxy/quota/probes/kimi.ts
  • src/cliproxy/quota/probes/xai.ts
  • src/cliproxy/quota/types.ts
  • tests/unit/login.test.ts

📝 Walkthrough

Walkthrough

This PR refactors the monolithic quota-probing module into a modular architecture with shared utilities, a provider registry, and five provider-specific implementations (Claude, Codex, GLM, Kimi, xAI). It also adds GLM as a supported login provider.

Changes

Quota Probing Refactor

Layer / File(s) Summary
Quota Type Contracts and Shared Utilities
src/cliproxy/quota/types.ts, src/cliproxy/quota/helpers.ts
Public type contracts define AuthFile, ProbeWindow, ProbeResult, and ProbeFn. Shared helpers normalize percentages and reset times, derive quota type labels from durations, fetch upstream JSON with timeout/abort, and extract error messages from unknown responses.
Quota Probe Registry and Orchestration
src/cliproxy/quota/index.ts, src/cliproxy/quota.ts
Replaces the previous monolithic quota module with a registry-based architecture. readAuthFiles() discovers and parses auth JSON files. QuotaProbe.refresh() loads auths, selects a provider probe from the registry, executes probes with error handling, and aggregates results into quota snapshots and account reports.
Provider-Specific Quota Probes
src/cliproxy/quota/probes/claude.ts, src/cliproxy/quota/probes/codex.ts, src/cliproxy/quota/probes/glm.ts, src/cliproxy/quota/probes/kimi.ts, src/cliproxy/quota/probes/xai.ts
Five provider implementations query provider-specific quota endpoints (Anthropic, OpenAI, GLM, Moonshot, xAI). Each validates auth fields, handles HTTP errors, parses provider-specific response schemas, normalizes usage percentages and reset times, and returns standardized ProbeWindow arrays.

GLM Login Provider Support

Layer / File(s) Summary
GLM Login Configuration
src/cliproxy/login.ts, tests/unit/login.test.ts
Registers GLM provider in login flags with -glm-login invocation and -no-browser flags, enabling startJob("glm", ...) to use GLM-specific authentication. Test expectations updated to include GLM.

🎯 3 (Moderate) | ⏱️ ~25 minutes

🐰 The quota probes now hop from provider to provider,
Each one a modular helper, each one much crisper!
With types and with tests, the refactor stands clear—
And GLM logs in without any fear! 🔐

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/quota-probes

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Comment @coderabbitai help to get the list of available commands and usage tips.

@INONONO66 INONONO66 deleted the refactor/quota-probes branch May 28, 2026 12:34
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