Skip to content

The VS Code of video editing, built for agents.

TypeScript

Update build configuration and resource staging for runtime

konstantin-paulusd9d5f3a19cfb
diff --git a/apps/desktop/.gitignore b/apps/desktop/.gitignore
index 0410c18b..ce8f2e2f 100644
--- a/apps/desktop/.gitignore
+++ b/apps/desktop/.gitignore
@@ -2,4 +2,5 @@ dist
web
out
cli
+runtime
docs
diff --git a/apps/desktop/forge.config.ts b/apps/desktop/forge.config.ts
index c5033721..924a5049 100644
--- a/apps/desktop/forge.config.ts
+++ b/apps/desktop/forge.config.ts
@@ -27,9 +27,9 @@ const config: ForgeConfig = {
!path.startsWith('/dist/') &&
path !== '/web' &&
!path.startsWith('/web/'),
- // Staged by scripts/stage-{cli,docs}.mjs; end up at
- // Contents/Resources/{cli,docs}.
- extraResource: ['./cli', './docs'],
+ // Staged by scripts/stage-{cli,runtime,docs}.mjs; end up at
+ // Contents/Resources/{cli,runtime,docs}.
+ extraResource: ['./cli', './runtime', './docs'],
osxSign: process.env.SKIP_SIGN ? undefined : {},
osxNotarize:
process.env.APPLE_ID && process.env.APPLE_PASSWORD && process.env.APPLE_TEAM_ID
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 9452ceee..d106c4eb 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -15,12 +15,13 @@
"build": "npm run check && npm run build:main && npm run build:preload && npm run build:agent-host && npm run build:native",
"build:web": "npm --prefix ../web run build:desktop",
"stage:cli": "npm --prefix ../cli run build && node scripts/stage-cli.mjs",
+ "stage:runtime": "node scripts/stage-runtime.mjs",
"stage:docs": "node scripts/stage-docs.mjs",
"check": "tsc --noEmit",
"dev": "npm run build && electron-forge start",
- "package": "npm run build && npm run build:web && npm run stage:cli && npm run stage:docs && electron-forge package",
- "make": "npm run build && npm run build:web && npm run stage:cli && npm run stage:docs && electron-forge make",
- "publish": "npm run build && npm run build:web && npm run stage:cli && npm run stage:docs && electron-forge publish",
+ "package": "npm run build && npm run build:web && npm run stage:cli && npm run stage:runtime && npm run stage:docs && electron-forge package",
+ "make": "npm run build && npm run build:web && npm run stage:cli && npm run stage:runtime && npm run stage:docs && electron-forge make",
+ "publish": "npm run build && npm run build:web && npm run stage:cli && npm run stage:runtime && npm run stage:docs && electron-forge publish",
"make:icns": "sh scripts/make-icns.sh",
"test": "vitest run",
"build:agent-host": "esbuild ../../packages/agent-chat/src/bin.ts --bundle --platform=node --format=esm --target=node20 --external:electron '--external:@anthropic-ai/claude-agent-sdk-*' --banner:js=\"import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);\" --outfile=dist/agent-host.mjs"
diff --git a/apps/desktop/scripts/stage-runtime.mjs b/apps/desktop/scripts/stage-runtime.mjs
new file mode 100644
index 00000000..04723b9f
--- /dev/null
+++ b/apps/desktop/scripts/stage-runtime.mjs
@@ -0,0 +1,87 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Stages the main process's external dependencies into apps/desktop/runtime
+// so electron-forge can ship them as an app resource (Contents/Resources/
+// runtime). A packaged app has no node_modules of its own: everything else
+// is bundled into dist/main.js, but esbuild (native binary) and the babel
+// toolchain stay external and are loaded from here at run time (see `load`
+// in src/projects.ts). The staged layout:
+// runtime/package.json the externals, pinned to the workspace's versions
+// runtime/node_modules a fresh `npm install --omit=dev` of them
+//
+// The list of externals is read from the build:main script so that it cannot
+// drift from what the bundle actually leaves unresolved.
+
+import { execFileSync } from "node:child_process";
+import { createRequire } from "node:module";
+import { existsSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, closeSync, rmSync, writeFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const desktopDir = join(dirname(fileURLToPath(import.meta.url)), "..");
+const stageDir = join(desktopDir, "runtime");
+const require = createRequire(join(desktopDir, "package.json"));
+
+const pkg = JSON.parse(readFileSync(join(desktopDir, "package.json"), "utf8"));
+const externals = [...pkg.scripts["build:main"].matchAll(/--external:(\S+)/g)]
+ .map((match) => match[1])
+ // Electron is provided by the host binary, not installed.
+ .filter((name) => name !== "electron");
+
+// Pin what the workspace has installed (and was tested against), not the
+// ranges in package.json.
+const dependencies = Object.fromEntries(
+ externals.map((name) => [name, require(`${name}/package.json`).version]),
+);
+
+rmSync(stageDir, { recursive: true, force: true });
+mkdirSync(stageDir, { recursive: true });
+
+writeFileSync(
+ join(stageDir, "package.json"),
+ JSON.stringify({ name: "desktop-runtime", private: true, dependencies }, null, 2),
+);
+
+execFileSync("npm", ["install", "--omit=dev", "--no-audit", "--no-fund", "--no-package-lock"], {
+ cwd: stageDir,
+ stdio: "inherit",
+});
+
+// Mach-O files inside Resources are not reached by the app-bundle signing
+// pass, and notarization rejects unsigned executables; sign them here.
+// esbuild's postinstall hard-links the native binary over its bin/esbuild
+// shim, so the same executable can sit in two places.
+const MACHO_MAGIC = new Set([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe, 0xcafebabe]);
+function isMachO(path) {
+ const fd = openSync(path, "r");
+ try {
+ const header = Buffer.alloc(4);
+ return readSync(fd, header, 0, 4, 0) === 4 && MACHO_MAGIC.has(header.readUInt32BE(0));
+ } finally {
+ closeSync(fd);
+ }
+}
+
+const esbuildDir = join(stageDir, "node_modules", "@esbuild");
+const binaries = [
+ join(stageDir, "node_modules", "esbuild", "bin", "esbuild"),
+ ...(existsSync(esbuildDir) ? readdirSync(esbuildDir).map((name) => join(esbuildDir, name, "bin", "esbuild")) : []),
+].filter((path) => existsSync(path) && isMachO(path));
+
+if (process.platform === "darwin" && !process.env.SKIP_SIGN) {
+ const identities = execFileSync("security", ["find-identity", "-v", "-p", "codesigning"], { encoding: "utf8" });
+ const identity = identities.match(/"(Developer ID Application: [^"]+)"/)?.[1];
+ if (identity) {
+ for (const bin of binaries) {
+ execFileSync("codesign", ["--force", "--options", "runtime", "--timestamp", "--sign", identity, bin], {
+ stdio: "inherit",
+ });
+ }
+ } else {
+ console.warn("stage-runtime: no Developer ID identity found, leaving esbuild binaries unsigned");
+ }
+}
+
+console.log(`stage-runtime: staged ${externals.join(", ")} at ${stageDir}`);
diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.ts
index 1aa279eb..8235238d 100644
--- a/apps/desktop/src/projects.ts
+++ b/apps/desktop/src/projects.ts
@@ -49,13 +49,13 @@ const BUILD_OPTIONS: BuildOptions = {
// esbuild and babel are kept external to the main bundle (esbuild ships a
// native binary). In development they resolve from the workspace; a packaged
-// app has no node_modules of its own, so they load from the CLI's staged
-// runtime at Contents/Resources/cli/node_modules (see scripts/stage-cli.mjs).
+// app has no node_modules of its own, so they load from the staged runtime at
+// Contents/Resources/runtime/node_modules (see scripts/stage-runtime.mjs).
let stagedRequire: NodeJS.Require | undefined;
function load<T>(name: string): T {
if (!app.isPackaged) return require(name) as T;
- stagedRequire ??= createRequire(join(process.resourcesPath, "cli", "package.json"));
+ stagedRequire ??= createRequire(join(process.resourcesPath, "runtime", "package.json"));
return stagedRequire(name) as T;
}
diff --git a/apps/web/src/components/dashboard/home-view.tsx b/apps/web/src/components/dashboard/home-view.tsx
index 8256e5c4..aea9019c 100644
--- a/apps/web/src/components/dashboard/home-view.tsx
+++ b/apps/web/src/components/dashboard/home-view.tsx
@@ -86,7 +86,6 @@ const MENU_PROJECTS = 8;
const PROMPT_EXAMPLES = [
"Turn this footage into a polished YouTube video. Add readable captions and an attention-grabbing graphic in the opening to give viewers a strong visual hook.",
"Can you pull the best 30-second moment from https://youtu.be/MtQ0qxyf-Ds and make a vertical version for social?",
- "In three bullets, explain what starts the conflict. Include timestamps. https://youtu.be/aqz-KE-bpKQ",
"Name three recurring locations and give one visual cue that distinguishes each. https://youtu.be/dQw4w9WgXcQ",
];
diff --git a/apps/web/src/lib/trpc.ts b/apps/web/src/lib/trpc.ts
index 19721d17..efb662a9 100644
--- a/apps/web/src/lib/trpc.ts
+++ b/apps/web/src/lib/trpc.ts
@@ -9,68 +9,6 @@ import { showUpgradeDialog } from "@/components/upgrade-dialog";
import type { AppRouter } from "@diffusionstudio/api-contract";
-/**
- * The procedures that reach a model. What each was called with is the first
- * thing wanted when a generation comes back wrong, so every call writes one
- * line before it goes out and one when it lands — in the app console, which
- * the desktop app mirrors into its log buffer (`dapi logs`).
- */
-const MODEL_PROCEDURES = new Set([
- "generateImage",
- "generateVideo",
- "generateSound",
- "textToSpeech",
- "removeBackground",
- "upscaleImage",
- "upscaleVideo",
- "addAudioToVideo",
- "transcribe",
- "analyze",
-]);
-
-/** How much of a single argument a log line carries: a prompt is worth reading whole, a data url is not. */
-const ARGUMENT_MAX = 500;
-/** How much of the whole argument object a log line carries, before the log buffer cuts it anyway. */
-const ARGUMENTS_MAX = 2000;
-
-/**
- * The arguments of a call, as one line. Strings are cut at `ARGUMENT_MAX`,
- * and a url keeps its path alone — the query carries the upload signature,
- * which is long and not worth writing down.
- */
-function describeArguments(input: unknown): string {
- const json = JSON.stringify(input, (_key, value) => {
- if (typeof value !== "string") return value;
- const text = /^https?:\/\//.test(value) ? value.split("?")[0]! : value;
- return text.length > ARGUMENT_MAX ? `${text.slice(0, ARGUMENT_MAX)}…` : text;
- });
- if (json === undefined) return "(no arguments)";
- return json.length > ARGUMENTS_MAX ? `${json.slice(0, ARGUMENTS_MAX)}…` : json;
-}
-
-/** Logs what every model call was asked for, and how long it took to answer. */
-const modelCallLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
- observable((observer) => {
- const logged = MODEL_PROCEDURES.has(op.path);
- const startedAt = performance.now();
- const elapsed = () => `${Math.round(performance.now() - startedAt)}ms`;
-
- if (logged) console.log(`[model] ${op.path} ${describeArguments(op.input)}`);
-
- const sub = next(op).subscribe({
- next: (value) => observer.next(value),
- error: (err) => {
- if (logged) console.error(`[model] ${op.path} failed after ${elapsed()}: ${err.message}`);
- observer.error(err);
- },
- complete: () => {
- if (logged) console.log(`[model] ${op.path} done in ${elapsed()}`);
- observer.complete();
- },
- });
- return () => sub.unsubscribe();
- });
-
const paymentRequiredLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
observable((observer) => {
const sub = next(op).subscribe({
@@ -88,7 +26,6 @@ const paymentRequiredLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
export const trpc = createTRPCClient<AppRouter>({
links: [
- modelCallLink,
paymentRequiredLink,
httpBatchLink({
url: `${import.meta.env.VITE_API_URL ?? ""}/api/trpc`,
diff --git a/apps/web/src/utils/gen-ai.ts b/apps/web/src/utils/gen-ai.ts
index 2828db97..2738fbbb 100644
--- a/apps/web/src/utils/gen-ai.ts
+++ b/apps/web/src/utils/gen-ai.ts
@@ -286,7 +286,6 @@ export class EditorGenAi extends GenAi {
});
try {
- console.log(`[gen-ai] generating ${spec.type} with ${spec.model}`);
const { name, results, generationId } = await this.requestGeneration(spec);
assert(results.length > 0, "No results returned from the model");
@@ -330,11 +329,9 @@ export class EditorGenAi extends GenAi {
const uploadId = crypto.randomUUID();
const audioFile = new File([result.data], `${uploadId}.ogg`, { type: "audio/ogg" });
- console.log(`[gen-ai] uploading scene audio for ${key} (${audioFile.size} bytes)`);
const fileRef = await uploadBlob(audioFile, uploadId);
assert(fileRef, "Failed to upload the scene audio for transcription");
- console.log(`[gen-ai] transcribing scene audio for ${key}`);
const { results: transcript } = await trpc.transcribe.mutate({ audio: fileRef });
assert(
transcript.length > 0 && transcript.some((segment) => segment.words.length > 0),
@@ -373,7 +370,6 @@ export class EditorGenAi extends GenAi {
track("generation_started", { mode: spec.type });
try {
- console.log(`[gen-ai] running ${spec.type} on ${asset.path}`);
const input = await this.uploadInput(asset.id);
const { url, generationId } = await this.requestTransform(spec.type, asset, input);
const stored = await this.store(url, assetName(partial), { key: partial.generation.key, id: generationId });