Spaces:
Running
Running
File size: 7,520 Bytes
3193174 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | """
Code Interpreter tool — Python code execution.
Allows agents to execute Python code in an isolated environment.
Supports timeouts and output limits.
"""
import io
import traceback
from contextlib import redirect_stderr, redirect_stdout
from typing import Any
from .base import BaseTool, ToolResult
class CodeInterpreterTool(BaseTool):
"""
Tool for executing Python code.
Executes Python code and returns the result. Supports:
- Execution time limit
- Output size limit
- Safe sandbox (restricted builtins)
Example:
tool = CodeInterpreterTool(timeout=10, max_output_size=4096)
result = tool.execute(code="print(2 + 2)")
if result.success:
print(result.output) # "4"
else:
print(f"Error: {result.error}")
"""
def __init__(
self,
timeout: int = 30,
max_output_size: int = 8192,
*,
safe_mode: bool = True,
):
"""
Create CodeInterpreterTool.
Args:
timeout: Maximum execution time in seconds.
max_output_size: Maximum output size in bytes.
safe_mode: If True, restricts available builtins for safety.
"""
self._timeout = timeout
self._max_output_size = max_output_size
self._safe_mode = safe_mode
# Safe builtins for sandbox
self._safe_builtins = {
# Types
"bool": bool,
"int": int,
"float": float,
"str": str,
"list": list,
"dict": dict,
"tuple": tuple,
"set": set,
"frozenset": frozenset,
"bytes": bytes,
"bytearray": bytearray,
# Functions
"abs": abs,
"all": all,
"any": any,
"bin": bin,
"chr": chr,
"divmod": divmod,
"enumerate": enumerate,
"filter": filter,
"format": format,
"hash": hash,
"hex": hex,
"len": len,
"map": map,
"max": max,
"min": min,
"oct": oct,
"ord": ord,
"pow": pow,
"print": print,
"range": range,
"repr": repr,
"reversed": reversed,
"round": round,
"slice": slice,
"sorted": sorted,
"sum": sum,
"zip": zip,
# Exceptions
"Exception": Exception,
"ValueError": ValueError,
"TypeError": TypeError,
"KeyError": KeyError,
"IndexError": IndexError,
"ZeroDivisionError": ZeroDivisionError,
# Other
"True": True,
"False": False,
"None": None,
"isinstance": isinstance,
"issubclass": issubclass,
"type": type,
"callable": callable,
"hasattr": hasattr,
"getattr": getattr,
"setattr": setattr,
"iter": iter,
"next": next,
"input": lambda _: "", # Input is blocked
}
@property
def name(self) -> str:
return "code_interpreter"
@property
def description(self) -> str:
return (
"Execute Python code and return the output. "
"Use for calculations, data processing, and algorithmic tasks. "
"The code runs in a sandboxed environment with limited access."
)
@property
def parameters_schema(self) -> dict[str, Any]:
return {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "Python code to execute. Can be multi-line.",
},
},
"required": ["code"],
}
def _get_safe_globals(self) -> dict[str, Any]:
"""Get safe globals for exec."""
import collections
import datetime
import functools
import itertools
import json
import math
import random
import re
import statistics
return {
"__builtins__": self._safe_builtins if self._safe_mode else __builtins__,
# Safe modules
"math": math,
"statistics": statistics,
"json": json,
"re": re,
"datetime": datetime,
"collections": collections,
"itertools": itertools,
"functools": functools,
"random": random,
}
def execute(self, code: str = "", **_kwargs: Any) -> ToolResult:
"""
Execute Python code.
Args:
code: Python code to execute.
Returns:
ToolResult with output or error.
"""
if not code:
return ToolResult(
tool_name=self.name,
success=False,
error="No code provided",
)
# Capture stdout and stderr
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
try:
# Prepare the environment
# Use a single dict for globals and locals to avoid
# scoping issues (functions defined in exec() must be
# visible when called)
exec_globals = self._get_safe_globals()
# Execute code
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
# Compile to determine type (expression or statement)
try:
# Try as expression (to return a result)
compiled = compile(code, "<code>", "eval")
result = eval(compiled, exec_globals)
if result is not None:
pass
except SyntaxError:
# Execute as statements
# Use a single dict for globals and locals
exec(code, exec_globals)
# Collect output
stdout_output = stdout_capture.getvalue()
stderr_output = stderr_capture.getvalue()
output = stdout_output
if stderr_output:
output += f"\n[stderr]\n{stderr_output}"
# Limit output size
if len(output) > self._max_output_size:
output = output[: self._max_output_size] + "\n... (output truncated)"
return ToolResult(
tool_name=self.name,
success=True,
output=output.strip() if output else "(no output)",
)
except (
ValueError,
TypeError,
SyntaxError,
NameError,
AttributeError,
KeyError,
IndexError,
ZeroDivisionError,
RuntimeError,
OSError,
) as e:
# Format error
error_output = stderr_capture.getvalue()
_ = traceback.format_exc() # Available for debugging
# Extract only the useful part of the traceback
error_msg = f"{type(e).__name__}: {e}"
if error_output:
error_msg = f"{error_output}\n{error_msg}"
return ToolResult(
tool_name=self.name,
success=False,
error=error_msg,
output=stdout_capture.getvalue(),
)
|