Repository navigation
Expand file tree
/
Copy pathcodex.ts
More file actions
283 lines (259 loc) · 9.73 KB
/
Copy pathcodex.ts
File metadata and controls
283 lines (259 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* AI provider router — public API for the entire app.
*
* This file is the ONLY entry point every agent, API route, and job
* queue uses for AI calls. It exposes the exact same public surface as
* the original Codex-only version:
*
* - runJson<T>(prompt, schema, opts?) → one-shot JSON
* - runJsonInThread<T>({ start|resume, … }) → multi-turn JSON
* - CodexError, CodexErrorKind → structured errors
* - getCodexHealth(), classifyCodexError() → health/error system
*
* Internally it reads `loadSettings().provider` and delegates to the
* active AIProvider implementation (Codex SDK, Gemini CLI, Claude Code, or BYOK).
*
* The health tracking, preflight rate-limit guard, and error classification
* are provider-agnostic — every provider's thrown errors get classified
* through the same regex battery and surfaced in the same banner.
*
* Note on naming: we keep the "Codex" names (CodexError, CodexHealth, etc.)
* in the public API so that no downstream file needs import changes.
*/
import { loadSettings } from "./settings-store";
import type { AIProvider, RunOptions as ProviderRunOptions } from "./provider-types";
import type { ProviderName } from "./provider-types";
import { PROVIDER_LABELS } from "./provider-types";
import { CodexProvider } from "./providers/codex-provider";
import { GeminiProvider } from "./providers/gemini-provider";
import { ClaudeProvider } from "./providers/claude-provider";
import { PiProvider } from "./providers/pi-provider";
import { CodexError, classifyCodexError, toCodexErrorPayload as toErrorPayloadBase } from "./codex-errors";
import type { CodexErrorKind } from "./codex-errors";
import { recordUsage, normalizeUsage } from "./usage-store";
// Re-export RunOptions from here so existing imports keep working.
// Add back the threadOverrides for Codex-specific callers.
export type RunOptions = ProviderRunOptions & {
/** Override default thread options (Codex-specific, ignored by CLI providers). */
threadOverrides?: Record<string, unknown>;
};
// Pure error model + presentation live in codex-errors.ts
export {
CodexError,
classifyCodexError,
} from "./codex-errors";
/**
* Provider-aware error payload: fills the friendly message with the ACTIVE
* engine's label (so the UI says "Claude Code"/"Gemini CLI"/… instead of a
* hard-coded "Codex"). All API routes import this from here.
*/
export function toCodexErrorPayload(err: unknown): { kind: CodexErrorKind; message: string } {
let label = "the AI engine";
try {
label = PROVIDER_LABELS[loadSettings().provider] ?? label;
} catch {
/* fall back to the generic label */
}
return toErrorPayloadBase(err, label);
}
export type { CodexErrorKind } from "./codex-errors";
// ── Provider singletons ─────────────────────────────────────────────────
const providers: Record<ProviderName, AIProvider> = {
codex: new CodexProvider(),
gemini: new GeminiProvider(),
claude: new ClaudeProvider(),
pi: new PiProvider(),
};
function activeProvider(): AIProvider {
const name = loadSettings().provider;
return providers[name] ?? providers.codex;
}
/** Return the currently-selected provider name. */
export function getActiveProviderName(): ProviderName {
return loadSettings().provider;
}
// ── Health mailbox ──────────────────────────────────────────────────────
export type CodexHealth = {
ok: boolean;
kind: CodexErrorKind | null;
message: string | null;
retryAt: number | null;
window: "5h" | "weekly" | "unknown" | null;
serial: number;
lastOkAt: number | null;
};
type HealthMap = Record<ProviderName, CodexHealth>;
declare global {
// eslint-disable-next-line no-var
var __getitHealthState: HealthMap | undefined;
}
const _initialHealth: CodexHealth = {
ok: true,
kind: null,
message: null,
retryAt: null,
window: null,
serial: 0,
lastOkAt: null,
};
const healthMap: HealthMap = globalThis.__getitHealthState ?? (globalThis.__getitHealthState = {
codex: { ..._initialHealth },
gemini: { ..._initialHealth },
claude: { ..._initialHealth },
pi: { ..._initialHealth },
});
function getHealth(providerName?: ProviderName): CodexHealth {
return healthMap[providerName ?? getActiveProviderName()];
}
export function getCodexHealth(): CodexHealth {
const health = getHealth();
if (
health.kind === "rate_limit" &&
health.retryAt != null &&
Date.now() >= health.retryAt
) {
Object.assign(health, _initialHealth, { serial: health.serial });
}
return { ...health };
}
function markOk(providerName: ProviderName) {
const health = getHealth(providerName);
if (!health.ok) {
Object.assign(health, _initialHealth, { serial: health.serial + 1 });
}
health.lastOkAt = Date.now();
health.ok = true;
}
function markError(providerName: ProviderName, err: CodexError) {
const health = getHealth(providerName);
health.ok = false;
health.kind = err.kind;
health.message = err.message;
health.retryAt = err.retryAt ?? null;
health.window = err.window ?? null;
health.serial += 1;
}
function preflightHealth(providerName: ProviderName): CodexError | null {
const health = getHealth(providerName);
if (
health.kind === "rate_limit" &&
health.retryAt != null &&
Date.now() < health.retryAt
) {
return new CodexError("rate_limit", health.message ?? "Rate limit active", {
retryAt: health.retryAt,
window: health.window ?? "unknown",
});
}
return null;
}
// ── Universal call timeout backstop ───────────────────────────────────────
// EVERY AI call, for EVERY tool and EVERY provider, is bounded by a hard
// wall-clock cap. On timeout we abort the call's signal — which the providers
// forward to their subprocess (codex SDK signal, gemini/claude runCliBinary
// AbortSignal, pi execFile signal) so the underlying CLI is actually killed —
// then surface a clear, retryable error. This guarantees a spinner can never
// hang forever (a stuck network call, a CLI's internal rate-limit backoff,
// codex/pi which have no timeout of their own, etc.). Normal calls finish in
// seconds; this is only a safety net.
const HARD_CALL_TIMEOUT_MS = 300_000; // 5 min
function linkAbort(caller?: AbortSignal) {
const controller = new AbortController();
let timedOut = false;
const onCallerAbort = () => controller.abort();
if (caller) {
if (caller.aborted) controller.abort();
else caller.addEventListener("abort", onCallerAbort, { once: true });
}
const timer = setTimeout(() => {
timedOut = true;
controller.abort();
}, HARD_CALL_TIMEOUT_MS);
return {
signal: controller.signal,
timedOut: () => timedOut,
clear: () => {
clearTimeout(timer);
if (caller) caller.removeEventListener("abort", onCallerAbort);
},
};
}
// ── Public API ──────────────────────────────────────────────────────────
/**
* Run a single turn that must return JSON conforming to the supplied schema.
* Retries once if the model returns un-parseable text. Throws CodexError on
* failure so callers can pattern-match on `.kind`.
*/
export async function runJson<T>(
prompt: string,
outputSchema: object,
opts: RunOptions = {},
): Promise<{ data: T; usage: unknown }> {
const providerName = getActiveProviderName();
const preflight = preflightHealth(providerName);
if (preflight) throw preflight;
const link = linkAbort(opts.signal);
try {
const provider = providers[providerName] ?? providers.codex;
const result = await provider.runJson<T>(prompt, outputSchema, {
...opts,
signal: link.signal,
});
markOk(providerName);
recordUsage(providerName, normalizeUsage(providerName, result.usage));
return result;
} catch (err) {
if (link.timedOut()) {
throw new CodexError("generic", "The AI request timed out — please try again.");
}
const classified = classifyCodexError(err);
if (classified.kind !== "generic") {
markError(providerName, classified);
}
throw classified;
} finally {
link.clear();
}
}
/**
* Thread-aware JSON runner for multi-turn tools (chat).
*
* Two modes, exactly one of which must be supplied:
* • start — open a NEW thread and send the full first-turn prompt
* • resume — continue an EXISTING thread by `threadId`
*
* Rate-limit / auth / binary errors are classified and thrown immediately in
* both modes so the health banner takes over.
*/
export async function runJsonInThread<T>(args: {
outputSchema: object;
opts?: RunOptions;
resume?: { threadId: string; input: string };
start?: { input: string };
}): Promise<{ data: T; usage: unknown; threadId: string | null }> {
const providerName = getActiveProviderName();
const preflight = preflightHealth(providerName);
if (preflight) throw preflight;
const link = linkAbort(args.opts?.signal);
try {
const provider = providers[providerName] ?? providers.codex;
const result = await provider.runJsonInThread<T>({
...args,
opts: { ...(args.opts ?? {}), signal: link.signal },
});
markOk(providerName);
recordUsage(providerName, normalizeUsage(providerName, result.usage));
return result;
} catch (err) {
if (link.timedOut()) {
throw new CodexError("generic", "The AI request timed out — please try again.");
}
const classified = classifyCodexError(err);
if (classified.kind !== "generic") {
markError(providerName, classified);
}
throw classified;
} finally {
link.clear();
}
}