|
| 1 | +"""Universal agent tool wrapper for CodeShield. |
| 2 | +
|
| 3 | +The function returned by ``create_code_execution_tool`` can be registered as a |
| 4 | +tool in any agent framework (LangChain, CrewAI, Google Gen AI, etc.). It runs |
| 5 | +the provided Python source inside a self-healing sandbox and returns either the |
| 6 | +stdout or a structured error report. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from collections.abc import Callable |
| 12 | + |
| 13 | +from codeshield.environment import SandboxError |
| 14 | +from codeshield.loop import SelfHealingEngine, SelfHealingError |
| 15 | +from codeshield.runner import SubprocessRunnerError |
| 16 | + |
| 17 | + |
| 18 | +def create_code_execution_tool( |
| 19 | + engine: SelfHealingEngine | None = None, |
| 20 | +) -> Callable[[str], str]: |
| 21 | + """Return a drop-in ``execute_python_code(code: str) -> str`` tool. |
| 22 | +
|
| 23 | + Args: |
| 24 | + engine: Optional ``SelfHealingEngine`` instance. When ``None``, a fresh |
| 25 | + engine is created for each tool call. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + A callable ready to be registered as an agent tool. |
| 29 | + """ |
| 30 | + |
| 31 | + def execute_python_code(code: str) -> str: |
| 32 | + """Execute Python code in an isolated, self-healing sandbox. |
| 33 | +
|
| 34 | + Use this tool to run numerical, statistical or data-processing |
| 35 | + computations that cannot be done directly in the conversation. |
| 36 | +
|
| 37 | + Args: |
| 38 | + code: A valid Python script as a string. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + The stdout of the script if execution succeeds, or a structured |
| 42 | + error report if it fails after all self-healing attempts. |
| 43 | + """ |
| 44 | + _engine = engine or SelfHealingEngine() |
| 45 | + with _engine: |
| 46 | + try: |
| 47 | + result, diagnosis = _engine.run(code) |
| 48 | + except SelfHealingError as exc: |
| 49 | + return f"error_type: SelfHealingError\nmessage: {exc}" |
| 50 | + except (SandboxError, SubprocessRunnerError) as exc: |
| 51 | + return f"error_type: {type(exc).__name__}\nmessage: {exc}" |
| 52 | + |
| 53 | + if ( |
| 54 | + result.exit_code == 0 |
| 55 | + and not result.silent_failure_detected |
| 56 | + and not result.timed_out |
| 57 | + ): |
| 58 | + return result.stdout.strip() |
| 59 | + |
| 60 | + report: list[str] = ["The Python script did not execute successfully."] |
| 61 | + if diagnosis is not None: |
| 62 | + report.append(f"error_type: {diagnosis.error_type}") |
| 63 | + if diagnosis.root_cause_line is not None: |
| 64 | + report.append(f"root_cause_line: {diagnosis.root_cause_line}") |
| 65 | + if diagnosis.message: |
| 66 | + report.append(f"message: {diagnosis.message}") |
| 67 | + if result.stderr.strip(): |
| 68 | + report.append(f"stderr: {result.stderr.strip()}") |
| 69 | + if result.timed_out: |
| 70 | + report.append("timed_out: true") |
| 71 | + if result.silent_failure_detected: |
| 72 | + report.append("silent_failure_detected: true") |
| 73 | + |
| 74 | + return "\n".join(report) |
| 75 | + |
| 76 | + return execute_python_code |
0 commit comments