@bitkyc08/opencodex 2.1.7 → 2.1.9
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/gui/dist/assets/{index-BVahEsvB.js → index-DVvcVBD_.js} +1 -1
- package/gui/dist/assets/index-xJdeQjzZ.css +1 -0
- package/gui/dist/index.html +2 -2
- package/package.json +2 -1
- package/src/adapters/anthropic.ts +88 -20
- package/src/adapters/openai-responses.ts +12 -0
- package/src/bridge.ts +21 -1
- package/src/codex-account-label.ts +34 -0
- package/src/codex-account-lifecycle.ts +21 -0
- package/src/codex-account-runtime-state.ts +13 -0
- package/src/codex-account-store.ts +355 -0
- package/src/codex-account-usability.ts +10 -0
- package/src/codex-auth-api.ts +446 -0
- package/src/codex-auth-collision.ts +66 -0
- package/src/codex-auth-context.ts +136 -0
- package/src/codex-catalog.ts +14 -4
- package/src/codex-quota.ts +130 -0
- package/src/codex-routing.ts +382 -0
- package/src/codex-websocket-registry.ts +57 -0
- package/src/config.ts +86 -26
- package/src/debug.ts +5 -4
- package/src/oauth/chatgpt.ts +150 -0
- package/src/oauth/index.ts +35 -7
- package/src/oauth/store.ts +9 -5
- package/src/privacy.ts +11 -0
- package/src/router.ts +1 -1
- package/src/server.ts +360 -23
- package/src/types.ts +38 -0
- package/src/vision/describe.ts +7 -3
- package/src/vision/index.ts +7 -3
- package/src/web-search/executor.ts +8 -3
- package/src/web-search/index.ts +3 -1
- package/src/web-search/loop.ts +6 -5
- package/src/ws-bridge.ts +56 -10
- package/gui/dist/assets/index-dCS-lwCM.css +0 -1
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, existsSync, readFileSync, mkdirSync, openSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { getConfigDir, atomicWriteFile, hardenConfigDir, hardenExistingSecret } from "./config";
|
|
5
|
+
import type { CodexAccountCredentialRecord, CodexAccountCredentials } from "./types";
|
|
6
|
+
|
|
7
|
+
type LegacyCodexAccountStore = Record<string, CodexAccountCredentials>;
|
|
8
|
+
type CodexAccountStore = Record<string, CodexAccountCredentialRecord>;
|
|
9
|
+
type RawCodexAccountStore = Record<string, CodexAccountCredentials | CodexAccountCredentialRecord>;
|
|
10
|
+
|
|
11
|
+
const REFRESH_SKEW_MS = 60_000;
|
|
12
|
+
const REFRESH_LOCK_STALE_MS = 60_000;
|
|
13
|
+
const REFRESH_LOCK_WAIT_MS = REFRESH_LOCK_STALE_MS + 5_000;
|
|
14
|
+
const REFRESH_LOCK_POLL_MS = 50;
|
|
15
|
+
|
|
16
|
+
function codexAccountsPath(): string {
|
|
17
|
+
return join(getConfigDir(), "codex-accounts.json");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function loadCodexAccountStore(): LegacyCodexAccountStore {
|
|
21
|
+
const records = loadCodexAccountRecordStore();
|
|
22
|
+
const credentials: LegacyCodexAccountStore = {};
|
|
23
|
+
for (const [id, record] of Object.entries(records)) {
|
|
24
|
+
if (record.deletedAt == null && record.credential) credentials[id] = record.credential;
|
|
25
|
+
}
|
|
26
|
+
return credentials;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
30
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isCredential(value: unknown): value is CodexAccountCredentials {
|
|
34
|
+
return isObject(value)
|
|
35
|
+
&& typeof value.accessToken === "string"
|
|
36
|
+
&& typeof value.refreshToken === "string"
|
|
37
|
+
&& typeof value.expiresAt === "number"
|
|
38
|
+
&& typeof value.chatgptAccountId === "string";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isCredentialRecord(value: unknown): value is CodexAccountCredentialRecord {
|
|
42
|
+
return isObject(value)
|
|
43
|
+
&& typeof value.generation === "number"
|
|
44
|
+
&& (value.credential === undefined || isCredential(value.credential))
|
|
45
|
+
&& (value.refreshGrantFingerprint === undefined || typeof value.refreshGrantFingerprint === "string")
|
|
46
|
+
&& (value.deletedAt === undefined || typeof value.deletedAt === "number")
|
|
47
|
+
&& (value.replacedAt === undefined || typeof value.replacedAt === "number");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function refreshGrantFingerprintForToken(refreshToken: string): string {
|
|
51
|
+
return createHash("sha256").update(`codex-refresh-grant:${refreshToken}`).digest("hex");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function recordGrantFingerprint(record: CodexAccountCredentialRecord): string | undefined {
|
|
55
|
+
return record.refreshGrantFingerprint ?? (
|
|
56
|
+
record.credential ? refreshGrantFingerprintForToken(record.credential.refreshToken) : undefined
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function normalizeRecord(value: CodexAccountCredentials | CodexAccountCredentialRecord | undefined): CodexAccountCredentialRecord | undefined {
|
|
61
|
+
if (!value) return undefined;
|
|
62
|
+
if (isCredentialRecord(value)) {
|
|
63
|
+
const refreshGrantFingerprint = recordGrantFingerprint(value);
|
|
64
|
+
return refreshGrantFingerprint ? { ...value, refreshGrantFingerprint } : value;
|
|
65
|
+
}
|
|
66
|
+
if (isCredential(value)) {
|
|
67
|
+
return {
|
|
68
|
+
credential: value,
|
|
69
|
+
generation: 0,
|
|
70
|
+
refreshGrantFingerprint: refreshGrantFingerprintForToken(value.refreshToken),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function loadCodexAccountRecordStore(): CodexAccountStore {
|
|
77
|
+
const path = codexAccountsPath();
|
|
78
|
+
hardenConfigDir();
|
|
79
|
+
hardenExistingSecret(path);
|
|
80
|
+
if (!existsSync(path)) return {};
|
|
81
|
+
try {
|
|
82
|
+
const raw = JSON.parse(readFileSync(path, "utf-8")) as RawCodexAccountStore;
|
|
83
|
+
const normalized: CodexAccountStore = {};
|
|
84
|
+
for (const [id, value] of Object.entries(raw)) {
|
|
85
|
+
const record = normalizeRecord(value);
|
|
86
|
+
if (record) normalized[id] = record;
|
|
87
|
+
}
|
|
88
|
+
return normalized;
|
|
89
|
+
} catch {
|
|
90
|
+
return {};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function persist(store: CodexAccountStore): void {
|
|
95
|
+
const dir = getConfigDir();
|
|
96
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
97
|
+
atomicWriteFile(codexAccountsPath(), JSON.stringify(store, null, 2) + "\n");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function getCodexAccountCredential(id: string): CodexAccountCredentials | null {
|
|
101
|
+
const record = readCodexAccountRecord(id);
|
|
102
|
+
if (!record || record.deletedAt != null) return null;
|
|
103
|
+
return record.credential ?? null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function saveCodexAccountCredential(id: string, cred: CodexAccountCredentials): void {
|
|
107
|
+
const store = loadCodexAccountRecordStore();
|
|
108
|
+
const current = store[id];
|
|
109
|
+
const refreshGrantFingerprint = current?.credential?.refreshToken === cred.refreshToken
|
|
110
|
+
? current.refreshGrantFingerprint ?? refreshGrantFingerprintForToken(cred.refreshToken)
|
|
111
|
+
: refreshGrantFingerprintForToken(cred.refreshToken);
|
|
112
|
+
store[id] = {
|
|
113
|
+
credential: cred,
|
|
114
|
+
generation: (current?.generation ?? 0) + 1,
|
|
115
|
+
refreshGrantFingerprint,
|
|
116
|
+
replacedAt: current ? Date.now() : undefined,
|
|
117
|
+
};
|
|
118
|
+
persist(store);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function removeCodexAccountCredential(id: string): void {
|
|
122
|
+
tombstoneCodexAccount(id);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function listCodexAccountIds(): string[] {
|
|
126
|
+
return Object.keys(loadCodexAccountStore());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function readCodexAccountRecord(id: string): CodexAccountCredentialRecord | null {
|
|
130
|
+
return loadCodexAccountRecordStore()[id] ?? null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function isCodexAccountGenerationLive(id: string, generation: number): boolean {
|
|
134
|
+
const record = readCodexAccountRecord(id);
|
|
135
|
+
return !!record?.credential && record.deletedAt == null && record.generation === generation;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function saveCodexAccountCredentialIfGeneration(
|
|
139
|
+
id: string,
|
|
140
|
+
generation: number,
|
|
141
|
+
cred: CodexAccountCredentials,
|
|
142
|
+
): boolean {
|
|
143
|
+
const store = loadCodexAccountRecordStore();
|
|
144
|
+
const current = store[id];
|
|
145
|
+
if (!current || current.generation !== generation || current.deletedAt != null || !current.credential) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
store[id] = {
|
|
149
|
+
credential: cred,
|
|
150
|
+
generation: generation + 1,
|
|
151
|
+
refreshGrantFingerprint: current.refreshGrantFingerprint ?? refreshGrantFingerprintForToken(current.credential.refreshToken),
|
|
152
|
+
replacedAt: current.replacedAt,
|
|
153
|
+
};
|
|
154
|
+
persist(store);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function tombstoneCodexAccount(id: string): number {
|
|
159
|
+
const store = loadCodexAccountRecordStore();
|
|
160
|
+
const current = store[id];
|
|
161
|
+
const generation = (current?.generation ?? 0) + 1;
|
|
162
|
+
store[id] = { generation, deletedAt: Date.now() };
|
|
163
|
+
persist(store);
|
|
164
|
+
return generation;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const CHATGPT_TOKEN_URL = "https://auth.openai.com/oauth/token";
|
|
168
|
+
const CHATGPT_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann";
|
|
169
|
+
|
|
170
|
+
export class TokenRefreshError extends Error {
|
|
171
|
+
reason: "expired" | "revoked" | "unknown";
|
|
172
|
+
constructor(reason: "expired" | "revoked" | "unknown", message: string) {
|
|
173
|
+
super(message);
|
|
174
|
+
this.name = "TokenRefreshError";
|
|
175
|
+
this.reason = reason;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export class CodexCredentialGenerationConflictError extends Error {
|
|
180
|
+
constructor(message = "Codex account changed during refresh") {
|
|
181
|
+
super(message);
|
|
182
|
+
this.name = "CodexCredentialGenerationConflictError";
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export class CodexCredentialRefreshLockTimeoutError extends Error {
|
|
187
|
+
constructor(message = "Timed out waiting for Codex account refresh lock") {
|
|
188
|
+
super(message);
|
|
189
|
+
this.name = "CodexCredentialRefreshLockTimeoutError";
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
type CodexTokenResult = { accessToken: string; chatgptAccountId: string; generation: number };
|
|
194
|
+
const refreshLocks = new Map<string, Promise<CodexTokenResult>>();
|
|
195
|
+
|
|
196
|
+
function codexRefreshLockPath(lockKey: string): string {
|
|
197
|
+
const digest = createHash("sha256").update(lockKey).digest("hex").slice(0, 32);
|
|
198
|
+
return join(getConfigDir(), `codex-refresh-${digest}.lock`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function sleep(ms: number): Promise<void> {
|
|
202
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function errCode(err: unknown): string | undefined {
|
|
206
|
+
return err && typeof err === "object" && "code" in err ? String((err as { code?: unknown }).code) : undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function isRefreshLockStale(path: string): boolean {
|
|
210
|
+
try {
|
|
211
|
+
hardenExistingSecret(path);
|
|
212
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8")) as { acquiredAt?: unknown };
|
|
213
|
+
return typeof parsed.acquiredAt !== "number" || Date.now() - parsed.acquiredAt > REFRESH_LOCK_STALE_MS;
|
|
214
|
+
} catch {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function withCodexRefreshFileLock<T>(lockKey: string, fn: () => Promise<T>): Promise<T> {
|
|
220
|
+
hardenConfigDir();
|
|
221
|
+
const dir = getConfigDir();
|
|
222
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
223
|
+
|
|
224
|
+
const path = codexRefreshLockPath(lockKey);
|
|
225
|
+
const deadline = Date.now() + REFRESH_LOCK_WAIT_MS;
|
|
226
|
+
let fd: number | null = null;
|
|
227
|
+
while (fd == null) {
|
|
228
|
+
try {
|
|
229
|
+
fd = openSync(path, "wx", 0o600);
|
|
230
|
+
writeFileSync(fd, JSON.stringify({ acquiredAt: Date.now(), pid: process.pid }) + "\n");
|
|
231
|
+
break;
|
|
232
|
+
} catch (err) {
|
|
233
|
+
if (errCode(err) !== "EEXIST") throw err;
|
|
234
|
+
if (isRefreshLockStale(path)) {
|
|
235
|
+
try {
|
|
236
|
+
unlinkSync(path);
|
|
237
|
+
} catch (unlinkErr) {
|
|
238
|
+
if (errCode(unlinkErr) !== "ENOENT") throw unlinkErr;
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (Date.now() >= deadline) throw new CodexCredentialRefreshLockTimeoutError();
|
|
243
|
+
await sleep(REFRESH_LOCK_POLL_MS);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
return await fn();
|
|
249
|
+
} finally {
|
|
250
|
+
if (fd != null) closeSync(fd);
|
|
251
|
+
try {
|
|
252
|
+
unlinkSync(path);
|
|
253
|
+
} catch (err) {
|
|
254
|
+
if (errCode(err) !== "ENOENT") throw err;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function findFreshCredentialForGrant(
|
|
260
|
+
refreshGrantFingerprint: string,
|
|
261
|
+
excludeId: string,
|
|
262
|
+
): CodexAccountCredentials | null {
|
|
263
|
+
const now = Date.now();
|
|
264
|
+
const records = loadCodexAccountRecordStore();
|
|
265
|
+
for (const [candidateId, candidate] of Object.entries(records)) {
|
|
266
|
+
if (candidateId === excludeId || candidate.deletedAt != null || !candidate.credential) continue;
|
|
267
|
+
if (recordGrantFingerprint(candidate) !== refreshGrantFingerprint) continue;
|
|
268
|
+
if (candidate.credential.expiresAt > now + REFRESH_SKEW_MS) return candidate.credential;
|
|
269
|
+
}
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export async function getValidCodexToken(id: string): Promise<CodexTokenResult> {
|
|
274
|
+
const record = readCodexAccountRecord(id);
|
|
275
|
+
const cred = record?.deletedAt == null ? record?.credential : undefined;
|
|
276
|
+
if (!record || !cred) throw new Error("Codex account credential is unavailable; reauthenticate the account.");
|
|
277
|
+
const refreshGrantFingerprint = recordGrantFingerprint(record);
|
|
278
|
+
if (!refreshGrantFingerprint) throw new Error("Codex account credential is unavailable; reauthenticate the account.");
|
|
279
|
+
|
|
280
|
+
if (cred.expiresAt > Date.now() + REFRESH_SKEW_MS) {
|
|
281
|
+
return { accessToken: cred.accessToken, chatgptAccountId: cred.chatgptAccountId, generation: record.generation };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const existing = refreshLocks.get(refreshGrantFingerprint);
|
|
285
|
+
if (existing) {
|
|
286
|
+
await existing;
|
|
287
|
+
return getValidCodexToken(id);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const refreshPromise = withCodexRefreshFileLock(refreshGrantFingerprint, async (): Promise<CodexTokenResult> => {
|
|
291
|
+
const lockedRecord = readCodexAccountRecord(id);
|
|
292
|
+
const lockedCred = lockedRecord?.deletedAt == null ? lockedRecord?.credential : undefined;
|
|
293
|
+
if (!lockedRecord || !lockedCred) throw new CodexCredentialGenerationConflictError();
|
|
294
|
+
const startGeneration = lockedRecord.generation;
|
|
295
|
+
const lockedRefreshGrantFingerprint = recordGrantFingerprint(lockedRecord);
|
|
296
|
+
if (lockedRefreshGrantFingerprint !== refreshGrantFingerprint) throw new CodexCredentialGenerationConflictError();
|
|
297
|
+
if (lockedCred.expiresAt > Date.now() + REFRESH_SKEW_MS) {
|
|
298
|
+
return {
|
|
299
|
+
accessToken: lockedCred.accessToken,
|
|
300
|
+
chatgptAccountId: lockedCred.chatgptAccountId,
|
|
301
|
+
generation: startGeneration,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
const sameGrantFreshCredential = findFreshCredentialForGrant(refreshGrantFingerprint, id);
|
|
305
|
+
if (sameGrantFreshCredential) {
|
|
306
|
+
if (!saveCodexAccountCredentialIfGeneration(id, startGeneration, sameGrantFreshCredential)) {
|
|
307
|
+
throw new CodexCredentialGenerationConflictError();
|
|
308
|
+
}
|
|
309
|
+
return {
|
|
310
|
+
accessToken: sameGrantFreshCredential.accessToken,
|
|
311
|
+
chatgptAccountId: sameGrantFreshCredential.chatgptAccountId,
|
|
312
|
+
generation: startGeneration + 1,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
const res = await fetch(CHATGPT_TOKEN_URL, {
|
|
316
|
+
method: "POST",
|
|
317
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
318
|
+
body: new URLSearchParams({
|
|
319
|
+
grant_type: "refresh_token",
|
|
320
|
+
client_id: CHATGPT_CLIENT_ID,
|
|
321
|
+
refresh_token: lockedCred.refreshToken,
|
|
322
|
+
}).toString(),
|
|
323
|
+
signal: AbortSignal.timeout(30_000),
|
|
324
|
+
});
|
|
325
|
+
if (!res.ok) {
|
|
326
|
+
const errText = await res.text().catch(() => "");
|
|
327
|
+
let errDesc: string;
|
|
328
|
+
try {
|
|
329
|
+
const parsed = JSON.parse(errText) as { error?: string; error_description?: string };
|
|
330
|
+
errDesc = [parsed.error, parsed.error_description].filter(Boolean).join(": ") || `HTTP ${res.status}`;
|
|
331
|
+
} catch { errDesc = `HTTP ${res.status}`; }
|
|
332
|
+
const reason = errDesc.includes("invalidated") || errDesc.includes("revoked") ? "revoked" as const
|
|
333
|
+
: errDesc.includes("expired") ? "expired" as const
|
|
334
|
+
: "unknown" as const;
|
|
335
|
+
throw new TokenRefreshError(reason, `Codex token refresh failed (${reason}); reauthenticate the account.`);
|
|
336
|
+
}
|
|
337
|
+
const data = (await res.json()) as { access_token: string; refresh_token?: string; expires_in: number };
|
|
338
|
+
|
|
339
|
+
const updated: CodexAccountCredentials = {
|
|
340
|
+
accessToken: data.access_token,
|
|
341
|
+
refreshToken: data.refresh_token ?? lockedCred.refreshToken,
|
|
342
|
+
expiresAt: Date.now() + data.expires_in * 1000,
|
|
343
|
+
chatgptAccountId: lockedCred.chatgptAccountId,
|
|
344
|
+
};
|
|
345
|
+
if (!saveCodexAccountCredentialIfGeneration(id, startGeneration, updated)) {
|
|
346
|
+
throw new CodexCredentialGenerationConflictError();
|
|
347
|
+
}
|
|
348
|
+
return { accessToken: updated.accessToken, chatgptAccountId: updated.chatgptAccountId, generation: startGeneration + 1 };
|
|
349
|
+
}).finally(() => {
|
|
350
|
+
refreshLocks.delete(refreshGrantFingerprint);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
refreshLocks.set(refreshGrantFingerprint, refreshPromise);
|
|
354
|
+
return refreshPromise;
|
|
355
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { getCodexAccountCredential } from "./codex-account-store";
|
|
2
|
+
import { isAccountNeedsReauth } from "./codex-account-runtime-state";
|
|
3
|
+
import type { OcxConfig } from "./types";
|
|
4
|
+
|
|
5
|
+
export function isCodexAccountUsable(config: OcxConfig, accountId: string): boolean {
|
|
6
|
+
const exists = (config.codexAccounts ?? []).some(account => !account.isMain && account.id === accountId);
|
|
7
|
+
if (!exists) return false;
|
|
8
|
+
if (isAccountNeedsReauth(accountId)) return false;
|
|
9
|
+
return !!getCodexAccountCredential(accountId);
|
|
10
|
+
}
|