Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(apps-sdk): ChatGPT widget now actually renders models

nirholasd018d4b0159c
diff --git a/api/_mcp-studio/component.js b/api/_mcp-studio/component.js
index 7cc6ec03e..6a3f12e3b 100644
--- a/api/_mcp-studio/component.js
+++ b/api/_mcp-studio/component.js
@@ -45,6 +45,9 @@ export const COMPONENT_HTML = `<!doctype html>
transition: opacity .28s ease;
}
model-viewer.fading { opacity: 0; }
+ /* Reveal veil: model-viewer defers loading (and never fires 'load') while
+ display:none, so the pre-load hide must be opacity, never .hidden. */
+ model-viewer.veiled { opacity: 0; pointer-events: none; }
@media (prefers-reduced-motion: reduce) { model-viewer { transition: none; } }
.overlay {
position: absolute; inset: 0;
@@ -111,7 +114,7 @@ export const COMPONENT_HTML = `<!doctype html>
<body>
<div class="wrap">
<div class="stage">
- <model-viewer id="mv" class="hidden" camera-controls auto-rotate touch-action="pan-y"
+ <model-viewer id="mv" class="veiled" camera-controls auto-rotate touch-action="pan-y"
shadow-intensity="1" exposure="1.05" environment-image="neutral"
ar ar-modes="webxr scene-viewer quick-look" alt="Generated 3D model">
</model-viewer>
@@ -163,10 +166,21 @@ export const COMPONENT_HTML = `<!doctype html>
// stage, hide overlays) or a cross-fade swap between versions (just fade in).
var swapping = false;
+ // A GLB that never finishes (network stall, bad asset) must end in the
+ // designed error state, not an eternal spinner. 90s covers cold storage reads.
+ var loadWatchdog = null;
+ function armWatchdog() {
+ clearTimeout(loadWatchdog);
+ loadWatchdog = setTimeout(function () {
+ if (!mv.loaded) fail('The 3D model is taking too long to load. You can still download the GLB file.');
+ }, 90000);
+ }
+
function show(el) { [loading, empty, errEl].forEach(function (n) { n.classList.add('hidden'); }); if (el) el.classList.remove('hidden'); }
function fail(msg) {
- mv.classList.add('hidden');
+ clearTimeout(loadWatchdog);
+ mv.classList.add('veiled');
bar.classList.add('hidden');
versionsEl.classList.add('hidden');
errMsg.textContent = msg || 'Something went wrong displaying this model.';
@@ -181,7 +195,7 @@ export const COMPONENT_HTML = `<!doctype html>
if (!isHttps(glb) || mv.getAttribute('src') === glb) return;
swapping = true;
mv.classList.add('fading');
- setTimeout(function () { mv.setAttribute('src', glb); }, 200);
+ setTimeout(function () { mv.setAttribute('src', glb); armWatchdog(); }, 200);
}
// Build the version strip from a lineage. Each chip swaps its GLB in on click;
@@ -215,16 +229,17 @@ export const COMPONENT_HTML = `<!doctype html>
}
function render(out) {
- if (!out) { mv.classList.add('hidden'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
+ if (!out) { mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
if (out.error || out.message && !out.glbUrl) { fail(out.message || 'Generation did not return a model.'); return; }
var glb = out.glbUrl;
- if (!isHttps(glb)) { mv.classList.add('hidden'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
+ if (!isHttps(glb)) { mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
swapping = false;
show(loading);
mv.classList.remove('fading');
- mv.classList.add('hidden');
+ mv.classList.add('veiled');
mv.setAttribute('src', glb);
+ armWatchdog();
var kind = (out.kind || 'model').replace(/_/g, ' ');
kindEl.textContent = out.rigged ? 'rigged ' + kind : kind;
@@ -238,8 +253,9 @@ export const COMPONENT_HTML = `<!doctype html>
}
mv.addEventListener('load', function () {
+ clearTimeout(loadWatchdog);
if (swapping) { swapping = false; mv.classList.remove('fading'); return; }
- show(null); mv.classList.remove('hidden');
+ show(null); mv.classList.remove('veiled');
});
mv.addEventListener('error', function () { fail('The 3D model could not be displayed. You can still download the GLB file.'); });
diff --git a/api/_mcp-studio/tools.js b/api/_mcp-studio/tools.js
index 924727b84..d93140678 100644
--- a/api/_mcp-studio/tools.js
+++ b/api/_mcp-studio/tools.js
@@ -44,9 +44,21 @@ const VALID_TIER = new Set(['draft', 'standard', 'high']);
// ── result helpers ──────────────────────────────────────────────────────────
+// The bucket's public r2.dev domain only answers CORS for our own origin, but
+// ChatGPT renders the widget in a sandboxed cross-origin iframe — model-viewer
+// there can only fetch a GLB served with open CORS. /cdn/<key> (api/cdn-object.js)
+// streams the same object first-party with `access-control-allow-origin: *`, so
+// every URL a widget must fetch goes out in its CDN form.
+function firstPartyGlbUrl(glbUrl, base) {
+ const pub = process.env.S3_PUBLIC_DOMAIN;
+ if (!pub || !base || typeof glbUrl !== 'string' || !glbUrl.startsWith(`${pub}/`)) return glbUrl;
+ return `${base}/cdn/${glbUrl.slice(pub.length + 1)}`;
+}
+
// Minimal, identifier-free success envelope. structuredContent is the contract
// the widget + model read; content is the human/agent-readable narration.
function ok({ glbUrl, base, kind, prompt, rigged }) {
+ glbUrl = firstPartyGlbUrl(glbUrl, base);
const vUrl = viewerUrl(base, glbUrl);
const structured = {
kind,
@@ -92,6 +104,7 @@ function toolError(message) {
// the next refinement (parent_lineage) so branch/revert work without any
// server-side session — a pointer move over an immutable array, never a mutation.
function refineOk({ glbUrl, base, prompt, instruction, lineage, activeIndex }) {
+ glbUrl = firstPartyGlbUrl(glbUrl, base);
const vUrl = viewerUrl(base, glbUrl);
const structured = {
kind: 'refined model',
@@ -100,7 +113,12 @@ function refineOk({ glbUrl, base, prompt, instruction, lineage, activeIndex }) {
format: 'glb',
...(prompt ? { prompt } : {}),
...(instruction ? { instruction } : {}),
- lineage: summarizeLineage(lineage, activeIndex),
+ // Version chips swap GLBs inside the same sandboxed iframe, so every
+ // lineage URL needs the CDN form too.
+ lineage: summarizeLineage(lineage, activeIndex).map((v) => ({
+ ...v,
+ glbUrl: firstPartyGlbUrl(v.glbUrl, base),
+ })),
activeIndex,
// Conformant Spatial MCP artifact (specs/SPATIAL_MCP.md) for the refined model.
spatial: buildSpatialArtifact({ glbUrl, kind: 'model', viewerUrl: vUrl, prompt: prompt || undefined, title: instruction || undefined }),
diff --git a/tests/mcp-studio.test.js b/tests/mcp-studio.test.js
index 25aa58300..2a87121a5 100644
--- a/tests/mcp-studio.test.js
+++ b/tests/mcp-studio.test.js
@@ -200,6 +200,44 @@ describe('mcp-studio dispatch', () => {
expect(serialized).not.toContain('creation_id');
});
+ it('rewrites bucket-domain GLB URLs to the first-party /cdn proxy (sandboxed widgets need open CORS)', async () => {
+ // The public r2.dev domain only answers CORS for our own origin; ChatGPT's
+ // widget iframe is cross-origin, so tool results must carry the /cdn form.
+ const prev = process.env.S3_PUBLIC_DOMAIN;
+ process.env.S3_PUBLIC_DOMAIN = 'https://pub-test.r2.dev';
+ try {
+ globalThis.fetch = vi.fn(async () => ({
+ ok: true,
+ status: 200,
+ json: async () => ({ status: 'done', glb_url: 'https://pub-test.r2.dev/forge/anon/abc.glb' }),
+ }));
+ const r = await dispatch(
+ { jsonrpc: '2.0', id: 61, method: 'tools/call', params: { name: 'forge_free', arguments: { prompt: 'a friendly round robot mascot' } } },
+ auth,
+ mkReq(),
+ );
+ const sc = r.result.structuredContent;
+ expect(sc.glbUrl).toBe('https://three.ws/cdn/forge/anon/abc.glb');
+ expect(sc.viewerUrl).toContain(encodeURIComponent('https://three.ws/cdn/forge/anon/abc.glb'));
+ } finally {
+ if (prev === undefined) delete process.env.S3_PUBLIC_DOMAIN;
+ else process.env.S3_PUBLIC_DOMAIN = prev;
+ }
+ });
+
+ it('widget veils the model-viewer with opacity, never display:none (display:none defers load forever)', async () => {
+ const read = await dispatch(
+ { jsonrpc: '2.0', id: 62, method: 'resources/read', params: { uri: COMPONENT_URI } },
+ auth,
+ mkReq(),
+ );
+ const html = read.result.contents[0].text;
+ const mvTag = html.match(/<model-viewer[^>]*>/)[0];
+ expect(mvTag).toContain('class="veiled"');
+ expect(mvTag).not.toContain('hidden');
+ expect(html).toMatch(/model-viewer\.veiled\s*\{[^}]*opacity:\s*0/);
+ });
+
it('refuses age-inappropriate prompts before any generation', async () => {
const spy = vi.fn();
globalThis.fetch = spy;