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.
@@ -0,0 +1,345 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+
6
+ // cli-chat-proxy has no Responses `video_generation` tool. grok-build generates
7
+ // video by calling the videos API itself when the model invokes a function
8
+ // (`image_to_video` / `reference_to_video`): POST {xai_api_base}/videos/generations,
9
+ // then GET /videos/{request_id}. The bridge does the same with the grok login
10
+ // bearer it already sends to cli-chat-proxy — the `key` from that session, not
11
+ // an XAI_API_KEY. The finished clip is a temporary URL, so the bytes are saved
12
+ // locally before that URL expires.
13
+
14
+ export const GROK_VIDEO_TOOL_NAME = "grok_bridge_generate_video";
15
+
16
+ export const DEFAULT_VIDEO_API_BASE = "https://api.x.ai/v1";
17
+
18
+ export const DEFAULT_VIDEO_DIR = path.join(
19
+ homedir(),
20
+ ".local/share/codex-grok-bridge/generated-videos",
21
+ );
22
+
23
+ const VIDEO_MODEL = "grok-imagine-video-1.5";
24
+ const POLL_LIMIT = 60;
25
+ const MAX_VIDEO_BYTES = 80 * 1024 * 1024;
26
+ const MAX_VIDEO_REDIRECTS = 3;
27
+
28
+ export const GROK_VIDEO_TOOL = Object.freeze({
29
+ type: "function",
30
+ name: GROK_VIDEO_TOOL_NAME,
31
+ description:
32
+ "Generate a short video with Grok. The bridge runs this tool itself; Codex does not. " +
33
+ "Provide a prompt. Provide image_url only when the clip should start from that still image. " +
34
+ "The result names the temporary URL and the local file the bridge saved.",
35
+ parameters: {
36
+ type: "object",
37
+ properties: {
38
+ prompt: { type: "string", description: "What the video should show." },
39
+ image_url: {
40
+ type: "string",
41
+ description: "Optional http(s) or data URL of a still image to animate.",
42
+ },
43
+ duration: {
44
+ type: "integer",
45
+ description: "Length in seconds, from 1 to 15.",
46
+ },
47
+ aspect_ratio: {
48
+ type: "string",
49
+ description: "One of 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3.",
50
+ },
51
+ resolution: {
52
+ type: "string",
53
+ description: "Optional 480p, 720p, or 1080p. Omit for the server default.",
54
+ },
55
+ },
56
+ required: ["prompt"],
57
+ additionalProperties: false,
58
+ },
59
+ });
60
+
61
+ function scrub(text, token) {
62
+ let out = String(text ?? "");
63
+ out = out.replace(/Bearer\s+\S+/gi, "Bearer [redacted]");
64
+ if (token) out = out.split(token).join("[redacted]");
65
+ return out;
66
+ }
67
+
68
+ function parseArgs(value) {
69
+ if (value && typeof value === "object") return value;
70
+ if (typeof value !== "string" || !value.trim()) return {};
71
+ try {
72
+ const parsed = JSON.parse(value);
73
+ return parsed && typeof parsed === "object" ? parsed : {};
74
+ } catch {
75
+ return {};
76
+ }
77
+ }
78
+
79
+ const ASPECT_RATIOS = new Set(["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3"]);
80
+ const VIDEO_RESOLUTIONS = new Set(["480p", "720p", "1080p"]);
81
+
82
+ function videoDuration(value) {
83
+ if (Number.isInteger(value)) return value;
84
+ if (typeof value === "string" && /^\d+$/.test(value)) return Number(value);
85
+ return null;
86
+ }
87
+
88
+ function videoArgumentError(args) {
89
+ if (typeof args.image_url === "string" && args.image_url.trim()) {
90
+ const image = args.image_url.trim();
91
+ if (!/^https?:\/\//i.test(image) && !/^data:/i.test(image)) {
92
+ return "Video generation failed: image_url must be an http(s) URL or a data URL.";
93
+ }
94
+ }
95
+ if (args.duration != null && args.duration !== "") {
96
+ const duration = videoDuration(args.duration);
97
+ if (duration == null || duration < 1 || duration > 15) {
98
+ return "Video generation failed: duration must be an integer from 1 to 15.";
99
+ }
100
+ }
101
+ const ratio = typeof args.aspect_ratio === "string" ? args.aspect_ratio.trim() : "";
102
+ if (ratio && !ASPECT_RATIOS.has(ratio)) {
103
+ return "Video generation failed: aspect_ratio must be one of 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3.";
104
+ }
105
+ const resolution = typeof args.resolution === "string" ? args.resolution.trim() : "";
106
+ if (resolution && !VIDEO_RESOLUTIONS.has(resolution)) {
107
+ return "Video generation failed: resolution must be 480p, 720p, or 1080p.";
108
+ }
109
+ return "";
110
+ }
111
+
112
+ function videoPayload(args) {
113
+ const payload = { model: VIDEO_MODEL, prompt: args.prompt };
114
+ if (typeof args.image_url === "string" && args.image_url.trim())
115
+ payload.image = { url: args.image_url.trim() };
116
+ if (Number.isInteger(args.duration)) payload.duration = args.duration;
117
+ else if (typeof args.duration === "string" && /^\d+$/.test(args.duration))
118
+ payload.duration = Number(args.duration);
119
+ if (typeof args.aspect_ratio === "string" && args.aspect_ratio.trim())
120
+ payload.aspect_ratio = args.aspect_ratio.trim();
121
+ if (typeof args.resolution === "string" && VIDEO_RESOLUTIONS.has(args.resolution.trim()))
122
+ payload.resolution = args.resolution.trim();
123
+ return payload;
124
+ }
125
+
126
+ async function videoHttp(fetchImpl, url, init) {
127
+ const response = await fetchImpl(url, { ...init, redirect: "manual" });
128
+ const text = await response.text().catch(() => "");
129
+ if (response.status >= 300 && response.status < 400) {
130
+ const error = new Error("Video generation failed (redirect).");
131
+ error.status = response.status;
132
+ throw error;
133
+ }
134
+ let body = null;
135
+ if (text) {
136
+ try {
137
+ body = JSON.parse(text);
138
+ } catch {
139
+ body = null;
140
+ }
141
+ }
142
+ return { status: response.status, body };
143
+ }
144
+
145
+ function httpFailure(status) {
146
+ const error = new Error(`Video generation failed (${status}).`);
147
+ error.status = status;
148
+ return error;
149
+ }
150
+
151
+ export async function generateVideo(options) {
152
+ const fetchImpl = options.fetchImpl ?? fetch;
153
+ const base = (options.baseUrl ?? DEFAULT_VIDEO_API_BASE).replace(/\/$/, "");
154
+ const headers = {
155
+ authorization: `Bearer ${options.token}`,
156
+ "content-type": "application/json",
157
+ };
158
+ const started = await videoHttp(fetchImpl, `${base}/videos/generations`, {
159
+ method: "POST",
160
+ headers,
161
+ body: JSON.stringify(videoPayload(options)),
162
+ signal: options.signal,
163
+ });
164
+ if (started.status < 200 || started.status >= 300)
165
+ throw httpFailure(started.status);
166
+ const requestId = started.body?.request_id;
167
+ if (typeof requestId !== "string" || !requestId)
168
+ throw httpFailure(started.status);
169
+ const pause = options.pause ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
170
+ for (let attempt = 0; attempt < POLL_LIMIT; attempt += 1) {
171
+ if (attempt > 0) await pause(5000);
172
+ const poll = await videoHttp(
173
+ fetchImpl,
174
+ `${base}/videos/${encodeURIComponent(requestId)}`,
175
+ { method: "GET", headers, signal: options.signal },
176
+ );
177
+ if (poll.status === 202) continue;
178
+ if (poll.status < 200 || poll.status >= 300) throw httpFailure(poll.status);
179
+ const status = poll.body?.status;
180
+ if (status === "done") {
181
+ const url = poll.body?.video?.url;
182
+ if (typeof url === "string" && url) return url;
183
+ const withheld =
184
+ poll.body?.respect_moderation === false ||
185
+ poll.body?.video?.respect_moderation === false;
186
+ if (withheld) {
187
+ const error = new Error("Video generation was withheld by moderation.");
188
+ error.moderation = true;
189
+ throw error;
190
+ }
191
+ throw new Error("Video generation finished without a URL.");
192
+ }
193
+ if (status === "failed" || status === "expired")
194
+ throw new Error(`Video generation ${status}.`);
195
+ }
196
+ throw new Error("Video generation timed out.");
197
+ }
198
+
199
+ function looksLikeVideo(bytes, contentType) {
200
+ if (bytes.length >= 8 && bytes.subarray(4, 8).toString("ascii") === "ftyp") return "mp4";
201
+ if (
202
+ bytes.length >= 4 &&
203
+ bytes[0] === 0x1a &&
204
+ bytes[1] === 0x45 &&
205
+ bytes[2] === 0xdf &&
206
+ bytes[3] === 0xa3
207
+ )
208
+ return "webm";
209
+ const type = String(contentType ?? "").toLowerCase();
210
+ if (type.includes("webm")) return "webm";
211
+ if (type.startsWith("video/")) return "mp4";
212
+ return "";
213
+ }
214
+
215
+ // The finished URL is a temporary file host (vidgen.x.ai), not the API host.
216
+ // The login bearer stays on the poll and is not sent with the download.
217
+ async function downloadGeneratedVideo(url, options, redirects = 0) {
218
+ if (redirects > MAX_VIDEO_REDIRECTS) return null;
219
+ let parsed;
220
+ try {
221
+ parsed = new URL(url);
222
+ } catch {
223
+ return null;
224
+ }
225
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
226
+ const fetchImpl = options.fetchImpl ?? fetch;
227
+ let response;
228
+ try {
229
+ response = await fetchImpl(url, {
230
+ method: "GET",
231
+ redirect: "manual",
232
+ signal: options.signal,
233
+ });
234
+ } catch {
235
+ return null;
236
+ }
237
+ if (response.status >= 300 && response.status < 400) {
238
+ const location = response.headers?.get?.("location");
239
+ await response.arrayBuffer?.().catch(() => {});
240
+ if (!location) return null;
241
+ let next;
242
+ try {
243
+ next = new URL(location, url).href;
244
+ } catch {
245
+ return null;
246
+ }
247
+ return downloadGeneratedVideo(next, options, redirects + 1);
248
+ }
249
+ if (response.status < 200 || response.status >= 300) {
250
+ await response.arrayBuffer?.().catch(() => {});
251
+ return null;
252
+ }
253
+ const advertised = Number(response.headers?.get?.("content-length"));
254
+ if (Number.isFinite(advertised) && advertised > MAX_VIDEO_BYTES) {
255
+ await response.arrayBuffer?.().catch(() => {});
256
+ return null;
257
+ }
258
+ let bytes;
259
+ try {
260
+ bytes = Buffer.from(await response.arrayBuffer());
261
+ } catch {
262
+ return null;
263
+ }
264
+ if (!bytes.length || bytes.length > MAX_VIDEO_BYTES) return null;
265
+ const extension = looksLikeVideo(bytes, response.headers?.get?.("content-type"));
266
+ if (!extension) return null;
267
+ const dir = options.dir ?? DEFAULT_VIDEO_DIR;
268
+ const stamp = (options.now ?? Date.now()).toString(36);
269
+ const file = path.join(dir, `grok-${stamp}.${extension}`);
270
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
271
+ writeFileSync(file, bytes, { mode: 0o600 });
272
+ return file;
273
+ }
274
+
275
+ export async function videoToolOutput(call, options) {
276
+ const token = options?.token;
277
+ try {
278
+ const args = parseArgs(call?.arguments);
279
+ const prompt = typeof args.prompt === "string" ? args.prompt.trim() : "";
280
+ if (!prompt) return "Video generation failed: a prompt is required.";
281
+ const invalid = videoArgumentError(args);
282
+ if (invalid) return invalid;
283
+ const url = await generateVideo({
284
+ token: options.token,
285
+ fetchImpl: options.fetchImpl,
286
+ baseUrl: options.baseUrl,
287
+ pause: options.pause,
288
+ signal: options.signal,
289
+ prompt,
290
+ image_url: args.image_url,
291
+ duration: args.duration,
292
+ aspect_ratio: args.aspect_ratio,
293
+ resolution: args.resolution,
294
+ });
295
+ let file = null;
296
+ try {
297
+ file = await downloadGeneratedVideo(url, options);
298
+ } catch {
299
+ file = null;
300
+ }
301
+ const note = file
302
+ ? `Video ready at ${url}. Saved to [${file}](${pathToFileURL(file).href}).`
303
+ : `Video ready at ${url}`;
304
+ return scrub(note, token);
305
+ } catch (error) {
306
+ const status = Number(error?.status) || 0;
307
+ if (status >= 400 && status < 500) return `Video generation failed (${status}).`;
308
+ if (error?.moderation)
309
+ return "Video generation finished, but moderation withheld the URL.";
310
+ return "Video generation failed.";
311
+ }
312
+ }
313
+
314
+ export function videoCallsFromParts(parts) {
315
+ const calls = [];
316
+ const seen = new Set();
317
+ for (const part of parts ?? []) {
318
+ for (const line of String(part).split("\n")) {
319
+ if (!line.startsWith("data:")) continue;
320
+ const payload = line.slice(5).trim();
321
+ if (!payload || payload === "[DONE]") continue;
322
+ let value;
323
+ try {
324
+ value = JSON.parse(payload);
325
+ } catch {
326
+ continue;
327
+ }
328
+ const item = value?.item;
329
+ if (value?.type !== "response.output_item.done") continue;
330
+ if (item?.type !== "function_call" || item.name !== GROK_VIDEO_TOOL_NAME)
331
+ continue;
332
+ if (typeof item.call_id !== "string" || seen.has(item.call_id)) continue;
333
+ seen.add(item.call_id);
334
+ calls.push({
335
+ name: item.name,
336
+ call_id: item.call_id,
337
+ arguments:
338
+ typeof item.arguments === "string"
339
+ ? item.arguments
340
+ : JSON.stringify(item.arguments ?? {}),
341
+ });
342
+ }
343
+ }
344
+ return calls;
345
+ }