mirror of
https://github.com/lobehub/lobehub
synced 2026-04-21 17:47:27 +00:00
* feat: 初步完成 * chore: type * feat: 图片功能 * feat: 文件下载功能 * refactor: 简化代码 * chore: 清理代码 * chore: clean * chore: 清理代码 * chore: 清理代码 * chore: 小改进 * fix: 上传完成前图片无法显示 * refactor: 增加 python-interpreter package * chore: 清理 * feat: 传入上下文中的文件 * chore: 小优化 * chore: 中文字体 * chore: clean * fix: 服务端部署 * fix: 重复文件检查 * test: 增加 interpreter.test.ts * test: add worker.test.ts * style: fix import * test: fix * style: fix import * style: move env file to envs * style: 限制代码框高度 * style: 重命名 * misc: 小修小补 * refactor: 重命名为 code-interpreter --------- Co-authored-by: Arvin Xu <arvinx@foxmail.com>
33 lines
1,021 B
TypeScript
33 lines
1,021 B
TypeScript
import { PythonInterpreter } from '@lobechat/python-interpreter';
|
|
import { CodeInterpreterResponse } from '@lobechat/types';
|
|
|
|
class PythonService {
|
|
async runPython(
|
|
code: string,
|
|
packages: string[],
|
|
files: File[],
|
|
): Promise<CodeInterpreterResponse | undefined> {
|
|
if (typeof Worker === 'undefined') return;
|
|
const interpreter = await new PythonInterpreter!({
|
|
pyodideIndexUrl: process.env.NEXT_PUBLIC_PYODIDE_INDEX_URL!,
|
|
pypiIndexUrl: process.env.NEXT_PUBLIC_PYPI_INDEX_URL!,
|
|
});
|
|
await interpreter.init();
|
|
await interpreter.installPackages(packages.filter((p) => p !== ''));
|
|
await interpreter.uploadFiles(files);
|
|
|
|
const result = await interpreter.runPython(code);
|
|
|
|
const resultFiles = await interpreter.downloadFiles();
|
|
return {
|
|
files: resultFiles.map((file) => ({
|
|
data: file,
|
|
filename: file.name,
|
|
previewUrl: URL.createObjectURL(file),
|
|
})),
|
|
...result,
|
|
};
|
|
}
|
|
}
|
|
|
|
export const pythonService = new PythonService();
|