@gajae-code/ai 0.22.1 → 0.22.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/types/utils/oauth/types.d.ts +4 -3
- package/package.json +4 -4
- package/src/auth-storage.ts +4 -3
- package/src/usage/openai-codex.ts +2 -0
- package/src/utils/oauth/openai-codex.ts +53 -9
- package/src/utils/oauth/types.d.ts +4 -3
- package/src/utils/oauth/types.ts +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.22.2] - 2026-11-25
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- OpenAI Codex logins now keep one credential per ChatGPT workspace. One ChatGPT login can belong to several workspaces (a personal plan and Team or Business workspaces), each with its own usage limits. The workspace id (`chatgpt_account_id`) is recorded as the credential's organization, and the workspace name is looked up once at login as its display name, so logging in to a second workspace no longer replaces the first, and usage is reported per workspace instead of being merged under the shared email. Token refresh does not repeat the name lookup. Rows stored before this change keep working and are deduplicated once their next refresh records the workspace.
|
|
10
|
+
|
|
5
11
|
## [0.22.1] - 2026-11-24
|
|
6
12
|
|
|
7
13
|
### Added
|
|
@@ -9,9 +9,10 @@ export type OAuthCredentials = {
|
|
|
9
9
|
profileArn?: string;
|
|
10
10
|
/**
|
|
11
11
|
* Organization the token is scoped to. One Anthropic account can belong to
|
|
12
|
-
* several organizations (e.g. a Team workspace and a personal Max plan)
|
|
13
|
-
* login
|
|
14
|
-
*
|
|
12
|
+
* several organizations (e.g. a Team workspace and a personal Max plan), and one
|
|
13
|
+
* ChatGPT login can belong to several workspaces (for OpenAI Codex this is the
|
|
14
|
+
* workspace id, `chatgpt_account_id`). Each login is bound to exactly one of
|
|
15
|
+
* them, so the organization is part of the credential's identity.
|
|
15
16
|
*/
|
|
16
17
|
organizationId?: string;
|
|
17
18
|
/** Display name of {@link organizationId}. */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gajae-code/ai",
|
|
4
|
-
"version": "0.22.
|
|
4
|
+
"version": "0.22.2",
|
|
5
5
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
6
6
|
"homepage": "https://gajae-code.com",
|
|
7
7
|
"author": "Yeachan-Heo and Gajae Code Contributors",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"pi-ai": "./src/cli.ts"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"check": "biome check . && bun run check:types",
|
|
32
|
+
"check": "bun ../../scripts/check-biome-max-size.ts && biome check . && bun run check:types",
|
|
33
33
|
"check:types": "bun ../coding-agent/scripts/generate-docs-index.ts && tsc -p tsconfig.json --noEmit",
|
|
34
34
|
"lint": "biome lint .",
|
|
35
35
|
"test": "bun test",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"@agentclientprotocol/sdk": "1.3.0",
|
|
42
42
|
"@anthropic-ai/sdk": "^0.94.0",
|
|
43
43
|
"@bufbuild/protobuf": "^2.12.0",
|
|
44
|
-
"@gajae-code/natives": "0.22.
|
|
45
|
-
"@gajae-code/utils": "0.22.
|
|
44
|
+
"@gajae-code/natives": "0.22.2",
|
|
45
|
+
"@gajae-code/utils": "0.22.2",
|
|
46
46
|
"openai": "^6.36.0",
|
|
47
47
|
"partial-json": "^0.1.7",
|
|
48
48
|
"zod": "4.4.3"
|
package/src/auth-storage.ts
CHANGED
|
@@ -1111,7 +1111,8 @@ const USAGE_CACHE_PREFIX = "usage_cache:";
|
|
|
1111
1111
|
* Argon / Claude-GPT quota pools; schema 1 rows carry window `default` with
|
|
1112
1112
|
* per-model labels and pool nothing.
|
|
1113
1113
|
*/
|
|
1114
|
-
|
|
1114
|
+
// openai-codex 2: reports carry the ChatGPT workspace id as `orgId`, so cached reports without it are not reused.
|
|
1115
|
+
const USAGE_REPORT_SCHEMA_VERSIONS: Readonly<Record<string, number>> = { "google-antigravity": 2, "openai-codex": 2 };
|
|
1115
1116
|
|
|
1116
1117
|
function usageReportSchemaTag(provider: string): string {
|
|
1117
1118
|
const version = USAGE_REPORT_SCHEMA_VERSIONS[provider];
|
|
@@ -5366,8 +5367,8 @@ export class AuthStorage {
|
|
|
5366
5367
|
const email = this.#getUsageReportMetadataValue(report, "email");
|
|
5367
5368
|
if (email) identifiers.push(`email:${email.toLowerCase()}`);
|
|
5368
5369
|
if (report.provider === "openai-codex" || report.provider === "anthropic") {
|
|
5369
|
-
// One
|
|
5370
|
-
const orgId =
|
|
5370
|
+
// One account can sign in to several Anthropic organizations or ChatGPT workspaces; each has its own limits.
|
|
5371
|
+
const orgId = this.#getUsageReportMetadataValue(report, "orgId");
|
|
5371
5372
|
if (orgId) return identifiers.map(identifier => `${report.provider}:${identifier}|org:${orgId}`.toLowerCase());
|
|
5372
5373
|
return identifiers.map(identifier => `${report.provider}:${identifier.toLowerCase()}`);
|
|
5373
5374
|
}
|
|
@@ -469,6 +469,8 @@ export const openaiCodexUsageProvider: UsageProvider = {
|
|
|
469
469
|
limitReached: parsed?.limitReached,
|
|
470
470
|
email,
|
|
471
471
|
accountId,
|
|
472
|
+
// The ChatGPT workspace id; one login can hold a credential per workspace, each with its own limits.
|
|
473
|
+
...(accountId ? { orgId: accountId } : {}),
|
|
472
474
|
},
|
|
473
475
|
raw: parsed?.raw ?? payload,
|
|
474
476
|
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* OpenAI code provider (ChatGPT OAuth) flow — browser and device-code flows.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { OPENAI_HEADER_VALUES } from "../../providers/openai-codex/constants";
|
|
5
|
+
import { CODEX_BASE_URL, OPENAI_HEADER_VALUES, OPENAI_HEADERS } from "../../providers/openai-codex/constants";
|
|
6
6
|
import { OAuthCallbackFlow, type OAuthCallbackFlowOptions } from "./callback-server";
|
|
7
7
|
import { generatePKCE } from "./pkce";
|
|
8
8
|
import type { OAuthController, OAuthCredentials } from "./types";
|
|
@@ -16,6 +16,8 @@ const SCOPE = "openid profile email offline_access";
|
|
|
16
16
|
const JWT_CLAIM_PATH = "https://api.openai.com/auth";
|
|
17
17
|
const JWT_PROFILE_CLAIM = "https://api.openai.com/profile";
|
|
18
18
|
const TOKEN_REQUEST_TIMEOUT_MS = 15_000;
|
|
19
|
+
const WORKSPACE_LOOKUP_URL = `${CODEX_BASE_URL}/wham/accounts/check`;
|
|
20
|
+
const WORKSPACE_LOOKUP_TIMEOUT_MS = 5_000;
|
|
19
21
|
const DEVICE_USERCODE_URL = "https://auth.openai.com/api/accounts/deviceauth/usercode";
|
|
20
22
|
const DEVICE_TOKEN_URL = "https://auth.openai.com/api/accounts/deviceauth/token";
|
|
21
23
|
const DEVICE_REDIRECT_URI = "https://auth.openai.com/deviceauth/callback";
|
|
@@ -63,6 +65,50 @@ interface PKCE {
|
|
|
63
65
|
challenge: string;
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Display name of the ChatGPT workspace a token is bound to, for account labels and `org:` selectors.
|
|
70
|
+
* One ChatGPT login can belong to several workspaces (a personal plan and Team/Business workspaces),
|
|
71
|
+
* and the token only carries the workspace id. The personal workspace has no name and is labeled
|
|
72
|
+
* "Personal". The lookup is best-effort: any failure leaves the name unset and never fails the login.
|
|
73
|
+
* It runs only at login, never on refresh: a refresh has already rotated the refresh token upstream and
|
|
74
|
+
* must not spend its timeout budget on a lookup. Refresh merges keep the stored name.
|
|
75
|
+
*/
|
|
76
|
+
async function fetchWorkspaceName(accessToken: string, workspaceId: string): Promise<string | undefined> {
|
|
77
|
+
try {
|
|
78
|
+
const response = await fetch(WORKSPACE_LOOKUP_URL, {
|
|
79
|
+
headers: {
|
|
80
|
+
Authorization: `Bearer ${accessToken}`,
|
|
81
|
+
"ChatGPT-Account-Id": workspaceId,
|
|
82
|
+
[OPENAI_HEADERS.ORIGINATOR]: OPENAI_HEADER_VALUES.ORIGINATOR_CODEX,
|
|
83
|
+
},
|
|
84
|
+
signal: AbortSignal.timeout(WORKSPACE_LOOKUP_TIMEOUT_MS),
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok) return undefined;
|
|
87
|
+
const body = (await response.json()) as { accounts?: unknown };
|
|
88
|
+
if (!Array.isArray(body.accounts)) return undefined;
|
|
89
|
+
const workspace = body.accounts.find(
|
|
90
|
+
(account): account is { name?: unknown; structure?: unknown } =>
|
|
91
|
+
typeof account === "object" && account !== null && (account as { id?: unknown }).id === workspaceId,
|
|
92
|
+
);
|
|
93
|
+
if (!workspace) return undefined;
|
|
94
|
+
const name = typeof workspace.name === "string" ? workspace.name.trim() : "";
|
|
95
|
+
if (name) return name;
|
|
96
|
+
return workspace.structure === "personal" ? "Personal" : undefined;
|
|
97
|
+
} catch {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Identity fields of a Codex token. The ChatGPT workspace id (`chatgpt_account_id`) is also the
|
|
104
|
+
* credential's organization, so each workspace of one login is stored, ranked, and preferred as its
|
|
105
|
+
* own credential instead of replacing the other workspaces of the same email.
|
|
106
|
+
*/
|
|
107
|
+
function getTokenIdentity(accessToken: string): Pick<OAuthCredentials, "accountId" | "email" | "organizationId"> {
|
|
108
|
+
const { accountId, email } = getTokenProfile(accessToken);
|
|
109
|
+
return accountId ? { accountId, email, organizationId: accountId } : { email };
|
|
110
|
+
}
|
|
111
|
+
|
|
66
112
|
/**
|
|
67
113
|
* Extract a readable error message from OpenAI OAuth error response.
|
|
68
114
|
* Handles both string errors (error + error_description) and object errors (code/type + message).
|
|
@@ -172,17 +218,18 @@ async function exchangeCodeForToken(code: string, verifier: string, redirectUri:
|
|
|
172
218
|
throw new Error("Token response missing required fields");
|
|
173
219
|
}
|
|
174
220
|
|
|
175
|
-
const
|
|
176
|
-
if (!accountId) {
|
|
221
|
+
const identity = getTokenIdentity(tokenData.access_token);
|
|
222
|
+
if (!identity.accountId) {
|
|
177
223
|
throw new Error("Failed to extract accountId from token");
|
|
178
224
|
}
|
|
225
|
+
const organizationName = await fetchWorkspaceName(tokenData.access_token, identity.accountId);
|
|
179
226
|
|
|
180
227
|
return {
|
|
181
228
|
access: tokenData.access_token,
|
|
182
229
|
refresh: tokenData.refresh_token,
|
|
183
230
|
expires: Date.now() + tokenData.expires_in * 1000,
|
|
184
|
-
|
|
185
|
-
|
|
231
|
+
...identity,
|
|
232
|
+
...(organizationName ? { organizationName } : {}),
|
|
186
233
|
};
|
|
187
234
|
}
|
|
188
235
|
|
|
@@ -324,13 +371,10 @@ export async function refreshOpenAICodexToken(refreshToken: string): Promise<OAu
|
|
|
324
371
|
throw new Error("Token response missing required fields");
|
|
325
372
|
}
|
|
326
373
|
|
|
327
|
-
const { accountId, email } = getTokenProfile(tokenData.access_token);
|
|
328
|
-
|
|
329
374
|
return {
|
|
330
375
|
access: tokenData.access_token,
|
|
331
376
|
refresh: tokenData.refresh_token || refreshToken,
|
|
332
377
|
expires: Date.now() + tokenData.expires_in * 1000,
|
|
333
|
-
|
|
334
|
-
email,
|
|
378
|
+
...getTokenIdentity(tokenData.access_token),
|
|
335
379
|
};
|
|
336
380
|
}
|
|
@@ -9,9 +9,10 @@ export type OAuthCredentials = {
|
|
|
9
9
|
profileArn?: string;
|
|
10
10
|
/**
|
|
11
11
|
* Organization the token is scoped to. One Anthropic account can belong to
|
|
12
|
-
* several organizations (e.g. a Team workspace and a personal Max plan)
|
|
13
|
-
* login
|
|
14
|
-
*
|
|
12
|
+
* several organizations (e.g. a Team workspace and a personal Max plan), and one
|
|
13
|
+
* ChatGPT login can belong to several workspaces (for OpenAI Codex this is the
|
|
14
|
+
* workspace id, `chatgpt_account_id`). Each login is bound to exactly one of
|
|
15
|
+
* them, so the organization is part of the credential's identity.
|
|
15
16
|
*/
|
|
16
17
|
organizationId?: string;
|
|
17
18
|
/** Display name of {@link organizationId}. */
|
package/src/utils/oauth/types.ts
CHANGED
|
@@ -9,9 +9,10 @@ export type OAuthCredentials = {
|
|
|
9
9
|
profileArn?: string;
|
|
10
10
|
/**
|
|
11
11
|
* Organization the token is scoped to. One Anthropic account can belong to
|
|
12
|
-
* several organizations (e.g. a Team workspace and a personal Max plan)
|
|
13
|
-
* login
|
|
14
|
-
*
|
|
12
|
+
* several organizations (e.g. a Team workspace and a personal Max plan), and one
|
|
13
|
+
* ChatGPT login can belong to several workspaces (for OpenAI Codex this is the
|
|
14
|
+
* workspace id, `chatgpt_account_id`). Each login is bound to exactly one of
|
|
15
|
+
* them, so the organization is part of the credential's identity.
|
|
15
16
|
*/
|
|
16
17
|
organizationId?: string;
|
|
17
18
|
/** Display name of {@link organizationId}. */
|