Open source CapCut alternative.
TypeScript
fix(chatgpt): offer a new project when the card's project was deleted
David Ledd1b6d199ead
diff --git a/site/scripts/eval-chatgpt-widget.ts b/site/scripts/eval-chatgpt-widget.tsindex b265e664..2ad8ba96 100644--- a/site/scripts/eval-chatgpt-widget.ts+++ b/site/scripts/eval-chatgpt-widget.ts@@ -44,8 +44,10 @@ const hostHtml = `<!doctype html><html><body><iframe title="Donkey Cut preview"const frame = document.querySelector('iframe');const project = {id:'project',name:'Launch film',revision:'cloud:2',url:'https://donkeycut.com/app/p/project'};let count = 0, renewals = 0;-window.calls = []; window.links = []; window.modes = []; window.editable = false; window.holdMs = 0; window.readyMs = 0; window.freshCard = false; window.signedOut = false; window.failOpen = false; window.editorCodes = 0;-const view = (selected, preview = null) => ({view:selected?'project':'projects',projects:selected?[]:[project],nextCursor:null,project:selected?project:null,preview,canRender:true,canEdit:window.editable,export:null,job:null,results:[],changed:false,account:null});+window.calls = []; window.links = []; window.modes = []; window.editable = false; window.holdMs = 0; window.readyMs = 0; window.freshCard = false; window.signedOut = false; window.failOpen = false; window.missing = false; window.editorCodes = 0;+const view = (selected, preview = null) => ({view:selected?'project':'projects',projects:selected?[]:[project],nextCursor:null,project:selected?project:null,preview,canRender:true,canEdit:window.editable,export:null,job:null,results:[],changed:false,account:null,missing:null});+const missingView = () => ({...view(false),view:'missing',projects:[],missing:{id:project.id}});+const editorLink = () => ({url:location.origin+'/embed?embed=chatgpt&code=one-use-'+ ++window.editorCodes +'&project=project',expiresAt:Date.now()+60000});// ChatGPT's sandbox drops null-valued keys before the widget sees a result.const dropNulls = (value) => Array.isArray(value) ? value.map(dropNulls) : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).filter(([, v]) => v !== null).map(([k, v]) => [k, dropNulls(v)])) : value;const result = (data, playback = null, editor = null) => dropNulls({content:[{type:'text',text:'Preview'}],structuredContent:data,_meta:{playback,editor,pollMs:1000}});@@ -61,7 +63,8 @@ window.addEventListener('message', ({source,data}) => {if(data.method === 'tools/call') {const name = data.params.name; window.calls.push(name);if(name === 'list_projects') reply(data.id,result(view(false)));- if(name === 'open_project') setTimeout(() => reply(data.id, window.failOpen ? {isError:true} : window.editable ? result(view(true),null,{url:location.origin+'/embed?embed=chatgpt&code=one-use-'+ ++window.editorCodes +'&project=project',expiresAt:Date.now()+60000}) : result(view(true))), window.holdMs);+ if(name === 'open_project') setTimeout(() => reply(data.id, window.failOpen ? {isError:true} : window.missing ? result(missingView()) : window.editable ? result(view(true),null,editorLink()) : result(view(true))), window.holdMs);+ if(name === 'create_project') reply(data.id, result(view(true),null,editorLink()));if(name === 'render_preview') { count=0; reply(data.id,result(view(true,{id:'job',status:'queued',progress:0,revision:'cloud:2'}))); }if(name === 'get_preview_status') {count++; const done = count >= 3;@@ -150,7 +153,7 @@ try {// A card rehydrated with a spent link holds the editor's space, inline and// without controls, while it mints a fresh link. The held reply outlasts the// stale playback's one-second renewal, so a poll would show up in the calls.- type Host = { calls: string[]; modes: string[]; holdMs: number; readyMs: number; freshCard: boolean; signedOut: boolean; failOpen: boolean; links: string[] };+ type Host = { calls: string[]; modes: string[]; holdMs: number; readyMs: number; freshCard: boolean; signedOut: boolean; failOpen: boolean; missing: boolean; links: string[] };await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.holdMs = 1500; });await frame.evaluate(() => { location.reload(); });await page.waitForFunction(() => (window as unknown as Host).calls.length === 1);@@ -200,8 +203,20 @@ try {await app.getByRole("button", { name: "Try again", exact: true }).click();await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project", "open_project"], "manual retry recovers without a browser refresh");+ // A card for a project that has since been deleted says so, in the editor's+ // space, and makes a new project there in its place.+ await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.freshCard = false; host.missing = true; });+ await frame.evaluate(() => { location.reload(); });+ await app.getByRole("status").filter({ hasText: "This project was deleted." }).waitFor();+ assert.equal(await app.locator("iframe.editor").count(), 0, "a deleted project shows no editor frame");+ assert.equal(await app.getByText("Your projects").count(), 0, "a deleted project shows no projects home");+ await page.evaluate(() => { (window as unknown as Host).missing = false; });+ await app.getByRole("button", { name: "Create New Project", exact: true }).click();+ await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();+ await app.locator(".skeleton").waitFor({ state: "detached" });+ assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project", "create_project"], "a deleted project creates a new one in its place");assert.deepEqual(errors, []);- console.log("PASS: project selection, repeated polling, native playback, URL recovery, hidden pause, Open in Donkey Cut, editor in the card, authenticated download redirects, download errors, skeleton until ready, waking editor card, session renewal and retry, decoder teardown, lazy HLS.");+ console.log("PASS: project selection, repeated polling, native playback, URL recovery, hidden pause, Open in Donkey Cut, editor in the card, authenticated download redirects, download errors, skeleton until ready, waking editor card, session renewal and retry, deleted project, decoder teardown, lazy HLS.");} finally {await browser.close(); server.stop(true); mediaServer.stop(true); await rm(scratch, { recursive: true, force: true });}diff --git a/site/src/clients/chatgpt/contracts.test.ts b/site/src/clients/chatgpt/contracts.test.tsindex e55b02b1..e960039a 100644--- a/site/src/clients/chatgpt/contracts.test.ts+++ b/site/src/clients/chatgpt/contracts.test.ts@@ -7,6 +7,7 @@ const views = [{ ...base, view: "projects", projects: [project] },{ ...base, view: "project", projects: [], project, preview: { id: "j1", status: "done", progress: 1, revision: null } },{ ...base, view: "project", projects: [], project, export: { id: "e1", status: "running", progress: 0.5, name: null }, history: { undo: "Trim", redo: null }, account: { credits: "3.00", storageBytes: 10, storageQuotaBytes: null, plan: "free" } },+ { ...base, view: "missing", projects: [], missing: { id: "gone" } },];/** What ChatGPT's sandbox hands the widget: every null-valued key removed. */diff --git a/site/src/clients/chatgpt/contracts.ts b/site/src/clients/chatgpt/contracts.tsindex 48da494f..5d6ed88f 100644--- a/site/src/clients/chatgpt/contracts.ts+++ b/site/src/clients/chatgpt/contracts.ts@@ -14,11 +14,13 @@ export const jobSchema = z.object({ id: z.string(), kind: z.string(), status: jo/** One command's outcome inside a batch. */export const outcomeSchema = z.object({ name: z.string(), ok: z.boolean(), output: z.unknown().optional(), error: z.string().optional() });export const accountSchema = z.object({ credits: z.string(), storageBytes: z.number(), storageQuotaBytes: z.number().nullish(), plan: z.string() });+/** The project open_project asked for and the account no longer has. */+export const missingSchema = z.object({ id: z.string() });export const viewSchema = z.object({- view: z.enum(["projects", "project"]), projects: z.array(projectSchema), nextCursor: z.string().nullish(),+ view: z.enum(["projects", "project", "missing"]), projects: z.array(projectSchema), nextCursor: z.string().nullish(),project: projectSchema.nullish(), preview: previewSchema.nullish(), canRender: z.boolean(), canEdit: z.boolean(),export: exportSchema.nullish(), job: jobSchema.nullish(), results: z.array(outcomeSchema), changed: z.boolean(),- account: accountSchema.nullish(),+ account: accountSchema.nullish(), missing: missingSchema.nullish(),});export const playbackSchema = z.object({ url: z.url(), expiresAt: z.number() });export const downloadSchema = z.object({ url: z.url(), expiresAt: z.number(), name: z.string() });diff --git a/site/src/clients/chatgpt/server/mcp.test.ts b/site/src/clients/chatgpt/server/mcp.test.tsindex d7ae726f..805e7f19 100644--- a/site/src/clients/chatgpt/server/mcp.test.ts+++ b/site/src/clients/chatgpt/server/mcp.test.ts@@ -51,6 +51,7 @@ const fakeProjects = (overrides: Partial<Projects> = {}): Projects => {return {list: answer,status: answer,+ open: answer,render: answer,withEditor: async (result: ProjectResult) => result,create: answer,@@ -155,6 +156,29 @@ describe("ChatGPT MCP protocol", () => {});});+test("open_project answers a deleted project as a typed view the card can read", async () => {+ const missing: ProjectResult = { view: { ...data.view, view: "missing", project: null, preview: null, missing: { id: "gone" } }, playback: null };+ const server = createChatgptServer(+ { userId: "owner", scopes: ["projects:read", "projects:write"], grantId: "grant" },+ config,+ fakeProjects({ open: async () => missing }),+ );+ const client = new Client({ name: "test", version: "1" });+ const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();+ await server.connect(serverTransport);+ await client.connect(clientTransport);+ try {+ const result = await client.callTool({ name: "open_project", arguments: { projectId: "gone" } });+ expect(result.isError).toBeFalsy();+ expect(result.structuredContent).toEqual(missing.view);+ expect(JSON.stringify(result.content)).toContain("no longer exists");+ expect(result._meta).toMatchObject({ editor: null });+ } finally {+ await client.close();+ await server.close();+ }+});+test("render scope requests a reconnect through the MCP authentication challenge", async () => {const server = createChatgptServer({ userId: "owner", scopes: ["projects:read"], grantId: "grant" },diff --git a/site/src/clients/chatgpt/server/mcp.ts b/site/src/clients/chatgpt/server/mcp.tsindex 0d972ae1..35f88a6d 100644--- a/site/src/clients/chatgpt/server/mcp.ts+++ b/site/src/clients/chatgpt/server/mcp.ts@@ -179,14 +179,14 @@ export function createChatgptServer({title: "Open a Donkey Cut project",description:- "Open a cloud project initially, switch projects, or recover a missing card. Keep the existing card during edits and preview checks: reopening creates a fresh editor with empty undo history. The editable card holds the full editor, timeline, media panel, inspector and preview. A read-only connection sees the preview; use render_preview when its revision is stale or missing. With no projectId, show the project picker.",+ "Open a cloud project initially, switch projects, or recover a missing card. Keep the existing card during edits and preview checks: reopening creates a fresh editor with empty undo history. The editable card holds the full editor, timeline, media panel, inspector and preview. A read-only connection sees the preview; use render_preview when its revision is stale or missing. With no projectId, show the project picker. A deleted project answers view \"missing\" and the card offers to create a new one.",inputSchema: z.object({ projectId: idSchema.optional() }),outputSchema: viewSchema,annotations: readOnlyAnnotations,_meta: { ...readMetadata, ui: { resourceUri: WIDGET_URI } },},({ projectId }) =>- runTool(() => (projectId ? projects.status(projectId) : projects.list())),+ runTool(() => (projectId ? projects.open(projectId) : projects.list())),);server.registerTool(@@ -505,6 +505,9 @@ function describeProjectView(view: ProjectView): string {.map((project) => `${project.name}: ${project.id} (${project.url})`).join("\n");}+ if (view.view === "missing") {+ return `Project ${view.missing?.id ?? ""} no longer exists. The card offers to create a new project; list_projects shows the others.`;+ }const project = view.project!;const lines: string[] = [];diff --git a/site/src/clients/chatgpt/server/projects.test.ts b/site/src/clients/chatgpt/server/projects.test.tsindex 08919f81..331fc738 100644--- a/site/src/clients/chatgpt/server/projects.test.ts+++ b/site/src/clients/chatgpt/server/projects.test.ts@@ -60,6 +60,15 @@ describe("ChatGPT cloud projects", () => {expect(db.cutRenderJob.findFirst).not.toHaveBeenCalled();expect(db.cutProject.findMany).not.toHaveBeenCalled();});+ test("opening a project the account no longer has answers a typed missing view", async () => {+ const { tools, db } = createTestContext(["projects:read", "projects:write"]);+ const gone = await tools.withEditor(await tools.open("foreign"));+ expect(gone.view).toMatchObject({ view: "missing", project: null, missing: { id: "foreign" } });+ expect(gone.editor).toBeUndefined();+ expect(db.cutRenderJob.findFirst).not.toHaveBeenCalled();+ const opened = await tools.open("mine");+ expect(opened.view).toMatchObject({ view: "project", project: { id: "mine" }, missing: null });+ });test("read scope cannot enqueue renders or edits", async () => {const { tools } = createTestContext();await expect(tools.render("mine")).rejects.toThrow("permission");diff --git a/site/src/clients/chatgpt/server/projects.ts b/site/src/clients/chatgpt/server/projects.tsindex 540b3f35..74773a19 100644--- a/site/src/clients/chatgpt/server/projects.ts+++ b/site/src/clients/chatgpt/server/projects.ts@@ -72,13 +72,18 @@ export function projectTools(results: [],changed: false,account: null,+ missing: null,});- async function getOwnedRow(projectId: string) {- const row = await db.cutProject.findFirst({+ function findOwnedRow(projectId: string) {+ return db.cutProject.findFirst({where: { id: projectId, userId: identity.userId },select: { id: true, name: true, version: true, previewKey: true },});+ }++ async function getOwnedRow(projectId: string) {+ const row = await findOwnedRow(projectId);if (!row) {throw new ProjectToolError("Project not found. Choose one of your cloud projects.",@@ -104,7 +109,14 @@ export function projectTools(projectId: string,jobId?: string,): Promise<ProjectResult> {- const row = await getOwnedRow(projectId);+ return previewStatus(await getOwnedRow(projectId), jobId);+ }++ async function previewStatus(+ row: NonNullable<Awaited<ReturnType<typeof findOwnedRow>>>,+ jobId?: string,+ ): Promise<ProjectResult> {+ const projectId = row.id;const selectedProject = projectSummary(row);const job = await db.cutRenderJob.findFirst({where: {@@ -365,6 +377,13 @@ export function projectTools(};},status: getPreviewStatus,+ /** open_project's answer. A project the account no longer has is a+ * typed view carrying the id asked for, so the card can say so. */+ async open(projectId: string): Promise<ProjectResult> {+ const row = await findOwnedRow(projectId);+ if (!row) return { view: { ...emptyProjectView(), view: "missing", missing: { id: projectId } }, playback: null };+ return previewStatus(row);+ },/** A project card is the full editor, signed in through a one-use link,* whenever the connection can edit; a read-only one shows the preview. */async withEditor(result: ProjectResult): Promise<ProjectResult> {diff --git a/site/src/clients/chatgpt/ui/PreviewApp.tsx b/site/src/clients/chatgpt/ui/PreviewApp.tsxindex 34518146..0ed732ad 100644--- a/site/src/clients/chatgpt/ui/PreviewApp.tsx+++ b/site/src/clients/chatgpt/ui/PreviewApp.tsx@@ -4,13 +4,25 @@ import { useProjectApp } from "./useProjectApp";import "./preview.css";export function PreviewApp() {- const { ready, view, playback, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openFromFrame, saveFromFrame, requestFullscreen, retryEditor } = useProjectApp();+ const { ready, view, playback, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openFromFrame, saveFromFrame, requestFullscreen, retryEditor, createProject } = useProjectApp();const project = view?.project;const preview = view?.preview;const rendering = preview?.status === "queued" || preview?.status === "running";// The frame whose editor has said it is on screen.const [shown, setShown] = useState<string | null>(null);const [downloadError, setDownloadError] = useState<string | null>(null);+ // The card is the whole surface, so a project that is gone has no projects+ // home to fall back to: it says so in the editor's space and a new project+ // opens there in its place.+ if (view?.view === "missing") {+ return <main className="editing" data-mode={fullscreen ? "fullscreen" : "inline"} aria-busy={busy}>+ <div className="waiting">+ <p role="status">This project was deleted.</p>+ {error && <p role="alert">{error}</p>}+ <button className="primary" disabled={busy || !ready} onClick={createProject}>Create New Project</button>+ </div>+ </main>;+ }// An editable project is the editor, and its own chrome carries every// control. The card holds the editor's inline space from the first paint,// so a card ChatGPT shows again after a reload is the editor while itdiff --git a/site/src/clients/chatgpt/ui/useProjectApp.ts b/site/src/clients/chatgpt/ui/useProjectApp.tsindex 41811930..00214b5c 100644--- a/site/src/clients/chatgpt/ui/useProjectApp.ts+++ b/site/src/clients/chatgpt/ui/useProjectApp.ts@@ -93,7 +93,9 @@ export function useProjectApp() {const current = ++epoch.current;if (manual) { recoveries.current = 0; setBusy(true); setError(null); }try {- const result = await app.callServerTool({ name, arguments: args });+ // The host rejects a failed call with its own error text; the card+ // shows its one sentence for that.+ const result = await app.callServerTool({ name, arguments: args }).catch((): ToolResult => ({ isError: true }));if (current === epoch.current && appRef.current === app) receive(result, origin);} catch (e) {if (current === epoch.current && appRef.current === app) setError(e instanceof Error ? e.message : "Request failed.");@@ -162,6 +164,11 @@ export function useProjectApp() {void run("open_project", { projectId }).finally(() => { reconnecting.current = false; });};+ /** The way forward from a project the account no longer has: a new cloud+ * project, opened here in its place. Its result carries the editor link,+ * so it opens like any other. */+ const createProject = () => { void run("create_project", { name: "Untitled" }); };+// The editor's own button asks for the whole window; the host grants what it supports.const requestFullscreen = useCallback(async () => {const app = appRef.current;@@ -173,5 +180,5 @@ export function useProjectApp() {setHostInset(full ? (app.getHostContext()?.safeAreaInsets?.bottom ?? 120) : 0);} catch { /* the inline card still works */ }}, []);- return { ready, view, playback, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openFromFrame, saveFromFrame, requestFullscreen, retryEditor };+ return { ready, view, playback, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openFromFrame, saveFromFrame, requestFullscreen, retryEditor, createProject };}