Agent Zero AI framework
Context window management, work in progress
frdela0ff118ad1c9
diff --git a/agent.py b/agent.pyindex f3d8a4f60..b59516aa0 100644--- a/agent.py+++ b/agent.py@@ -3,11 +3,10 @@ from dataclasses import dataclass, fieldimport time, importlib, inspect, os, jsonfrom typing import Any, Optional, Dict, TypedDictimport uuid-from python.helpers import extract_tools, rate_limiter, files, errors+from python.helpers import extract_tools, rate_limiter, files, errors, history, tokensfrom python.helpers.print_style import PrintStyle-from langchain.schema import AIMessagefrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder-from langchain_core.messages import HumanMessage, SystemMessage+from langchain_core.messages import HumanMessage, SystemMessage, AIMessagefrom langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.language_models.llms import BaseLLMfrom langchain_core.embeddings import Embeddings@@ -88,7 +87,7 @@ class AgentContext:while intervention_agent and broadcast_level != 0:intervention_agent.intervention_message = msgbroadcast_level -= 1- intervention_agent = intervention_agent.data.get("superior", None)+ intervention_agent = intervention_agent.data.get(Agent.DATA_NAME_SUPERIOR, None)else:# self.process = DeferredTask(current_agent.monologue, msg)@@ -97,19 +96,17 @@ 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: str, user=True):try:msg_template = (- agent.read_prompt("fw.user_message.md", message=msg)+ await agent.hist_add_user_message(msg)if user- else agent.read_prompt(- "fw.tool_response.md",- tool_name="call_subordinate",- tool_response=msg,+ else await agent.hist_add_tool_result(+ tool_name="call_subordinate", tool_result=msg))- response = await agent.monologue(msg_template)- superior = agent.data.get("superior", None)+ response = await agent.monologue()+ superior = agent.data.get(Agent.DATA_NAME_SUPERIOR, None)if superior:response = await self._process_chain(superior, response, False)return response@@ -154,15 +151,18 @@ class AgentConfig:additional: Dict[str, Any] = field(default_factory=dict)-class LoopData:- def __init__(self):+ def __init__(self, **kwargs):self.iteration = -1self.system = []- self.message = ""- self.history_from = 0- self.history = []- self.attachments = [] # Add attachments field+ self.user_message: history.Message | None = None+ self.history_output: list[history.OutputMessage] = []+ self.last_response = ""+ self.attachments = [] # Add attachments field++ # override values with kwargs+ for key, value in kwargs.items():+ setattr(self, key, value)# intervention exception class - skips rest of message loop iteration@@ -181,6 +181,10 @@ class HandledException(Exception):class Agent:+ DATA_NAME_SUPERIOR = "_superior"+ DATA_NAME_SUBORDINATE = "_subordinate"+ DATA_NAME_CTX_WINDOW = "ctx_window"+def __init__(self, number: int, config: AgentConfig, context: AgentContext | None = None):@@ -195,8 +199,8 @@ class Agent:self.number = numberself.agent_name = f"Agent {self.number}"- self.history = []- self.last_message = ""+ self.history = history.History(self)+ self.last_user_message: history.Message | None = Noneself.intervention_message = ""self.rate_limiter = rate_limiter.RateLimiter(self.context.log,@@ -207,59 +211,67 @@ class Agent:)self.data = {} # free data object all the tools can use- async def monologue(self, msg: str):+ async def monologue(self):while True:try:# loop data dictionary to pass to extensions- loop_data = LoopData()- loop_data.message = msg- loop_data.history_from = len(self.history)-+ self.loop_data = LoopData(user_message=self.last_user_message)# call monologue_start extensions- await self.call_extensions("monologue_start", loop_data=loop_data)+ await self.call_extensions("monologue_start", loop_data=self.loop_data)printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)- user_message = loop_data.message-- # 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+ # # 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.append_message(user_message, human=True)+ # 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:self.context.streaming_agent = self # mark self as current streameragent_response = ""- loop_data.iteration += 1+ self.loop_data.iteration += 1try:# set system prompt and message history- loop_data.system = []- loop_data.history = self.history+ self.loop_data.system = []+ self.loop_data.history_output = self.history.output()# and allow extensions to edit themawait self.call_extensions(- "message_loop_prompts", loop_data=loop_data+ "message_loop_prompts", loop_data=self.loop_data)# build chain from system prompt, message history and modelprompt = ChatPromptTemplate.from_messages([- SystemMessage(content="\n\n".join(loop_data.system)),+ 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+ )+# rate limiter TODO - move to extension, make per-model- formatted_inputs = prompt.format(messages=self.history)- tokens = int(len(formatted_inputs) / 4)- self.rate_limiter.limit_call_and_input(tokens)+ formatted_inputs = prompt.format(messages=history_langchain)+ 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)# output that the agent is startingPrintStyle(@@ -273,11 +285,10 @@ class Agent:)async for chunk in chain.astream(- {"messages": loop_data.history}+ {"messages": history_langchain}):- await self.handle_intervention(- agent_response- ) # wait for intervention and handle it, if paused+ # wait for intervention and handle it, if paused+ await self.handle_intervention(agent_response)if isinstance(chunk, str):content = chunk@@ -287,12 +298,10 @@ class Agent:content = str(chunk)if content:- printer.stream(- content- ) # output the agent response stream- agent_response += (- content # concatenate stream into the response- )+ # output the agent response stream+ printer.stream(content)+ # concatenate stream into the response+ agent_response += contentself.log_from_stream(agent_response, log)self.rate_limiter.set_output_tokens(@@ -302,27 +311,23 @@ class Agent:await self.handle_intervention(agent_response)if (- self.last_message == agent_response+ self.loop_data.last_response == agent_response): # if assistant_response is the same as last message in history, let him know- await self.append_message(- agent_response- ) # Append the assistant's response to the history+ # Append the assistant's response to the history+ await self.hist_add_ai_response(agent_response)+ # Append warning message to the historywarning_msg = self.read_prompt("fw.msg_repeat.md")- await self.append_message(- warning_msg, human=True- ) # Append warning message to the history+ await self.hist_add_warning(message=warning_msg)PrintStyle(font_color="orange", padding=True).print(warning_msg)self.context.log.log(type="warning", content=warning_msg)else: # otherwise proceed with tool- await self.append_message(- agent_response- ) # Append the assistant's response to the history- tools_result = await self.process_tools(- agent_response- ) # process tools requested in agent message+ # Append the assistant's response to the history+ await self.hist_add_ai_response(agent_response)+ # process tools requested in agent message+ tools_result = await self.process_tools(agent_response)if tools_result: # final response of message loop availablereturn tools_result # break the execution if the task is done@@ -333,19 +338,16 @@ class Agent:RepairableException) as e: # Forward repairable errors to the LLM, maybe it can fix themerror_message = errors.format_error(e)- msg_response = self.read_prompt(- "fw.error.md", error=error_message- ) # error message template- await self.append_message(msg_response, human=True)- PrintStyle(font_color="red", padding=True).print(msg_response)- self.context.log.log(type="error", content=msg_response)+ await self.hist_add_warning(error_message)+ PrintStyle(font_color="red", padding=True).print(error_message)+ self.context.log.log(type="error", content=error_message)except Exception as e: # Other exception kill the loopself.handle_critical_exception(e)finally:# call message_loop_end extensionsawait self.call_extensions(- "message_loop_end", loop_data=loop_data+ "message_loop_end", loop_data=self.loop_data)# exceptions outside message loop:@@ -356,7 +358,7 @@ class Agent:finally:self.context.streaming_agent = None # unset current streamer# call monologue_end extensions- await self.call_extensions("monologue_end", loop_data=loop_data) # type: ignore+ await self.call_extensions("monologue_end", loop_data=self.loop_data) # type: ignoredef handle_critical_exception(self, exception: Exception):if isinstance(exception, HandledException):@@ -376,6 +378,24 @@ 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]:+ prompt_dir = files.get_abs_path("prompts/default")+ backup_dir = []+ if (+ self.config.prompts_subdir+ ): # if agent has custom folder, use it and use default as backup+ prompt_dir = files.get_abs_path("prompts", self.config.prompts_subdir)+ backup_dir.append(files.get_abs_path("prompts/default"))+ 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], {}+def read_prompt(self, file: str, **kwargs) -> str:prompt_dir = files.get_abs_path("prompts/default")backup_dir = []@@ -385,7 +405,7 @@ class Agent:prompt_dir = files.get_abs_path("prompts", self.config.prompts_subdir)backup_dir.append(files.get_abs_path("prompts/default"))return files.read_file(- files.get_abs_path(prompt_dir, file), backup_dirs=backup_dir, **kwargs+ files.get_abs_path(prompt_dir, file), _backup_dirs=backup_dir, **kwargs)def get_data(self, field: str):@@ -394,23 +414,36 @@ class Agent:def set_data(self, field: str, value):self.data[field] = value- async def append_message(self, msg: str, human: bool = False):- message_type = "human" if human else "ai"- if self.history and self.history[-1].type == message_type:- self.history[-1].content += "\n\n" + msg+ async def hist_add_user_message(self, message: str, intervention: bool = False):+ self.history.new_topic() # user message starts a new topic in history+ if intervention:+ args, kwargs = self.parse_prompt("fw.intervention.md", message=message)+ msg = self.history.add_message(False, *args, **kwargs)else:- new_message = HumanMessage(content=msg) if human else AIMessage(content=msg)- self.history.append(new_message)- await self.cleanup_history(- self.config.msgs_keep_max,- self.config.msgs_keep_start,- self.config.msgs_keep_end,- )- if message_type == "ai":- self.last_message = msg+ args, kwargs = self.parse_prompt("fw.user_message.md", message=message)+ msg = self.history.add_message(False, *args, **kwargs)+ self.last_user_message = msg+ return msg++ async 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)++ 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_tool_result(self, tool_name: str, tool_result: str):+ args, kwargs = self.parse_prompt(+ "fw.tool_result.md", tool_name=tool_name, tool_result=tool_result+ )+ return self.history.add_message(False, *args, **kwargs)- def concat_messages(self, messages):- return "\n".join([f"{msg.type}: {msg.content}" for msg in messages])+ def concat_messages(+ self, messages+ ): # TODO add param for message range, topic, history+ return self.history.output_text(human_label="user", ai_label="assistant")async def call_utility_llm(self, system: str, msg: str, callback: Callable[[str], None] | None = None@@ -423,8 +456,8 @@ class Agent:response = ""formatted_inputs = prompt.format()- tokens = int(len(formatted_inputs) / 4)- self.rate_limiter.limit_call_and_input(tokens)+ token_count = tokens.approximate_tokens(formatted_inputs)+ self.rate_limiter.limit_call_and_input(token_count)async for chunk in chain.astream({}):await self.handle_intervention() # wait for intervention and handle it, if paused@@ -469,28 +502,28 @@ class Agent: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+ # if len(self.history) <= max:+ # return self.history- first_x = self.history[:keep_start]- last_y = self.history[-keep_end:]+ # first_x = self.history[:keep_start]+ # last_y = self.history[-keep_end:]- # Identify the middle part- middle_part = self.history[keep_start:-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 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]+ # # 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)+ # # 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+ # self.history = first_x + new_middle_part + last_yreturn self.history@@ -503,15 +536,11 @@ class Agent:msg = self.intervention_messageself.intervention_message = "" # reset the intervention messageif progress.strip():- await self.append_message(- progress- ) # append the response generated so far- user_msg = self.read_prompt(- "fw.intervention.md", user_message=msg- ) # format the user intervention template- await self.append_message(- user_msg, human=True- ) # append the intervention message+ await self.hist_add_ai_response(progress)+ # format the user intervention template+ user_msg = self.read_prompt("fw.intervention.md", user_message=msg)+ # append the intervention message+ await self.hist_add_user_message(user_msg, intervention=True)raise InterventionException(msg)async def process_tools(self, msg: str):@@ -534,7 +563,7 @@ class Agent:return response.messageelse:msg = self.read_prompt("fw.msg_misformat.md")- await self.append_message(msg, human=True)+ await self.hist_add_warning(msg)PrintStyle(font_color="red", padding=True).print(msg)self.context.log.log(type="error", content=f"{self.agent_name}: Message misformat"@@ -546,9 +575,8 @@ class Agent:return # no reason to tryresponse = DirtyJson.parse_string(stream)if isinstance(response, dict):- logItem.update(- content=stream, kvps=response- ) # log if result is a dictionary already+ # log if result is a dictionary already+ logItem.update(content=stream, kvps=response)except Exception as e:passdiff --git a/prompts/default/fw.ai_response.md b/prompts/default/fw.ai_response.mdnew file mode 100644index 000000000..bd62cd6d1--- /dev/null+++ b/prompts/default/fw.ai_response.md@@ -0,0 +1 @@+{{message}}\ No newline at end of filediff --git a/prompts/default/fw.bulk_summary.msg.md b/prompts/default/fw.bulk_summary.msg.mdnew file mode 100644index 000000000..fc1f1de3f--- /dev/null+++ b/prompts/default/fw.bulk_summary.msg.md@@ -0,0 +1,2 @@+# Message history to summarize:+{{content}}\ No newline at end of filediff --git a/prompts/default/fw.bulk_summary.sys.md b/prompts/default/fw.bulk_summary.sys.mdnew file mode 100644index 000000000..46004b15b--- /dev/null+++ b/prompts/default/fw.bulk_summary.sys.md@@ -0,0 +1,13 @@+# AI role+You are AI summarization assistant+You are provided with a conversation history and your goal is to provide a short summary of the conversation+Records in the conversation may already be summarized+You must return a single summary of all records++# Expected output+Your output will be a text of the summary+Length of the text should be one paragraph, approximately 100 words+No intro+No conclusion+No formatting+Only the summary text is returned\ No newline at end of filediff --git a/prompts/default/fw.call_subordinate.md b/prompts/default/fw.call_subordinate.mdnew file mode 100644index 000000000..bd62cd6d1--- /dev/null+++ b/prompts/default/fw.call_subordinate.md@@ -0,0 +1 @@+{{message}}\ No newline at end of filediff --git a/prompts/default/fw.intervention.md b/prompts/default/fw.intervention.mdindex 21d6c674a..bd62cd6d1 100644--- a/prompts/default/fw.intervention.md+++ b/prompts/default/fw.intervention.md@@ -1,5 +1 @@-~~~json-{- "user_intervention": "{{user_message}}"-}-~~~\ No newline at end of file+{{message}}\ No newline at end of filediff --git a/prompts/default/fw.msg_misformat.md b/prompts/default/fw.msg_misformat.mdindex 4adabcd7d..23ae0eff1 100644--- a/prompts/default/fw.msg_misformat.md+++ b/prompts/default/fw.msg_misformat.md@@ -1,5 +1 @@-~~~json-{- "system_warning": "You have misformatted your message. Follow system prompt instructions on JSON message formatting precisely."-}-~~~\ No newline at end of file+You have misformatted your message. Follow system prompt instructions on JSON message formatting precisely.\ No newline at end of filediff --git a/prompts/default/fw.msg_repeat.md b/prompts/default/fw.msg_repeat.mdindex 116031899..74da5835f 100644--- a/prompts/default/fw.msg_repeat.md+++ b/prompts/default/fw.msg_repeat.md@@ -1,5 +1 @@-~~~json-{- "system_warning": "You have sent the same message again. You have to do something else!"-}-~~~\ No newline at end of file+You have sent the same message again. You have to do something else!\ No newline at end of filediff --git a/prompts/default/fw.msg_summary.md b/prompts/default/fw.msg_summary.mdnew file mode 100644index 000000000..7e5008135--- /dev/null+++ b/prompts/default/fw.msg_summary.md@@ -0,0 +1,5 @@+```json+{+ "messages_summary": {{summary}}+}+```diff --git a/prompts/default/fw.tool_not_found.md b/prompts/default/fw.tool_not_found.mdindex a7a69cac4..13192b435 100644--- a/prompts/default/fw.tool_not_found.md+++ b/prompts/default/fw.tool_not_found.md@@ -1,5 +1 @@-~~~json-{- "system_warning": "Tool {{tool_name}} not found. Available tools: \n{{tools_prompt}}"-}-~~~\ No newline at end of file+Tool {{tool_name}} not found. Available tools: \n{{tools_prompt}}\ No newline at end of filediff --git a/prompts/default/fw.tool_response.md b/prompts/default/fw.tool_response.mddeleted file mode 100644index c45990649..000000000--- a/prompts/default/fw.tool_response.md+++ /dev/null@@ -1,6 +0,0 @@-~~~json-{- "response_from_tool": "{{tool_name}}",- "data": {{tool_response}}-}-~~~\ No newline at end of filediff --git a/prompts/default/fw.tool_result.md b/prompts/default/fw.tool_result.mdnew file mode 100644index 000000000..1943cc022--- /dev/null+++ b/prompts/default/fw.tool_result.md@@ -0,0 +1,6 @@+~~~json+{+ "tool_name": {{tool_name}},+ "tool_result": {{tool_result}}+}+~~~\ No newline at end of filediff --git a/prompts/default/fw.topic_summary.msg.md b/prompts/default/fw.topic_summary.msg.mdnew file mode 100644index 000000000..fc1f1de3f--- /dev/null+++ b/prompts/default/fw.topic_summary.msg.md@@ -0,0 +1,2 @@+# Message history to summarize:+{{content}}\ No newline at end of filediff --git a/prompts/default/fw.topic_summary.sys.md b/prompts/default/fw.topic_summary.sys.mdnew file mode 100644index 000000000..46004b15b--- /dev/null+++ b/prompts/default/fw.topic_summary.sys.md@@ -0,0 +1,13 @@+# AI role+You are AI summarization assistant+You are provided with a conversation history and your goal is to provide a short summary of the conversation+Records in the conversation may already be summarized+You must return a single summary of all records++# Expected output+Your output will be a text of the summary+Length of the text should be one paragraph, approximately 100 words+No intro+No conclusion+No formatting+Only the summary text is returned\ No newline at end of filediff --git a/prompts/default/fw.user_message.md b/prompts/default/fw.user_message.mdindex 7843c594b..ff111f6fe 100644--- a/prompts/default/fw.user_message.md+++ b/prompts/default/fw.user_message.md@@ -1,5 +1,5 @@-~~~json+```json{- "user": "{{message}}"+ "user_message": {{message}}}-~~~\ No newline at end of file+```diff --git a/prompts/default/fw.warning.md b/prompts/default/fw.warning.mdnew file mode 100644index 000000000..cccb140fe--- /dev/null+++ b/prompts/default/fw.warning.md@@ -0,0 +1,5 @@+~~~json+{+ "system_warning": {{message}}+}+~~~diff --git a/python/api/chat_export.py b/python/api/chat_export.pyindex 0a3f94076..c54586635 100644--- a/python/api/chat_export.py+++ b/python/api/chat_export.py@@ -11,7 +11,6 @@ class ExportChat(ApiHandler):context = self.get_context(ctxid)content = persist_chat.export_json_chat(context)-return {"message": "Chats exported.","ctxid": context.id,diff --git a/python/api/ctx_window_get.py b/python/api/ctx_window_get.pynew file mode 100644index 000000000..22e4edbd9--- /dev/null+++ b/python/api/ctx_window_get.py@@ -0,0 +1,14 @@+from python.helpers import tokens+from python.helpers.api import ApiHandler+from flask import Request, Response+++class GetCtxWindow(ApiHandler):+ async def process(self, input: dict, request: Request) -> dict | Response:+ ctxid = input.get("context", [])+ context = self.get_context(ctxid)+ agent = context.streaming_agent or context.agent0+ window = agent.get_data(agent.DATA_NAME_CTX_WINDOW)+ size = tokens.approximate_tokens(window)++ return {"content": window, "tokens": size}diff --git a/python/api/history_get.py b/python/api/history_get.pynew file mode 100644index 000000000..579a32890--- /dev/null+++ b/python/api/history_get.py@@ -0,0 +1,17 @@+from python.helpers import tokens+from python.helpers.api import ApiHandler+from flask import Request, Response+++class GetHistory(ApiHandler):+ async def process(self, input: dict, request: Request) -> dict | Response:+ ctxid = input.get("context", [])+ context = self.get_context(ctxid)+ agent = context.streaming_agent or context.agent0+ history = agent.history.output()+ size = tokens.approximate_tokens(agent.history.output_text())++ return {+ "history": history,+ "tokens": size+ }\ No newline at end of filediff --git a/python/extensions/message_loop_end/_10_organize_history.py b/python/extensions/message_loop_end/_10_organize_history.pynew file mode 100644index 000000000..a529b49ce--- /dev/null+++ b/python/extensions/message_loop_end/_10_organize_history.py@@ -0,0 +1,18 @@+import asyncio+from python.helpers.extension import Extension+from agent import LoopData++DATA_NAME_TASK = "_organize_history_task"+++class OrganizeHistory(Extension):+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ # is there a running task? if yes, skip this round, the wait extension will double check the context size+ task = self.agent.get_data(DATA_NAME_TASK)+ if task and not task.done():+ return++ # start task+ task = asyncio.create_task(self.agent.history.compress())+ # set to agent to be able to wait for it+ self.agent.set_data(DATA_NAME_TASK, task)diff --git a/python/extensions/message_loop_prompts/_50_recall_memories.py b/python/extensions/message_loop_prompts/_50_recall_memories.pyindex 2d791f9dd..c302d120c 100644--- a/python/extensions/message_loop_prompts/_50_recall_memories.py+++ b/python/extensions/message_loop_prompts/_50_recall_memories.py@@ -6,7 +6,7 @@ from agent import LoopDataclass RecallMemories(Extension):INTERVAL = 3- HISTORY = 5+ HISTORY = 5 # TODO cleanupRESULTS = 3THRESHOLD = 0.6@@ -31,9 +31,10 @@ class RecallMemories(Extension):)# get system message and chat history for util llm- msgs_text = self.agent.concat_messages(- self.agent.history[-RecallMemories.HISTORY :]- ) # only last X messages+ # msgs_text = self.agent.concat_messages(+ # self.agent.history[-RecallMemories.HISTORY :]+ # ) # only last X messages+ msgs_text = self.agent.history.current.output_text()system = self.agent.read_prompt("memory.memories_query.sys.md", history=msgs_text)@@ -44,7 +45,7 @@ class RecallMemories(Extension):# call util llm to summarize conversationquery = await self.agent.call_utility_llm(- system=system, msg=loop_data.message, callback=log_callback+ system=system, msg=loop_data.user_message.output_text() if loop_data.user_message else "", callback=log_callback)# get solutions databasediff --git a/python/extensions/message_loop_prompts/_51_recall_solutions.py b/python/extensions/message_loop_prompts/_51_recall_solutions.pyindex 8de5d855e..4ea3afe74 100644--- a/python/extensions/message_loop_prompts/_51_recall_solutions.py+++ b/python/extensions/message_loop_prompts/_51_recall_solutions.py@@ -6,7 +6,7 @@ from agent import LoopDataclass RecallSolutions(Extension):INTERVAL = 3- HISTORY = 5+ HISTORY = 5 # TODO cleanupSOLUTIONS_COUNT = 2INSTRUMENTS_COUNT = 2THRESHOLD = 0.6@@ -32,9 +32,10 @@ class RecallSolutions(Extension):)# get system message and chat history for util llm- msgs_text = self.agent.concat_messages(- self.agent.history[-RecallSolutions.HISTORY :]- ) # only last X messages+ # msgs_text = self.agent.concat_messages(+ # self.agent.history[-RecallSolutions.HISTORY :]+ # ) # only last X messages+ msgs_text = self.agent.history.current.output_text()system = self.agent.read_prompt("memory.solutions_query.sys.md", history=msgs_text)@@ -45,7 +46,7 @@ class RecallSolutions(Extension):# call util llm to summarize conversationquery = await self.agent.call_utility_llm(- system=system, msg=loop_data.message, callback=log_callback+ system=system, msg=loop_data.user_message.output_text() if loop_data.user_message else "", callback=log_callback)# get solutions databasediff --git a/python/extensions/message_loop_prompts/_90_organize_history_wait.py b/python/extensions/message_loop_prompts/_90_organize_history_wait.pynew file mode 100644index 000000000..72d37ebf2--- /dev/null+++ b/python/extensions/message_loop_prompts/_90_organize_history_wait.py@@ -0,0 +1,34 @@+from python.helpers.extension import Extension+from agent import LoopData+from python.extensions.message_loop_end._10_organize_history import DATA_NAME_TASK+import asyncio+++class OrganizeHistoryWait(Extension):+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):++ # sync action only required if the history is too large, otherwise leave it in background+ while self.agent.history.is_over_limit():+ # get task+ task = self.agent.get_data(DATA_NAME_TASK)++ # Check if the task is already done+ if task:+ if not task.done():+ self.log()++ # Wait for the task to complete+ await task++ # Clear the coroutine data after it's done+ self.agent.set_data(DATA_NAME_TASK, None)+ else:+ # no task running, start and wait+ self.log()+ await self.agent.history.compress()++ def log(self):+ if not hasattr(self, 'log_item') or not self.log_item:+ self.log_item = self.agent.context.log.log(+ type="util", heading="Waiting for history to be compressed..."+ )diff --git a/python/helpers/errors.py b/python/helpers/errors.pyindex 7dd96ea1d..544d90ce9 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=2):+def format_error(e: Exception, max_entries=4):traceback_text = traceback.format_exc()# Split the traceback into lineslines = traceback_text.split('\n')diff --git a/python/helpers/files.py b/python/helpers/files.pyindex 274d5073c..372e017ec 100644--- a/python/helpers/files.py+++ b/python/helpers/files.py@@ -1,45 +1,110 @@from fnmatch import fnmatch+import jsonimport os, reimport re-def read_file(relative_path, backup_dirs=None, encoding="utf-8", **kwargs):- if backup_dirs is None:- backup_dirs = []++def parse_file(_relative_path, _backup_dirs=None, _encoding="utf-8", **kwargs):+ content = read_file(_relative_path, _backup_dirs, _encoding)+ is_json = is_full_json_template(content)+ content = remove_code_fences(content)+ if is_json:+ content = replace_placeholders_json(content, **kwargs)+ obj = json.loads(content)+ # obj = replace_placeholders_dict(obj, **kwargs)+ return obj+ else:+ content = replace_placeholders_text(content, **kwargs)+ return content+++def read_file(_relative_path, _backup_dirs=None, _encoding="utf-8", **kwargs):+ if _backup_dirs is None:+ _backup_dirs = []# Try to get the absolute path for the file from the original directory or backup directories- absolute_path = find_file_in_dirs(relative_path, backup_dirs)+ absolute_path = find_file_in_dirs(_relative_path, _backup_dirs)# Read the file content- with open(absolute_path, 'r', encoding=encoding) as f:- content = remove_code_fences(f.read())+ with open(absolute_path, "r", encoding=_encoding) as f:+ # content = remove_code_fences(f.read())+ content = f.read()# Replace placeholders with values from kwargs- for key, value in kwargs.items():- placeholder = "{{" + key + "}}"- strval = str(value)- content = content.replace(placeholder, strval)+ content = replace_placeholders_text(content, **kwargs)# Process include statements- content = process_includes(content, os.path.dirname(relative_path), backup_dirs, **kwargs)+ content = process_includes(+ content, os.path.dirname(_relative_path), _backup_dirs, **kwargs+ )return content-def process_includes(content, base_path, backup_dirs, **kwargs):++def replace_placeholders_text(_content: str, **kwargs):+ # Replace placeholders with values from kwargs+ for key, value in kwargs.items():+ placeholder = "{{" + key + "}}"+ strval = str(value)+ _content = _content.replace(placeholder, strval)+ return _content++def replace_placeholders_json(_content: str, **kwargs):+ # Replace placeholders with values from kwargs+ for key, value in kwargs.items():+ placeholder = "{{" + key + "}}"+ strval = json.dumps(value)+ _content = _content.replace(placeholder, strval)+ return _content++def replace_placeholders_dict(_content: dict, **kwargs):+ def replace_value(value):+ if isinstance(value, str):+ placeholders = re.findall(r"{{(\w+)}}", value)+ if placeholders:+ for placeholder in placeholders:+ if placeholder in kwargs:+ replacement = kwargs[placeholder]+ if value == f"{{{{{placeholder}}}}}":+ return replacement+ elif isinstance(replacement, (dict, list)):+ value = value.replace(+ f"{{{{{placeholder}}}}}", json.dumps(replacement)+ )+ else:+ value = value.replace(+ f"{{{{{placeholder}}}}}", str(replacement)+ )+ return value+ elif isinstance(value, dict):+ return {k: replace_value(v) for k, v in value.items()}+ elif isinstance(value, list):+ return [replace_value(item) for item in value]+ else:+ return value++ return replace_value(_content)+++def process_includes(_content, _base_path, _backup_dirs, **kwargs):# Regex to find {{ include 'path' }} or {{include'path'}}include_pattern = re.compile(r"{{\s*include\s*['\"](.*?)['\"]\s*}}")def replace_include(match):include_path = match.group(1)# First attempt to resolve the include relative to the base path- full_include_path = find_file_in_dirs(os.path.join(base_path, include_path), backup_dirs)-+ full_include_path = find_file_in_dirs(+ os.path.join(_base_path, include_path), _backup_dirs+ )+# Recursively read the included file content, keeping the original base path- included_content = read_file(full_include_path, backup_dirs, **kwargs)+ included_content = read_file(full_include_path, _backup_dirs, **kwargs)return included_content# Replace all includes with the file content- return re.sub(include_pattern, replace_include, content)+ return re.sub(include_pattern, replace_include, _content)+def find_file_in_dirs(file_path, backup_dirs):"""@@ -58,43 +123,82 @@ def find_file_in_dirs(file_path, backup_dirs):return get_abs_path(backup_path)# If the file is not found, let it raise the FileNotFoundError- raise FileNotFoundError(f"File '{file_path}' not found in the original path or backup directories.")+ raise FileNotFoundError(+ f"File '{file_path}' not found in the original path or backup directories."+ )+++import re+def remove_code_fences(text):- return re.sub(r'~~~\w*\n|~~~', '', text)+ # Pattern to match code fences with optional language specifier+ pattern = r"(```|~~~)(.*?\n)(.*?)(\1)"++ # Function to replace the code fences+ def replacer(match):+ return match.group(3) # Return the code without fences++ # Use re.DOTALL to make '.' match newlines+ result = re.sub(pattern, replacer, text, flags=re.DOTALL)++ return result+++import re+-def write_file(relative_path:str, content:str, encoding:str="utf-8"):+def is_full_json_template(text):+ # Pattern to match the entire text enclosed in ```json or ~~~json fences+ pattern = r"^\s*(```|~~~)\s*json\s*\n(.*?)\n\1\s*$"+ # Use re.DOTALL to make '.' match newlines+ match = re.fullmatch(pattern, text.strip(), flags=re.DOTALL)+ return bool(match)+++def write_file(relative_path: str, content: str, encoding: str = "utf-8"):abs_path = get_abs_path(relative_path)os.makedirs(os.path.dirname(abs_path), exist_ok=True)- with open(abs_path, 'w', encoding=encoding) as f:+ with open(abs_path, "w", encoding=encoding) as f:f.write(content)-def delete_file(relative_path:str):++def delete_file(relative_path: str):abs_path = get_abs_path(relative_path)if os.path.exists(abs_path):os.remove(abs_path)-def list_files(relative_path:str, filter:str="*"):++def list_files(relative_path: str, filter: str = "*"):abs_path = get_abs_path(relative_path)if not os.path.exists(abs_path):return []return [file for file in os.listdir(abs_path) if fnmatch(file, filter)]+def get_abs_path(*relative_paths):return os.path.join(get_base_dir(), *relative_paths)+def exists(*relative_paths):path = get_abs_path(*relative_paths)return os.path.exists(path)+def get_base_dir():# Get the base directory from the current file path- base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__,"../../")))+ base_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "../../")))return base_dir-def get_subdirectories(relative_path:str, include:str="*", exclude=None):++def get_subdirectories(relative_path: str, include: str = "*", exclude=None):abs_path = get_abs_path(relative_path)if not os.path.exists(abs_path):return []- return [subdir for subdir in os.listdir(abs_path) if os.path.isdir(os.path.join(abs_path, subdir)) and fnmatch(subdir, include) and (exclude is None or not fnmatch(subdir, exclude))]-+ return [+ subdir+ for subdir in os.listdir(abs_path)+ if os.path.isdir(os.path.join(abs_path, subdir))+ and fnmatch(subdir, include)+ and (exclude is None or not fnmatch(subdir, exclude))+ ]diff --git a/python/helpers/history.py b/python/helpers/history.pyindex 895ea0cfb..5617bc480 100644--- a/python/helpers/history.py+++ b/python/helpers/history.py@@ -1,40 +1,468 @@from abc import abstractmethod-from python.helpers import tokens+import asyncio+from collections import OrderedDict+import json+import math+from typing import Coroutine, Literal, TypedDict, cast+from python.helpers import messages, tokens, settings, call_llm+from enum import Enum+from langchain_core.messages import HumanMessage, SystemMessage, AIMessage-class Record():- def __init__(self):+BULK_MERGE_COUNT = 3+TOPICS_KEEP_COUNT = 3+CURRENT_TOPIC_RATIO = 0.5+HISTORY_TOPIC_RATIO = 0.3+HISTORY_BULK_RATIO = 0.2+TOPIC_COMPRESS_RATIO = 0.65+LARGE_MESSAGE_TO_TOPIC_RATIO = 0.25++OutputType = (+ list["OutputType"]+ | OrderedDict[str, "OutputType"]+ | list[OrderedDict[str, "OutputType"]]+ | str+ | list[str]+)+++class OutputMessage(TypedDict):+ ai: bool+ content: OutputType+++class Record:+ def __init__(self):pass- @abstractmethoddef get_tokens(self) -> int:+ out = self.output_text()+ return tokens.approximate_tokens(out)++ @abstractmethod+ async def compress(self) -> bool:+ pass++ @abstractmethod+ def output(self) -> list[OutputMessage]:pass+ @abstractmethod+ async def summarize(self) -> str:+ pass++ @abstractmethod+ def to_dict(self) -> dict:+ pass++ @staticmethod+ def from_dict(data: dict, history: "History"):+ cls = data["_cls"]+ return globals()[cls].from_dict(data, history=history)++ def output_langchain(self):+ return output_langchain(self.output())++ def output_text(self, human_label="user", ai_label="ai"):+ return output_text(self.output(), ai_label, human_label)++class Message(Record):- def __init__(self):- self.segments: list[str]- self.human: bool+ def __init__(self, ai: bool, text: str | None = None, **kwargs: OutputType):+ self.ai = ai+ self.text = text+ self.kwargs: OrderedDict[str, OutputType] = OrderedDict(**kwargs)+ self.summary: OutputType = ""+ async def compress(self):+ return False-class Monologue:- def __init__(self):+ def output(self):+ return [+ OutputMessage(ai=self.ai, content=self.summary or self.text or self.kwargs)+ ]++ def output_langchain(self):+ return output_langchain(self.output())++ def output_text(self, human_label="user", ai_label="ai"):+ return output_text(self.output(), ai_label, human_label)++ def to_dict(self):+ return {+ "_cls": "Message",+ "ai": self.ai,+ "text": self.text,+ "kwargs": self.kwargs,+ "summary": self.summary,+ }++ @staticmethod+ def from_dict(data: dict, history: "History"):+ msg = Message(ai=data["ai"], text=data["text"], **data["kwargs"])+ msg.summary = data["summary"]+ return msg+++class Topic(Record):+ def __init__(self, history: "History"):+ self.history = historyself.summary: str = ""self.messages: list[Message] = []- def finish(self):- pass+ def add_message(self, ai: bool, text: str | None = None, **kwargs):+ msg = Message(ai=ai, text=text, **kwargs)+ self.messages.append(msg)+ return msg+ def output(self) -> list[OutputMessage]:+ if self.summary:+ return [OutputMessage(ai=False, content=self.summary)]+ else:+ msgs = [m for r in self.messages for m in r.output()]+ return group_outputs_abab(msgs)-class History:- def __init__(self):- self.monologues: list[Monologue] = []- self.messages: list[Message] = []- self.start_monologue()+ async def summarize(self):+ self.summary = await self.summarize_messages(self.messages)+ return self.summary++ async def compress_large_messages(self) -> bool:+ set = settings.get_settings()+ msg_max_size = (+ set["chat_model_ctx_length"]+ * set["chat_model_ctx_history"]+ * HISTORY_TOPIC_RATIO+ * LARGE_MESSAGE_TO_TOPIC_RATIO+ )+ large_msgs = []+ for m in self.messages:+ out = m.output()+ text = output_text(out)+ tok = tokens.approximate_tokens(text)+ leng = len(text)+ if leng > msg_max_size:+ large_msgs.append((m, tok, leng, out))+ large_msgs.sort(key=lambda x: x[1], reverse=True)+ 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, trim_to_chars * 1.15, trim_to_chars * 0.85+ )+ msg.summary = trunc++ return True+ return False++ async def compress(self) -> bool:+ compress = await self.compress_large_messages()+ if not compress:+ compress = await self.compress_attention()+ return compress++ async def compress_attention(self) -> bool:++ if len(self.messages) > 2:+ 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(+ "fw.msg_summary.md", summary=summary+ )+ sum_msg = Message(False, *sum_msg_args, **sum_msg_kwargs)+ self.messages[1 : cnt_to_sum + 1] = [sum_msg]+ return True+ return False++ async def summarize_messages(self, messages: list[Message]):+ msg_txt = [m.output_text() for m in messages]+ summary = await call_llm.call_llm(+ system=self.history.agent.read_prompt("fw.topic_summary.sys.md"),+ message=self.history.agent.read_prompt(+ "fw.topic_summary.msg.md", content=msg_txt+ ),+ model=settings.get_utility_model(),+ )+ return summary++ def to_dict(self):+ return {+ "_cls": "Topic",+ "summary": self.summary,+ "messages": [m.to_dict() for m in self.messages],+ }++ @staticmethod+ def from_dict(data: dict, history: "History"):+ topic = Topic(history=history)+ topic.summary = data["summary"]+ topic.messages = [+ Message.from_dict(m, history=history) for m in data["messages"]+ ]+ return topic+++class Bulk(Record):+ def __init__(self, history: "History"):+ self.history = history+ self.summary: str = ""+ self.records: list[Record] = []++ def output(+ self, human_label: str = "user", ai_label: str = "ai"+ ) -> list[OutputMessage]:+ if self.summary:+ return [OutputMessage(ai=False, content=self.summary)]+ else:+ msgs = [m for r in self.records for m in r.output()]+ return group_outputs_abab(msgs)++ async def compress(self):+ return False++ async def summarize(self):+ self.summary = await call_llm.call_llm(+ system=self.history.agent.read_prompt("fw.topic_summary.sys.md"),+ message=self.history.agent.read_prompt(+ "fw.topic_summary.msg.md", content=self.output_text()+ ),+ model=settings.get_utility_model(),+ )+ return self.summary++ def to_dict(self):+ return {+ "_cls": "Bulk",+ "summary": self.summary,+ "records": [r.to_dict() for r in self.records],+ }++ @staticmethod+ def from_dict(data: dict, history: "History"):+ bulk = Bulk(history=history)+ bulk.summary = data["summary"]+ cls = data["_cls"]+ bulk.records = [Record.from_dict(r, history=history) for r in data["records"]]+ return bulk+++class History(Record):+ def __init__(self, agent):+ from agent import Agent++ self.bulks: list[Bulk] = []+ self.topics: list[Topic] = []+ self.current = Topic(history=self)+ self.agent: Agent = agent++ def is_over_limit(self):+ limit = get_ctx_size_for_history()+ total = self.get_tokens()+ return total > limit++ def get_bulks_tokens(self) -> int:+ return sum(record.get_tokens() for record in self.bulks)++ def get_topics_tokens(self) -> int:+ return sum(record.get_tokens() for record in self.topics)++ def get_current_topic_tokens(self) -> int:+ return self.current.get_tokens()++ def get_tokens(self) -> int:+ return (+ self.get_bulks_tokens()+ + self.get_topics_tokens()+ + 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 new_topic(self):+ if self.current.messages:+ self.topics.append(self.current)+ self.current = Topic(history=self)++ def output(self) -> list[OutputMessage]:+ result: list[OutputMessage] = []+ result += [m for b in self.bulks for m in b.output()]+ result += [m for t in self.topics for m in t.output()]+ result += self.current.output()+ result = group_outputs_abab(result)+ return result++ @staticmethod+ def from_dict(data: dict, history: "History"):+ history.bulks = [Bulk.from_dict(b, history=history) for b in data["bulks"]]+ history.topics = [Topic.from_dict(t, history=history) for t in data["topics"]]+ history.current = Topic.from_dict(data["current"], history=history)+ return history++ def to_dict(self):+ return {+ "_cls": "History",+ "bulks": [b.to_dict() for b in self.bulks],+ "topics": [t.to_dict() for t in self.topics],+ "current": self.current.to_dict(),+ }++ def serialize(self):+ data = self.to_dict()+ return json.dumps(data)++ async def compress(self):+ curr, hist, bulk = (+ self.get_current_topic_tokens(),+ self.get_topics_tokens(),+ self.get_bulks_tokens(),+ )+ total = get_ctx_size_for_history()+ compressed = False++ # calculate ratios of individual parts+ ratios = [+ (curr, CURRENT_TOPIC_RATIO, "current_topic"),+ (hist, HISTORY_TOPIC_RATIO, "history_topic"),+ (bulk, HISTORY_BULK_RATIO, "history_bulk"),+ ]+ # start from the most oversized part and compress it+ ratios = sorted(ratios, key=lambda x: (x[0] / total) / x[1], reverse=True)+ for ratio in ratios:+ if ratio[0] > ratio[1] * total:+ over_part = ratio[2]+ if over_part == "current_topic":+ compressed = await self.current.compress()+ elif over_part == "history_topic":+ compressed = await self.compress_topics()+ else:+ compressed = await self.compress_bulks()+ # if part was compressed, stop the loop and try the whole function again, maybe no more compression is necessary+ if compressed:+ break+ else:+ break++ # try the whole function again to see if there is still a need for compression+ if compressed:+ await self.compress()++ return compressed++ async def compress_topics(self) -> bool:+ # summarize topics one by one+ for topic in self.topics:+ if not topic.summary:+ await topic.summarize()+ return True++ # move oldest topic to bulks and summarize+ for topic in self.topics:+ bulk = Bulk(history=self)+ bulk.records.append(topic)+ if topic.summary:+ bulk.summary = topic.summary+ else:+ await bulk.summarize()+ self.bulks.append(bulk)+ self.topics.remove(topic)+ return True++ async def compress_bulks(self):+ compressed = await self.merge_bulks_by(BULK_MERGE_COUNT)+ return compressed++ async def merge_bulks_by(self, count: int):+ if len(self.bulks) < count:+ return False+ bulks = await asyncio.gather(+ *[+ self.merge_bulks(self.bulks[i : i + count])+ for i in range(0, len(self.bulks), count)+ ]+ )+ self.bulks = bulks+ return True++ async def merge_bulks(self, bulks: list[Bulk]) -> Bulk:+ bulk = Bulk(history=self)+ bulk.records = cast(list[Record], bulks)+ await bulk.summarize()+ return bulk+++def deserialize_history(json_data: str, agent) -> History:+ history = History(agent=agent)+ if json_data:+ data = json.loads(json_data)+ history = History.from_dict(data, history=history)+ return history+++def get_ctx_size_for_history() -> int:+ set = settings.get_settings()+ return int(set["chat_model_ctx_length"] * set["chat_model_ctx_history"])+++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:+ if isinstance(content, str):+ return content+ return json.dumps(content)+++def group_outputs_abab(outputs: list[OutputMessage]) -> list[OutputMessage]:+ result = []+ for out in outputs:+ if result and result[-1]["ai"] == out["ai"]:+ result[-1] = OutputMessage(+ ai=result[-1]["ai"],+ content=merge_outputs(result[-1]["content"], out["content"]),+ )+ else:+ result.append(out)+ return result+++def output_langchain(messages: list[OutputMessage]):+ result = []+ for m in messages:+ if m["ai"]:+ result.append(AIMessage(content=serialize_content(m["content"])))+ else:+ result.append(HumanMessage(content=serialize_content(m["content"])))+ return result+++def output_text(messages: list[OutputMessage], ai_label="ai", human_label="human"):+ return "\n".join(serialize_output(o, ai_label, human_label) for o in messages)+++def merge_outputs(a: OutputType, b: OutputType) -> OutputType:+ if not isinstance(a, list):+ a = [a]+ if not isinstance(b, list):+ b = [b]+ return a + b # type: ignore+ # return merge_properties(a, b)- def current_monologue(self):- return self.monologues[-1]- def start_monologue(self):- if self.monologues:- self.current_monologue().finish()- self.monologues.append(Monologue())- return self.current_monologue()+def merge_properties(a: OutputType, b: OutputType) -> OutputType:+ if isinstance(a, list):+ if isinstance(b, list):+ return a + b # type: ignore+ else:+ return a + [b]+ elif isinstance(b, list):+ return [a] + b # type: ignore+ elif isinstance(a, dict) and isinstance(b, dict):+ for key, value in b.items():+ if key in a:+ a[key] = merge_properties(a[key], value)+ else:+ a[key] = value+ return a+ elif isinstance(a, str) and isinstance(b, str):+ return a + b+ raise ValueError(f"Cannot merge {a} and {b}")diff --git a/python/helpers/messages.py b/python/helpers/messages.pyindex 6033e2532..b9f66ad43 100644--- a/python/helpers/messages.py+++ b/python/helpers/messages.py@@ -1,15 +1,75 @@# from . import files+import json++def truncate_text(agent, output, threshold=1000):+ threshold = int(threshold)if len(output) <= threshold:return output# Adjust the file path as needed- placeholder = agent.read_prompt("fw.msg_truncated.md", length=(len(output) - threshold))+ placeholder = agent.read_prompt(+ "fw.msg_truncated.md", length=(len(output) - threshold)+ )# placeholder = files.read_file("./prompts/default/fw.msg_truncated.md", length=(len(output) - threshold))start_len = (threshold - len(placeholder)) // 2end_len = threshold - len(placeholder) - start_lentruncated_output = output[:start_len] + placeholder + output[-end_len:]- return truncated_output\ No newline at end of file+ return truncated_output+++def truncate_dict_by_ratio(agent, data: dict|list|str, threshold_chars: int, truncate_to: int):+ threshold_chars = int(threshold_chars)+ truncate_to = int(truncate_to)++ def process_item(item):+ if isinstance(item, dict):+ truncated_dict = {}+ cumulative_size = 0++ for key, value in item.items():+ processed_value = process_item(value)+ serialized_value = json.dumps(processed_value, ensure_ascii=False)+ size = len(serialized_value)++ if cumulative_size + size > threshold_chars:+ truncated_dict[key] = truncate_text(+ agent, serialized_value, truncate_to+ )+ else:+ cumulative_size += size+ truncated_dict[key] = processed_value++ return truncated_dict++ elif isinstance(item, list):+ truncated_list = []+ cumulative_size = 0++ for value in item:+ processed_value = process_item(value)+ serialized_value = json.dumps(processed_value, ensure_ascii=False)+ size = len(serialized_value)++ if cumulative_size + size > threshold_chars:+ truncated_list.append(+ truncate_text(agent, serialized_value, truncate_to)+ )+ else:+ cumulative_size += size+ truncated_list.append(processed_value)++ return truncated_list++ elif isinstance(item, str):+ if len(item) > threshold_chars:+ return truncate_text(agent, item, truncate_to)+ return item++ else:+ return item++ return process_item(data)diff --git a/python/helpers/persist_chat.py b/python/helpers/persist_chat.pyindex 3939337a7..2906d1380 100644--- a/python/helpers/persist_chat.py+++ b/python/helpers/persist_chat.py@@ -2,7 +2,7 @@ from collections import OrderedDictfrom typing import Anyimport uuidfrom agent import Agent, AgentConfig, AgentContext, HumanMessage, AIMessage-from python.helpers import files+from python.helpers import files, historyimport jsonfrom initialize import initialize@@ -18,6 +18,7 @@ def save_tmp_chat(context: AgentContext):js = _safe_json_serialize(data, ensure_ascii=False)files.write_file(relative_path, js)+def load_tmp_chats():json_files = files.list_files("tmp/chats", "*.json")ctxids = []@@ -29,20 +30,24 @@ def load_tmp_chats():ctxids.append(ctx.id)return ctxids+def load_json_chats(jsons: list[str]):ctxids = []for js in jsons:data = json.loads(js)- if "id" in data: del data["id"] # remove id to get new+ if "id" in data:+ del data["id"] # remove id to get newctx = _deserialize_context(data)ctxids.append(ctx.id)return ctxids+def export_json_chat(context: AgentContext):data = _serialize_context(context)js = _safe_json_serialize(data, ensure_ascii=False)return js+def remove_chat(ctxid):files.delete_file(_get_file_path(ctxid))@@ -57,7 +62,7 @@ def _serialize_context(context: AgentContext):agent = context.agent0while agent:agents.append(_serialize_agent(agent))- agent = agent.data.get("subordinate", None)+ agent = agent.data.get(Agent.DATA_NAME_SUBORDINATE, None)return {"id": context.id,@@ -70,15 +75,9 @@ def _serialize_context(context: AgentContext):def _serialize_agent(agent: Agent):- data = {**agent.data}- if "superior" in data:- del data["superior"]- if "subordinate" in data:- del data["subordinate"]+ data = {k: v for k, v in agent.data.items() if not k.startswith("_")}- history = []- for msg in agent.history:- history.append({"type": msg.type, "content": msg.content})+ history = agent.history.serialize()return {"number": agent.number,@@ -90,8 +89,9 @@ def _serialize_agent(agent: Agent):def _serialize_log(log: Log):return {"guid": log.guid,- "logs": [item.output() for item in log.logs[-LOG_SIZE:]]-, # serialize LogItem objects+ "logs": [+ item.output() for item in log.logs[-LOG_SIZE:]+ ], # serialize LogItem objects"progress": log.progress,"progress_no": log.progress_no,}@@ -103,7 +103,7 @@ def _deserialize_context(data):context = AgentContext(config=config,- id=data.get("id", None), #get new id+ id=data.get("id", None), # get new idname=data.get("name", None),log=log,paused=False,@@ -115,8 +115,8 @@ def _deserialize_context(data):agent0 = _deserialize_agents(agents, config, context)streaming_agent = agent0while streaming_agent.number != data.get("streaming_agent", 0):- streaming_agent = streaming_agent.data.get("subordinate", None)-+ streaming_agent = streaming_agent.data.get(Agent.DATA_NAME_SUBORDINATE, None)+context.agent0 = agent0context.streaming_agent = streaming_agent@@ -136,53 +136,56 @@ def _deserialize_agents(context=context,)current.data = ag.get("data", {})- current.history = _deserialize_history(ag.get("history", []))-+ current.history = history.deserialize_history(+ ag.get("history", ""), agent=current+ )if not zero:zero = currentif prev:- prev.set_data("subordinate", current)- current.set_data("superior", prev)+ prev.set_data(Agent.DATA_NAME_SUBORDINATE, current)+ current.set_data(Agent.DATA_NAME_SUPERIOR, prev)prev = currentreturn zero or Agent(0, config, context)-def _deserialize_history(history: list[dict[str, Any]]):- result = []- for hist in history:- content = hist.get("content", "")- msg = (- HumanMessage(content=content)- if hist.get("type") == "human"- else AIMessage(content=content)- )- result.append(msg)- return result+# def _deserialize_history(history: list[dict[str, Any]]):+# result = []+# for hist in history:+# content = hist.get("content", "")+# msg = (+# HumanMessage(content=content)+# if hist.get("type") == "human"+# else AIMessage(content=content)+# )+# result.append(msg)+# return resultdef _deserialize_log(data: dict[str, Any]) -> "Log":log = Log()log.guid = data.get("guid", str(uuid.uuid4()))- log.progress = "" #data.get("progress", "")+ log.progress = "" # data.get("progress", "")log.progress_no = data.get("progress_no", 0)# Deserialize the list of LogItem objectsi = 0for item_data in data.get("logs", []):- log.logs.append(LogItem(- log=log, # restore the log reference- no=item_data["no"],- type=item_data["type"],- heading=item_data.get("heading", ""),- content=item_data.get("content", ""),- kvps=OrderedDict(item_data["kvps"]) if item_data["kvps"] else None,- temp=item_data.get("temp", False),- ))+ log.logs.append(+ LogItem(+ log=log, # restore the log reference+ no=item_data["no"],+ type=item_data["type"],+ heading=item_data.get("heading", ""),+ content=item_data.get("content", ""),+ kvps=OrderedDict(item_data["kvps"]) if item_data["kvps"] else None,+ temp=item_data.get("temp", False),+ )+ )log.updates.append(i)i += 1-+return logdiff --git a/python/helpers/rate_limiter.py b/python/helpers/rate_limiter.pyindex f28bdc98d..264352e5a 100644--- a/python/helpers/rate_limiter.py+++ b/python/helpers/rate_limiter.py@@ -51,6 +51,7 @@ class RateLimiter:if wait_time > 0:PrintStyle(font_color="yellow", padding=True).print(f"Rate limit exceeded. Waiting for {wait_time:.2f} seconds due to: {', '.join(wait_reasons)}")self.logger.log("rate_limit","Rate limit exceeded",f"Rate limit exceeded. Waiting for {wait_time:.2f} seconds due to: {', '.join(wait_reasons)}")+ # TODO rate limit log typetime.sleep(wait_time)current_time = time.time()diff --git a/python/helpers/settings.py b/python/helpers/settings.pyindex c4c7d90bc..ede754fd3 100644--- a/python/helpers/settings.py+++ b/python/helpers/settings.py@@ -15,6 +15,8 @@ class Settings(TypedDict):chat_model_name: strchat_model_temperature: floatchat_model_kwargs: dict[str, str]+ chat_model_ctx_length: int+ chat_model_ctx_history: floatutil_model_provider: strutil_model_name: str@@ -120,6 +122,28 @@ def convert_out(settings: Settings) -> SettingsOutput:})+ chat_model_fields.append(+ {+ "id": "chat_model_ctx_length",+ "title": "Chat model context length",+ "description": "Maximum number of tokens in the context window for LLM. System prompt, chat history, RAG and response all count towards this limit.",+ "type": "input",+ "value": settings["chat_model_ctx_length"],+ })++ chat_model_fields.append(+ {+ "id": "chat_model_ctx_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,+ "max": 1,+ "step": 0.01,+ "value": settings["chat_model_ctx_history"],+ })++chat_model_section: SettingsSection = {"title": "Chat Model","description": "Selection and settings for main chat model used by Agent Zero",@@ -434,7 +458,8 @@ def get_settings() -> Settings:_settings = _read_settings_file()if not _settings:_settings = _get_default_settings()- return _settings.copy()+ norm = normalize_settings(_settings)+ return normdef set_settings(settings: Settings):@@ -450,9 +475,15 @@ def normalize_settings(settings: Settings) -> Settings:for key, value in default.items():if key not in copy:copy[key] = value+ else:+ try:+ copy[key] = type(value)(copy[key]) # type: ignore+ except (ValueError, TypeError):+ passreturn copy+def get_chat_model(settings: Settings | None = None) -> BaseChatModel:if not settings:settings = get_settings()@@ -525,6 +556,8 @@ def _get_default_settings() -> Settings:chat_model_name="gpt-4o-mini",chat_model_temperature=0,chat_model_kwargs={},+ chat_model_ctx_length=8192,+ chat_model_ctx_history=0.65,util_model_provider=ModelProvider.OPENAI.name,util_model_name="gpt-4o-mini",util_model_temperature=0,@@ -555,7 +588,7 @@ def _apply_settings():agent = ctx.agent0while agent:agent.config = ctx.config- agent = agent.get_data("subordinate")+ agent = agent.get_data(agent.DATA_NAME_SUBORDINATE)def _env_to_dict(data: str):diff --git a/python/helpers/tool.py b/python/helpers/tool.pyindex c89a74ad1..8e96b790a 100644--- a/python/helpers/tool.py+++ b/python/helpers/tool.py@@ -32,8 +32,7 @@ class Tool:async def after_execution(self, response: Response, **kwargs):text = messages.truncate_text(self.agent, response.message.strip(), self.agent.config.max_tool_response_length)- msg_response = self.agent.read_prompt("fw.tool_response.md", tool_name=self.name, tool_response=text)- await self.agent.append_message(msg_response, human=True)+ await self.agent.hist_add_tool_result(self.name, text)PrintStyle(font_color="#1B4F72", background_color="white", padding=True, bold=True).print(f"{self.agent.agent_name}: Response from tool '{self.name}'")PrintStyle(font_color="#85C1E9").print(response.message)self.log.update(content=response.message)diff --git a/python/tools/call_subordinate.py b/python/tools/call_subordinate.pyindex 227e87a03..56302baa8 100644--- a/python/tools/call_subordinate.py+++ b/python/tools/call_subordinate.py@@ -1,14 +1,26 @@from agent import Agentfrom python.helpers.tool import Tool, Response+class Delegation(Tool):async def execute(self, message="", reset="", **kwargs):# create subordinate agent using the data object on this agent and set superior agent to his data object- if self.agent.get_data("subordinate") is None or str(reset).lower().strip() == "true":- subordinate = Agent(self.agent.number+1, self.agent.config, self.agent.context)- subordinate.set_data("superior", self.agent)- self.agent.set_data("subordinate", subordinate)- # run subordinate agent message loop- subordinate: Agent = self.agent.get_data("subordinate")- return Response( message= await subordinate.monologue(message), break_loop=False)\ No newline at end of file+ if (+ self.agent.get_data(Agent.DATA_NAME_SUBORDINATE) is None+ or str(reset).lower().strip() == "true"+ ):+ sub = Agent(+ self.agent.number + 1, self.agent.config, self.agent.context+ )+ sub.set_data(Agent.DATA_NAME_SUPERIOR, self.agent)+ self.agent.set_data(Agent.DATA_NAME_SUBORDINATE, sub)++ # add user message to subordinate agent+ subordinate: Agent = self.agent.get_data(Agent.DATA_NAME_SUBORDINATE)+ template = self.agent.read_prompt("fw.call_subordinate.md", message=message)+ await subordinate.hist_add_user_message(template)+ # run subordinate monologue+ result = await subordinate.monologue()+ # result+ return Response(message=result, break_loop=False)diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex f26c3671b..e9fc8b35d 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -72,10 +72,7 @@ class CodeExecution(Tool):PrintStyle().print()async def after_execution(self, response, **kwargs):- msg_response = self.agent.read_prompt(- "fw.tool_response.md", tool_name=self.name, tool_response=response.message- )- await self.agent.append_message(msg_response, human=True)+ await self.agent.hist_add_tool_result(self.name, response.message)async def prepare_state(self, reset=False):self.state = self.agent.get_data("cot_state")diff --git a/run_ui.py b/run_ui.pyindex 9fffa60bf..117e0e774 100644--- a/run_ui.py+++ b/run_ui.py@@ -1,24 +1,14 @@-import jsonfrom functools import wrapsimport os-from pathlib import Pathimport threading-import uuid-from flask import Flask, request, jsonify, Response, send_file+from flask import Flask, request, Responsefrom flask_basicauth import BasicAuth-from agent import AgentContext-from initialize import initializefrom python.helpers import files, gitfrom python.helpers.files import get_abs_path-from python.helpers.print_style import PrintStyle-from python.helpers.dotenv import load_dotenv-from python.helpers import persist_chat, settings, whisper, rfc, runtime, dotenv-import base64-from werkzeug.utils import secure_filename+from python.helpers import persist_chat, runtime, dotenvfrom python.helpers.cloudflare_tunnel import CloudflareTunnelfrom python.helpers.extract_tools import load_classes_from_folderfrom python.helpers.api import ApiHandler-from python.helpers.file_browser import FileBrowser# initialize the internal Flask serverdiff --git a/webui/history.css b/webui/history.cssnew file mode 100644index 000000000..e026d7be2--- /dev/null+++ b/webui/history.css@@ -0,0 +1,5 @@+#json-viewer-container{+ width: 100%;+ height: 60vh;+ overflow: auto;+}\ No newline at end of filediff --git a/webui/history.js b/webui/history.jsnew file mode 100644index 000000000..5332e11ab--- /dev/null+++ b/webui/history.js@@ -0,0 +1,41 @@+import { getContext } from "./index.js";++export async function openHistoryModal() {+ const hist = await window.sendJsonData("/history_get", { context: getContext() });+ const data = JSON.stringify(hist.history, null, 4);+ const size = hist.tokens+ await showEditorModal(data, "json", `History ~${size} tokens`,"Conversation history how the agent can see it. History is compressed to fit into the context window.");+}++export async function openCtxWindowModal() {+ const win = await window.sendJsonData("/ctx_window_get", { context: getContext() });+ const data = win.content+ const size = win.tokens+ await showEditorModal(data, "text", `Context window ~${size} tokens`,"Data passed to the LLM during last interaction. Contains system message, conversation history and RAG.");+}++async function showEditorModal(data, type = "json", title, description="") {+ // Generate the HTML with JSON Viewer container+ const html = `<div id="json-viewer-container"></div>`;++ // Open the modal with the generated HTML+ await window.genericModalProxy.openModal(title, description, html);++ // Initialize the JSON Viewer after the modal is rendered+ const container = document.getElementById("json-viewer-container");+ if (container) {+ const editor = ace.edit("json-viewer-container");++ const dark = localStorage.getItem('darkMode')+ if (dark != "false") {+ editor.setTheme("ace/theme/monokai");+ }++ editor.session.setMode("ace/mode/" + type);+ editor.setValue(data);+ // editor.session.$toggleFoldWidget(5, {})+ }+}++window.openHistoryModal = openHistoryModal;+window.openCtxWindowModal = openCtxWindowModal;diff --git a/webui/index.html b/webui/index.htmlindex fc4002f21..f5bccf1c5 100644--- a/webui/index.html+++ b/webui/index.html@@ -10,6 +10,7 @@<link rel="stylesheet" href="settings.css"><link rel="stylesheet" href="file_browser.css"><link rel="stylesheet" href="speech.css">+ <link rel="stylesheet" href="history.css"><script>window.safeCall = function (name, ...args) {@@ -19,6 +20,7 @@<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js" type="text/javascript" charset="utf-8"></script><!-- KaTeX CSS --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">@@ -31,7 +33,9 @@<script type="module" src="index.js"></script><script type="text/javascript" src="settings.js"></script><script type="text/javascript" src="file_browser.js"></script>+ <script type="text/javascript" src="modal.js"></script><script type="module" src="speech.js"></script>+ <script type="module" src="history.js"></script></head>@@ -305,21 +309,6 @@<!-- Bottom row with text buttons --><div class="text-buttons-row">- <button class="text-button" @click="loadKnowledge()"><svg xmlns="http://www.w3.org/2000/svg"- fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">- <path stroke-linecap="round" stroke-linejoin="round"- d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5">- </path>- </svg>Import knowledge</button>- <button class="text-button" id="work_dir_browser" @click="fileBrowserModalProxy.openModal()">- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 123.37 92.59">- <path- d="m5.72,11.5l-3.93,8.73h119.77s-3.96-8.73-3.96-8.73h-60.03c-1.59,0-2.88-1.29-2.88-2.88V1.75H13.72v6.87c0,1.59-1.29,2.88-2.88,2.88h-5.12Z"- fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>- <path- 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="5"></path>- </svg>work_dir Browser</button><button class="text-button" @click="pauseAgent(!paused)"><!-- Dynamic path that switches between pause and play icons -->@@ -338,6 +327,43 @@</svg><span x-text="paused ? 'Resume Agent' : 'Pause Agent'"></span></button>++ <button class="text-button" @click="loadKnowledge()"><svg xmlns="http://www.w3.org/2000/svg"+ fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">+ <path stroke-linecap="round" stroke-linejoin="round"+ d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5">+ </path>+ </svg>Import knowledge</button>+ <button class="text-button" id="work_dir_browser" @click="fileBrowserModalProxy.openModal()">+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 123.37 92.59">+ <path+ d="m5.72,11.5l-3.93,8.73h119.77s-3.96-8.73-3.96-8.73h-60.03c-1.59,0-2.88-1.29-2.88-2.88V1.75H13.72v6.87c0,1.59-1.29,2.88-2.88,2.88h-5.12Z"+ fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>+ <path+ 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="5"></path>+ </svg>work_dir Browser</button>++ <button class="text-button" id="history_inspect" @click="window.openHistoryModal()">+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 123.37 92.59">+ <path+ d="m5.72,11.5l-3.93,8.73h119.77s-3.96-8.73-3.96-8.73h-60.03c-1.59,0-2.88-1.29-2.88-2.88V1.75H13.72v6.87c0,1.59-1.29,2.88-2.88,2.88h-5.12Z"+ fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>+ <path+ 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="5"></path>+ </svg>History</button>++ <button class="text-button" id="ctx_window" @click="window.openCtxWindowModal()">+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 123.37 92.59">+ <path+ d="m5.72,11.5l-3.93,8.73h119.77s-3.96-8.73-3.96-8.73h-60.03c-1.59,0-2.88-1.29-2.88-2.88V1.75H13.72v6.87c0,1.59-1.29,2.88-2.88,2.88h-5.12Z"+ fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>+ <path+ 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="5"></path>+ </svg>Context</button>+</div></div></div>@@ -567,6 +593,29 @@</template></div>+<!-- generic modal -->++<div id="genericModal" x-data="genericModalProxy">+ <template x-teleport="body">+ <div x-show="isOpen" class="modal-overlay" @click.self="handleClose()" @keydown.escape.window="handleClose()" x-transition>+ <div class="modal-container">+ <div class="modal-header">+ <h2 class="modal-title" x-text="title"></h2>+ <button class="modal-close" @click="handleClose()">×</button>+ </div>+ <div class="modal-description" x-text="description"></div>+ <div class="modal-content">+ <div class="html-pre" x-html="html"></div>+ </div>+ <!-- <div class="modal-footer">+ <div id="buttons-container">+ <button class="btn btn-cancel" @click="handleClose()">Close</button>+ </div>+ </div> -->+ </div>+ </template>+</div>+</body></html>\ No newline at end of filediff --git a/webui/index.js b/webui/index.jsindex c8f9fe060..4c43a86df 100644--- a/webui/index.js+++ b/webui/index.js@@ -443,7 +443,7 @@ window.selectChat = async function (id) {updateAfterScroll()}-const setContext = function (id) {+export const setContext = function (id) {if (id == context) returncontext = idlastLogGuid = ""@@ -453,6 +453,10 @@ const setContext = function (id) {chatsAD.selected = id}+export const getContext = function () {+ return context+}+window.toggleAutoScroll = async function (_autoScroll) {autoScroll = _autoScroll;}diff --git a/webui/modal.js b/webui/modal.jsnew file mode 100644index 000000000..d873a9c5c--- /dev/null+++ b/webui/modal.js@@ -0,0 +1,34 @@+const genericModalProxy = {+ isOpen: false,+ isLoading: false,++ async openModal(title, description, html) {+ const modalEl = document.getElementById('genericModal');+ const modalAD = Alpine.$data(modalEl);++ modalAD.isOpen = true;+ modalAD.title = title+ modalAD.description = description+ modalAD.html = html+ },++ handleClose() {+ this.isOpen = false;+ }+}++// Wait for Alpine to be ready+document.addEventListener('alpine:init', () => {+ Alpine.data('genericModalProxy', () => ({+ init() {+ Object.assign(this, genericModalProxy);+ // Ensure immediate file fetch when modal opens+ this.$watch('isOpen', async (value) => {+ // what now?+ });+ }+ }));+});++// Keep the global assignment for backward compatibility+window.genericModalProxy = genericModalProxy;\ No newline at end of file