Open source CapCut alternative.
TypeScript
feat(chatgpt): every project card is the editor
David Lee77d0303df96
diff --git a/docs/guides/chatgpt-app.md b/docs/guides/chatgpt-app.mdindex c1c07702..1453295f 100644--- a/docs/guides/chatgpt-app.md+++ b/docs/guides/chatgpt-app.md@@ -74,11 +74,11 @@ host bridge. It has no account cookie or OAuth token. Preview rendering usesno Donkey Cut AI credits. Derived previews remain exempt from storage quota;retained media uses the account's existing storage allowance.-Opening a project from a connection that can edit puts the whole editor in-the card: the widget frames `donkeycut.com` through a one-use, one-minute-link the tool mints on the connection, and that link redeems for a-partitioned session cookie scoped to the frame, so the editor is signed in-there and nowhere else. Revoking the+Every project card from a connection that can edit is the whole editor: the+widget frames `donkeycut.com` through a one-use, one-minute link each tool+result mints on the connection, a card that wakes up with a spent link asks+for a fresh one, and the link redeems for a partitioned session cookie+scoped to the frame, so the editor is signed in there and nowhere else. Revoking theconnection ends those sessions. A browser without partitioned cookies showsan Open in Donkey Cut link inside the frame. Public submission has to justify`frameDomains`; the justification is that the frame is the app's own editordiff --git a/site/scripts/eval-chatgpt-widget.ts b/site/scripts/eval-chatgpt-widget.tsindex a3feadf7..f8d68be5 100644--- a/site/scripts/eval-chatgpt-widget.ts+++ b/site/scripts/eval-chatgpt-widget.ts@@ -31,7 +31,7 @@ const reply = (id, result) => frame.contentWindow.postMessage({jsonrpc:'2.0',id,window.addEventListener('message', ({source,data}) => {if(source !== frame.contentWindow || !data.method) return;if(data.method === 'ui/initialize') reply(data.id,{protocolVersion:data.params.protocolVersion,hostInfo:{name:'fixture',version:'1'},hostCapabilities:{serverTools:{},openLinks:{}},hostContext:{theme:'light',displayMode:'inline',availableDisplayModes:['inline','fullscreen'],safeAreaInsets:{top:0,right:0,bottom:120,left:0}}});- if(data.method === 'ui/notifications/initialized') frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:result(view(false))},'*');+ if(data.method === 'ui/notifications/initialized') frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:window.editable ? result(view(true),null,{url:location.origin+'/embed?code=spent&project=project',expiresAt:Date.now()-1}) : result(view(false))},'*');if(data.method === 'tools/call') {const name = data.params.name; window.calls.push(name);if(name === 'list_projects') reply(data.id,result(view(false)));@@ -86,6 +86,12 @@ try {await app.frameLocator("iframe.editor").locator("#inset").filter({ hasText: /^120$/ }).waitFor();assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), ["fullscreen", "fullscreen"], "the button asks again and the granted mode hides it");await page.screenshot({ path: "/tmp/donkey-chatgpt-widget-editor.png" });+ // A card rehydrated with a spent link asks for a fresh one instead of showing the preview.+ await page.evaluate(() => { (window as unknown as { calls: string[] }).calls = []; });+ await frame.evaluate(() => { location.reload(); });+ await app.frameLocator("iframe.editor").locator("#editor").waitFor();+ const rehydrated = await page.evaluate(() => (window as unknown as { calls: string[] }).calls);+ assert.deepEqual(rehydrated, ["open_project"], "the rehydrated card mints one fresh link");assert.deepEqual(errors, []);console.log("PASS: project selection, repeated polling, native playback, URL recovery, hidden pause, Open in Donkey Cut, editor in the card, decoder teardown, lazy HLS.");} finally {diff --git a/site/src/clients/chatgpt/server/mcp.test.ts b/site/src/clients/chatgpt/server/mcp.test.tsindex 647f2a27..ec2ef0dd 100644--- a/site/src/clients/chatgpt/server/mcp.test.ts+++ b/site/src/clients/chatgpt/server/mcp.test.ts@@ -53,7 +53,7 @@ const fakeProjects = (overrides: Partial<Projects> = {}): Projects => {list: answer,status: answer,render: answer,- open: answer,+ withEditor: async (result: ProjectResult) => result,create: answer,inspect: answer,edit: answer,diff --git a/site/src/clients/chatgpt/server/mcp.ts b/site/src/clients/chatgpt/server/mcp.tsindex 579992b4..c0ff5fa1 100644--- a/site/src/clients/chatgpt/server/mcp.ts+++ b/site/src/clients/chatgpt/server/mcp.ts@@ -130,7 +130,7 @@ export function createChatgptServer(});const runTool = async (run: () => Promise<ProjectResult>) => {try {- const { view, playback, download, editor } = await run();+ const { view, playback, download, editor } = await projects.withEditor(await run());const media = liftMedia(view);const text = describeProjectView(view) + (editor ? "\nThe Donkey Cut editor is open in the card." : "");return {@@ -182,7 +182,7 @@ export function createChatgptServer(_meta: { ...readMetadata, ui: { resourceUri: WIDGET_URI } },},({ projectId }) =>- runTool(() => (projectId ? projects.open(projectId) : projects.list())),+ runTool(() => (projectId ? projects.status(projectId) : projects.list())),);server.registerTool(diff --git a/site/src/clients/chatgpt/server/projects.test.ts b/site/src/clients/chatgpt/server/projects.test.tsindex 3958dd12..75213820 100644--- a/site/src/clients/chatgpt/server/projects.test.ts+++ b/site/src/clients/chatgpt/server/projects.test.ts@@ -106,12 +106,13 @@ describe("ChatGPT cloud projects", () => {expect(rendered.view.preview?.id).toBe("job");expect(rendered.playback?.url).toContain("proxy");});- test("opening a project with write scope carries a one-use editor link; read scope gets the preview", async () => {+ test("a project card with write scope carries a one-use editor link; read scope and the list get none", async () => {const readOnly = createTestContext();- expect((await readOnly.tools.open("mine")).editor).toBeUndefined();+ expect((await readOnly.tools.withEditor(await readOnly.tools.status("mine"))).editor).toBeUndefined();expect(readOnly.db.chatgptToken.create).not.toHaveBeenCalled();const { tools, db } = createTestContext(["projects:read", "projects:write"]);- const result = await tools.open("mine");+ expect((await tools.withEditor(await tools.list())).editor).toBeUndefined();+ const result = await tools.withEditor(await tools.status("mine"));expect(/^https:\/\/donkeycut\.com\/api\/chatgpt\/embed\?code=[A-Za-z0-9_-]{43}&project=mine$/.test(result.editor?.url ?? "")).toBe(true);expect(db.chatgptToken.create.mock.calls[0][0].data).toMatchObject({ grantId: "grant", kind: "embed" });expect(result.view.project?.id).toBe("mine");diff --git a/site/src/clients/chatgpt/server/projects.ts b/site/src/clients/chatgpt/server/projects.tsindex a8fc5df4..2df08849 100644--- a/site/src/clients/chatgpt/server/projects.ts+++ b/site/src/clients/chatgpt/server/projects.ts@@ -283,14 +283,14 @@ export function projectTools(};},status: getPreviewStatus,- /** The project as the card shows it: the full editor, signed in through a- * one-use link, when the connection can edit; its preview otherwise. */- async open(projectId: string): Promise<ProjectResult> {- const current = await getPreviewStatus(projectId);- if (!canEdit) return current;+ /** 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> {+ const projectId = result.view.project?.id;+ if (!canEdit || !projectId || result.editor) return result;const code = await createEditorCode(identity.grantId, db);const url = `${config.issuer}/api/chatgpt/embed?code=${code}&project=${encodeURIComponent(projectId)}`;- return { ...current, editor: { url, expiresAt: Date.now() + 60_000 } };+ return { ...result, editor: { url, expiresAt: Date.now() + 60_000 } };},async render(projectId: string): Promise<ProjectResult> {if (!canRender) {diff --git a/site/src/clients/chatgpt/ui/useProjectApp.ts b/site/src/clients/chatgpt/ui/useProjectApp.tsindex ff7fbf99..0a53adfd 100644--- a/site/src/clients/chatgpt/ui/useProjectApp.ts+++ b/site/src/clients/chatgpt/ui/useProjectApp.ts@@ -15,6 +15,8 @@ export function useProjectApp() {const [editor, setEditor] = useState<Editor | null>(null);const [fullscreen, setFullscreen] = useState(false);const [hostInset, setHostInset] = useState(0);+ const [editorRequest, setEditorRequest] = useState<string | null>(null);+ const requested = useRef<string | null>(null);const [error, setError] = useState<string | null>(null);const [busy, setBusy] = useState(false);const [visible, setVisible] = useState(!document.hidden);@@ -32,10 +34,12 @@ export function useProjectApp() {setView(parsed.data);setPlayback(media.success ? media.data : null);setDownload(file.success ? file.data : null);- // A one-use link that has already had its minute is a spent one: a card- // rehydrated later shows the preview, and Edit here mints a fresh link.+ // A one-use link that has already had its minute is spent: a card+ // rehydrated later asks the server for a fresh one, once.const embed = editorSchema.safeParse(result._meta?.editor);- if (embed.success && embed.data.expiresAt > Date.now()) setEditor(embed.data);+ const fresh = embed.success && embed.data.expiresAt > Date.now();+ if (fresh) setEditor(embed.data);+ setEditorRequest(fresh || !parsed.data.canEdit || !parsed.data.project ? null : parsed.data.project.id);setError(null);}, []);@@ -132,6 +136,12 @@ export function useProjectApp() {const openDonkey = () => { if (view?.project) void openLink(view.project.url, "Could not open Donkey Cut."); };const openDownload = () => { if (download) void openLink(download.url, "Could not open the download."); };+ useEffect(() => {+ if (!ready || !editorRequest || editor || requested.current === editorRequest) return;+ requested.current = editorRequest;+ void run("open_project", { projectId: editorRequest }, false);+ }, [ready, editorRequest, editor, run]);+// The editor wants the whole window; the host grants what it supports.const requestFullscreen = useCallback(async () => {const app = appRef.current;