Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a headless session refuses media tools it cannot run

David Le7a40151eb32c
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 4acddd79..17540932 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -2386,6 +2386,25 @@ export const UI_TOOLS: ReadonlySet<string> = new Set([
"set_playing",
]);
+/** Tools that decode or rasterize media — frame grabs, audio scans, image
+ * decodes, the transcription mixdown. A page has those primitives and a
+ * headless process installs them; a process that installed neither refuses
+ * these with a typed error rather than a stray "document is not defined". */
+export const MEDIA_RUNTIME_TOOLS: ReadonlySet<string> = new Set([
+ "convert_media",
+ "watch_video",
+ "listen_audio",
+ "detect_silence",
+ "refine_speech_cuts",
+ "capture_frame",
+ "freeze_frame",
+ "create_sticker",
+ "subtitles_generate",
+ "captions_generate",
+ "subtitles_from_visuals",
+ "stock_add",
+]);
+
export async function runAiTool(
name: string,
input: Record<string, unknown>
diff --git a/site/src/cut/lib/headless/runtime.ts b/site/src/cut/lib/headless/runtime.ts
index bd546d2f..e5a0f274 100644
--- a/site/src/cut/lib/headless/runtime.ts
+++ b/site/src/cut/lib/headless/runtime.ts
@@ -21,17 +21,25 @@ export interface HeadlessRuntime {
}
let installed: Promise<HeadlessRuntime> | null = null;
+let current: HeadlessRuntime | null = null;
export function installHeadlessRuntime(): Promise<HeadlessRuntime> {
installed ??= (async () => {
// Raster first: the frame sink draws into canvases the seam hands out.
const raster = await installSkiaRaster();
const [media, fonts] = await Promise.all([installNodeMedia(), installHeadlessFonts()]);
- return { raster, media, fonts };
+ const rt = { raster, media, fonts };
+ current = rt;
+ return rt;
})();
return installed;
}
+/** What this process actually got, or null before the install finishes. Code
+ * that has to answer synchronously — a tool dispatch — reads it to tell a
+ * capability it has from one it has to refuse. */
+export const headlessRuntime = (): HeadlessRuntime | null => current;
+
/** One line for a startup log. */
export const describeRuntime = (r: HeadlessRuntime): string =>
`raster: ${r.raster ? "skia" : "unavailable"}, media: ${r.media ? "nodeav" : "unavailable"}, fonts: ${r.fonts}`;
diff --git a/site/src/cut/lib/pi/serverDeps.ts b/site/src/cut/lib/pi/serverDeps.ts
index 44fe050b..e6a88082 100644
--- a/site/src/cut/lib/pi/serverDeps.ts
+++ b/site/src/cut/lib/pi/serverDeps.ts
@@ -1,11 +1,12 @@
import { geminiModelRoles } from "@/lib/inference/gemini-models";
import { AI_SKILL_INDEX, AI_SKILLS } from "@/cut/server/ai/catalog";
import { buildAiContext } from "../aiContext";
-import { runAiTool, UI_TOOLS } from "../aiTools";
+import { MEDIA_RUNTIME_TOOLS, runAiTool, UI_TOOLS } from "../aiTools";
import { normalizeRef } from "../assetRef";
import { NO_CREDITS_MESSAGE } from "../generate";
import { hostedPost } from "../hosted";
import { bindHeadlessSession, type HeadlessSession } from "../headless/bind";
+import { headlessRuntime } from "../headless/runtime";
import { refsToParts } from "../refMedia";
import type { CutAgentDeps } from "./cutAgent";
import { currentDebris } from "./debris";
@@ -32,6 +33,15 @@ export function headlessDeps(session: HeadlessSession): CutAgentDeps {
noEditor: true,
note: "No editor page is attached to this session, so this tool had no effect. The project itself is unchanged — keep working from the editor state.",
};
+ // The worker installs canvas and decoders at startup and these run as
+ // written. A process that came up without them says so plainly.
+ if (MEDIA_RUNTIME_TOOLS.has(name)) {
+ const rt = headlessRuntime();
+ if (!rt?.raster || !rt.media)
+ throw new Error(
+ "This session has no media runtime, so it cannot decode or draw media. Work from the editor state and the transcript instead."
+ );
+ }
return runAiTool(name, args);
},
models: {