Skip to content

Agent Zero AI framework

shell improvements

frdel07e3e59e6f96
diff --git a/python/helpers/files.py b/python/helpers/files.py
index 97cc31a40..9d0cb6f52 100644
--- a/python/helpers/files.py
+++ b/python/helpers/files.py
@@ -12,7 +12,6 @@ import zipfile
import importlib
import importlib.util
import inspect
-from python.helpers.print_style import PrintStyle
class VariablesPlugin(ABC):
diff --git a/python/helpers/shell_ssh.py b/python/helpers/shell_ssh.py
index 2d45f8f03..160ccaa57 100644
--- a/python/helpers/shell_ssh.py
+++ b/python/helpers/shell_ssh.py
@@ -41,7 +41,7 @@ class SSHInteractiveSession:
allow_agent=False,
look_for_keys=False,
)
- self.shell = self.client.invoke_shell(width=80, height=40)
+ self.shell = self.client.invoke_shell(width=100, height=50)
# self.shell.send(f'PS1="{SSHInteractiveSession.ps1_label}"'.encode())
# return
while True: # wait for end of initial output
@@ -193,6 +193,9 @@ class SSHInteractiveSession:
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
cleaned = ansi_escape.sub("", input_string)
+ # remove null bytes
+ cleaned = cleaned.replace("\x00", "")
+
# Replace '\r\n' with '\n'
cleaned = cleaned.replace("\r\n", "\n")
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.py
index 572545d9e..8331bed3f 100644
--- a/python/tools/code_execution_tool.py
+++ b/python/tools/code_execution_tool.py
@@ -73,7 +73,9 @@ class CodeExecution(Tool):
if not text:
text = f"{self.name} - {self.args['runtime']}"
text = truncate_text_string(text, 60)
- return f"icon://terminal {text}"
+ session = self.args.get("session", None)
+ session_text = f"[{session}] " if session or session == 0 else ""
+ return f"icon://terminal {session_text}{text}"
async def after_execution(self, response, **kwargs):
self.agent.hist_add_tool_result(self.name, response.message)
@@ -135,22 +137,24 @@ class CodeExecution(Tool):
async def execute_python_code(self, session: int, code: str, reset: bool = False):
escaped_code = shlex.quote(code)
command = f"ipython -c {escaped_code}"
- prefix = "python> "+self.format_command_for_output(code)+"\n\n"
+ prefix = "python> " + self.format_command_for_output(code) + "\n\n"
return await self.terminal_session(session, command, reset, prefix)
async def execute_nodejs_code(self, session: int, code: str, reset: bool = False):
escaped_code = shlex.quote(code)
- command = f"node /exe/node_eval.js {escaped_code}"
- prefix = "node> "+self.format_command_for_output(code)+"\n\n"
+ command = f"node /exe/node_eval.js {escaped_code}"
+ prefix = "node> " + self.format_command_for_output(code) + "\n\n"
return await self.terminal_session(session, command, reset, prefix)
async def execute_terminal_command(
self, session: int, command: str, reset: bool = False
):
- prefix = "bash> "+self.format_command_for_output(command)+"\n\n"
+ prefix = "bash> " + self.format_command_for_output(command) + "\n\n"
return await self.terminal_session(session, command, reset, prefix)
- async def terminal_session(self, session: int, command: str, reset: bool = False, prefix: str = ""):
+ async def terminal_session(
+ self, session: int, command: str, reset: bool = False, prefix: str = ""
+ ):
await self.agent.handle_intervention() # wait for intervention and handle it, if paused
# try again on lost connection
@@ -205,8 +209,6 @@ class CodeExecution(Tool):
# final length
short_cmd = truncate_text_string(short_cmd, 100)
return f"{short_cmd}"
-
-
async def get_terminal_output(
self,
@@ -217,7 +219,7 @@ class CodeExecution(Tool):
dialog_timeout=5, # potential dialog detection timeout
max_exec_timeout=180, # hard cap on total runtime
sleep_time=0.1,
- prefix=""
+ prefix="",
):
# Common shell prompt regex patterns (add more as needed)
prompt_patterns = [
@@ -343,7 +345,9 @@ class CodeExecution(Tool):
heading = self.get_heading_from_output(
truncated_output, 0
)
- self.log.update(content=prefix + response, heading=heading)
+ self.log.update(
+ content=prefix + response, heading=heading
+ )
return response
async def reset_terminal(self, session=0, reason: str | None = None):
@@ -384,7 +388,7 @@ class CodeExecution(Tool):
def fix_full_output(self, output: str):
# remove any single byte \xXX escapes
- output = re.sub(r'(?<!\\)\\x[0-9A-Fa-f]{2}', '', output)
+ output = re.sub(r"(?<!\\)\\x[0-9A-Fa-f]{2}", "", output)
# Strip every line of output before truncation
output = "\n".join(line.strip() for line in output.splitlines())
output = truncate_text_agent(agent=self.agent, output=output, threshold=10000)
diff --git a/test.py b/test.py
deleted file mode 100644
index 913449b44..000000000
--- a/test.py
+++ /dev/null
@@ -1,74 +0,0 @@
-import asyncio
-from os import sep
-from langchain_core.messages import HumanMessage, SystemMessage
-from langchain_core.prompts import ChatPromptTemplate
-import models
-from python.helpers import dotenv
-
-
-async def test():
-
- dotenv.load_dotenv()
-
- # model_name = "moonshotai/kimi-dev-72b:free"
- # model_name = "qwen/qwq-32b"
- # model_name = "qwen/qwen3-32b"
- # model_name = "anthropic/claude-3.7-sonnet:thinking"
- model_name = "openai/gpt-4.1-nano"
- system = ""
- message = "hello"
-
- model = models.get_chat_model(models.ModelProvider.OPENROUTER, model_name)
-
- async def response_callback(chunk: str, full: str):
- if chunk == full:
- print("\n")
- print("Response:")
- print(chunk, end="", flush=True)
-
- async def reasoning_callback(chunk: str, full: str):
- if chunk == full:
- print("\n")
- print("Reasoning:")
- print(chunk, end="", flush=True)
-
- response, reasoning = await model.unified_call(
- system_message=system,
- user_message=message,
- response_callback=response_callback,
- reasoning_callback=reasoning_callback,
- )
-
- print("\n")
- print("Final:")
- print("Reasoning:", reasoning)
- print("Response:", response)
-
-
-async def test2():
-
- dotenv.load_dotenv()
-
- import initialize
- config = initialize.initialize_agent()
-
- model = models.get_browser_model(
- provider=config.browser_model.provider,
- name=config.browser_model.name,
- **config.browser_model.kwargs,
- )
-
- response, reasoning = await model.unified_call(
- system_message="",
- user_message="hi",
- )
-
- print("\n")
- print("Final:")
- print("Reasoning:", reasoning)
- print("Response:", response)
-
-
-if __name__ == "__main__":
- # asyncio.run(test())
- asyncio.run(test2())
\ No newline at end of file