@threadwell/ai 0.0.1
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 +1313 -0
- package/dist/api-registry.d.ts +20 -0
- package/dist/api-registry.d.ts.map +1 -0
- package/dist/api-registry.js +44 -0
- package/dist/api-registry.js.map +1 -0
- package/dist/bedrock-provider.d.ts +5 -0
- package/dist/bedrock-provider.d.ts.map +1 -0
- package/dist/bedrock-provider.js +6 -0
- package/dist/bedrock-provider.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/env-api-keys.d.ts +18 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +169 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +18 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +16993 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +16228 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +71 -0
- package/dist/models.js.map +1 -0
- package/dist/oauth.d.ts +2 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/providers/amazon-bedrock.d.ts +38 -0
- package/dist/providers/amazon-bedrock.d.ts.map +1 -0
- package/dist/providers/amazon-bedrock.js +759 -0
- package/dist/providers/amazon-bedrock.js.map +1 -0
- package/dist/providers/anthropic.d.ts +54 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +951 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/azure-openai-responses.d.ts +15 -0
- package/dist/providers/azure-openai-responses.d.ts.map +1 -0
- package/dist/providers/azure-openai-responses.js +208 -0
- package/dist/providers/azure-openai-responses.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +13 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +26 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/faux.d.ts +56 -0
- package/dist/providers/faux.d.ts.map +1 -0
- package/dist/providers/faux.js +368 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/github-copilot-headers.d.ts +8 -0
- package/dist/providers/github-copilot-headers.d.ts.map +1 -0
- package/dist/providers/github-copilot-headers.js +29 -0
- package/dist/providers/github-copilot-headers.js.map +1 -0
- package/dist/providers/google-shared.d.ts +70 -0
- package/dist/providers/google-shared.d.ts.map +1 -0
- package/dist/providers/google-shared.js +329 -0
- package/dist/providers/google-shared.js.map +1 -0
- package/dist/providers/google-vertex.d.ts +15 -0
- package/dist/providers/google-vertex.d.ts.map +1 -0
- package/dist/providers/google-vertex.js +442 -0
- package/dist/providers/google-vertex.js.map +1 -0
- package/dist/providers/google.d.ts +13 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +400 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/mistral.d.ts +25 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +535 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/openai-codex-responses.d.ts +30 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -0
- package/dist/providers/openai-codex-responses.js +1034 -0
- package/dist/providers/openai-codex-responses.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +19 -0
- package/dist/providers/openai-completions.d.ts.map +1 -0
- package/dist/providers/openai-completions.js +925 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses-shared.d.ts +18 -0
- package/dist/providers/openai-responses-shared.d.ts.map +1 -0
- package/dist/providers/openai-responses-shared.js +492 -0
- package/dist/providers/openai-responses-shared.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +13 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +220 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/register-builtins.d.ts +35 -0
- package/dist/providers/register-builtins.d.ts.map +1 -0
- package/dist/providers/register-builtins.js +243 -0
- package/dist/providers/register-builtins.js.map +1 -0
- package/dist/providers/simple-options.d.ts +8 -0
- package/dist/providers/simple-options.d.ts.map +1 -0
- package/dist/providers/simple-options.js +39 -0
- package/dist/providers/simple-options.js.map +1 -0
- package/dist/providers/transform-messages.d.ts +8 -0
- package/dist/providers/transform-messages.d.ts.map +1 -0
- package/dist/providers/transform-messages.js +184 -0
- package/dist/providers/transform-messages.js.map +1 -0
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/stream.d.ts +8 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +27 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +405 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/diagnostics.d.ts +19 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/event-stream.d.ts +21 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +81 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +8 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/json-parse.d.ts +16 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +113 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/oauth/anthropic.d.ts +25 -0
- package/dist/utils/oauth/anthropic.d.ts.map +1 -0
- package/dist/utils/oauth/anthropic.js +335 -0
- package/dist/utils/oauth/anthropic.js.map +1 -0
- package/dist/utils/oauth/github-copilot.d.ts +30 -0
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
- package/dist/utils/oauth/github-copilot.js +292 -0
- package/dist/utils/oauth/github-copilot.js.map +1 -0
- package/dist/utils/oauth/index.d.ts +57 -0
- package/dist/utils/oauth/index.d.ts.map +1 -0
- package/dist/utils/oauth/index.js +121 -0
- package/dist/utils/oauth/index.js.map +1 -0
- package/dist/utils/oauth/oauth-page.d.ts +3 -0
- package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
- package/dist/utils/oauth/oauth-page.js +105 -0
- package/dist/utils/oauth/oauth-page.js.map +1 -0
- package/dist/utils/oauth/openai-codex.d.ts +34 -0
- package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
- package/dist/utils/oauth/openai-codex.js +385 -0
- package/dist/utils/oauth/openai-codex.js.map +1 -0
- package/dist/utils/oauth/pkce.d.ts +13 -0
- package/dist/utils/oauth/pkce.d.ts.map +1 -0
- package/dist/utils/oauth/pkce.js +31 -0
- package/dist/utils/oauth/pkce.js.map +1 -0
- package/dist/utils/oauth/types.d.ts +57 -0
- package/dist/utils/oauth/types.d.ts.map +1 -0
- package/dist/utils/oauth/types.js +2 -0
- package/dist/utils/oauth/types.js.map +1 -0
- package/dist/utils/overflow.d.ts +55 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +146 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/validation.d.ts +18 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +281 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +108 -0
|
@@ -0,0 +1,1034 @@
|
|
|
1
|
+
// NEVER convert to top-level runtime imports - breaks browser/Vite builds
|
|
2
|
+
let _os = null;
|
|
3
|
+
const dynamicImport = (specifier) => import(specifier);
|
|
4
|
+
const NODE_OS_SPECIFIER = "node:" + "os";
|
|
5
|
+
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
|
|
6
|
+
dynamicImport(NODE_OS_SPECIFIER).then((m) => {
|
|
7
|
+
_os = m;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
import { getEnvApiKey } from "../env-api-keys.js";
|
|
11
|
+
import { clampThinkingLevel } from "../models.js";
|
|
12
|
+
import { registerSessionResourceCleanup } from "../session-resources.js";
|
|
13
|
+
import { appendAssistantMessageDiagnostic, createAssistantMessageDiagnostic, formatThrownValue, } from "../utils/diagnostics.js";
|
|
14
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
15
|
+
import { headersToRecord } from "../utils/headers.js";
|
|
16
|
+
import { convertResponsesMessages, convertResponsesTools, processResponsesStream } from "./openai-responses-shared.js";
|
|
17
|
+
import { buildBaseOptions } from "./simple-options.js";
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// Configuration
|
|
20
|
+
// ============================================================================
|
|
21
|
+
const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
|
|
22
|
+
const JWT_CLAIM_PATH = "https://api.openai.com/auth";
|
|
23
|
+
const MAX_RETRIES = 3;
|
|
24
|
+
const BASE_DELAY_MS = 1000;
|
|
25
|
+
const CODEX_TOOL_CALL_PROVIDERS = new Set(["openai", "openai-codex", "opencode"]);
|
|
26
|
+
const WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE = 1009;
|
|
27
|
+
const CODEX_RESPONSE_STATUSES = new Set([
|
|
28
|
+
"completed",
|
|
29
|
+
"incomplete",
|
|
30
|
+
"failed",
|
|
31
|
+
"cancelled",
|
|
32
|
+
"queued",
|
|
33
|
+
"in_progress",
|
|
34
|
+
]);
|
|
35
|
+
// ============================================================================
|
|
36
|
+
// Retry Helpers
|
|
37
|
+
// ============================================================================
|
|
38
|
+
function isRetryableError(status, errorText) {
|
|
39
|
+
if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return /rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(errorText);
|
|
43
|
+
}
|
|
44
|
+
function sleep(ms, signal) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
if (signal?.aborted) {
|
|
47
|
+
reject(new Error("Request was aborted"));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const timeout = setTimeout(resolve, ms);
|
|
51
|
+
signal?.addEventListener("abort", () => {
|
|
52
|
+
clearTimeout(timeout);
|
|
53
|
+
reject(new Error("Request was aborted"));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// ============================================================================
|
|
58
|
+
// Main Stream Function
|
|
59
|
+
// ============================================================================
|
|
60
|
+
export const streamOpenAICodexResponses = (model, context, options) => {
|
|
61
|
+
const stream = new AssistantMessageEventStream();
|
|
62
|
+
(async () => {
|
|
63
|
+
const output = {
|
|
64
|
+
role: "assistant",
|
|
65
|
+
content: [],
|
|
66
|
+
api: "openai-codex-responses",
|
|
67
|
+
provider: model.provider,
|
|
68
|
+
model: model.id,
|
|
69
|
+
usage: {
|
|
70
|
+
input: 0,
|
|
71
|
+
output: 0,
|
|
72
|
+
cacheRead: 0,
|
|
73
|
+
cacheWrite: 0,
|
|
74
|
+
totalTokens: 0,
|
|
75
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
76
|
+
},
|
|
77
|
+
stopReason: "stop",
|
|
78
|
+
timestamp: Date.now(),
|
|
79
|
+
};
|
|
80
|
+
try {
|
|
81
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
82
|
+
if (!apiKey) {
|
|
83
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
84
|
+
}
|
|
85
|
+
const accountId = extractAccountId(apiKey);
|
|
86
|
+
let body = buildRequestBody(model, context, options);
|
|
87
|
+
const nextBody = await options?.onPayload?.(body, model);
|
|
88
|
+
if (nextBody !== undefined) {
|
|
89
|
+
body = nextBody;
|
|
90
|
+
}
|
|
91
|
+
const websocketRequestId = options?.sessionId || createCodexRequestId();
|
|
92
|
+
const sseHeaders = buildSSEHeaders(model.headers, options?.headers, accountId, apiKey, options?.sessionId);
|
|
93
|
+
const websocketHeaders = buildWebSocketHeaders(model.headers, options?.headers, accountId, apiKey, websocketRequestId);
|
|
94
|
+
const bodyJson = JSON.stringify(body);
|
|
95
|
+
const transport = options?.transport || "auto";
|
|
96
|
+
const websocketDisabledForSession = transport !== "sse" && isWebSocketSseFallbackActive(options?.sessionId);
|
|
97
|
+
if (websocketDisabledForSession) {
|
|
98
|
+
recordWebSocketSseFallback(options?.sessionId);
|
|
99
|
+
}
|
|
100
|
+
if (transport !== "sse" && !websocketDisabledForSession) {
|
|
101
|
+
let websocketStarted = false;
|
|
102
|
+
try {
|
|
103
|
+
await processWebSocketStream(resolveCodexWebSocketUrl(model.baseUrl), body, websocketHeaders, output, stream, model, () => {
|
|
104
|
+
websocketStarted = true;
|
|
105
|
+
}, options);
|
|
106
|
+
if (options?.signal?.aborted) {
|
|
107
|
+
throw new Error("Request was aborted");
|
|
108
|
+
}
|
|
109
|
+
stream.push({
|
|
110
|
+
type: "done",
|
|
111
|
+
reason: output.stopReason,
|
|
112
|
+
message: output,
|
|
113
|
+
});
|
|
114
|
+
stream.end();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
const aborted = options?.signal?.aborted;
|
|
119
|
+
if (aborted || isCodexNonTransportError(error)) {
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
appendAssistantMessageDiagnostic(output, createAssistantMessageDiagnostic("provider_transport_failure", error, {
|
|
123
|
+
configuredTransport: transport,
|
|
124
|
+
fallbackTransport: websocketStarted ? undefined : "sse",
|
|
125
|
+
eventsEmitted: websocketStarted,
|
|
126
|
+
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
127
|
+
requestBytes: new TextEncoder().encode(bodyJson).byteLength,
|
|
128
|
+
}));
|
|
129
|
+
recordWebSocketFailure(options?.sessionId, error);
|
|
130
|
+
if (websocketStarted) {
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
recordWebSocketSseFallback(options?.sessionId);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Fetch with retry logic for rate limits and transient errors
|
|
137
|
+
let response;
|
|
138
|
+
let lastError;
|
|
139
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
140
|
+
if (options?.signal?.aborted) {
|
|
141
|
+
throw new Error("Request was aborted");
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
response = await fetch(resolveCodexUrl(model.baseUrl), {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: sseHeaders,
|
|
147
|
+
body: bodyJson,
|
|
148
|
+
signal: options?.signal,
|
|
149
|
+
});
|
|
150
|
+
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
151
|
+
if (response.ok) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
const errorText = await response.text();
|
|
155
|
+
if (attempt < MAX_RETRIES && isRetryableError(response.status, errorText)) {
|
|
156
|
+
const delayMs = BASE_DELAY_MS * 2 ** attempt;
|
|
157
|
+
await sleep(delayMs, options?.signal);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
// Parse error for friendly message on final attempt or non-retryable error
|
|
161
|
+
const fakeResponse = new Response(errorText, {
|
|
162
|
+
status: response.status,
|
|
163
|
+
statusText: response.statusText,
|
|
164
|
+
});
|
|
165
|
+
const info = await parseErrorResponse(fakeResponse);
|
|
166
|
+
throw new Error(info.friendlyMessage || info.message);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
if (error instanceof Error) {
|
|
170
|
+
if (error.name === "AbortError" || error.message === "Request was aborted") {
|
|
171
|
+
throw new Error("Request was aborted");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
175
|
+
// Network errors are retryable
|
|
176
|
+
if (attempt < MAX_RETRIES && !lastError.message.includes("usage limit")) {
|
|
177
|
+
const delayMs = BASE_DELAY_MS * 2 ** attempt;
|
|
178
|
+
await sleep(delayMs, options?.signal);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
throw lastError;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (!response?.ok) {
|
|
185
|
+
throw lastError ?? new Error("Failed after retries");
|
|
186
|
+
}
|
|
187
|
+
if (!response.body) {
|
|
188
|
+
throw new Error("No response body");
|
|
189
|
+
}
|
|
190
|
+
stream.push({ type: "start", partial: output });
|
|
191
|
+
await processStream(response, output, stream, model, options);
|
|
192
|
+
if (options?.signal?.aborted) {
|
|
193
|
+
throw new Error("Request was aborted");
|
|
194
|
+
}
|
|
195
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
196
|
+
stream.end();
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
for (const block of output.content) {
|
|
200
|
+
// partialJson is only a streaming scratch buffer; never persist it.
|
|
201
|
+
delete block.partialJson;
|
|
202
|
+
}
|
|
203
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
204
|
+
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
205
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
206
|
+
stream.end();
|
|
207
|
+
}
|
|
208
|
+
})();
|
|
209
|
+
return stream;
|
|
210
|
+
};
|
|
211
|
+
export const streamSimpleOpenAICodexResponses = (model, context, options) => {
|
|
212
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
213
|
+
if (!apiKey) {
|
|
214
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
215
|
+
}
|
|
216
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
217
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;
|
|
218
|
+
const reasoningEffort = clampedReasoning === "off" ? undefined : clampedReasoning;
|
|
219
|
+
return streamOpenAICodexResponses(model, context, {
|
|
220
|
+
...base,
|
|
221
|
+
reasoningEffort,
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
// ============================================================================
|
|
225
|
+
// Request Building
|
|
226
|
+
// ============================================================================
|
|
227
|
+
function buildRequestBody(model, context, options) {
|
|
228
|
+
const messages = convertResponsesMessages(model, context, CODEX_TOOL_CALL_PROVIDERS, {
|
|
229
|
+
includeSystemPrompt: false,
|
|
230
|
+
});
|
|
231
|
+
const body = {
|
|
232
|
+
model: model.id,
|
|
233
|
+
store: false,
|
|
234
|
+
stream: true,
|
|
235
|
+
instructions: context.systemPrompt || "You are a helpful assistant.",
|
|
236
|
+
input: messages,
|
|
237
|
+
text: { verbosity: options?.textVerbosity || "low" },
|
|
238
|
+
include: ["reasoning.encrypted_content"],
|
|
239
|
+
prompt_cache_key: options?.sessionId,
|
|
240
|
+
tool_choice: "auto",
|
|
241
|
+
parallel_tool_calls: true,
|
|
242
|
+
};
|
|
243
|
+
if (options?.temperature !== undefined) {
|
|
244
|
+
body.temperature = options.temperature;
|
|
245
|
+
}
|
|
246
|
+
if (options?.serviceTier !== undefined) {
|
|
247
|
+
body.service_tier = options.serviceTier;
|
|
248
|
+
}
|
|
249
|
+
if (context.tools && context.tools.length > 0) {
|
|
250
|
+
body.tools = convertResponsesTools(context.tools, { strict: null });
|
|
251
|
+
}
|
|
252
|
+
if (options?.reasoningEffort !== undefined) {
|
|
253
|
+
const effort = options.reasoningEffort === "none"
|
|
254
|
+
? (model.thinkingLevelMap?.off ?? "none")
|
|
255
|
+
: (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort);
|
|
256
|
+
if (effort !== null) {
|
|
257
|
+
body.reasoning = {
|
|
258
|
+
effort,
|
|
259
|
+
summary: options.reasoningSummary ?? "auto",
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return body;
|
|
264
|
+
}
|
|
265
|
+
function getServiceTierCostMultiplier(model, serviceTier) {
|
|
266
|
+
switch (serviceTier) {
|
|
267
|
+
case "flex":
|
|
268
|
+
return 0.5;
|
|
269
|
+
case "priority":
|
|
270
|
+
return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
271
|
+
default:
|
|
272
|
+
return 1;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function applyServiceTierPricing(usage, serviceTier, model) {
|
|
276
|
+
const multiplier = getServiceTierCostMultiplier(model, serviceTier);
|
|
277
|
+
if (multiplier === 1)
|
|
278
|
+
return;
|
|
279
|
+
usage.cost.input *= multiplier;
|
|
280
|
+
usage.cost.output *= multiplier;
|
|
281
|
+
usage.cost.cacheRead *= multiplier;
|
|
282
|
+
usage.cost.cacheWrite *= multiplier;
|
|
283
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
284
|
+
}
|
|
285
|
+
function resolveCodexServiceTier(responseServiceTier, requestServiceTier) {
|
|
286
|
+
if (responseServiceTier === "default" && (requestServiceTier === "flex" || requestServiceTier === "priority")) {
|
|
287
|
+
return requestServiceTier;
|
|
288
|
+
}
|
|
289
|
+
return responseServiceTier ?? requestServiceTier;
|
|
290
|
+
}
|
|
291
|
+
function resolveCodexUrl(baseUrl) {
|
|
292
|
+
const raw = baseUrl && baseUrl.trim().length > 0 ? baseUrl : DEFAULT_CODEX_BASE_URL;
|
|
293
|
+
const normalized = raw.replace(/\/+$/, "");
|
|
294
|
+
if (normalized.endsWith("/codex/responses"))
|
|
295
|
+
return normalized;
|
|
296
|
+
if (normalized.endsWith("/codex"))
|
|
297
|
+
return `${normalized}/responses`;
|
|
298
|
+
return `${normalized}/codex/responses`;
|
|
299
|
+
}
|
|
300
|
+
function resolveCodexWebSocketUrl(baseUrl) {
|
|
301
|
+
const url = new URL(resolveCodexUrl(baseUrl));
|
|
302
|
+
if (url.protocol === "https:")
|
|
303
|
+
url.protocol = "wss:";
|
|
304
|
+
if (url.protocol === "http:")
|
|
305
|
+
url.protocol = "ws:";
|
|
306
|
+
return url.toString();
|
|
307
|
+
}
|
|
308
|
+
// ============================================================================
|
|
309
|
+
// Response Processing
|
|
310
|
+
// ============================================================================
|
|
311
|
+
async function processStream(response, output, stream, model, options) {
|
|
312
|
+
await processResponsesStream(mapCodexEvents(parseSSE(response)), output, stream, model, {
|
|
313
|
+
serviceTier: options?.serviceTier,
|
|
314
|
+
resolveServiceTier: resolveCodexServiceTier,
|
|
315
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
class CodexApiError extends Error {
|
|
319
|
+
code;
|
|
320
|
+
payload;
|
|
321
|
+
constructor(message, options) {
|
|
322
|
+
super(message);
|
|
323
|
+
this.name = "CodexApiError";
|
|
324
|
+
this.code = options?.code;
|
|
325
|
+
this.payload = options?.payload;
|
|
326
|
+
this.cause = options?.cause;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
class CodexProtocolError extends Error {
|
|
330
|
+
payload;
|
|
331
|
+
constructor(message, options) {
|
|
332
|
+
super(message);
|
|
333
|
+
this.name = "CodexProtocolError";
|
|
334
|
+
this.payload = options?.payload;
|
|
335
|
+
this.cause = options?.cause;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function isCodexNonTransportError(error) {
|
|
339
|
+
return error instanceof CodexApiError || error instanceof CodexProtocolError;
|
|
340
|
+
}
|
|
341
|
+
async function* mapCodexEvents(events) {
|
|
342
|
+
for await (const event of events) {
|
|
343
|
+
const type = typeof event.type === "string" ? event.type : undefined;
|
|
344
|
+
if (!type)
|
|
345
|
+
continue;
|
|
346
|
+
if (type === "error") {
|
|
347
|
+
const code = event.code || "";
|
|
348
|
+
const message = event.message || "";
|
|
349
|
+
throw new CodexApiError(`Codex error: ${message || code || JSON.stringify(event)}`, {
|
|
350
|
+
code: code || undefined,
|
|
351
|
+
payload: event,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
if (type === "response.failed") {
|
|
355
|
+
const response = event.response;
|
|
356
|
+
const code = response?.error?.code;
|
|
357
|
+
const message = response?.error?.message;
|
|
358
|
+
throw new CodexApiError(message || "Codex response failed", { code, payload: event });
|
|
359
|
+
}
|
|
360
|
+
if (type === "response.done" || type === "response.completed" || type === "response.incomplete") {
|
|
361
|
+
const response = event.response;
|
|
362
|
+
const normalizedResponse = response
|
|
363
|
+
? { ...response, status: normalizeCodexStatus(response.status) }
|
|
364
|
+
: response;
|
|
365
|
+
yield { ...event, type: "response.completed", response: normalizedResponse };
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
yield event;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function normalizeCodexStatus(status) {
|
|
372
|
+
if (typeof status !== "string")
|
|
373
|
+
return undefined;
|
|
374
|
+
return CODEX_RESPONSE_STATUSES.has(status) ? status : undefined;
|
|
375
|
+
}
|
|
376
|
+
// ============================================================================
|
|
377
|
+
// SSE Parsing
|
|
378
|
+
// ============================================================================
|
|
379
|
+
async function* parseSSE(response) {
|
|
380
|
+
if (!response.body)
|
|
381
|
+
return;
|
|
382
|
+
const reader = response.body.getReader();
|
|
383
|
+
const decoder = new TextDecoder();
|
|
384
|
+
let buffer = "";
|
|
385
|
+
try {
|
|
386
|
+
while (true) {
|
|
387
|
+
const { done, value } = await reader.read();
|
|
388
|
+
if (done)
|
|
389
|
+
break;
|
|
390
|
+
buffer += decoder.decode(value, { stream: true });
|
|
391
|
+
let idx = buffer.indexOf("\n\n");
|
|
392
|
+
while (idx !== -1) {
|
|
393
|
+
const chunk = buffer.slice(0, idx);
|
|
394
|
+
buffer = buffer.slice(idx + 2);
|
|
395
|
+
const dataLines = chunk
|
|
396
|
+
.split("\n")
|
|
397
|
+
.filter((l) => l.startsWith("data:"))
|
|
398
|
+
.map((l) => l.slice(5).trim());
|
|
399
|
+
if (dataLines.length > 0) {
|
|
400
|
+
const data = dataLines.join("\n").trim();
|
|
401
|
+
if (data && data !== "[DONE]") {
|
|
402
|
+
try {
|
|
403
|
+
yield JSON.parse(data);
|
|
404
|
+
}
|
|
405
|
+
catch (cause) {
|
|
406
|
+
throw new CodexProtocolError(`Invalid Codex SSE JSON: ${formatThrownValue(cause)}`, {
|
|
407
|
+
cause,
|
|
408
|
+
payload: data,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
idx = buffer.indexOf("\n\n");
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
finally {
|
|
418
|
+
try {
|
|
419
|
+
await reader.cancel();
|
|
420
|
+
}
|
|
421
|
+
catch { }
|
|
422
|
+
try {
|
|
423
|
+
reader.releaseLock();
|
|
424
|
+
}
|
|
425
|
+
catch { }
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// ============================================================================
|
|
429
|
+
// WebSocket Parsing
|
|
430
|
+
// ============================================================================
|
|
431
|
+
const OPENAI_BETA_RESPONSES_WEBSOCKETS = "responses_websockets=2026-02-06";
|
|
432
|
+
const SESSION_WEBSOCKET_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
433
|
+
const websocketSessionCache = new Map();
|
|
434
|
+
const websocketDebugStats = new Map();
|
|
435
|
+
const websocketSseFallbackSessions = new Set();
|
|
436
|
+
function getOrCreateWebSocketDebugStats(sessionId) {
|
|
437
|
+
let stats = websocketDebugStats.get(sessionId);
|
|
438
|
+
if (!stats) {
|
|
439
|
+
stats = {
|
|
440
|
+
requests: 0,
|
|
441
|
+
connectionsCreated: 0,
|
|
442
|
+
connectionsReused: 0,
|
|
443
|
+
cachedContextRequests: 0,
|
|
444
|
+
storeTrueRequests: 0,
|
|
445
|
+
fullContextRequests: 0,
|
|
446
|
+
deltaRequests: 0,
|
|
447
|
+
lastInputItems: 0,
|
|
448
|
+
websocketFailures: 0,
|
|
449
|
+
sseFallbacks: 0,
|
|
450
|
+
};
|
|
451
|
+
websocketDebugStats.set(sessionId, stats);
|
|
452
|
+
}
|
|
453
|
+
return stats;
|
|
454
|
+
}
|
|
455
|
+
export function getOpenAICodexWebSocketDebugStats(sessionId) {
|
|
456
|
+
const stats = websocketDebugStats.get(sessionId);
|
|
457
|
+
return stats ? { ...stats } : undefined;
|
|
458
|
+
}
|
|
459
|
+
export function resetOpenAICodexWebSocketDebugStats(sessionId) {
|
|
460
|
+
if (sessionId) {
|
|
461
|
+
websocketDebugStats.delete(sessionId);
|
|
462
|
+
websocketSseFallbackSessions.delete(sessionId);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
websocketDebugStats.clear();
|
|
466
|
+
websocketSseFallbackSessions.clear();
|
|
467
|
+
}
|
|
468
|
+
export function closeOpenAICodexWebSocketSessions(sessionId) {
|
|
469
|
+
const closeEntry = (entry) => {
|
|
470
|
+
if (entry.idleTimer)
|
|
471
|
+
clearTimeout(entry.idleTimer);
|
|
472
|
+
closeWebSocketSilently(entry.socket, 1000, "debug_close");
|
|
473
|
+
};
|
|
474
|
+
if (sessionId) {
|
|
475
|
+
const entry = websocketSessionCache.get(sessionId);
|
|
476
|
+
if (entry)
|
|
477
|
+
closeEntry(entry);
|
|
478
|
+
websocketSessionCache.delete(sessionId);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
for (const entry of websocketSessionCache.values()) {
|
|
482
|
+
closeEntry(entry);
|
|
483
|
+
}
|
|
484
|
+
websocketSessionCache.clear();
|
|
485
|
+
}
|
|
486
|
+
registerSessionResourceCleanup(closeOpenAICodexWebSocketSessions);
|
|
487
|
+
function isWebSocketSseFallbackActive(sessionId) {
|
|
488
|
+
return sessionId ? websocketSseFallbackSessions.has(sessionId) : false;
|
|
489
|
+
}
|
|
490
|
+
function recordWebSocketSseFallback(sessionId) {
|
|
491
|
+
if (!sessionId)
|
|
492
|
+
return;
|
|
493
|
+
const stats = getOrCreateWebSocketDebugStats(sessionId);
|
|
494
|
+
stats.sseFallbacks++;
|
|
495
|
+
stats.websocketFallbackActive = isWebSocketSseFallbackActive(sessionId);
|
|
496
|
+
}
|
|
497
|
+
function recordWebSocketFailure(sessionId, error) {
|
|
498
|
+
if (!sessionId)
|
|
499
|
+
return;
|
|
500
|
+
websocketSseFallbackSessions.add(sessionId);
|
|
501
|
+
const stats = getOrCreateWebSocketDebugStats(sessionId);
|
|
502
|
+
stats.websocketFailures++;
|
|
503
|
+
stats.lastWebSocketError = formatThrownValue(error);
|
|
504
|
+
stats.websocketFallbackActive = true;
|
|
505
|
+
}
|
|
506
|
+
function getWebSocketConstructor() {
|
|
507
|
+
const ctor = globalThis.WebSocket;
|
|
508
|
+
if (typeof ctor !== "function")
|
|
509
|
+
return null;
|
|
510
|
+
return ctor;
|
|
511
|
+
}
|
|
512
|
+
class WebSocketCloseError extends Error {
|
|
513
|
+
code;
|
|
514
|
+
reason;
|
|
515
|
+
wasClean;
|
|
516
|
+
constructor(message, options) {
|
|
517
|
+
super(message);
|
|
518
|
+
this.name = "WebSocketCloseError";
|
|
519
|
+
this.code = options?.code;
|
|
520
|
+
this.reason = options?.reason;
|
|
521
|
+
this.wasClean = options?.wasClean;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function getWebSocketReadyState(socket) {
|
|
525
|
+
const readyState = socket.readyState;
|
|
526
|
+
return typeof readyState === "number" ? readyState : undefined;
|
|
527
|
+
}
|
|
528
|
+
function isWebSocketReusable(socket) {
|
|
529
|
+
const readyState = getWebSocketReadyState(socket);
|
|
530
|
+
// If readyState is unavailable, assume the runtime keeps it open/reusable.
|
|
531
|
+
return readyState === undefined || readyState === 1;
|
|
532
|
+
}
|
|
533
|
+
function closeWebSocketSilently(socket, code = 1000, reason = "done") {
|
|
534
|
+
try {
|
|
535
|
+
socket.close(code, reason);
|
|
536
|
+
}
|
|
537
|
+
catch { }
|
|
538
|
+
}
|
|
539
|
+
function scheduleSessionWebSocketExpiry(sessionId, entry) {
|
|
540
|
+
if (entry.idleTimer) {
|
|
541
|
+
clearTimeout(entry.idleTimer);
|
|
542
|
+
}
|
|
543
|
+
entry.idleTimer = setTimeout(() => {
|
|
544
|
+
if (entry.busy)
|
|
545
|
+
return;
|
|
546
|
+
closeWebSocketSilently(entry.socket, 1000, "idle_timeout");
|
|
547
|
+
websocketSessionCache.delete(sessionId);
|
|
548
|
+
}, SESSION_WEBSOCKET_CACHE_TTL_MS);
|
|
549
|
+
}
|
|
550
|
+
async function connectWebSocket(url, headers, signal) {
|
|
551
|
+
const WebSocketCtor = getWebSocketConstructor();
|
|
552
|
+
if (!WebSocketCtor) {
|
|
553
|
+
throw new Error("WebSocket transport is not available in this runtime");
|
|
554
|
+
}
|
|
555
|
+
const wsHeaders = headersToRecord(headers);
|
|
556
|
+
delete wsHeaders["OpenAI-Beta"];
|
|
557
|
+
return new Promise((resolve, reject) => {
|
|
558
|
+
let settled = false;
|
|
559
|
+
let socket;
|
|
560
|
+
try {
|
|
561
|
+
socket = new WebSocketCtor(url, { headers: wsHeaders });
|
|
562
|
+
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
const onOpen = () => {
|
|
568
|
+
if (settled)
|
|
569
|
+
return;
|
|
570
|
+
settled = true;
|
|
571
|
+
cleanup();
|
|
572
|
+
resolve(socket);
|
|
573
|
+
};
|
|
574
|
+
const onError = (event) => {
|
|
575
|
+
const error = extractWebSocketError(event);
|
|
576
|
+
if (settled)
|
|
577
|
+
return;
|
|
578
|
+
settled = true;
|
|
579
|
+
cleanup();
|
|
580
|
+
reject(error);
|
|
581
|
+
};
|
|
582
|
+
const onClose = (event) => {
|
|
583
|
+
const error = extractWebSocketCloseError(event);
|
|
584
|
+
if (settled)
|
|
585
|
+
return;
|
|
586
|
+
settled = true;
|
|
587
|
+
cleanup();
|
|
588
|
+
reject(error);
|
|
589
|
+
};
|
|
590
|
+
const onAbort = () => {
|
|
591
|
+
if (settled)
|
|
592
|
+
return;
|
|
593
|
+
settled = true;
|
|
594
|
+
cleanup();
|
|
595
|
+
socket.close(1000, "aborted");
|
|
596
|
+
reject(new Error("Request was aborted"));
|
|
597
|
+
};
|
|
598
|
+
const cleanup = () => {
|
|
599
|
+
socket.removeEventListener("open", onOpen);
|
|
600
|
+
socket.removeEventListener("error", onError);
|
|
601
|
+
socket.removeEventListener("close", onClose);
|
|
602
|
+
signal?.removeEventListener("abort", onAbort);
|
|
603
|
+
};
|
|
604
|
+
socket.addEventListener("open", onOpen);
|
|
605
|
+
socket.addEventListener("error", onError);
|
|
606
|
+
socket.addEventListener("close", onClose);
|
|
607
|
+
signal?.addEventListener("abort", onAbort);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
async function acquireWebSocket(url, headers, sessionId, signal) {
|
|
611
|
+
if (!sessionId) {
|
|
612
|
+
const socket = await connectWebSocket(url, headers, signal);
|
|
613
|
+
return {
|
|
614
|
+
socket,
|
|
615
|
+
reused: false,
|
|
616
|
+
release: ({ keep } = {}) => {
|
|
617
|
+
if (keep === false) {
|
|
618
|
+
closeWebSocketSilently(socket);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
closeWebSocketSilently(socket);
|
|
622
|
+
},
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
const cached = websocketSessionCache.get(sessionId);
|
|
626
|
+
if (cached) {
|
|
627
|
+
if (cached.idleTimer) {
|
|
628
|
+
clearTimeout(cached.idleTimer);
|
|
629
|
+
cached.idleTimer = undefined;
|
|
630
|
+
}
|
|
631
|
+
if (!cached.busy && isWebSocketReusable(cached.socket)) {
|
|
632
|
+
cached.busy = true;
|
|
633
|
+
return {
|
|
634
|
+
socket: cached.socket,
|
|
635
|
+
entry: cached,
|
|
636
|
+
reused: true,
|
|
637
|
+
release: ({ keep } = {}) => {
|
|
638
|
+
if (!keep || !isWebSocketReusable(cached.socket)) {
|
|
639
|
+
closeWebSocketSilently(cached.socket);
|
|
640
|
+
websocketSessionCache.delete(sessionId);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
cached.busy = false;
|
|
644
|
+
scheduleSessionWebSocketExpiry(sessionId, cached);
|
|
645
|
+
},
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
if (cached.busy) {
|
|
649
|
+
const socket = await connectWebSocket(url, headers, signal);
|
|
650
|
+
return {
|
|
651
|
+
socket,
|
|
652
|
+
reused: false,
|
|
653
|
+
release: () => {
|
|
654
|
+
closeWebSocketSilently(socket);
|
|
655
|
+
},
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
if (!isWebSocketReusable(cached.socket)) {
|
|
659
|
+
closeWebSocketSilently(cached.socket);
|
|
660
|
+
websocketSessionCache.delete(sessionId);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
const socket = await connectWebSocket(url, headers, signal);
|
|
664
|
+
const entry = { socket, busy: true };
|
|
665
|
+
websocketSessionCache.set(sessionId, entry);
|
|
666
|
+
return {
|
|
667
|
+
socket,
|
|
668
|
+
entry,
|
|
669
|
+
reused: false,
|
|
670
|
+
release: ({ keep } = {}) => {
|
|
671
|
+
if (!keep || !isWebSocketReusable(entry.socket)) {
|
|
672
|
+
closeWebSocketSilently(entry.socket);
|
|
673
|
+
if (entry.idleTimer)
|
|
674
|
+
clearTimeout(entry.idleTimer);
|
|
675
|
+
if (websocketSessionCache.get(sessionId) === entry) {
|
|
676
|
+
websocketSessionCache.delete(sessionId);
|
|
677
|
+
}
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
entry.busy = false;
|
|
681
|
+
scheduleSessionWebSocketExpiry(sessionId, entry);
|
|
682
|
+
},
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function extractWebSocketError(event) {
|
|
686
|
+
if (event && typeof event === "object") {
|
|
687
|
+
const message = "message" in event ? event.message : undefined;
|
|
688
|
+
if (typeof message === "string" && message.length > 0) {
|
|
689
|
+
return new Error(message);
|
|
690
|
+
}
|
|
691
|
+
const nestedError = "error" in event ? event.error : undefined;
|
|
692
|
+
if (nestedError instanceof Error && nestedError.message.length > 0) {
|
|
693
|
+
return nestedError;
|
|
694
|
+
}
|
|
695
|
+
if (nestedError && typeof nestedError === "object" && "message" in nestedError) {
|
|
696
|
+
const nestedMessage = nestedError.message;
|
|
697
|
+
if (typeof nestedMessage === "string" && nestedMessage.length > 0) {
|
|
698
|
+
return new Error(nestedMessage);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return new Error("WebSocket error");
|
|
703
|
+
}
|
|
704
|
+
function extractWebSocketCloseError(event) {
|
|
705
|
+
if (event && typeof event === "object") {
|
|
706
|
+
const code = "code" in event ? event.code : undefined;
|
|
707
|
+
const reason = "reason" in event ? event.reason : undefined;
|
|
708
|
+
const wasClean = "wasClean" in event ? event.wasClean : undefined;
|
|
709
|
+
const codeText = typeof code === "number" ? ` ${code}` : "";
|
|
710
|
+
let reasonText = typeof reason === "string" && reason.length > 0 ? ` ${reason}` : "";
|
|
711
|
+
if (!reasonText && code === WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE) {
|
|
712
|
+
reasonText = " message too big";
|
|
713
|
+
}
|
|
714
|
+
return new WebSocketCloseError(`WebSocket closed${codeText}${reasonText}`.trim(), {
|
|
715
|
+
code: typeof code === "number" ? code : undefined,
|
|
716
|
+
reason: typeof reason === "string" && reason.length > 0 ? reason : undefined,
|
|
717
|
+
wasClean: typeof wasClean === "boolean" ? wasClean : undefined,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
return new Error("WebSocket closed");
|
|
721
|
+
}
|
|
722
|
+
async function decodeWebSocketData(data) {
|
|
723
|
+
if (typeof data === "string")
|
|
724
|
+
return data;
|
|
725
|
+
if (data instanceof ArrayBuffer) {
|
|
726
|
+
return new TextDecoder().decode(new Uint8Array(data));
|
|
727
|
+
}
|
|
728
|
+
if (ArrayBuffer.isView(data)) {
|
|
729
|
+
const view = data;
|
|
730
|
+
return new TextDecoder().decode(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
|
|
731
|
+
}
|
|
732
|
+
if (data && typeof data === "object" && "arrayBuffer" in data) {
|
|
733
|
+
const blobLike = data;
|
|
734
|
+
const arrayBuffer = await blobLike.arrayBuffer();
|
|
735
|
+
return new TextDecoder().decode(new Uint8Array(arrayBuffer));
|
|
736
|
+
}
|
|
737
|
+
return null;
|
|
738
|
+
}
|
|
739
|
+
async function* parseWebSocket(socket, signal) {
|
|
740
|
+
const queue = [];
|
|
741
|
+
let pending = null;
|
|
742
|
+
let done = false;
|
|
743
|
+
let failed = null;
|
|
744
|
+
let sawCompletion = false;
|
|
745
|
+
const wake = () => {
|
|
746
|
+
if (!pending)
|
|
747
|
+
return;
|
|
748
|
+
const resolve = pending;
|
|
749
|
+
pending = null;
|
|
750
|
+
resolve();
|
|
751
|
+
};
|
|
752
|
+
const onMessage = (event) => {
|
|
753
|
+
void (async () => {
|
|
754
|
+
let text = null;
|
|
755
|
+
try {
|
|
756
|
+
if (!event || typeof event !== "object" || !("data" in event))
|
|
757
|
+
return;
|
|
758
|
+
text = await decodeWebSocketData(event.data);
|
|
759
|
+
if (!text)
|
|
760
|
+
return;
|
|
761
|
+
const parsed = JSON.parse(text);
|
|
762
|
+
const type = typeof parsed.type === "string" ? parsed.type : "";
|
|
763
|
+
if (type === "response.completed" || type === "response.done" || type === "response.incomplete") {
|
|
764
|
+
sawCompletion = true;
|
|
765
|
+
done = true;
|
|
766
|
+
}
|
|
767
|
+
queue.push(parsed);
|
|
768
|
+
wake();
|
|
769
|
+
}
|
|
770
|
+
catch (cause) {
|
|
771
|
+
failed = new CodexProtocolError(`Invalid Codex WebSocket JSON: ${formatThrownValue(cause)}`, {
|
|
772
|
+
cause,
|
|
773
|
+
payload: text,
|
|
774
|
+
});
|
|
775
|
+
done = true;
|
|
776
|
+
wake();
|
|
777
|
+
}
|
|
778
|
+
})();
|
|
779
|
+
};
|
|
780
|
+
const onError = (event) => {
|
|
781
|
+
failed = extractWebSocketError(event);
|
|
782
|
+
done = true;
|
|
783
|
+
wake();
|
|
784
|
+
};
|
|
785
|
+
const onClose = (event) => {
|
|
786
|
+
if (sawCompletion) {
|
|
787
|
+
done = true;
|
|
788
|
+
wake();
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
if (!failed) {
|
|
792
|
+
failed = extractWebSocketCloseError(event);
|
|
793
|
+
}
|
|
794
|
+
done = true;
|
|
795
|
+
wake();
|
|
796
|
+
};
|
|
797
|
+
const onAbort = () => {
|
|
798
|
+
failed = new Error("Request was aborted");
|
|
799
|
+
done = true;
|
|
800
|
+
wake();
|
|
801
|
+
};
|
|
802
|
+
socket.addEventListener("message", onMessage);
|
|
803
|
+
socket.addEventListener("error", onError);
|
|
804
|
+
socket.addEventListener("close", onClose);
|
|
805
|
+
signal?.addEventListener("abort", onAbort);
|
|
806
|
+
try {
|
|
807
|
+
while (true) {
|
|
808
|
+
if (signal?.aborted) {
|
|
809
|
+
throw new Error("Request was aborted");
|
|
810
|
+
}
|
|
811
|
+
if (queue.length > 0) {
|
|
812
|
+
yield queue.shift();
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
if (done)
|
|
816
|
+
break;
|
|
817
|
+
await new Promise((resolve) => {
|
|
818
|
+
pending = resolve;
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
if (failed) {
|
|
822
|
+
throw failed;
|
|
823
|
+
}
|
|
824
|
+
if (!sawCompletion) {
|
|
825
|
+
throw new Error("WebSocket stream closed before response.completed");
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
finally {
|
|
829
|
+
socket.removeEventListener("message", onMessage);
|
|
830
|
+
socket.removeEventListener("error", onError);
|
|
831
|
+
socket.removeEventListener("close", onClose);
|
|
832
|
+
signal?.removeEventListener("abort", onAbort);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
function requestBodyWithoutInput(body) {
|
|
836
|
+
const { input: _input, previous_response_id: _previousResponseId, ...rest } = body;
|
|
837
|
+
return rest;
|
|
838
|
+
}
|
|
839
|
+
function responseInputsEqual(a, b) {
|
|
840
|
+
return JSON.stringify(a ?? []) === JSON.stringify(b ?? []);
|
|
841
|
+
}
|
|
842
|
+
function requestBodiesMatchExceptInput(a, b) {
|
|
843
|
+
return JSON.stringify(requestBodyWithoutInput(a)) === JSON.stringify(requestBodyWithoutInput(b));
|
|
844
|
+
}
|
|
845
|
+
function getCachedWebSocketInputDelta(body, continuation) {
|
|
846
|
+
if (!requestBodiesMatchExceptInput(body, continuation.lastRequestBody)) {
|
|
847
|
+
return undefined;
|
|
848
|
+
}
|
|
849
|
+
const currentInput = body.input ?? [];
|
|
850
|
+
const baseline = [...(continuation.lastRequestBody.input ?? []), ...continuation.lastResponseItems];
|
|
851
|
+
if (currentInput.length < baseline.length) {
|
|
852
|
+
return undefined;
|
|
853
|
+
}
|
|
854
|
+
const prefix = currentInput.slice(0, baseline.length);
|
|
855
|
+
if (!responseInputsEqual(prefix, baseline)) {
|
|
856
|
+
return undefined;
|
|
857
|
+
}
|
|
858
|
+
return currentInput.slice(baseline.length);
|
|
859
|
+
}
|
|
860
|
+
function buildCachedWebSocketRequestBody(entry, body) {
|
|
861
|
+
const continuation = entry.continuation;
|
|
862
|
+
if (!continuation) {
|
|
863
|
+
return body;
|
|
864
|
+
}
|
|
865
|
+
const delta = getCachedWebSocketInputDelta(body, continuation);
|
|
866
|
+
if (!delta || !continuation.lastResponseId) {
|
|
867
|
+
entry.continuation = undefined;
|
|
868
|
+
return body;
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
...body,
|
|
872
|
+
previous_response_id: continuation.lastResponseId,
|
|
873
|
+
input: delta,
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
async function* startWebSocketOutputOnFirstEvent(events, output, stream, onStart) {
|
|
877
|
+
let started = false;
|
|
878
|
+
for await (const event of events) {
|
|
879
|
+
if (!started) {
|
|
880
|
+
started = true;
|
|
881
|
+
onStart();
|
|
882
|
+
stream.push({ type: "start", partial: output });
|
|
883
|
+
}
|
|
884
|
+
yield event;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
async function processWebSocketStream(url, body, headers, output, stream, model, onStart, options) {
|
|
888
|
+
const { socket, entry, reused, release } = await acquireWebSocket(url, headers, options?.sessionId, options?.signal);
|
|
889
|
+
let keepConnection = true;
|
|
890
|
+
const useCachedContext = options?.transport === "websocket-cached" || options?.transport === "auto";
|
|
891
|
+
// ChatGPT Codex Responses rejects `store: true` ("Store must be set to false").
|
|
892
|
+
// WebSocket continuation still works via connection-scoped previous_response_id state.
|
|
893
|
+
const fullBody = body;
|
|
894
|
+
const requestBody = useCachedContext && entry ? buildCachedWebSocketRequestBody(entry, fullBody) : fullBody;
|
|
895
|
+
const stats = options?.sessionId ? getOrCreateWebSocketDebugStats(options.sessionId) : undefined;
|
|
896
|
+
if (stats) {
|
|
897
|
+
stats.requests++;
|
|
898
|
+
if (reused)
|
|
899
|
+
stats.connectionsReused++;
|
|
900
|
+
else
|
|
901
|
+
stats.connectionsCreated++;
|
|
902
|
+
if (useCachedContext)
|
|
903
|
+
stats.cachedContextRequests++;
|
|
904
|
+
if (requestBody.store === true)
|
|
905
|
+
stats.storeTrueRequests++;
|
|
906
|
+
stats.lastInputItems = requestBody.input?.length ?? 0;
|
|
907
|
+
if (requestBody.previous_response_id) {
|
|
908
|
+
stats.deltaRequests++;
|
|
909
|
+
stats.lastDeltaInputItems = requestBody.input?.length ?? 0;
|
|
910
|
+
stats.lastPreviousResponseId = requestBody.previous_response_id;
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
stats.fullContextRequests++;
|
|
914
|
+
stats.lastDeltaInputItems = undefined;
|
|
915
|
+
stats.lastPreviousResponseId = undefined;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
try {
|
|
919
|
+
socket.send(JSON.stringify({ type: "response.create", ...requestBody }));
|
|
920
|
+
await processResponsesStream(startWebSocketOutputOnFirstEvent(mapCodexEvents(parseWebSocket(socket, options?.signal)), output, stream, onStart), output, stream, model, {
|
|
921
|
+
serviceTier: options?.serviceTier,
|
|
922
|
+
resolveServiceTier: resolveCodexServiceTier,
|
|
923
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),
|
|
924
|
+
});
|
|
925
|
+
if (options?.signal?.aborted) {
|
|
926
|
+
keepConnection = false;
|
|
927
|
+
}
|
|
928
|
+
else if (useCachedContext && entry && output.responseId) {
|
|
929
|
+
const responseItems = convertResponsesMessages(model, { messages: [output] }, CODEX_TOOL_CALL_PROVIDERS, {
|
|
930
|
+
includeSystemPrompt: false,
|
|
931
|
+
}).filter((item) => item.type !== "function_call_output");
|
|
932
|
+
entry.continuation = {
|
|
933
|
+
lastRequestBody: fullBody,
|
|
934
|
+
lastResponseId: output.responseId,
|
|
935
|
+
lastResponseItems: responseItems,
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
catch (error) {
|
|
940
|
+
if (entry) {
|
|
941
|
+
entry.continuation = undefined;
|
|
942
|
+
}
|
|
943
|
+
keepConnection = false;
|
|
944
|
+
throw error;
|
|
945
|
+
}
|
|
946
|
+
finally {
|
|
947
|
+
release({ keep: keepConnection });
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
// ============================================================================
|
|
951
|
+
// Error Handling
|
|
952
|
+
// ============================================================================
|
|
953
|
+
async function parseErrorResponse(response) {
|
|
954
|
+
const raw = await response.text();
|
|
955
|
+
let message = raw || response.statusText || "Request failed";
|
|
956
|
+
let friendlyMessage;
|
|
957
|
+
try {
|
|
958
|
+
const parsed = JSON.parse(raw);
|
|
959
|
+
const err = parsed?.error;
|
|
960
|
+
if (err) {
|
|
961
|
+
const code = err.code || err.type || "";
|
|
962
|
+
if (/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(code) || response.status === 429) {
|
|
963
|
+
const plan = err.plan_type ? ` (${err.plan_type.toLowerCase()} plan)` : "";
|
|
964
|
+
const mins = err.resets_at
|
|
965
|
+
? Math.max(0, Math.round((err.resets_at * 1000 - Date.now()) / 60000))
|
|
966
|
+
: undefined;
|
|
967
|
+
const when = mins !== undefined ? ` Try again in ~${mins} min.` : "";
|
|
968
|
+
friendlyMessage = `You have hit your ChatGPT usage limit${plan}.${when}`.trim();
|
|
969
|
+
}
|
|
970
|
+
message = err.message || friendlyMessage || message;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
catch { }
|
|
974
|
+
return { message, friendlyMessage };
|
|
975
|
+
}
|
|
976
|
+
// ============================================================================
|
|
977
|
+
// Auth & Headers
|
|
978
|
+
// ============================================================================
|
|
979
|
+
function extractAccountId(token) {
|
|
980
|
+
try {
|
|
981
|
+
const parts = token.split(".");
|
|
982
|
+
if (parts.length !== 3)
|
|
983
|
+
throw new Error("Invalid token");
|
|
984
|
+
const payload = JSON.parse(atob(parts[1]));
|
|
985
|
+
const accountId = payload?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
|
|
986
|
+
if (!accountId)
|
|
987
|
+
throw new Error("No account ID in token");
|
|
988
|
+
return accountId;
|
|
989
|
+
}
|
|
990
|
+
catch {
|
|
991
|
+
throw new Error("Failed to extract accountId from token");
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
function createCodexRequestId() {
|
|
995
|
+
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
996
|
+
return globalThis.crypto.randomUUID();
|
|
997
|
+
}
|
|
998
|
+
return `codex_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
|
999
|
+
}
|
|
1000
|
+
function buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token) {
|
|
1001
|
+
const headers = new Headers(initHeaders);
|
|
1002
|
+
for (const [key, value] of Object.entries(additionalHeaders || {})) {
|
|
1003
|
+
headers.set(key, value);
|
|
1004
|
+
}
|
|
1005
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
1006
|
+
headers.set("chatgpt-account-id", accountId);
|
|
1007
|
+
headers.set("originator", "threadwell");
|
|
1008
|
+
const userAgent = _os ? `thread (${_os.platform()} ${_os.release()}; ${_os.arch()})` : "thread (browser)";
|
|
1009
|
+
headers.set("User-Agent", userAgent);
|
|
1010
|
+
return headers;
|
|
1011
|
+
}
|
|
1012
|
+
function buildSSEHeaders(initHeaders, additionalHeaders, accountId, token, sessionId) {
|
|
1013
|
+
const headers = buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token);
|
|
1014
|
+
headers.set("OpenAI-Beta", "responses=experimental");
|
|
1015
|
+
headers.set("accept", "text/event-stream");
|
|
1016
|
+
headers.set("content-type", "application/json");
|
|
1017
|
+
if (sessionId) {
|
|
1018
|
+
headers.set("session_id", sessionId);
|
|
1019
|
+
headers.set("x-client-request-id", sessionId);
|
|
1020
|
+
}
|
|
1021
|
+
return headers;
|
|
1022
|
+
}
|
|
1023
|
+
function buildWebSocketHeaders(initHeaders, additionalHeaders, accountId, token, requestId) {
|
|
1024
|
+
const headers = buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token);
|
|
1025
|
+
headers.delete("accept");
|
|
1026
|
+
headers.delete("content-type");
|
|
1027
|
+
headers.delete("OpenAI-Beta");
|
|
1028
|
+
headers.delete("openai-beta");
|
|
1029
|
+
headers.set("OpenAI-Beta", OPENAI_BETA_RESPONSES_WEBSOCKETS);
|
|
1030
|
+
headers.set("x-client-request-id", requestId);
|
|
1031
|
+
headers.set("session_id", requestId);
|
|
1032
|
+
return headers;
|
|
1033
|
+
}
|
|
1034
|
+
//# sourceMappingURL=openai-codex-responses.js.map
|