Skip to content

Commit 4722563

Browse files
author
Your Name
committed
fix(videos): address Codex P1/P2 + CodeRabbit review findings
P1 fixes: - Non-streaming requests no longer 400 when video bridge is enabled (only image bridge requires stream=true; video-only skips bridge for non-stream requests) - Restore globalThis.fetch after video client tests (afterEach cleanup) P2 fixes: - Use clamped vidPlan.timeoutMs instead of raw config value - Honor videoMaxRounds when both image+video bridges active (tighter wins) - guessVideoExtFromMagic throws on unrecognized magic (was defaulting to mp4) - Poll 4xx permanent failures (except 429) fail fast instead of retrying - buildVideoResult uses pathToFileURL for cross-platform markdown links - Initial poll interval restored to 5s (was 200ms) - done-without-videoUrl returns error instead of spinning until timeout - VideoBudget charges every streamed chunk (was only first chunk) - downloadVideoToArtifact: reader cleanup on file-open failure - readBoundedText: cancel reader body on size cap breach - Namespaced video_gen tools preserved in dedup filter - Fix JSON missing closing brace in docs - Clarify API key auth requirement in docs prerequisites
1 parent 78109c2 commit 4722563

8 files changed

Lines changed: 84 additions & 50 deletions

File tree

‎docs-site/src/content/docs/guides/video-bridge.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ job to xAI, polls until completion, and downloads the result.
1212

1313
## Prerequisites
1414

15-
- An xAI account with an API key (`ocx login xai` or set the key in your provider config)
15+
- An xAI account with an API key (set `XAI_API_KEY` or configure the key in your provider config — `ocx login xai` alone is not sufficient for the video bridge, which requires API key auth)
1616
- A non-OpenAI model as your routed provider (e.g. Anthropic Claude, Google Gemini)
1717
- opencodex configured to route through the non-OpenAI provider
1818

@@ -28,6 +28,7 @@ Add `videoBridgeEnabled: true` to your `images` config:
2828
"videoBridgeModel": "grok-imagine-video",
2929
"videoMaxRounds": 2,
3030
"videoTimeoutMs": 300000
31+
}
3132
}
3233
```
3334

‎src/images/artifacts.ts‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,13 @@ export function createVideoBudget(): VideoBudget {
487487
}
488488

489489
export function guessVideoExtFromMagic(bytes: Uint8Array): string {
490+
if (bytes.byteLength < 12) throw new Error("video data too short for magic byte sniffing");
490491
const sig = Buffer.from(bytes.slice(0, 12)).toString("latin1");
491492
// MP4/QuickTime/MOV: bytes 4-7 == "ftyp" (ISO BMFF)
492493
if (sig.slice(4, 8) === "ftyp") return "mp4";
493494
// WebM/Matroska: \x1a\x45\xdf\xa3
494495
if (sig.startsWith("\x1a\x45\xdf\xa3")) return "webm";
495-
return "mp4";
496+
throw new Error("unrecognized video format — magic bytes do not match MP4 or WebM");
496497
}
497498

498499
/**
@@ -549,39 +550,46 @@ export async function downloadVideoToArtifact(
549550
if (!reader) throw new Error("video download returned no body");
550551

551552
// Peek the first chunk for magic-byte sniffing before opening the file.
552-
const first = await reader.read();
553-
if (first.done || !first.value) {
554-
throw new Error("video download returned empty body");
555-
}
556-
const ext = guessVideoExtFromMagic(first.value);
557-
const name = `vid-${timestampPrefix()}-${crypto.randomUUID()}.${ext}`;
558-
const dest = join(dir, name);
559-
const fh = await open(dest, "w", 0o600);
560-
let totalBytes = first.value.byteLength;
561-
if (budget) budget.spent += totalBytes;
562-
if (totalBytes > MAX_VIDEO_DOWNLOAD_BYTES) {
563-
reader.releaseLock();
564-
await fh.close();
565-
await unlink(dest).catch(() => {});
566-
throw new Error("video download exceeds size cap");
567-
}
568-
let success = false;
553+
// If anything fails between acquiring the reader and opening the file, cancel + release the reader.
554+
let dest: string | undefined;
555+
let fh: { close(): Promise<void>; writeFile(data: Uint8Array): Promise<void> } | undefined;
569556
try {
557+
const first = await reader.read();
558+
if (first.done || !first.value) {
559+
throw new Error("video download returned empty body");
560+
}
561+
const ext = guessVideoExtFromMagic(first.value);
562+
const name = `vid-${timestampPrefix()}-${crypto.randomUUID()}.${ext}`;
563+
dest = join(dir, name);
564+
fh = await open(dest, "w", 0o600);
565+
// Write first chunk and set up accounting
566+
let totalBytes = first.value.byteLength;
567+
if (budget) budget.spent += totalBytes;
568+
if (totalBytes > MAX_VIDEO_DOWNLOAD_BYTES) {
569+
throw new Error("video download exceeds size cap");
570+
}
570571
await fh.writeFile(first.value);
571572
for (;;) {
572573
const { value, done } = await reader.read();
573574
if (done) break;
574575
totalBytes += value.byteLength;
576+
if (budget) budget.spent += value.byteLength;
575577
if (totalBytes > MAX_VIDEO_DOWNLOAD_BYTES) {
576578
throw new Error("video download exceeds size cap");
577579
}
578580
await fh.writeFile(value);
579581
}
580-
success = true;
581-
} finally {
582-
reader.releaseLock();
583582
await fh.close();
584-
if (!success) await unlink(dest).catch(() => {});
583+
try { await reader.cancel(); } catch { /* ignore */ }
584+
reader.releaseLock();
585+
return dest;
586+
} catch (err) {
587+
try { await reader.cancel(); } catch { /* ignore */ }
588+
reader.releaseLock();
589+
if (fh) {
590+
try { await fh.close(); } catch { /* ignore */ }
591+
}
592+
if (dest) await unlink(dest).catch(() => {});
593+
throw err;
585594
}
586-
return dest;
587595
}

‎src/images/fulfill-video.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pathToFileURL } from "node:url";
12
import type { VideoBridgePlan, VideoCallResult } from "./types";
23
import { submitVideoJob, pollVideoJob } from "./xai-video-client";
34
import { downloadVideoToArtifact, createVideoBudget, type VideoBudget } from "./artifacts";
@@ -11,7 +12,7 @@ export interface ParsedVideoArgs {
1112
aspectRatio?: string;
1213
}
1314

14-
const INITIAL_POLL_INTERVAL_MS = 200;
15+
const INITIAL_POLL_INTERVAL_MS = 5_000;
1516
const MAX_POLL_INTERVAL_MS = 15_000;
1617
const POLL_BACKOFF = 1.5;
1718
const DEFAULT_VIDEO_TIMEOUT_MS = 300_000; // 5 min
@@ -91,8 +92,11 @@ export async function* pollVideoWithHeartbeats(
9192

9293
try {
9394
const poll = await pollVideoJob(requestId, auth, signal);
94-
if (poll.status === "done" && poll.videoUrl) {
95-
return { ok: true, videoUrl: poll.videoUrl };
95+
if (poll.status === "done") {
96+
if (poll.videoUrl) {
97+
return { ok: true, videoUrl: poll.videoUrl };
98+
}
99+
return { ok: false, error: "video generation completed but no video URL was returned" };
96100
}
97101
if (poll.status === "failed") {
98102
return { ok: false, error: "video generation failed" };
@@ -102,10 +106,12 @@ export async function* pollVideoWithHeartbeats(
102106
}
103107
// still processing — continue with backoff
104108
} catch (e) {
105-
// Transient poll errors (timeout, network) are tolerable — keep polling.
106-
const msg = e instanceof Error ? e.message : String(e);
107109
if (signal.aborted) return { ok: false, error: "client closed request during video generation" };
108-
console.warn(`[videos] poll error (will retry): ${msg}`);
110+
const status = (e as Error & { status?: number }).status;
111+
if (status && status >= 400 && status < 500 && status !== 429) {
112+
return { ok: false, error: `video poll failed permanently: HTTP ${status}` };
113+
}
114+
console.warn(`[videos] poll error (will retry): ${e instanceof Error ? e.message : String(e)}`);
109115
}
110116

111117
const elapsed = Math.floor((Date.now() - start) / 1000);
@@ -132,7 +138,7 @@ export function buildVideoResult(path: string, prompt: string, model: string): V
132138
path,
133139
files: [path],
134140
count: 1,
135-
markdown: `[video](${path})`,
141+
markdown: `[video](${pathToFileURL(path).href})`,
136142
};
137143
}
138144

‎src/images/xai-video-client.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async function readBoundedText(resp: Response): Promise<string> {
5353
}
5454
text += decoder.decode();
5555
} finally {
56+
try { await reader.cancel(); } catch { /* ignore */ }
5657
reader.releaseLock();
5758
}
5859
return text;
@@ -89,7 +90,9 @@ export async function submitVideoJob(
8990

9091
if (!resp.ok) {
9192
try { await resp.body?.cancel(); } catch { /* ignore */ }
92-
throw new Error("xAI videos API returned " + resp.status);
93+
const err = new Error("xAI videos API returned " + resp.status) as Error & { status: number };
94+
err.status = resp.status;
95+
throw err;
9396
}
9497

9598
const text = await readBoundedText(resp);
@@ -123,7 +126,9 @@ export async function pollVideoJob(
123126

124127
if (!resp.ok) {
125128
try { await resp.body?.cancel(); } catch { /* ignore */ }
126-
throw new Error("xAI videos poll API returned " + resp.status);
129+
const err = new Error("xAI videos poll API returned " + resp.status) as Error & { status: number };
130+
err.status = resp.status;
131+
throw err;
127132
}
128133

129134
const text = await readBoundedText(resp);

‎src/server/responses/core.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,11 +1600,16 @@ export async function handleResponses(
16001600
const vidPlan = !routedCompaction ? await planVideoBridge(config, parsed, route.provider) : undefined;
16011601
const canRunWebSearch = !!wsPlan && !adapter.runTurn;
16021602
if ((imgPlan || vidPlan) && (!wsPlan || adapter.runTurn)) {
1603-
// The bridge forces stream:true internally and returns SSE. Non-streaming requests can't be
1604-
// served — reject explicitly rather than returning SSE to a client expecting JSON.
1603+
// The image bridge detects a hosted image_generation tool and requires streaming.
1604+
// The video bridge activates from config and injects a tool — it also needs streaming
1605+
// (the loop returns SSE). For video-only (no imgPlan) on a non-streaming request, skip
1606+
// the bridge entirely so enabling the feature doesn't break ordinary non-streaming traffic.
16051607
if (!parsed.stream) {
1606-
return formatErrorResponse(400, "invalid_request_error", "media bridge requires stream=true");
1607-
}
1608+
if (imgPlan) {
1609+
return formatErrorResponse(400, "invalid_request_error", "image bridge requires stream=true");
1610+
}
1611+
// Video-only: skip bridge for non-streaming requests
1612+
} else {
16081613
// Replace any pre-existing image_gen/video_gen aliases instead of appending duplicate wire names.
16091614
const priorTools = parsed.context.tools ?? [];
16101615
const bridgeTools = [...priorTools.filter(t => {
@@ -1613,6 +1618,7 @@ export async function handleResponses(
16131618
if (imgPlan && imgPlan.toolNames.has(t.name)) return false;
16141619
if (imgPlan && t.namespace && imgPlan.toolNames.has(namespacedToolName(t.namespace, t.name))) return false;
16151620
if (vidPlan && vidPlan.toolNames.has(t.name)) return false;
1621+
if (vidPlan && t.namespace && vidPlan.toolNames.has(namespacedToolName(t.namespace, t.name))) return false;
16161622
return true;
16171623
})];
16181624
const existingNames = new Set(bridgeTools.map(t => t.name));
@@ -1639,12 +1645,16 @@ export async function handleResponses(
16391645
forwardHeaders: selectedForwardHeaders,
16401646
onAttemptSend: () => noteAttemptSend(logCtx.activeAttempt, logCtx.usageLogInputTokens),
16411647
abortSignal: options.abortSignal,
1642-
maxRounds: imgPlan ? clampImageMaxRounds(config.images?.maxRounds) : clampImageMaxRounds(config.images?.videoMaxRounds ?? 2),
1648+
maxRounds: imgPlan && vidPlan
1649+
? clampImageMaxRounds(Math.min(config.images?.maxRounds ?? 3, config.images?.videoMaxRounds ?? 2))
1650+
: imgPlan
1651+
? clampImageMaxRounds(config.images?.maxRounds)
1652+
: clampImageMaxRounds(config.images?.videoMaxRounds ?? 2),
16431653
connectTimeoutMs: config.connectTimeoutMs ?? 200_000,
16441654
stallTimeoutSec: config.stallTimeoutSec,
16451655
fetchImpl: providerFetch(route.provider),
16461656
onRequestBuilt: request => recordAdapterReasoning(logCtx, request),
1647-
...(config.images?.videoTimeoutMs ? { videoTimeoutMs: config.images.videoTimeoutMs } : {}),
1657+
...(vidPlan?.timeoutMs ? { videoTimeoutMs: vidPlan.timeoutMs } : {}),
16481658
onUsage: usage => {
16491659
// Cursor may assign _cursorConversationId inside the image loop's first runTurn;
16501660
// backfill so Logs can filter/total that opening request (parity with the normal
@@ -1690,6 +1700,7 @@ export async function handleResponses(
16901700
});
16911701
}
16921702
return imgResponse;
1703+
} // end else (streaming bridge)
16931704
}
16941705

16951706
// Web-search sidecar: Codex enabled web_search but this is a routed (non-OpenAI) model that can't

‎tests/images/z-handler-activation.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe("image bridge dispatch priority (handler activation)", () => {
149149
const res = await post(false, [{ type: "image_generation" }]);
150150
expect(res.status).toBe(400);
151151
expect(imageBridgeRun).toBe(false);
152-
expect((await res.text())).toContain("media bridge requires stream=true");
152+
expect((await res.text())).toContain("image bridge requires stream=true");
153153
});
154154

155155
test("dual-tool (image_generation + web_search), both eligible → web-search wins, image bridge deferred", async () => {

‎tests/videos/fulfill-video.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("pollVideoWithHeartbeats", () => {
112112
}
113113
expect(heartbeats.length).toBeGreaterThanOrEqual(1);
114114
expect(result.ok).toBe(true);
115-
});
115+
}, 15_000); // 5s initial poll interval means ~10s for 2 polls
116116

117117
test("returns failed status", async () => {
118118
mock.module("../../src/images/xai-video-client", () => ({
@@ -158,4 +158,9 @@ describe("buildVideoResult", () => {
158158
expect(result.count).toBe(1);
159159
expect(result.markdown).toContain("vid-123.mp4");
160160
});
161+
162+
test("uses file:// URL in markdown", () => {
163+
const result = buildVideoResult("/tmp/vid-123.mp4", "dance", "grok-imagine-video");
164+
expect(result.markdown).toContain("file://");
165+
});
161166
});

‎tests/videos/xai-video-client.test.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { describe, expect, test, mock, beforeEach } from "bun:test";
1+
import { describe, expect, test, mock, afterEach } from "bun:test";
22
import { submitVideoJob, pollVideoJob } from "../../src/images/xai-video-client";
33

44
const auth = { baseUrl: "https://api.x.ai/v1", token: "test-key" };
55

6+
const originalFetch = globalThis.fetch;
7+
afterEach(() => {
8+
globalThis.fetch = originalFetch;
9+
mock.restore();
10+
});
11+
612
function mockFetchResponse(body: unknown, status = 200): Response {
713
return new Response(JSON.stringify(body), {
814
status,
@@ -11,10 +17,6 @@ function mockFetchResponse(body: unknown, status = 200): Response {
1117
}
1218

1319
describe("submitVideoJob", () => {
14-
beforeEach(() => {
15-
mock.restore();
16-
});
17-
1820
test("returns request_id from response", async () => {
1921
const fetchMock = mock(() => Promise.resolve(mockFetchResponse({ request_id: "vid-123" })));
2022
globalThis.fetch = fetchMock as typeof globalThis.fetch;
@@ -68,10 +70,6 @@ describe("submitVideoJob", () => {
6870
});
6971

7072
describe("pollVideoJob", () => {
71-
beforeEach(() => {
72-
mock.restore();
73-
});
74-
7573
test("returns done status with video URL", async () => {
7674
const fetchMock = mock(() => Promise.resolve(mockFetchResponse({
7775
status: "done",

0 commit comments

Comments
 (0)