Skip to content

CodesnipToolkit

https://github.com/bytedance/SandboxFusion https://bytedance.github.io/SandboxFusion/docs/docs/get-started

CodesnipToolkit

Bases: AsyncBaseToolkit

Source code in utu/tools/codesnip_toolkit.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class CodesnipToolkit(AsyncBaseToolkit):
    def __init__(self, config: ToolkitConfig = None) -> None:
        super().__init__(config)
        self.server_url = self.config.config.get("server_url")

    async def run_code(self, code: str, language: str = "python") -> str:
        """Run code in sandbox and return the result.
        Supported languages: python, cpp, nodejs, go, go_test, java, php, csharp, bash, typescript, sql, rust, cuda,
         lua, R, perl, D_ut, ruby, scala, julia, pttest, junit, kotlin_script, jest, verilog, python_gpu, lean, swift,
         racket

        Args:
            code (str): The code to run.
            language (str, optional): The language of the code. Defaults to "python".
        Returns:
            str: The result of the code.
        """
        payload = {
            "code": code,
            "language": language,
        }
        response = requests.post(f"{self.server_url}/run_code", json=payload)
        result = response.json()
        logger.info(f"[tool] run_code ```{oneline_object(payload)}``` got result: {oneline_object(result)}")
        return str(result)

    async def get_tools_map(self) -> dict[str, Callable]:
        return {
            "run_code": self.run_code,
        }

run_code async

run_code(code: str, language: str = 'python') -> str

Run code in sandbox and return the result. Supported languages: python, cpp, nodejs, go, go_test, java, php, csharp, bash, typescript, sql, rust, cuda, lua, R, perl, D_ut, ruby, scala, julia, pttest, junit, kotlin_script, jest, verilog, python_gpu, lean, swift, racket

Parameters:

Name Type Description Default
code str

The code to run.

required
language str

The language of the code. Defaults to "python".

'python'

Returns: str: The result of the code.

Source code in utu/tools/codesnip_toolkit.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
async def run_code(self, code: str, language: str = "python") -> str:
    """Run code in sandbox and return the result.
    Supported languages: python, cpp, nodejs, go, go_test, java, php, csharp, bash, typescript, sql, rust, cuda,
     lua, R, perl, D_ut, ruby, scala, julia, pttest, junit, kotlin_script, jest, verilog, python_gpu, lean, swift,
     racket

    Args:
        code (str): The code to run.
        language (str, optional): The language of the code. Defaults to "python".
    Returns:
        str: The result of the code.
    """
    payload = {
        "code": code,
        "language": language,
    }
    response = requests.post(f"{self.server_url}/run_code", json=payload)
    result = response.json()
    logger.info(f"[tool] run_code ```{oneline_object(payload)}``` got result: {oneline_object(result)}")
    return str(result)

get_tools_map_func async

get_tools_map_func() -> dict[str, Callable]

Get tools map. It will filter tools by config.activated_tools if it is not None.

Source code in utu/tools/base.py
58
59
60
61
62
63
64
65
66
67
68
69
async def get_tools_map_func(self) -> dict[str, Callable]:
    """Get tools map. It will filter tools by config.activated_tools if it is not None."""
    if self.tools_map is None:
        self.tools_map = await self.get_tools_map()
    if self.config.activated_tools:
        assert all(tool_name in self.tools_map for tool_name in self.config.activated_tools), (
            f"Error config activated tools: {self.config.activated_tools}! available tools: {self.tools_map.keys()}"
        )
        tools_map = {tool_name: self.tools_map[tool_name] for tool_name in self.config.activated_tools}
    else:
        tools_map = self.tools_map
    return tools_map

get_tools_in_agents async

get_tools_in_agents() -> list[FunctionTool]

Get tools in openai-agents format.

Source code in utu/tools/base.py
71
72
73
74
75
76
77
78
79
80
81
82
async def get_tools_in_agents(self) -> list[FunctionTool]:
    """Get tools in openai-agents format."""
    tools_map = await self.get_tools_map_func()
    tools = []
    for _, tool in tools_map.items():
        tools.append(
            function_tool(
                tool,
                strict_mode=False,  # turn off strict mode
            )
        )
    return tools

get_tools_in_openai async

get_tools_in_openai() -> list[dict]

Get tools in OpenAI format.

Source code in utu/tools/base.py
84
85
86
87
async def get_tools_in_openai(self) -> list[dict]:
    """Get tools in OpenAI format."""
    tools = await self.get_tools_in_agents()
    return [ChatCompletionConverter.tool_to_openai(tool) for tool in tools]

get_tools_in_mcp async

get_tools_in_mcp() -> list[Tool]

Get tools in MCP format.

Source code in utu/tools/base.py
89
90
91
92
async def get_tools_in_mcp(self) -> list[types.Tool]:
    """Get tools in MCP format."""
    tools = await self.get_tools_in_agents()
    return [MCPConverter.function_tool_to_mcp(tool) for tool in tools]

call_tool async

call_tool(name: str, arguments: dict) -> str

Call a tool by its name.

Source code in utu/tools/base.py
 94
 95
 96
 97
 98
 99
100
async def call_tool(self, name: str, arguments: dict) -> str:
    """Call a tool by its name."""
    tools_map = await self.get_tools_map_func()
    if name not in tools_map:
        raise ValueError(f"Tool {name} not found")
    tool = tools_map[name]
    return await tool(**arguments)