changing Code_Interpreter root directory to `Tools/Filesystem` (same as File_System and Shell_Command tools)
Browse files
Modules/Code_Interpreter.py
CHANGED
|
@@ -1,17 +1,19 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
| 3 |
import sys
|
| 4 |
from io import StringIO
|
| 5 |
from typing import Annotated
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from ._docstrings import autodoc
|
|
|
|
| 9 |
|
| 10 |
from app import _log_call_end, _log_call_start, _truncate_for_log
|
| 11 |
|
| 12 |
# Single source of truth for the LLM-facing tool description
|
| 13 |
TOOL_SUMMARY = (
|
| 14 |
-
"Execute Python code; returns captured stdout or the exception text."
|
| 15 |
)
|
| 16 |
|
| 17 |
|
|
@@ -25,14 +27,20 @@ def Code_Interpreter(code: Annotated[str, "Python source code to run; stdout is
|
|
| 25 |
_log_call_end("Code_Interpreter", result)
|
| 26 |
return result
|
| 27 |
old_stdout = sys.stdout
|
|
|
|
| 28 |
redirected_output = sys.stdout = StringIO()
|
| 29 |
try:
|
|
|
|
| 30 |
exec(code)
|
| 31 |
result = redirected_output.getvalue()
|
| 32 |
except Exception as exc: # pylint: disable=broad-except
|
| 33 |
result = str(exc)
|
| 34 |
finally:
|
| 35 |
sys.stdout = old_stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
_log_call_end("Code_Interpreter", _truncate_for_log(result))
|
| 37 |
return result
|
| 38 |
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
import sys
|
| 5 |
from io import StringIO
|
| 6 |
from typing import Annotated
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
from ._docstrings import autodoc
|
| 10 |
+
from .File_System import ROOT_DIR
|
| 11 |
|
| 12 |
from app import _log_call_end, _log_call_start, _truncate_for_log
|
| 13 |
|
| 14 |
# Single source of truth for the LLM-facing tool description
|
| 15 |
TOOL_SUMMARY = (
|
| 16 |
+
"Execute Python code from the tool root; returns captured stdout or the exception text."
|
| 17 |
)
|
| 18 |
|
| 19 |
|
|
|
|
| 27 |
_log_call_end("Code_Interpreter", result)
|
| 28 |
return result
|
| 29 |
old_stdout = sys.stdout
|
| 30 |
+
old_cwd = os.getcwd()
|
| 31 |
redirected_output = sys.stdout = StringIO()
|
| 32 |
try:
|
| 33 |
+
os.chdir(ROOT_DIR)
|
| 34 |
exec(code)
|
| 35 |
result = redirected_output.getvalue()
|
| 36 |
except Exception as exc: # pylint: disable=broad-except
|
| 37 |
result = str(exc)
|
| 38 |
finally:
|
| 39 |
sys.stdout = old_stdout
|
| 40 |
+
try:
|
| 41 |
+
os.chdir(old_cwd)
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
_log_call_end("Code_Interpreter", _truncate_for_log(result))
|
| 45 |
return result
|
| 46 |
|