codex-grok-bridge 1.8.4 → 1.8.6

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/proxy.mjs CHANGED
@@ -3,6 +3,7 @@ import { homedir } from "node:os";
3
3
  import { resolveGrokBinary } from "./paths.mjs";
4
4
  import { createSseRewriter } from "./tools.mjs";
5
5
  import { requestStream } from "./transport.mjs";
6
+ import { redact } from "./diagnostics.mjs";
6
7
  import { BRIDGE_ERROR, classifyBridgeError } from "./errors.mjs";
7
8
 
8
9
  export const DEFAULT_PROXY_BASE = "https://cli-chat-proxy.grok.com/v1";
@@ -32,10 +33,10 @@ export function detectGrokClientVersion(home = homedir()) {
32
33
  return cachedClientVersion;
33
34
  }
34
35
 
35
- function redactedError(status, text) {
36
- const trimmed = String(text || "")
37
- .replace(/Bearer\s+\S+/gi, "Bearer [redacted]")
38
- .slice(0, 200);
36
+ function redactedError(status, text, token) {
37
+ let trimmed = String(text || "");
38
+ if (token) trimmed = trimmed.split(token).join("[redacted]");
39
+ trimmed = redact(trimmed).slice(0, 200);
39
40
  if (status === 401 || status === 403)
40
41
  return "Grok login expired. Run grok login.";
41
42
  return `Grok Responses proxy failed (${status})${trimmed ? `: ${trimmed}` : ""}`;
@@ -73,7 +74,9 @@ export async function openProxyStream(options) {
73
74
  });
74
75
  if (!response.ok) {
75
76
  const text = await response.text().catch(() => "");
76
- throw new Error(redactedError(response.status, text));
77
+ const error = new Error(redactedError(response.status, text, options.token));
78
+ error.status = response.status;
79
+ throw error;
77
80
  }
78
81
  if (!response.body) throw new Error("Grok Responses proxy returned no body");
79
82
  return response;
@@ -129,6 +132,40 @@ const TERMINAL_EVENTS = new Set([
129
132
  "response.failed",
130
133
  ]);
131
134
 
135
+ export const MAX_SSE_BYTES = 40 * 1024 * 1024;
136
+
137
+ function sseTooLarge() {
138
+ return Object.assign(new Error("Grok response exceeded the bridge buffer"), {
139
+ code: "ERR_UPSTREAM_TOO_LARGE",
140
+ });
141
+ }
142
+
143
+ function noteSseBytes(total, extra) {
144
+ const next = total + extra;
145
+ if (next > MAX_SSE_BYTES) throw sseTooLarge();
146
+ return next;
147
+ }
148
+
149
+ export function isFailedSseBlock(part) {
150
+ for (const line of String(part).split("\n")) {
151
+ if (line.startsWith("event:") && line.slice(6).trim() === "response.failed")
152
+ return true;
153
+ if (!line.startsWith("data:")) continue;
154
+ const payload = line.slice(5).trim();
155
+ if (!payload || payload === "[DONE]") continue;
156
+ try {
157
+ if (JSON.parse(payload)?.type === "response.failed") return true;
158
+ } catch {
159
+ // A non-JSON data line is not a failure event.
160
+ }
161
+ }
162
+ return false;
163
+ }
164
+
165
+ function noteUpstreamFailure(usageBox, part) {
166
+ if (usageBox && isFailedSseBlock(part)) usageBox.upstreamFailed = true;
167
+ }
168
+
132
169
  export function isTerminalSseBlock(part) {
133
170
  for (const line of part.split("\n")) {
134
171
  if (line.startsWith("event:") && TERMINAL_EVENTS.has(line.slice(6).trim()))
@@ -182,10 +219,18 @@ export async function openProxyStreamWithRetry(options, attempts = PROXY_ATTEMPT
182
219
  throw lastError;
183
220
  }
184
221
 
185
- function completeBlocks(buffer) {
186
- const parts = buffer.split("\n\n");
187
- const rest = parts.pop() ?? "";
188
- return { parts: parts.filter((part) => part.trim()), rest };
222
+ export function completeBlocks(buffer) {
223
+ const parts = [];
224
+ let rest = String(buffer ?? "");
225
+ const boundary = /\r?\n\r?\n/;
226
+ while (rest.length) {
227
+ const match = boundary.exec(rest);
228
+ if (!match) break;
229
+ const part = rest.slice(0, match.index).replaceAll("\r\n", "\n").replaceAll("\r", "\n");
230
+ rest = rest.slice(match.index + match[0].length);
231
+ if (part.trim()) parts.push(part);
232
+ }
233
+ return { parts, rest };
189
234
  }
190
235
 
191
236
  // Read the upstream body to memory. Write to Codex only after a terminal
@@ -194,6 +239,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
194
239
  const reader = stream.getReader();
195
240
  const decoder = new TextDecoder();
196
241
  let buffer = "";
242
+ let bufferedBytes = 0;
197
243
  const rewrite = createSseRewriter(map);
198
244
  const pending = [];
199
245
  let clientBytes = 0;
@@ -205,6 +251,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
205
251
  const rewritten = rewrite(part);
206
252
  if (rewritten === null) continue;
207
253
  const block = `${rewritten}\n\n`;
254
+ noteUpstreamFailure(usageBox, rewritten);
208
255
  if (output.destroyed || output.writableEnded) throw clientClosed();
209
256
  // Count the byte as soon as the client stream accepts it. A later drain
210
257
  // error must not look like "Codex saw nothing" and resubmit the prompt.
@@ -238,6 +285,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
238
285
  if (output.destroyed || output.writableEnded) throw clientClosed();
239
286
  const { done, value } = await reader.read();
240
287
  if (done) break;
288
+ bufferedBytes = noteSseBytes(bufferedBytes, value?.byteLength ?? value?.length ?? 0);
241
289
  buffer += decoder.decode(value, { stream: true });
242
290
  const split = completeBlocks(buffer);
243
291
  buffer = split.rest;
@@ -301,6 +349,7 @@ export async function readUntilTerminal(stream, output) {
301
349
  const reader = stream.getReader();
302
350
  const decoder = new TextDecoder();
303
351
  let buffer = "";
352
+ let bufferedBytes = 0;
304
353
  const collected = [];
305
354
  let released = false;
306
355
  const finish = async (error) => {
@@ -313,6 +362,7 @@ export async function readUntilTerminal(stream, output) {
313
362
  if (output?.destroyed || output?.writableEnded) throw clientClosed();
314
363
  const { done, value } = await reader.read();
315
364
  if (done) break;
365
+ bufferedBytes = noteSseBytes(bufferedBytes, value?.byteLength ?? value?.length ?? 0);
316
366
  buffer += decoder.decode(value, { stream: true });
317
367
  const split = completeBlocks(buffer);
318
368
  buffer = split.rest;
@@ -356,6 +406,7 @@ export async function emitProxySse(parts, output, map, usageBox, onClientByte) {
356
406
  const rewritten = rewrite(part);
357
407
  if (rewritten === null) continue;
358
408
  const block = `${rewritten}\n\n`;
409
+ noteUpstreamFailure(usageBox, rewritten);
359
410
  if (output.destroyed || output.writableEnded) throw clientClosed();
360
411
  const accepted = output.write(block);
361
412
  clientBytes += Buffer.byteLength(block);
package/src/rollout.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
1
+ import { randomUUID } from "node:crypto";
2
+ import { lstat, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
2
3
  import { homedir } from "node:os";
3
4
  import path from "node:path";
4
5
  import { isDeepStrictEqual } from "node:util";
@@ -19,9 +20,37 @@ function recordedProvider(record) {
19
20
  const modelProvider = typeof record.model_provider === "string" ? record.model_provider : "";
20
21
  const model = typeof record.model === "string" ? record.model : "";
21
22
  if (modelProvider === "grok_build_cli" || model.startsWith("grok-")) return "grok_build_cli";
23
+ if (model && !model.startsWith("grok-")) return modelProvider || "non_grok";
22
24
  return modelProvider || null;
23
25
  }
24
26
 
27
+ // Codex 0.166+ resume checks the first session_meta provider. A model-only
28
+ // line is not enough to leave Grok, so a later GPT line names a non-Grok provider.
29
+ export function rewriteRolloutProviderText(text, provider, model) {
30
+ if (typeof text !== "string" || !provider) return text;
31
+ const lines = text.split("\n");
32
+ for (let i = 0; i < lines.length; i += 1) {
33
+ if (!lines[i]) continue;
34
+ let value;
35
+ try {
36
+ value = JSON.parse(lines[i]);
37
+ } catch {
38
+ continue;
39
+ }
40
+ if (value?.type !== "session_meta" || !value.payload || typeof value.payload !== "object")
41
+ continue;
42
+ const sameProvider = value.payload.model_provider === provider;
43
+ const sameModel = model == null || value.payload.model === model;
44
+ if (sameProvider && sameModel) return text;
45
+ const copy = structuredClone(value);
46
+ copy.payload.model_provider = provider;
47
+ if (model != null) copy.payload.model = model;
48
+ lines[i] = JSON.stringify(copy);
49
+ return lines.join("\n");
50
+ }
51
+ return text;
52
+ }
53
+
25
54
  // session_meta sets the provider from model_provider. A later line that names
26
55
  // grok_build_cli, or a model starting with grok-, switches to Grok. Anything
27
56
  // else, including unknown, is not Grok.
@@ -73,17 +102,19 @@ export function rewriteRolloutText(text) {
73
102
 
74
103
  export async function migrateRolloutFile(file) {
75
104
  if (typeof file !== "string" || !file || isSkippedHistoryPath(file)) return false;
76
- let first;
105
+ let info;
77
106
  try {
78
- first = await readFile(file);
107
+ info = await lstat(file);
79
108
  } catch (error) {
80
109
  if (error.code === "ENOENT") return false;
81
110
  throw error;
82
111
  }
112
+ if (!info.isFile()) return false;
113
+ const first = await readFile(file);
83
114
  const next = rewriteRolloutText(first.toString("utf8"));
84
115
  if (next === first.toString("utf8")) return false;
85
- const mode = (await stat(file)).mode & 0o777;
86
- const tmp = `${file}.${process.pid}.tmp`;
116
+ const mode = info.mode & 0o777;
117
+ const tmp = `${file}.${process.pid}.${randomUUID()}.tmp`;
87
118
  await writeFile(tmp, next, { mode });
88
119
  const again = await readFile(file);
89
120
  if (!again.equals(first)) {
package/src/router.mjs CHANGED
@@ -1,14 +1,41 @@
1
1
  import { isDeepStrictEqual } from "node:util";
2
2
  import { catalogModelEntries, isGrokModel, MODEL_ENTRY } from "./models.mjs";
3
+ import { alignConfiguredProvider, rewriteSavedModelProvider } from "./provider-align.mjs";
3
4
 
4
5
  export { MODEL_ENTRY };
5
6
 
7
+ // A non-grok model on a Grok thread is an OpenAI model even when the slug
8
+ // does not start with gpt- (o3, o4-mini, codex-*).
9
+ export function targetProvider(model, snapshotProvider) {
10
+ if (isGrokModel(model)) return "grok_build_cli";
11
+ if (
12
+ snapshotProvider === "grok_build_cli" &&
13
+ typeof model === "string" &&
14
+ model.length > 0
15
+ )
16
+ return "openai";
17
+ return snapshotProvider;
18
+ }
19
+
6
20
  export class Router {
7
- constructor(catalogPath) {
21
+ constructor(catalogPath, options = {}) {
8
22
  this.catalogPath = catalogPath;
23
+ this.codexHome = options.codexHome ?? null;
9
24
  this.pending = new Map();
10
- this.threads = new Map();
11
25
  this.unstarted = new Map();
26
+ this.alignProvider = options.alignProvider ?? ((provider) => {
27
+ if (this.codexHome) alignConfiguredProvider(this.codexHome, provider);
28
+ });
29
+ this.rewriteSaved = options.rewriteSaved ?? ((threadId, provider, snapshot, model) => {
30
+ if (!this.codexHome) return false;
31
+ return rewriteSavedModelProvider({
32
+ codexHome: this.codexHome,
33
+ threadId,
34
+ rolloutPath: snapshot?.path,
35
+ provider,
36
+ model,
37
+ });
38
+ });
12
39
  }
13
40
  applyGrokProvider(params) {
14
41
  params.modelProvider = "grok_build_cli";
@@ -27,15 +54,21 @@ export class Router {
27
54
  const fresh = this.unstarted.get(p.threadId);
28
55
  const snapshot = fresh ?? await rpc("thread/resume", { threadId: p.threadId, excludeTurns: true });
29
56
  const model = selected ?? snapshot.model;
30
- const provider = isGrokModel(model)
31
- ? "grok_build_cli"
32
- : model?.startsWith("gpt-") && snapshot.modelProvider === "grok_build_cli"
33
- ? "openai" : snapshot.modelProvider;
34
- if (provider === snapshot.modelProvider) return;
57
+ const provider = targetProvider(model, snapshot.modelProvider);
58
+ if (provider === snapshot.modelProvider) {
59
+ await this.alignProvider(provider);
60
+ return;
61
+ }
35
62
  if (fresh) throw new Error("Save the first turn before switching providers, or start a new thread with the desired model.");
36
63
  if (snapshot.thread.status?.type !== "idle" || snapshot.thread.ephemeral || snapshot.thread.parentThreadId)
37
64
  throw new Error("Model provider switching requires an idle, saved root thread. Finish the active turn before switching.");
38
65
  await rpc("thread/unsubscribe", { threadId: p.threadId });
66
+ await this.rewriteSaved(p.threadId, provider, snapshot, model);
67
+ await this.alignProvider(provider);
68
+ // Codex 0.166 keeps the loaded thread's provider even after unsubscribe.
69
+ // Archive drops that copy; unarchive reads the provider we just saved.
70
+ await rpc("thread/archive", { threadId: p.threadId });
71
+ await rpc("thread/unarchive", { threadId: p.threadId });
39
72
  const resumed = await rpc("thread/resume", {
40
73
  threadId: p.threadId, model, modelProvider: provider, excludeTurns: true,
41
74
  cwd: snapshot.cwd, approvalPolicy: snapshot.approvalPolicy,
@@ -63,7 +96,6 @@ export class Router {
63
96
  grok
64
97
  ) {
65
98
  msg.params = this.applyGrokProvider(p);
66
- if (p.threadId) this.threads.set(p.threadId, "grok_build_cli");
67
99
  }
68
100
  if (msg.id !== undefined && msg.method)
69
101
  this.pending.set(msg.id, { method: msg.method, params: msg.params ?? p });
@@ -83,20 +115,6 @@ export class Router {
83
115
  msg.result.data.push(entry);
84
116
  }
85
117
  }
86
- if (
87
- ["thread/start", "thread/resume", "thread/fork"].includes(
88
- request.method,
89
- ) &&
90
- msg.result?.thread?.id
91
- ) {
92
- this.threads.set(
93
- msg.result.thread.id,
94
- msg.result.modelProvider ??
95
- msg.result.thread.modelProvider ??
96
- request.params.modelProvider ??
97
- "openai",
98
- );
99
- }
100
118
  return msg;
101
119
  }
102
120
  }
package/src/runtime.mjs CHANGED
@@ -5,6 +5,7 @@ import path from "node:path";
5
5
  import { once } from "node:events";
6
6
  import { createBridgeServer } from "./bridge.mjs";
7
7
  import { catalogModelInfos } from "./models.mjs";
8
+ import { closeVoiceCalls } from "./voice.mjs";
8
9
 
9
10
  export async function startRuntime(options = {}) {
10
11
  const token = randomBytes(32).toString("hex");
@@ -52,6 +53,7 @@ export async function startRuntime(options = {}) {
52
53
  catalogPath,
53
54
  args,
54
55
  async close() {
56
+ closeVoiceCalls();
55
57
  server.closeAllConnections();
56
58
  await new Promise((resolve) => server.close(resolve));
57
59
  await rm(dir, { recursive: true, force: true });
package/src/tools.mjs CHANGED
@@ -197,12 +197,11 @@ function readPointer(root, tokens) {
197
197
 
198
198
  function inlineLocalSchemaRefs(schema) {
199
199
  function resolve(ref, trail) {
200
+ if (typeof ref !== "string" || trail.includes(ref)) return {};
200
201
  const tokens = definitionPointer(ref);
201
- if (!tokens || trail.includes(ref))
202
- throw new Error(`Unresolvable schema $ref: ${ref}`);
202
+ if (!tokens) return {};
203
203
  const target = readPointer(schema, tokens);
204
- if (target === undefined)
205
- throw new Error(`Unresolvable schema $ref: ${ref}`);
204
+ if (target === undefined) return {};
206
205
  return walk(structuredClone(target), trail.concat(ref));
207
206
  }
208
207
 
@@ -210,8 +209,6 @@ function inlineLocalSchemaRefs(schema) {
210
209
  if (Array.isArray(node)) return node.map((item) => walk(item, trail));
211
210
  if (!plainObject(node)) return node;
212
211
  if (Object.prototype.hasOwnProperty.call(node, "$ref")) {
213
- if (typeof node.$ref !== "string")
214
- throw new Error("Unresolvable schema $ref");
215
212
  const resolved = resolve(node.$ref, trail);
216
213
  const rest = {};
217
214
  for (const [key, value] of Object.entries(node)) {
@@ -241,6 +238,14 @@ function emitsObjectParameters(schema) {
241
238
  }
242
239
 
243
240
  function usableParameters(parameters) {
241
+ try {
242
+ return parametersForTool(parameters);
243
+ } catch {
244
+ return OBJECT_SCHEMA;
245
+ }
246
+ }
247
+
248
+ function parametersForTool(parameters) {
244
249
  if (!parameters || typeof parameters !== "object" || Array.isArray(parameters))
245
250
  return OBJECT_SCHEMA;
246
251
  parameters = inlineLocalSchemaRefs(parameters);
package/src/transport.mjs CHANGED
@@ -18,11 +18,36 @@ export const MAX_SOCKETS = 4;
18
18
  export const SOCKET_TIMEOUT_MS = 120_000;
19
19
 
20
20
  const dnsCache = new Map();
21
+ let lookupImpl = dns.lookup;
21
22
 
22
23
  export function clearDnsCache() {
23
24
  dnsCache.clear();
24
25
  }
25
26
 
27
+ export function expireDnsCache() {
28
+ for (const entry of dnsCache.values()) entry.expiresAt = 0;
29
+ }
30
+
31
+ // Tests replace dns.lookup so a cached name can fail on the next query.
32
+ export function setLookupForTests(fn) {
33
+ lookupImpl = fn ?? dns.lookup;
34
+ }
35
+
36
+ function lookupKey(hostname, settings) {
37
+ return [
38
+ hostname,
39
+ settings.family ?? 0,
40
+ settings.all ? 1 : 0,
41
+ settings.hints ?? 0,
42
+ settings.verbatim ? 1 : 0,
43
+ ].join(":");
44
+ }
45
+
46
+ function answerLookup(resolve, settings, address, family) {
47
+ if (settings.all) resolve(null, address);
48
+ else resolve(null, address, family);
49
+ }
50
+
26
51
  /**
27
52
  * dns.lookup with a short-lived cache, in the shape http.Agent expects.
28
53
  * A failed lookup falls back to a still-known address rather than failing the
@@ -31,20 +56,20 @@ export function clearDnsCache() {
31
56
  export function cachedLookup(hostname, options, callback) {
32
57
  const resolve = typeof options === "function" ? options : callback;
33
58
  const settings = typeof options === "function" ? {} : (options ?? {});
34
- const key = `${hostname}:${settings.family ?? 0}`;
59
+ const key = lookupKey(hostname, settings);
35
60
  const cached = dnsCache.get(key);
36
61
  if (cached && cached.expiresAt > Date.now()) {
37
- process.nextTick(() => resolve(null, cached.address, cached.family));
62
+ process.nextTick(() => answerLookup(resolve, settings, cached.address, cached.family));
38
63
  return;
39
64
  }
40
- dns.lookup(hostname, settings, (error, address, family) => {
65
+ lookupImpl(hostname, settings, (error, address, family) => {
41
66
  if (!error) {
42
67
  dnsCache.set(key, { address, family, expiresAt: Date.now() + DNS_TTL_MS });
43
- resolve(null, address, family);
68
+ answerLookup(resolve, settings, address, family);
44
69
  return;
45
70
  }
46
71
  if (cached) {
47
- resolve(null, cached.address, cached.family);
72
+ answerLookup(resolve, settings, cached.address, cached.family);
48
73
  return;
49
74
  }
50
75
  resolve(error);
@@ -102,6 +127,13 @@ export function requestStream(url, init = {}) {
102
127
  );
103
128
  },
104
129
  );
130
+ const timeoutMs = init.timeout ?? SOCKET_TIMEOUT_MS;
131
+ const timedOut = () =>
132
+ request.destroy(
133
+ Object.assign(new Error("Upstream socket timed out"), { code: "ETIMEDOUT" }),
134
+ );
135
+ request.setTimeout(timeoutMs, timedOut);
136
+ request.once("timeout", timedOut);
105
137
  request.once("error", reject);
106
138
  if (init.body === undefined || init.body === null) request.end();
107
139
  else request.end(init.body);
package/src/videogen.mjs CHANGED
@@ -3,6 +3,17 @@ import { homedir } from "node:os";
3
3
  import path from "node:path";
4
4
  import { pathToFileURL } from "node:url";
5
5
 
6
+ function stripCiphertext(value) {
7
+ if (!value || typeof value !== "object") return;
8
+ if (Array.isArray(value)) {
9
+ for (const item of value) stripCiphertext(item);
10
+ return;
11
+ }
12
+ delete value.encrypted_content;
13
+ delete value.encrypted_function_args;
14
+ for (const child of Object.values(value)) stripCiphertext(child);
15
+ }
16
+
6
17
  // cli-chat-proxy has no Responses `video_generation` tool. grok-build generates
7
18
  // video by calling the videos API itself when the model invokes a function
8
19
  // (`image_to_video` / `reference_to_video`): POST {xai_api_base}/videos/generations,
@@ -24,6 +35,21 @@ const VIDEO_MODEL = "grok-imagine-video-1.5";
24
35
  const POLL_LIMIT = 60;
25
36
  const MAX_VIDEO_BYTES = 80 * 1024 * 1024;
26
37
  const MAX_VIDEO_REDIRECTS = 3;
38
+ const VIDEO_REQUEST_TIMEOUT_MS = 300_000;
39
+ const USAGE_KEYS = [
40
+ "input_tokens",
41
+ "output_tokens",
42
+ "total_tokens",
43
+ "cached_prompt_tokens",
44
+ "cache_read_input_tokens",
45
+ ];
46
+
47
+ function combineSignal(signal, ms = VIDEO_REQUEST_TIMEOUT_MS) {
48
+ const timeout = AbortSignal.timeout(ms);
49
+ if (!signal) return timeout;
50
+ if (typeof AbortSignal.any === "function") return AbortSignal.any([signal, timeout]);
51
+ return signal;
52
+ }
27
53
 
28
54
  export const GROK_VIDEO_TOOL = Object.freeze({
29
55
  type: "function",
@@ -124,7 +150,11 @@ function videoPayload(args) {
124
150
  }
125
151
 
126
152
  async function videoHttp(fetchImpl, url, init) {
127
- const response = await fetchImpl(url, { ...init, redirect: "manual" });
153
+ const response = await fetchImpl(url, {
154
+ ...init,
155
+ redirect: "manual",
156
+ signal: combineSignal(init.signal),
157
+ });
128
158
  const text = await response.text().catch(() => "");
129
159
  if (response.status >= 300 && response.status < 400) {
130
160
  const error = new Error("Video generation failed (redirect).");
@@ -229,7 +259,7 @@ async function downloadGeneratedVideo(url, options, redirects = 0) {
229
259
  response = await fetchImpl(url, {
230
260
  method: "GET",
231
261
  redirect: "manual",
232
- signal: options.signal,
262
+ signal: combineSignal(options.signal),
233
263
  });
234
264
  } catch {
235
265
  return null;
@@ -311,6 +341,189 @@ export async function videoToolOutput(call, options) {
311
341
  }
312
342
  }
313
343
 
344
+ function doneItems(parts) {
345
+ const items = [];
346
+ for (const part of parts ?? []) {
347
+ for (const line of String(part).split("\n")) {
348
+ if (!line.startsWith("data:")) continue;
349
+ const payload = line.slice(5).trim();
350
+ if (!payload || payload === "[DONE]") continue;
351
+ let value;
352
+ try {
353
+ value = JSON.parse(payload);
354
+ } catch {
355
+ continue;
356
+ }
357
+ if (value?.type === "response.output_item.done" && value.item) items.push(value.item);
358
+ }
359
+ }
360
+ return items;
361
+ }
362
+
363
+ const videoHandoffs = new Map();
364
+
365
+ function functionCallItem(item) {
366
+ return {
367
+ type: "function_call",
368
+ name: item.name,
369
+ call_id: item.call_id,
370
+ arguments:
371
+ typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments ?? {}),
372
+ };
373
+ }
374
+
375
+ export function ordinaryFunctionCalls(parts) {
376
+ return doneItems(parts)
377
+ .filter((item) => item?.type === "function_call" && item.name !== GROK_VIDEO_TOOL_NAME)
378
+ .map(functionCallItem);
379
+ }
380
+
381
+ export function clearVideoHandoffs() {
382
+ videoHandoffs.clear();
383
+ }
384
+
385
+ export function noteVideoHandoff(threadId, handoff) {
386
+ videoHandoffs.set(threadId || "default", handoff);
387
+ }
388
+
389
+ export function applyVideoHandoff(threadId, input) {
390
+ const pending = videoHandoffs.get(threadId || "default");
391
+ if (!pending || !Array.isArray(input) || !pending.waiting?.length) return input;
392
+ const outputs = new Set(
393
+ input
394
+ .filter((item) => item?.type === "function_call_output" && item.call_id)
395
+ .map((item) => item.call_id),
396
+ );
397
+ if (!pending.waiting.every((id) => outputs.has(id))) return input;
398
+ videoHandoffs.delete(threadId || "default");
399
+ const items = (pending.videos ?? []).flatMap((video) => [
400
+ functionCallItem(video),
401
+ {
402
+ type: "function_call_output",
403
+ name: video.name,
404
+ call_id: video.call_id,
405
+ output: video.output,
406
+ },
407
+ ]);
408
+ const at = input.findIndex(
409
+ (item) => item?.type === "function_call" && pending.waiting.includes(item.call_id),
410
+ );
411
+ if (at < 0) return [...items, ...input];
412
+ return [...input.slice(0, at), ...items, ...input.slice(at)];
413
+ }
414
+
415
+ export function retainedInputItems(parts) {
416
+ const items = [];
417
+ for (const item of doneItems(parts)) {
418
+ if (item?.type !== "reasoning" && item?.type !== "message") continue;
419
+ const copy = structuredClone(item);
420
+ stripCiphertext(copy);
421
+ items.push(copy);
422
+ }
423
+ return items;
424
+ }
425
+
426
+ export function stripVideoToolEvents(parts) {
427
+ const kept = [];
428
+ for (const part of parts ?? []) {
429
+ const lines = [];
430
+ let droppedData = false;
431
+ for (const line of String(part).split("\n")) {
432
+ if (!line.startsWith("data:")) {
433
+ lines.push(line);
434
+ continue;
435
+ }
436
+ const payload = line.slice(5).trim();
437
+ if (!payload || payload === "[DONE]") {
438
+ lines.push(line);
439
+ continue;
440
+ }
441
+ let value;
442
+ try {
443
+ value = JSON.parse(payload);
444
+ } catch {
445
+ lines.push(line);
446
+ continue;
447
+ }
448
+ const item = value?.item;
449
+ if (item?.type === "function_call" && item.name === GROK_VIDEO_TOOL_NAME) {
450
+ droppedData = true;
451
+ continue;
452
+ }
453
+ if (Array.isArray(value?.response?.output)) {
454
+ value.response.output = value.response.output.filter(
455
+ (entry) => !(entry?.type === "function_call" && entry?.name === GROK_VIDEO_TOOL_NAME),
456
+ );
457
+ lines.push(`data: ${JSON.stringify(value)}`);
458
+ continue;
459
+ }
460
+ lines.push(line);
461
+ }
462
+ if (droppedData && !lines.some((line) => line.startsWith("data:"))) continue;
463
+ const text = lines.join("\n");
464
+ if (text.trim()) kept.push(text);
465
+ }
466
+ return kept;
467
+ }
468
+
469
+ export function usageFromParts(parts) {
470
+ let found = null;
471
+ for (const part of parts ?? []) {
472
+ for (const line of String(part).split("\n")) {
473
+ if (!line.startsWith("data:")) continue;
474
+ const payload = line.slice(5).trim();
475
+ if (!payload || payload === "[DONE]") continue;
476
+ try {
477
+ const value = JSON.parse(payload);
478
+ if (
479
+ (value?.type === "response.completed" || value?.type === "response.incomplete") &&
480
+ value.response?.usage
481
+ )
482
+ found = value.response.usage;
483
+ } catch {
484
+ // Ignore non-JSON data lines.
485
+ }
486
+ }
487
+ }
488
+ return found;
489
+ }
490
+
491
+ export function addUsage(total, usage) {
492
+ const next = { ...(total ?? {}) };
493
+ if (!usage) return next;
494
+ for (const key of USAGE_KEYS) {
495
+ if (typeof usage[key] === "number") next[key] = (next[key] ?? 0) + usage[key];
496
+ }
497
+ return next;
498
+ }
499
+
500
+ export function stampUsage(parts, usage) {
501
+ if (!usage || Object.keys(usage).length === 0) return parts;
502
+ return (parts ?? []).map((part) =>
503
+ String(part)
504
+ .split("\n")
505
+ .map((line) => {
506
+ if (!line.startsWith("data:")) return line;
507
+ const payload = line.slice(5).trim();
508
+ if (!payload || payload === "[DONE]") return line;
509
+ try {
510
+ const value = JSON.parse(payload);
511
+ if (
512
+ value?.response &&
513
+ (value.type === "response.completed" || value.type === "response.incomplete")
514
+ ) {
515
+ value.response.usage = { ...(value.response.usage ?? {}), ...usage };
516
+ return `data: ${JSON.stringify(value)}`;
517
+ }
518
+ } catch {
519
+ // Keep a non-JSON data line unchanged.
520
+ }
521
+ return line;
522
+ })
523
+ .join("\n"),
524
+ );
525
+ }
526
+
314
527
  export function videoCallsFromParts(parts) {
315
528
  const calls = [];
316
529
  const seen = new Set();