Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions apps/server/src/usage/cliproxyApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,70 @@ describe("CLIProxyAPI built-in management API", () => {
}),
);

it.effect.each([
{
name: "spend budget",
budget: {
spend: {
enabled: true,
used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
},
},
},
{
name: "extra usage",
budget: {
extra_usage: {
is_enabled: true,
used_credits: 4_631,
monthly_limit: 50_000,
utilization: 9,
},
},
},
])("keeps Claude monthly spend from $name beside rolling windows", ({ budget }) =>
Effect.gen(function* () {
const test = fixture({
accounts: [{ ...accounts[0]!, provider: "claude" }],
upstream: () => ({
status: 200,
body: { five_hour: { utilization: 10, resets_at: null }, ...budget },
}),
});
const result = yield* (yield* test.api).readAccounts(config);
expect(
result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
).toEqual([
["five_hour", 10],
["monthly_spend", expect.closeTo(9.262, 10)],
]);
}),
);

it.effect("omits disabled Claude budgets without losing rolling windows", () =>
Effect.gen(function* () {
const test = fixture({
accounts: [{ ...accounts[0]!, provider: "claude" }],
upstream: () => ({
status: 200,
body: {
five_hour: { utilization: 10, resets_at: null },
spend: { enabled: false, used: null, limit: null },
extra_usage: {
is_enabled: false,
used_credits: null,
monthly_limit: null,
utilization: null,
},
},
}),
});
const result = yield* (yield* test.api).readAccounts(config);
expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["five_hour"]);
}),
);

it.effect("pins redemption to the displayed credit and clears only that account's cooldown", () =>
Effect.gen(function* () {
const test = fixture();
Expand Down
24 changes: 19 additions & 5 deletions apps/server/src/usage/cliproxyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ const ClaudeWindow = Schema.Struct({
const ClaudeUsage = Schema.Struct({
five_hour: Schema.optional(Schema.NullOr(ClaudeWindow)),
seven_day: Schema.optional(Schema.NullOr(ClaudeWindow)),
spend: Schema.optional(Schema.Unknown),
extra_usage: Schema.optional(
Schema.NullOr(
Schema.Struct({
is_enabled: Schema.Boolean,
monthly_limit: Schema.NullOr(Schema.Number),
used_credits: Schema.NullOr(Schema.Number),
utilization: Schema.NullOr(Schema.Number),
}),
),
),
limits: Schema.optional(
Schema.Array(
Schema.Struct({
Expand Down Expand Up @@ -276,18 +287,21 @@ export const makeCliproxyApi = Effect.gen(function* () {
]
: [],
);
const rateLimits = {
five_hour: usage.five_hour ?? null,
seven_day: usage.seven_day ?? null,
model_scoped,
spend: usage.spend,
extra_usage: usage.extra_usage ?? null,
};
return {
...base,
plan: "Claude Subscription",
usageLimits: claudeUsageResponseToLimits({
checkedAt,
response: {
rate_limits_available: true,
rate_limits: {
five_hour: usage.five_hour ?? null,
seven_day: usage.seven_day ?? null,
model_scoped,
},
rate_limits: rateLimits,
},
}).limits,
};
Expand Down
Loading