@zmeel/adapter-codex-local 0.1.0
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/LICENSE +21 -0
- package/dist/cli/format-event.d.ts +2 -0
- package/dist/cli/format-event.d.ts.map +1 -0
- package/dist/cli/format-event.js +213 -0
- package/dist/cli/format-event.js.map +1 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/quota-probe.d.ts +3 -0
- package/dist/cli/quota-probe.d.ts.map +1 -0
- package/dist/cli/quota-probe.js +97 -0
- package/dist/cli/quota-probe.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/server/codex-home.d.ts +6 -0
- package/dist/server/codex-home.d.ts.map +1 -0
- package/dist/server/codex-home.js +78 -0
- package/dist/server/codex-home.js.map +1 -0
- package/dist/server/execute.d.ts +15 -0
- package/dist/server/execute.d.ts.map +1 -0
- package/dist/server/execute.js +496 -0
- package/dist/server/execute.js.map +1 -0
- package/dist/server/index.d.ts +8 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +57 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/parse.d.ts +12 -0
- package/dist/server/parse.d.ts.map +1 -0
- package/dist/server/parse.js +67 -0
- package/dist/server/parse.js.map +1 -0
- package/dist/server/quota-spawn-error.test.d.ts +2 -0
- package/dist/server/quota-spawn-error.test.d.ts.map +1 -0
- package/dist/server/quota-spawn-error.test.js +77 -0
- package/dist/server/quota-spawn-error.test.js.map +1 -0
- package/dist/server/quota.d.ts +64 -0
- package/dist/server/quota.d.ts.map +1 -0
- package/dist/server/quota.js +432 -0
- package/dist/server/quota.js.map +1 -0
- package/dist/server/skills.d.ts +8 -0
- package/dist/server/skills.d.ts.map +1 -0
- package/dist/server/skills.js +65 -0
- package/dist/server/skills.js.map +1 -0
- package/dist/server/test.d.ts +3 -0
- package/dist/server/test.d.ts.map +1 -0
- package/dist/server/test.js +207 -0
- package/dist/server/test.js.map +1 -0
- package/dist/ui/build-config.d.ts +3 -0
- package/dist/ui/build-config.d.ts.map +1 -0
- package/dist/ui/build-config.js +116 -0
- package/dist/ui/build-config.js.map +1 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +3 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/parse-stdout.d.ts +3 -0
- package/dist/ui/parse-stdout.d.ts.map +1 -0
- package/dist/ui/parse-stdout.js +232 -0
- package/dist/ui/parse-stdout.js.map +1 -0
- package/package.json +56 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { asString, asNumber, parseObject, parseJson } from "@zmeel/adapter-utils/server-utils";
|
|
2
|
+
export function parseCodexJsonl(stdout) {
|
|
3
|
+
let sessionId = null;
|
|
4
|
+
const messages = [];
|
|
5
|
+
let errorMessage = null;
|
|
6
|
+
const usage = {
|
|
7
|
+
inputTokens: 0,
|
|
8
|
+
cachedInputTokens: 0,
|
|
9
|
+
outputTokens: 0,
|
|
10
|
+
};
|
|
11
|
+
for (const rawLine of stdout.split(/\r?\n/)) {
|
|
12
|
+
const line = rawLine.trim();
|
|
13
|
+
if (!line)
|
|
14
|
+
continue;
|
|
15
|
+
const event = parseJson(line);
|
|
16
|
+
if (!event)
|
|
17
|
+
continue;
|
|
18
|
+
const type = asString(event.type, "");
|
|
19
|
+
if (type === "thread.started") {
|
|
20
|
+
sessionId = asString(event.thread_id, sessionId ?? "") || sessionId;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (type === "error") {
|
|
24
|
+
const msg = asString(event.message, "").trim();
|
|
25
|
+
if (msg)
|
|
26
|
+
errorMessage = msg;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (type === "item.completed") {
|
|
30
|
+
const item = parseObject(event.item);
|
|
31
|
+
if (asString(item.type, "") === "agent_message") {
|
|
32
|
+
const text = asString(item.text, "");
|
|
33
|
+
if (text)
|
|
34
|
+
messages.push(text);
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (type === "turn.completed") {
|
|
39
|
+
const usageObj = parseObject(event.usage);
|
|
40
|
+
usage.inputTokens = asNumber(usageObj.input_tokens, usage.inputTokens);
|
|
41
|
+
usage.cachedInputTokens = asNumber(usageObj.cached_input_tokens, usage.cachedInputTokens);
|
|
42
|
+
usage.outputTokens = asNumber(usageObj.output_tokens, usage.outputTokens);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (type === "turn.failed") {
|
|
46
|
+
const err = parseObject(event.error);
|
|
47
|
+
const msg = asString(err.message, "").trim();
|
|
48
|
+
if (msg)
|
|
49
|
+
errorMessage = msg;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
sessionId,
|
|
54
|
+
summary: messages.join("\n\n").trim(),
|
|
55
|
+
usage,
|
|
56
|
+
errorMessage,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function isCodexUnknownSessionError(stdout, stderr) {
|
|
60
|
+
const haystack = `${stdout}\n${stderr}`
|
|
61
|
+
.split(/\r?\n/)
|
|
62
|
+
.map((line) => line.trim())
|
|
63
|
+
.filter(Boolean)
|
|
64
|
+
.join("\n");
|
|
65
|
+
return /unknown (session|thread)|session .* not found|thread .* not found|conversation .* not found|missing rollout path for thread|state db missing rollout path/i.test(haystack);
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../../src/server/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAE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SACf,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,OAAO,4JAA4J,CAAC,IAAI,CACtK,QAAQ,CACT,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota-spawn-error.test.d.ts","sourceRoot":"","sources":["../../src/server/quota-spawn-error.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
|
6
|
+
const { mockSpawn } = vi.hoisted(() => ({
|
|
7
|
+
mockSpawn: vi.fn(),
|
|
8
|
+
}));
|
|
9
|
+
vi.mock("node:child_process", async (importOriginal) => {
|
|
10
|
+
const cp = await importOriginal();
|
|
11
|
+
return {
|
|
12
|
+
...cp,
|
|
13
|
+
spawn: (...args) => mockSpawn(...args),
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
import { getQuotaWindows } from "./quota.js";
|
|
17
|
+
function createChildThatErrorsOnMicrotask(err) {
|
|
18
|
+
const child = new EventEmitter();
|
|
19
|
+
const stream = Object.assign(new EventEmitter(), {
|
|
20
|
+
setEncoding: () => { },
|
|
21
|
+
});
|
|
22
|
+
Object.assign(child, {
|
|
23
|
+
stdout: stream,
|
|
24
|
+
stderr: Object.assign(new EventEmitter(), { setEncoding: () => { } }),
|
|
25
|
+
stdin: { write: vi.fn(), end: vi.fn() },
|
|
26
|
+
kill: vi.fn(),
|
|
27
|
+
});
|
|
28
|
+
queueMicrotask(() => {
|
|
29
|
+
child.emit("error", err);
|
|
30
|
+
});
|
|
31
|
+
return child;
|
|
32
|
+
}
|
|
33
|
+
describe("CodexRpcClient spawn failures", () => {
|
|
34
|
+
let previousCodexHome;
|
|
35
|
+
let isolatedCodexHome;
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
mockSpawn.mockReset();
|
|
38
|
+
// After the RPC path fails, getQuotaWindows() calls readCodexToken() which
|
|
39
|
+
// reads $CODEX_HOME/auth.json (default ~/.codex). Point CODEX_HOME at an
|
|
40
|
+
// empty temp directory so we never hit real host auth or the WHAM network.
|
|
41
|
+
previousCodexHome = process.env.CODEX_HOME;
|
|
42
|
+
isolatedCodexHome = fs.mkdtempSync(path.join(os.tmpdir(), "zmeel-codex-spawn-test-"));
|
|
43
|
+
process.env.CODEX_HOME = isolatedCodexHome;
|
|
44
|
+
});
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
if (isolatedCodexHome) {
|
|
47
|
+
try {
|
|
48
|
+
fs.rmSync(isolatedCodexHome, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
/* ignore */
|
|
52
|
+
}
|
|
53
|
+
isolatedCodexHome = undefined;
|
|
54
|
+
}
|
|
55
|
+
if (previousCodexHome === undefined) {
|
|
56
|
+
delete process.env.CODEX_HOME;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
process.env.CODEX_HOME = previousCodexHome;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
it("does not crash the process when codex is missing; getQuotaWindows returns ok: false", async () => {
|
|
63
|
+
const enoent = Object.assign(new Error("spawn codex ENOENT"), {
|
|
64
|
+
code: "ENOENT",
|
|
65
|
+
errno: -2,
|
|
66
|
+
syscall: "spawn codex",
|
|
67
|
+
path: "codex",
|
|
68
|
+
});
|
|
69
|
+
mockSpawn.mockImplementation(() => createChildThatErrorsOnMicrotask(enoent));
|
|
70
|
+
const result = await getQuotaWindows();
|
|
71
|
+
expect(result.ok).toBe(false);
|
|
72
|
+
expect(result.windows).toEqual([]);
|
|
73
|
+
expect(result.error).toContain("Codex app-server");
|
|
74
|
+
expect(result.error).toContain("spawn codex ENOENT");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
//# sourceMappingURL=quota-spawn-error.test.js.map
|
|
@@ -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GAAG,CAAC,UAAU,GAAG,iBAAiB,CAAC;QAC7C,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qFAAqF,EAAE,KAAK,IAAI,EAAE;QACnG,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,oBAAoB,CAAC,EAAE;YAC5D,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,CAAC,CAAC;YACT,OAAO,EAAE,aAAa;YACtB,IAAI,EAAE,OAAO;SACd,CAAC,CAAC;QACH,SAAS,CAAC,kBAAkB,CAAC,GAAG,EAAE,CAAC,gCAAgC,CAAC,MAAM,CAAC,CAAC,CAAC;QAE7E,MAAM,MAAM,GAAG,MAAM,eAAe,EAAE,CAAC;QAEvC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACnC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;QACnD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,oBAAoB,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ProviderQuotaResult, QuotaWindow } from "@zmeel/adapter-utils";
|
|
2
|
+
export declare function codexHomeDir(): string;
|
|
3
|
+
export interface CodexAuthInfo {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
accountId: string | null;
|
|
6
|
+
refreshToken: string | null;
|
|
7
|
+
idToken: string | null;
|
|
8
|
+
email: string | null;
|
|
9
|
+
planType: string | null;
|
|
10
|
+
lastRefresh: string | null;
|
|
11
|
+
}
|
|
12
|
+
export declare function readCodexAuthInfo(codexHome?: string): Promise<CodexAuthInfo | null>;
|
|
13
|
+
export declare function readCodexToken(): Promise<{
|
|
14
|
+
token: string;
|
|
15
|
+
accountId: string | null;
|
|
16
|
+
} | null>;
|
|
17
|
+
/**
|
|
18
|
+
* Map a window duration in seconds to a human-readable label.
|
|
19
|
+
* Falls back to the provided fallback string when seconds is null/undefined.
|
|
20
|
+
*/
|
|
21
|
+
export declare function secondsToWindowLabel(seconds: number | null | undefined, fallback: string): string;
|
|
22
|
+
/** fetch with an abort-based timeout so a hanging provider api doesn't block the response indefinitely */
|
|
23
|
+
export declare function fetchWithTimeout(url: string, init: RequestInit, ms?: number): Promise<Response>;
|
|
24
|
+
export declare function fetchCodexQuota(token: string, accountId: string | null): Promise<QuotaWindow[]>;
|
|
25
|
+
interface CodexRpcWindow {
|
|
26
|
+
usedPercent?: number | null;
|
|
27
|
+
windowDurationMins?: number | null;
|
|
28
|
+
resetsAt?: number | null;
|
|
29
|
+
}
|
|
30
|
+
interface CodexRpcCredits {
|
|
31
|
+
hasCredits?: boolean | null;
|
|
32
|
+
unlimited?: boolean | null;
|
|
33
|
+
balance?: string | number | null;
|
|
34
|
+
}
|
|
35
|
+
interface CodexRpcLimit {
|
|
36
|
+
limitId?: string | null;
|
|
37
|
+
limitName?: string | null;
|
|
38
|
+
primary?: CodexRpcWindow | null;
|
|
39
|
+
secondary?: CodexRpcWindow | null;
|
|
40
|
+
credits?: CodexRpcCredits | null;
|
|
41
|
+
planType?: string | null;
|
|
42
|
+
}
|
|
43
|
+
interface CodexRpcRateLimitsResult {
|
|
44
|
+
rateLimits?: CodexRpcLimit | null;
|
|
45
|
+
rateLimitsByLimitId?: Record<string, CodexRpcLimit> | null;
|
|
46
|
+
}
|
|
47
|
+
interface CodexRpcAccountResult {
|
|
48
|
+
account?: {
|
|
49
|
+
type?: string | null;
|
|
50
|
+
email?: string | null;
|
|
51
|
+
planType?: string | null;
|
|
52
|
+
} | null;
|
|
53
|
+
requiresOpenaiAuth?: boolean | null;
|
|
54
|
+
}
|
|
55
|
+
export interface CodexRpcQuotaSnapshot {
|
|
56
|
+
windows: QuotaWindow[];
|
|
57
|
+
email: string | null;
|
|
58
|
+
planType: string | null;
|
|
59
|
+
}
|
|
60
|
+
export declare function mapCodexRpcQuota(result: CodexRpcRateLimitsResult, account?: CodexRpcAccountResult | null): CodexRpcQuotaSnapshot;
|
|
61
|
+
export declare function fetchCodexRpcQuota(): Promise<CodexRpcQuotaSnapshot>;
|
|
62
|
+
export declare function getQuotaWindows(): Promise<ProviderQuotaResult>;
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=quota.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota.d.ts","sourceRoot":"","sources":["../../src/server/quota.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAK7E,wBAAgB,YAAY,IAAI,MAAM,CAIrC;AAoBD,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAoED,wBAAsB,iBAAiB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAoDzF;AAED,wBAAsB,cAAc,IAAI,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAAG,IAAI,CAAC,CAIlG;AAsBD;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAClC,QAAQ,EAAE,MAA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AG,IAAI,CAAC;QACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC1B,GAAG,IAAI,CAAC;IACT,kBAAkB,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CACrC;AAED,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAgCD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,wBAAwB,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,IAAI,GAAG,qBAAqB,CAiDhI;AAiHD,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAYzE;AAOD,wBAAsB,eAAe,IAAI,OAAO,CAAC,mBAAmB,CAAC,CA8BpE"}
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
const CODEX_USAGE_SOURCE_RPC = "codex-rpc";
|
|
6
|
+
const CODEX_USAGE_SOURCE_WHAM = "codex-wham";
|
|
7
|
+
export function codexHomeDir() {
|
|
8
|
+
const fromEnv = process.env.CODEX_HOME;
|
|
9
|
+
if (typeof fromEnv === "string" && fromEnv.trim().length > 0)
|
|
10
|
+
return fromEnv.trim();
|
|
11
|
+
return path.join(os.homedir(), ".codex");
|
|
12
|
+
}
|
|
13
|
+
function base64UrlDecode(input) {
|
|
14
|
+
try {
|
|
15
|
+
let normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
16
|
+
const remainder = normalized.length % 4;
|
|
17
|
+
if (remainder > 0)
|
|
18
|
+
normalized += "=".repeat(4 - remainder);
|
|
19
|
+
return Buffer.from(normalized, "base64").toString("utf8");
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function decodeJwtPayload(token) {
|
|
26
|
+
if (typeof token !== "string" || token.trim().length === 0)
|
|
27
|
+
return null;
|
|
28
|
+
const parts = token.split(".");
|
|
29
|
+
if (parts.length < 2)
|
|
30
|
+
return null;
|
|
31
|
+
const decoded = base64UrlDecode(parts[1] ?? "");
|
|
32
|
+
if (!decoded)
|
|
33
|
+
return null;
|
|
34
|
+
try {
|
|
35
|
+
const parsed = JSON.parse(decoded);
|
|
36
|
+
return typeof parsed === "object" && parsed !== null ? parsed : null;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function readNestedString(record, pathSegments) {
|
|
43
|
+
let current = record;
|
|
44
|
+
for (const segment of pathSegments) {
|
|
45
|
+
if (typeof current !== "object" || current === null || Array.isArray(current))
|
|
46
|
+
return null;
|
|
47
|
+
current = current[segment];
|
|
48
|
+
}
|
|
49
|
+
return typeof current === "string" && current.trim().length > 0 ? current.trim() : null;
|
|
50
|
+
}
|
|
51
|
+
function parsePlanAndEmailFromToken(idToken, accessToken) {
|
|
52
|
+
const payloads = [decodeJwtPayload(idToken), decodeJwtPayload(accessToken)].filter((value) => value != null);
|
|
53
|
+
for (const payload of payloads) {
|
|
54
|
+
const directEmail = typeof payload.email === "string" ? payload.email : null;
|
|
55
|
+
const authBlock = typeof payload["https://api.openai.com/auth"] === "object" &&
|
|
56
|
+
payload["https://api.openai.com/auth"] !== null &&
|
|
57
|
+
!Array.isArray(payload["https://api.openai.com/auth"])
|
|
58
|
+
? payload["https://api.openai.com/auth"]
|
|
59
|
+
: null;
|
|
60
|
+
const profileBlock = typeof payload["https://api.openai.com/profile"] === "object" &&
|
|
61
|
+
payload["https://api.openai.com/profile"] !== null &&
|
|
62
|
+
!Array.isArray(payload["https://api.openai.com/profile"])
|
|
63
|
+
? payload["https://api.openai.com/profile"]
|
|
64
|
+
: null;
|
|
65
|
+
const email = directEmail
|
|
66
|
+
?? (typeof profileBlock?.email === "string" ? profileBlock.email : null)
|
|
67
|
+
?? (typeof authBlock?.chatgpt_user_email === "string" ? authBlock.chatgpt_user_email : null);
|
|
68
|
+
const planType = typeof authBlock?.chatgpt_plan_type === "string" ? authBlock.chatgpt_plan_type : null;
|
|
69
|
+
if (email || planType)
|
|
70
|
+
return { email: email ?? null, planType };
|
|
71
|
+
}
|
|
72
|
+
return { email: null, planType: null };
|
|
73
|
+
}
|
|
74
|
+
export async function readCodexAuthInfo(codexHome) {
|
|
75
|
+
const authPath = path.join(codexHome ?? codexHomeDir(), "auth.json");
|
|
76
|
+
let raw;
|
|
77
|
+
try {
|
|
78
|
+
raw = await fs.readFile(authPath, "utf8");
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
let parsed;
|
|
84
|
+
try {
|
|
85
|
+
parsed = JSON.parse(raw);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
91
|
+
return null;
|
|
92
|
+
const obj = parsed;
|
|
93
|
+
const modern = obj;
|
|
94
|
+
const legacy = obj;
|
|
95
|
+
const accessToken = legacy.accessToken
|
|
96
|
+
?? modern.tokens?.access_token
|
|
97
|
+
?? readNestedString(obj, ["tokens", "access_token"]);
|
|
98
|
+
if (typeof accessToken !== "string" || accessToken.length === 0)
|
|
99
|
+
return null;
|
|
100
|
+
const accountId = legacy.accountId
|
|
101
|
+
?? modern.tokens?.account_id
|
|
102
|
+
?? readNestedString(obj, ["tokens", "account_id"]);
|
|
103
|
+
const refreshToken = modern.tokens?.refresh_token
|
|
104
|
+
?? readNestedString(obj, ["tokens", "refresh_token"]);
|
|
105
|
+
const idToken = modern.tokens?.id_token
|
|
106
|
+
?? readNestedString(obj, ["tokens", "id_token"]);
|
|
107
|
+
const { email, planType } = parsePlanAndEmailFromToken(idToken, accessToken);
|
|
108
|
+
return {
|
|
109
|
+
accessToken,
|
|
110
|
+
accountId: typeof accountId === "string" && accountId.trim().length > 0 ? accountId.trim() : null,
|
|
111
|
+
refreshToken: typeof refreshToken === "string" && refreshToken.trim().length > 0 ? refreshToken.trim() : null,
|
|
112
|
+
idToken: typeof idToken === "string" && idToken.trim().length > 0 ? idToken.trim() : null,
|
|
113
|
+
email,
|
|
114
|
+
planType,
|
|
115
|
+
lastRefresh: typeof modern.last_refresh === "string" && modern.last_refresh.trim().length > 0
|
|
116
|
+
? modern.last_refresh.trim()
|
|
117
|
+
: null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export async function readCodexToken() {
|
|
121
|
+
const auth = await readCodexAuthInfo();
|
|
122
|
+
if (!auth)
|
|
123
|
+
return null;
|
|
124
|
+
return { token: auth.accessToken, accountId: auth.accountId };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Map a window duration in seconds to a human-readable label.
|
|
128
|
+
* Falls back to the provided fallback string when seconds is null/undefined.
|
|
129
|
+
*/
|
|
130
|
+
export function secondsToWindowLabel(seconds, fallback) {
|
|
131
|
+
if (seconds == null)
|
|
132
|
+
return fallback;
|
|
133
|
+
const hours = seconds / 3600;
|
|
134
|
+
if (hours < 6)
|
|
135
|
+
return "5h";
|
|
136
|
+
if (hours <= 24)
|
|
137
|
+
return "24h";
|
|
138
|
+
if (hours <= 168)
|
|
139
|
+
return "7d";
|
|
140
|
+
return `${Math.round(hours / 24)}d`;
|
|
141
|
+
}
|
|
142
|
+
/** fetch with an abort-based timeout so a hanging provider api doesn't block the response indefinitely */
|
|
143
|
+
export async function fetchWithTimeout(url, init, ms = 8000) {
|
|
144
|
+
const controller = new AbortController();
|
|
145
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
146
|
+
try {
|
|
147
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function normalizeCodexUsedPercent(rawPct) {
|
|
154
|
+
if (rawPct == null)
|
|
155
|
+
return null;
|
|
156
|
+
return Math.min(100, Math.round(rawPct < 1 ? rawPct * 100 : rawPct));
|
|
157
|
+
}
|
|
158
|
+
export async function fetchCodexQuota(token, accountId) {
|
|
159
|
+
const headers = {
|
|
160
|
+
Authorization: `Bearer ${token}`,
|
|
161
|
+
};
|
|
162
|
+
if (accountId)
|
|
163
|
+
headers["ChatGPT-Account-Id"] = accountId;
|
|
164
|
+
const resp = await fetchWithTimeout("https://chatgpt.com/backend-api/wham/usage", { headers });
|
|
165
|
+
if (!resp.ok)
|
|
166
|
+
throw new Error(`chatgpt wham api returned ${resp.status}`);
|
|
167
|
+
const body = (await resp.json());
|
|
168
|
+
const windows = [];
|
|
169
|
+
const rateLimit = body.rate_limit;
|
|
170
|
+
if (rateLimit?.primary_window != null) {
|
|
171
|
+
const w = rateLimit.primary_window;
|
|
172
|
+
windows.push({
|
|
173
|
+
label: "5h limit",
|
|
174
|
+
usedPercent: normalizeCodexUsedPercent(w.used_percent),
|
|
175
|
+
resetsAt: typeof w.reset_at === "number"
|
|
176
|
+
? unixSecondsToIso(w.reset_at)
|
|
177
|
+
: (w.reset_at ?? null),
|
|
178
|
+
valueLabel: null,
|
|
179
|
+
detail: null,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (rateLimit?.secondary_window != null) {
|
|
183
|
+
const w = rateLimit.secondary_window;
|
|
184
|
+
windows.push({
|
|
185
|
+
label: "Weekly limit",
|
|
186
|
+
usedPercent: normalizeCodexUsedPercent(w.used_percent),
|
|
187
|
+
resetsAt: typeof w.reset_at === "number"
|
|
188
|
+
? unixSecondsToIso(w.reset_at)
|
|
189
|
+
: (w.reset_at ?? null),
|
|
190
|
+
valueLabel: null,
|
|
191
|
+
detail: null,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (body.credits != null && body.credits.unlimited !== true) {
|
|
195
|
+
const balance = body.credits.balance;
|
|
196
|
+
const valueLabel = balance != null ? `$${(balance / 100).toFixed(2)} remaining` : "N/A";
|
|
197
|
+
windows.push({
|
|
198
|
+
label: "Credits",
|
|
199
|
+
usedPercent: null,
|
|
200
|
+
resetsAt: null,
|
|
201
|
+
valueLabel,
|
|
202
|
+
detail: null,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return windows;
|
|
206
|
+
}
|
|
207
|
+
function unixSecondsToIso(value) {
|
|
208
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
209
|
+
return null;
|
|
210
|
+
return new Date(value * 1000).toISOString();
|
|
211
|
+
}
|
|
212
|
+
function buildCodexRpcWindow(label, window) {
|
|
213
|
+
if (!window)
|
|
214
|
+
return null;
|
|
215
|
+
return {
|
|
216
|
+
label,
|
|
217
|
+
usedPercent: normalizeCodexUsedPercent(window.usedPercent),
|
|
218
|
+
resetsAt: unixSecondsToIso(window.resetsAt),
|
|
219
|
+
valueLabel: null,
|
|
220
|
+
detail: null,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function parseCreditBalance(value) {
|
|
224
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
225
|
+
return `$${value.toFixed(2)} remaining`;
|
|
226
|
+
}
|
|
227
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
228
|
+
const parsed = Number(value);
|
|
229
|
+
if (Number.isFinite(parsed)) {
|
|
230
|
+
return `$${parsed.toFixed(2)} remaining`;
|
|
231
|
+
}
|
|
232
|
+
return value.trim();
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
export function mapCodexRpcQuota(result, account) {
|
|
237
|
+
const windows = [];
|
|
238
|
+
const limitOrder = ["codex"];
|
|
239
|
+
const limitsById = result.rateLimitsByLimitId ?? {};
|
|
240
|
+
for (const key of Object.keys(limitsById)) {
|
|
241
|
+
if (!limitOrder.includes(key))
|
|
242
|
+
limitOrder.push(key);
|
|
243
|
+
}
|
|
244
|
+
const rootLimit = result.rateLimits ?? null;
|
|
245
|
+
const allLimits = new Map();
|
|
246
|
+
if (rootLimit?.limitId)
|
|
247
|
+
allLimits.set(rootLimit.limitId, rootLimit);
|
|
248
|
+
for (const [key, value] of Object.entries(limitsById)) {
|
|
249
|
+
allLimits.set(key, value);
|
|
250
|
+
}
|
|
251
|
+
if (!allLimits.has("codex") && rootLimit)
|
|
252
|
+
allLimits.set("codex", rootLimit);
|
|
253
|
+
for (const limitId of limitOrder) {
|
|
254
|
+
const limit = allLimits.get(limitId);
|
|
255
|
+
if (!limit)
|
|
256
|
+
continue;
|
|
257
|
+
const prefix = limitId === "codex"
|
|
258
|
+
? ""
|
|
259
|
+
: `${limit.limitName ?? limitId} · `;
|
|
260
|
+
const primary = buildCodexRpcWindow(`${prefix}5h limit`, limit.primary);
|
|
261
|
+
if (primary)
|
|
262
|
+
windows.push(primary);
|
|
263
|
+
const secondary = buildCodexRpcWindow(`${prefix}Weekly limit`, limit.secondary);
|
|
264
|
+
if (secondary)
|
|
265
|
+
windows.push(secondary);
|
|
266
|
+
if (limitId === "codex" && limit.credits && limit.credits.unlimited !== true) {
|
|
267
|
+
windows.push({
|
|
268
|
+
label: "Credits",
|
|
269
|
+
usedPercent: null,
|
|
270
|
+
resetsAt: null,
|
|
271
|
+
valueLabel: parseCreditBalance(limit.credits.balance) ?? "N/A",
|
|
272
|
+
detail: null,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
windows,
|
|
278
|
+
email: typeof account?.account?.email === "string" && account.account.email.trim().length > 0
|
|
279
|
+
? account.account.email.trim()
|
|
280
|
+
: null,
|
|
281
|
+
planType: typeof account?.account?.planType === "string" && account.account.planType.trim().length > 0
|
|
282
|
+
? account.account.planType.trim()
|
|
283
|
+
: (typeof rootLimit?.planType === "string" && rootLimit.planType.trim().length > 0 ? rootLimit.planType.trim() : null),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
class CodexRpcClient {
|
|
287
|
+
proc = spawn("codex", ["-s", "read-only", "-a", "untrusted", "app-server"], { stdio: ["pipe", "pipe", "pipe"], env: process.env });
|
|
288
|
+
nextId = 1;
|
|
289
|
+
buffer = "";
|
|
290
|
+
pending = new Map();
|
|
291
|
+
stderr = "";
|
|
292
|
+
constructor() {
|
|
293
|
+
this.proc.stdout.setEncoding("utf8");
|
|
294
|
+
this.proc.stderr.setEncoding("utf8");
|
|
295
|
+
this.proc.stdout.on("data", (chunk) => this.onStdout(chunk));
|
|
296
|
+
this.proc.stderr.on("data", (chunk) => {
|
|
297
|
+
this.stderr += chunk;
|
|
298
|
+
});
|
|
299
|
+
this.proc.on("exit", () => {
|
|
300
|
+
for (const request of this.pending.values()) {
|
|
301
|
+
clearTimeout(request.timer);
|
|
302
|
+
request.reject(new Error(this.stderr.trim() || "codex app-server closed unexpectedly"));
|
|
303
|
+
}
|
|
304
|
+
this.pending.clear();
|
|
305
|
+
});
|
|
306
|
+
this.proc.on("error", (err) => {
|
|
307
|
+
for (const request of this.pending.values()) {
|
|
308
|
+
clearTimeout(request.timer);
|
|
309
|
+
request.reject(err);
|
|
310
|
+
}
|
|
311
|
+
this.pending.clear();
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
onStdout(chunk) {
|
|
315
|
+
this.buffer += chunk;
|
|
316
|
+
while (true) {
|
|
317
|
+
const newlineIndex = this.buffer.indexOf("\n");
|
|
318
|
+
if (newlineIndex < 0)
|
|
319
|
+
break;
|
|
320
|
+
const line = this.buffer.slice(0, newlineIndex).trim();
|
|
321
|
+
this.buffer = this.buffer.slice(newlineIndex + 1);
|
|
322
|
+
if (!line)
|
|
323
|
+
continue;
|
|
324
|
+
let parsed;
|
|
325
|
+
try {
|
|
326
|
+
parsed = JSON.parse(line);
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const id = typeof parsed.id === "number" ? parsed.id : null;
|
|
332
|
+
if (id == null)
|
|
333
|
+
continue;
|
|
334
|
+
const pending = this.pending.get(id);
|
|
335
|
+
if (!pending)
|
|
336
|
+
continue;
|
|
337
|
+
this.pending.delete(id);
|
|
338
|
+
clearTimeout(pending.timer);
|
|
339
|
+
pending.resolve(parsed);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
request(method, params = {}, timeoutMs = 6_000) {
|
|
343
|
+
const id = this.nextId++;
|
|
344
|
+
const payload = JSON.stringify({ id, method, params }) + "\n";
|
|
345
|
+
return new Promise((resolve, reject) => {
|
|
346
|
+
const timer = setTimeout(() => {
|
|
347
|
+
this.pending.delete(id);
|
|
348
|
+
reject(new Error(`codex app-server timed out on ${method}`));
|
|
349
|
+
}, timeoutMs);
|
|
350
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
351
|
+
this.proc.stdin.write(payload);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
notify(method, params = {}) {
|
|
355
|
+
this.proc.stdin.write(JSON.stringify({ method, params }) + "\n");
|
|
356
|
+
}
|
|
357
|
+
async initialize() {
|
|
358
|
+
await this.request("initialize", {
|
|
359
|
+
clientInfo: {
|
|
360
|
+
name: "zmeel",
|
|
361
|
+
version: "0.0.0",
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
this.notify("initialized", {});
|
|
365
|
+
}
|
|
366
|
+
async fetchRateLimits() {
|
|
367
|
+
const message = await this.request("account/rateLimits/read");
|
|
368
|
+
return message.result ?? {};
|
|
369
|
+
}
|
|
370
|
+
async fetchAccount() {
|
|
371
|
+
try {
|
|
372
|
+
const message = await this.request("account/read");
|
|
373
|
+
return message.result ?? null;
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
async shutdown() {
|
|
380
|
+
this.proc.kill("SIGTERM");
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
export async function fetchCodexRpcQuota() {
|
|
384
|
+
const client = new CodexRpcClient();
|
|
385
|
+
try {
|
|
386
|
+
await client.initialize();
|
|
387
|
+
const [limits, account] = await Promise.all([
|
|
388
|
+
client.fetchRateLimits(),
|
|
389
|
+
client.fetchAccount(),
|
|
390
|
+
]);
|
|
391
|
+
return mapCodexRpcQuota(limits, account);
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
await client.shutdown();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function formatProviderError(source, error) {
|
|
398
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
399
|
+
return `${source}: ${message}`;
|
|
400
|
+
}
|
|
401
|
+
export async function getQuotaWindows() {
|
|
402
|
+
const errors = [];
|
|
403
|
+
try {
|
|
404
|
+
const rpc = await fetchCodexRpcQuota();
|
|
405
|
+
if (rpc.windows.length > 0) {
|
|
406
|
+
return { provider: "openai", source: CODEX_USAGE_SOURCE_RPC, ok: true, windows: rpc.windows };
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
catch (error) {
|
|
410
|
+
errors.push(formatProviderError("Codex app-server", error));
|
|
411
|
+
}
|
|
412
|
+
const auth = await readCodexToken();
|
|
413
|
+
if (auth) {
|
|
414
|
+
try {
|
|
415
|
+
const windows = await fetchCodexQuota(auth.token, auth.accountId);
|
|
416
|
+
return { provider: "openai", source: CODEX_USAGE_SOURCE_WHAM, ok: true, windows };
|
|
417
|
+
}
|
|
418
|
+
catch (error) {
|
|
419
|
+
errors.push(formatProviderError("ChatGPT WHAM usage", error));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
errors.push("no local codex auth token");
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
provider: "openai",
|
|
427
|
+
ok: false,
|
|
428
|
+
error: errors.join("; "),
|
|
429
|
+
windows: [],
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
//# sourceMappingURL=quota.js.map
|