better agent instructions, easier to understand for small models
Browse files- Modules/Agent_Terminal.py +43 -6
Modules/Agent_Terminal.py
CHANGED
|
@@ -2,8 +2,10 @@ 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
|
|
@@ -21,6 +23,19 @@ 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,
|
|
@@ -74,10 +89,7 @@ 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 |
|
|
@@ -85,7 +97,14 @@ TOOL_SUMMARY = (
|
|
| 85 |
@autodoc(
|
| 86 |
summary=TOOL_SUMMARY,
|
| 87 |
)
|
| 88 |
-
def Agent_Terminal(code: Annotated[str,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
_log_call_start("Agent_Terminal", code=_truncate_for_log(code or "", 300))
|
| 90 |
if code is None:
|
| 91 |
result = "No code provided."
|
|
@@ -112,13 +131,31 @@ def Agent_Terminal(code: Annotated[str, "Python source code to run; stdout is ca
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
result = redirected_output.getvalue()
|
| 123 |
except Exception as exc: # pylint: disable=broad-except
|
| 124 |
result = str(exc)
|
|
|
|
| 2 |
|
| 3 |
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
|
| 11 |
from ._docstrings import autodoc
|
|
|
|
| 23 |
|
| 24 |
from app import _log_call_end, _log_call_start, _truncate_for_log
|
| 25 |
|
| 26 |
+
def search_packages(query: str = "") -> str:
|
| 27 |
+
"""Search for installed Python packages by name. If query is empty, lists all."""
|
| 28 |
+
packages = []
|
| 29 |
+
query = query.lower()
|
| 30 |
+
for dist in importlib.metadata.distributions():
|
| 31 |
+
name = dist.metadata['Name']
|
| 32 |
+
if query in name.lower():
|
| 33 |
+
packages.append(f"{name} ({dist.version})")
|
| 34 |
+
packages.sort()
|
| 35 |
+
if not packages:
|
| 36 |
+
return f"No packages found matching '{query}'."
|
| 37 |
+
return "\n".join(packages)
|
| 38 |
+
|
| 39 |
def _get_tools_map():
|
| 40 |
return {
|
| 41 |
"Web_Fetch": Web_Fetch,
|
|
|
|
| 89 |
"Executes Python code as the unified interface for the entire tools ecosystem. "
|
| 90 |
"All tool interactions must happen through this code-execution gateway. "
|
| 91 |
"Use Agent Terminal repeatedly whenever you need to chain or combine tool operations. "
|
| 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 |
|
|
|
|
| 97 |
@autodoc(
|
| 98 |
summary=TOOL_SUMMARY,
|
| 99 |
)
|
| 100 |
+
def Agent_Terminal(code: Annotated[str, (
|
| 101 |
+
"Python source code to run; stdout is captured and returned. "
|
| 102 |
+
"Execute these commands: "
|
| 103 |
+
"`search_tools('query')` to search for tools by name or capability; "
|
| 104 |
+
"`list_tools()` to list all available tools; "
|
| 105 |
+
"`usage('ToolName')` to inspect a tool’s expected input parameters; "
|
| 106 |
+
"`search_packages('query')` to search for installed Python libraries."
|
| 107 |
+
)]) -> str:
|
| 108 |
_log_call_start("Agent_Terminal", code=_truncate_for_log(code or "", 300))
|
| 109 |
if code is None:
|
| 110 |
result = "No code provided."
|
|
|
|
| 131 |
"list_tools": list_tools,
|
| 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 |
|
| 139 |
try:
|
| 140 |
os.chdir(ROOT_DIR)
|
| 141 |
+
|
| 142 |
+
# Parse code to check if the last statement is an expression
|
| 143 |
+
tree = ast.parse(code)
|
| 144 |
+
if tree.body and isinstance(tree.body[-1], ast.Expr):
|
| 145 |
+
last_node = tree.body.pop()
|
| 146 |
+
|
| 147 |
+
# Execute preceding statements
|
| 148 |
+
if tree.body:
|
| 149 |
+
exec(compile(tree, filename="<string>", mode="exec"), tools_env)
|
| 150 |
+
|
| 151 |
+
# Evaluate and print the last expression
|
| 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 |
+
|
| 159 |
result = redirected_output.getvalue()
|
| 160 |
except Exception as exc: # pylint: disable=broad-except
|
| 161 |
result = str(exc)
|