Skip to content

Commit 80b2204

Browse files
committed
fix: Gemma4 worker must be classic type — module worker breaks CDN dynamic import
Root cause: initAiWorker creates workers with {type:'module'} but ai-worker-gemma4.js uses dynamic import() of external CDN URL (jsdelivr transformers.js). In Vite dev mode, module workers have their imports intercepted by the bundler which cannot resolve https://cdn.jsdelivr.net/... URLs, causing immediate worker.onerror and the 'Worker failed to initialize' message in the consent modal. Fix: - ai-assistant.js: read cfg.workerType; default 'module' for existing workers - ai-models.js: workerType:'classic' on gemma4-e2b and gemma4-e4b - ai-worker-gemma4.js: unhandledrejection safety net, try/catch in message handler, null-check for Gemma4ForConditionalGeneration/Gemma4Processor
1 parent a5440fc commit 80b2204

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Fix: Gemma 4 Worker Init — classic worker type + error handling
2+
3+
- ai-assistant.js: read `workerType` from model config (default: module); Gemma 4 workers must be classic, not module workers, because Vite dev mode intercepts module worker imports and fails to resolve external CDN URLs via its module bundler
4+
- ai-models.js: added `workerType: "classic"` to gemma4-e2b and gemma4-e4b configs
5+
- ai-worker-gemma4.js: added unhandledrejection handler, try/catch in message handler, null-checks for Gemma4 classes with readable error messages

js/ai-assistant.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,11 @@
697697
const workerPath = (cfg && cfg.workerFile) || 'ai-worker.js';
698698
// Add cache-buster for local workers in dev mode so edits are always picked up
699699
const workerUrl = workerPath + '?v=' + (window._workerVersion || Date.now());
700-
const worker = new Worker(workerUrl, { type: 'module' });
700+
// Some workers (e.g. gemma4) use dynamic import() of external CDN URLs.
701+
// These MUST be classic workers — module workers in Vite dev mode have their
702+
// imports intercepted by the module bundler, causing CDN imports to fail.
703+
const workerType = (cfg && cfg.workerType) || 'module';
704+
const worker = new Worker(workerUrl, { type: workerType });
701705
ls.worker = worker;
702706

703707
// Send model ID before loading

js/ai-models.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
category: 'local-multimodal',
104104
localModelId: 'onnx-community/gemma-4-E2B-it-ONNX',
105105
workerFile: 'ai-worker-gemma4.js',
106+
workerType: 'classic', // Must be classic — uses dynamic import() of CDN URL
106107
downloadSize: '~2 GB',
107108
requiresWebGPU: true,
108109
supportsVision: true,
@@ -124,6 +125,7 @@
124125
category: 'local-multimodal',
125126
localModelId: 'onnx-community/gemma-4-E4B-it-ONNX',
126127
workerFile: 'ai-worker-gemma4.js',
128+
workerType: 'classic', // Must be classic — uses dynamic import() of CDN URL
127129
downloadSize: '~4 GB',
128130
requiresWebGPU: true,
129131
requiresHighEnd: true,

0 commit comments

Comments
 (0)