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(lib): stop bundling transformers.js into the dist-lib CDN embed

nirholasf939d5ef12f0
diff --git a/src/runtime/whisper-stt.js b/src/runtime/whisper-stt.js
index 0591f05a6..43c36774a 100644
--- a/src/runtime/whisper-stt.js
+++ b/src/runtime/whisper-stt.js
@@ -32,8 +32,19 @@
*/
import { log } from '../shared/log.js';
+import { loadModule } from '../shared/load-module.js';
import { VoiceActivityDetector, VAD_ASSET_PATH, VAD_SAMPLE_RATE, float32ToWav } from '../voice/vad.js';
+// Loaded from the CDN, never bundled: transformers.js drags in onnxruntime-web's
+// WebGPU runtime, tens of MB. A bare `import('@huggingface/transformers')` is
+// statically analyzable, so Vite/Rollup bundles it anyway wherever the build
+// doesn't split dynamic imports into their own chunk (the single-file CDN embed
+// in vite.config.js's libConfig sets inlineDynamicImports for exactly that
+// reason), blowing that embed from ~600KB to 70+MB and tripping Cloud Run's
+// response-size limit. loadModule()'s import() specifier is a runtime string,
+// which no bundler can resolve at build time, so this stays lazy everywhere.
+const TRANSFORMERS_CDN_URL = 'https://esm.sh/@huggingface/[email protected]';
+
export const ON_DEVICE_MODELS = Object.freeze({
english: 'onnx-community/moonshine-base-ONNX',
multilingual: 'onnx-community/whisper-base',
@@ -130,7 +141,7 @@ function loadTranscriber(model, onProgress) {
for (const listener of entry.listeners) listener(event);
};
entry.promise = (async () => {
- transformersModule ||= import('@huggingface/transformers');
+ transformersModule ||= loadModule(TRANSFORMERS_CDN_URL);
const { pipeline, env } = await transformersModule;
// Models come from the Hugging Face CDN only. Without this the library first
// probes /models/<id>/ on the current origin, which on a host page that
diff --git a/tests/lib-bundle-size.test.js b/tests/lib-bundle-size.test.js
new file mode 100644
index 000000000..b679508ca
--- /dev/null
+++ b/tests/lib-bundle-size.test.js
@@ -0,0 +1,49 @@
+// The CDN embed (dist-lib/agent-3d.js, dist-lib/agent-3d.umd.cjs) is a single,
+// self-contained file by design (vite.config.js's libConfig: no externals,
+// inlineDynamicImports: true) so a <script type="module"> drop-in never fetches
+// a second chunk. That setting also inlines any dynamic import() reachable from
+// the entry, so a heavy optional dependency loaded lazily inside src/ (meant to
+// stay a separate chunk/CDN fetch) gets baked into the single file instead.
+//
+// 2026-10-12: src/runtime/whisper-stt.js's on-device speech recognizer lazily
+// imports '@huggingface/transformers', which pulls in onnxruntime-web's WebGPU
+// bundle. inlineDynamicImports forced that whole graph into agent-3d.js, which
+// bloated it from the documented ~600-900KB gzipped to 73MB raw (76MB over the
+// wire), well past Cloud Run's response-size handling for HTTP/1.1 and
+// identity-encoding clients: curl -H "Accept-Encoding: identity"
+// https://three.ws/dist-lib/agent-3d.js returned a bare 500 in production.
+//
+// Skips when dist-lib/ has not been built locally (npm run build:lib:full),
+// same convention as tests/helpers/dist-built.js.
+
+import { describe, it, expect } from 'vitest';
+import { existsSync, statSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import path from 'node:path';
+
+const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const LIB_JS = path.join(REPO_ROOT, 'dist-lib', 'agent-3d.js');
+const LIB_UMD = path.join(REPO_ROOT, 'dist-lib', 'agent-3d.umd.cjs');
+
+// Generous headroom over the documented ~600-900KB gzipped (raw is larger);
+// 5MB catches a regression of this magnitude without being flaky on growth.
+const MAX_BYTES = 5 * 1024 * 1024;
+
+describe('lib bundle size (dist-lib/agent-3d.{js,umd.cjs})', () => {
+ const hasBuild = existsSync(LIB_JS) && existsSync(LIB_UMD);
+ const maybe = hasBuild ? it : it.skip;
+
+ maybe('keeps the ES CDN embed free of bundled optional heavy dependencies', () => {
+ const size = statSync(LIB_JS).size;
+ expect(size).toBeLessThan(MAX_BYTES);
+ });
+
+ maybe('keeps the UMD CDN embed free of bundled optional heavy dependencies', () => {
+ const size = statSync(LIB_UMD).size;
+ expect(size).toBeLessThan(MAX_BYTES);
+ });
+
+ if (!hasBuild) {
+ it.skip('dist-lib/ has not been built locally; run `npm run build:lib:full`', () => {});
+ }
+});