From 1dbcdb0d757ceea6be93b498db6990d5a9be14ce Mon Sep 17 00:00:00 2001 From: Vadym O Date: Fri, 6 Nov 2026 12:38:55 +0800 Subject: [PATCH] fix(kimi): read the usages.limit_* quota shape before the legacy one Kimi's /coding/v1/usages now returns usages.{limit_5h, limit_7d, limit_month_total, limit_month_code}, each {used_ratio 0..1, reset_time} (MoonshotAI/kimi-code packages/oauth/src/managed-usage.ts). When usages is present it alone is read (5h, weekly, monthly total); the legacy limits[] / totalQuota blocks can be placeholders. Without it, the legacy parse is unchanged. Diff by be-low-chief (Vadym 19319). Co-Authored-By: Claude Code --- src/providers/quota/vendor-probes-key.ts | 32 +++++++++++++--- tests/fixtures/file-size-baseline.json | 2 +- tests/providers/provider-quota.test.ts | 47 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/providers/quota/vendor-probes-key.ts b/src/providers/quota/vendor-probes-key.ts index 59347607f5e..74ab52016b0 100644 --- a/src/providers/quota/vendor-probes-key.ts +++ b/src/providers/quota/vendor-probes-key.ts @@ -962,8 +962,8 @@ function unwrapKimiQuotaPayload(value: unknown): Record | null // A null/non-usable outer field is a placeholder, not data — an envelope like // { usage: null, data: { usage: {...} } } must still unwrap to the nested payload. const usable = (field: unknown): boolean => field !== undefined && field !== null; - const outerHasUsage = usable(body.usage) || usable(body.limits) || usable(body.totalQuota); - const nestedHasUsage = usable(nested.usage) || usable(nested.limits) || usable(nested.totalQuota); + const outerHasUsage = usable(body.usage) || usable(body.usages) || usable(body.limits) || usable(body.totalQuota); + const nestedHasUsage = usable(nested.usage) || usable(nested.usages) || usable(nested.limits) || usable(nested.totalQuota); return !outerHasUsage && nestedHasUsage ? nested : body; } @@ -1009,13 +1009,29 @@ function isKimiWeeklyLimit(item: Record, detail: Record { expect(seen).toEqual([]); }); + test("Kimi quota reads the Kimi Code usages shape: 0..1 ratios, monthly pool, legacy placeholder ignored", async () => { + await saveCredential("kimi", { access: "kimi-access-secret", refresh: "kimi-refresh-secret", expires: Date.now() + 3600_000 }); + globalThis.fetch = (async (input: RequestInfo | URL) => { + if (String(input) !== "https://api.kimi.com/coding/v1/usages") return new Response("not found", { status: 404 }); + return new Response(JSON.stringify({ + usages: { + limit_5h: { used_ratio: 0.15, reset_time: "2026-11-06T07:46:00.000Z" }, + limit_month_total: { used_ratio: "0.0082", reset_time: "2026-12-06T03:22:00Z" }, + limit_month_code: { used_ratio: 0.5, reset_time: "2026-12-06T03:22:00Z" }, + }, + limits: [{ window: { duration: 300, timeUnit: "TIME_UNIT_MINUTE" }, detail: { limit: "100", remaining: "100" } }], + totalQuota: { limit: "100", remaining: "99" }, + }), { status: 200 }); + }) as typeof fetch; + + const result = await fetchProviderQuotaReports(kimiOnlyConfig(), true); + + expect(result.reports[0]?.quota).toEqual({ + fiveHourPercent: 15, + fiveHourResetAt: Date.parse("2026-11-06T07:46:00.000Z"), + monthlyPercent: 0.82, + monthlyResetAt: Date.parse("2026-12-06T03:22:00Z"), + updatedAt: expect.any(Number), + }); + }); + + test("Kimi quota usages shape: a 0 ratio is 0%, limit_7d is weekly, a missing ratio is skipped", async () => { + await saveCredential("kimi", { access: "kimi-access-secret", refresh: "kimi-refresh-secret", expires: Date.now() + 3600_000 }); + globalThis.fetch = (async () => new Response(JSON.stringify({ + usages: { + limit_5h: { used_ratio: 0, reset_time: "2026-11-06T07:46:00Z" }, + limit_7d: { used_ratio: "0.5", reset_time: "2026-11-10T00:00:00Z" }, + limit_month_total: { reset_time: "2026-12-06T03:22:00Z" }, + }, + }), { status: 200 })) as typeof fetch; + + const result = await fetchProviderQuotaReports(kimiOnlyConfig(), true); + + expect(result.reports[0]?.quota).toEqual({ + fiveHourPercent: 0, + fiveHourResetAt: Date.parse("2026-11-06T07:46:00Z"), + weeklyPercent: 50, + weeklyResetAt: Date.parse("2026-11-10T00:00:00Z"), + updatedAt: expect.any(Number), + }); + }); + test("Kimi quota refreshes an expired OAuth token before calling usages", async () => { await saveCredential("kimi", { access: "expired-kimi-access", refresh: "kimi-refresh-secret", expires: Date.now() - 1 }); const seen: Array<{ url: string; authorization?: string }> = [];