Skip to content
Closed
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
9 changes: 9 additions & 0 deletions apps/server/src/provider/Drivers/ClaudeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
),
),
),
Effect.flatMap((provider) =>
provider.auth.status === "authenticated"
? ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath).pipe(
Effect.map((accountId) =>
accountId ? { ...provider, auth: { ...provider.auth, accountId } } : provider,
),
)
: Effect.succeed(provider),
),
Effect.map(stampIdentity),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => {
' output_style: "default",',
' available_output_styles: ["default"],',
" models: [],",
' account: { email: "dev@example.com", subscriptionType: "pro", tokenSource: "oauth" },',
' account: { email: "dev@example.com", organization: "Acme", subscriptionType: "pro", tokenSource: "oauth" },',
" });",
" }",
" // The probe follows initialize with get_usage on the same process.",
Expand Down Expand Up @@ -147,6 +147,7 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => {

assert.deepEqual(capabilities, {
email: "dev@example.com",
organization: "Acme",
subscriptionType: "pro",
tokenSource: "oauth",
apiProvider: undefined,
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/provider/Layers/ClaudeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ function nonEmptyProbeString(value: string): string | undefined {

type ClaudeCapabilitiesProbe = {
readonly email: string | undefined;
readonly organization: string | undefined;
readonly subscriptionType: string | undefined;
readonly tokenSource: string | undefined;
/**
Expand Down Expand Up @@ -376,13 +377,15 @@ const probeClaudeCapabilities = (
const account = init.account as
| {
readonly email?: string;
readonly organization?: string;
readonly subscriptionType?: string;
readonly tokenSource?: string;
readonly apiProvider?: string;
}
| undefined;
return {
email: account?.email,
organization: nonEmptyProbeString(account?.organization ?? ""),
subscriptionType: account?.subscriptionType,
tokenSource: account?.tokenSource,
apiProvider: account?.apiProvider,
Expand Down Expand Up @@ -592,6 +595,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
auth: {
status: "authenticated",
...(capabilities.email ? { email: capabilities.email } : {}),
...(capabilities.organization ? { organization: capabilities.organization } : {}),
...(authMetadata ? authMetadata : {}),
},
...(versionUpgradeMessage ? { message: versionUpgradeMessage } : {}),
Expand Down
8 changes: 6 additions & 2 deletions apps/server/src/provider/Layers/ProviderRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function booleanDescriptor(id: string, label: string) {

type TestClaudeCapabilities = {
readonly email: string | undefined;
readonly organization: string | undefined;
readonly subscriptionType: string | undefined;
readonly tokenSource: string | undefined;
readonly apiProvider: string | undefined;
Expand All @@ -161,6 +162,7 @@ function claudeCapabilities(overrides: Partial<TestClaudeCapabilities> = {}) {
return () =>
Effect.succeed({
email: undefined,
organization: undefined,
subscriptionType: undefined,
tokenSource: undefined,
apiProvider: undefined,
Expand Down Expand Up @@ -2828,6 +2830,7 @@ it.layer(Layer.mergeAll(TestNodeServices, ServerSettingsModule.layerTest(), Test
() =>
Effect.succeed({
email: undefined,
organization: undefined,
subscriptionType: undefined,
tokenSource: undefined,
apiProvider: undefined,
Expand Down Expand Up @@ -2900,14 +2903,15 @@ it.layer(Layer.mergeAll(TestNodeServices, ServerSettingsModule.layerTest(), Test
),
);

it.effect("returns claude auth email from initialization result", () =>
it.effect("returns claude auth email and organization from initialization result", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus(
defaultClaudeSettings,
claudeCapabilities({ email: "claude@example.com" }),
claudeCapabilities({ email: "claude@example.com", organization: "Acme" }),
);
assert.strictEqual(status.auth.status, "authenticated");
assert.strictEqual(status.auth.email, "claude@example.com");
assert.strictEqual(status.auth.organization, "Acme");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/provider/Layers/claudeResetCredits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,19 @@ effectIt.layer(NodeServices.layer)("consumeClaudeResetCredit", (it) => {
}),
);
});

effectIt.layer(NodeServices.layer)("readClaudeOrganizationId", (it) => {
it.effect("reads the login's org id, and nothing from a missing or garbled account", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { configDir, accountConfigPath } = yield* writeLogin;
const signedIn = yield* ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath);
const missing = yield* ClaudeResetCredits.readClaudeOrganizationId(
`${configDir}/absent.json`,
);
yield* fs.writeFileString(accountConfigPath, "{not json");
const garbled = yield* ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath);
expect([signedIn, missing, garbled]).toEqual(["org-1", undefined, undefined]);
}),
);
});
10 changes: 10 additions & 0 deletions apps/server/src/provider/Layers/claudeResetCredits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ export const claudeAccountConfigPath = (configDir: string | undefined) =>
configDir ? path.join(configDir, ".claude.json") : path.join(NodeOS.homedir(), ".claude.json"),
);

/**
* The org the login in `accountConfigPath` draws its quota from, or undefined
* when the account record is missing or unreadable.
*/
export const readClaudeOrganizationId = (accountConfigPath: string) =>
readJson(Config, accountConfigPath).pipe(
Effect.map((config) => config.oauthAccount?.organizationUuid?.trim() || undefined),
Effect.orElseSucceed(() => undefined),
);

const CLAIM_OUTCOMES = {
reset: "reset",
not_limited: "nothingToReset",
Expand Down
Loading
Loading