codex-multi-auth 2.0.1 → 2.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/README.md +22 -8
- package/config/README.md +4 -4
- package/config/codex-legacy.json +571 -571
- package/config/codex-modern.json +258 -258
- package/config/minimal-codex.json +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +91 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/account-policy.d.ts +22 -0
- package/dist/lib/account-policy.d.ts.map +1 -0
- package/dist/lib/account-policy.js +159 -0
- package/dist/lib/account-policy.js.map +1 -0
- package/dist/lib/accounts.d.ts.map +1 -1
- package/dist/lib/accounts.js +2 -1
- package/dist/lib/accounts.js.map +1 -1
- package/dist/lib/auth/device-auth.d.ts +46 -0
- package/dist/lib/auth/device-auth.d.ts.map +1 -0
- package/dist/lib/auth/device-auth.js +325 -0
- package/dist/lib/auth/device-auth.js.map +1 -0
- package/dist/lib/auth/index.d.ts +2 -0
- package/dist/lib/auth/index.d.ts.map +1 -1
- package/dist/lib/auth/index.js +1 -0
- package/dist/lib/auth/index.js.map +1 -1
- package/dist/lib/budget-guard.d.ts +39 -0
- package/dist/lib/budget-guard.d.ts.map +1 -0
- package/dist/lib/budget-guard.js +201 -0
- package/dist/lib/budget-guard.js.map +1 -0
- package/dist/lib/codex-manager/commands/account.d.ts +13 -0
- package/dist/lib/codex-manager/commands/account.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/account.js +156 -0
- package/dist/lib/codex-manager/commands/account.js.map +1 -0
- package/dist/lib/codex-manager/commands/bridge.d.ts +6 -0
- package/dist/lib/codex-manager/commands/bridge.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/bridge.js +100 -0
- package/dist/lib/codex-manager/commands/bridge.js.map +1 -0
- package/dist/lib/codex-manager/commands/budget.d.ts +12 -0
- package/dist/lib/codex-manager/commands/budget.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/budget.js +123 -0
- package/dist/lib/codex-manager/commands/budget.js.map +1 -0
- package/dist/lib/codex-manager/commands/integrations.d.ts +6 -0
- package/dist/lib/codex-manager/commands/integrations.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/integrations.js +78 -0
- package/dist/lib/codex-manager/commands/integrations.js.map +1 -0
- package/dist/lib/codex-manager/commands/models.d.ts +14 -0
- package/dist/lib/codex-manager/commands/models.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/models.js +74 -0
- package/dist/lib/codex-manager/commands/models.js.map +1 -0
- package/dist/lib/codex-manager/commands/monitor.d.ts +22 -0
- package/dist/lib/codex-manager/commands/monitor.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/monitor.js +94 -0
- package/dist/lib/codex-manager/commands/monitor.js.map +1 -0
- package/dist/lib/codex-manager/commands/rotation.d.ts +1 -0
- package/dist/lib/codex-manager/commands/rotation.d.ts.map +1 -1
- package/dist/lib/codex-manager/commands/rotation.js +293 -10
- package/dist/lib/codex-manager/commands/rotation.js.map +1 -1
- package/dist/lib/codex-manager/commands/usage.d.ts +13 -0
- package/dist/lib/codex-manager/commands/usage.d.ts.map +1 -0
- package/dist/lib/codex-manager/commands/usage.js +347 -0
- package/dist/lib/codex-manager/commands/usage.js.map +1 -0
- package/dist/lib/codex-manager/help.d.ts +1 -0
- package/dist/lib/codex-manager/help.d.ts.map +1 -1
- package/dist/lib/codex-manager/help.js +26 -1
- package/dist/lib/codex-manager/help.js.map +1 -1
- package/dist/lib/codex-manager.d.ts.map +1 -1
- package/dist/lib/codex-manager.js +64 -7
- package/dist/lib/codex-manager.js.map +1 -1
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +2 -22
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/constants.d.ts +1 -0
- package/dist/lib/constants.d.ts.map +1 -1
- package/dist/lib/constants.js +1 -0
- package/dist/lib/constants.js.map +1 -1
- package/dist/lib/env-parsing.d.ts +21 -0
- package/dist/lib/env-parsing.d.ts.map +1 -0
- package/dist/lib/env-parsing.js +34 -0
- package/dist/lib/env-parsing.js.map +1 -0
- package/dist/lib/index.d.ts +9 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +9 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/integration-generators.d.ts +14 -0
- package/dist/lib/integration-generators.d.ts.map +1 -0
- package/dist/lib/integration-generators.js +93 -0
- package/dist/lib/integration-generators.js.map +1 -0
- package/dist/lib/local-bridge.d.ts +17 -0
- package/dist/lib/local-bridge.d.ts.map +1 -0
- package/dist/lib/local-bridge.js +249 -0
- package/dist/lib/local-bridge.js.map +1 -0
- package/dist/lib/local-client-tokens.d.ts +37 -0
- package/dist/lib/local-client-tokens.d.ts.map +1 -0
- package/dist/lib/local-client-tokens.js +200 -0
- package/dist/lib/local-client-tokens.js.map +1 -0
- package/dist/lib/model-capability-matrix.d.ts +38 -0
- package/dist/lib/model-capability-matrix.d.ts.map +1 -0
- package/dist/lib/model-capability-matrix.js +89 -0
- package/dist/lib/model-capability-matrix.js.map +1 -0
- package/dist/lib/policy/runtime-policy.d.ts +59 -0
- package/dist/lib/policy/runtime-policy.d.ts.map +1 -0
- package/dist/lib/policy/runtime-policy.js +156 -0
- package/dist/lib/policy/runtime-policy.js.map +1 -0
- package/dist/lib/proactive-refresh.js +4 -4
- package/dist/lib/proactive-refresh.js.map +1 -1
- package/dist/lib/refresh-lease.d.ts.map +1 -1
- package/dist/lib/refresh-lease.js +1 -10
- package/dist/lib/refresh-lease.js.map +1 -1
- package/dist/lib/routing-profiles.d.ts +36 -0
- package/dist/lib/routing-profiles.d.ts.map +1 -0
- package/dist/lib/routing-profiles.js +203 -0
- package/dist/lib/routing-profiles.js.map +1 -0
- package/dist/lib/runtime-rotation-proxy.d.ts.map +1 -1
- package/dist/lib/runtime-rotation-proxy.js +96 -6
- package/dist/lib/runtime-rotation-proxy.js.map +1 -1
- package/dist/lib/schemas.d.ts +3 -0
- package/dist/lib/schemas.d.ts.map +1 -1
- package/dist/lib/schemas.js +1 -0
- package/dist/lib/schemas.js.map +1 -1
- package/dist/lib/usage/index.d.ts +5 -0
- package/dist/lib/usage/index.d.ts.map +1 -0
- package/dist/lib/usage/index.js +5 -0
- package/dist/lib/usage/index.js.map +1 -0
- package/dist/lib/usage/ledger.d.ts +12 -0
- package/dist/lib/usage/ledger.d.ts.map +1 -0
- package/dist/lib/usage/ledger.js +493 -0
- package/dist/lib/usage/ledger.js.map +1 -0
- package/dist/lib/usage/pricing.d.ts +11 -0
- package/dist/lib/usage/pricing.d.ts.map +1 -0
- package/dist/lib/usage/pricing.js +67 -0
- package/dist/lib/usage/pricing.js.map +1 -0
- package/dist/lib/usage/redaction.d.ts +10 -0
- package/dist/lib/usage/redaction.d.ts.map +1 -0
- package/dist/lib/usage/redaction.js +129 -0
- package/dist/lib/usage/redaction.js.map +1 -0
- package/dist/lib/usage/types.d.ts +89 -0
- package/dist/lib/usage/types.d.ts.map +1 -0
- package/dist/lib/usage/types.js +2 -0
- package/dist/lib/usage/types.js.map +1 -0
- package/package.json +1 -1
- package/scripts/bench-format/codex-host.mjs +142 -142
- package/scripts/bench-format/models.mjs +23 -23
- package/scripts/benchmark-edit-formats.mjs +9 -9
- package/scripts/codex-app-router.js +121 -121
- package/scripts/codex.js +165 -165
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { estimateUsageCostUsd } from "./pricing.js";
|
|
3
|
+
const VALID_SOURCES = new Set([
|
|
4
|
+
"runtime-proxy",
|
|
5
|
+
"plugin-host",
|
|
6
|
+
"local-bridge",
|
|
7
|
+
"cli",
|
|
8
|
+
"unknown",
|
|
9
|
+
]);
|
|
10
|
+
const VALID_OPERATIONS = new Set([
|
|
11
|
+
"responses",
|
|
12
|
+
"models",
|
|
13
|
+
"auth-refresh",
|
|
14
|
+
"diagnostic",
|
|
15
|
+
"unknown",
|
|
16
|
+
]);
|
|
17
|
+
const VALID_OUTCOMES = new Set([
|
|
18
|
+
"success",
|
|
19
|
+
"failure",
|
|
20
|
+
"blocked",
|
|
21
|
+
"cancelled",
|
|
22
|
+
]);
|
|
23
|
+
function normalizeFiniteNumber(value) {
|
|
24
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
25
|
+
}
|
|
26
|
+
function normalizeNonNegativeInteger(value) {
|
|
27
|
+
const numeric = normalizeFiniteNumber(value);
|
|
28
|
+
return numeric === null ? 0 : Math.max(0, Math.trunc(numeric));
|
|
29
|
+
}
|
|
30
|
+
function normalizeOptionalString(value) {
|
|
31
|
+
if (typeof value !== "string")
|
|
32
|
+
return null;
|
|
33
|
+
const trimmed = value.trim();
|
|
34
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
35
|
+
}
|
|
36
|
+
function normalizeStatusCode(value) {
|
|
37
|
+
const numeric = normalizeFiniteNumber(value);
|
|
38
|
+
if (numeric === null)
|
|
39
|
+
return null;
|
|
40
|
+
const statusCode = Math.trunc(numeric);
|
|
41
|
+
return statusCode >= 100 && statusCode <= 599 ? statusCode : null;
|
|
42
|
+
}
|
|
43
|
+
function normalizeDurationMs(value) {
|
|
44
|
+
const numeric = normalizeFiniteNumber(value);
|
|
45
|
+
return numeric === null ? null : Math.max(0, Math.trunc(numeric));
|
|
46
|
+
}
|
|
47
|
+
function normalizeSource(value) {
|
|
48
|
+
return typeof value === "string" && VALID_SOURCES.has(value)
|
|
49
|
+
? value
|
|
50
|
+
: "unknown";
|
|
51
|
+
}
|
|
52
|
+
function normalizeOperation(value) {
|
|
53
|
+
return typeof value === "string" &&
|
|
54
|
+
VALID_OPERATIONS.has(value)
|
|
55
|
+
? value
|
|
56
|
+
: "unknown";
|
|
57
|
+
}
|
|
58
|
+
function normalizeOutcome(value) {
|
|
59
|
+
if (typeof value === "string" &&
|
|
60
|
+
VALID_OUTCOMES.has(value)) {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
return "failure";
|
|
64
|
+
}
|
|
65
|
+
export function hashUsageIdentifier(value) {
|
|
66
|
+
return `sha256:${createHash("sha256").update(value.trim()).digest("hex")}`;
|
|
67
|
+
}
|
|
68
|
+
export function createUsageAccountRef(input) {
|
|
69
|
+
const accountId = normalizeOptionalString(input.accountId);
|
|
70
|
+
const email = normalizeOptionalString(input.email)?.toLowerCase() ?? null;
|
|
71
|
+
const index = typeof input.accountIndex === "number" &&
|
|
72
|
+
Number.isInteger(input.accountIndex) &&
|
|
73
|
+
input.accountIndex >= 0
|
|
74
|
+
? input.accountIndex
|
|
75
|
+
: null;
|
|
76
|
+
if (!accountId && !email && index === null) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
accountHash: accountId ? hashUsageIdentifier(accountId) : undefined,
|
|
81
|
+
emailHash: email ? hashUsageIdentifier(email) : undefined,
|
|
82
|
+
index: index ?? undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function normalizeTokens(input) {
|
|
86
|
+
const inputTokens = normalizeNonNegativeInteger(input.inputTokens);
|
|
87
|
+
const outputTokens = normalizeNonNegativeInteger(input.outputTokens);
|
|
88
|
+
const cachedInputTokens = normalizeNonNegativeInteger(input.cachedInputTokens);
|
|
89
|
+
const reasoningTokens = normalizeNonNegativeInteger(input.reasoningTokens);
|
|
90
|
+
const providedTotal = normalizeFiniteNumber(input.totalTokens);
|
|
91
|
+
const computedTotal = inputTokens + outputTokens + reasoningTokens;
|
|
92
|
+
return {
|
|
93
|
+
inputTokens,
|
|
94
|
+
outputTokens,
|
|
95
|
+
cachedInputTokens,
|
|
96
|
+
reasoningTokens,
|
|
97
|
+
totalTokens: providedTotal === null
|
|
98
|
+
? computedTotal
|
|
99
|
+
: Math.max(0, Math.trunc(providedTotal)),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
export function normalizeUsageLedgerRow(input) {
|
|
103
|
+
const model = normalizeOptionalString(input.model);
|
|
104
|
+
const tokens = normalizeTokens(input);
|
|
105
|
+
const explicitCost = normalizeFiniteNumber(input.costUsd);
|
|
106
|
+
return {
|
|
107
|
+
version: 1,
|
|
108
|
+
id: normalizeOptionalString(input.id) ?? randomUUID(),
|
|
109
|
+
createdAt: normalizeFiniteNumber(input.createdAt) ?? Date.now(),
|
|
110
|
+
source: normalizeSource(input.source),
|
|
111
|
+
operation: normalizeOperation(input.operation),
|
|
112
|
+
outcome: normalizeOutcome(input.outcome),
|
|
113
|
+
model,
|
|
114
|
+
projectKey: normalizeOptionalString(input.projectKey),
|
|
115
|
+
account: createUsageAccountRef(input),
|
|
116
|
+
requestId: normalizeOptionalString(input.requestId),
|
|
117
|
+
statusCode: normalizeStatusCode(input.statusCode),
|
|
118
|
+
errorCode: normalizeOptionalString(input.errorCode),
|
|
119
|
+
durationMs: normalizeDurationMs(input.durationMs),
|
|
120
|
+
tokens,
|
|
121
|
+
costUsd: explicitCost === null
|
|
122
|
+
? estimateUsageCostUsd(model, tokens)
|
|
123
|
+
: Math.max(0, explicitCost),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
export function usageRowToJsonLine(row) {
|
|
127
|
+
return `${JSON.stringify(row)}\n`;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=redaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redaction.js","sourceRoot":"","sources":["../../../lib/usage/redaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAUrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,aAAa,GAAG,IAAI,GAAG,CAAoB;IAChD,eAAe;IACf,aAAa;IACb,cAAc;IACd,KAAK;IACL,SAAS;CACT,CAAC,CAAC;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAuB;IACtD,WAAW;IACX,QAAQ;IACR,cAAc;IACd,YAAY;IACZ,SAAS;CACT,CAAC,CAAC;AACH,MAAM,cAAc,GAAG,IAAI,GAAG,C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Operation = "responses" | "models" | "auth-refresh" | "diagnostic" | "unknown";
|
|
3
|
+
export type UsageLedgerOutcome = "success" | "failure" | "blocked" | "cancelled";
|
|
4
|
+
export interface UsageTokenCounts {
|
|
5
|
+
inputTokens: number;
|
|
6
|
+
outputTokens: number;
|
|
7
|
+
cachedInputTokens: number;
|
|
8
|
+
reasoningTokens: number;
|
|
9
|
+
totalTokens: number;
|
|
10
|
+
}
|
|
11
|
+
export interface UsageLedgerAccountRef {
|
|
12
|
+
accountHash?: string;
|
|
13
|
+
emailHash?: string;
|
|
14
|
+
index?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface UsageLedgerRow {
|
|
17
|
+
version: 1;
|
|
18
|
+
id: string;
|
|
19
|
+
createdAt: number;
|
|
20
|
+
source: UsageLedgerSource;
|
|
21
|
+
operation: UsageLedgerOperation;
|
|
22
|
+
outcome: UsageLedgerOutcome;
|
|
23
|
+
model: string | null;
|
|
24
|
+
projectKey: string | null;
|
|
25
|
+
account: UsageLedgerAccountRef | null;
|
|
26
|
+
requestId: string | null;
|
|
27
|
+
statusCode: number | null;
|
|
28
|
+
errorCode: string | null;
|
|
29
|
+
durationMs: number | null;
|
|
30
|
+
tokens: UsageTokenCounts;
|
|
31
|
+
costUsd: number | null;
|
|
32
|
+
}
|
|
33
|
+
export interface UsageLedgerAppendInput {
|
|
34
|
+
id?: string;
|
|
35
|
+
createdAt?: number;
|
|
36
|
+
source?: UsageLedgerSource;
|
|
37
|
+
operation?: UsageLedgerOperation;
|
|
38
|
+
outcome: UsageLedgerOutcome;
|
|
39
|
+
model?: string | null;
|
|
40
|
+
projectKey?: string | null;
|
|
41
|
+
accountId?: string | null;
|
|
42
|
+
email?: string | null;
|
|
43
|
+
accountIndex?: number | null;
|
|
44
|
+
requestId?: string | null;
|
|
45
|
+
statusCode?: number | null;
|
|
46
|
+
errorCode?: string | null;
|
|
47
|
+
durationMs?: number | null;
|
|
48
|
+
inputTokens?: number | null;
|
|
49
|
+
outputTokens?: number | null;
|
|
50
|
+
cachedInputTokens?: number | null;
|
|
51
|
+
reasoningTokens?: number | null;
|
|
52
|
+
totalTokens?: number | null;
|
|
53
|
+
costUsd?: number | null;
|
|
54
|
+
}
|
|
55
|
+
export type UsageSummaryGroupBy = "model" | "account" | "project" | "outcome" | "day";
|
|
56
|
+
export interface UsageLedgerQuery {
|
|
57
|
+
since?: number | Date | string;
|
|
58
|
+
until?: number | Date | string;
|
|
59
|
+
includeArchives?: boolean;
|
|
60
|
+
}
|
|
61
|
+
export interface UsageSummaryQuery extends UsageLedgerQuery {
|
|
62
|
+
by?: UsageSummaryGroupBy;
|
|
63
|
+
}
|
|
64
|
+
export interface UsageSummaryBucket {
|
|
65
|
+
key: string;
|
|
66
|
+
requests: number;
|
|
67
|
+
successes: number;
|
|
68
|
+
failures: number;
|
|
69
|
+
blocked: number;
|
|
70
|
+
cancelled: number;
|
|
71
|
+
inputTokens: number;
|
|
72
|
+
outputTokens: number;
|
|
73
|
+
cachedInputTokens: number;
|
|
74
|
+
reasoningTokens: number;
|
|
75
|
+
totalTokens: number;
|
|
76
|
+
costUsd: number;
|
|
77
|
+
}
|
|
78
|
+
export interface UsageSummary {
|
|
79
|
+
since: number | null;
|
|
80
|
+
until: number | null;
|
|
81
|
+
by: UsageSummaryGroupBy;
|
|
82
|
+
totals: UsageSummaryBucket;
|
|
83
|
+
buckets: UsageSummaryBucket[];
|
|
84
|
+
}
|
|
85
|
+
export interface UsageLedgerPaths {
|
|
86
|
+
dir: string;
|
|
87
|
+
current: string;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../lib/usage/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GAC1B,eAAe,GACf,aAAa,GACb,cAAc,GACd,KAAK,GACL,SAAS,CAAC;AAEb,MAAM,MAAM,oBAAoB,GAC7B,WAAW,GACX,QA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AAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,YAAY;IAC5B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,EAAE,EAAE,mBAAmB,CAAC;IACxB,MAAM,EAAE,kBAAkB,CAAC;IAC3B,OAAO,EAAE,kBAAkB,EAAE,CAAC;CAC9B;AAED,MAAM,WAAW,gBAAgB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CAChB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/usage/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codex-multi-auth",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Multi-account OAuth manager and codex auth wrapper for the official @openai/codex CLI, with switching, health checks, and recovery tools",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { dirname, join, resolve } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
5
|
|
|
6
6
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
7
7
|
const repoRoot = resolve(scriptDir, "..", "..");
|
|
8
8
|
|
|
9
|
-
export function getRepoRoot() {
|
|
10
|
-
return repoRoot;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function resolveWindowsNpmShim(command) {
|
|
14
|
-
if (!/\.(cmd|bat)$/i.test(command)) {
|
|
15
|
-
return { command, shell: false };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const codexScript = join(dirname(command), "node_modules", "codex-multi-auth", "scripts", "codex.js");
|
|
19
|
-
if (existsSync(codexScript)) {
|
|
20
|
-
return {
|
|
21
|
-
command: process.execPath,
|
|
22
|
-
shell: false,
|
|
23
|
-
argsPrefix: [codexScript],
|
|
24
|
-
shimCommand: command,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return { command, shell: true };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function resolveCodexExecutable() {
|
|
32
|
-
const envOverride = process.env.CODEX_BIN;
|
|
33
|
-
if (envOverride && envOverride.trim().length > 0) {
|
|
34
|
-
const command = envOverride.trim();
|
|
35
|
-
return process.platform === "win32" ? resolveWindowsNpmShim(command) : { command, shell: false };
|
|
36
|
-
}
|
|
9
|
+
export function getRepoRoot() {
|
|
10
|
+
return repoRoot;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function resolveWindowsNpmShim(command) {
|
|
14
|
+
if (!/\.(cmd|bat)$/i.test(command)) {
|
|
15
|
+
return { command, shell: false };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const codexScript = join(dirname(command), "node_modules", "codex-multi-auth", "scripts", "codex.js");
|
|
19
|
+
if (existsSync(codexScript)) {
|
|
20
|
+
return {
|
|
21
|
+
command: process.execPath,
|
|
22
|
+
shell: false,
|
|
23
|
+
argsPrefix: [codexScript],
|
|
24
|
+
shimCommand: command,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { command, shell: true };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveCodexExecutable() {
|
|
32
|
+
const envOverride = process.env.CODEX_BIN;
|
|
33
|
+
if (envOverride && envOverride.trim().length > 0) {
|
|
34
|
+
const command = envOverride.trim();
|
|
35
|
+
return process.platform === "win32" ? resolveWindowsNpmShim(command) : { command, shell: false };
|
|
36
|
+
}
|
|
37
37
|
|
|
38
38
|
if (process.platform !== "win32") {
|
|
39
39
|
return { command: "codex", shell: false };
|
|
@@ -57,15 +57,15 @@ export function resolveCodexExecutable() {
|
|
|
57
57
|
return { command: exactExe, shell: false };
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
const exactCmd = candidates.find((candidate) => /npm\\Codex\.cmd$/i.test(candidate));
|
|
61
|
-
if (exactCmd) {
|
|
62
|
-
return resolveWindowsNpmShim(exactCmd);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const anyCmd = candidates.find((candidate) => /\.cmd$/i.test(candidate));
|
|
66
|
-
if (anyCmd) {
|
|
67
|
-
return resolveWindowsNpmShim(anyCmd);
|
|
68
|
-
}
|
|
60
|
+
const exactCmd = candidates.find((candidate) => /npm\\Codex\.cmd$/i.test(candidate));
|
|
61
|
+
if (exactCmd) {
|
|
62
|
+
return resolveWindowsNpmShim(exactCmd);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const anyCmd = candidates.find((candidate) => /\.cmd$/i.test(candidate));
|
|
66
|
+
if (anyCmd) {
|
|
67
|
+
return resolveWindowsNpmShim(anyCmd);
|
|
68
|
+
}
|
|
69
69
|
|
|
70
70
|
return { command: candidates[0], shell: false };
|
|
71
71
|
}
|
|
@@ -86,35 +86,35 @@ export function parseNdjson(text) {
|
|
|
86
86
|
return events;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
export function getToolEvents(events) {
|
|
90
|
-
const legacyToolEvents = events
|
|
91
|
-
.filter((event) => event?.type === "tool_use" && event?.part?.type === "tool")
|
|
92
|
-
.map((event) => ({
|
|
93
|
-
tool: event.part.tool,
|
|
94
|
-
input: event.part.state?.input ?? {},
|
|
95
|
-
output: event.part.state?.output,
|
|
89
|
+
export function getToolEvents(events) {
|
|
90
|
+
const legacyToolEvents = events
|
|
91
|
+
.filter((event) => event?.type === "tool_use" && event?.part?.type === "tool")
|
|
92
|
+
.map((event) => ({
|
|
93
|
+
tool: event.part.tool,
|
|
94
|
+
input: event.part.state?.input ?? {},
|
|
95
|
+
output: event.part.state?.output,
|
|
96
96
|
status: event.part.state?.status,
|
|
97
97
|
start: event.part.state?.time?.start,
|
|
98
98
|
end: event.part.state?.time?.end,
|
|
99
99
|
durationMs:
|
|
100
100
|
typeof event.part.state?.time?.start === "number" &&
|
|
101
101
|
typeof event.part.state?.time?.end === "number"
|
|
102
|
-
? event.part.state.time.end - event.part.state.time.start
|
|
103
|
-
: null,
|
|
104
|
-
}));
|
|
105
|
-
const execToolEvents = events
|
|
106
|
-
.filter((event) => event?.type === "item.completed" && event?.item?.type === "command_execution")
|
|
107
|
-
.map((event) => ({
|
|
108
|
-
tool: "command_execution",
|
|
109
|
-
input: { command: event.item.command },
|
|
110
|
-
output: event.item.aggregated_output,
|
|
111
|
-
status: event.item.status,
|
|
112
|
-
start: null,
|
|
113
|
-
end: null,
|
|
114
|
-
durationMs: null,
|
|
115
|
-
}));
|
|
116
|
-
return [...legacyToolEvents, ...execToolEvents];
|
|
117
|
-
}
|
|
102
|
+
? event.part.state.time.end - event.part.state.time.start
|
|
103
|
+
: null,
|
|
104
|
+
}));
|
|
105
|
+
const execToolEvents = events
|
|
106
|
+
.filter((event) => event?.type === "item.completed" && event?.item?.type === "command_execution")
|
|
107
|
+
.map((event) => ({
|
|
108
|
+
tool: "command_execution",
|
|
109
|
+
input: { command: event.item.command },
|
|
110
|
+
output: event.item.aggregated_output,
|
|
111
|
+
status: event.item.status,
|
|
112
|
+
start: null,
|
|
113
|
+
end: null,
|
|
114
|
+
durationMs: null,
|
|
115
|
+
}));
|
|
116
|
+
return [...legacyToolEvents, ...execToolEvents];
|
|
117
|
+
}
|
|
118
118
|
|
|
119
119
|
export function getSessionDuration(events) {
|
|
120
120
|
const starts = events
|
|
@@ -129,12 +129,12 @@ export function getSessionDuration(events) {
|
|
|
129
129
|
return Math.max(...finishes) - Math.min(...starts);
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
export function getTokenTotals(events) {
|
|
133
|
-
const stepFinishes = events.filter((event) => event?.type === "step_finish" && event?.part?.tokens);
|
|
134
|
-
const turnCompletions = events.filter((event) => event?.type === "turn.completed" && event?.usage);
|
|
135
|
-
if (stepFinishes.length === 0 && turnCompletions.length === 0) {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
132
|
+
export function getTokenTotals(events) {
|
|
133
|
+
const stepFinishes = events.filter((event) => event?.type === "step_finish" && event?.part?.tokens);
|
|
134
|
+
const turnCompletions = events.filter((event) => event?.type === "turn.completed" && event?.usage);
|
|
135
|
+
if (stepFinishes.length === 0 && turnCompletions.length === 0) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
138
|
const total = {
|
|
139
139
|
total: 0,
|
|
140
140
|
input: 0,
|
|
@@ -153,49 +153,49 @@ export function getTokenTotals(events) {
|
|
|
153
153
|
total.input += input;
|
|
154
154
|
total.output += output;
|
|
155
155
|
total.reasoning += reasoning;
|
|
156
|
-
total.cacheRead += Number(tokens.cache?.read ?? 0);
|
|
157
|
-
total.cacheWrite += Number(tokens.cache?.write ?? 0);
|
|
158
|
-
}
|
|
159
|
-
for (const event of turnCompletions) {
|
|
160
|
-
const usage = event.usage ?? {};
|
|
161
|
-
const input = Number(usage.input_tokens ?? 0);
|
|
162
|
-
const output = Number(usage.output_tokens ?? 0);
|
|
163
|
-
const reasoning = Number(usage.reasoning_output_tokens ?? 0);
|
|
164
|
-
total.total += input + output;
|
|
165
|
-
total.input += input;
|
|
166
|
-
total.output += output;
|
|
167
|
-
total.reasoning += reasoning;
|
|
168
|
-
total.cacheRead += Number(usage.cached_input_tokens ?? 0);
|
|
169
|
-
}
|
|
170
|
-
return total;
|
|
171
|
-
}
|
|
156
|
+
total.cacheRead += Number(tokens.cache?.read ?? 0);
|
|
157
|
+
total.cacheWrite += Number(tokens.cache?.write ?? 0);
|
|
158
|
+
}
|
|
159
|
+
for (const event of turnCompletions) {
|
|
160
|
+
const usage = event.usage ?? {};
|
|
161
|
+
const input = Number(usage.input_tokens ?? 0);
|
|
162
|
+
const output = Number(usage.output_tokens ?? 0);
|
|
163
|
+
const reasoning = Number(usage.reasoning_output_tokens ?? 0);
|
|
164
|
+
total.total += input + output;
|
|
165
|
+
total.input += input;
|
|
166
|
+
total.output += output;
|
|
167
|
+
total.reasoning += reasoning;
|
|
168
|
+
total.cacheRead += Number(usage.cached_input_tokens ?? 0);
|
|
169
|
+
}
|
|
170
|
+
return total;
|
|
171
|
+
}
|
|
172
172
|
|
|
173
|
-
export function getTextOutput(events) {
|
|
174
|
-
const legacyText = events
|
|
175
|
-
.filter((event) => event?.type === "text" && typeof event?.part?.text === "string")
|
|
176
|
-
.map((event) => event.part.text)
|
|
177
|
-
.join("\n");
|
|
178
|
-
const execText = events
|
|
179
|
-
.filter((event) => event?.type === "item.completed" && event?.item?.type === "agent_message" && typeof event.item.text === "string")
|
|
180
|
-
.map((event) => event.item.text)
|
|
181
|
-
.join("\n");
|
|
182
|
-
return [legacyText, execText].filter(Boolean).join("\n");
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function getEventError(events) {
|
|
186
|
-
const lastCompletedIndex = events.findLastIndex((event) => event?.type === "turn.completed");
|
|
187
|
-
const failureIndex = events.findLastIndex(
|
|
188
|
-
(event, index) => index > lastCompletedIndex && (event?.type === "error" || event?.type === "turn.failed"),
|
|
189
|
-
);
|
|
190
|
-
if (failureIndex < 0) {
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
const errorEvent = events[failureIndex];
|
|
194
|
-
return {
|
|
195
|
-
name: errorEvent.error?.name ?? errorEvent.type ?? "UnknownError",
|
|
196
|
-
message: errorEvent.error?.data?.message ?? errorEvent.error?.message ?? errorEvent.message ?? "Unknown error",
|
|
197
|
-
};
|
|
198
|
-
}
|
|
173
|
+
export function getTextOutput(events) {
|
|
174
|
+
const legacyText = events
|
|
175
|
+
.filter((event) => event?.type === "text" && typeof event?.part?.text === "string")
|
|
176
|
+
.map((event) => event.part.text)
|
|
177
|
+
.join("\n");
|
|
178
|
+
const execText = events
|
|
179
|
+
.filter((event) => event?.type === "item.completed" && event?.item?.type === "agent_message" && typeof event.item.text === "string")
|
|
180
|
+
.map((event) => event.item.text)
|
|
181
|
+
.join("\n");
|
|
182
|
+
return [legacyText, execText].filter(Boolean).join("\n");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function getEventError(events) {
|
|
186
|
+
const lastCompletedIndex = events.findLastIndex((event) => event?.type === "turn.completed");
|
|
187
|
+
const failureIndex = events.findLastIndex(
|
|
188
|
+
(event, index) => index > lastCompletedIndex && (event?.type === "error" || event?.type === "turn.failed"),
|
|
189
|
+
);
|
|
190
|
+
if (failureIndex < 0) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const errorEvent = events[failureIndex];
|
|
194
|
+
return {
|
|
195
|
+
name: errorEvent.error?.name ?? errorEvent.type ?? "UnknownError",
|
|
196
|
+
message: errorEvent.error?.data?.message ?? errorEvent.error?.message ?? errorEvent.message ?? "Unknown error",
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
199
|
|
|
200
200
|
export function runCodexJson({
|
|
201
201
|
executable,
|
|
@@ -207,33 +207,33 @@ export function runCodexJson({
|
|
|
207
207
|
homeDir,
|
|
208
208
|
timeoutMs,
|
|
209
209
|
extraEnv,
|
|
210
|
-
}) {
|
|
211
|
-
const startWall = Date.now();
|
|
212
|
-
const args = [
|
|
213
|
-
"exec",
|
|
214
|
-
"--json",
|
|
215
|
-
"--model",
|
|
216
|
-
model,
|
|
217
|
-
"--skip-git-repo-check",
|
|
218
|
-
"--sandbox",
|
|
219
|
-
"danger-full-access",
|
|
220
|
-
"-c",
|
|
221
|
-
"approval_policy='never'",
|
|
222
|
-
];
|
|
223
|
-
if (variant) {
|
|
224
|
-
args.push("-c", `model_reasoning_effort='${variant}'`);
|
|
225
|
-
}
|
|
226
|
-
void agent;
|
|
227
|
-
args.push(prompt);
|
|
228
|
-
const childArgs = [...(executable.argsPrefix ?? []), ...args];
|
|
229
|
-
|
|
230
|
-
const child = spawnSync(executable.command, childArgs, {
|
|
210
|
+
}) {
|
|
211
|
+
const startWall = Date.now();
|
|
212
|
+
const args = [
|
|
213
|
+
"exec",
|
|
214
|
+
"--json",
|
|
215
|
+
"--model",
|
|
216
|
+
model,
|
|
217
|
+
"--skip-git-repo-check",
|
|
218
|
+
"--sandbox",
|
|
219
|
+
"danger-full-access",
|
|
220
|
+
"-c",
|
|
221
|
+
"approval_policy='never'",
|
|
222
|
+
];
|
|
223
|
+
if (variant) {
|
|
224
|
+
args.push("-c", `model_reasoning_effort='${variant}'`);
|
|
225
|
+
}
|
|
226
|
+
void agent;
|
|
227
|
+
args.push(prompt);
|
|
228
|
+
const childArgs = [...(executable.argsPrefix ?? []), ...args];
|
|
229
|
+
|
|
230
|
+
const child = spawnSync(executable.command, childArgs, {
|
|
231
231
|
cwd: cwd ?? repoRoot,
|
|
232
232
|
encoding: "utf8",
|
|
233
|
-
windowsHide: true,
|
|
234
|
-
shell: executable.shell,
|
|
235
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
236
|
-
timeout: timeoutMs,
|
|
233
|
+
windowsHide: true,
|
|
234
|
+
shell: executable.shell,
|
|
235
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
236
|
+
timeout: timeoutMs,
|
|
237
237
|
maxBuffer: 30 * 1024 * 1024,
|
|
238
238
|
env: {
|
|
239
239
|
...process.env,
|
|
@@ -254,11 +254,11 @@ export function runCodexJson({
|
|
|
254
254
|
if (child.error && !timedOut) {
|
|
255
255
|
throw child.error;
|
|
256
256
|
}
|
|
257
|
-
const failed = (child.status ?? 1) !== 0 || eventError !== null;
|
|
258
|
-
const modelNotFound =
|
|
259
|
-
failed &&
|
|
260
|
-
(/Model not found|ProviderModelNotFoundError|model is not supported|not supported when using Codex/i.test(`${stdout}\n${stderr}`) ||
|
|
261
|
-
/Model not found|model is not supported|not supported when using Codex/i.test(eventError?.message ?? ""));
|
|
257
|
+
const failed = (child.status ?? 1) !== 0 || eventError !== null;
|
|
258
|
+
const modelNotFound =
|
|
259
|
+
failed &&
|
|
260
|
+
(/Model not found|ProviderModelNotFoundError|model is not supported|not supported when using Codex/i.test(`${stdout}\n${stderr}`) ||
|
|
261
|
+
/Model not found|model is not supported|not supported when using Codex/i.test(eventError?.message ?? ""));
|
|
262
262
|
|
|
263
263
|
return {
|
|
264
264
|
status: child.status ?? 1,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import { resolveCodexExecutable } from "./codex-host.mjs";
|
|
3
3
|
|
|
4
|
-
const FALLBACK_OPENAI_CODEX_STABLE = [
|
|
5
|
-
"openai/gpt-5.3-codex",
|
|
6
|
-
"openai/gpt-5.4",
|
|
7
|
-
"openai/gpt-5.4-mini",
|
|
8
|
-
"openai/gpt-5.5",
|
|
9
|
-
];
|
|
4
|
+
const FALLBACK_OPENAI_CODEX_STABLE = [
|
|
5
|
+
"openai/gpt-5.3-codex",
|
|
6
|
+
"openai/gpt-5.4",
|
|
7
|
+
"openai/gpt-5.4-mini",
|
|
8
|
+
"openai/gpt-5.5",
|
|
9
|
+
];
|
|
10
10
|
|
|
11
11
|
const OPENAI_CODEX_PREFIXES = ["openai/", "openai-multi/"];
|
|
12
12
|
|
|
@@ -23,14 +23,14 @@ export function canonicalCodexModelId(modelId) {
|
|
|
23
23
|
return `openai/${codexTail(modelId)}`;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export function aliasCandidatesForCodexModel(modelId) {
|
|
27
|
-
if (!isOpenAiCodexProviderPrefix(modelId)) {
|
|
28
|
-
return [modelId];
|
|
29
|
-
}
|
|
30
|
-
const tail = codexTail(modelId);
|
|
31
|
-
const candidates = [`openai/${tail}`, `openai-multi/${tail}`];
|
|
32
|
-
return [...new Set([tail, modelId, ...candidates])];
|
|
33
|
-
}
|
|
26
|
+
export function aliasCandidatesForCodexModel(modelId) {
|
|
27
|
+
if (!isOpenAiCodexProviderPrefix(modelId)) {
|
|
28
|
+
return [modelId];
|
|
29
|
+
}
|
|
30
|
+
const tail = codexTail(modelId);
|
|
31
|
+
const candidates = [`openai/${tail}`, `openai-multi/${tail}`];
|
|
32
|
+
return [...new Set([tail, modelId, ...candidates])];
|
|
33
|
+
}
|
|
34
34
|
|
|
35
35
|
export function isStableOpenAiCodexModel(modelId) {
|
|
36
36
|
if (!isOpenAiCodexProviderPrefix(modelId)) {
|
|
@@ -45,15 +45,15 @@ export function isStableOpenAiCodexModel(modelId) {
|
|
|
45
45
|
return true;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export function listCodexModels() {
|
|
49
|
-
const executable = resolveCodexExecutable();
|
|
50
|
-
const child = spawnSync(executable.command, [...(executable.argsPrefix ?? []), "models"], {
|
|
51
|
-
encoding: "utf8",
|
|
52
|
-
windowsHide: true,
|
|
53
|
-
shell: executable.shell,
|
|
54
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
55
|
-
maxBuffer: 10 * 1024 * 1024,
|
|
56
|
-
timeout: Number.parseInt(process.env.CODEX_MODELS_TIMEOUT_MS ?? "30000", 10),
|
|
48
|
+
export function listCodexModels() {
|
|
49
|
+
const executable = resolveCodexExecutable();
|
|
50
|
+
const child = spawnSync(executable.command, [...(executable.argsPrefix ?? []), "models"], {
|
|
51
|
+
encoding: "utf8",
|
|
52
|
+
windowsHide: true,
|
|
53
|
+
shell: executable.shell,
|
|
54
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
55
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
56
|
+
timeout: Number.parseInt(process.env.CODEX_MODELS_TIMEOUT_MS ?? "30000", 10),
|
|
57
57
|
killSignal: "SIGKILL",
|
|
58
58
|
});
|
|
59
59
|
if (child.error && child.error.code === "ETIMEDOUT") {
|