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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(embed): register the Meshopt decoder so a compressed avatar renders on a host page
nirholasf49b258be715
diff --git a/public/embed/v1.js b/public/embed/v1.jsindex c7315a9cd..ba64cd550 100644--- a/public/embed/v1.js+++ b/public/embed/v1.js@@ -43,6 +43,44 @@})();var MV_CDN = 'https://ajax.googleapis.com/ajax/libs/model-viewer/4.0.0/model-viewer.min.js';+ var MESHOPT_CDN = 'https://cdn.jsdelivr.net/npm/[email protected]/meshopt_decoder.js';++ // model-viewer auto-loads the Draco and KTX2 decoders but leaves Meshopt+ // unset, and every server-baked avatar (the /api/avatars/<id>/glb lane and+ // Forge output) ships EXT_meshopt_compression. Without this the embed threw+ // "setMeshoptDecoder must be called before loading compressed files" and the+ // agent never rendered on the host page. Pages inside three.ws get this from+ // /model-viewer-meshopt.js; an embed on someone else's site has to carry it.+ //+ // Timing matters: model-viewer captures its loader's decoder config when a+ // load STARTS, so the property has to be set before the element upgrades.+ // Setting it via a define() interceptor installed BEFORE the CDN script is+ // appended wins that race; the whenDefined path covers every other ordering.+ function applyMeshopt(ctor) {+ if (ctor && !ctor.meshoptDecoderLocation) {+ try {+ ctor.meshoptDecoderLocation = MESHOPT_CDN;+ } catch (_) {+ /* property not writable in this build: nothing more we can do */+ }+ }+ }+ function installMeshopt() {+ if (!window.customElements) return;+ applyMeshopt(customElements.get('model-viewer'));+ if (!customElements.get('model-viewer') && !customElements.__meshoptDefinePatched) {+ customElements.__meshoptDefinePatched = true;+ var nativeDefine = customElements.define;+ customElements.define = function (name, ctor, options) {+ var result = nativeDefine.call(this, name, ctor, options);+ if (name === 'model-viewer') applyMeshopt(ctor);+ return result;+ };+ }+ customElements.whenDefined('model-viewer').then(function (resolved) {+ applyMeshopt(resolved || customElements.get('model-viewer'));+ });+ }// model-viewer is loaded once, lazily, on first <three-d> instance.// All instances share the same Promise so we never fetch it twice.@@ -50,6 +88,7 @@function ensureModelViewer() {if (modelViewerReady) return modelViewerReady;modelViewerReady = new Promise(function (resolve, reject) {+ installMeshopt();if (window.customElements && customElements.get('model-viewer')) return resolve();var s = document.createElement('script');s.type = 'module';diff --git a/tests/embed-v1-meshopt.test.js b/tests/embed-v1-meshopt.test.jsnew file mode 100644index 000000000..65ded600f--- /dev/null+++ b/tests/embed-v1-meshopt.test.js@@ -0,0 +1,111 @@+// public/embed/v1.js registers the Meshopt decoder with model-viewer.+//+// model-viewer auto-loads the Draco and KTX2 decoders but leaves Meshopt unset,+// and every server-baked avatar (the /api/avatars/<id>/glb lane and Forge+// output) ships EXT_meshopt_compression. Without the decoder the viewer throws+// "setMeshoptDecoder must be called before loading compressed files" and the+// agent never renders. Pages inside three.ws get this from+// /model-viewer-meshopt.js; an embed running on someone else's site has to+// carry it, which /embed/v1/preview proved it did not.+//+// Timing is the whole trick: model-viewer captures its decoder config when a+// load STARTS, so the property must be set before the element upgrades. These+// run the real script against a minimal custom-elements stub and assert the+// decoder survives every ordering.++import { describe, it, expect, beforeEach, vi } from 'vitest';+import { readFileSync } from 'node:fs';+import { resolve, dirname } from 'node:path';+import { fileURLToPath } from 'node:url';++const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');+const source = readFileSync(resolve(ROOT, 'public/embed/v1.js'), 'utf8');++const MESHOPT = 'https://cdn.jsdelivr.net/npm/[email protected]/meshopt_decoder.js';++// Minimal registry: enough for the embed's install path, no DOM engine needed.+function makeRegistry(predefined) {+ const defined = new Map(predefined ? [['model-viewer', predefined]] : []);+ const waiters = [];+ return {+ get: (name) => defined.get(name),+ define(name, ctor) {+ defined.set(name, ctor);+ for (const w of waiters) if (w.name === name) w.resolve(ctor);+ },+ whenDefined(name) {+ const hit = defined.get(name);+ if (hit) return Promise.resolve(hit);+ return new Promise((res) => waiters.push({ name, resolve: res }));+ },+ };+}++// Run just the decoder-install unit out of the real file, so the assertions+// track the shipped source rather than a copy of it.+function loadInstaller(registry) {+ const start = source.indexOf('\tfunction applyMeshopt(ctor) {');+ const end = source.indexOf('\t// model-viewer is loaded once, lazily');+ expect(start).toBeGreaterThan(-1);+ expect(end).toBeGreaterThan(start);+ const unit = source.slice(start, end);+ const factory = new Function(+ 'window',+ 'customElements',+ 'MESHOPT_CDN',+ `${unit}\nreturn installMeshopt;`,+ );+ return factory({ customElements: registry }, registry, MESHOPT);+}++describe('embed v1 meshopt wiring', () => {+ let registry;+ beforeEach(() => {+ registry = makeRegistry();+ });++ it('sets the decoder immediately when model-viewer is already defined', () => {+ const ctor = {};+ registry = makeRegistry(ctor);+ loadInstaller(registry)();+ expect(ctor.meshoptDecoderLocation).toBe(MESHOPT);+ });++ it('sets the decoder the moment model-viewer is defined afterwards', () => {+ loadInstaller(registry)();+ const ctor = {};+ registry.define('model-viewer', ctor);+ expect(ctor.meshoptDecoderLocation).toBe(MESHOPT);+ });++ it('leaves other custom elements untouched', () => {+ loadInstaller(registry)();+ const other = {};+ registry.define('some-widget', other);+ expect(other.meshoptDecoderLocation).toBeUndefined();+ });++ it('never overwrites a decoder the host page already chose', () => {+ const ctor = { meshoptDecoderLocation: 'https://host.example/meshopt.js' };+ registry = makeRegistry(ctor);+ loadInstaller(registry)();+ expect(ctor.meshoptDecoderLocation).toBe('https://host.example/meshopt.js');+ });++ it('survives a build where the static is read-only', () => {+ const ctor = {};+ Object.defineProperty(ctor, 'meshoptDecoderLocation', { get: () => undefined, set() { throw new Error('read only'); } });+ registry = makeRegistry(ctor);+ expect(() => loadInstaller(registry)()).not.toThrow();+ });+});++describe('embed v1 install ordering', () => {+ it('installs the decoder before the model-viewer CDN script is appended', () => {+ const install = source.indexOf('installMeshopt();');+ const append = source.indexOf("s.src = MV_CDN;");+ expect(install).toBeGreaterThan(-1);+ expect(append).toBeGreaterThan(-1);+ expect(install).toBeLessThan(append);+ });+});