codex-grok-bridge 1.7.1 → 1.8.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/src/bridge.mjs CHANGED
@@ -8,7 +8,18 @@ import {
8
8
  parseGrokResult,
9
9
  runGrok,
10
10
  } from "./cli-inference.mjs";
11
- import { relayProxySse } from "./proxy.mjs";
11
+ import { emitProxySse, readRelayProxySse } from "./proxy.mjs";
12
+ import { videoCallsFromParts, videoToolOutput } from "./videogen.mjs";
13
+ import { forwardImagine } from "./imagine.mjs";
14
+ import { attachSidebandUpgrade, forwardRealtime } from "./realtime.mjs";
15
+ import {
16
+ answerVoiceCall,
17
+ developerTextFromCallBody,
18
+ initialItemsFromCallBody,
19
+ instructionsFromCallBody,
20
+ ackFillerFromCallBody,
21
+ offerFromCallBody,
22
+ } from "./voice.mjs";
12
23
  import {
13
24
  applyCacheUsage,
14
25
  createPrefixMemory,
@@ -23,6 +34,167 @@ import { catalogModelInfos, isGrokModel, MODEL_INFO } from "./models.mjs";
23
34
 
24
35
  export { MODEL_INFO };
25
36
 
37
+ const VIDEO_FOLLOW_UPS = 2;
38
+
39
+ async function relayWithVideo(options, output, map, usageBox, onClientByte) {
40
+ let body = options.body;
41
+ for (let step = 0; step <= VIDEO_FOLLOW_UPS; step += 1) {
42
+ const parts = await readRelayProxySse({ ...options, body }, output);
43
+ const calls =
44
+ process.env.GROK_BRIDGE_VIDEO_GEN === "off" ? [] : videoCallsFromParts(parts);
45
+ if (!calls.length || step === VIDEO_FOLLOW_UPS) {
46
+ await emitProxySse(parts, output, map, usageBox, onClientByte);
47
+ return;
48
+ }
49
+ // The videos API poll can sit for minutes. Keepalive is safe here: this
50
+ // upstream reply is finished, and nothing has been written to Codex yet.
51
+ onClientByte();
52
+ const additions = [];
53
+ for (const call of calls) {
54
+ const toolOutput = await videoToolOutput(call, options.video);
55
+ additions.push(
56
+ {
57
+ type: "function_call",
58
+ name: call.name,
59
+ call_id: call.call_id,
60
+ arguments: call.arguments,
61
+ },
62
+ {
63
+ type: "function_call_output",
64
+ name: call.name,
65
+ call_id: call.call_id,
66
+ output: toolOutput,
67
+ },
68
+ );
69
+ }
70
+ body = { ...body, input: [...body.input, ...additions] };
71
+ }
72
+ }
73
+
74
+ async function relayImagine(req, res, json, kind, options) {
75
+ const controller = new AbortController();
76
+ res.on("close", () => controller.abort());
77
+ let body;
78
+ try {
79
+ const chunks = [];
80
+ let size = 0;
81
+ const limit = options.maxBodyBytes ?? 40 * 1024 * 1024;
82
+ for await (const chunk of req) {
83
+ size += chunk.length;
84
+ if (size > limit) return json(413, { error: "Request too large" });
85
+ chunks.push(chunk);
86
+ }
87
+ body = JSON.parse(Buffer.concat(chunks).toString("utf8"));
88
+ } catch {
89
+ return json(400, { error: "Invalid request" });
90
+ }
91
+ try {
92
+ const session = options.grokSession ?? readGrokBearerToken(options.grokHome);
93
+ const response = await forwardImagine({
94
+ kind,
95
+ body,
96
+ token: session.token,
97
+ fetchImpl: options.imagineFetch,
98
+ baseUrl: options.imagineBaseUrl,
99
+ signal: controller.signal,
100
+ });
101
+ if (!res.writableEnded && !res.destroyed) json(200, response);
102
+ } catch (error) {
103
+ if (res.writableEnded || res.destroyed) return;
104
+ if (error instanceof GrokAuthError) return json(401, { error: error.message });
105
+ const status = Number(error?.status);
106
+ const message = String(error?.message ?? "");
107
+ if (
108
+ status >= 400 &&
109
+ status < 600 &&
110
+ (message.startsWith("Image generation") ||
111
+ message.startsWith("Image edit") ||
112
+ message.startsWith("OpenAI file") ||
113
+ message.startsWith("Grok login"))
114
+ )
115
+ return json(status, { error: message });
116
+ return json(502, { error: "Image generation failed." });
117
+ }
118
+ }
119
+
120
+ function requestSearch(url) {
121
+ const query = String(url ?? "").indexOf("?");
122
+ return query === -1 ? "" : String(url).slice(query);
123
+ }
124
+
125
+ async function relayRealtime(req, res, json, upstreamPath, options) {
126
+ const controller = new AbortController();
127
+ res.on("close", () => controller.abort());
128
+ const chunks = [];
129
+ let size = 0;
130
+ const limit = options.maxBodyBytes ?? 40 * 1024 * 1024;
131
+ try {
132
+ for await (const chunk of req) {
133
+ size += chunk.length;
134
+ if (size > limit) return json(413, { error: "Request too large" });
135
+ chunks.push(chunk);
136
+ }
137
+ } catch {
138
+ if (!res.writableEnded && !res.destroyed) json(400, { error: "Invalid request" });
139
+ return;
140
+ }
141
+ try {
142
+ const session = options.grokSession ?? readGrokBearerToken(options.grokHome);
143
+ const forwarded = await forwardRealtime({
144
+ path: upstreamPath,
145
+ search: requestSearch(req.url),
146
+ body: Buffer.concat(chunks),
147
+ contentType: req.headers["content-type"],
148
+ token: session.token,
149
+ fetchImpl: options.realtimeFetch,
150
+ baseUrl: options.realtimeBaseUrl,
151
+ signal: controller.signal,
152
+ });
153
+ if (res.writableEnded || res.destroyed) return;
154
+ if (forwarded.status >= 200 && forwarded.status < 300) {
155
+ const headers = {};
156
+ if (forwarded.contentType) headers["content-type"] = forwarded.contentType;
157
+ if (forwarded.location) headers.location = forwarded.location;
158
+ res.writeHead(forwarded.status, headers);
159
+ res.end(forwarded.body);
160
+ return;
161
+ }
162
+ try {
163
+ const body = Buffer.concat(chunks);
164
+ const answered = await answerVoiceCall({
165
+ offer: offerFromCallBody(body, req.headers["content-type"]),
166
+ instructions: instructionsFromCallBody(body, req.headers["content-type"]),
167
+ ackFiller: ackFillerFromCallBody(body, req.headers["content-type"]),
168
+ developerContext: developerTextFromCallBody(body, req.headers["content-type"]),
169
+ initialItems: initialItemsFromCallBody(body, req.headers["content-type"]),
170
+ token: session.token,
171
+ webSocketFactory: options.voiceWebSocket,
172
+ });
173
+ if (res.writableEnded || res.destroyed) {
174
+ answered.close();
175
+ return;
176
+ }
177
+ res.writeHead(201, {
178
+ "content-type": "application/sdp",
179
+ location: answered.location,
180
+ });
181
+ res.end(answered.sdp);
182
+ return;
183
+ } catch {
184
+ // An offer werift cannot answer stays the upstream response.
185
+ }
186
+ const headers = {};
187
+ if (forwarded.contentType) headers["content-type"] = forwarded.contentType;
188
+ if (forwarded.location) headers.location = forwarded.location;
189
+ res.writeHead(forwarded.status, headers);
190
+ res.end(forwarded.body);
191
+ } catch (error) {
192
+ if (res.writableEnded || res.destroyed) return;
193
+ if (error instanceof GrokAuthError) return json(401, { error: error.message });
194
+ return json(502, { error: "Realtime call failed." });
195
+ }
196
+ }
197
+
26
198
  export function publicBridgeError(error, context = {}) {
27
199
  if (error instanceof GrokAuthError) return error.message;
28
200
  const message = String(error?.message ?? "");
@@ -43,7 +215,7 @@ export function createBridgeServer(options = {}) {
43
215
  queueLimit: options.maxQueuedInference,
44
216
  });
45
217
  const prefixes = createPrefixMemory();
46
- return http.createServer(async (req, res) => {
218
+ const server = http.createServer(async (req, res) => {
47
219
  const json = (status, data) => {
48
220
  res.writeHead(status, { "content-type": "application/json" });
49
221
  res.end(JSON.stringify(data));
@@ -51,13 +223,27 @@ export function createBridgeServer(options = {}) {
51
223
  const route = new URL(req.url, "http://127.0.0.1").pathname;
52
224
  if (req.method === "GET" && route === "/v1/models")
53
225
  return json(200, { models: catalogModelInfos() });
54
- if (req.method !== "POST" || route !== "/v1/responses")
226
+ const imagineKind =
227
+ route === "/v1/images/generations"
228
+ ? "generations"
229
+ : route === "/v1/images/edits"
230
+ ? "edits"
231
+ : null;
232
+ const realtimePath =
233
+ route === "/v1/realtime/calls"
234
+ ? "/realtime/calls"
235
+ : route === "/v1/live"
236
+ ? "/live"
237
+ : null;
238
+ if (req.method !== "POST" || (!imagineKind && !realtimePath && route !== "/v1/responses"))
55
239
  return json(404, { error: "Not found" });
56
240
  const auth = Buffer.from(req.headers.authorization ?? "");
57
241
  if (auth.length !== token.length || !timingSafeEqual(auth, token))
58
242
  return json(401, { error: "Unauthorized" });
59
243
  if (req.headers.origin)
60
244
  return json(403, { error: "Browser requests are not accepted" });
245
+ if (imagineKind) return relayImagine(req, res, json, imagineKind, options);
246
+ if (realtimePath) return relayRealtime(req, res, json, realtimePath, options);
61
247
  let body;
62
248
  let requestBytes = 0;
63
249
  try {
@@ -165,7 +351,7 @@ export function createBridgeServer(options = {}) {
165
351
  },
166
352
  });
167
353
  } else {
168
- const session = readGrokBearerToken(options.grokHome);
354
+ const session = options.grokSession ?? readGrokBearerToken(options.grokHome);
169
355
  // Grok takes input_image blocks natively; only unusable attachments
170
356
  // are swapped for an explanation, so one bad image cannot make the
171
357
  // upstream reject the whole conversation.
@@ -179,7 +365,7 @@ export function createBridgeServer(options = {}) {
179
365
  request.input = prefixes.reuse(convId, projected);
180
366
  const usageBox = {};
181
367
  try {
182
- await relayProxySse(
368
+ await relayWithVideo(
183
369
  {
184
370
  token: session.token,
185
371
  userId: session.userId,
@@ -189,9 +375,6 @@ export function createBridgeServer(options = {}) {
189
375
  baseUrl: options.proxyBaseUrl,
190
376
  convId,
191
377
  sessionId: Array.isArray(threadId) ? threadId[0] : threadId,
192
- onClientByte: () => {
193
- responseStarted = true;
194
- },
195
378
  onRetry: ({ attempt, kind }) =>
196
379
  diagnostics.record({
197
380
  event: "turn_retried",
@@ -199,10 +382,21 @@ export function createBridgeServer(options = {}) {
199
382
  attempt,
200
383
  elapsedMs: Date.now() - startedAt,
201
384
  }),
385
+ video: {
386
+ token: session.token,
387
+ fetchImpl: options.videoFetch,
388
+ baseUrl: options.videoBaseUrl,
389
+ pause: options.videoPause,
390
+ signal: controller.signal,
391
+ dir: options.videoDir,
392
+ },
202
393
  },
203
394
  res,
204
395
  map,
205
396
  usageBox,
397
+ () => {
398
+ responseStarted = true;
399
+ },
206
400
  );
207
401
  } finally {
208
402
  cacheUsage = usageBox.cacheUsage ?? null;
@@ -248,4 +442,6 @@ export function createBridgeServer(options = {}) {
248
442
  res.end();
249
443
  }
250
444
  });
445
+ attachSidebandUpgrade(server, options);
446
+ return server;
251
447
  }
@@ -0,0 +1,184 @@
1
+ // Codex posts OpenAI Images requests on the provider base
2
+ // (`POST /v1/images/generations`, `POST /v1/images/edits`). grok-build sends
3
+ // the same two paths to the Imagine API with the login bearer, not an
4
+ // XAI_API_KEY. The bodies are not the same: Codex hardcodes `gpt-image-2`
5
+ // plus `size` / `quality` / `background`, and edits use `{image_url}` or
6
+ // `{file_id}`. Imagine wants `grok-imagine-image-quality`, `response_format:
7
+ // b64_json`, and `{url}` references. Codex `size` maps to `aspect_ratio`.
8
+ // Codex `quality: high` maps to resolution `2k`, and `low` to `1k`.
9
+ // `auto` omits resolution so Imagine keeps its own default. `quality` itself
10
+ // is not sent: that field belongs to a different Imagine model.
11
+
12
+ export const DEFAULT_IMAGINE_API_BASE = "https://api.x.ai/v1";
13
+ export const IMAGINE_MODEL = "grok-imagine-image-quality";
14
+
15
+ const IMAGINE_TIMEOUT_MS = 300_000;
16
+
17
+ function promptOf(input) {
18
+ const prompt = typeof input?.prompt === "string" ? input.prompt.trim() : "";
19
+ if (!prompt) {
20
+ const error = new Error("Image generation requires a prompt.");
21
+ error.status = 400;
22
+ throw error;
23
+ }
24
+ return prompt;
25
+ }
26
+
27
+ function basePayload(prompt) {
28
+ return {
29
+ model: IMAGINE_MODEL,
30
+ prompt,
31
+ n: 1,
32
+ response_format: "b64_json",
33
+ };
34
+ }
35
+
36
+ const SIZE_ASPECT = {
37
+ "1024x1024": "1:1",
38
+ "1536x1024": "3:2",
39
+ "1024x1536": "2:3",
40
+ };
41
+
42
+ function aspectFromSize(size) {
43
+ if (typeof size !== "string") return "";
44
+ return SIZE_ASPECT[size.trim()] ?? "";
45
+ }
46
+
47
+ function imageCount(value) {
48
+ const n = typeof value === "string" && /^\d+$/.test(value) ? Number(value) : value;
49
+ if (!Number.isInteger(n) || n < 1) return 1;
50
+ return Math.min(n, 10);
51
+ }
52
+
53
+ function applyImageOptions(payload, input) {
54
+ const aspect = aspectFromSize(input?.size);
55
+ if (aspect) payload.aspect_ratio = aspect;
56
+ if (input?.quality === "high") payload.resolution = "2k";
57
+ else if (input?.quality === "low") payload.resolution = "1k";
58
+ else delete payload.resolution;
59
+ payload.n = imageCount(input?.n);
60
+ return payload;
61
+ }
62
+
63
+ export function imagineGenerationBody(input) {
64
+ return applyImageOptions(basePayload(promptOf(input)), input);
65
+ }
66
+
67
+ export function imagineEditBody(input) {
68
+ const prompt = promptOf(input);
69
+ const images = Array.isArray(input?.images) ? input.images : [];
70
+ const urls = [];
71
+ for (const image of images) {
72
+ if (!image || typeof image !== "object") continue;
73
+ if (typeof image.image_url === "string" && image.image_url.trim()) {
74
+ const url = image.image_url.trim();
75
+ if (!/^https?:\/\//i.test(url) && !/^data:/i.test(url)) {
76
+ const error = new Error("Image edit requires an http(s) URL or a data URL.");
77
+ error.status = 400;
78
+ throw error;
79
+ }
80
+ urls.push(url);
81
+ continue;
82
+ }
83
+ if (typeof image.file_id === "string" && image.file_id.trim()) {
84
+ const error = new Error("OpenAI file ids cannot be edited with Grok.");
85
+ error.status = 400;
86
+ throw error;
87
+ }
88
+ }
89
+ if (!urls.length) {
90
+ const error = new Error("Image edit requires a reference image.");
91
+ error.status = 400;
92
+ throw error;
93
+ }
94
+ const payload = basePayload(prompt);
95
+ if (urls.length === 1) payload.image = { url: urls[0] };
96
+ else payload.images = urls.map((url) => ({ url }));
97
+ applyImageOptions(payload, input);
98
+ if (!payload.aspect_ratio && urls.length > 1) payload.aspect_ratio = "auto";
99
+ return payload;
100
+ }
101
+
102
+ export function codexImageResponse(payload) {
103
+ const data = [];
104
+ if (Array.isArray(payload?.data)) {
105
+ for (const item of payload.data) {
106
+ if (item && typeof item.b64_json === "string" && item.b64_json.length)
107
+ data.push({ b64_json: item.b64_json });
108
+ }
109
+ }
110
+ if (!data.length) {
111
+ const error = new Error("Image generation returned no image.");
112
+ error.status = 502;
113
+ throw error;
114
+ }
115
+ const created = Number.isInteger(payload?.created)
116
+ ? payload.created
117
+ : Math.floor(Date.now() / 1000);
118
+ return { created, data };
119
+ }
120
+
121
+ function scrub(text, token) {
122
+ let out = String(text ?? "");
123
+ out = out.replace(/Bearer\s+\S+/gi, "Bearer [redacted]");
124
+ if (token) out = out.split(token).join("[redacted]");
125
+ return out;
126
+ }
127
+
128
+ async function imagineHttp(fetchImpl, url, init) {
129
+ const response = await fetchImpl(url, { ...init, redirect: "manual" });
130
+ const text = await response.text().catch(() => "");
131
+ if (response.status >= 300 && response.status < 400) {
132
+ const error = new Error("Image generation failed (redirect).");
133
+ error.status = 502;
134
+ throw error;
135
+ }
136
+ let body = null;
137
+ if (text) {
138
+ try {
139
+ body = JSON.parse(text);
140
+ } catch {
141
+ body = null;
142
+ }
143
+ }
144
+ return { status: response.status, body };
145
+ }
146
+
147
+ export async function forwardImagine(options) {
148
+ const token = options.token;
149
+ const kind = options.kind === "edits" ? "edits" : "generations";
150
+ const payload =
151
+ kind === "edits" ? imagineEditBody(options.body) : imagineGenerationBody(options.body);
152
+ const fetchImpl = options.fetchImpl ?? fetch;
153
+ const base = (options.baseUrl ?? DEFAULT_IMAGINE_API_BASE).replace(/\/$/, "");
154
+ const timeout = AbortSignal.timeout(IMAGINE_TIMEOUT_MS);
155
+ const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
156
+ let result;
157
+ try {
158
+ result = await imagineHttp(fetchImpl, `${base}/images/${kind}`, {
159
+ method: "POST",
160
+ headers: {
161
+ authorization: `Bearer ${token}`,
162
+ "content-type": "application/json",
163
+ },
164
+ body: JSON.stringify(payload),
165
+ signal,
166
+ });
167
+ } catch (error) {
168
+ if (error?.status) throw error;
169
+ const wrapped = new Error(scrub(error?.message || "Image generation failed.", token));
170
+ wrapped.status = 502;
171
+ throw wrapped;
172
+ }
173
+ if (result.status === 401 || result.status === 403) {
174
+ const error = new Error("Grok login expired. Run grok login.");
175
+ error.status = 401;
176
+ throw error;
177
+ }
178
+ if (result.status < 200 || result.status >= 300) {
179
+ const error = new Error(`Image generation failed (${result.status}).`);
180
+ error.status = result.status >= 400 && result.status < 500 ? result.status : 502;
181
+ throw error;
182
+ }
183
+ return codexImageResponse(result.body);
184
+ }
package/src/proxy.mjs CHANGED
@@ -261,13 +261,13 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
261
261
 
262
262
  // Open and read one attempt. If the socket dies before Codex has a byte, send
263
263
  // the same request again. Once a byte has been written, never resubmit.
264
- export async function relayProxySse(options, output, map, usageBox, attempts = PROXY_ATTEMPTS) {
264
+ async function relayAttempts(options, consume, attempts = PROXY_ATTEMPTS) {
265
265
  const rounds = Math.max(1, attempts);
266
266
  let lastError;
267
267
  for (let attempt = 1; attempt <= rounds; attempt += 1) {
268
268
  try {
269
269
  const response = await openProxyStream(options);
270
- await pipeProxySse(response.body, output, map, usageBox, options.onClientByte);
270
+ await consume(response);
271
271
  return response;
272
272
  } catch (error) {
273
273
  lastError = error;
@@ -284,3 +284,88 @@ export async function relayProxySse(options, output, map, usageBox, attempts = P
284
284
  }
285
285
  throw lastError;
286
286
  }
287
+
288
+ export async function relayProxySse(options, output, map, usageBox, attempts = PROXY_ATTEMPTS) {
289
+ return relayAttempts(
290
+ options,
291
+ (response) =>
292
+ pipeProxySse(response.body, output, map, usageBox, options.onClientByte),
293
+ attempts,
294
+ );
295
+ }
296
+
297
+ // Read until response.completed (or the other terminal events). Do not write
298
+ // those bytes yet: a video tool call has to be answered before Codex sees the
299
+ // turn, and a reset before that write is still safe to retry.
300
+ export async function readUntilTerminal(stream, output) {
301
+ const reader = stream.getReader();
302
+ const decoder = new TextDecoder();
303
+ let buffer = "";
304
+ const collected = [];
305
+ let released = false;
306
+ const finish = async (error) => {
307
+ if (released) return;
308
+ released = true;
309
+ await reader.cancel(error).catch(() => {});
310
+ };
311
+ try {
312
+ while (true) {
313
+ if (output?.destroyed || output?.writableEnded) throw clientClosed();
314
+ const { done, value } = await reader.read();
315
+ if (done) break;
316
+ buffer += decoder.decode(value, { stream: true });
317
+ const split = completeBlocks(buffer);
318
+ buffer = split.rest;
319
+ collected.push(...split.parts);
320
+ if (collected.some((part) => isTerminalSseBlock(part))) {
321
+ await finish();
322
+ return collected;
323
+ }
324
+ }
325
+ buffer += decoder.decode();
326
+ if (buffer.trim()) collected.push(buffer);
327
+ if (!collected.some((part) => isTerminalSseBlock(part)))
328
+ throw prematureUpstreamClose();
329
+ await finish();
330
+ return collected;
331
+ } catch (error) {
332
+ await finish(error);
333
+ if (error && typeof error === "object" && error.clientBytes == null)
334
+ error.clientBytes = 0;
335
+ throw error;
336
+ }
337
+ }
338
+
339
+ export async function readRelayProxySse(options, output, attempts = PROXY_ATTEMPTS) {
340
+ let parts;
341
+ await relayAttempts(
342
+ options,
343
+ async (response) => {
344
+ parts = await readUntilTerminal(response.body, output);
345
+ },
346
+ attempts,
347
+ );
348
+ return parts;
349
+ }
350
+
351
+ export async function emitProxySse(parts, output, map, usageBox, onClientByte) {
352
+ const rewrite = createSseRewriter(map);
353
+ let clientBytes = 0;
354
+ try {
355
+ for (const part of parts) {
356
+ const rewritten = rewrite(part);
357
+ if (rewritten === null) continue;
358
+ const block = `${rewritten}\n\n`;
359
+ if (output.destroyed || output.writableEnded) throw clientClosed();
360
+ const accepted = output.write(block);
361
+ clientBytes += Buffer.byteLength(block);
362
+ onClientByte?.();
363
+ if (!accepted) await drained(output);
364
+ }
365
+ } catch (error) {
366
+ if (error && typeof error === "object") error.clientBytes = clientBytes;
367
+ throw error;
368
+ } finally {
369
+ if (usageBox) usageBox.cacheUsage = rewrite.cacheUsage ?? null;
370
+ }
371
+ }