Nymbo commited on
Commit
b97b64f
·
verified ·
1 Parent(s): e5e1de3

Update Modules/Agent_Terminal.py

Browse files
Files changed (1) hide show
  1. Modules/Agent_Terminal.py +18 -4
Modules/Agent_Terminal.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import sys
5
  import ast
6
  from io import StringIO
7
- from typing import Annotated
8
  import importlib.metadata
9
 
10
  import gradio as gr
@@ -92,7 +92,17 @@ TOOL_SUMMARY = (
92
  "Available tools: `Web_Fetch`, `Web_Search`, `Code_Interpreter`, `Shell_Command`, `File_System`, `Obsidian_Vault`, `Memory_Manager`, `Generate_Speech`, `Generate_Image`, `Generate_Video`, `Deep_Research`."
93
  )
94
 
95
-
 
 
 
 
 
 
 
 
 
 
96
 
97
  @autodoc(
98
  summary=TOOL_SUMMARY,
@@ -114,6 +124,10 @@ def Agent_Terminal(code: Annotated[str, (
114
  old_cwd = os.getcwd()
115
  redirected_output = sys.stdout = StringIO()
116
 
 
 
 
 
117
  # Prepare the execution environment with all tools
118
  tools_env = {
119
  "Web_Fetch": Web_Fetch,
@@ -132,7 +146,7 @@ def Agent_Terminal(code: Annotated[str, (
132
  "search_tools": search_tools,
133
  "usage": usage,
134
  "search_packages": search_packages,
135
- "print": print, # Ensure print is available
136
  "__builtins__": __builtins__,
137
  }
138
 
@@ -152,7 +166,7 @@ def Agent_Terminal(code: Annotated[str, (
152
  expr = compile(ast.Expression(last_node.value), filename="<string>", mode="eval")
153
  result_val = eval(expr, tools_env)
154
  if result_val is not None:
155
- print(result_val)
156
  else:
157
  exec(code, tools_env)
158
 
 
4
  import sys
5
  import ast
6
  from io import StringIO
7
+ from typing import Annotated, Any
8
  import importlib.metadata
9
 
10
  import gradio as gr
 
92
  "Available tools: `Web_Fetch`, `Web_Search`, `Code_Interpreter`, `Shell_Command`, `File_System`, `Obsidian_Vault`, `Memory_Manager`, `Generate_Speech`, `Generate_Image`, `Generate_Video`, `Deep_Research`."
93
  )
94
 
95
+ def _convert_to_url_if_path(val: Any) -> Any:
96
+ if isinstance(val, str) and os.path.exists(val) and os.path.isfile(val):
97
+ try:
98
+ abs_path = os.path.abspath(val)
99
+ # Normalize slashes
100
+ return f"/file={abs_path.replace('\\', '/')}"
101
+ except Exception:
102
+ return val
103
+ elif isinstance(val, (list, tuple)):
104
+ return type(val)(_convert_to_url_if_path(v) for v in val)
105
+ return val
106
 
107
  @autodoc(
108
  summary=TOOL_SUMMARY,
 
124
  old_cwd = os.getcwd()
125
  redirected_output = sys.stdout = StringIO()
126
 
127
+ def custom_print(*args, **kwargs):
128
+ new_args = [_convert_to_url_if_path(arg) for arg in args]
129
+ print(*new_args, **kwargs)
130
+
131
  # Prepare the execution environment with all tools
132
  tools_env = {
133
  "Web_Fetch": Web_Fetch,
 
146
  "search_tools": search_tools,
147
  "usage": usage,
148
  "search_packages": search_packages,
149
+ "print": custom_print, # Ensure print is available
150
  "__builtins__": __builtins__,
151
  }
152
 
 
166
  expr = compile(ast.Expression(last_node.value), filename="<string>", mode="eval")
167
  result_val = eval(expr, tools_env)
168
  if result_val is not None:
169
+ custom_print(result_val)
170
  else:
171
  exec(code, tools_env)
172