Open source CapCut alternative.
TypeScript
feat(site): command batches and import adoption in the worker [rebuild]
David Le98afc65b2b18
diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex cf74065c..711704ed 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -1,6 +1,7 @@"use client";import { captureRenderSnapshot, renderDoc, type ExportDoc, type RenderSnapshot } from "./renderSnapshot";+import { DELIVERY_DEFAULTS, EXPORT_PRESETS } from "./exportPresets";import { operationFailure, type OperationFailure } from "./operationFailure";import { projectOperation } from "./projectOperation";@@ -70,9 +71,7 @@ export interface ExportSettings {range?: ExportRange;}-/** The delivery every render that is not the user's own export uses — hover- * proxies, share cards, ladder masters, the phone's presets. */-export const DELIVERY_DEFAULTS = { codec: "h264", container: "mp4", audioCodec: "aac" } as const;+export { DELIVERY_DEFAULTS, EXPORT_PRESETS } from "./exportPresets";export const EXPORT_CODECS = [{ id: "h264", label: "H.264", detail: "plays everywhere" },@@ -98,31 +97,6 @@ export function exportExtension(settings: Pick<ExportSettings, "container">): streturn deliveryContainer(settings.container).ext;}-/** Presets pick a short-side target; `presetSettings` derives both dims from- * the project ratio. */-export const EXPORT_PRESETS = [- {- id: "tiktok",- label: "Best · 1080p",- detail: "H.264 · best quality",- shortSide: 1080,- settings: { fps: 30, crf: 19, preset: "medium", ...DELIVERY_DEFAULTS },- },- {- id: "fast",- label: "Quick share · 1080p",- detail: "smaller file, faster",- shortSide: 1080,- settings: { fps: 30, crf: 24, preset: "veryfast", ...DELIVERY_DEFAULTS },- },- {- id: "light",- label: "Draft · 720p",- detail: "fastest render",- shortSide: 720,- settings: { fps: 30, crf: 24, preset: "veryfast", ...DELIVERY_DEFAULTS },- },-] as const;/*** The axes the export dialog offers, each one independent of the others: howdiff --git a/site/src/cut/lib/exportPresets.ts b/site/src/cut/lib/exportPresets.tsnew file mode 100644index 00000000..29819648--- /dev/null+++ b/site/src/cut/lib/exportPresets.ts@@ -0,0 +1,42 @@+// The export sizes offered by name. Kept apart from the export client so a+// server route can list them without pulling the editor in.++/** The delivery every render that is not the user's own export uses — hover+ * proxies, share cards, ladder masters, the phone's presets. */+export const DELIVERY_DEFAULTS = { codec: "h264", container: "mp4", audioCodec: "aac" } as const;++/** Presets pick a short-side target; `presetSettings` derives both dims from+ * the project ratio. */+export const EXPORT_PRESETS = [+ {+ id: "tiktok",+ label: "Best · 1080p",+ detail: "H.264 · best quality",+ shortSide: 1080,+ settings: { fps: 30, crf: 19, preset: "medium", ...DELIVERY_DEFAULTS },+ },+ {+ id: "fast",+ label: "Quick share · 1080p",+ detail: "smaller file, faster",+ shortSide: 1080,+ settings: { fps: 30, crf: 24, preset: "veryfast", ...DELIVERY_DEFAULTS },+ },+ {+ id: "light",+ label: "Draft · 720p",+ detail: "fastest render",+ shortSide: 720,+ settings: { fps: 30, crf: 24, preset: "veryfast", ...DELIVERY_DEFAULTS },+ },+] as const;++/** The sizes a document export offers: `original` matches the footage on the+ * timeline; the rest are the fixed presets flipped to the project's ratio. */+export const DOC_EXPORT_PRESETS = ["original", ...EXPORT_PRESETS.map((p) => p.id)] as const;++export type DocExportPreset = (typeof DOC_EXPORT_PRESETS)[number];++export function isDocExportPreset(value: unknown): value is DocExportPreset {+ return typeof value === "string" && (DOC_EXPORT_PRESETS as readonly string[]).includes(value);+}diff --git a/site/src/cut/lib/headless/docExport.ts b/site/src/cut/lib/headless/docExport.tsindex 7c36a8c7..5bc575e0 100644--- a/site/src/cut/lib/headless/docExport.ts+++ b/site/src/cut/lib/headless/docExport.ts@@ -17,16 +17,8 @@ import { openCloudSnapshot, type CloudDocSnapshot } from "./docSession";// worker, prepare the same payload as the editor, and stage its overlay// pictures in the render's own scratch directory.-/** The sizes a doc-built export offers, named as the export dialog names- * them. `original` is derived from the footage on the timeline; the rest are- * the fixed presets flipped to the project's ratio. */-export const DOC_EXPORT_PRESETS = ["original", ...EXPORT_PRESETS.map((p) => p.id)] as const;--export type DocExportPreset = (typeof DOC_EXPORT_PRESETS)[number];--export function isDocExportPreset(value: unknown): value is DocExportPreset {- return typeof value === "string" && (DOC_EXPORT_PRESETS as readonly string[]).includes(value);-}+export { DOC_EXPORT_PRESETS, isDocExportPreset, type DocExportPreset } from "../exportPresets";+import type { DocExportPreset } from "../exportPresets";function settingsFor(preset: DocExportPreset, doc: ExportDoc): ExportSettings {const fixed = EXPORT_PRESETS.find((p) => p.id === preset);diff --git a/site/src/cut/lib/headless/docSession.ts b/site/src/cut/lib/headless/docSession.tsindex a7937729..983b2f51 100644--- a/site/src/cut/lib/headless/docSession.ts+++ b/site/src/cut/lib/headless/docSession.ts@@ -16,6 +16,8 @@ export interface HeadlessDocSession {projectId: string;/** The doc version the store's contents are built on. */version: string;+ /** The document as read, before any edit. */+ doc: ProjectDoc;}const projectPath = (s: HeadlessSession, projectId: string) =>@@ -80,12 +82,19 @@ export async function openCloudSnapshot(// account's shelf fonts come along the same way — a run reaches the cloud// shelf, so a font kept on a device shelf is the one case that falls back.await Promise.all([syncFontAssets(assets), syncLinkedLibrary()]);- return { projectId, version };+ return { projectId, version, doc };}/** Push the store's persistable slice back up on the session's version. A 409* means another writer moved the document past this session's base; the* caller decides whether to reopen and redo or surface the conflict. */+/** The versioned PUT lost: another writer saved a newer version first. */+export class ProjectConflictError extends Error {+ constructor() {+ super("The project changed under this session — another writer holds a newer version.");+ }+}+export async function pushCloudProject(s: HeadlessSession,session: HeadlessDocSession@@ -102,8 +111,7 @@ export async function pushCloudProject(},`save project ${session.projectId}`);- if (res.status === 409)- throw new Error("The project changed under this session — another writer holds a newer version.");+ if (res.status === 409) throw new ProjectConflictError();if (!res.ok) throw new Error(`Could not save project ${session.projectId} (${res.status}).`);const body = (await res.json()) as { version?: number };if (body.version !== undefined) session.version = String(body.version);diff --git a/site/src/cut/server/cloud/commands.ts b/site/src/cut/server/cloud/commands.tsnew file mode 100644index 00000000..d753ed6a--- /dev/null+++ b/site/src/cut/server/cloud/commands.ts@@ -0,0 +1,90 @@+// A batch of editor commands as a durable job. A host with its own+// conversation — the ChatGPT app — sends typed tool calls with no model of+// ours in the loop; the worker container opens the project document into the+// editor store, runs each command through the same executor the chat uses,+// pushes the document back, and records one undo checkpoint for the batch.+import type { Prisma } from "@/generated/prisma/client";+import { prisma } from "@/lib/prisma";+import { liveJobCheck } from "./limits";+import { queueLeasedJob } from "./lease";+import { getProject } from "./projects";+import { err } from "./util";+import { wakeRenderWorker } from "./wake";++export const MAX_COMMANDS_PER_BATCH = 40;++export interface CommandCall {+ name: string;+ input: Record<string, unknown>;+}++export interface CommandJobSpec {+ commands: CommandCall[];+ /** A read: the document is opened and inspected, never pushed. */+ readOnly?: boolean;+ /** The undo history's name for this batch. */+ label?: string;+}++export interface CommandOutcome {+ name: string;+ ok: boolean;+ output?: unknown;+ error?: string;+}++/** What a commands job records in CutRenderJob.result. */+export interface CommandJobResult {+ results: CommandOutcome[];+ /** The document changed and was saved. */+ changed: boolean;+ /** The project version after the save, when one happened. */+ docVersion: string | null;+}++export async function queueCommands(+ userId: string,+ projectId: string,+ spec: CommandJobSpec+): Promise<{ id: string } | Response> {+ if (!(await getProject(userId, projectId))) return err("Project not found.", 404);+ if (!Array.isArray(spec.commands) || spec.commands.length === 0) return err("At least one command is required.", 400);+ if (spec.commands.length > MAX_COMMANDS_PER_BATCH) return err(`At most ${MAX_COMMANDS_PER_BATCH} commands per batch.`, 400);+ const capped = await liveJobCheck(userId);+ if (capped) return capped;+ if (spec.readOnly) {+ // A read never saves, so it runs beside whatever holds the write lease.+ const row = await prisma.cutRenderJob.create({+ data: { userId, projectId, kind: "commands", spec: spec as unknown as Prisma.InputJsonValue },+ });+ wakeRenderWorker();+ return { id: row.id };+ }+ return queueLeasedJob(userId, projectId, "commands", spec as unknown as Prisma.InputJsonValue);+}++export type JobRow = NonNullable<Awaited<ReturnType<typeof prisma.cutRenderJob.findFirst>>>;++const HEARTBEAT_QUIET_MS = 30_000;++/** The row is waiting to be claimed, or its worker's heartbeat went quiet. */+export function needsWorker(row: Pick<JobRow, "state" | "claimedAt" | "updatedAt">): boolean {+ if (row.state === "queued") return true;+ return row.state === "running" && row.claimedAt !== null && Date.now() - row.updatedAt.getTime() > HEARTBEAT_QUIET_MS;+}++const TERMINAL = new Set(["done", "error", "canceled", "dismissed"]);++/** Poll a job row until it settles or the budget runs out. The row comes back+ * either way; the caller reads its state. */+export async function waitForJob(userId: string, jobId: string, budgetMs: number): Promise<JobRow | null> {+ const deadline = Date.now() + budgetMs;+ for (;;) {+ const row = await prisma.cutRenderJob.findFirst({ where: { id: jobId, userId } });+ if (!row) return null;+ if (TERMINAL.has(row.state)) return row;+ if (needsWorker(row)) wakeRenderWorker();+ if (Date.now() >= deadline) return row;+ await new Promise((r) => setTimeout(r, Math.min(1000, Math.max(50, deadline - Date.now()))));+ }+}diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.tsindex 382f419d..32fc2558 100644--- a/site/src/cut/server/cloud/jobs.ts+++ b/site/src/cut/server/cloud/jobs.ts@@ -4,6 +4,7 @@import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";import { cutLimitsFor, EXPORT_QUOTA_MARGIN, renderJobCheck } from "./limits";+import { liveLeaseHolder } from "./lease";import { wakeRenderWorker } from "./wake";import { queuePreview } from "./previewJobs";import { getProject } from "./projects";@@ -187,6 +188,72 @@ async function exportName(}}+/** Queue a render of the stored document at a named size. A refusal — a+ * missing project, the render or storage caps — comes back as its Response. */+export async function queueDocExport(+ userId: string,+ projectId: string,+ preset: string+): Promise<{ id: string; outName: string } | Response> {+ const project = await getProject(userId, projectId);+ if (!project) return err("Project not found.", 404);+ const capped = await renderJobCheck(userId);+ if (capped) return capped;+ const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);+ if (over) return over;+ const outName = await exportName(userId, projectId, project.name, undefined);+ const row = await prisma.cutRenderJob.create({+ data: {+ userId,+ projectId,+ kind: "export",+ spec: { fromDoc: { preset, snapshot: { doc: project.doc, version: String(project.version) } } } as unknown as Prisma.InputJsonValue,+ outName,+ },+ });+ wakeRenderWorker();+ return { id: row.id, outName };+}++/** Queue a URL import into a project. `adopt` asks the worker to register+ * what lands as project assets itself — for a caller with no editor of its+ * own to adopt them (the ChatGPT app); `name` is what that asset is called.+ * A refusal comes back as its Response. */+export async function queueImportUrl(+ userId: string,+ projectId: string,+ opts: { url: string; audio?: boolean; key?: string; adopt?: { name?: string } }+): Promise<{ id: string } | Response> {+ if (!(await getProject(userId, projectId))) return err("Project not found.", 404);+ const open = await importJobFor(userId, opts.key);+ if (open) return { id: open.id };+ const capped = await renderJobCheck(userId);+ if (capped) return capped;+ if (opts.adopt && (await liveLeaseHolder(userId, projectId)))+ return err("An edit is still running for this project. Wait for it to finish, then import.", 409);+ // What lands counts against storage, so an account with none left gets+ // no download, and the worker downloads no more than what is left.+ const over = await quotaCheck(userId, 0);+ if (over) return over;+ const maxBytes = await importByteCeiling(userId);+ const row = await prisma.cutRenderJob.create({+ data: {+ userId,+ projectId,+ kind: "import_url",+ spec: {+ url: opts.url,+ maxBytes,+ ...(opts.audio === true ? { audio: true } : {}),+ ...(opts.key ? { key: opts.key } : {}),+ ...(opts.adopt ? { adopt: opts.adopt } : {}),+ } as unknown as Prisma.InputJsonValue,+ },+ });+ wakeRenderWorker();+ return { id: row.id };+}+export const jobsCloud = {/** Presign the overlay uploads for an export — stamped title and effect* frames, and the behind-speaker mask clip. Transient worker inputs: no@@ -371,27 +438,11 @@ export const jobsCloud = {async exportFromDoc(userId: string, projectId: string, req: Request) {try {const body = (await req.json().catch(() => ({}))) as { preset?: string };- const project = await getProject(userId, projectId);- if (!project) return err("Project not found.", 404);- const capped = await renderJobCheck(userId);- if (capped) return capped;- const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);- if (over) return over;// The worker owns the size list (it builds the spec), and falls back to// the source-matched original for anything it does not know.- const preset = typeof body.preset === "string" ? body.preset : "original";- const outName = await exportName(userId, projectId, project.name, undefined);- const row = await prisma.cutRenderJob.create({- data: {- userId,- projectId,- kind: "export",- spec: { fromDoc: { preset, snapshot: { doc: project.doc, version: String(project.version) } } } as unknown as Prisma.InputJsonValue,- outName,- },- });- wakeRenderWorker();- return Response.json({ id: row.id, outName });+ const queued = await queueDocExport(userId, projectId, typeof body.preset === "string" ? body.preset : "original");+ if (queued instanceof Response) return queued;+ return Response.json(queued);} catch (e) {return caught(e, "Export failed to start.");}@@ -659,31 +710,9 @@ export const jobsCloud = {};if (!url) return err("No URL provided.", 400);if (!validImportKey(key)) return err("Bad import key.", 400);- if (!(await getProject(userId, projectId))) return err("Project not found.", 404);- const open = await importJobFor(userId, key);- if (open) return Response.json({ jobId: open.id });- const capped = await renderJobCheck(userId);- if (capped) return capped;- // What lands counts against storage, so an account with none left gets- // no download, and the worker downloads no more than what is left.- const over = await quotaCheck(userId, 0);- if (over) return over;- const maxBytes = await importByteCeiling(userId);- const row = await prisma.cutRenderJob.create({- data: {- userId,- projectId,- kind: "import_url",- spec: {- url,- maxBytes,- ...(audio === true ? { audio: true } : {}),- ...(key ? { key } : {}),- } as unknown as Prisma.InputJsonValue,- },- });- wakeRenderWorker();- return Response.json({ jobId: row.id });+ const queued = await queueImportUrl(userId, projectId, { url, audio: audio === true, key });+ if (queued instanceof Response) return queued;+ return Response.json({ jobId: queued.id });} catch (e) {return caught(e, "Could not import that URL.");}diff --git a/site/src/cut/worker/commandJob.ts b/site/src/cut/worker/commandJob.tsnew file mode 100644index 00000000..b5ccc057--- /dev/null+++ b/site/src/cut/worker/commandJob.ts@@ -0,0 +1,144 @@+import { assetFromProjectFile } from "../lib/media";+import { bindHeadlessSession } from "../lib/headless/bind";+import { openCloudProject, ProjectConflictError, pushCloudProject, type HeadlessDocSession } from "../lib/headless/docSession";+import { headlessDeps, type HeadlessSession } from "../lib/pi/serverDeps";+import { serializeDoc, useEditor } from "../lib/store";+import type { CommandJobResult, CommandJobSpec, CommandOutcome } from "../server/cloud/commands";+import { recordCheckpoint } from "../server/cloud/history";+import type { RenderHandle } from "../server/exportPipeline";+import { prisma, type ClaimedJob } from "./db";+import { runnerSession } from "./session";++// A command batch, executed where the tab used to be: open the project+// document into the editor store, run each typed command through the chat's+// own executor, push the document back through the versioned PUT, and record+// the batch as one undo checkpoint. No model runs here — the caller decided+// what to do; this process does it.++/** Contact sheets, captured frames and listened audio ride in the result as+ * data URLs. Past this much, later ones are dropped and say so, so one batch+ * never writes a row the poll cannot read. */+const MAX_MEDIA_BYTES = 8 * 1024 * 1024;++/** How many times an adoption re-opens the document after losing the+ * versioned PUT to a parallel import. */+const ADOPT_ATTEMPTS = 5;++const snapshot = () => JSON.stringify(serializeDoc(useEditor.getState()));++/** Save the store's edits under the session's version and record the step.+ * The checkpoint holds the document as the PUT stored it — the server+ * normalizes what it receives, so the checkpoint reads the row back. */+async function saveStep(+ session: HeadlessSession,+ userId: string,+ doc: HeadlessDocSession,+ label: string+): Promise<string> {+ const before = { doc: doc.doc, version: Number(doc.version) };+ await pushCloudProject(session, doc);+ const stored = await prisma.cutProject.findFirst({+ where: { id: doc.projectId, userId },+ select: { doc: true, version: true },+ });+ if (!stored) throw new Error("The project is gone after the save.");+ await recordCheckpoint(prisma, {+ userId,+ projectId: doc.projectId,+ before,+ after: { doc: stored.doc as unknown as HeadlessDocSession["doc"], version: stored.version },+ label,+ });+ return String(stored.version);+}++/** Keep the batch's inline media (frames, sheets, listened audio) inside one+ * result budget. */+function boundMedia(results: CommandOutcome[]): void {+ let budget = MAX_MEDIA_BYTES;+ const take = (url: unknown): unknown => {+ if (typeof url !== "string" || !url.startsWith("data:")) return url;+ if (url.length <= budget) {+ budget -= url.length;+ return url;+ }+ return "[dropped: the batch's inline media exceeds the result budget — ask for this in its own call]";+ };+ for (const r of results) {+ const out = r.output as { image?: unknown; images?: unknown[]; audio?: unknown } | null;+ if (!out || typeof out !== "object") continue;+ if ("image" in out) out.image = take(out.image);+ if (Array.isArray(out.images)) out.images = out.images.map(take);+ if ("audio" in out) out.audio = take(out.audio);+ }+}++export async function runCommandJob(+ job: ClaimedJob,+ handle: RenderHandle,+ isCanceled: () => boolean+): Promise<CommandJobResult> {+ const spec = job.spec as CommandJobSpec;+ if (!job.projectId) throw new Error("Command job carries no project.");+ if (!Array.isArray(spec?.commands) || spec.commands.length === 0)+ throw new Error("Command job carries no commands.");++ const session = runnerSession(job);+ bindHeadlessSession(session);+ const doc = await openCloudProject(session, job.projectId);+ const deps = headlessDeps(session);+ const before = snapshot();+ const results: CommandOutcome[] = [];+ for (const [i, call] of spec.commands.entries()) {+ if (isCanceled()) throw new Error("Canceled.");+ handle.progress = i / spec.commands.length;+ try {+ const output = await deps.execTool(call.name, call.input ?? {});+ results.push({ name: call.name, ok: true, output: output ?? null });+ } catch (err) {+ // The batch stops at the first failure: later commands assumed this+ // one landed. The caller sees exactly how far it got.+ results.push({ name: call.name, ok: false, error: err instanceof Error ? err.message : String(err) });+ break;+ }+ }+ boundMedia(results);+ const changed = snapshot() !== before;+ let docVersion: string | null = null;+ if (changed && !spec.readOnly) {+ const label = spec.label?.trim() || results.filter((r) => r.ok).map((r) => r.name).join(", ") || "Edit";+ docVersion = await saveStep(session, job.userId, doc, label);+ }+ handle.progress = 1;+ return { results, changed: changed && !spec.readOnly, docVersion };+}++/** Register files an import landed as project assets and save the document:+ * the adoption a tab does after its own import, done here for a caller with+ * no tab. Returns the assets as the catalog's import reports them. */+export async function adoptImportedFiles(+ job: ClaimedJob,+ files: { fileName: string; title: string }[],+ name?: string+): Promise<{ assets: { assetId: string; name: string; kind: string; duration: number }[]; docVersion: string }> {+ if (!job.projectId) throw new Error("Import job carries no project.");+ const session = runnerSession(job);+ bindHeadlessSession(session);+ // Parallel imports into one project each add their own assets; the loser+ // of the versioned PUT re-opens the newer document and adds them again.+ for (let attempt = 1; ; attempt++) {+ const doc = await openCloudProject(session, job.projectId);+ const assets: { assetId: string; name: string; kind: string; duration: number }[] = [];+ for (const f of files) {+ const asset = await assetFromProjectFile(job.projectId, f.fileName, (files.length === 1 && name) || f.title || f.fileName);+ useEditor.getState().addAsset(asset);+ assets.push({ assetId: asset.id, name: asset.name, kind: asset.type, duration: Math.round(asset.duration * 100) / 100 });+ }+ try {+ const docVersion = await saveStep(session, job.userId, doc, `Imported ${assets.map((a) => a.name).join(", ")}`);+ return { assets, docVersion };+ } catch (err) {+ if (!(err instanceof ProjectConflictError) || attempt >= ADOPT_ATTEMPTS) throw err;+ }+ }+}diff --git a/site/src/cut/worker/importUrlJob.ts b/site/src/cut/worker/importUrlJob.tsindex 71e5cb95..7bae107f 100644--- a/site/src/cut/worker/importUrlJob.ts+++ b/site/src/cut/worker/importUrlJob.ts@@ -8,6 +8,7 @@ import type { LibraryAsset } from "../server/library";import { probeDuration } from "../server/frames";import { download } from "../server/urlDownload";import { videoDimensions } from "../server/util";+import { adoptImportedFiles } from "./commandJob";import { prisma, registerObject, unregisterObjects, type ClaimedJob } from "./db";import { deleteObjects, libraryKey, mediaKey, mimeFor, uploadFile } from "./r2";@@ -16,6 +17,9 @@ import { deleteObjects, libraryKey, mediaKey, mimeFor, uploadFile } from "./r2";export interface ImportUrlResult {files: { fileName: string; title: string }[];text?: string;+ /** Set when the job adopted the files into the project document itself. */+ assets?: { assetId: string; name: string; kind: string; duration: number }[];+ docVersion?: string;}/** What a library import_url job records: the assets, exactly as the engine's@@ -36,11 +40,14 @@ export async function runImportUrlJob(job: ClaimedJob,isCanceled: () => boolean): Promise<ImportUrlResult | LibraryImportResult> {- const { url, target, origin, audio, maxBytes } = (job.spec ?? {}) as {+ const { url, target, origin, audio, maxBytes, adopt } = (job.spec ?? {}) as {url?: string;target?: string;origin?: string;audio?: boolean;+ /** Register what lands as project assets here, for a caller with no+ * editor to do it (the ChatGPT app). */+ adopt?: { name?: string };/** What is left of the account's storage when the job was queued; the* route always sets it. Everything that came down is measured against it* before any byte is uploaded — the one place every rung and every@@ -156,6 +163,11 @@ export async function runImportUrlJob(assets.push(asset);}if (toLibrary) return { assets, ...(dl.text ? { text: dl.text } : {}) };+ if (adopt && files.length > 0) {+ // Landed in the document as well; a failure here cleans up like any other.+ const adopted = await adoptImportedFiles(job, files, adopt.name);+ return { files, ...adopted, ...(dl.text ? { text: dl.text } : {}) };+ }return { files, ...(dl.text ? { text: dl.text } : {}) };} catch (err) {if (libraryRows.length > 0)diff --git a/site/src/cut/worker/main.ts b/site/src/cut/worker/main.tsindex 7305c317..060a94bc 100644--- a/site/src/cut/worker/main.ts+++ b/site/src/cut/worker/main.ts@@ -6,11 +6,13 @@ import { runHlsJob } from "./hlsJob";import { runConvertJob } from "./convertJob";import { runImportUrlJob } from "./importUrlJob";import { runTurnJob } from "./turnJob";+import { runCommandJob } from "./commandJob";import { describeRuntime, installHeadlessRuntime } from "../lib/headless/runtime";import { deleteObjects } from "./r2";// The cloud render worker: a headless loop that claims CutRenderJob rows the-// hosted API queues (export, preview, import_url, convert), executes them with the+// hosted API queues (export, preview, import_url, convert, agent_turn,+// commands), executes them with the// same pipeline code the local engine runs, and writes progress and results// back to the row the client polls. Scale is horizontal: one job per process,// several processes — the atomic claim keeps them apart.@@ -51,6 +53,7 @@ const MAX_RUNTIME_MS: Record<string, number> = {import_url: 20 * 60_000,convert: 60 * 60_000,agent_turn: 20 * 60_000,+ commands: 20 * 60_000,};const DEFAULT_MAX_RUNTIME_MS = 60 * 60_000;@@ -74,7 +77,7 @@ const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));/** The kinds someone is watching happen: an export, a URL import, or a chat* turn has a progress surface on screen, while a hover proxy and a share card* are background polish nobody is waiting on. */-const WATCHED_KINDS = ["export", "import_url", "convert", "agent_turn"];+const WATCHED_KINDS = ["export", "import_url", "convert", "agent_turn", "commands"];/** Atomically claim the next queued job: the updateMany's state guard makes* exactly one worker win each row, so replicas never double-run a job.@@ -220,6 +223,12 @@ async function runJob(job: ClaimedJob): Promise<void> {where: { id: job.id, state: "running" },data: { state: "done", progress: 1, result: result as unknown as Prisma.InputJsonValue },});+ } else if (job.kind === "commands") {+ const result = await runCommandJob(job, handle, () => entry.canceled);+ await prisma.cutRenderJob.updateMany({+ where: { id: job.id, state: "running" },+ data: { state: "done", progress: 1, result: result as unknown as Prisma.InputJsonValue },+ });} else {throw new Error(`Unknown job kind: ${job.kind}`);}diff --git a/site/src/lib/config/registry.ts b/site/src/lib/config/registry.tsindex 6c74d0c3..57b3825e 100644--- a/site/src/lib/config/registry.ts+++ b/site/src/lib/config/registry.ts@@ -41,11 +41,13 @@ export const SETTINGS = defineSettings({requestsPerMinute: z.number().int().min(10).max(600),oauthRequestsPerIpMinute: z.number().int().min(10).max(10000).default(600),pollMs: z.number().int().min(1000).max(30000),+ // How long a tool call waits on the worker before handing back a job id.+ commandWaitMs: z.number().int().min(5000).max(240000).default(50000),}).strict(),- default: { enabled: false, issuer: "https://donkeycut.com", redirectUris: ["https://chatgpt.com/connector_platform_oauth_redirect"], accessSeconds: 3600, refreshDays: 30, requestsPerMinute: 120, oauthRequestsPerIpMinute: 600, pollMs: 2000 },+ default: { enabled: false, issuer: "https://donkeycut.com", redirectUris: ["https://chatgpt.com/connector_platform_oauth_redirect"], accessSeconds: 3600, refreshDays: 30, requestsPerMinute: 120, oauthRequestsPerIpMinute: 600, pollMs: 2000, commandWaitMs: 50000 },public: false,title: "ChatGPT app",- description: "Account linking, allowed OAuth callbacks, token lifetimes, and preview polling. Enable after the OAuth tables are deployed.",+ description: "Account linking, allowed OAuth callbacks, token lifetimes, preview polling, and how long an edit call waits on the worker. Enable after the OAuth tables are deployed.",},cutPreviewJobs: {schema: z.object({ maxAttempts: z.number().int().min(1).max(10) }).strict(),