Skip to content

Open source CapCut alternative.

TypeScript

fix(chatgpt): widget contract survives the sandbox dropping null keys

David Leb60193dd33e0
diff --git a/docs/guides/chatgpt-app.md b/docs/guides/chatgpt-app.md
index b69fa033..d2eaeef6 100644
--- a/docs/guides/chatgpt-app.md
+++ b/docs/guides/chatgpt-app.md
@@ -54,7 +54,9 @@ catalog and guides, editing, importing, undo and redo, previews, exports and job
polling. Its schemas and widget data types share one contract. Signed media and
download URLs travel in tool-result metadata, which reaches the widget. The
model gets project metadata, command outcomes, render state, and an Open in
-Donkey Cut link.
+Donkey Cut link. ChatGPT's sandbox drops null-valued keys from a tool result
+before the widget sees it, so the widget contract treats a missing key and null
+alike.
Cloud previews use the existing headless renderer and a captured document.
Repeated requests for the same saved revision reuse a queued, running, or
diff --git a/site/scripts/eval-chatgpt-widget.ts b/site/scripts/eval-chatgpt-widget.ts
index 7fd0c129..4005d530 100644
--- a/site/scripts/eval-chatgpt-widget.ts
+++ b/site/scripts/eval-chatgpt-widget.ts
@@ -23,7 +23,9 @@ const project = {id:'project',name:'Launch film',revision:'cloud:2',url:'https:/
let count = 0, renewals = 0;
window.calls = []; window.links = [];
const view = (selected, preview = null) => ({view:selected?'project':'projects',projects:selected?[]:[project],nextCursor:null,project:selected?project:null,preview,canRender:true,canEdit:false,export:null,job:null,results:[],changed:false,history:null,account:null});
-const result = (data, playback = null) => ({content:[{type:'text',text:'Preview'}],structuredContent:data,_meta:{playback,pollMs:1000}});
+// 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) => dropNulls({content:[{type:'text',text:'Preview'}],structuredContent:data,_meta:{playback,pollMs:1000}});
const reply = (id, result) => frame.contentWindow.postMessage({jsonrpc:'2.0',id,result},'*');
window.addEventListener('message', ({source,data}) => {
if(source !== frame.contentWindow || !data.method) return;
diff --git a/site/src/clients/chatgpt/contracts.test.ts b/site/src/clients/chatgpt/contracts.test.ts
new file mode 100644
index 00000000..e55b02b1
--- /dev/null
+++ b/site/src/clients/chatgpt/contracts.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, test } from "bun:test";
+import { viewSchema } from "./contracts";
+
+const project = { id: "p1", name: "Launch film", url: "https://donkeycut.com/app/p/p1", revision: "cloud:2" };
+const base = { canRender: true, canEdit: false, results: [], changed: false, project: null, preview: null, export: null, job: null, history: null, account: null, nextCursor: null };
+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" } },
+];
+
+/** What ChatGPT's sandbox hands the widget: every null-valued key removed. */
+const dropNulls = (value: unknown): unknown =>
+ 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;
+
+describe("widget view contract", () => {
+ test("parses a view with nulls and the same view with its null keys dropped", () => {
+ for (const view of views) {
+ expect(viewSchema.safeParse(view).success).toBe(true);
+ const stripped = viewSchema.safeParse(dropNulls(view));
+ expect(stripped.success).toBe(true);
+ }
+ });
+});
diff --git a/site/src/clients/chatgpt/contracts.ts b/site/src/clients/chatgpt/contracts.ts
index 8ad3a26a..b7ce4769 100644
--- a/site/src/clients/chatgpt/contracts.ts
+++ b/site/src/clients/chatgpt/contracts.ts
@@ -1,23 +1,26 @@
import { z } from "zod";
export const WIDGET_URI = "ui://donkey/project-preview-v1.html";
+// ChatGPT's sandbox drops every null-valued key from a tool result before the
+// widget sees it, so an absent value parses whether it arrives as null or as a
+// missing key.
export const projectSchema = z.object({ id: z.string(), name: z.string(), url: z.url(), revision: z.string() });
export const jobStatusSchema = z.enum(["queued", "running", "done", "error", "expired"]);
-export const previewSchema = z.object({ id: z.string(), status: jobStatusSchema, progress: z.number(), revision: z.string().nullable(), error: z.string().optional() });
+export const previewSchema = z.object({ id: z.string(), status: jobStatusSchema, progress: z.number(), revision: z.string().nullish(), error: z.string().optional() });
/** A finished export downloads from the widget; the model gets the project link. */
-export const exportSchema = z.object({ id: z.string(), status: jobStatusSchema, progress: z.number(), name: z.string().nullable(), error: z.string().optional() });
+export const exportSchema = z.object({ id: z.string(), status: jobStatusSchema, progress: z.number(), name: z.string().nullish(), error: z.string().optional() });
/** Any queued work the caller may poll with get_job_status. */
export const jobSchema = z.object({ id: z.string(), kind: z.string(), status: jobStatusSchema, progress: z.number(), error: z.string().optional() });
/** 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() });
/** What undo and redo would each revert, by the step's label. */
-export const historySchema = z.object({ undo: z.string().nullable(), redo: z.string().nullable() });
-export const accountSchema = z.object({ credits: z.string(), storageBytes: z.number(), storageQuotaBytes: z.number().nullable(), plan: z.string() });
+export const historySchema = z.object({ undo: z.string().nullish(), redo: z.string().nullish() });
+export const accountSchema = z.object({ credits: z.string(), storageBytes: z.number(), storageQuotaBytes: z.number().nullish(), plan: z.string() });
export const viewSchema = z.object({
- view: z.enum(["projects", "project"]), projects: z.array(projectSchema), nextCursor: z.string().nullable(),
- project: projectSchema.nullable(), preview: previewSchema.nullable(), canRender: z.boolean(), canEdit: z.boolean(),
- export: exportSchema.nullable(), job: jobSchema.nullable(), results: z.array(outcomeSchema), changed: z.boolean(),
- history: historySchema.nullable(), account: accountSchema.nullable(),
+ view: z.enum(["projects", "project"]), 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(),
+ history: historySchema.nullish(), account: accountSchema.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.ts b/site/src/clients/chatgpt/server/mcp.ts
index 8013ae8e..b2b9ef7e 100644
--- a/site/src/clients/chatgpt/server/mcp.ts
+++ b/site/src/clients/chatgpt/server/mcp.ts
@@ -487,7 +487,7 @@ export function createChatgptServer(
function describeProjectView(view: ProjectView): string {
if (view.account) {
const { credits, storageBytes, storageQuotaBytes, plan } = view.account;
- const storage = storageQuotaBytes === null ? `${mb(storageBytes)} MB used` : `${mb(storageBytes)} of ${mb(storageQuotaBytes)} MB used`;
+ const storage = typeof storageQuotaBytes !== "number" ? `${mb(storageBytes)} MB used` : `${mb(storageBytes)} of ${mb(storageQuotaBytes)} MB used`;
return `Plan: ${plan}. AI credits: $${credits}. Cloud storage: ${storage}. Editing, previews and exports spend no credits.`;
}
if (view.view === "projects") {