Nymbo commited on
Commit
5d58591
·
verified ·
1 Parent(s): cba5017

Create Agent_Terminal.py

Browse files
Files changed (1) hide show
  1. Modules/Agent_Terminal.py +147 -0
Modules/Agent_Terminal.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, File_System
11
+ from .Web_Fetch import Web_Fetch
12
+ from .Web_Search import Web_Search
13
+ from .Memory_Manager import Memory_Manager
14
+ from .Generate_Speech import Generate_Speech, List_Kokoro_Voices
15
+ from .Generate_Image import Generate_Image
16
+ from .Generate_Video import Generate_Video
17
+ from .Deep_Research import Deep_Research
18
+ from .Obsidian_Vault import Obsidian_Vault
19
+ from .Shell_Command import Shell_Command
20
+ from .Code_Interpreter import Code_Interpreter
21
+
22
+ from app import _log_call_end, _log_call_start, _truncate_for_log
23
+
24
+ def _get_tools_map():
25
+ return {
26
+ "Web_Fetch": Web_Fetch,
27
+ "Web_Search": Web_Search,
28
+ "Memory_Manager": Memory_Manager,
29
+ "Generate_Speech": Generate_Speech,
30
+ "List_Kokoro_Voices": List_Kokoro_Voices,
31
+ "Generate_Image": Generate_Image,
32
+ "Generate_Video": Generate_Video,
33
+ "Deep_Research": Deep_Research,
34
+ "File_System": File_System,
35
+ "Obsidian_Vault": Obsidian_Vault,
36
+ "Shell_Command": Shell_Command,
37
+ "Code_Interpreter": Code_Interpreter,
38
+ }
39
+
40
+ def list_tools() -> list[str]:
41
+ """List all available tools in the Code Interpreter environment."""
42
+ return list(_get_tools_map().keys())
43
+
44
+ def search_tools(query: str) -> str:
45
+ """Search for tools by name or description. Returns usage info for matches."""
46
+ query = query.lower()
47
+ matches = []
48
+ tools = _get_tools_map()
49
+ for name, func in tools.items():
50
+ doc = (func.__doc__ or "").lower()
51
+ if query in name.lower() or query in doc:
52
+ matches.append((name, func))
53
+
54
+ if not matches:
55
+ return f"No tools found matching '{query}'."
56
+
57
+ output = []
58
+ for name, func in matches:
59
+ output.append(f"--- {name} ---")
60
+ output.append(func.__doc__ or "No documentation available.")
61
+ output.append("")
62
+ return "\n".join(output)
63
+
64
+ def usage(tool_name: str) -> str:
65
+ """Get detailed usage information for a specific tool."""
66
+ tools = _get_tools_map()
67
+ if tool_name not in tools:
68
+ return f"Tool '{tool_name}' not found. Available tools: {', '.join(tools.keys())}"
69
+ func = tools[tool_name]
70
+ return f"--- {tool_name} ---\n{func.__doc__ or 'No documentation available.'}"
71
+
72
+ # Single source of truth for the LLM-facing tool description
73
+ TOOL_SUMMARY = (
74
+ "Executes Python code as the unified interface for the entire tools ecosystem. "
75
+ "All tool interactions must happen through this code-execution gateway. "
76
+ "Use Agent Terminal repeatedly whenever you need to chain or combine tool operations. "
77
+ "Available tools: `Web_Fetch`, `Web_Search`, `Code_Interpreter`, `Shell_Command`, `File_System`, `Obsidian_Vault`, `Memory_Manager`, `Generate_Speech`, `Generate_Image`, `Generate_Video`, `Deep_Research`. "
78
+ "Helpful commands: "
79
+ "`print(search_tools('query'))` to search for tools by name or capability; "
80
+ "`print(usage('ToolName'))` to inspect a tool’s expected input parameters."
81
+ )
82
+
83
+
84
+
85
+ @autodoc(
86
+ summary=TOOL_SUMMARY,
87
+ )
88
+ def Agent_Terminal(code: Annotated[str, "Python source code to run; stdout is captured and returned."]) -> str:
89
+ _log_call_start("Agent_Terminal", code=_truncate_for_log(code or "", 300))
90
+ if code is None:
91
+ result = "No code provided."
92
+ _log_call_end("Agent_Terminal", result)
93
+ return result
94
+ old_stdout = sys.stdout
95
+ old_cwd = os.getcwd()
96
+ redirected_output = sys.stdout = StringIO()
97
+
98
+ # Prepare the execution environment with all tools
99
+ tools_env = {
100
+ "Web_Fetch": Web_Fetch,
101
+ "Web_Search": Web_Search,
102
+ "Memory_Manager": Memory_Manager,
103
+ "Generate_Speech": Generate_Speech,
104
+ "List_Kokoro_Voices": List_Kokoro_Voices,
105
+ "Generate_Image": Generate_Image,
106
+ "Generate_Video": Generate_Video,
107
+ "Deep_Research": Deep_Research,
108
+ "File_System": File_System,
109
+ "Obsidian_Vault": Obsidian_Vault,
110
+ "Shell_Command": Shell_Command,
111
+ "Code_Interpreter": Code_Interpreter,
112
+ "list_tools": list_tools,
113
+ "search_tools": search_tools,
114
+ "usage": usage,
115
+ "print": print, # Ensure print is available
116
+ "__builtins__": __builtins__,
117
+ }
118
+
119
+ try:
120
+ os.chdir(ROOT_DIR)
121
+ exec(code, tools_env)
122
+ result = redirected_output.getvalue()
123
+ except Exception as exc: # pylint: disable=broad-except
124
+ result = str(exc)
125
+ finally:
126
+ sys.stdout = old_stdout
127
+ try:
128
+ os.chdir(old_cwd)
129
+ except Exception:
130
+ pass
131
+ _log_call_end("Agent_Terminal", _truncate_for_log(result))
132
+ return result
133
+
134
+
135
+ def build_interface() -> gr.Interface:
136
+ return gr.Interface(
137
+ fn=Agent_Terminal,
138
+ inputs=gr.Code(label="Python Code", language="python"),
139
+ outputs=gr.Textbox(label="Output", lines=5, max_lines=20),
140
+ title="Agent Terminal",
141
+ description="<div style=\"text-align:center\">Interact with all other tools via a Python API. Reduces token usage by 90%.</div>",
142
+ api_description=TOOL_SUMMARY,
143
+ flagging_mode="never",
144
+ )
145
+
146
+
147
+ __all__ = ["Agent_Terminal", "build_interface"]