open-webui/src/lib/workers/pyodide.worker.ts

71 lines
1.7 KiB
TypeScript
Raw Normal View History

import { loadPyodide, type PyodideInterface } from 'pyodide';
2024-05-17 08:39:07 +00:00
declare global {
interface Window {
stdout: string | null;
stderr: string | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result: any;
pyodide: PyodideInterface;
packages: string[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
}
async function loadPyodideAndPackages(packages: string[] = []) {
2024-05-17 08:39:07 +00:00
self.stdout = null;
self.stderr = null;
self.result = null;
self.pyodide = await loadPyodide({
indexURL: '/pyodide/',
stdout: (text) => {
console.log('Python output:', text);
if (self.stdout) {
self.stdout += `${text}\n`;
} else {
self.stdout = `${text}\n`;
}
},
stderr: (text) => {
console.log('An error occurred:', text);
2024-05-17 08:39:07 +00:00
if (self.stderr) {
self.stderr += `${text}\n`;
} else {
self.stderr = `${text}\n`;
}
},
packages: ['micropip']
2024-05-17 08:39:07 +00:00
});
const micropip = self.pyodide.pyimport('micropip');
2024-05-21 02:20:49 +00:00
// await micropip.set_index_urls('https://pypi.org/pypi/{package_name}/json');
2024-05-17 08:39:07 +00:00
await micropip.install(packages);
}
self.onmessage = async (event) => {
const { id, code, ...context } = event.data;
console.log(event.data);
2024-05-17 08:39:07 +00:00
// The worker copies the context in its own "memory" (an object mapping name to values)
for (const key of Object.keys(context)) {
self[key] = context[key];
}
// make sure loading is done
await loadPyodideAndPackages(self.packages);
2024-05-19 12:19:48 +00:00
try {
self.result = await self.pyodide.runPythonAsync(code);
} catch (error) {
self.stderr = error.toString();
}
2024-05-17 08:39:07 +00:00
self.postMessage({ id, result: self.result, stdout: self.stdout, stderr: self.stderr });
};
export default {};