Files
MathModelAgent/backend/app/tools/interpreter_factory.py
MuWinds 5305754f0b refactor: 重构注释规范、修复类型错误、添加 lint hook 和 CLAUDE.md (#66)
- 后端按 Google Python Style Guide 重构注释,修复 ruff 和 pyright 错误(161→0)
- 前端按 Vue.js Style Guide 重构注释,修复 biome 错误
- 添加 PostToolUse lint hook(ruff + biome 自动检查)
- 更新 CLAUDE.md 覆盖 Commands/Testing/Structure/CodeStyle/GitWorkflow/Boundaries
- 修复 tsconfig.json 废弃的 baseUrl 配置
2026-05-11 23:02:39 +08:00

61 lines
1.9 KiB
Python

"""代码解释器工厂模块,根据配置创建本地或远程解释器。"""
from typing import Literal
from app.tools.base_interpreter import BaseCodeInterpreter
from app.tools.e2b_interpreter import E2BCodeInterpreter
from app.tools.local_interpreter import LocalCodeInterpreter
from app.tools.notebook_serializer import NotebookSerializer
from app.config.setting import settings
from app.utils.log_util import logger
async def create_interpreter(
kind: Literal["remote", "local"] = "local",
*,
task_id: str,
work_dir: str,
notebook_serializer: NotebookSerializer,
timeout=3000,
):
"""创建代码解释器实例。
Args:
kind: 解释器类型,"remote" 使用 E2B 沙箱,"local" 使用本地 Jupyter。
task_id: 任务 ID。
work_dir: 工作目录。
notebook_serializer: Notebook 序列化器。
timeout: 超时时间(秒)。
Returns:
初始化完成的代码解释器实例。
Raises:
ValueError: 未知的解释器类型时抛出。
"""
if not settings.E2B_API_KEY:
logger.info("默认使用本地解释器")
kind = "local"
else:
logger.info("使用远程解释器")
kind = "remote"
interp: BaseCodeInterpreter
if kind == "remote":
interp = await E2BCodeInterpreter.create(
task_id=task_id,
work_dir=work_dir,
notebook_serializer=notebook_serializer,
)
await interp.initialize(timeout=timeout) # type: ignore[reportCallIssue]
return interp
elif kind == "local":
interp = LocalCodeInterpreter(
task_id=task_id,
work_dir=work_dir,
notebook_serializer=notebook_serializer,
)
await interp.initialize()
return interp
else:
raise ValueError(f"未知 interpreter 类型:{kind}")