Skip to content

Commit bb54384

Browse files
author
Your Name
committed
fix(videos): web search coexistence, unified timeout budget, VideoBudget ceiling
Address Wibias CHANGES_REQUESTED review: 1. Web search coexistence (P1): - When wsPlan is active, media bridge is skipped (existing behavior) - Now emits console.warn so the user sees the skip instead of silent loss - Documented priority rule in video-bridge.md 2. Timeout budget (P2): - Start deadline BEFORE submitVideoJob, not after - Poll receives remaining budget (deadline - now), min 5s floor - Submit (60s) + poll now share one videoTimeoutMs deadline 3. VideoBudget aggregate ceiling (optional, addressed): - VideoBudget now has a cap (600 MiB = 3 × single-download max) - chargeVideoBudget() enforces the ceiling per-chunk during streaming - Exceeding the budget throws mid-download (partial file is cleaned up) 4. New tests: done-without-videoUrl, permanent 4xx poll stop, requestId encoding
1 parent 73cab25 commit bb54384

6 files changed

Lines changed: 87 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ The `video_gen` tool accepts:
8181
- **Cost**: Video generation is a paid xAI feature (~$0.05/sec @480p, ~$0.07/sec @720p)
8282
- **One video per call**: Each `video_gen` call produces one video
8383
- **Coexists with Image Bridge**: Both bridges can be enabled simultaneously
84+
- **Web search priority**: When web search is active for a turn, the video bridge is skipped (web search and media bridging cannot run concurrently). A `console.warn` is emitted so you can detect this in logs.
85+
- **Timeout covers submit + poll**: The `videoTimeoutMs` budget starts before job submission, so the submit call (60 s) and subsequent polling share the same deadline.

‎src/images/artifacts.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,24 @@ export async function downloadImageToArtifact(
477477
}
478478

479479
const MAX_VIDEO_DOWNLOAD_BYTES = 200 * 1024 * 1024; // 200 MiB
480+
/** Aggregate per-turn video download cap (600 MiB = 3 × max single download). */
481+
const MAX_VIDEO_BYTES_PER_TURN = MAX_VIDEO_DOWNLOAD_BYTES * 3;
480482

481483
export interface VideoBudget {
482484
spent: number;
485+
/** Ceiling on total bytes across all downloads this turn. */
486+
cap: number;
483487
}
484488

485489
export function createVideoBudget(): VideoBudget {
486-
return { spent: 0 };
490+
return { spent: 0, cap: MAX_VIDEO_BYTES_PER_TURN };
491+
}
492+
493+
/** Charge bytes to the budget; returns false if the ceiling would be exceeded. */
494+
export function chargeVideoBudget(budget: VideoBudget, bytes: number): boolean {
495+
if (budget.spent + bytes > budget.cap) return false;
496+
budget.spent += bytes;
497+
return true;
487498
}
488499

489500
export function guessVideoExtFromMagic(bytes: Uint8Array): string {
@@ -514,7 +525,9 @@ export async function downloadVideoToArtifact(
514525
const isBase64 = meta.includes(";base64");
515526
if (!isBase64) throw new Error("non-base64 data URI for video is not supported");
516527
const buf = Buffer.from(data, "base64");
517-
if (budget) budget.spent += buf.byteLength;
528+
if (budget && !chargeVideoBudget(budget, buf.byteLength)) {
529+
throw new Error("video data URI exceeds per-turn download budget");
530+
}
518531
if (buf.byteLength > MAX_VIDEO_DOWNLOAD_BYTES) throw new Error("video data URI exceeds size cap");
519532
const ext = guessVideoExtFromMagic(buf);
520533
const dir = getArtifactsDir();
@@ -564,7 +577,9 @@ export async function downloadVideoToArtifact(
564577
fh = await open(dest, "w", 0o600);
565578
// Write first chunk and set up accounting
566579
let totalBytes = first.value.byteLength;
567-
if (budget) budget.spent += totalBytes;
580+
if (budget && !chargeVideoBudget(budget, totalBytes)) {
581+
throw new Error("video download exceeds per-turn budget");
582+
}
568583
if (totalBytes > MAX_VIDEO_DOWNLOAD_BYTES) {
569584
throw new Error("video download exceeds size cap");
570585
}
@@ -573,7 +588,9 @@ export async function downloadVideoToArtifact(
573588
const { value, done } = await reader.read();
574589
if (done) break;
575590
totalBytes += value.byteLength;
576-
if (budget) budget.spent += value.byteLength;
591+
if (budget && !chargeVideoBudget(budget, value.byteLength)) {
592+
throw new Error("video download exceeds per-turn budget");
593+
}
577594
if (totalBytes > MAX_VIDEO_DOWNLOAD_BYTES) {
578595
throw new Error("video download exceeds size cap");
579596
}

‎src/images/loop.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ export async function runWithImageBridge(deps: ImageBridgeDeps): Promise<Respons
610610
} else {
611611
paidVideoCalls += 1;
612612
try {
613+
const videoDeadline = Date.now() + (videoTimeoutMs ?? 300_000);
613614
const { requestId } = await submitVideoJob(
614615
{
615616
prompt: vArgs.prompt, model: videoPlan!.model,
@@ -619,7 +620,8 @@ export async function runWithImageBridge(deps: ImageBridgeDeps): Promise<Respons
619620
},
620621
videoPlan!.auth, signal,
621622
);
622-
const pollGen = pollVideoWithHeartbeats(requestId, videoPlan!.auth, signal, videoTimeoutMs);
623+
const remainingMs = Math.max(5_000, videoDeadline - Date.now());
624+
const pollGen = pollVideoWithHeartbeats(requestId, videoPlan!.auth, signal, remainingMs);
623625
let pollResult: { ok: true; videoUrl: string } | { ok: false; error: string };
624626
try {
625627
for (;;) {

‎src/server/responses/core.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,14 @@ export async function handleResponses(
15991599
const imgPlan = !routedCompaction ? await planImageBridge(config, parsed, route.provider) : undefined;
16001600
const vidPlan = !routedCompaction ? await planVideoBridge(config, parsed, route.provider) : undefined;
16011601
const canRunWebSearch = !!wsPlan && !adapter.runTurn;
1602+
if ((imgPlan || vidPlan) && canRunWebSearch) {
1603+
// Web search takes priority when both are active — the media bridge cannot run
1604+
// alongside runWithWebSearch. Surface a runtime signal so the user knows their
1605+
// configured video/image bridge was skipped for this turn, rather than silently
1606+
// dropping a paid capability.
1607+
if (vidPlan) console.warn("[videos] video bridge skipped: web search is active for this turn");
1608+
if (imgPlan) console.warn("[images] image bridge skipped: web search is active for this turn");
1609+
}
16021610
if ((imgPlan || vidPlan) && (!wsPlan || adapter.runTurn)) {
16031611
// The image bridge detects a hosted image_generation tool and requires streaming.
16041612
// The video bridge activates from config and injects a tool — it also needs streaming

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,45 @@ describe("pollVideoWithHeartbeats", () => {
145145
expect(result.ok).toBe(false);
146146
if (!result.ok) expect(result.error).toContain("timed out");
147147
});
148+
149+
test("returns error when done but no videoUrl", async () => {
150+
mock.module("../../src/images/xai-video-client", () => ({
151+
pollVideoJob: mock(() => Promise.resolve({ status: "done" as const })),
152+
}));
153+
154+
const ac = new AbortController();
155+
const gen = pollVideoWithHeartbeats("r1", { baseUrl: "https://api.x.ai/v1", token: "t" }, ac.signal);
156+
let result;
157+
for (;;) {
158+
const { value, done } = await gen.next();
159+
if (done) { result = value; break; }
160+
}
161+
expect(result.ok).toBe(false);
162+
if (!result.ok) expect(result.error).toContain("no video URL");
163+
});
164+
165+
test("stops retrying on permanent 4xx poll error", async () => {
166+
let pollCount = 0;
167+
mock.module("../../src/images/xai-video-client", () => ({
168+
pollVideoJob: mock(() => {
169+
pollCount++;
170+
const err = new Error("xAI videos poll API returned 401") as Error & { status: number };
171+
err.status = 401;
172+
return Promise.reject(err);
173+
}),
174+
}));
175+
176+
const ac = new AbortController();
177+
const gen = pollVideoWithHeartbeats("r1", { baseUrl: "https://api.x.ai/v1", token: "t" }, ac.signal);
178+
let result;
179+
for (;;) {
180+
const { value, done } = await gen.next();
181+
if (done) { result = value; break; }
182+
}
183+
expect(result.ok).toBe(false);
184+
if (!result.ok) expect(result.error).toContain("permanently");
185+
expect(pollCount).toBe(1);
186+
});
148187
});
149188

150189
describe("buildVideoResult", () => {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,18 @@ describe("pollVideoJob", () => {
127127
expect(capturedUrl).toContain("/videos/vid-789");
128128
expect(capturedMethod).toBe("GET");
129129
});
130+
131+
test("encodes requestId in poll URL", async () => {
132+
let capturedUrl: string | undefined;
133+
const fetchMock = mock((url: string) => {
134+
capturedUrl = url;
135+
return Promise.resolve(mockFetchResponse({ status: "processing" }));
136+
});
137+
globalThis.fetch = fetchMock as typeof globalThis.fetch;
138+
139+
await pollVideoJob("req/with?special&chars", auth);
140+
expect(capturedUrl).toContain(encodeURIComponent("req/with?special&chars"));
141+
// Must NOT contain the raw special chars in the path
142+
expect(capturedUrl).not.toMatch(/\/videos\/req\/with/);
143+
});
130144
});

0 commit comments

Comments
 (0)