GeezerVison is showing github.com — fix(usage): keep CLIProxy Claude accounts apart per organization · pingdotgg/t3code@41e634f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/cliproxyApi.test.ts +23-12Lines changed: 23 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -315,22 +315,28 @@ describe("CLIProxyAPI built-in management API", () => { | 315 | 315 | it.effect("maps Claude scoped windows without a scheduler plugin", () => | 316 | 316 | Effect.gen(function* () { | 317 | 317 | 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 | + ], | 319 | 323 | // The usage endpoint answers 429 to requests without a Claude Code user agent. | 320 | 324 | upstream: (request) => ({ | 321 | 325 | 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 | + ], | 331 | 339 | }, | 332 | | - ], | 333 | | - }, | 334 | 340 | }), | 335 | 341 | }); | 336 | 342 | const api = yield* test.api; | @@ -342,6 +348,11 @@ describe("CLIProxyAPI built-in management API", () => { | 342 | 348 | ["seven_day", 50], | 343 | 349 | ["seven_day_fable", 80], | 344 | 350 | ]); | | 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); | 345 | 356 | }), | 346 | 357 | ); | 347 | 358 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/cliproxyApi.ts +25-3Lines changed: 25 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,9 @@ const ClaudeUsage = Schema.Struct({ | 72 | 72 | ), | 73 | 73 | ), | 74 | 74 | }); | | 75 | +const ClaudeProfile = Schema.Struct({ | | 76 | + organization: Schema.optional(Schema.NullOr(Schema.Struct({ uuid: Schema.String }))), | | 77 | +}); | 75 | 78 | const CreditList = Schema.Struct({ | 76 | 79 | credits: Schema.Array( | 77 | 80 | Schema.Struct({ | @@ -89,6 +92,7 @@ const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); | 89 | 92 | const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); | 90 | 93 | const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); | 91 | 94 | const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage)); | | 95 | +const decodeClaudeProfile = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeProfile)); | 92 | 96 | const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); | 93 | 97 |
| 94 | 98 | // Extra CAs can fix untrusted chains; expiry/hostname failures report codes only. | @@ -135,6 +139,7 @@ const decodeConsumeResponse = Schema.decodeUnknownEffect( | 135 | 139 |
| 136 | 140 | const CODEX_BASE = "https://chatgpt.com/backend-api/wham"; | 137 | 141 | const CREDIT_URL = `${CODEX_BASE}/rate-limit-reset-credits`; | | 142 | +const CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile"; | 138 | 143 |
| 139 | 144 | const CREDIT_REDEEM_NAMESPACE = new Uint8Array([ | 140 | 145 | 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* () { | 251 | 256 | .toSorted((a, b) => Date.parse(a.expires_at) - Date.parse(b.expires_at)); | 252 | 257 | }); | 253 | 258 |
| | 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 | + | 254 | 274 | const readAccount = Effect.fn("CliproxyApi.readAccount")(function* ( | 255 | 275 | config: UsageLimitSourceConfig, | 256 | 276 | account: typeof AuthFile.Type, | 257 | 277 | ) { | 258 | 278 | 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(); | 259 | 283 | const base = { | 260 | 284 | id: account.id, | 261 | 285 | driver: ProviderDriverKind.make(account.provider === "codex" ? "codex" : "claudeAgent"), | 262 | 286 | ...(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 } : {}), | 266 | 288 | }; | 267 | 289 | const read = Effect.gen(function* () { | 268 | 290 | if (account.provider === "claude") { | |
|