Agent Zero AI framework
attachments, files, prompt extras, prompt caching, refactors, cleanups
frdel19f50d6d9509
diff --git a/agent.py b/agent.pyindex de41e097d..4e218d2e8 100644--- a/agent.py+++ b/agent.py@@ -1,4 +1,5 @@import asyncio+from collections import OrderedDictfrom dataclasses import dataclass, fieldimport time, importlib, inspect, os, jsonfrom typing import Any, Optional, Dict, TypedDict@@ -73,7 +74,7 @@ class AgentContext:self.streaming_agent = Noneself.paused = False- def communicate(self, msg: str, broadcast_level: int = 1):+ def communicate(self, msg: "UserMessage", broadcast_level: int = 1):self.paused = False # unpause if pausedif self.streaming_agent:@@ -85,9 +86,11 @@ class AgentContext:# set intervention messages to agent(s):intervention_agent = current_agentwhile intervention_agent and broadcast_level != 0:- intervention_agent.intervention_message = msg+ intervention_agent.intervention = msgbroadcast_level -= 1- intervention_agent = intervention_agent.data.get(Agent.DATA_NAME_SUPERIOR, None)+ intervention_agent = intervention_agent.data.get(+ Agent.DATA_NAME_SUPERIOR, None+ )else:# self.process = DeferredTask(current_agent.monologue, msg)@@ -96,13 +99,13 @@ class AgentContext:return self.process# this wrapper ensures that superior agents are called back if the chat was loaded from file and original callstack is gone- async def _process_chain(self, agent: "Agent", msg: str, user=True):+ async def _process_chain(self, agent: "Agent", msg: "UserMessage|str", user=True):try:msg_template = (- await agent.hist_add_user_message(msg)+ await agent.hist_add_user_message(msg) # type: ignoreif userelse await agent.hist_add_tool_result(- tool_name="call_subordinate", tool_result=msg+ tool_name="call_subordinate", tool_result=msg # type: ignore))response = await agent.monologue()@@ -132,33 +135,40 @@ class AgentConfig:response_timeout_seconds: int = 60max_tool_response_length: int = 3000code_exec_docker_enabled: bool = True- code_exec_docker_name: str = "agent-zero-exe"- code_exec_docker_image: str = "frdel/agent-zero-exe:latest"+ code_exec_docker_name: str = "agent-zero-dev"+ code_exec_docker_image: str = "frdel/agent-zero-run:development"code_exec_docker_ports: dict[str, int] = field(- default_factory=lambda: {"22/tcp": 50022}+ default_factory=lambda: {"22/tcp": 55022, "80/tcp": 55080})code_exec_docker_volumes: dict[str, dict[str, str]] = field(default_factory=lambda: {+ files.get_base_dir(): {"bind": "/a0", "mode": "rw"},files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"},- files.get_abs_path("instruments"): {"bind": "/instruments", "mode": "rw"},})code_exec_ssh_enabled: bool = Truecode_exec_ssh_addr: str = "localhost"- code_exec_ssh_port: int = 50022+ code_exec_ssh_port: int = 55022code_exec_ssh_user: str = "root"code_exec_ssh_pass: str = "toor"additional: Dict[str, Any] = field(default_factory=dict)+@dataclass+class UserMessage:+ message: str+ attachments: list[str]++class LoopData:def __init__(self, **kwargs):self.iteration = -1self.system = []self.user_message: history.Message | None = Noneself.history_output: list[history.OutputMessage] = []+ self.extras_temporary: OrderedDict[str, history.MessageContent] = OrderedDict()+ self.extras_persistent: OrderedDict[str, history.MessageContent] = OrderedDict()self.last_response = ""- self.attachments = [] # Add attachments field# override values with kwargsfor key, value in kwargs.items():@@ -201,7 +211,7 @@ class Agent:self.history = history.History(self)self.last_user_message: history.Message | None = None- self.intervention_message = ""+ self.intervention: UserMessage | None = Noneself.rate_limiter = rate_limiter.RateLimiter(self.context.log,max_calls=self.config.rate_limit_requests,@@ -221,18 +231,6 @@ class Agent:printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)- # # Include attachments in user message if available- # if loop_data.attachments:- # user_message += "\n" + "\n".join(- # loop_data.attachments- # ) # Add attachments to message- # loop_data.attachments = (- # []- # ) # Clear attachments after adding to message- # TODO attachments to extension-- # await self.hist_add_user_message(message=self.loop_data.message)-# let the agent run message loop until he stops it with a response toolwhile True:@@ -241,34 +239,13 @@ class Agent:self.loop_data.iteration += 1try:-- # set system prompt and message history- self.loop_data.system = []- self.loop_data.history_output = self.history.output()-- # and allow extensions to edit them- await self.call_extensions(- "message_loop_prompts", loop_data=self.loop_data- )-- # build chain from system prompt, message history and model- prompt = ChatPromptTemplate.from_messages(- [- SystemMessage(- content="\n\n".join(self.loop_data.system)- ),- MessagesPlaceholder(variable_name="messages"),- ]- )- chain = prompt | self.config.chat_model-- # convert history to LLM format- history_langchain = history.output_langchain(- self.loop_data.history_output+ # prepare LLM chain (model, system, history)+ chain, prompt = await self.prepare_chain(+ loop_data=self.loop_data)# rate limiter TODO - move to extension, make per-model- formatted_inputs = prompt.format(messages=history_langchain)+ formatted_inputs = prompt.format()self.set_data(self.DATA_NAME_CTX_WINDOW, formatted_inputs)token_count = tokens.approximate_tokens(formatted_inputs)self.rate_limiter.limit_call_and_input(token_count)@@ -284,9 +261,7 @@ class Agent:type="agent", heading=f"{self.agent_name}: Generating")- async for chunk in chain.astream(- {"messages": history_langchain}- ):+ async for chunk in chain.astream({}):# wait for intervention and handle it, if pausedawait self.handle_intervention(agent_response)@@ -360,6 +335,40 @@ class Agent:# call monologue_end extensionsawait self.call_extensions("monologue_end", loop_data=self.loop_data) # type: ignore+ async def prepare_chain(self, loop_data: LoopData):+ # set system prompt and message history+ loop_data.system = await self.get_system_prompt(self.loop_data)+ loop_data.history_output = self.history.output()++ # and allow extensions to edit them+ await self.call_extensions("message_loop_prompts", loop_data=loop_data)++ # extras (memory etc.)+ extras: list[history.OutputMessage] = []+ for extra in loop_data.extras_persistent.values():+ extras += history.Message(False, content=extra).output()+ for extra in loop_data.extras_temporary.values():+ extras += history.Message(False, content=extra).output()+ loop_data.extras_temporary.clear()++ # combine history and extras+ history_combined = history.group_outputs_abab(loop_data.history_output + extras)++ # convert history to LLM format+ history_langchain = history.output_langchain(history_combined)++ # build chain from system prompt, message history and model+ prompt = ChatPromptTemplate.from_messages(+ [+ SystemMessage(content="\n\n".join(loop_data.system)),+ *history_langchain,+ ]+ )++ # return callable chain+ chain = prompt | self.config.chat_model+ return chain, prompt+def handle_critical_exception(self, exception: Exception):if isinstance(exception, HandledException):raise exception # Re-raise the exception to kill the loop@@ -378,7 +387,14 @@ class Agent:self.context.log.log(type="error", content=error_message)raise HandledException(exception) # Re-raise the exception to kill the loop- def parse_prompt(self, file: str, **kwargs) -> tuple[list, dict]:+ async def get_system_prompt(self, loop_data: LoopData) -> list[str]:+ system_prompt = []+ await self.call_extensions(+ "system_prompt", system_prompt=system_prompt, loop_data=loop_data+ )+ return system_prompt++ def parse_prompt(self, file: str, **kwargs):prompt_dir = files.get_abs_path("prompts/default")backup_dir = []if (@@ -389,12 +405,7 @@ class Agent:prompt = files.parse_file(files.get_abs_path(prompt_dir, file), _backup_dirs=backup_dir, **kwargs)- if isinstance(prompt, dict):- return [], prompt- elif isinstance(prompt, list):- return prompt, {}- else:- return [prompt], {}+ return promptdef read_prompt(self, file: str, **kwargs) -> str:prompt_dir = files.get_abs_path("prompts/default")@@ -416,31 +427,55 @@ class Agent:def set_data(self, field: str, value):self.data[field] = value- async def hist_add_user_message(self, message: str, intervention: bool = False):+ def hist_add_message(self, ai: bool, content: history.MessageContent):+ return self.history.add_message(ai=ai, content=content)++ async def hist_add_user_message(+ self, message: UserMessage, intervention: bool = False+ ):self.history.new_topic() # user message starts a new topic in history++ # load message template based on interventionif intervention:- args, kwargs = self.parse_prompt("fw.intervention.md", message=message)- msg = self.history.add_message(False, *args, **kwargs)+ content = self.parse_prompt(+ "fw.intervention.md",+ message=message.message,+ attachments=message.attachments,+ )else:- args, kwargs = self.parse_prompt("fw.user_message.md", message=message)- msg = self.history.add_message(False, *args, **kwargs)+ content = self.parse_prompt(+ "fw.user_message.md",+ message=message.message,+ attachments=message.attachments,+ )++ # remove empty attachments from template+ if (+ isinstance(content, dict)+ and "attachments" in content+ and not content["attachments"]+ ):+ del content["attachments"]++ # add to history+ msg = self.hist_add_message(False, content=content) # type: ignoreself.last_user_message = msgreturn msgasync def hist_add_ai_response(self, message: str):self.loop_data.last_response = message- args, kwargs = self.parse_prompt("fw.ai_response.md", message=message)- return self.history.add_message(True, *args, **kwargs)+ content = self.parse_prompt("fw.ai_response.md", message=message)+ return self.hist_add_message(True, content=content)- async def hist_add_warning(self, message: str):- args, kwargs = self.parse_prompt("fw.warning.md", message=message)- return self.history.add_message(False, *args, **kwargs)+ async def hist_add_warning(self, message: history.MessageContent):+ content = self.parse_prompt("fw.warning.md", message=message)+ return self.hist_add_message(False, content=content)async def hist_add_tool_result(self, tool_name: str, tool_result: str):- args, kwargs = self.parse_prompt(+ content = self.parse_prompt("fw.tool_result.md", tool_name=tool_name, tool_result=tool_result)- return self.history.add_message(False, *args, **kwargs)+ return self.hist_add_message(False, content=content)def concat_messages(self, messages@@ -480,63 +515,14 @@ class Agent:return response- async def replace_middle_messages(self, middle_messages):- cleanup_prompt = self.read_prompt("fw.msg_cleanup.md")- log_item = self.context.log.log(- type="util", heading="Mid messages cleanup summary"- )-- PrintStyle(- bold=True, font_color="orange", padding=True, background_color="white"- ).print(f"{self.agent_name}: Mid messages cleanup summary")- printer = PrintStyle(italic=True, font_color="orange", padding=False)-- def log_callback(content):- printer.stream(content)- log_item.stream(content=content)-- summary = await self.call_utility_llm(- system=cleanup_prompt,- msg=self.concat_messages(middle_messages),- callback=log_callback,- )- new_human_message = HumanMessage(content=summary)- return [new_human_message]-- async def cleanup_history(self, max: int, keep_start: int, keep_end: int):- # if len(self.history) <= max:- # return self.history-- # first_x = self.history[:keep_start]- # last_y = self.history[-keep_end:]-- # # Identify the middle part- # middle_part = self.history[keep_start:-keep_end]-- # # Ensure the first message in the middle is "human", if not, move one message back- # if middle_part and middle_part[0].type != "human":- # if len(first_x) > 0:- # middle_part.insert(0, first_x.pop())-- # # Ensure the middle part has an odd number of messages- # if len(middle_part) % 2 == 0:- # middle_part = middle_part[:-1]-- # # Replace the middle part using the replacement function- # new_middle_part = await self.replace_middle_messages(middle_part)-- # self.history = first_x + new_middle_part + last_y-- return self.history-async def handle_intervention(self, progress: str = ""):while self.context.paused:await asyncio.sleep(0.1) # wait if pausedif (- self.intervention_message+ self.intervention): # if there is an intervention message, but not yet processed- msg = self.intervention_message- self.intervention_message = "" # reset the intervention message+ msg = self.intervention+ self.intervention = None # reset the intervention messageif progress.strip():await self.hist_add_ai_response(progress)# append the intervention messagediff --git a/docker/run/Dockerfile b/docker/run/Dockerfileindex 73bea5c89..a640f4aa6 100644--- a/docker/run/Dockerfile+++ b/docker/run/Dockerfile@@ -42,8 +42,7 @@ RUN rm -rf /var/lib/apt/lists/*RUN apt-get clean# Expose ports-# EXPOSE 22-EXPOSE 80+EXPOSE 22 80# initialize runtime-CMD ["/bin/bash", "/exe/initialize.sh"]\ No newline at end of file+CMD ["/bin/bash", "/exe/initialize.sh", "$BRANCH"]\ No newline at end of filediff --git a/docker/run/fs/exe/initialize.sh b/docker/run/fs/exe/initialize.shindex 993379053..86a5be8e8 100644--- a/docker/run/fs/exe/initialize.sh+++ b/docker/run/fs/exe/initialize.sh@@ -1,5 +1,12 @@#!/bin/bash+# branch from parameter+if [ -z "$1" ]; then+ echo "Error: Branch parameter is empty. Please provide a valid branch name."+ exit 1+fi+BRANCH="$1"+# Copy all contents from persistent /per to root directory (/) without overwritingcp -r --no-preserve=ownership,mode /per/* /@@ -11,7 +18,11 @@ chmod 444 /root/.profileapt-get update &# Start SSH service in background-/usr/sbin/sshd -D &+if [ "$BRANCH" != "development" ]; then+ /usr/sbin/sshd -D -o ListenAddress=127.0.0.1 &+else+ /usr/sbin/sshd -D &+fi# Start searxng server in backgroundsudo -H -u searxng -i bash /exe/run_searxng.sh &diff --git a/initialize.py b/initialize.pyindex 24e84aa67..5f19e38b8 100644--- a/initialize.py+++ b/initialize.py@@ -55,16 +55,16 @@ def initialize():max_tool_response_length=3000,# response_timeout_seconds = 60,# code_exec_docker_enabled = True,- # code_exec_docker_name = "agent-zero-exe",- # code_exec_docker_image = "frdel/agent-zero-exe:latest",- # code_exec_docker_ports = { "22/tcp": 50022 }+ # code_exec_docker_name = "agent-zero-dev",+ # code_exec_docker_image = "frdel/agent-zero-run:development",+ # code_exec_docker_ports = { "22/tcp": 55022, "80/tcp": 55080 }# code_exec_docker_volumes = {- # files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"},- # files.get_abs_path("instruments"): {"bind": "/instruments", "mode": "rw"},- # },+ # files.get_base_dir(): {"bind": "/a0", "mode": "rw"},+ # files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"},+ # },# code_exec_ssh_enabled = True,# code_exec_ssh_addr = "localhost",- # code_exec_ssh_port = 50022,+ # code_exec_ssh_port = 55022,# code_exec_ssh_user = "root",# code_exec_ssh_pass = "toor",# additional = {},diff --git a/prompts/default/fw.intervention.md b/prompts/default/fw.intervention.mdindex aeca71e4f..78488abf0 100644--- a/prompts/default/fw.intervention.md+++ b/prompts/default/fw.intervention.md@@ -1,5 +1,6 @@```json{- "user_intervention": {{message}}+ "user_intervention": {{message}},+ "attachments": {{attachments}}}```\ No newline at end of filediff --git a/prompts/default/fw.user_message.md b/prompts/default/fw.user_message.mdindex ff111f6fe..5c7ec0d17 100644--- a/prompts/default/fw.user_message.md+++ b/prompts/default/fw.user_message.md@@ -1,5 +1,6 @@```json{- "user_message": {{message}}+ "user_message": {{message}},+ "attachments": {{attachments}}}```diff --git a/python/api/delete_work_dir_file.py b/python/api/delete_work_dir_file.pyindex 40721161b..4b190df55 100644--- a/python/api/delete_work_dir_file.py+++ b/python/api/delete_work_dir_file.py@@ -10,8 +10,7 @@ class DeleteWorkDirFile(ApiHandler):file_path = input.get('path', '')current_path = input.get('currentPath', '')- work_dir = files.get_abs_path("work_dir")- browser = FileBrowser(work_dir)+ browser = FileBrowser()if browser.delete_file(file_path):# Get updated file listdiff --git a/python/api/download_work_dir_file.py b/python/api/download_work_dir_file.pyindex dd6639443..52498c5f2 100644--- a/python/api/download_work_dir_file.py+++ b/python/api/download_work_dir_file.py@@ -12,8 +12,7 @@ class DownloadWorkDirFile(ApiHandler):if not file_path:raise ValueError("No file path provided")- work_dir = files.get_abs_path("work_dir")- browser = FileBrowser(work_dir)+ browser = FileBrowser()full_path = browser.get_full_path(file_path, True)if os.path.isdir(full_path):diff --git a/python/api/get_work_dir_files.py b/python/api/get_work_dir_files.pyindex 447b368bf..836332fde 100644--- a/python/api/get_work_dir_files.py+++ b/python/api/get_work_dir_files.py@@ -2,14 +2,18 @@ from python.helpers.api import ApiHandlerfrom flask import Request, Responsefrom python.helpers.file_browser import FileBrowser-from python.helpers import files+from python.helpers import files, runtimeclass GetWorkDirFiles(ApiHandler):async def process(self, input: dict, request: Request) -> dict | Response:current_path = request.args.get("path", "")- work_dir = files.get_abs_path("work_dir")- browser = FileBrowser(work_dir)+ if current_path == "$WORK_DIR":+ if runtime.is_development():+ current_path = "work_dir"+ else:+ current_path = "root"+ browser = FileBrowser()result = browser.get_files(current_path)return {"data": result}diff --git a/python/api/message.py b/python/api/message.pyindex b7a8e970b..fc3091fba 100644--- a/python/api/message.py+++ b/python/api/message.py@@ -1,4 +1,4 @@-from agent import AgentContext+from agent import AgentContext, UserMessagefrom python.helpers.api import ApiHandlerfrom flask import Request, Response@@ -30,17 +30,18 @@ class Message(ApiHandler):attachments = request.files.getlist("attachments")attachment_paths = []- upload_folder = files.get_abs_path("work_dir/uploads")+ upload_folder_int = "/a0/tmp/uploads"+ upload_folder_ext = files.get_abs_path("tmp/uploads")if attachments:- os.makedirs(upload_folder, exist_ok=True)+ os.makedirs(upload_folder_ext, exist_ok=True)for attachment in attachments:if attachment.filename is None:continuefilename = secure_filename(attachment.filename)- save_path = files.get_abs_path(upload_folder, filename)+ save_path = files.get_abs_path(upload_folder_ext, filename)attachment.save(save_path)- attachment_paths.append(save_path)+ attachment_paths.append(os.path.join(upload_folder_int, filename))else:# Handle JSON request as beforeinput_data = request.get_json()@@ -56,7 +57,7 @@ class Message(ApiHandler):context = self.get_context(ctxid)# Store attachments in agent data- context.agent0.set_data("attachments", attachment_paths)+ # context.agent0.set_data("attachments", attachment_paths)# Prepare attachment filenames for loggingattachment_filenames = (@@ -84,4 +85,4 @@ class Message(ApiHandler):id=message_id,)- return context.communicate(message), context\ No newline at end of file+ return context.communicate(UserMessage(message, attachment_paths)), context\ No newline at end of filediff --git a/python/api/upload.py b/python/api/upload.pyindex 8413122a7..ea6851577 100644--- a/python/api/upload.py+++ b/python/api/upload.py@@ -1,7 +1,6 @@from python.helpers.api import ApiHandlerfrom flask import Request, Response-from python.helpers.file_browser import FileBrowserfrom python.helpers import filesfrom werkzeug.utils import secure_filename@@ -17,7 +16,7 @@ class UploadFile(ApiHandler):for file in file_list:if file and self.allowed_file(file.filename): # Check file typefilename = secure_filename(file.filename) # type: ignore- file.save(files.get_abs_path("work_dir/upload", filename))+ file.save(files.get_abs_path("tmp/upload", filename))saved_filenames.append(filename)return {"filenames": saved_filenames} # Return saved filenamesdiff --git a/python/api/upload_work_dir_files.py b/python/api/upload_work_dir_files.pyindex 32ed52493..eb6d0b940 100644--- a/python/api/upload_work_dir_files.py+++ b/python/api/upload_work_dir_files.py@@ -16,8 +16,7 @@ class UploadWorkDirFiles(ApiHandler):current_path = request.form.get('path', '')uploaded_files = request.files.getlist("files[]")- work_dir = files.get_abs_path("work_dir")- browser = FileBrowser(work_dir)+ browser = FileBrowser()successful, failed = browser.save_files(uploaded_files, current_path)diff --git a/python/extensions/message_loop_prompts/_30_include_attachments.py b/python/extensions/message_loop_prompts/_30_include_attachments._pysimilarity index 100%rename from python/extensions/message_loop_prompts/_30_include_attachments.pyrename to python/extensions/message_loop_prompts/_30_include_attachments._pydiff --git a/python/extensions/message_loop_prompts/_50_recall_memories.py b/python/extensions/message_loop_prompts/_50_recall_memories.pyindex c302d120c..f25a8b856 100644--- a/python/extensions/message_loop_prompts/_50_recall_memories.py+++ b/python/extensions/message_loop_prompts/_50_recall_memories.py@@ -18,6 +18,12 @@ class RecallMemories(Extension):await self.search_memories(loop_data=loop_data, **kwargs)async def search_memories(self, loop_data: LoopData, **kwargs):++ #cleanup+ extras = loop_data.extras_temporary+ if "memories" in extras:+ del extras["memories"]+# try:# show temp info messageself.agent.context.log.log(@@ -79,13 +85,13 @@ class RecallMemories(Extension):log_item.update(memories=memories_text)# place to prompt- memories_prompt = self.agent.read_prompt(+ memories_prompt = self.agent.parse_prompt("agent.system.memories.md", memories=memories_text)- # append to system message- loop_data.system.append(memories_prompt)-+ # append to prompt+ extras["memories"] = memories_prompt+# except Exception as e:# err = errors.format_error(e)# self.agent.context.log.log(diff --git a/python/extensions/message_loop_prompts/_51_recall_solutions.py b/python/extensions/message_loop_prompts/_51_recall_solutions.pyindex 4ea3afe74..9a0884a5c 100644--- a/python/extensions/message_loop_prompts/_51_recall_solutions.py+++ b/python/extensions/message_loop_prompts/_51_recall_solutions.py@@ -19,6 +19,12 @@ class RecallSolutions(Extension):await self.search_solutions(loop_data=loop_data, **kwargs)async def search_solutions(self, loop_data: LoopData, **kwargs):++ #cleanup+ extras = loop_data.extras_temporary+ if "solutions" in extras:+ del extras["solutions"]+# try:# show temp info messageself.agent.context.log.log(@@ -86,10 +92,12 @@ class RecallSolutions(Extension):solutions_text += solution.page_content + "\n\n"solutions_text = solutions_text.strip()log_item.update(solutions=solutions_text)- solutions_prompt = self.agent.read_prompt(+ solutions_prompt = self.agent.parse_prompt("agent.system.solutions.md", solutions=solutions_text)- loop_data.system.append(solutions_prompt)++ # append to prompt+ extras["solutions"] = solutions_prompt# except Exception as e:# err = errors.format_error(e)diff --git a/python/extensions/message_loop_prompts/_10_system_prompt.py b/python/extensions/system_prompt/_10_system_prompt.pysimilarity index 79%rename from python/extensions/message_loop_prompts/_10_system_prompt.pyrename to python/extensions/system_prompt/_10_system_prompt.pyindex b2de383e0..e7544b2ec 100644--- a/python/extensions/message_loop_prompts/_10_system_prompt.py+++ b/python/extensions/system_prompt/_10_system_prompt.py@@ -5,12 +5,12 @@ from agent import Agent, LoopDataclass SystemPrompt(Extension):- async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ async def execute(self, system_prompt: list[str]=[], loop_data: LoopData = LoopData(), **kwargs):# append main system prompt and toolsmain = get_main_prompt(self.agent)tools = get_tools_prompt(self.agent)- loop_data.system.append(main)- loop_data.system.append(tools)+ system_prompt.append(main)+ system_prompt.append(tools)def get_main_prompt(agent: Agent):return get_prompt("agent.system.main.md", agent)diff --git a/python/extensions/message_loop_prompts/_20_behaviour_prompt.py b/python/extensions/system_prompt/_20_behaviour_prompt.pysimilarity index 81%rename from python/extensions/message_loop_prompts/_20_behaviour_prompt.pyrename to python/extensions/system_prompt/_20_behaviour_prompt.pyindex 384ffd3aa..7f4a37fdd 100644--- a/python/extensions/message_loop_prompts/_20_behaviour_prompt.py+++ b/python/extensions/system_prompt/_20_behaviour_prompt.py@@ -6,9 +6,9 @@ from python.helpers import files, memoryclass BehaviourPrompt(Extension):- async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ async def execute(self, system_prompt: list[str]=[], loop_data: LoopData = LoopData(), **kwargs):prompt = read_rules(self.agent)- loop_data.system.insert(0, prompt) #.append(prompt)+ system_prompt.insert(0, prompt) #.append(prompt)def get_custom_rules_file(agent: Agent):return memory.get_memory_subdir_abs(agent) + f"/behaviour.md"diff --git a/python/helpers/errors.py b/python/helpers/errors.pyindex 544d90ce9..92e2ac2f7 100644--- a/python/helpers/errors.py+++ b/python/helpers/errors.py@@ -7,7 +7,7 @@ def handle_error(e: Exception):if isinstance(e, asyncio.CancelledError):raise e-def format_error(e: Exception, max_entries=4):+def format_error(e: Exception, start_entries=6, end_entries=4):traceback_text = traceback.format_exc()# Split the traceback into lineslines = traceback_text.split('\n')@@ -15,13 +15,15 @@ def format_error(e: Exception, max_entries=4):# Find all "File" linesfile_indices = [i for i, line in enumerate(lines) if line.strip().startswith("File ")]- # If we found at least one "File" line, keep up to max_entries- if file_indices:- start_index = max(0, len(file_indices) - max_entries)- trimmed_lines = lines[file_indices[start_index]:]+ # If we found at least one "File" line, trim the middle if there are more than start_entries+end_entries lines+ if len(file_indices) > start_entries + end_entries:+ start_index = max(0, len(file_indices) - start_entries - end_entries)+ trimmed_lines = lines[:file_indices[start_index]] + [+ f"\n>>> {len(file_indices) - start_entries - end_entries} stack lines skipped <<<\n"+ ] + lines[file_indices[start_index + end_entries]:]else:- # If no "File" lines found, just return the original traceback- return traceback_text+ # If no "File" lines found, or not enough to trim, just return the original traceback+ trimmed_lines = lines# Find the error message at the enderror_message = ""diff --git a/python/helpers/file_browser.py b/python/helpers/file_browser.pyindex 8727442e4..28194ea7e 100644--- a/python/helpers/file_browser.py+++ b/python/helpers/file_browser.py@@ -7,7 +7,7 @@ import zipfilefrom werkzeug.utils import secure_filenamefrom datetime import datetime-from python.helpers import files+from python.helpers import files, runtimeclass FileBrowser:ALLOWED_EXTENSIONS = {@@ -18,8 +18,12 @@ class FileBrowser:MAX_FILE_SIZE = 100 * 1024 * 1024 # 100MB- def __init__(self, base_dir: str):- self.base_dir = Path(base_dir).resolve()+ def __init__(self):+ if runtime.is_development():+ base_dir = files.get_base_dir()+ else:+ base_dir = "/"+ self.base_dir = Path(base_dir)def _check_file_size(self, file) -> bool:try:diff --git a/python/helpers/history.py b/python/helpers/history.pyindex 10c387d38..9fd3c1efd 100644--- a/python/helpers/history.py+++ b/python/helpers/history.py@@ -16,10 +16,10 @@ HISTORY_BULK_RATIO = 0.2TOPIC_COMPRESS_RATIO = 0.65LARGE_MESSAGE_TO_TOPIC_RATIO = 0.25-OutputType = (- list["OutputType"]- | OrderedDict[str, "OutputType"]- | list[OrderedDict[str, "OutputType"]]+MessageContent = (+ list["MessageContent"]+ | OrderedDict[str, "MessageContent"]+ | list[OrderedDict[str, "MessageContent"]]| str| list[str])@@ -27,7 +27,7 @@ OutputType = (class OutputMessage(TypedDict):ai: bool- content: OutputType+ content: MessageContentclass Record:@@ -67,19 +67,16 @@ class Record:class Message(Record):- def __init__(self, ai: bool, text: str | None = None, **kwargs: OutputType):+ def __init__(self, ai: bool, content: MessageContent):self.ai = ai- self.text = text- self.kwargs: OrderedDict[str, OutputType] = OrderedDict(**kwargs)- self.summary: OutputType = ""+ self.content = content+ self.summary: MessageContent = ""async def compress(self):return Falsedef output(self):- return [- OutputMessage(ai=self.ai, content=self.summary or self.text or self.kwargs)- ]+ return [OutputMessage(ai=self.ai, content=self.summary or self.content)]def output_langchain(self):return output_langchain(self.output())@@ -91,15 +88,14 @@ class Message(Record):return {"_cls": "Message","ai": self.ai,- "text": self.text,- "kwargs": self.kwargs,+ "content": self.content,"summary": self.summary,}@staticmethoddef from_dict(data: dict, history: "History"):- msg = Message(ai=data["ai"], text=data["text"], **data["kwargs"])- msg.summary = data["summary"]+ msg = Message(ai=data["ai"], content=data.get("content", "Content lost"))+ msg.summary = data.get("summary", "")return msg@@ -109,8 +105,8 @@ class Topic(Record):self.summary: str = ""self.messages: list[Message] = []- def add_message(self, ai: bool, text: str | None = None, **kwargs):- msg = Message(ai=ai, text=text, **kwargs)+ def add_message(self, ai: bool, content: MessageContent):+ msg = Message(ai=ai, content=content)self.messages.append(msg)return msg@@ -145,7 +141,10 @@ class Topic(Record):for msg, tok, leng, out in large_msgs:trim_to_chars = leng * (msg_max_size / tok)trunc = messages.truncate_dict_by_ratio(- self.history.agent, out[0]["content"], trim_to_chars * 1.15, trim_to_chars * 0.85+ self.history.agent,+ out[0]["content"],+ trim_to_chars * 1.15,+ trim_to_chars * 0.85,)msg.summary = trunc@@ -164,10 +163,10 @@ class Topic(Record):cnt_to_sum = math.ceil((len(self.messages) - 2) * TOPIC_COMPRESS_RATIO)msg_to_sum = self.messages[1 : cnt_to_sum + 1]summary = await self.summarize_messages(msg_to_sum)- sum_msg_args, sum_msg_kwargs = self.history.agent.parse_prompt(+ sum_msg_content = self.history.agent.parse_prompt("fw.msg_summary.md", summary=summary)- sum_msg = Message(False, *sum_msg_args, **sum_msg_kwargs)+ sum_msg = Message(False, sum_msg_content)self.messages[1 : cnt_to_sum + 1] = [sum_msg]return Truereturn False@@ -274,8 +273,8 @@ class History(Record):+ self.get_current_topic_tokens())- def add_message(self, ai: bool, text: str | None = None, **kwargs: OutputType):- return self.current.add_message(ai, text=text, **kwargs)+ def add_message(self, ai: bool, content: MessageContent):+ return self.current.add_message(ai, content=content)def new_topic(self):if self.current.messages:@@ -367,9 +366,9 @@ class History(Record):return Trueasync def compress_bulks(self):- #merge bulks if possible+ # merge bulks if possiblecompressed = await self.merge_bulks_by(BULK_MERGE_COUNT)- #remove oldest bulk if necessary+ # remove oldest bulk if necessaryif not compressed:self.bulks.pop(0)return compressed@@ -395,7 +394,7 @@ class History(Record):def deserialize_history(json_data: str, agent) -> History:history = History(agent=agent)- if json_data:+ if json_data:data = json.loads(json_data)history = History.from_dict(data, history=history)return history@@ -410,7 +409,7 @@ def serialize_output(output: OutputMessage, ai_label="ai", human_label="human"):return f'{ai_label if output["ai"] else human_label}: {serialize_content(output["content"])}'-def serialize_content(content: OutputType) -> str:+def serialize_content(content: MessageContent) -> str:if isinstance(content, str):return contenttry:@@ -446,7 +445,7 @@ def output_text(messages: list[OutputMessage], ai_label="ai", human_label="humanreturn "\n".join(serialize_output(o, ai_label, human_label) for o in messages)-def merge_outputs(a: OutputType, b: OutputType) -> OutputType:+def merge_outputs(a: MessageContent, b: MessageContent) -> MessageContent:if not isinstance(a, list):a = [a]if not isinstance(b, list):@@ -455,7 +454,7 @@ def merge_outputs(a: OutputType, b: OutputType) -> OutputType:# return merge_properties(a, b)-def merge_properties(a: OutputType, b: OutputType) -> OutputType:+def merge_properties(a: MessageContent, b: MessageContent) -> MessageContent:if isinstance(a, list):if isinstance(b, list):return a + b # type: ignorediff --git a/python/helpers/settings.py b/python/helpers/settings.pyindex af0627367..e57a087e0 100644--- a/python/helpers/settings.py+++ b/python/helpers/settings.py@@ -46,7 +46,6 @@ class Settings(TypedDict):stt_silence_duration: intstt_waiting_timeout: int-class PartialSettings(Settings, total=False):pass@@ -132,7 +131,7 @@ def convert_out(settings: Settings) -> SettingsOutput:chat_model_fields.append({"id": "chat_model_ctx_history",- "title": "Context window space for chat history.",+ "title": "Context window space for chat history","description": "Portion of context window dedicated to chat history visible to the agent. Chat history will automatically be optimized to fit. Smaller size will result in shorter and more summarized history. The remaining space will be used for system prompt, RAG and response.","type": "range","min": 0.01,diff --git a/python/tools/unknown.py b/python/tools/unknown.pyindex 5dbaf8be5..57ebd0b81 100644--- a/python/tools/unknown.py+++ b/python/tools/unknown.py@@ -1,5 +1,5 @@from python.helpers.tool import Tool, Response-from python.extensions.message_loop_prompts._10_system_prompt import (+from python.extensions.system_prompt._10_system_prompt import (get_tools_prompt,)diff --git a/webui/index.html b/webui/index.htmlindex 0ad481e78..90b4cc132 100644--- a/webui/index.html+++ b/webui/index.html@@ -216,17 +216,17 @@handleFileUpload(event) {const files = event.target.files;- if (files.length + this.attachments.length > 4) {- alert('Maximum 4 attachments allowed');- return;- }+ //if (files.length + this.attachments.length > 4) {+ // alert('Maximum 4 attachments allowed');+ // return;+ //}Array.from(files).forEach(file => {const ext = file.name.split('.').pop().toLowerCase();const allowedExts = new Set(['jpg', 'jpeg', 'png', 'bmp', 'md', 'py', 'js', 'sh','html', 'css', 'pdf', 'txt', 'csv', 'json']);- if (allowedExts.has(ext)) {+ //if (allowedExts.has(ext)) {const isImage = ['jpg', 'jpeg', 'png', 'bmp'].includes(ext);if (isImage) {@@ -253,7 +253,7 @@});this.hasAttachments = true;}- }+ //}});}}">@@ -294,7 +294,7 @@multiple style="display: none" @change="handleFileUpload($event)"><div x-show="showTooltip" class="tooltip">- Limit: 4 attachments per message+ Add attachments to the message</div></div>@@ -361,7 +361,7 @@d="m6.38,20.23H1.75l7.03,67.03c.11,1.07.55,2.02,1.2,2.69.55.55,1.28.89,2.11.89h97.1c.82,0,1.51-.33,2.05-.87.68-.68,1.13-1.67,1.28-2.79l9.1-66.94H6.38Z"fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="8"></path></svg>- <p>work_dir Browser</p>+ <p>Files</p></button><button class="text-button" id="history_inspect" @click="window.openHistoryModal()">diff --git a/webui/js/file_browser.js b/webui/js/file_browser.jsindex de7ba443d..fd9eca788 100644--- a/webui/js/file_browser.js+++ b/webui/js/file_browser.js@@ -3,7 +3,7 @@ const fileBrowserModalProxy = {isLoading: false,browser: {- title: "Work Directory Browser",+ title: "File Browser",currentPath: "",entries: [],parentPath: "",@@ -24,7 +24,7 @@ const fileBrowserModalProxy = {// Initialize currentPath to root if it's emptyif (!modalAD.browser.currentPath) {- modalAD.browser.currentPath = "";+ modalAD.browser.currentPath = "$WORK_DIR";}await modalAD.fetchFiles(modalAD.browser.currentPath);