GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(usage): keep CLIProxy Claude accounts apart per organization · pingdotgg/t3code@41e634f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/usage
    • cliproxyApi.test.ts
    • cliproxyApi.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/usage/cliproxyApi.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/usage/cliproxyApi.test.ts
+23-12Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,28 @@ describe("CLIProxyAPI built-in management API", () => {
315315
it.effect("maps Claude scoped windows without a scheduler plugin", () =>
316316
Effect.gen(function* () {
317317
const test = fixture({
318-
accounts: [{ ...accounts[0]!, provider: "claude" }],
318+
// One login in two organizations: separate quotas that share an email.
319+
accounts: [
320+
{ ...accounts[0]!, provider: "claude" },
321+
{ ...accounts[0]!, id: "first-team.json", auth_index: "b", provider: "claude" },
322+
],
319323
// The usage endpoint answers 429 to requests without a Claude Code user agent.
320324
upstream: (request) => ({
321325
status: request.header?.["User-Agent"]?.startsWith("claude-code/") ? 200 : 429,
322-
body: {
323-
five_hour: { utilization: 10, resets_at: null },
324-
seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" },
325-
limits: [
326-
{
327-
kind: "weekly_scoped",
328-
percent: 80,
329-
resets_at: null,
330-
scope: { model: { display_name: "Fable" } },
326+
body: request.url?.endsWith("/oauth/profile")
327+
? { organization: { uuid: `org-${request.auth_index}` } }
328+
: {
329+
five_hour: { utilization: 10, resets_at: null },
330+
seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" },
331+
limits: [
332+
{
333+
kind: "weekly_scoped",
334+
percent: 80,
335+
resets_at: null,
336+
scope: { model: { display_name: "Fable" } },
337+
},
338+
],
331339
},
332-
],
333-
},
334340
}),
335341
});
336342
const api = yield* test.api;
@@ -342,6 +348,11 @@ describe("CLIProxyAPI built-in management API", () => {
342348
["seven_day", 50],
343349
["seven_day_fable", 80],
344350
]);
351+
expect(result.map((account) => account.workspaceId)).toEqual(["org-a", "org-b"]);
352+
yield* api.readAccounts(config);
353+
expect(
354+
test.requests.filter((request) => request.body?.url?.endsWith("/oauth/profile")),
355+
).toHaveLength(2);
345356
}),
346357
);
347358

Collapse file

‎apps/server/src/usage/cliproxyApi.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/usage/cliproxyApi.ts
+25-3Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const ClaudeUsage = Schema.Struct({
7272
),
7373
),
7474
});
75+
const ClaudeProfile = Schema.Struct({
76+
organization: Schema.optional(Schema.NullOr(Schema.Struct({ uuid: Schema.String }))),
77+
});
7578
const CreditList = Schema.Struct({
7679
credits: Schema.Array(
7780
Schema.Struct({
@@ -89,6 +92,7 @@ const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse);
8992
const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList));
9093
const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage));
9194
const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage));
95+
const decodeClaudeProfile = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeProfile));
9296
const isUsageLimitSourceError = Schema.is(UsageLimitSourceError);
9397

9498
// Extra CAs can fix untrusted chains; expiry/hostname failures report codes only.
@@ -135,6 +139,7 @@ const decodeConsumeResponse = Schema.decodeUnknownEffect(
135139

136140
const CODEX_BASE = "https://chatgpt.com/backend-api/wham";
137141
const CREDIT_URL = `${CODEX_BASE}/rate-limit-reset-credits`;
142+
const CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
138143

139144
const CREDIT_REDEEM_NAMESPACE = new Uint8Array([
140145
0x6f, 0x1c, 0x2a, 0x9e, 0x2d, 0x4b, 0x4c, 0x1e, 0x9a, 0x7f, 0x3b, 0x8d, 0x5e, 0x0c, 0x1a, 0x42,
@@ -251,18 +256,35 @@ export const makeCliproxyApi = Effect.gen(function* () {
251256
.toSorted((a, b) => Date.parse(a.expires_at) - Date.parse(b.expires_at));
252257
});
253258

259+
// One Claude login can belong to several organizations, each with its own quota.
260+
const claudeOrganizations = new Map<string, string>();
261+
const claudeOrganizationId = Effect.fn("CliproxyApi.claudeOrganizationId")(function* (
262+
config: UsageLimitSourceConfig,
263+
account: typeof AuthFile.Type,
264+
) {
265+
const cached = claudeOrganizations.get(account.id);
266+
if (cached) return cached;
267+
const profile = yield* decodeClaudeProfile(yield* apiCall(config, account, CLAUDE_PROFILE_URL));
268+
const organizationId = profile.organization?.uuid.trim();
269+
if (!organizationId) return undefined;
270+
claudeOrganizations.set(account.id, organizationId);
271+
return organizationId;
272+
});
273+
254274
const readAccount = Effect.fn("CliproxyApi.readAccount")(function* (
255275
config: UsageLimitSourceConfig,
256276
account: typeof AuthFile.Type,
257277
) {
258278
const checkedAt = DateTime.formatIso(yield* DateTime.now);
279+
const workspaceId =
280+
account.provider === "claude"
281+
? yield* claudeOrganizationId(config, account).pipe(Effect.orElseSucceed(() => undefined))
282+
: account.id_token?.chatgpt_account_id?.trim();
259283
const base = {
260284
id: account.id,
261285
driver: ProviderDriverKind.make(account.provider === "codex" ? "codex" : "claudeAgent"),
262286
...(account.email ? { email: account.email } : {}),
263-
...(account.id_token?.chatgpt_account_id?.trim()
264-
? { workspaceId: account.id_token.chatgpt_account_id.trim() }
265-
: {}),
287+
...(workspaceId ? { workspaceId } : {}),
266288
};
267289
const read = Effect.gen(function* () {
268290
if (account.provider === "claude") {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.