Files
paperclip/server/src/services/quota-windows.ts
Sai Shankar 656b4659fc refactor(quota): move provider quota logic into adapter layer, add unit tests
- Extract all Anthropic credential/API logic into claude-local/src/server/quota.ts
- Extract all OpenAI/WHAM credential/API logic into codex-local/src/server/quota.ts
- Add optional getQuotaWindows() to ServerAdapterModule in adapter-utils
- Rewrite quota-windows.ts as a 29-line thin aggregator with zero provider knowledge
- Wire getQuotaWindows into adapter registry for claude-local and codex-local
- Add 47 unit tests covering toPercent, secondsToWindowLabel, WHAM normalization,
  readClaudeToken, readCodexToken, fetchClaudeQuota, fetchCodexQuota, fetchWithTimeout
- Add 8 unit tests covering parseDateRange validation and byProvider pro-rata math

Adding a third provider now requires only touching that provider's adapter.
2026-03-16 15:08:54 -05:00

30 lines
1.1 KiB
TypeScript

import type { ProviderQuotaResult } from "@paperclipai/shared";
import { listServerAdapters } from "../adapters/registry.js";
/**
* Asks each registered adapter for its provider quota windows and aggregates the results.
* Adapters that don't implement getQuotaWindows() are silently skipped.
* Individual adapter failures are caught and returned as error results rather than
* letting one provider's outage block the entire response.
*/
export async function fetchAllQuotaWindows(): Promise<ProviderQuotaResult[]> {
const adapters = listServerAdapters().filter((a) => a.getQuotaWindows != null);
const settled = await Promise.allSettled(
adapters.map((adapter) => adapter.getQuotaWindows!()),
);
return settled.map((result, i) => {
if (result.status === "fulfilled") return result.value;
// Determine provider slug from the fulfilled value if available, otherwise fall back
// to the adapter type so the error is still attributable to the right provider.
const adapterType = adapters[i]!.type;
return {
provider: adapterType,
ok: false,
error: String(result.reason),
windows: [],
};
});
}