Skip to content

SDK 不支持 AIO 类型模板的 ToolSet 集成 #44

@XeonYang

Description

@XeonYang

问题概述

当使用 AIO (AllInOne) 类型的沙箱模板时,无法正常使用 CodeInterpreterToolSetBrowserToolSet,导致:

  1. 每次工具调用都创建新的沙箱实例(无法复用)
  2. 工具调用实际上失败,返回空错误 {'error': ''}

环境信息

  • agentrun-sdk 版本: 0.0.16
  • Python 版本: 3.12
  • 操作系统: Windows 10

问题复现

场景 1:覆盖 template_type 为 AIO

from agentrun.integration.builtin.sandbox import CodeInterpreterToolSet
from agentrun.sandbox import TemplateType

toolset = CodeInterpreterToolSet(
    template_name="sandbox-nAtr2H",  # AIO 类型模板
    config=None,
    sandbox_idle_timeout_seconds=600,
)
toolset.template_type = TemplateType.AIO  # 覆盖为 AIO

# 调用工具
result = toolset.process_exec_cmd(command="echo 'Hello'")
print(result)  # {'error': ''}

结果

  • 返回 {'error': ''}(看似成功,实际失败)
  • 每次调用都创建新的沙箱 ID

根本原因
CodeInterpreterToolSet 的每个方法内部都有类型检查:

# sandbox.py 第 193 行等
def inner(sb: Sandbox):
    assert isinstance(sb, CodeInterpreterSandbox)  # ← AioSandbox 不通过!
    ...

当使用 AIO 模板时,Sandbox.create() 返回的是 AioSandbox,不是 CodeInterpreterSandbox,导致 AssertionError

SDK 的 _run_in_sandbox() 方法捕获异常后会重置沙箱并重试:

def _run_in_sandbox(self, callback):
    sb = self._ensure_sandbox()
    try:
        return callback(sb)
    except Exception as e:
        self.sandbox = None  # ← 重置沙箱
        sb = self._ensure_sandbox()  # ← 创建新沙箱
        return callback(sb)  # ← 再次失败

场景 2:使用默认 template_type

from agentrun.integration.builtin.sandbox import CodeInterpreterToolSet

toolset = CodeInterpreterToolSet(
    template_name="sandbox-nAtr2H",  # AIO 类型模板
    config=None,
    sandbox_idle_timeout_seconds=600,
)
# 不覆盖 template_type,使用默认值 CODE_INTERPRETER

result = toolset.process_exec_cmd(command="echo 'Hello'")

结果

ValueError: template_type of sandbox-nAtr2H is AllInOne, not TemplateType.CODE_INTERPRETER

原因:SDK 会检查模板的实际类型与请求的类型是否匹配。

期望行为

SDK 应该支持 AIO 类型的模板,提供以下任一解决方案:

方案 A:提供 AioToolSet

创建一个新的 AioToolSet 类,专门支持 AIO 类型模板:

class AioToolSet(SandboxToolSet):
    def __init__(self, template_name, ...):
        super().__init__(
            template_name=template_name,
            template_type=TemplateType.AIO,  # 使用 AIO 类型
            ...
        )
    
    # 方法内部检查 AioSandbox 而不是 CodeInterpreterSandbox
    def process_exec_cmd(self, ...):
        def inner(sb: Sandbox):
            assert isinstance(sb, AioSandbox)  # 或者不做类型检查
            ...

方案 B:放宽类型检查

修改现有 ToolSet 的类型检查逻辑,允许 AioSandbox

def inner(sb: Sandbox):
    assert isinstance(sb, (CodeInterpreterSandbox, AioSandbox))
    ...

方案 C:使 AioSandbox 继承自 CodeInterpreterSandbox

如果 AioSandbox 继承自 CodeInterpreterSandbox(而不是直接继承 Sandbox),现有的 isinstance 检查就会通过。

相关代码位置

  • agentrun/integration/builtin/sandbox.py
    • 第 16-87 行:SandboxToolSet 基类
    • 第 193 行等:assert isinstance(sb, CodeInterpreterSandbox)
    • 第 69-87 行:_run_in_sandbox() 异常处理

影响

  • 用户无法使用 AIO 类型模板与 LangChain/LangGraph Agent 集成
  • AIO 模板是最灵活的模板类型(同时支持代码执行和浏览器),但无法通过 SDK 的 ToolSet 使用
  • 用户被迫为代码执行和浏览器分别创建不同类型的模板

临时解决方案

目前用户只能:

  1. 在 AgentRun 控制台创建 CODE_INTERPRETER 类型的模板
  2. 使用 CodeInterpreterToolSet 配合该模板
  3. 放弃使用 AIO 模板的灵活性

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions