File size: 1,213 Bytes
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import io
import contextlib
import logging

logger = logging.getLogger("uvicorn")

class CodeSandboxTool:
    """
    A tool that allows agents to execute Python code and see the output.
    """
    async def execute_python(self, code: str) -> str:
        """
        Executes the provided Python code and returns the stdout/stderr.
        """
        logger.info("CodeSandboxTool: Executing Python code")
        
        # Capture stdout and stderr
        stdout = io.StringIO()
        stderr = io.StringIO()
        
        try:
            with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
                # Using a fresh globals dictionary for each execution
                exec_globals = {}
                exec(code, exec_globals)
            
            output = stdout.getvalue()
            errors = stderr.getvalue()
            
            if errors:
                return f"Output:\n{output}\nErrors:\n{errors}"
            return output if output else "Execution successful (no output)."
            
        except Exception as e:
            return f"Execution failed: {str(e)}"
        finally:
            stdout.close()
            stderr.close()