Skip to content

Commit 49091e6

Browse files
committed
fix(cli): propagate live capture budget
1 parent 45aead8 commit 49091e6

11 files changed

Lines changed: 393 additions & 57 deletions

‎packages/cli/src/capture/contentExtractor.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { afterEach, describe, expect, it, vi } from "vitest";
22
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5-
import { captionImagesWithGemini } from "./contentExtractor.js";
5+
import {
6+
captionImagesWithGemini,
7+
resolveVisionPhaseCompletion,
8+
type VisionCaptionOutcome,
9+
} from "./contentExtractor.js";
610

711
const { generateContentMock } = vi.hoisted(() => ({
812
generateContentMock: vi.fn(),
@@ -151,9 +155,20 @@ describe("captionImagesWithGemini — OpenRouter provider", () => {
151155
);
152156

153157
const warnings: string[] = [];
154-
const captions = await captionImagesWithGemini(dir, () => {}, warnings);
158+
let outcome: VisionCaptionOutcome | undefined;
159+
const captions = await captionImagesWithGemini(dir, () => {}, warnings, {
160+
onOutcome: (value) => {
161+
outcome = value;
162+
},
163+
});
155164

156165
expect(captions).toEqual({ "success.png": "Successful image." });
166+
expect(outcome).toEqual({ timedOutRequests: 1, budgetExhausted: false });
167+
if (!outcome) throw new Error("Expected vision caption outcome");
168+
expect(resolveVisionPhaseCompletion(outcome, 10_000)).toEqual({
169+
status: "degraded",
170+
reason: "request-timeout",
171+
});
157172
});
158173

159174
it("does not call a configured provider when vision is skipped", async () => {

‎packages/cli/src/capture/contentExtractor.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,30 @@ import type { DesignTokens } from "./types.js";
1717

1818
const DEFAULT_VISION_REQUEST_TIMEOUT_MS = 30_000;
1919

20+
export interface VisionCaptionOutcome {
21+
timedOutRequests: number;
22+
budgetExhausted: boolean;
23+
}
24+
2025
interface VisionCaptionOptions {
2126
skipVision?: boolean;
2227
remainingMs?: () => number;
28+
onOutcome?: (outcome: VisionCaptionOutcome) => void;
29+
}
30+
31+
export function resolveVisionPhaseCompletion(
32+
outcome: VisionCaptionOutcome,
33+
remainingMs: number,
34+
):
35+
| { status: "completed" }
36+
| { status: "degraded"; reason: "budget-exhausted" | "request-timeout" } {
37+
if (outcome.budgetExhausted || remainingMs <= 0) {
38+
return { status: "degraded", reason: "budget-exhausted" };
39+
}
40+
if (outcome.timedOutRequests > 0) {
41+
return { status: "degraded", reason: "request-timeout" };
42+
}
43+
return { status: "completed" };
2344
}
2445

2546
class VisionRequestTimeoutError extends Error {
@@ -214,10 +235,21 @@ export async function captionImagesWithGemini(
214235
options: VisionCaptionOptions = {},
215236
): Promise<Record<string, string>> {
216237
const geminiCaptions: Record<string, string> = {};
217-
if (options.skipVision) return geminiCaptions;
238+
let timedOutCount = 0;
239+
let budgetExhausted = false;
240+
const reportOutcome = (): void => {
241+
options.onOutcome?.({ timedOutRequests: timedOutCount, budgetExhausted });
242+
};
243+
if (options.skipVision) {
244+
reportOutcome();
245+
return geminiCaptions;
246+
}
218247
const openRouterKey = process.env.OPENROUTER_API_KEY;
219248
const geminiKey = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
220-
if (!openRouterKey && !geminiKey) return geminiCaptions;
249+
if (!openRouterKey && !geminiKey) {
250+
reportOutcome();
251+
return geminiCaptions;
252+
}
221253

222254
// OpenRouter takes priority when both keys are set — it's the explicit opt-in
223255
// for users without Google access. Both providers satisfy the same
@@ -321,7 +353,6 @@ export async function captionImagesWithGemini(
321353
// on Promise.allSettled so a rate-limited image degrades to "" rather than
322354
// failing the batch.
323355
const BATCH_SIZE = 20;
324-
let timedOutCount = 0;
325356
const collectCaptionResults = (
326357
results: PromiseSettledResult<{ file: string; caption: string }>[],
327358
): number => {
@@ -340,7 +371,10 @@ export async function captionImagesWithGemini(
340371
};
341372
for (let i = 0; i < imageFiles.length; i += BATCH_SIZE) {
342373
const remainingMs = options.remainingMs?.() ?? Number.POSITIVE_INFINITY;
343-
if (remainingMs <= 0) break;
374+
if (remainingMs <= 0) {
375+
budgetExhausted = true;
376+
break;
377+
}
344378
const timeoutMs = Math.max(1, Math.min(requestTimeoutMs, remainingMs));
345379
const batch = imageFiles.slice(i, i + BATCH_SIZE);
346380
const results = await Promise.allSettled(
@@ -404,6 +438,7 @@ export async function captionImagesWithGemini(
404438
`Skipped ${svgFiles.length} SVG caption(s): sharp could not load (${(err as Error).message}). ` +
405439
`Reinstall with optional dependencies enabled (e.g. \`npm i sharp\`) to caption SVG assets.`,
406440
);
441+
reportOutcome();
407442
return geminiCaptions;
408443
}
409444
progress("design", `Rasterizing + captioning ${svgFiles.length} SVGs via vision API...`);
@@ -412,7 +447,10 @@ export async function captionImagesWithGemini(
412447
let svgsSkipped = 0;
413448
for (let i = 0; i < svgFiles.length; i += SVG_BATCH) {
414449
const remainingMs = options.remainingMs?.() ?? Number.POSITIVE_INFINITY;
415-
if (remainingMs <= 0) break;
450+
if (remainingMs <= 0) {
451+
budgetExhausted = true;
452+
break;
453+
}
416454
const timeoutMs = Math.max(1, Math.min(requestTimeoutMs, remainingMs));
417455
const batch = svgFiles.slice(i, i + SVG_BATCH);
418456
const results = await Promise.allSettled(
@@ -489,6 +527,7 @@ export async function captionImagesWithGemini(
489527
warnings.push(`${providerName} captioning failed: ${err}`);
490528
}
491529

530+
reportOutcome();
492531
return geminiCaptions;
493532
}
494533

‎packages/cli/src/capture/index.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import {
3737
extractVisibleText,
3838
captionImagesWithGemini,
3939
generateAssetDescriptions,
40+
resolveVisionPhaseCompletion,
4041
} from "./contentExtractor.js";
42+
import type { VisionCaptionOutcome } from "./contentExtractor.js";
4143
import { loadEnvFile, generateProjectScaffold } from "./scaffolding.js";
4244
import type { CaptureOptions, CapturePhase, CapturePhaseProgress, CaptureResult } from "./types.js";
4345

@@ -343,10 +345,11 @@ export async function captureWebsite(
343345
if (discoveredLotties.length > 0 && remainingMs() > 0) {
344346
const lottieDir = join(outputDir, "assets", "lottie");
345347
mkdirSync(lottieDir, { recursive: true });
346-
const savedCount = await saveLottieAnimations(discoveredLotties, lottieDir);
348+
const lottieBudget = { remainingMs };
349+
const savedCount = await saveLottieAnimations(discoveredLotties, lottieDir, lottieBudget);
347350
// Generate manifest + preview thumbnails so the agent can SEE what each animation is
348351
if (savedCount > 0 && remainingMs() > 0) {
349-
await renderLottiePreviews(chromeBrowser, lottieDir, outputDir);
352+
await renderLottiePreviews(chromeBrowser, lottieDir, outputDir, lottieBudget);
350353
progress("lottie", `${savedCount} Lottie animation(s) saved`);
351354
}
352355
}
@@ -494,6 +497,7 @@ export async function captureWebsite(
494497
networkVideoUrls: discoveredVideoUrls, // Layer 1 (live Set, read after sampling)
495498
sampleMs: Math.min(12000, videoBudgetMs), // Layer 2: poll DOM within the shared budget
496499
downloadBudgetMs: videoBudgetMs,
500+
remainingMs,
497501
});
498502
}
499503
} catch {
@@ -670,13 +674,21 @@ export async function captureWebsite(
670674
phase("vision", "degraded", "budget-exhausted");
671675
} else {
672676
phase("vision", "started");
677+
let visionOutcome: VisionCaptionOutcome = {
678+
timedOutRequests: 0,
679+
budgetExhausted: false,
680+
};
673681
geminiCaptions = await captionImagesWithGemini(outputDir, progress, warnings, {
674682
remainingMs,
683+
onOutcome: (outcome) => {
684+
visionOutcome = outcome;
685+
},
675686
});
687+
const completion = resolveVisionPhaseCompletion(visionOutcome, remainingMs());
676688
phase(
677689
"vision",
678-
remainingMs() > 0 ? "completed" : "degraded",
679-
remainingMs() > 0 ? undefined : "budget-exhausted",
690+
completion.status,
691+
completion.status === "degraded" ? completion.reason : undefined,
680692
);
681693
}
682694

‎packages/cli/src/capture/indexBudget.test.ts‎

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,127 @@
1-
import { readFileSync } from "node:fs";
2-
import { describe, expect, it } from "vitest";
3-
import { remainingVideoDownloadTimeoutMs } from "./mediaCapture.js";
1+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import type { Browser, Page } from "puppeteer-core";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import {
7+
captureVideoManifest,
8+
remainingVideoDownloadTimeoutMs,
9+
renderLottiePreviews,
10+
saveLottieAnimations,
11+
} from "./mediaCapture.js";
412

5-
const source = readFileSync(new URL("./mediaCapture.ts", import.meta.url), "utf-8");
13+
const tempDirs: string[] = [];
14+
15+
function tempDir(): string {
16+
const dir = mkdtempSync(join(tmpdir(), "hf-media-budget-"));
17+
tempDirs.push(dir);
18+
return dir;
19+
}
20+
21+
afterEach(() => {
22+
vi.useRealTimers();
23+
vi.unstubAllGlobals();
24+
for (const dir of tempDirs) rmSync(dir, { recursive: true, force: true });
25+
tempDirs.length = 0;
26+
});
27+
28+
describe("Lottie capture budget", () => {
29+
it("does not start another Lottie fetch after the live budget expires", async () => {
30+
const dir = tempDir();
31+
let remainingMs = 10_000;
32+
const fetchMock = vi.fn(async () => {
33+
remainingMs = 0;
34+
return new Response(JSON.stringify({ w: 100, h: 100, layers: [] }), { status: 200 });
35+
});
36+
vi.stubGlobal("fetch", fetchMock);
37+
38+
const saved = await saveLottieAnimations(
39+
[{ url: "https://one.example/a.json" }, { url: "https://two.example/b.json" }],
40+
dir,
41+
{ remainingMs: () => remainingMs },
42+
);
43+
44+
expect(saved).toBe(1);
45+
expect(fetchMock).toHaveBeenCalledTimes(1);
46+
});
47+
48+
it("does not open another Lottie preview page after the live budget expires", async () => {
49+
const dir = tempDir();
50+
const lottieDir = join(dir, "assets", "lottie");
51+
mkdirSync(join(dir, "extracted"), { recursive: true });
52+
mkdirSync(lottieDir, { recursive: true });
53+
const lottie = JSON.stringify({ w: 100, h: 100, fr: 30, ip: 0, op: 30, layers: [] });
54+
writeFileSync(join(lottieDir, "a.json"), lottie);
55+
writeFileSync(join(lottieDir, "b.json"), lottie);
56+
57+
let remainingMs = 10_000;
58+
const previewPage = {
59+
setViewport: vi.fn(async () => undefined),
60+
setContent: vi.fn(async () => undefined),
61+
evaluate: vi.fn(async () => undefined),
62+
waitForFunction: vi.fn(async () => undefined),
63+
screenshot: vi.fn(async () => undefined),
64+
close: vi.fn(async () => undefined),
65+
};
66+
const newPage = vi.fn(async () => {
67+
remainingMs = 0;
68+
return previewPage;
69+
});
70+
const browser = { newPage } as unknown as Browser;
71+
72+
await renderLottiePreviews(browser, lottieDir, dir, { remainingMs: () => remainingMs });
73+
74+
expect(newPage).toHaveBeenCalledTimes(1);
75+
expect(previewPage.setViewport).not.toHaveBeenCalled();
76+
expect(previewPage.screenshot).not.toHaveBeenCalled();
77+
});
78+
});
79+
80+
describe("video capture live budget", () => {
81+
it("does not preview or download when the budget expires during DOM sampling", async () => {
82+
vi.useFakeTimers();
83+
vi.setSystemTime(0);
84+
const dir = tempDir();
85+
mkdirSync(join(dir, "extracted"), { recursive: true });
86+
const descriptor = {
87+
src: "https://video.example/hero.mp4",
88+
filename: "hero.mp4",
89+
width: 640,
90+
height: 360,
91+
sourceWidth: 640,
92+
sourceHeight: 360,
93+
top: 0,
94+
left: 0,
95+
heading: "Hero",
96+
caption: "Demo",
97+
ariaLabel: "",
98+
};
99+
const screenshot = vi.fn(async () => Buffer.from("preview"));
100+
const evaluate = vi.fn(async (expression: unknown) =>
101+
typeof expression === "function" ? { x: 0, y: 0, width: 640, height: 360 } : [descriptor],
102+
);
103+
const page = { evaluate, screenshot } as unknown as Page;
104+
const fetchMock = vi.fn(
105+
async () =>
106+
new Response(Buffer.alloc(2048), {
107+
status: 200,
108+
headers: { "content-type": "video/mp4" },
109+
}),
110+
);
111+
vi.stubGlobal("fetch", fetchMock);
112+
113+
const capture = captureVideoManifest(page, dir, () => {}, {
114+
sampleMs: 10_000,
115+
downloadBudgetMs: 10_000,
116+
remainingMs: () => Math.max(0, 1_000 - Date.now()),
117+
});
118+
await vi.runAllTimersAsync();
119+
await capture;
120+
121+
expect(screenshot).not.toHaveBeenCalled();
122+
expect(fetchMock).not.toHaveBeenCalled();
123+
});
124+
});
6125

7126
describe("remainingVideoDownloadTimeoutMs", () => {
8127
it("caps a video request to the remaining capture budget", () => {
@@ -16,10 +135,4 @@ describe("remainingVideoDownloadTimeoutMs", () => {
16135
it("retains the existing per-request ceiling when more budget remains", () => {
17136
expect(remainingVideoDownloadTimeoutMs(1_000, 300_000, 2_000)).toBe(120_000);
18137
});
19-
20-
it("checks the aggregate budget before starting each preview/download iteration", () => {
21-
expect(source).toContain(
22-
"if (remainingVideoDownloadTimeoutMs(dlStart, downloadBudgetMs) <= 0) break;",
23-
);
24-
});
25138
});

0 commit comments

Comments
 (0)