Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(python-interpreter): run the worker as classic, and publish workers where a page can load them (#18277)

Inneia608bd3f1db0
diff --git a/packages/python-interpreter/src/interpreter.ts b/packages/python-interpreter/src/interpreter.ts
index 46244069e4b..cfedb3dcafd 100644
--- a/packages/python-interpreter/src/interpreter.ts
+++ b/packages/python-interpreter/src/interpreter.ts
@@ -21,10 +21,12 @@ export const getPythonInterpreter = (): Comlink.Remote<PythonWorkerType> | undef
if (typeof Worker !== 'undefined') {
try {
+ // Classic, not a module worker: `worker.ts` loads Pyodide through
+ // `importScripts`, and that call throws "Module scripts don't support
+ // importScripts()" under `type: 'module'`. The bundler emits this worker as
+ // an IIFE anyway, so the module type was never matched by the output.
interpreter = Comlink.wrap<PythonWorkerType>(
- new Worker(new URL('worker.ts', import.meta.url), {
- type: 'module',
- }),
+ new Worker(new URL('worker.ts', import.meta.url)),
);
} catch (error) {
constructionError = error;
diff --git a/scripts/copySpaBuild.test.ts b/scripts/copySpaBuild.test.ts
index 61a2975766d..647d20c8313 100644
--- a/scripts/copySpaBuild.test.ts
+++ b/scripts/copySpaBuild.test.ts
@@ -31,6 +31,26 @@ describe('copySpaBuild', () => {
}
});
+ // `new Worker` only accepts a same-origin script, so these are requested from the
+ // page's own origin rather than the asset host β€” which means they have to reach
+ // `public/`, and at its root, because that is the path the build emits.
+ it('publishes workers to the public root, outside the per-variant directories', () => {
+ const root = mkdtempSync(path.join(tmpdir(), 'copy-spa-build-workers-'));
+ testRoots.push(root);
+
+ for (const variant of ['desktop', 'workbench']) {
+ const sourceDir = path.join(root, `dist/${variant}/app-workers`);
+ mkdirSync(sourceDir, { recursive: true });
+ writeFileSync(path.join(sourceDir, 'worker-abc.js'), 'self.onmessage = () => {};');
+ }
+
+ copySpaBuild(root);
+
+ expect(existsSync(path.join(root, 'public/app-workers/worker-abc.js'))).toBe(true);
+ expect(existsSync(path.join(root, 'public/_spa/app-workers'))).toBe(false);
+ expect(existsSync(path.join(root, 'public/_spa-workbench/app-workers'))).toBe(false);
+ });
+
it('runs through the production Node entrypoint', () => {
const root = mkdtempSync(path.join(tmpdir(), 'copy-spa-build-entry-'));
testRoots.push(root);
diff --git a/scripts/copySpaBuildCore.ts b/scripts/copySpaBuildCore.ts
index 016d8bb5a11..8d987354134 100644
--- a/scripts/copySpaBuildCore.ts
+++ b/scripts/copySpaBuildCore.ts
@@ -2,6 +2,12 @@ import { cpSync, existsSync, mkdirSync, readdirSync, statSync } from 'node:fs';
import path from 'node:path';
const copyDirs = ['assets', 'devtools', 'i18n', 'model-bank', 'shiki', 'vendor'] as const;
+
+// Workers are requested root-relative, not from under the entry prefix, because
+// `new Worker` needs a same-origin script and the file is byte-identical across
+// variants. So this one lands in `public/` itself rather than in each SPA
+// directory β€” a single copy behind a single path.
+const rootCopyDirs = ['app-workers'] as const;
const copyRootFilePatterns = [/^favicon.*\.ico$/, /^apple-touch-icon\.png$/] as const;
const targets = [
{ distDir: 'desktop', publicDir: 'public/_spa' },
@@ -26,6 +32,15 @@ export const copySpaBuild = (root = path.resolve(import.meta.dirname, '..')) =>
console.log(`Copied dist/${distDir}/${dir} -> ${publicDir}/${dir}`);
}
+ for (const dir of rootCopyDirs) {
+ const sourceDir = path.resolve(distRoot, dir);
+
+ if (!existsSync(sourceDir)) continue;
+
+ cpSync(sourceDir, path.resolve(root, 'public', dir), { recursive: true });
+ console.log(`Copied dist/${distDir}/${dir} -> public/${dir}`);
+ }
+
if (!existsSync(distRoot)) continue;
for (const file of readdirSync(distRoot)) {