Skip to content

Agent Zero AI framework

litellm fixes - message hoarding and missing user message

frdel73778cdd34d1
diff --git a/models.py b/models.py
index 77f48d14e..5e4317158 100644
--- a/models.py
+++ b/models.py
@@ -278,12 +278,14 @@ class LiteLLMChatWrapper(SimpleChatModel):
self,
system_message="",
user_message="",
- messages: List[BaseMessage] = [],
+ messages: List[BaseMessage]|None = None,
response_callback: Callable[[str, str], Awaitable[None]] | None = None,
reasoning_callback: Callable[[str, str], Awaitable[None]] | None = None,
tokens_callback: Callable[[str, int], Awaitable[None]] | None = None,
**kwargs: Any,
) -> Tuple[str, str]:
+ if not messages:
+ messages = []
# construct messages
if system_message:
messages.insert(0, SystemMessage(content=system_message))
diff --git a/python/extensions/message_loop_prompts_after/_50_recall_memories.py b/python/extensions/message_loop_prompts_after/_50_recall_memories.py
index 06084c07a..a7939c9a0 100644
--- a/python/extensions/message_loop_prompts_after/_50_recall_memories.py
+++ b/python/extensions/message_loop_prompts_after/_50_recall_memories.py
@@ -58,7 +58,7 @@ class RecallMemories(Extension):
query = await self.agent.call_utility_model(
system=system,
message=(
- loop_data.user_message.output_text() if loop_data.user_message else ""
+ loop_data.user_message.output_text() if loop_data.user_message else "None"
),
callback=log_callback,
)
diff --git a/python/extensions/message_loop_prompts_after/_51_recall_solutions.py b/python/extensions/message_loop_prompts_after/_51_recall_solutions.py
index 6aeed9a37..8275bb30d 100644
--- a/python/extensions/message_loop_prompts_after/_51_recall_solutions.py
+++ b/python/extensions/message_loop_prompts_after/_51_recall_solutions.py
@@ -56,7 +56,7 @@ class RecallSolutions(Extension):
# call util llm to summarize conversation
query = await self.agent.call_utility_model(
- system=system, message=loop_data.user_message.output_text() if loop_data.user_message else "", callback=log_callback
+ system=system, message=loop_data.user_message.output_text() if loop_data.user_message else "None", callback=log_callback
)
# get solutions database
diff --git a/python/extensions/monologue_start/_60_rename_chat.py b/python/extensions/monologue_start/_60_rename_chat.py
index 280d73e74..dba8de51c 100644
--- a/python/extensions/monologue_start/_60_rename_chat.py
+++ b/python/extensions/monologue_start/_60_rename_chat.py
@@ -13,7 +13,9 @@ class RenameChat(Extension):
try:
# prepare history
history_text = self.agent.history.output_text()
- ctx_length = int(self.agent.config.utility_model.ctx_length * 0.3)
+ ctx_length = min(
+ int(self.agent.config.utility_model.ctx_length * 0.7), 5000
+ )
history_text = tokens.trim_to_tokens(history_text, ctx_length, "start")
# prepare system and user prompt
system = self.agent.read_prompt("fw.rename_chat.sys.md")
diff --git a/python/helpers/shell_ssh.py b/python/helpers/shell_ssh.py
index 346daeb72..2d45f8f03 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=160, height=48)
+ self.shell = self.client.invoke_shell(width=80, height=40)
# self.shell.send(f'PS1="{SSHInteractiveSession.ps1_label}"'.encode())
# return
while True: # wait for end of initial output
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.py
index 657381092..572545d9e 100644
--- a/python/tools/code_execution_tool.py
+++ b/python/tools/code_execution_tool.py
@@ -135,19 +135,22 @@ 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}"
- return await self.terminal_session(session, command, reset)
+ 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}"
- return await self.terminal_session(session, command, reset)
+ 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
):
- return await self.terminal_session(session, command, reset)
+ 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):
+ 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
@@ -181,7 +184,7 @@ class CodeExecution(Tool):
PrintStyle(
background_color="white", font_color="#1B4F72", bold=True
).print(f"{self.agent.agent_name} code execution output")
- return await self.get_terminal_output(session)
+ return await self.get_terminal_output(session=session, prefix=prefix)
except Exception as e:
if i == 1:
@@ -192,6 +195,19 @@ class CodeExecution(Tool):
else:
raise e
+ def format_command_for_output(self, command: str):
+ # truncate long commands
+ short_cmd = command[:200]
+ # normalize whitespace for cleaner output
+ short_cmd = " ".join(short_cmd.split())
+ # replace any sequence of ', ", or ` with a single '
+ # short_cmd = re.sub(r"['\"`]+", "'", short_cmd) # no need anymore
+ # final length
+ short_cmd = truncate_text_string(short_cmd, 100)
+ return f"{short_cmd}"
+
+
+
async def get_terminal_output(
self,
session=0,
@@ -201,6 +217,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=""
):
# Common shell prompt regex patterns (add more as needed)
prompt_patterns = [
@@ -223,6 +240,10 @@ class CodeExecution(Tool):
truncated_output = ""
got_output = False
+ # if prefix, log right away
+ if prefix:
+ self.log.update(content=prefix)
+
while True:
await asyncio.sleep(sleep_time)
full_output, partial_output = await self.state.shells[session].read_output(
@@ -238,7 +259,7 @@ class CodeExecution(Tool):
# full_output += partial_output # Append new output
truncated_output = self.fix_full_output(full_output)
heading = self.get_heading_from_output(truncated_output, 0)
- self.log.update(content=truncated_output, heading=heading)
+ self.log.update(content=prefix + truncated_output, heading=heading)
last_output_time = now
got_output = True
@@ -270,7 +291,7 @@ class CodeExecution(Tool):
response = truncated_output + "\n\n" + response
PrintStyle.warning(sysinfo)
heading = self.get_heading_from_output(truncated_output, 0)
- self.log.update(content=response, heading=heading)
+ self.log.update(content=prefix + response, heading=heading)
return response
# Waiting for first output
@@ -281,7 +302,7 @@ class CodeExecution(Tool):
)
response = self.agent.read_prompt("fw.code.info.md", info=sysinfo)
PrintStyle.warning(sysinfo)
- self.log.update(content=response)
+ self.log.update(content=prefix + response)
return response
else:
# Waiting for more output after first output
@@ -294,7 +315,7 @@ class CodeExecution(Tool):
response = truncated_output + "\n\n" + response
PrintStyle.warning(sysinfo)
heading = self.get_heading_from_output(truncated_output, 0)
- self.log.update(content=response, heading=heading)
+ self.log.update(content=prefix + response, heading=heading)
return response
# potential dialog detection
@@ -322,7 +343,7 @@ class CodeExecution(Tool):
heading = self.get_heading_from_output(
truncated_output, 0
)
- self.log.update(content=response, heading=heading)
+ self.log.update(content=prefix + response, heading=heading)
return response
async def reset_terminal(self, session=0, reason: str | None = None):
diff --git a/webui/css/messages.css b/webui/css/messages.css
index 25d5592d3..9fbb01f0a 100644
--- a/webui/css/messages.css
+++ b/webui/css/messages.css
@@ -162,7 +162,7 @@
display: block;
width: 100%;
overflow-x: auto;
- margin-bottom: 1em;
+ padding-bottom: 1em;
}
.message-body .message-markdown-table-wrap table {
diff --git a/webui/js/messages.js b/webui/js/messages.js
index facd805f3..884caae86 100644
--- a/webui/js/messages.js
+++ b/webui/js/messages.js
@@ -826,20 +826,15 @@ function convertPathsToLinks(str) {
}
function adjustMarkdownRender(element) {
- // find all tables in the element
- const tables = element.querySelectorAll("table");
+ // find all tables and code blocks in the element
+ const elements = element.querySelectorAll("table, code");
- // wrap each table with a div with class message-markdown-table-wrap
- tables.forEach((table) => {
- // create wrapper div
+ // wrap each with a div with class message-markdown-table-wrap
+ elements.forEach((el) => {
const wrapper = document.createElement("div");
wrapper.className = "message-markdown-table-wrap";
-
- // insert wrapper before table in the DOM
- table.parentNode.insertBefore(wrapper, table);
-
- // move table into wrapper
- wrapper.appendChild(table);
+ el.parentNode.insertBefore(wrapper, el);
+ wrapper.appendChild(el);
});
}