-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathopenai-codex-stealth.mjs
More file actions
680 lines (606 loc) · 19.9 KB
/
openai-codex-stealth.mjs
File metadata and controls
680 lines (606 loc) · 19.9 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/**
* OpenAI Codex Stealth Mode
*
* Monkey-patches global fetch to intercept OpenAI API requests made with
* ChatGPT subscription OAuth tokens. Converts chat/completions requests
* to the Codex responses API format and routes them to chatgpt.com/backend-api.
*
* Handles:
* 1. /v1/chat/completions → /codex/responses (format conversion)
* 2. /v1/embeddings → local fallback (subscriptions don't support embeddings API)
* 3. /v1/models → passthrough (works with subscription tokens)
*
* Loaded before the elizaOS runtime so ALL OpenAI calls are patched.
*/
const CODEX_BASE_URL = "https://chatgpt.com/backend-api";
const JWT_CLAIM_PATH = "https://api.openai.com/auth";
// Codex API only supports specific model names — remap standard ones
const DEFAULT_CODEX_MODEL = "gpt-5.3-codex";
const MODEL_REMAP = {
// Standard API models → Codex 5.3
"gpt-4o": DEFAULT_CODEX_MODEL,
"gpt-4o-mini": DEFAULT_CODEX_MODEL,
"gpt-4.1": DEFAULT_CODEX_MODEL,
"gpt-4.1-mini": DEFAULT_CODEX_MODEL,
"gpt-4.1-nano": DEFAULT_CODEX_MODEL,
"gpt-4": DEFAULT_CODEX_MODEL,
"gpt-4-turbo": DEFAULT_CODEX_MODEL,
"gpt-3.5-turbo": DEFAULT_CODEX_MODEL,
"o3-mini": DEFAULT_CODEX_MODEL,
o3: DEFAULT_CODEX_MODEL,
o1: DEFAULT_CODEX_MODEL,
"o1-mini": DEFAULT_CODEX_MODEL,
"o1-preview": DEFAULT_CODEX_MODEL,
// elizaOS defaults (plugin-openai alpha.3)
"gpt-5": DEFAULT_CODEX_MODEL,
"gpt-5-mini": DEFAULT_CODEX_MODEL,
"gpt-5-mini-transcribe": DEFAULT_CODEX_MODEL,
"gpt-5.1": DEFAULT_CODEX_MODEL,
};
function remapModel(model) {
return MODEL_REMAP[model] || model;
}
// Detect ChatGPT subscription OAuth tokens (JWT format from auth.openai.com)
function isSubscriptionToken(val) {
if (typeof val !== "string") return false;
// Standard API keys start with sk-
if (val.startsWith("sk-")) return false;
// OAuth tokens are JWTs (3 dot-separated parts)
const parts = val.split(".");
if (parts.length !== 3) return false;
// Verify it has the chatgpt account claim
try {
const payload = JSON.parse(atob(parts[1]));
return !!payload?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
} catch {
return false;
}
}
function extractAccountId(token) {
try {
const parts = token.split(".");
const payload = JSON.parse(atob(parts[1]));
return payload[JWT_CLAIM_PATH]?.chatgpt_account_id;
} catch {
return null;
}
}
function extractToken(headers) {
if (!headers) return null;
// Check Authorization: Bearer <token>
const auth = headers.Authorization || headers.authorization;
if (auth) {
const token = auth.replace(/^Bearer\s+/i, "");
if (isSubscriptionToken(token)) return token;
}
return null;
}
// ============================================================================
// Chat Completions → Responses API conversion
// ============================================================================
function convertMessagesToInput(messages) {
const input = [];
for (const msg of messages) {
if (msg.role === "system") continue; // handled separately as instructions
const role = msg.role === "assistant" ? "assistant" : "user";
if (typeof msg.content === "string") {
input.push({
role,
content: [
{
type: role === "user" ? "input_text" : "output_text",
text: msg.content,
},
],
});
} else if (Array.isArray(msg.content)) {
const parts = msg.content.map((part) => {
if (part.type === "text") {
return {
type: role === "user" ? "input_text" : "output_text",
text: part.text,
};
}
if (part.type === "image_url") {
return { type: "input_image", image_url: part.image_url.url };
}
return part;
});
input.push({ role, content: parts });
}
// Handle tool calls from assistant
if (msg.role === "assistant" && msg.tool_calls) {
for (const tc of msg.tool_calls) {
input.push({
type: "function_call",
id: tc.id,
call_id: tc.id,
name: tc.function.name,
arguments: tc.function.arguments,
});
}
}
// Handle tool results
if (msg.role === "tool") {
input.push({
type: "function_call_output",
call_id: msg.tool_call_id,
output:
typeof msg.content === "string"
? msg.content
: JSON.stringify(msg.content),
});
}
}
return input;
}
function convertToolsToResponsesFormat(tools) {
if (!tools) return undefined;
return tools.map((tool) => ({
type: "function",
name: tool.function.name,
description: tool.function.description,
parameters: tool.function.parameters,
strict: null,
}));
}
function buildResponsesBody(chatBody) {
// Extract system prompt from messages
const systemMessages = (chatBody.messages || [])
.filter((m) => m.role === "system")
.map((m) =>
typeof m.content === "string" ? m.content : JSON.stringify(m.content),
);
const instructions = systemMessages.join("\n\n") || undefined;
const mappedModel = remapModel(chatBody.model);
// Codex API requires stream:true always — we'll convert back for non-streaming callers
const body = {
model: mappedModel,
store: false,
stream: true,
input: convertMessagesToInput(chatBody.messages || []),
text: { verbosity: "medium" },
include: ["reasoning.encrypted_content"],
tool_choice: chatBody.tool_choice || "auto",
parallel_tool_calls: true,
};
// Codex API requires instructions field, always provide one
body.instructions = instructions || "You are a helpful assistant.";
if (chatBody.tools) {
body.tools = convertToolsToResponsesFormat(chatBody.tools);
}
if (chatBody.temperature !== undefined) {
body.temperature = chatBody.temperature;
}
if (chatBody.max_tokens !== undefined) {
body.max_completion_tokens = chatBody.max_tokens;
}
return body;
}
// ============================================================================
// Responses → Chat Completions SSE stream conversion
// ============================================================================
function createChatCompletionChunk(id, model, delta, finishReason = null) {
return {
id,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
delta,
finish_reason: finishReason,
},
],
};
}
function transformResponsesStream(_responsesBody, model) {
const id = `chatcmpl-${Date.now()}`;
let sentRole = false;
let currentToolCallIndex = -1;
const toolCallIds = new Map(); // item_id -> index
const transform = new TransformStream({
transform(chunk, controller) {
const text = new TextDecoder().decode(chunk);
const lines = text.split("\n");
for (const line of lines) {
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
if (!data || data === "[DONE]") {
if (data === "[DONE]") {
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"));
}
continue;
}
let event;
try {
event = JSON.parse(data);
} catch {
continue;
}
const type = event.type;
// Send initial role
if (
type === "response.output_item.added" &&
event.item?.type === "message" &&
!sentRole
) {
sentRole = true;
const chunk = createChatCompletionChunk(id, model, {
role: "assistant",
content: "",
});
controller.enqueue(
new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`),
);
}
// Text content delta
if (
type === "response.output_text.delta" ||
type === "response.content_part.delta"
) {
const deltaText = event.delta?.text ?? event.delta ?? "";
if (typeof deltaText === "string" && deltaText) {
const chunk = createChatCompletionChunk(id, model, {
content: deltaText,
});
controller.enqueue(
new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`),
);
}
}
// Function call added
if (
type === "response.output_item.added" &&
event.item?.type === "function_call"
) {
currentToolCallIndex++;
toolCallIds.set(event.item.id, currentToolCallIndex);
const chunk = createChatCompletionChunk(id, model, {
tool_calls: [
{
index: currentToolCallIndex,
id: event.item.call_id || event.item.id,
type: "function",
function: {
name: event.item.name || "",
arguments: "",
},
},
],
});
controller.enqueue(
new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`),
);
}
// Function call arguments delta
if (type === "response.function_call_arguments.delta") {
const idx = toolCallIds.get(event.item_id) ?? currentToolCallIndex;
const chunk = createChatCompletionChunk(id, model, {
tool_calls: [
{
index: idx,
function: {
arguments: event.delta || "",
},
},
],
});
controller.enqueue(
new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`),
);
}
// Done
if (type === "response.completed" || type === "response.done") {
const finishChunk = createChatCompletionChunk(id, model, {}, "stop");
// Add usage if available
const usage = event.response?.usage;
if (usage) {
finishChunk.usage = {
prompt_tokens: usage.input_tokens || 0,
completion_tokens: usage.output_tokens || 0,
total_tokens:
(usage.input_tokens || 0) + (usage.output_tokens || 0),
};
}
controller.enqueue(
new TextEncoder().encode(
`data: ${JSON.stringify(finishChunk)}\n\n`,
),
);
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"));
}
// Error
if (type === "error" || type === "response.failed") {
const msg =
event.message || event.response?.error?.message || "Unknown error";
console.error(`[openai-stealth] Codex error: ${msg}`);
}
}
},
});
return transform;
}
// Non-streaming response conversion
function _convertResponsesResultToCompletion(responsesResult, model) {
const id = `chatcmpl-${Date.now()}`;
const output = responsesResult.output || [];
let content = "";
const toolCalls = [];
for (const item of output) {
if (item.type === "message" && item.content) {
for (const part of item.content) {
if (part.type === "output_text") {
content += part.text;
}
}
}
if (item.type === "function_call") {
toolCalls.push({
id: item.call_id || item.id,
type: "function",
function: {
name: item.name,
arguments: item.arguments,
},
});
}
}
const message = { role: "assistant", content: content || null };
if (toolCalls.length > 0) {
message.tool_calls = toolCalls;
}
const usage = responsesResult.usage;
return {
id,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
message,
finish_reason: toolCalls.length > 0 ? "tool_calls" : "stop",
},
],
usage: usage
? {
prompt_tokens: usage.input_tokens || 0,
completion_tokens: usage.output_tokens || 0,
total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0),
}
: undefined,
};
}
// ============================================================================
// Fetch Interceptor
// ============================================================================
const originalFetch = globalThis.fetch;
globalThis.fetch = async function openaiStealthFetch(input, init) {
const url = typeof input === "string" ? input : input?.url || "";
// Only intercept OpenAI API calls
if (!url.includes("api.openai.com")) {
return originalFetch(input, init);
}
if (!init) {
return originalFetch(input, init);
}
const token = extractToken(init.headers || {});
if (!token) {
// Not a subscription token, pass through
return originalFetch(input, init);
}
const accountId = extractAccountId(token);
if (!accountId) {
console.warn("[openai-stealth] Could not extract account ID from token");
return originalFetch(input, init);
}
// === Handle /v1/chat/completions ===
if (url.includes("/chat/completions")) {
let chatBody;
try {
chatBody = JSON.parse(init.body);
} catch {
return originalFetch(input, init);
}
const isStreaming = chatBody.stream === true;
const originalModel = chatBody.model;
chatBody.model = remapModel(chatBody.model);
const responsesBody = buildResponsesBody(chatBody);
console.log(
`[openai-stealth] ${originalModel}${originalModel !== chatBody.model ? ` → ${chatBody.model}` : ""} → chatgpt.com/backend-api/codex/responses (${isStreaming ? "stream" : "sync"})`,
);
const codexUrl = `${CODEX_BASE_URL}/codex/responses`;
const headers = {
Authorization: `Bearer ${token}`,
"chatgpt-account-id": accountId,
"OpenAI-Beta": "responses=experimental",
originator: "pi",
"User-Agent": "pi (linux; stealth)",
accept: "text/event-stream",
"content-type": "application/json",
};
const response = await originalFetch(codexUrl, {
method: "POST",
headers,
body: JSON.stringify(responsesBody),
signal: init.signal,
});
if (!response.ok) {
// Return error in OpenAI format
const errorText = await response.text();
console.error(
`[openai-stealth] Codex API error ${response.status}: ${errorText}`,
);
// Try to parse friendly error
let errorMessage = errorText;
try {
const parsed = JSON.parse(errorText);
const err = parsed?.error;
if (err?.code?.includes("usage_limit")) {
const plan = err.plan_type ? ` (${err.plan_type} plan)` : "";
errorMessage = `ChatGPT usage limit reached${plan}. Try again later.`;
} else {
errorMessage = err?.message || errorText;
}
} catch {}
return new Response(
JSON.stringify({
error: {
message: errorMessage,
type: "api_error",
code: response.status,
},
}),
{
status: response.status,
headers: { "content-type": "application/json" },
},
);
}
if (isStreaming && response.body) {
// Transform the responses stream → chat completions stream
const transform = transformResponsesStream(responsesBody, chatBody.model);
const transformedStream = response.body.pipeThrough(transform);
return new Response(transformedStream, {
status: 200,
headers: {
"content-type": "text/event-stream",
"cache-control": "no-cache",
connection: "keep-alive",
},
});
} else {
// Non-streaming caller but Codex always streams — collect SSE and build JSON response
const fullText = await response.text();
const events = fullText
.split("\n")
.filter((l) => l.startsWith("data:"))
.map((l) => l.slice(5).trim())
.filter((d) => d && d !== "[DONE]");
let content = "";
const toolCalls = [];
let usage = null;
for (const eventStr of events) {
try {
const event = JSON.parse(eventStr);
const type = event.type;
// Collect text deltas
if (
type === "response.output_text.delta" ||
type === "response.content_part.delta"
) {
const deltaText = event.delta?.text ?? event.delta ?? "";
if (typeof deltaText === "string") content += deltaText;
}
// Collect function calls from completed response
if (type === "response.completed" || type === "response.done") {
const resp = event.response;
if (resp?.usage) {
usage = {
prompt_tokens: resp.usage.input_tokens || 0,
completion_tokens: resp.usage.output_tokens || 0,
total_tokens:
(resp.usage.input_tokens || 0) +
(resp.usage.output_tokens || 0),
};
}
// Extract any function calls from output
for (const item of resp?.output || []) {
if (item.type === "function_call") {
toolCalls.push({
id: item.call_id || item.id,
type: "function",
function: { name: item.name, arguments: item.arguments },
});
}
}
}
} catch {}
}
const message = { role: "assistant", content: content || null };
if (toolCalls.length > 0) message.tool_calls = toolCalls;
const completion = {
id: `chatcmpl-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: chatBody.model,
choices: [
{
index: 0,
message,
finish_reason: toolCalls.length > 0 ? "tool_calls" : "stop",
},
],
usage,
};
return new Response(JSON.stringify(completion), {
status: 200,
headers: { "content-type": "application/json" },
});
}
}
// === Handle /v1/embeddings ===
if (url.includes("/embeddings")) {
// ChatGPT subscriptions don't support embeddings API
// Return a zero vector so the runtime doesn't crash
let body;
try {
body = JSON.parse(init.body);
} catch {
body = {};
}
const inputs = Array.isArray(body.input) ? body.input : [body.input || ""];
const dimensions = body.dimensions || 1536;
console.log(
`[openai-stealth] Embeddings not supported with subscription token, returning zero vectors (${inputs.length} inputs)`,
);
const data = inputs.map((_, i) => ({
object: "embedding",
index: i,
embedding: new Array(dimensions).fill(0),
}));
return new Response(
JSON.stringify({
object: "list",
data,
model: body.model || "text-embedding-3-small",
usage: { prompt_tokens: 0, total_tokens: 0 },
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
);
}
// === Handle /v1/models ===
if (url.includes("/models")) {
// Models endpoint doesn't work with subscription tokens either
// Return a minimal list so the runtime can check model availability
console.log(
"[openai-stealth] Models list → returning defaults for subscription",
);
return new Response(
JSON.stringify({
object: "list",
data: [
{ id: "gpt-4o", object: "model", owned_by: "openai" },
{ id: "gpt-4o-mini", object: "model", owned_by: "openai" },
{ id: "gpt-4.1", object: "model", owned_by: "openai" },
{ id: "gpt-4.1-mini", object: "model", owned_by: "openai" },
{ id: "o3-mini", object: "model", owned_by: "openai" },
],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
);
}
// Everything else: pass through
return originalFetch(input, init);
};
console.log(
"[openai-stealth] OpenAI Codex stealth mode active — ChatGPT subscription requests will be routed to chatgpt.com/backend-api",
);