codex-grok-bridge 1.7.2 → 1.8.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/src/videogen.mjs CHANGED
@@ -1,23 +1,37 @@
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
+
1
6
  // cli-chat-proxy has no Responses `video_generation` tool. grok-build generates
2
7
  // video by calling the videos API itself when the model invokes a function
3
8
  // (`image_to_video` / `reference_to_video`): POST {xai_api_base}/videos/generations,
4
9
  // then GET /videos/{request_id}. The bridge does the same with the grok login
5
10
  // bearer it already sends to cli-chat-proxy — the `key` from that session, not
6
- // an XAI_API_KEY.
11
+ // an XAI_API_KEY. The finished clip is a temporary URL, so the bytes are saved
12
+ // locally before that URL expires.
7
13
 
8
14
  export const GROK_VIDEO_TOOL_NAME = "grok_bridge_generate_video";
9
15
 
10
16
  export const DEFAULT_VIDEO_API_BASE = "https://api.x.ai/v1";
11
17
 
18
+ export const DEFAULT_VIDEO_DIR = path.join(
19
+ homedir(),
20
+ ".local/share/codex-grok-bridge/generated-videos",
21
+ );
22
+
12
23
  const VIDEO_MODEL = "grok-imagine-video-1.5";
13
24
  const POLL_LIMIT = 60;
25
+ const MAX_VIDEO_BYTES = 80 * 1024 * 1024;
26
+ const MAX_VIDEO_REDIRECTS = 3;
14
27
 
15
28
  export const GROK_VIDEO_TOOL = Object.freeze({
16
29
  type: "function",
17
30
  name: GROK_VIDEO_TOOL_NAME,
18
31
  description:
19
32
  "Generate a short video with Grok. The bridge runs this tool itself; Codex does not. " +
20
- "Provide a prompt. Provide image_url only when the clip should start from that still image.",
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.",
21
35
  parameters: {
22
36
  type: "object",
23
37
  properties: {
@@ -34,6 +48,10 @@ export const GROK_VIDEO_TOOL = Object.freeze({
34
48
  type: "string",
35
49
  description: "One of 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3.",
36
50
  },
51
+ resolution: {
52
+ type: "string",
53
+ description: "Optional 480p, 720p, or 1080p. Omit for the server default.",
54
+ },
37
55
  },
38
56
  required: ["prompt"],
39
57
  additionalProperties: false,
@@ -58,6 +76,39 @@ function parseArgs(value) {
58
76
  }
59
77
  }
60
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
+
61
112
  function videoPayload(args) {
62
113
  const payload = { model: VIDEO_MODEL, prompt: args.prompt };
63
114
  if (typeof args.image_url === "string" && args.image_url.trim())
@@ -67,6 +118,8 @@ function videoPayload(args) {
67
118
  payload.duration = Number(args.duration);
68
119
  if (typeof args.aspect_ratio === "string" && args.aspect_ratio.trim())
69
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();
70
123
  return payload;
71
124
  }
72
125
 
@@ -126,9 +179,16 @@ export async function generateVideo(options) {
126
179
  const status = poll.body?.status;
127
180
  if (status === "done") {
128
181
  const url = poll.body?.video?.url;
129
- if (typeof url !== "string" || !url)
130
- throw new Error("Video generation finished without a URL.");
131
- return 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.");
132
192
  }
133
193
  if (status === "failed" || status === "expired")
134
194
  throw new Error(`Video generation ${status}.`);
@@ -136,12 +196,90 @@ export async function generateVideo(options) {
136
196
  throw new Error("Video generation timed out.");
137
197
  }
138
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
+
139
275
  export async function videoToolOutput(call, options) {
140
276
  const token = options?.token;
141
277
  try {
142
278
  const args = parseArgs(call?.arguments);
143
279
  const prompt = typeof args.prompt === "string" ? args.prompt.trim() : "";
144
280
  if (!prompt) return "Video generation failed: a prompt is required.";
281
+ const invalid = videoArgumentError(args);
282
+ if (invalid) return invalid;
145
283
  const url = await generateVideo({
146
284
  token: options.token,
147
285
  fetchImpl: options.fetchImpl,
@@ -152,12 +290,23 @@ export async function videoToolOutput(call, options) {
152
290
  image_url: args.image_url,
153
291
  duration: args.duration,
154
292
  aspect_ratio: args.aspect_ratio,
293
+ resolution: args.resolution,
155
294
  });
156
- return scrub(`Video ready at ${url}`, token);
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);
157
305
  } catch (error) {
158
306
  const status = Number(error?.status) || 0;
159
- if (status === 401 || status === 403)
160
- return `Video generation failed (${status}).`;
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.";
161
310
  return "Video generation failed.";
162
311
  }
163
312
  }