Agent Zero AI framework
Memory layers - in progress
frdele4d05364951b
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 8aef7b134..50879afee 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -1,3 +1,4 @@{- "python.analysis.typeCheckingMode": "standard"+ "python.analysis.typeCheckingMode": "standard",+ "deno.enable": true}\ No newline at end of filediff --git a/agent.py b/agent.pyindex 0cdb572b6..60a07a63d 100644--- a/agent.py+++ b/agent.py@@ -15,73 +15,79 @@ import python.helpers.log as Logfrom python.helpers.dirty_json import DirtyJsonfrom python.helpers.defer import DeferredTask+class AgentContext:- _contexts: dict[str, 'AgentContext'] = {}+ _contexts: dict[str, "AgentContext"] = {}_counter: int = 0-- def __init__(self, config: 'AgentConfig', id:str|None = None, agent0: 'Agent|None' = None):++ def __init__(+ self, config: "AgentConfig", id: str | None = None, agent0: "Agent|None" = None+ ):# build contextself.id = id or str(uuid.uuid4())self.config = configself.log = Log.Log()self.agent0 = agent0 or Agent(0, self.config, self)self.paused = False- self.streaming_agent: Agent|None = None- self.process: DeferredTask|None = None+ self.streaming_agent: Agent | None = None+ self.process: DeferredTask | None = NoneAgentContext._counter += 1- self.no = AgentContext._counter+ self.no = AgentContext._counterself._contexts[self.id] = self@staticmethod- def get(id:str):+ def get(id: str):return AgentContext._contexts.get(id, None)@staticmethoddef first():- if not AgentContext._contexts: return None+ if not AgentContext._contexts:+ return Nonereturn list(AgentContext._contexts.values())[0]-@staticmethod- def remove(id:str):+ def remove(id: str):context = AgentContext._contexts.pop(id, None)- if context and context.process: context.process.kill()+ if context and context.process:+ context.process.kill()return contextdef reset(self):- if self.process: self.process.kill()+ if self.process:+ self.process.kill()self.log.reset()self.agent0 = Agent(0, self.config, self)self.streaming_agent = None- self.paused = False+ self.paused = False-def communicate(self, msg: str, broadcast_level: int = 1):- self.paused=False #unpause if paused-+ self.paused = False # unpause if paused+if self.process and self.process.is_alive():- if self.streaming_agent: current_agent = self.streaming_agent- else: current_agent = self.agent0+ if self.streaming_agent:+ current_agent = self.streaming_agent+ else:+ current_agent = self.agent0# set intervention messages to agent(s):intervention_agent = current_agent- while intervention_agent and broadcast_level !=0:+ 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("superior", None)else:- self.process = DeferredTask(self.agent0.message_loop, msg)+ self.process = DeferredTask(self.agent0.monologue, msg)return self.process--++@dataclass-class AgentConfig:+class AgentConfig:chat_model: BaseChatModel | BaseLLMutility_model: BaseChatModel | BaseLLM- embeddings_model:Embeddings+ embeddings_model: Embeddingsprompts_subdir: str = ""memory_subdir: str = ""knowledge_subdir: str = ""@@ -99,8 +105,14 @@ class AgentConfig:code_exec_docker_enabled: bool = Truecode_exec_docker_name: str = "agent-zero-exe"code_exec_docker_image: str = "frdel/agent-zero-exe:latest"- code_exec_docker_ports: dict[str,int] = field(default_factory=lambda: {"22/tcp": 50022})- code_exec_docker_volumes: dict[str, dict[str, str]] = field(default_factory=lambda: {files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"}})+ code_exec_docker_ports: dict[str, int] = field(+ default_factory=lambda: {"22/tcp": 50022}+ )+ code_exec_docker_volumes: dict[str, dict[str, str]] = field(+ default_factory=lambda: {+ files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"}+ }+ )code_exec_ssh_enabled: bool = Truecode_exec_ssh_addr: str = "localhost"code_exec_ssh_port: int = 50022@@ -108,20 +120,25 @@ class AgentConfig:code_exec_ssh_pass: str = "toor"additional: Dict[str, Any] = field(default_factory=dict)+# intervention exception class - skips rest of message loop iterationclass InterventionException(Exception):pass+# killer exception class - not forwarded to LLM, cannot be fixed on its own, ends message loop-class KillerException(Exception):+class RepairableException(Exception):pass+class Agent:-- def __init__(self, number:int, config: AgentConfig, context: AgentContext|None = None):- # agent config- self.config = config+ def __init__(+ self, number: int, config: AgentConfig, context: AgentContext | None = None+ ):++ # agent config+ self.config = config# agent contextself.context = context or AgentContext(config)@@ -133,104 +150,181 @@ class Agent:self.history = []self.last_message = ""self.intervention_message = ""- self.rate_limiter = rate_limiter.RateLimiter(self.context.log,max_calls=self.config.rate_limit_requests,max_input_tokens=self.config.rate_limit_input_tokens,max_output_tokens=self.config.rate_limit_output_tokens,window_seconds=self.config.rate_limit_seconds)- self.data = {} # free data object all the tools can use-- async def message_loop(self, msg: str):+ self.rate_limiter = rate_limiter.RateLimiter(+ self.context.log,+ max_calls=self.config.rate_limit_requests,+ max_input_tokens=self.config.rate_limit_input_tokens,+ max_output_tokens=self.config.rate_limit_output_tokens,+ window_seconds=self.config.rate_limit_seconds,+ )+ self.data = {} # free data object all the tools can use++ async def monologue(self, msg: str):try:- printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)++ loop_data: dict[str, Any] = {"message": msg, "iteration": -1, "history_from": len(self.history) }++ await self.call_extensions(+ "monologue_start", loop_data=loop_data+ ) # call monologue_start extensions++ printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)user_message = self.read_prompt("fw.user_message.md", message=msg)- await self.append_message(user_message, human=True) # Append the user's input to the history- memories = await self.fetch_memories(True)-- while True: # let the agent iterate on his thoughts until he stops by using a tool- self.context.streaming_agent = self #mark self as current streamer+ await self.append_message(user_message, human=True)++ await self.call_extensions(+ "monologue_start", message=msg+ ) # call monologue_end extensions++ while (+ True+ ): # let the agent iterate on his thoughts until he stops by using a tool++ self.context.streaming_agent = self # mark self as current streameragent_response = ""+ loop_data["iteration"] += 1- try:- system = self.read_prompt("agent.system.md", agent_name=self.agent_name) + "\n\n" + self.read_prompt("agent.tools.md")- memories = await self.fetch_memories()- if memories: system+= "\n\n"+memories+ try:- prompt = ChatPromptTemplate.from_messages([- SystemMessage(content=system),- MessagesPlaceholder(variable_name="messages") ])-- inputs = {"messages": self.history}+ # set system prompt and message history+ loop_data["system"] = [+ self.read_prompt("agent.system.md", agent_name=self.agent_name),+ self.read_prompt("agent.system.tools.md"),+ ]+ loop_data["history"] = {"messages": self.history}++ # and allow extensions to edit them+ await self.call_extensions(+ "message_loop_prompts", loop_data=loop_data+ )++ # build chain from system prompt, message history and model+ prompt = ChatPromptTemplate.from_messages(+ [+ SystemMessage(content="\n\n".join(loop_data["system"])),+ MessagesPlaceholder(variable_name="messages"),+ ]+ )chain = prompt | self.config.chat_model+ # rate limiter TODO - move to extension, make per-modelformatted_inputs = prompt.format(messages=self.history)- tokens = int(len(formatted_inputs)/4)+ tokens = int(len(formatted_inputs) / 4)self.rate_limiter.limit_call_and_input(tokens)-+# output that the agent is starting- PrintStyle(bold=True, font_color="green", padding=True, background_color="white").print(f"{self.agent_name}: Generating:")- log = self.context.log.log(type="agent", heading=f"{self.agent_name}: Generating:")-- async for chunk in chain.astream(inputs):- await self.handle_intervention(agent_response) # wait for intervention and handle it, if paused-- if isinstance(chunk, str): content = chunk- elif hasattr(chunk, "content"): content = str(chunk.content)- else: content = str(chunk)-+ PrintStyle(+ bold=True,+ font_color="green",+ padding=True,+ background_color="white",+ ).print(f"{self.agent_name}: Generating:")+ log = self.context.log.log(+ type="agent", heading=f"{self.agent_name}: Generating:"+ )++ async for chunk in chain.astream(loop_data["history"]):+ await self.handle_intervention(+ agent_response+ ) # wait for intervention and handle it, if paused++ if isinstance(chunk, str):+ content = chunk+ elif hasattr(chunk, "content"):+ content = str(chunk.content)+ else:+ content = str(chunk)+if content:- printer.stream(content) # output the agent response stream- agent_response += content # concatenate stream into the response+ printer.stream(content) # output the agent response stream+ agent_response += (+ content # concatenate stream into the response+ )self.log_from_stream(agent_response, log)- self.rate_limiter.set_output_tokens(int(len(agent_response)/4)) # rough estimation-+ self.rate_limiter.set_output_tokens(+ int(len(agent_response) / 4)+ ) # rough estimation+await self.handle_intervention(agent_response)- if self.last_message == 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+ if (+ self.last_message == 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 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.append_message(+ warning_msg, human=True+ ) # Append warning message to the historyPrintStyle(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- if tools_result: #final response of message loop available- return tools_result #break the execution if the task is done+ 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+ if tools_result: # final response of message loop available+ await self.call_extensions(+ "monologue_end", tools_result=tools_result+ ) # call monologue_end extensions+ return (+ tools_result # break the execution if the task is done+ )except InterventionException as e:- pass # intervention message has been handled in handle_intervention(), proceed with conversation loop+ pass # intervention message has been handled in handle_intervention(), proceed with conversation loopexcept asyncio.CancelledError as e:- PrintStyle(font_color="white", background_color="red", padding=True).print(f"Context {self.context.id} terminated during message loop")- raise e # process cancelled from outside, kill the loop- except KillerException as e:- error_message = errors.format_error(e)- self.context.log.log(type="error", content=error_message)- raise e # kill the loop- except Exception as e: # Forward other errors to the LLM, maybe it can fix them+ PrintStyle(+ font_color="white", background_color="red", padding=True+ ).print(f"Context {self.context.id} terminated during message loop")+ raise e # process cancelled from outside, kill the loop+ except (+ 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+ msg_response = self.read_prompt(+ "fw.error.md", error=error_message+ ) # error message templateawait 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)-+ except Exception as e: # Other exception kill the loop+ error_message = errors.format_error(e)+ PrintStyle(font_color="red", padding=True).print(error_message)+ self.context.log.log(type="error", content=error_message)+ raise e # kill the loop+finally:- self.context.streaming_agent = None # unset current streamer+ self.context.streaming_agent = None # unset current streamer- def read_prompt(self, file:str, **kwargs):+ def read_prompt(self, file: str, **kwargs):content = ""if self.config.prompts_subdir:try:- content = files.read_file(files.get_abs_path(f"./prompts/{self.config.prompts_subdir}/{file}"), **kwargs)+ content = files.read_file(+ files.get_abs_path(+ f"./prompts/{self.config.prompts_subdir}/{file}"+ ),+ **kwargs,+ )except Exception as e:passif not content:- content = files.read_file(files.get_abs_path(f"./prompts/default/{file}"), **kwargs)+ content = files.read_file(+ files.get_abs_path(f"./prompts/default/{file}"), **kwargs+ )return content- def get_data(self, field:str):+ def get_data(self, field: str):return self.data.get(field, None)- def set_data(self, field:str, value):+ def set_data(self, field: str, value):self.data[field] = valueasync def append_message(self, msg: str, human: bool = False):@@ -240,17 +334,23 @@ class Agent: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":+ 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- def concat_messages(self,messages):+ def concat_messages(self, messages):return "\n".join([f"{msg.type}: {msg.content}" for msg in messages])- async def send_adhoc_message(self, system: str, msg: str, output_label:str):- prompt = ChatPromptTemplate.from_messages([- SystemMessage(content=system),- HumanMessage(content=msg)])+ async def call_utility_llm(+ self, system: str, msg: str, log_type: Log.Type = "util", output_label: str = ""+ ):+ prompt = ChatPromptTemplate.from_messages(+ [SystemMessage(content=system), HumanMessage(content=msg)]+ )chain = prompt | self.config.utility_modelresponse = ""@@ -258,40 +358,53 @@ class Agent:logger = Noneif output_label:- PrintStyle(bold=True, font_color="orange", padding=True, background_color="white").print(f"{self.agent_name}: {output_label}:")+ PrintStyle(+ bold=True, font_color="orange", padding=True, background_color="white"+ ).print(f"{self.agent_name}: {output_label}:")printer = PrintStyle(italic=True, font_color="orange", padding=False)- logger = self.context.log.log(type="adhoc", heading=f"{self.agent_name}: {output_label}:")+ logger = self.context.log.log(+ type=log_type, heading=f"{self.agent_name}: {output_label}:"+ )formatted_inputs = prompt.format()- tokens = int(len(formatted_inputs)/4)+ tokens = int(len(formatted_inputs) / 4)self.rate_limiter.limit_call_and_input(tokens)-+async for chunk in chain.astream({}):- if self.handle_intervention(): break # wait for intervention and handle it, if paused+ await self.handle_intervention() # wait for intervention and handle it, if paused- if isinstance(chunk, str): content = chunk- elif hasattr(chunk, "content"): content = str(chunk.content)- else: content = str(chunk)+ if isinstance(chunk, str):+ content = chunk+ elif hasattr(chunk, "content"):+ content = str(chunk.content)+ else:+ content = str(chunk)- if printer: printer.stream(content)- response+=content- if logger: logger.update(content=response)+ if printer:+ printer.stream(content)+ response += content+ if logger:+ logger.update(content=response)- self.rate_limiter.set_output_tokens(int(len(response)/4))+ self.rate_limiter.set_output_tokens(int(len(response) / 4))return response-+def get_last_message(self):if self.history:return self.history[-1]- async def replace_middle_messages(self,middle_messages):+ async def replace_middle_messages(self, middle_messages):cleanup_prompt = self.read_prompt("fw.msg_cleanup.md")- summary = await self.send_adhoc_message(system=cleanup_prompt,msg=self.concat_messages(middle_messages), output_label="Mid messages cleanup summary")+ summary = await self.call_utility_llm(+ system=cleanup_prompt,+ msg=self.concat_messages(middle_messages),+ output_label="Mid messages cleanup summary",+ )new_human_message = HumanMessage(content=summary)return [new_human_message]- async def cleanup_history(self, max:int, keep_start:int, keep_end:int):+ async def cleanup_history(self, max: int, keep_start: int, keep_end: int):if len(self.history) <= max:return self.history@@ -317,14 +430,24 @@ class Agent:return self.history- async def handle_intervention(self, progress:str=""):- while self.context.paused: await asyncio.sleep(0.1) # wait if paused- if self.intervention_message: # if there is an intervention message, but not yet processed+ async def handle_intervention(self, progress: str = ""):+ while self.context.paused:+ await asyncio.sleep(0.1) # wait if paused+ if (+ self.intervention_message+ ): # if there is an intervention message, but not yet processedmsg = self.intervention_message- self.intervention_message = "" # reset the intervention message- if 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+ self.intervention_message = "" # reset the intervention message+ if 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 messageraise InterventionException(msg)async def process_tools(self, msg: str):@@ -335,65 +458,51 @@ class Agent:tool_name = tool_request.get("tool_name", "")tool_args = tool_request.get("tool_args", {})tool = self.get_tool(tool_name, tool_args, msg)-- await self.handle_intervention() # wait if paused and handle intervention message if needed++ await self.handle_intervention() # wait if paused and handle intervention message if neededawait tool.before_execution(**tool_args)- await self.handle_intervention() # wait if paused and handle intervention message if needed+ await self.handle_intervention() # wait if paused and handle intervention message if neededresponse = await tool.execute(**tool_args)- await self.handle_intervention() # wait if paused and handle intervention message if needed+ await self.handle_intervention() # wait if paused and handle intervention message if neededawait tool.after_execution(response)- await self.handle_intervention() # wait if paused and handle intervention message if needed- if response.break_loop: return response.message+ await self.handle_intervention() # wait if paused and handle intervention message if needed+ if response.break_loop:+ return response.messageelse:msg = self.read_prompt("fw.msg_misformat.md")await self.append_message(msg, human=True)PrintStyle(font_color="red", padding=True).print(msg)- self.context.log.log(type="error", content=f"{self.agent_name}: Message misformat:")+ self.context.log.log(+ type="error", content=f"{self.agent_name}: Message misformat:"+ )+ def log_from_stream(self, stream: str, logItem: Log.LogItem):+ try:+ if len(stream) < 25:+ return # no reason to try+ response = DirtyJson.parse_string(stream)+ if isinstance(response, dict):+ logItem.update(+ content=stream, kvps=response+ ) # log if result is a dictionary already+ except Exception as e:+ passdef get_tool(self, name: str, args: dict, message: str, **kwargs):- from python.tools.unknown import Unknown+ from python.tools.unknown import Unknownfrom python.helpers.tool import Tool-- tool_class = Unknown- if files.exists("python/tools",f"{name}.py"):- module = importlib.import_module("python.tools." + name) # Import the module- class_list = inspect.getmembers(module, inspect.isclass) # Get all functions in the module-- for cls in class_list:- if cls[1] is not Tool and issubclass(cls[1], Tool):- tool_class = cls[1]- break+ classes = extract_tools.load_classes_from_folder(+ "python/tools", name + ".py", Tool+ )+ tool_class = classes[0] if classes else Unknownreturn tool_class(agent=self, name=name, args=args, message=message, **kwargs)- async def fetch_memories(self,reset_skip=False):- if self.config.auto_memory_count<=0: return ""- if reset_skip: self.memory_skip_counter = 0-- if self.memory_skip_counter > 0:- self.memory_skip_counter-=1- return ""- else:- self.memory_skip_counter = self.config.auto_memory_skip- from python.tools import memory_tool- messages = self.concat_messages(self.history)- memories = memory_tool.search(self,messages)- input = {- "conversation_history" : messages,- "raw_memories": memories- }- cleanup_prompt = self.read_prompt("msg.memory_cleanup.md").replace("{", "{{")- clean_memories = await self.send_adhoc_message(cleanup_prompt,json.dumps(input), output_label="Memory injection")- return clean_memories-- def log_from_stream(self, stream: str, logItem: Log.LogItem):- try:- if len(stream) < 25: return # no reason to try- response = DirtyJson.parse_string(stream)- if isinstance(response, dict): logItem.update(content=stream, kvps=response) #log if result is a dictionary already- except Exception as e:- pass+ async def call_extensions(self, folder: str, **kwargs) -> Any:+ from python.helpers.extension import Extension- def call_extension(self, name: str, **kwargs) -> Any:- pass\ No newline at end of file+ classes = extract_tools.load_classes_from_folder(+ "python/extensions/" + folder, "*", Extension+ )+ for cls in classes:+ await cls(agent=self).execute(**kwargs)diff --git a/docker/.bashrc b/docker/exe/.bashrcsimilarity index 100%rename from docker/.bashrcrename to docker/exe/.bashrcdiff --git a/docker/Dockerfile b/docker/exe/Dockerfilesimilarity index 100%rename from docker/Dockerfilerename to docker/exe/Dockerfilediff --git a/docker/build.txt b/docker/exe/build.txtsimilarity index 100%rename from docker/build.txtrename to docker/exe/build.txtdiff --git a/docker/initialize.sh b/docker/exe/initialize.shsimilarity index 100%rename from docker/initialize.shrename to docker/exe/initialize.shdiff --git a/docker/run/.bashrc b/docker/run/.bashrcnew file mode 100644index 000000000..88a273c5f--- /dev/null+++ b/docker/run/.bashrc@@ -0,0 +1,9 @@+# .bashrc++# Source global definitions+if [ -f /etc/bashrc ]; then+ . /etc/bashrc+fi++# Activate the virtual environment+source /opt/venv/bin/activatediff --git a/docker/run/Dockerfile b/docker/run/Dockerfilenew file mode 100644index 000000000..21685d893--- /dev/null+++ b/docker/run/Dockerfile@@ -0,0 +1,42 @@+# Use the latest slim version of Debian+FROM --platform=$TARGETPLATFORM debian:bookworm-slim++# Set ARG for platform-specific commands+ARG TARGETPLATFORM++# Update and install necessary packages+RUN apt-get update && apt-get install -y \+ python3 \+ python3-pip \+ python3-venv \+ nodejs \+ npm \+ openssh-server \+ sudo \+ git \+ && rm -rf /var/lib/apt/lists/*++# Set up SSH+RUN mkdir /var/run/sshd && \+ echo 'root:toor' | chpasswd && \+ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config++# Create and activate Python virtual environment+ENV VIRTUAL_ENV=/opt/venv+RUN python3 -m venv $VIRTUAL_ENV++# Copy initial .bashrc with virtual environment activation to a temporary location+COPY .bashrc /etc/skel/.bashrc++# Copy the script to ensure .bashrc is in the root directory+COPY initialize.sh /usr/local/bin/initialize.sh+RUN chmod +x /usr/local/bin/initialize.sh++# Ensure the virtual environment and pip setup+RUN $VIRTUAL_ENV/bin/pip install --upgrade pip++# Expose SSH port+EXPOSE 22++# Init .bashrc+CMD ["/usr/local/bin/initialize.sh"]\ No newline at end of filediff --git a/docker/run/build.txt b/docker/run/build.txtnew file mode 100644index 000000000..37d76f0cb--- /dev/null+++ b/docker/run/build.txt@@ -0,0 +1 @@+docker buildx build --platform linux/amd64,linux/arm64 -t frdel/agent-zero-exe:latest --push .\ No newline at end of filediff --git a/docker/run/initialize.sh b/docker/run/initialize.shnew file mode 100644index 000000000..a0a377698--- /dev/null+++ b/docker/run/initialize.sh@@ -0,0 +1,18 @@+#!/bin/bash++# Ensure .bashrc is in the root directory+if [ ! -f /root/.bashrc ]; then+ cp /etc/skel/.bashrc /root/.bashrc+ chmod 444 /root/.bashrc+fi++# Ensure .profile is in the root directory+if [ ! -f /root/.profile ]; then+ cp /etc/skel/.bashrc /root/.profile+ chmod 444 /root/.profile+fi++apt-get update++# Start SSH service+exec /usr/sbin/sshd -Ddiff --git a/example.env b/example.envindex 0fe504d80..1663a38be 100644--- a/example.env+++ b/example.env@@ -1,4 +1,4 @@-API_KEY_OPENAI=+API_KEY_OPENAI=sk-hyBlbkFJCJjaYGCbqPTyT3uaYGCbqFBlbkFJCyJCyuPhYGCbAPI_KEY_ANTHROPIC=API_KEY_GROQ=API_KEY_PERPLEXITY=diff --git a/initialize.py b/initialize.pyindex fc6bac5d2..ff13a7c7b 100644--- a/initialize.py+++ b/initialize.py@@ -5,13 +5,13 @@ def initialize():# main chat model used by agents (smarter, more accurate)chat_llm = models.get_openai_chat(model_name="gpt-4o-mini", temperature=0)- # chat_llm = models.get_ollama_chat(model_name="gemma2:latest", temperature=0)+ # chat_llm = models.get_ollama_chat(model_name="llama3.1", temperature=0)# chat_llm = models.get_lmstudio_chat(model_name="TheBloke/Mistral-7B-Instruct-v0.2-GGUF", temperature=0)- # chat_llm = models.get_openrouter(model_name="meta-llama/llama-3-8b-instruct:free")+ # chat_llm = models.get_openrouter_chat(model_name="google/gemini-flash-1.5-exp")# chat_llm = models.get_azure_openai_chat(deployment_name="gpt-4o-mini", temperature=0)# chat_llm = models.get_anthropic_chat(model_name="claude-3-5-sonnet-20240620", temperature=0)# chat_llm = models.get_google_chat(model_name="gemini-1.5-flash", temperature=0)- # chat_llm = models.get_groq_chat(model_name="llama-3.1-70b-versatile", temperature=0)+ # chat_llm = models.get_groq_chat(model_name="llama3-8b-8192", temperature=0)# utility model used for helper functions (cheaper, faster)utility_llm = chat_llm # change if you want to use a different utility modeldiff --git a/models.py b/models.pyindex aa402275a..55498278b 100644--- a/models.py+++ b/models.py@@ -2,6 +2,7 @@ import osfrom dotenv import load_dotenvfrom langchain_openai import ChatOpenAI, OpenAI, OpenAIEmbeddings, AzureChatOpenAI, AzureOpenAIEmbeddings, AzureOpenAIfrom langchain_community.llms.ollama import Ollama+from langchain_ollama import ChatOllamafrom langchain_community.embeddings import OllamaEmbeddingsfrom langchain_anthropic import ChatAnthropicfrom langchain_groq import ChatGroq@@ -22,11 +23,12 @@ def get_api_key(service):# Ollama models-def get_ollama_chat(model_name:str, temperature=DEFAULT_TEMPERATURE, base_url=os.getenv("OLLAMA_BASE_URL")):- return Ollama(model=model_name,temperature=temperature, base_url=base_url)+def get_ollama_chat(model_name:str, temperature=DEFAULT_TEMPERATURE, base_url=os.getenv("OLLAMA_BASE_URL") or "http://127.0.0.1:11434", num_ctx=8192):+ return ChatOllama(model=model_name,temperature=temperature, base_url=base_url, num_ctx=num_ctx)-def get_ollama_embedding(model_name:str, temperature=DEFAULT_TEMPERATURE):- return OllamaEmbeddings(model=model_name,temperature=temperature)+def get_ollama_embedding(model_name:str, temperature=DEFAULT_TEMPERATURE, base_url=os.getenv("OLLAMA_BASE_URL") or "http://127.0.0.1:11434"):++ return OllamaEmbeddings(model=model_name,temperature=temperature, base_url=base_url)# HuggingFace models@@ -34,63 +36,46 @@ def get_huggingface_embedding(model_name:str):return HuggingFaceEmbeddings(model_name=model_name)# LM Studio and other OpenAI compatible interfaces-def get_lmstudio_chat(model_name:str, base_url=os.getenv("LM_STUDIO_BASE_URL"), temperature=DEFAULT_TEMPERATURE):+def get_lmstudio_chat(model_name:str, temperature=DEFAULT_TEMPERATURE, base_url=os.getenv("LM_STUDIO_BASE_URL") or "http://127.0.0.1:1234/v1"):return ChatOpenAI(model_name=model_name, base_url=base_url, temperature=temperature, api_key="none") # type: ignore-def get_lmstudio_embedding(model_name:str, base_url=os.getenv("LM_STUDIO_BASE_URL")):+def get_lmstudio_embedding(model_name:str, base_url=os.getenv("LM_STUDIO_BASE_URL") or "http://127.0.0.1:1234/v1"):return OpenAIEmbeddings(model_name=model_name, base_url=base_url) # type: ignore# Anthropic models-def get_anthropic_chat(model_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("anthropic")+def get_anthropic_chat(model_name:str, api_key=get_api_key("anthropic"), temperature=DEFAULT_TEMPERATURE):return ChatAnthropic(model_name=model_name, temperature=temperature, api_key=api_key) # type: ignore# OpenAI models-def get_openai_chat(model_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("openai")+def get_openai_chat(model_name:str, api_key=get_api_key("openai"), temperature=DEFAULT_TEMPERATURE):return ChatOpenAI(model_name=model_name, temperature=temperature, api_key=api_key) # type: ignore-def get_openai_instruct(model_name:str,api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("openai")+def get_openai_instruct(model_name:str, api_key=get_api_key("openai"), temperature=DEFAULT_TEMPERATURE):return OpenAI(model=model_name, temperature=temperature, api_key=api_key) # type: ignore-def get_openai_embedding(model_name:str, api_key=None):- api_key = api_key or get_api_key("openai")+def get_openai_embedding(model_name:str, api_key=get_api_key("openai")):return OpenAIEmbeddings(model=model_name, api_key=api_key) # type: ignore-def get_azure_openai_chat(deployment_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE, azure_endpoint=None):- api_key = api_key or get_api_key("openai_azure")- azure_endpoint = azure_endpoint or os.getenv("OPENAI_AZURE_ENDPOINT")+def get_azure_openai_chat(deployment_name:str, api_key=get_api_key("openai_azure"), temperature=DEFAULT_TEMPERATURE, azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT")):return AzureChatOpenAI(deployment_name=deployment_name, temperature=temperature, api_key=api_key, azure_endpoint=azure_endpoint) # type: ignore-def get_azure_openai_instruct(deployment_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE, azure_endpoint=None):- api_key = api_key or get_api_key("openai_azure")- azure_endpoint = azure_endpoint or os.getenv("OPENAI_AZURE_ENDPOINT")+def get_azure_openai_instruct(deployment_name:str, api_key=get_api_key("openai_azure"), temperature=DEFAULT_TEMPERATURE, azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT")):return AzureOpenAI(deployment_name=deployment_name, temperature=temperature, api_key=api_key, azure_endpoint=azure_endpoint) # type: ignore-def get_azure_openai_embedding(deployment_name:str, api_key=None, azure_endpoint=None):- api_key = api_key or get_api_key("openai_azure")- azure_endpoint = azure_endpoint or os.getenv("OPENAI_AZURE_ENDPOINT")+def get_azure_openai_embedding(deployment_name:str, api_key=get_api_key("openai_azure"), azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT")):return AzureOpenAIEmbeddings(deployment_name=deployment_name, api_key=api_key, azure_endpoint=azure_endpoint) # type: ignore# Google models-def get_google_chat(model_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("google")+def get_google_chat(model_name:str, api_key=get_api_key("google"), temperature=DEFAULT_TEMPERATURE):return GoogleGenerativeAI(model=model_name, temperature=temperature, google_api_key=api_key, safety_settings={HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE }) # type: ignore# Groq models-def get_groq_chat(model_name:str, api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("groq")+def get_groq_chat(model_name:str, api_key=get_api_key("groq"), temperature=DEFAULT_TEMPERATURE):return ChatGroq(model_name=model_name, temperature=temperature, api_key=api_key) # type: ignore# OpenRouter models-def get_openrouter(model_name: str="meta-llama/llama-3.1-8b-instruct:free", api_key=None, temperature=DEFAULT_TEMPERATURE):- api_key = api_key or get_api_key("openrouter")- return ChatOpenAI(api_key=api_key, base_url=os.getenv("OPEN_ROUTER_BASE_URL"), model=model_name, temperature=temperature) # type: ignore+def get_openrouter_chat(model_name: str, api_key=get_api_key("openrouter"), temperature=DEFAULT_TEMPERATURE, base_url=os.getenv("OPEN_ROUTER_BASE_URL") or "https://openrouter.ai/api/v1"):+ return ChatOpenAI(api_key=api_key, model=model_name, temperature=temperature, base_url=base_url) # type: ignore-def get_embedding_hf(model_name="sentence-transformers/all-MiniLM-L6-v2"):- return HuggingFaceEmbeddings(model_name=model_name)--def get_embedding_openai(api_key=None):- api_key = api_key or get_api_key("openai")- return OpenAIEmbeddings(api_key=api_key) #type: ignore+def get_openrouter_embedding(model_name: str, api_key=get_api_key("openrouter"), base_url=os.getenv("OPEN_ROUTER_BASE_URL") or "https://openrouter.ai/api/v1"):+ return OpenAIEmbeddings(model=model_name, api_key=api_key, base_url=base_url) # type: ignore\ No newline at end of filediff --git a/prompts/default/agent.memory.md b/prompts/default/agent.memory.mddeleted file mode 100644index e79020abf..000000000--- a/prompts/default/agent.memory.md+++ /dev/null@@ -1,4 +0,0 @@-# Memories-- following are your memories on the current topic--{{memories}}\ No newline at end of filediff --git a/prompts/default/agent.system.solutions.md b/prompts/default/agent.system.solutions.mdnew file mode 100644index 000000000..8b8eb8997--- /dev/null+++ b/prompts/default/agent.system.solutions.md@@ -0,0 +1,4 @@+# Solutions in the past+- following are your memories about successful solutions of related problems:++{{solutions}}\ No newline at end of filediff --git a/prompts/default/agent.tools.md b/prompts/default/agent.system.tools.mdsimilarity index 98%rename from prompts/default/agent.tools.mdrename to prompts/default/agent.system.tools.mdindex 26a4e432e..4b2d7b76c 100644--- a/prompts/default/agent.tools.md+++ b/prompts/default/agent.system.tools.md@@ -150,7 +150,7 @@ This tool can be used to achieve any task that requires computation, or any othePlace your code escaped and properly indented in the "code" argument.Select the corresponding runtime with "runtime" argument. Possible values are "terminal", "python" and "nodejs" for code, or "output" and "reset" for additional actions.Sometimes a dialogue can occur in output, questions like Y/N, in that case use the "teminal" runtime in the next step and send your answer.-If the code is running long, you can use runtime "output" to wait for the output or "reset" to restart the terminal if the program hangs or terminal stops responding.+If the code is running long, you can use runtime "output" to wait for next output part or use runtime "reset" to kill the process.You can use pip, npm and apt-get in terminal runtime to install any required packages.IMPORTANT: Never use implicit print or implicit output, it does not work! If you need output of your code, you MUST use print() or console.log() to output selected variables.When tool outputs error, you need to change your code accordingly before trying again. knowledge_tool can help analyze errors.diff --git a/prompts/default/fw.memory.hist_suc.sys.md b/prompts/default/fw.memory.hist_suc.sys.mdnew file mode 100644index 000000000..0bbfd8e0b--- /dev/null+++ b/prompts/default/fw.memory.hist_suc.sys.md@@ -0,0 +1,23 @@+# Assistant's job+1. The assistant receives a history of conversation between USER and AGENT+2. Assistant searches for succesful technical solutions by the AGENT+3. Assistant writes notes about the succesful solution for later reproduction++# Format+- The response format is a JSON array of succesfull solutions containng "problem" and "solution" properties+- The problem section contains a description of the problem, the solution section contains step by step instructions to solve the problem including necessary details and code.+- If the history does not contain any helpful technical solutions, the response will be an empty JSON array.++# Example+```json+[+ {+ "problem": "Task is to download a video from YouTube. A video URL is specified by the user.",+ "solution": "1. Install yt-dlp library using 'pip install yt-dlp'\n2. Download the video using yt-dlp command: 'yt-dlp YT_URL', replace YT_URL with your video URL."+ }+]+```++# Rules+- Focus on important details like libraries used, code, encountered issues, error fixing etc.+- Do not include simple solutions that don't require instructions to reproduce like file handling, web search etc.\ No newline at end of filediff --git a/prompts/default/fw.memory.hist_sum.sys.md b/prompts/default/fw.memory.hist_sum.sys.mdnew file mode 100644index 000000000..8f4cd13f4--- /dev/null+++ b/prompts/default/fw.memory.hist_sum.sys.md@@ -0,0 +1,26 @@+# Assistant's job+1. The assistant receives a history of conversation between USER and AGENT+2. Assistant writes a summary that will serve as a search index later+3. Assistant responds with the summary plain text without any formatting or own thoughts or phrases++The goal is to provide shortest possible summary containing all key elements that can be searched later.+For this reason all long texts like code, results, contents will be removed.++# Format+- The response format is plain text containing only the summary of the conversation+- No formatting+- Do not write any introduction or conclusion, no additional text unrelated to the summary itself++# Rules+- Important details such as identifiers must be preserved in the summary as they can be used for search+- Unimportant details, phrases, fillers, redundant text, etc. should be removed++# Must be preserved:+- Keywords, names, IDs, URLs, etc.+- Technologies used, libraries used++# Must be removed:+- Full code+- File contents+- Search results+- Long outputs\ No newline at end of filediff --git a/prompts/default/memory.solutions_query.sys.md b/prompts/default/memory.solutions_query.sys.mdnew file mode 100644index 000000000..fe31890e3--- /dev/null+++ b/prompts/default/memory.solutions_query.sys.md@@ -0,0 +1,19 @@+# AI's job+1. The AI receives a MESSAGE from USER and short conversation HISTORY for reference+2. AI analyzes the intention of the USER based on MESSAGE and HISTORY+3. AI provide a search query for search engine where previous solutions are stored++# Format+- The response format is a plain text string containing the query+- No other text, no formatting++# Example+```json+USER: "I want to download a video from YouTube. A video URL is specified by the user."+AI: "download youtube video"+USER: "Now compress all files in that folder"+AI: "compress files in folder"+```++# HISTORY:+{{history}}\ No newline at end of filediff --git a/prompts/default/memory.solutions_sum.sys.md b/prompts/default/memory.solutions_sum.sys.mdnew file mode 100644index 000000000..1646501b0--- /dev/null+++ b/prompts/default/memory.solutions_sum.sys.md@@ -0,0 +1,24 @@+# Assistant's job+1. The assistant receives a history of conversation between USER and AGENT+2. Assistant searches for succesful technical solutions by the AGENT+3. Assistant writes notes about the succesful solution for later reproduction++# Format+- The response format is a JSON array of succesfull solutions containng "problem" and "solution" properties+- The problem section contains a description of the problem, the solution section contains step by step instructions to solve the problem including necessary details and code.+- If the history does not contain any helpful technical solutions, the response will be an empty JSON array.++# Example+~~~json+[+ {+ "problem": "Task is to download a video from YouTube. A video URL is specified by the user.",+ "solution": "1. Install yt-dlp library using 'pip install yt-dlp'\n2. Download the video using yt-dlp command: 'yt-dlp YT_URL', replace YT_URL with your video URL."+ }+]+~~~++# Rules+- Focus on important details like libraries used, code, encountered issues, error fixing etc.+- Do not include simple solutions that don't require instructions to reproduce like file handling, web search etc.+- Do not add your own details that are not specifically mentioned in the history\ No newline at end of filediff --git a/python/extensions/message_loop_prompts/recall_solutions.py b/python/extensions/message_loop_prompts/recall_solutions.pynew file mode 100644index 000000000..ab3fb1c9d--- /dev/null+++ b/python/extensions/message_loop_prompts/recall_solutions.py@@ -0,0 +1,73 @@+from agent import Agent+from python.helpers.extension import Extension+from python.helpers.files import read_file+from python.helpers.vector_db import get_or_create+import json+++class RecallSolutions(Extension):++ INTERVAL = 3+ HISTORY = 5+ RESULTS = 3+ THRESHOLD = 0.1++ async def execute(self, loop_data={}, **kwargs):++ iter = loop_data.get("iteration", 0)+ if iter % RecallSolutions.INTERVAL == 0: # every 3 iterations (or the first one) recall solution memories+ await self.search_solutions(loop_data=loop_data, **kwargs)+++ async def search_solutions(self, loop_data={}, **kwargs):+ self.agent.context.log.log(+ type="info", content="Searching memory for solutions...", temp=False+ )++ # get system message and chat history for util llm+ msgs_text = self.agent.concat_messages(+ self.agent.history[-RecallSolutions.HISTORY:]+ ) # only last X messages+ system = self.agent.read_prompt(+ "memory.solutions_query.sys.md", history=msgs_text+ )++ # call util llm to summarize conversation+ query = await self.agent.call_utility_llm(+ system=system, msg=loop_data["message"]+ )++ # get solutions database+ vdb = get_or_create(+ logger=self.agent.context.log,+ embeddings_model=self.agent.config.embeddings_model,+ memory_dir="./memory/solutions",+ knowledge_dir="",+ )++ solutions = vdb.search_similarity_threshold(+ query=query, results=RecallSolutions.RESULTS, threshold=RecallSolutions.THRESHOLD+ )++ if not isinstance(solutions, list) or len(solutions) == 0:+ self.agent.context.log.log(+ type="info", content="No successful solution memories found.", temp=False+ )+ return+ else:+ self.agent.context.log.log(+ type="info",+ content=f"{len(solutions)} successful solution memories found.",+ temp=False,+ )++ # concatenate solution.page_content in solutions:+ solutions_text = ""+ for solution in solutions:+ solutions_text += solution.page_content + "\n\n"++ # place to prompt+ solutions_prompt = self.agent.read_prompt("agent.system.solutions.md", solutions=solutions_text)++ # append to system message+ loop_data["system"] += solutions_promptdiff --git a/python/extensions/monologue_end/memorize_solutions.py b/python/extensions/monologue_end/memorize_solutions.pynew file mode 100644index 000000000..666ffff66--- /dev/null+++ b/python/extensions/monologue_end/memorize_solutions.py@@ -0,0 +1,70 @@+from agent import Agent+from python.helpers.extension import Extension+from python.helpers.files import read_file+from python.helpers.vector_db import get_or_create+import json+from python.helpers.dirty_json import DirtyJson+from python.helpers import errors+++class MemorizeSolutions(Extension):++ async def execute(self, loop_data={}, **kwargs):+ try:+ self.agent.context.log.log(+ type="info", content="Memorizing succesful solutions...", temp=True+ )++ # get system message and chat history for util llm+ system = self.agent.read_prompt("memory.solutions_sum.sys.md")+ msgs_text = self.agent.concat_messages(self.agent.history)++ # call util llm to find solutions in history+ solutions_json = await self.agent.call_utility_llm(+ system=system,+ msg=msgs_text,+ log_type="util",+ )++ solutions = DirtyJson.parse_string(solutions_json)++ if not isinstance(solutions, list) or len(solutions) == 0:+ self.agent.context.log.log(+ type="info", content="No succesful solutions found.", temp=False+ )+ return+ else:+ self.agent.context.log.log(+ type="info",+ content=f"{len(solutions)} succesful solutions found.",+ temp=True,+ )++ # save chat history+ vdb = get_or_create(+ logger=self.agent.context.log,+ embeddings_model=self.agent.config.embeddings_model,+ memory_dir="./memory/solutions",+ knowledge_dir="",+ )++ for solution in solutions:+ # solution to plain text:+ txt = (+ f"Problem: {solution['problem']}\nSolution: {solution['solution']}"+ )+ vdb.insert_text(+ text=txt,+ ) # metadata={"full": msgs_text})++ self.agent.context.log.log(+ type="info",+ content=f"{len(solutions)} solutions memorized.",+ temp=False,+ )++ except Exception as e:+ err = errors.format_error(e)+ self.agent.context.log.log(+ type="error", heading="Memorize solutions extension error:", content=err+ )diff --git a/python/extensions/monologue_end/zero_memorize_history.py_ b/python/extensions/monologue_end/zero_memorize_history.py_new file mode 100644index 000000000..85dc9d46b--- /dev/null+++ b/python/extensions/monologue_end/zero_memorize_history.py_@@ -0,0 +1,45 @@+from agent import Agent+from python.helpers.extension import Extension+from python.helpers.files import read_file+from python.helpers.vector_db import get_or_create+import json+++class MemorizeHistory(Extension):++ async def execute(self, **kwargs):+ if self.agent.number != 0:+ return # only agent 0 will memorize chat history with user++ self.agent.context.log.log(+ type="info", content="Memorizing chat history...", temp=True+ )++ # get system message and chat history for util llm+ system = self.agent.read_prompt("fw.memory.hist_sum.sys.md")+ # msgs = []+ # for msg in self.agent.history:+ # content = msg.get("content", "")+ # if content:+ # msgs.append(content)+ # msgs_text = json.dumps(msgs)+ msgs_text = self.agent.concat_messages(self.agent.history)++ # call util llm to summarize conversation+ summary = await self.agent.call_utility_llm(+ system=system, msg=msgs_text, output_label=""+ )++ # save chat history+ vdb = get_or_create(+ logger=self.agent.context.log,+ embeddings_model=self.agent.config.embeddings_model,+ memory_dir="./memory/history",+ knowledge_dir="",+ )++ vdb.insert_text(text=summary, metadata={"full": msgs_text})++ self.agent.context.log.log(+ type="info", content="Chat history memorized.", temp=True+ )diff --git a/python/extensions/msg_loop_break/zero_memorize_history.py b/python/extensions/msg_loop_break/zero_memorize_history.pynew file mode 100644index 000000000..a2beec64d--- /dev/null+++ b/python/extensions/msg_loop_break/zero_memorize_history.py@@ -0,0 +1,35 @@+from agent import Agent+from python.helpers.extension import Extension+from python.helpers.files import read_file+from python.helpers.vector_db import VectorDB+import json++class MemorizeHistory(Extension):++ async def execute(self, **kwargs):+ if self.agent.number != 0: return #only agent 0 will memorize chat history with user++ self.agent.context.log.log(type="info", content="Memorizing chat history...", temp=True)++ #get system message and chat history for util llm+ system = self.agent.read_prompt("fw.memory.hist_sum.sys")+ msgs = []+ for msg in self.agent.history:+ content = msg.get("content", "")+ if content:+ msgs.append(content)+ msgs_json = json.dumps(msgs)++ #call util llm to summarize conversation+ summary = await self.agent.call_utility_llm(system=system,msg=msgs_json,output_label="")++ #save chat history+ vdb = VectorDB(+ logger=self.agent.context.log,+ embeddings_model=self.agent.config.embeddings_model,+ memory_dir="./memory/history",+ knowledge_dir=""+ )++ self.agent.context.log.log(type="info", content="Chat history memorized.", temp=True)+\ No newline at end of filediff --git a/python/helpers/dirty_json.py b/python/helpers/dirty_json.pyindex 116899f11..622ea8b12 100644--- a/python/helpers/dirty_json.py+++ b/python/helpers/dirty_json.py@@ -17,7 +17,7 @@ class DirtyJson:def parse(self, json_string):self._reset()self.json_string = json_string- self.index = self.index_of_first_brace(self.json_string) #skip any text up to the first brace+ self.index = self.get_start_pos(self.json_string) #skip any text up to the first braceself.current_char = self.json_string[self.index]self._parse()return self.result@@ -260,5 +260,8 @@ class DirtyJson:breakreturn result- def index_of_first_brace(self, input_str: str) -> int:- return input_str.find("{")+ def get_start_pos(self, input_str: str) -> int:+ chars = ["{", "[", '"']+ indices = [input_str.find(char) for char in chars if input_str.find(char) != -1]+ return min(indices) if indices else 0+diff --git a/python/helpers/docker.py b/python/helpers/docker.pyindex f157dd432..e53d80d01 100644--- a/python/helpers/docker.py+++ b/python/helpers/docker.py@@ -56,7 +56,7 @@ class DockerContainerManager:if existing_container:if existing_container.status != 'running':print(f"Starting existing container: {self.name} for safe code execution...")- self.logger.log(type="info", content=f"Starting existing container: {self.name} for safe code execution...")+ self.logger.log(type="info", content=f"Starting existing container: {self.name} for safe code execution...", temp=True)existing_container.start()self.container = existing_container@@ -67,7 +67,7 @@ class DockerContainerManager:# print(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")else:print(f"Initializing docker container {self.name} for safe code execution...")- self.logger.log(type="info", content=f"Initializing docker container {self.name} for safe code execution...")+ self.logger.log(type="info", content=f"Initializing docker container {self.name} for safe code execution...", temp=True)self.container = self.client.containers.run(self.image,diff --git a/python/helpers/extension.py b/python/helpers/extension.pynew file mode 100644index 000000000..a96de6996--- /dev/null+++ b/python/helpers/extension.py@@ -0,0 +1,13 @@+from abc import abstractmethod+from typing import Any+from agent import Agent++class Extension:++ def __init__(self, agent: Agent, *args, **kwargs):+ self.agent = agent+ self.kwargs = kwargs++ @abstractmethod+ async def execute(self, **kwargs) -> Any:+ pass\ No newline at end of filediff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.pyindex fd1559a3d..f60655072 100644--- a/python/helpers/extract_tools.py+++ b/python/helpers/extract_tools.py@@ -1,15 +1,12 @@-import re, os-from typing import Any-from . import files-# import dirtyjson+import re, os, importlib, inspect+from typing import Any, Type, TypeVarfrom .dirty_json import DirtyJsonimport regex-+from fnmatch import fnmatchdef json_parse_dirty(json:str) -> dict[str,Any] | None:ext_json = extract_json_object_string(json)if ext_json:- # ext_json = fix_json_string(ext_json)data = DirtyJson.parse_string(ext_json)if isinstance(data,dict): return datareturn None@@ -50,150 +47,26 @@ def fix_json_string(json_string):fixed_string = re.sub(r'(?<=: ")(.*?)(?=")', replace_unescaped_newlines, json_string, flags=re.DOTALL)return fixed_string-# def extract_tool_requests2(response):-# # Regex to match the tags ending with $, allowing for varying whitespace-# pattern = r'<(\w+)\$[\s]*(.*?)>([\s\S]*?)(?=<\w+\$|<\/\1\$|$)'-# matches = re.findall(pattern, response, re.DOTALL)--# tool_usages = []-# allowed_tags = list_python_files("tools")--# for match in matches:-# tag_name, attributes, content = match-# if tag_name not in allowed_tags: continue--# tool_dict = {}-# tool_dict['name'] = tag_name-# tool_dict['args'] = {}--# # Parse attributes-# for attr in re.findall(r'(\w+)\s*=\s*"([^"]+)"', attributes):-# tool_dict['args'][attr[0]] = attr[1]--# # Add body content-# tool_dict["content"] = content.strip()-# tool_dict["index"] = len(tool_usages)-# tool_usages.append(tool_dict)--# return tool_usages+T = TypeVar('T') # Define a generic type variable-# def extract_tool_requests(response):-# # Regex to match the tool blocks, allowing for varying whitespace-# pattern = r'<tool\$[\s]*(.*?)>(.*?)<\/tool\$\s*>'-# matches = re.findall(pattern, response, re.DOTALL)--# tool_usages = []--# for match in matches:-# attributes, body = match-# tool_dict = {}-# # Parse attributes-# for attr in re.findall(r'(\w+)\s*=\s*"([^"]+)"', attributes):-# tool_dict[attr[0]] = attr[1]-# # Add body content-# tool_dict["body"] = body.strip()-# tool_usages.append(tool_dict)--# return tool_usages+def load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T]) -> list[Type[T]]:-# def extract_specified_tags(response):+ classes = []-# allowed_tags = list_python_files("tools")--# # Create a regex pattern to match specified tags and their attributes-# pattern = r'<({})([\s\S]*?)>'.format('|'.join(allowed_tags))-# matches = re.findall(pattern, response, re.DOTALL)--# extracted_tags = []--# for match in matches:-# tag_name, attributes = match-# tag_dict = {}-# tag_dict['name'] = tag_name--# # Parse attributes-# for attr in re.findall(r'(\w+)\s*=\s*"([^"]+)"', attributes):-# tag_dict[attr[0]] = attr[1]--# # Extract the body text (everything after the tag until the next tag or end of string)-# body_pattern = r'<{0}[\s\S]*?>([\s\S]*?)(?=<|$)'.format(tag_name)-# body_match = re.search(body_pattern, response, re.DOTALL)-# tag_dict['body'] = body_match.group(1).strip() if body_match else ''--# extracted_tags.append(tag_dict)--# return extracted_tags--# def list_python_files(directory):-# # List all files in the given directory-# list = os.listdir(files.get_abs_path(directory))-# # Filter for Python files and remove the extension-# python_files = { os.path.splitext(file)[0] for file in list if file.endswith('.py') }-# return python_files--# import re-# from xml.etree import ElementTree as ET--# def extract_tool_usages_advanced(response):-# tool_usages = []-# pattern = re.compile(r'<tool.*?>', re.DOTALL)--# start_pos = 0-# while start_pos < len(response):-# match = pattern.search(response, start_pos)-# if not match:-# break--# tag_start = match.start()-# tag_end = match.end()-# end_tag = '</tool>'--# # To find the corresponding end tag correctly handling nested tags-# depth = 1-# search_pos = tag_end--# while depth > 0:-# next_open = response.find('<tool', search_pos)-# next_close = response.find(end_tag, search_pos)--# if next_close == -1:-# break--# if next_open != -1 and next_open < next_close:-# depth += 1-# search_pos = next_open + len('<tool')-# else:-# depth -= 1-# search_pos = next_close + len(end_tag)--# end_tag_end = search_pos--# # Extract the whole tool block-# tool_block = response[tag_start:end_tag_end]--# try:-# element = ET.fromstring(tool_block)-# tool_dict = element.attrib-# tool_dict["body"] = ET.tostring(element, encoding='unicode', method='xml').split('>', 1)[1].rsplit('<', 1)[0].strip()-# tool_usages.append(tool_dict)-# except ET.ParseError:-# # In case of parsing error, fall back to including entire content between the tags-# body_content = response[tag_end:end_tag_end - len(end_tag)].strip()-# tool_dict = {"name": re.search(r'name="(.*?)"', match.group(0)).group(1), "body": body_content}-# tool_usages.append(tool_dict)--# start_pos = end_tag_end+ # Get all .py files in the folder that match the pattern+ for file_name in os.listdir(folder):+ if fnmatch(file_name, name_pattern) and file_name.endswith(".py"):+ module_name = file_name[:-3] # remove .py extension+ module_path = folder.replace(os.sep, ".") + "." + module_name+ module = importlib.import_module(module_path)-# return tool_usages+ # Get all classes in thde module+ class_list = inspect.getmembers(module, inspect.isclass)-# # Example usage with the given input-# response = """-# <tool name="code_execution_tool">-# #comment <tool<tool name="abc><tool><loot><tool>"-# print(text)-# </tool>-# """+ # Filter for classes that are subclasses of the given base_class+ for cls in class_list:+ if cls[1] is not base_class and issubclass(cls[1], base_class):+ classes.append(cls[1])-# tool_usages = extract_tool_usages(response)-# print(tool_usages)\ No newline at end of file+ return classes\ No newline at end of filediff --git a/python/helpers/files.py b/python/helpers/files.pyindex 278e064d3..c3e228cc3 100644--- a/python/helpers/files.py+++ b/python/helpers/files.py@@ -1,4 +1,4 @@-import os, re, sys+import os, redef read_file(relative_path, **kwargs):absolute_path = get_abs_path(relative_path) # Construct the absolute path to the target file@@ -30,4 +30,5 @@ def exists(*relative_paths):def get_base_dir():# Get the base directory from the current file pathbase_dir = os.path.dirname(os.path.abspath(os.path.join(__file__,"../../")))- return base_dir\ No newline at end of file+ return base_dir+diff --git a/python/helpers/log.py b/python/helpers/log.pyindex 96fdfcbde..4d1912849 100644--- a/python/helpers/log.py+++ b/python/helpers/log.py@@ -1,9 +1,21 @@from dataclasses import dataclass, fieldimport json-from typing import Optional, Dict+from typing import Literal, Optional, Dictimport uuid+type Type = Literal[+ 'agent',+ 'code_exe',+ 'error',+ 'hint',+ 'info',+ 'tool',+ 'user',+ 'util',+ 'warning',+ ]+@dataclassclass LogItem:log: 'Log'@@ -11,15 +23,16 @@ class LogItem:type: strheading: strcontent: str+ temp: boolkvps: Optional[Dict] = Noneguid: str = ""def __post_init__(self):self.guid = self.log.guid- def update(self, type: str | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None):+ def update(self, type: Type | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None, temp: bool | None = None):if self.guid == self.log.guid:- self.log.update_item(self.no, type=type, heading=heading, content=content, kvps=kvps)+ self.log.update_item(self.no, type=type, heading=heading, content=content, kvps=kvps, temp=temp)def output(self):return {@@ -27,6 +40,7 @@ class LogItem:"type": self.type,"heading": self.heading,"content": self.content,+ "temp": self.temp,"kvps": self.kvps}@@ -37,13 +51,13 @@ class Log:self.updates: list[int] = []self.logs: list[LogItem] = []- def log(self, type: str, heading: str | None = None, content: str | None = None, kvps: dict | None = None) -> LogItem:- item = LogItem(log=self,no=len(self.logs), type=type, heading=heading or "", content=content or "", kvps=kvps)+ def log(self, type: Type, heading: str | None = None, content: str | None = None, kvps: dict | None = None, temp: bool | None = None) -> LogItem:+ item = LogItem(log=self,no=len(self.logs), type=type, heading=heading or "", content=content or "", kvps=kvps, temp=temp or False)self.logs.append(item)self.updates += [item.no]return item- def update_item(self, no: int, type: str | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None):+ def update_item(self, no: int, type: str | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None, temp: bool | None = None):item = self.logs[no]if type is not None:item.type = type@@ -53,6 +67,8 @@ class Log:item.content = contentif kvps is not None:item.kvps = kvps+ if temp is not None:+ item.temp = tempself.updates += [item.no]def output(self, start=None, end=None):diff --git a/python/helpers/shell_local.py b/python/helpers/shell_local.pyindex 50d1ba57e..f7343caa7 100644--- a/python/helpers/shell_local.py+++ b/python/helpers/shell_local.py@@ -44,12 +44,14 @@ class LocalInteractiveSession:self.process.stdin.write(command + '\n') # type: ignoreself.process.stdin.flush() # type: ignore- async def read_output(self) -> Tuple[str, Optional[str]]:+ async def read_output(self, timeout: float = 0) -> Tuple[str, Optional[str]]:if not self.process:raise Exception("Shell not connected")partial_output = ''- while True:+ start_time = time.time()++ while (timeout <= 0 or time.time() - start_time < timeout):rlist, _, _ = select.select([self.process.stdout], [], [], 0.1)if rlist:line = self.process.stdout.readline() # type: ignorediff --git a/python/helpers/shell_ssh.py b/python/helpers/shell_ssh.pyindex 3e598c7d6..ea46fb0bc 100644--- a/python/helpers/shell_ssh.py+++ b/python/helpers/shell_ssh.py@@ -42,7 +42,7 @@ class SSHInteractiveSession:errors += 1if errors < 3:print(f"SSH Connection attempt {errors}...")- self.logger.log(type="info", content=f"SSH Connection attempt {errors}...")+ self.logger.log(type="info", content=f"SSH Connection attempt {errors}...", temp=True)time.sleep(5)else:@@ -66,14 +66,16 @@ class SSHInteractiveSession:self.trimmed_command_length = 0self.shell.send(self.last_command)- async def read_output(self) -> Tuple[str, str]:+ async def read_output(self, timeout: float = 0) -> Tuple[str, str]:if not self.shell:raise Exception("Shell not connected")partial_output = b''leftover = b''+ start_time = time.time()++ while self.shell.recv_ready() and (timeout <= 0 or time.time() - start_time < timeout):- while self.shell.recv_ready():data = self.shell.recv(1024)# Trim own command from output@@ -106,11 +108,6 @@ class SSHInteractiveSession:decoded_partial_output = self.clean_string(decoded_partial_output)decoded_full_output = self.clean_string(decoded_full_output)- # # Split output at end_comment- # if SSHInteractiveSession.end_comment in decoded_full_output:- # decoded_full_output = decoded_full_output.split(SSHInteractiveSession.end_comment)[-1].lstrip("\r\n")- # decoded_partial_output = decoded_partial_output.split(SSHInteractiveSession.end_comment)[-1].lstrip("\r\n")-return decoded_full_output, decoded_partial_outputdiff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.pyindex 01e444102..33f6ad481 100644--- a/python/helpers/vector_db.py+++ b/python/helpers/vector_db.py@@ -1,5 +1,6 @@from langchain.storage import InMemoryByteStore, LocalFileStorefrom langchain.embeddings import CacheBackedEmbeddings+# from langchain_chroma import Chromafrom langchain_community.vectorstores import FAISSimport faiss@@ -12,43 +13,74 @@ import uuidfrom python.helpers import knowledge_importfrom python.helpers.log import Log+index: dict[str, "VectorDB"] = {}+++def get_or_create(+ logger: Log,+ embeddings_model,+ in_memory=False,+ memory_dir="./memory",+ knowledge_dir=None,+):+ if index.get(memory_dir) is None:+ index[memory_dir] = VectorDB(+ logger, embeddings_model, in_memory, memory_dir, knowledge_dir+ )+ return index[memory_dir]++class VectorDB:- def __init__(self, logger: Log, embeddings_model, in_memory=False, memory_dir="./memory", knowledge_dir="./knowledge"):+ def __init__(+ self,+ logger: Log,+ embeddings_model,+ in_memory=False,+ memory_dir="./memory/default",+ knowledge_dir=None,+ ):self.logger = loggerprint("Initializing VectorDB...")- self.logger.log("info", content="Initializing VectorDB...")-+ self.logger.log("info", content="Initializing VectorDB...", temp=True)+self.embeddings_model = embeddings_model- self.em_dir = files.get_abs_path(memory_dir,"embeddings")- self.db_dir = files.get_abs_path(memory_dir,"database")+ self.em_dir = files.get_abs_path("./memory/embeddings") # just caching, no need to parameterize+ self.db_dir = files.get_abs_path("./memory", memory_dir, "database")self.kn_dir = files.get_abs_path(knowledge_dir) if knowledge_dir else ""-++ # make sure embeddings and database directories exist+ os.makedirs(self.db_dir, exist_ok=True)+if in_memory:self.store = InMemoryByteStore()else:+ os.makedirs(self.em_dir, exist_ok=True)self.store = LocalFileStore(self.em_dir)-- #here we setup the embeddings model with the chosen cache storage+ # here we setup the embeddings model with the chosen cache storageself.embedder = CacheBackedEmbeddings.from_bytes_store(- embeddings_model,- self.store,- namespace=getattr(embeddings_model, 'model', getattr(embeddings_model, 'model_name', "default")) )+ embeddings_model,+ self.store,+ namespace=getattr(+ embeddings_model,+ "model",+ getattr(embeddings_model, "model_name", "default"),+ ),+ )# self.db = Chroma(# embedding_function=self.embedder,# persist_directory=db_dir)-# if db folder exists and is not empty:- if os.path.exists(self.db_dir) and files.exists(self.db_dir,"index.faiss"):+ if os.path.exists(self.db_dir) and files.exists(self.db_dir, "index.faiss"):self.db = FAISS.load_local(folder_path=self.db_dir,embeddings=self.embedder,- allow_dangerous_deserialization=True+ allow_dangerous_deserialization=True,)else:index = faiss.IndexFlatL2(len(self.embedder.embed_query("example text")))@@ -57,65 +89,79 @@ class VectorDB:embedding_function=self.embedder,index=index,docstore=InMemoryDocstore(),- index_to_docstore_id={})+ index_to_docstore_id={},+ )- #preload knowledge files+ # preload knowledge filesif self.kn_dir:self.preload_knowledge(self.kn_dir, self.db_dir)-- def preload_knowledge(self, kn_dir:str, db_dir:str):+ def preload_knowledge(self, kn_dir: str, db_dir: str):# Load the index file if it existsindex_path = files.get_abs_path(db_dir, "knowledge_import.json")- #make sure directory exists+ # make sure directory existsif not os.path.exists(db_dir):os.makedirs(db_dir)-+index: dict[str, knowledge_import.KnowledgeImport] = {}if os.path.exists(index_path):- with open(index_path, 'r') as f:+ with open(index_path, "r") as f:index = json.load(f)-- index = knowledge_import.load_knowledge(self.logger,kn_dir,index)-++ index = knowledge_import.load_knowledge(self.logger, kn_dir, index)+for file in index:- if index[file]['state'] in ['changed', 'removed'] and index[file].get('ids',[]): # for knowledge files that have been changed or removed and have IDs- self.delete_documents_by_ids(index[file]['ids']) # remove original version- if index[file]['state'] == 'changed':- index[file]['ids'] = self.insert_documents(index[file]['documents']) # insert new version+ if index[file]["state"] in ["changed", "removed"] and index[file].get(+ "ids", []+ ): # for knowledge files that have been changed or removed and have IDs+ self.delete_documents_by_ids(+ index[file]["ids"]+ ) # remove original version+ if index[file]["state"] == "changed":+ index[file]["ids"] = self.insert_documents(+ index[file]["documents"]+ ) # insert new version# remove index where state="removed"- index = {k: v for k, v in index.items() if v['state'] != 'removed'}-+ index = {k: v for k, v in index.items() if v["state"] != "removed"}+# strip state and documents from index and save itfor file in index:- if "documents" in index[file]: del index[file]['documents'] # type: ignore- if "state" in index[file]: del index[file]['state'] # type: ignore- with open(index_path, 'w') as f:- json.dump(index, f)-+ if "documents" in index[file]:+ del index[file]["documents"] # type: ignore+ if "state" in index[file]:+ del index[file]["state"] # type: ignore+ with open(index_path, "w") as f:+ json.dump(index, f)+def search_similarity(self, query, results=3):- return self.db.similarity_search(query,results)-+ return self.db.similarity_search(query, results)+def search_similarity_threshold(self, query, results=3, threshold=0.5):- return self.db.search(query, search_type="similarity_score_threshold", k=results, score_threshold=threshold)+ return self.db.search(+ query,+ search_type="similarity_score_threshold",+ k=results,+ score_threshold=threshold,+ )def search_max_rel(self, query, results=3):- return self.db.max_marginal_relevance_search(query,results)+ return self.db.max_marginal_relevance_search(query, results)- def delete_documents_by_query(self, query:str, threshold=0.1):+ def delete_documents_by_query(self, query: str, threshold=0.1):k = 100tot = 0while True:# Perform similarity search with score- docs = self.search_similarity_threshold(query, results=k, threshold=threshold)+ docs = self.search_similarity_threshold(+ query, results=k, threshold=threshold+ )# Extract document IDs and filter based on score# document_ids = [result[0].metadata["id"] for result in docs if result[1] < score_limit]document_ids = [result.metadata["id"] for result in docs]-# Delete documents with IDs over the threshold scoreif document_ids:@@ -124,33 +170,36 @@ class VectorDB:# tot += len(fnd["ids"])self.db.delete(ids=document_ids)tot += len(document_ids)-+# If fewer than K document IDs, break the loopif len(document_ids) < k:break- if tot: self.db.save_local(folder_path=self.db_dir) # persist+ if tot:+ self.db.save_local(folder_path=self.db_dir) # persistreturn tot- def delete_documents_by_ids(self, ids:list[str]):+ def delete_documents_by_ids(self, ids: list[str]):# pre = self.db.get(ids=ids)["ids"]self.db.delete(ids=ids)# post = self.db.get(ids=ids)["ids"]- #TODO? compare pre and post- if ids: self.db.save_local(folder_path=self.db_dir) #persist+ # TODO? compare pre and post+ if ids:+ self.db.save_local(folder_path=self.db_dir) # persistreturn len(ids)-- def insert_text(self, text):++ def insert_text(self, text, metadata: dict = {}):id = str(uuid.uuid4())- self.db.add_documents(documents=[ Document(text, metadata={"id": id}) ], ids=[id])- self.db.save_local(folder_path=self.db_dir) #persist+ self.db.add_documents(+ documents=[Document(text, metadata={"id": id, **metadata})], ids=[id]+ )+ self.db.save_local(folder_path=self.db_dir) # persistreturn id-- def insert_documents(self, docs:list[Document]):++ def insert_documents(self, docs: list[Document]):ids = [str(uuid.uuid4()) for _ in range(len(docs))]- for doc, id in zip(docs, ids): doc.metadata["id"] = id #add ids to documents metadata+ for doc, id in zip(docs, ids):+ doc.metadata["id"] = id # add ids to documents metadataself.db.add_documents(documents=docs, ids=ids)- self.db.save_local(folder_path=self.db_dir) #persist+ self.db.save_local(folder_path=self.db_dir) # persistreturn ids--diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex b5b12e9d6..45a6e0607 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -9,23 +9,25 @@ from python.helpers.shell_local import LocalInteractiveSessionfrom python.helpers.shell_ssh import SSHInteractiveSessionfrom python.helpers.docker import DockerContainerManager+@dataclassclass State:shell: LocalInteractiveSession | SSHInteractiveSessiondocker: DockerContainerManager | None-+class CodeExecution(Tool):- async def execute(self,**kwargs):+ async def execute(self, **kwargs):++ await self.agent.handle_intervention() # wait for intervention and handle it, if paused- await self.agent.handle_intervention() # wait for intervention and handle it, if paused-await self.prepare_state()# os.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir-- runtime = self.args["runtime"].lower().strip()++ runtime = self.args.get("runtime", "").lower().strip()+if runtime == "python":response = await self.execute_python_code(self.args["code"])elif runtime == "nodejs":@@ -33,90 +35,138 @@ class CodeExecution(Tool):elif runtime == "terminal":response = await self.execute_terminal_command(self.args["code"])elif runtime == "output":- response = await self.get_terminal_output(wait_with_output=5, wait_without_output=20)+ response = await self.get_terminal_output(+ wait_with_output=5, wait_without_output=20+ )elif runtime == "reset":response = await self.reset_terminal()else:- response = self.agent.read_prompt("fw.code_runtime_wrong.md", runtime=runtime)+ response = self.agent.read_prompt(+ "fw.code_runtime_wrong.md", runtime=runtime+ )- if not response: response = self.agent.read_prompt("fw.code_no_output.md")+ if not response:+ response = self.agent.read_prompt("fw.code_no_output.md")return Response(message=response, break_loop=False)async def before_execution(self, **kwargs):- await self.agent.handle_intervention() # wait for intervention and handle it, if paused- PrintStyle(font_color="#1B4F72", padding=True, background_color="white", bold=True).print(f"{self.agent.agent_name}: Using tool '{self.name}':")- self.log = self.agent.context.log.log(type="code_exe", heading=f"{self.agent.agent_name}: Using tool '{self.name}':", content="", kvps=self.args)+ await self.agent.handle_intervention() # wait for intervention and handle it, if paused+ PrintStyle(+ font_color="#1B4F72", padding=True, background_color="white", bold=True+ ).print(f"{self.agent.agent_name}: Using tool '{self.name}':")+ self.log = self.agent.context.log.log(+ type="code_exe",+ heading=f"{self.agent.agent_name}: Using tool '{self.name}':",+ content="",+ kvps=self.args,+ )if self.args and isinstance(self.args, dict):for key, value in self.args.items():- PrintStyle(font_color="#85C1E9", bold=True).stream(self.nice_key(key)+": ")- PrintStyle(font_color="#85C1E9", padding=isinstance(value,str) and "\n" in value).stream(value)+ PrintStyle(font_color="#85C1E9", bold=True).stream(+ self.nice_key(key) + ": "+ )+ PrintStyle(+ font_color="#85C1E9",+ padding=isinstance(value, str) and "\n" in value,+ ).stream(value)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)+ 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)async def prepare_state(self, reset=False):self.state = self.agent.get_data("cot_state")if not self.state or reset:- #initialize docker container if execution in docker is configured+ # initialize docker container if execution in docker is configuredif self.agent.config.code_exec_docker_enabled:- docker = DockerContainerManager(logger=self.agent.context.log,name=self.agent.config.code_exec_docker_name, image=self.agent.config.code_exec_docker_image, ports=self.agent.config.code_exec_docker_ports, volumes=self.agent.config.code_exec_docker_volumes)+ docker = DockerContainerManager(+ logger=self.agent.context.log,+ name=self.agent.config.code_exec_docker_name,+ image=self.agent.config.code_exec_docker_image,+ ports=self.agent.config.code_exec_docker_ports,+ volumes=self.agent.config.code_exec_docker_volumes,+ )docker.start_container()- else: docker = None+ else:+ docker = None- #initialize local or remote interactive shell insterface+ # initialize local or remote interactive shell insterfaceif self.agent.config.code_exec_ssh_enabled:- shell = SSHInteractiveSession(self.agent.context.log,self.agent.config.code_exec_ssh_addr,self.agent.config.code_exec_ssh_port,self.agent.config.code_exec_ssh_user,self.agent.config.code_exec_ssh_pass)- else: shell = LocalInteractiveSession()-- self.state = State(shell=shell,docker=docker)+ shell = SSHInteractiveSession(+ self.agent.context.log,+ self.agent.config.code_exec_ssh_addr,+ self.agent.config.code_exec_ssh_port,+ self.agent.config.code_exec_ssh_user,+ self.agent.config.code_exec_ssh_pass,+ )+ else:+ shell = LocalInteractiveSession()++ self.state = State(shell=shell, docker=docker)await shell.connect()self.agent.set_data("cot_state", self.state)-- async def execute_python_code(self, code):++ async def execute_python_code(self, code: str, reset: bool = False):escaped_code = shlex.quote(code)- command = f'python3 -c {escaped_code}'- return await self.terminal_session(command)+ command = f"python3 -c {escaped_code}"+ return await self.terminal_session(command, reset)- async def execute_nodejs_code(self, code):+ async def execute_nodejs_code(self, code: str, reset: bool = False):escaped_code = shlex.quote(code)- command = f'node -e {escaped_code}'- return await self.terminal_session(command)+ command = f"node -e {escaped_code}"+ return await self.terminal_session(command, reset)- async def execute_terminal_command(self, command):- return await self.terminal_session(command)+ async def execute_terminal_command(self, command: str, reset: bool = False):+ return await self.terminal_session(command, reset)- async def terminal_session(self, command):+ async def terminal_session(self, command: str, reset: bool = False):++ await self.agent.handle_intervention() # wait for intervention and handle it, if paused+ if reset:+ await self.reset_terminal()- await self.agent.handle_intervention() # wait for intervention and handle it, if paused-self.state.shell.send_command(command)- PrintStyle(background_color="white",font_color="#1B4F72",bold=True).print(f"{self.agent.agent_name} code execution output:")+ PrintStyle(background_color="white", font_color="#1B4F72", bold=True).print(+ f"{self.agent.agent_name} code execution output:"+ )return await self.get_terminal_output()- async def get_terminal_output(self, wait_with_output=3, wait_without_output=10):- idle=0+ async def get_terminal_output(+ self, wait_with_output=3, wait_without_output=10, max_exec_time=15+ ):+ idle = 0SLEEP_TIME = 0.1- while True:+ start_time = time.time()+ full_output = ""++ while max_exec_time <= 0 or time.time() - start_time < max_exec_time:await asyncio.sleep(SLEEP_TIME) # Wait for some output to be generated- full_output, partial_output = await self.state.shell.read_output()+ full_output, partial_output = await self.state.shell.read_output(+ max_exec_time+ )++ await self.agent.handle_intervention() # wait for intervention and handle it, if paused- await self.agent.handle_intervention() # wait for intervention and handle it, if paused-if partial_output:PrintStyle(font_color="#85C1E9").stream(partial_output)self.log.update(content=full_output)- idle=0+ idle = 0else:- idle+=1- if ( full_output and idle > wait_with_output / SLEEP_TIME ) or ( not full_output and idle > wait_without_output / SLEEP_TIME ): return full_output+ idle += 1+ if (full_output and idle > wait_with_output / SLEEP_TIME) or (+ not full_output and idle > wait_without_output / SLEEP_TIME+ ):+ break+ return full_outputasync def reset_terminal(self):self.state.shell.close()await self.prepare_state(reset=True)response = self.agent.read_prompt("fw.code_reset.md")self.log.update(content=response)- return response\ No newline at end of file+ return responsediff --git a/python/tools/knowledge_tool.py b/python/tools/knowledge_tool.pyindex 515a90a91..ae5d36b47 100644--- a/python/tools/knowledge_tool.py+++ b/python/tools/knowledge_tool.py@@ -24,8 +24,10 @@ class Knowledge(Tool):# duckduckgo searchduckduckgo = executor.submit(duckduckgo_search.search, question)- # memory search- future_memory = executor.submit(memory_tool.search, self.agent, question)+ # manual memory search+ future_memory_man = executor.submit(memory_tool.search, self.agent, "manual", question)+ # history memory search+ # future_memory_man = executor.submit(memory_tool.search, self.agent, "history", question)# Wait for both functions to completetry:@@ -41,7 +43,7 @@ class Knowledge(Tool):duckduckgo_result = "DuckDuckGo search failed: " + str(e)try:- memory_result = future_memory.result()+ memory_result = future_memory_man.result()except Exception as e:handle_error(e)memory_result = "Memory search failed: " + str(e)diff --git a/python/tools/memory_tool.py b/python/tools/memory_tool.pyindex f6182dc78..ffa9c1200 100644--- a/python/tools/memory_tool.py+++ b/python/tools/memory_tool.py@@ -1,29 +1,32 @@import re+from typing import Literalfrom agent import Agent-from python.helpers.vector_db import VectorDB, Document+from python.helpers.vector_db import get_or_createimport osfrom python.helpers.tool import Tool, Responsefrom python.helpers.print_style import PrintStylefrom python.helpers.errors import handle_error-# databases based on subdirectories from agent config-dbs = {}++type Area = Literal['manual', 'history']class Memory(Tool):async def execute(self,**kwargs):result=""-++ area = kwargs.get("area", "manual") # when called by agent, it will always be manual+try:if "query" in kwargs:threshold = float(kwargs.get("threshold", 0.1))count = int(kwargs.get("count", 5))- result = search(self.agent, kwargs["query"], count, threshold)+ result = search(self.agent, area, kwargs["query"], count, threshold)elif "memorize" in kwargs:- result = save(self.agent, kwargs["memorize"])+ result = save(self.agent, area, kwargs["memorize"])elif "forget" in kwargs:- result = forget(self.agent, kwargs["forget"])- elif "delete" in kwargs:- result = delete(self.agent, kwargs["delete"])+ result = forget(self.agent, area, kwargs["forget"])+ # elif "delete" in kwargs+ result = delete(self.agent, area, kwargs["delete"])except Exception as e:handle_error(e)# hint about embedding change with existing database@@ -34,39 +37,39 @@ class Memory(Tool):# result = process_query(self.agent, self.args["memory"],self.args["action"], result_count=self.agent.config.auto_memory_count)return Response(message=result, break_loop=False)-def search(agent:Agent, query:str, count:int=5, threshold:float=0.1):- db = get_db(agent)+def search(agent:Agent, area: Area, query:str, count:int=5, threshold:float=0.1):+ db = get_db(agent, area)# docs = db.search_similarity(query,count) # type: ignoredocs = db.search_similarity_threshold(query,count,threshold) # type: ignoreif len(docs)==0: return agent.read_prompt("fw.memories_not_found.md", query=query)else: return str(docs)-def save(agent:Agent, text:str):- db = get_db(agent)+def save(agent:Agent, area: Area, text:str):+ db = get_db(agent, area)id = db.insert_text(text) # type: ignorereturn agent.read_prompt("fw.memory_saved.md", memory_id=id)-def delete(agent:Agent, ids_str:str):- db = get_db(agent)+def delete(agent:Agent, area: Area, ids_str:str):+ db = get_db(agent, area)ids = extract_guids(ids_str)deleted = db.delete_documents_by_ids(ids) # type: ignorereturn agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)-def forget(agent:Agent, query:str):- db = get_db(agent)+def forget(agent:Agent, area: Area, query:str):+ db = get_db(agent, area)deleted = db.delete_documents_by_query(query) # type: ignorereturn agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)-def get_db(agent: Agent):- mem_dir = os.path.join("memory", agent.config.memory_subdir)+def get_db(agent: Agent, area: Area):+ mem_dir = os.path.join("memory", agent.config.memory_subdir or "default", area)kn_dir = os.path.join("knowledge", agent.config.knowledge_subdir)- key = (mem_dir, kn_dir)- if key not in dbs:- db = VectorDB(agent.context.log,embeddings_model=agent.config.embeddings_model, in_memory=False, memory_dir=mem_dir, knowledge_dir=kn_dir)- dbs[key] = db- else:- db = dbs[key]+ db = get_or_create(+ agent.context.log,+ embeddings_model=agent.config.embeddings_model,+ in_memory=False,+ memory_dir=mem_dir,+ knowledge_dir=kn_dir)return dbdiff --git a/requirements.txt b/requirements.txtindex 63d179149..d78330580 100644--- a/requirements.txt+++ b/requirements.txt@@ -19,4 +19,5 @@ pynput==1.7.7pypdf==4.3.1Flask[async]==3.0.3Flask-BasicAuth==0.2.0-faiss-cpu==1.8.0.post1\ No newline at end of file+faiss-cpu==1.8.0.post1+langchain-ollama==0.1.3\ No newline at end of filediff --git a/run_ui.py b/run_ui.pyindex a6481594f..6dd6310af 100644--- a/run_ui.py+++ b/run_ui.py@@ -26,8 +26,13 @@ basic_auth = BasicAuth(app)# get context to run agent zero indef get_context(ctxid:str):with lock:- if not ctxid: return AgentContext.first() or AgentContext(config=initialize())- return AgentContext.get(ctxid) or AgentContext(config=initialize(),id=ctxid)+ if not ctxid:+ first = AgentContext.first()+ if first: return first+ return AgentContext(config=initialize())+ got = AgentContext.get(ctxid)+ if got: return got+ return AgentContext(config=initialize(),id=ctxid)# Now you can use @requires_auth function decorator to require login on certain pagesdef requires_auth(f):diff --git a/test.py b/test.pyindex d556cbf4d..3b1b5133a 100644--- a/test.py+++ b/test.py@@ -1,22 +1,293 @@-from python.helpers.strings import calculate_valid_match_lengths+from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError+from python.helpers.print_style import PrintStyle+import asyncio+import random+import re-# first = b'python3 -c \'from selenium import webdriver\nfrom selenium.webdriver.chrome.service import Service\nfrom webdriver_manager.chrome import ChromeDriverManager\nimport time\n\n# Set up the Chromium WebDriver\noptions = webdriver.ChromeOptions()\noptions.add_argument(\'"\'"\'--headless\'"\'"\') # Run in headless mode\noptions.add_argument(\'"\'"\'--no-sandbox\'"\'"\')\noptions.add_argument(\'"\'"\'--disable-dev-shm-usage\'"\'"\')\n\n# Specify the correct version of ChromeDriver\nservice = Service(\'"\'"\'/root/.wdm/drivers/chromedriver/linux64/128.0.6613.113/chromedriver\'"\'"\')\ndriver = webdriver.Chrome(service=service, options=options)\n\n# Navigate to the LinkedIn profile\nurl = \'"\'"\'https://www.linkedin.com/in/jan-tomasek/\'"\'"\'\ndriver.get(url)\n\n# Wait for the page to load\ntime.sleep(5)\n\n# Save the page source to a file\nwith open(\'"\'"\'jan_tomasek_linkedin.html\'"\'"\', \'"\'"\'w\'"\'"\', encoding=\'"\'"\'utf-8\'"\'"\') as file:\n file.write(driver.page_source)\n\n# Close the WebDriver\ndriver.quit()\'\n'-first = b'https://www.linkedin.com/in/jan-tomasek/\'"\'"\'\ndriver.get(url)\n\n# Wait for the page to load\ntime.sleep(5)\n\n# Save the page source to a file\nwith open(\'"\'"\'jan_tomasek_linkedin.html\'"\'"\', \'"\'"\'w\'"\'"\', encoding=\'"\'"\'utf-8\'"\'"\') as file:\n file.write(driver.page_source)\n\n# Close the WebDriver\ndriver.quit()\'\n'+# List of user agents+user_agents = [+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15',+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36',+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1',+ 'Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/91.0.4472.80 Mobile/15E148 Safari/604.1',+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59',+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'+]-# second = b'python3 -c \'from selenium import webdriver\r\n\x1b[?2004l\r\x1b[?2004h> from selenium.webdriver.chrome.service import Service\r\n\x1b[?2004l\r\x1b[?2004h> from webdriver_manager.chrome import ChromeDriverManager\r\n\x1b[?2004l\r\x1b[?2004h> import time\r\n\x1b[?2004l\r\x1b[?2004h> \r\n\x1b[?2004l\r\x1b[?2004h> # Set up the Chromium WebDriver\r\n\x1b[?2004l\r\x1b[?2004h> options = webdriver.ChromeOptions()\r\n\x1b[?2004l\r\x1b[?2004h> options.add_argument(\'"\'"\'--headless\'"\'"\') # Run in headless mode\r\n\x1b[?2004l\r\x1b[?2004h> options.add_argument(\'"\'"\'--no-sandbox\'"\'"\')\r\n\x1b[?2004l\r\x1b[?2004h> options.add_argument(\'"\'"\'--disable-dev-shm-usage\'"\'"\')\r\n\x1b[?2004l\r\x1b[?2004h> \r\n\x1b[?2004l\r\x1b[?2004h> # Specify the correct version of ChromeDriver\r\n\x1b[?2004l\r\x1b[?2004h> service = Service(\'"\'"\'/root/.wdm/drivers/chromedriver/linux64/128.0.6613.113/chromedriver\'"\'"\')\r\n\x1b[?2004l\r\x1b[?2004h> driver = webdriver.Chrome(service=service, options=options)\r\n\x1b[?2004l\r\x1b[?2004h> \r\n\x1b[?2004l\r\x1b[?2004h> # Navigate to the LinkedIn profile\r\n\x1b[?2004l\r\x1b[?2004h> url = \'"\'"\'https://www.linkedin.com/in/jan-tomasek/\'"\'"\'\r\n\x1b[?'-second = b'https://www.linkedin.com/in/jan-tomasek/\'"\'"\'\r\n\x1b[?'+async def scrape_page(url, headless=True):+ try:+ async with async_playwright() as p:+ browser = await p.chromium.launch(headless=headless)+ context = await browser.new_context(+ user_agent=random.choice(user_agents)+ )+ page = await context.new_page()+ page.set_default_timeout(30000)++ await page.goto(url, wait_until='domcontentloaded', timeout=30000)+ await page.wait_for_selector('body', timeout=5000)++ result = await page.evaluate(r'''() => {+ const cleanText = (text) => {+ return text+ .replace(/class="[^"]*"/g, '')+ .replace(/id="[^"]*"/g, '')+ .replace(/<[^>]+>/g, '')+ .replace(/\s+/g, ' ')+ .trim();+ };+ const getMetaContent = (name) => {+ const meta = document.querySelector(`meta[name="${name}"], meta[property="${name}"]`);+ return meta ? cleanText(meta.content) : null;+ };+ const getMainContent = () => {+ const contentSelectors = [+ 'main', '#content', '#main-content', '.content',+ '.post-content', '.entry-content', '.article-content',+ '#mw-content-text', '.mw-parser-output'+ ];+ for (const selector of contentSelectors) {+ const element = document.querySelector(selector);+ if (element) {+ return cleanText(element.innerText);+ }+ }+ return cleanText(document.body.innerText);+ };+ const getProducts = () => {+ const productSelectors = [+ '.s-result-item', '.product-item', '[data-component-type="s-search-result"]', '.sg-col-inner'+ ];+ for (const selector of productSelectors) {+ const elements = document.querySelectorAll(selector);+ if (elements.length > 0) {+ return Array.from(elements).slice(0, 10).map(product => {+ const titleElement = product.querySelector('h2 a, .a-link-normal.a-text-normal');+ const priceElement = product.querySelector('.a-price .a-offscreen, .a-price');+ const imageElement = product.querySelector('img.s-image');+ let price = priceElement ? cleanText(priceElement.textContent) : null;+ // Remove duplicate price+ price = price ? price.replace(/(\$\d+(?:\.\d{2}?))\1+/, '$1') : null;+ return {+ title: titleElement ? cleanText(titleElement.textContent) : null,+ link: titleElement ? titleElement.href : null,+ price: price,+ image: imageElement ? imageElement.src : null+ };+ }).filter(product => product.title && product.link);+ }+ }+ return [];+ };+ const getNavigation = () => {+ const navSelectors = ['nav', 'header', '[data-cy="header-nav"]'];+ for (const selector of navSelectors) {+ const navElement = document.querySelector(selector);+ if (navElement) {+ return Array.from(navElement.querySelectorAll('a'))+ .filter(link => !link.href.endsWith('.svg'))+ .slice(0, 10) // Limit to 10 items+ .map(link => ({+ href: link.href,+ text: cleanText(link.textContent)+ }));+ }+ }+ return [];+ };+ const getImages = () => {+ return Array.from(document.querySelectorAll('img'))+ .filter(img => !img.src.endsWith('.svg'))+ .map(img => ({+ src: img.src,+ alt: img.alt,+ }));+ };+ const getLists = () => {+ const listSelectors = [+ '.mw-parser-output > ul', '.mw-parser-output > ol',+ '[data-cy="main-content"] ul', '[data-cy="main-content"] ol',+ 'main ul', 'main ol',+ '.content ul', '.content ol',+ '#content ul', '#content ol'+ ];+ let lists = [];+ for (const selector of listSelectors) {+ const elements = document.querySelectorAll(selector);+ if (elements.length > 0) {+ lists = Array.from(elements)+ .filter(list => list.children.length >= 3 && list.children.length <= 20)+ .map(list => ({+ type: list.tagName.toLowerCase(),+ items: Array.from(list.children)+ .filter(li => li.textContent.trim().length > 10)+ .map(li => {+ // Remove CSS classes, IDs, and inline styles+ let text = li.textContent.replace(/\s+/g, ' ').trim();+ // Remove any remaining HTML tags+ text = text.replace(/<[^>]+>/g, '');+ // Remove CSS-related content+ text = text.replace(/\.mw-parser-output[^{]+\{[^}]+\}/g, '');+ // Remove ISBN prefix if present+ text = text.replace(/^ISBN\s+/, '');+ return text;+ })+ .filter(text => text.length > 0 && !text.includes('mw-parser-output'))+ .slice(0, 10)+ }))+ .filter(list => list.items.length >= 3)+ .slice(0, 3);+ if (lists.length > 0) break;+ }+ }+ return lists;+ };+ const getSocialMediaLinks = () => {+ const socialSelectors = [+ 'a[href*="facebook.com"]',+ 'a[href*="twitter.com"]',+ 'a[href*="x.com"]',+ 'a[href*="github.com"]',+ 'a[href*="reddit.com"]',+ 'a[href*="tiktok.com"]',+ 'a[href*="discord.com"]',+ 'a[href*="instagram.com"]',+ 'a[href*="linkedin.com"]',+ 'a[href*="youtube.com"]',+ 'a[href*="pinterest.com"]',+ 'a[href*="snapchat.com"]',+ 'a[href*="tumblr.com"]',+ 'a[href*="medium.com"]',+ 'a[href*="whatsapp.com"]',+ 'a[href*="telegram.org"]',+ 'a[href*="vimeo.com"]',+ 'a[href*="flickr.com"]',+ 'a[href*="quora.com"]',+ 'a[href*="twitch.tv"]'+ ];+ return socialSelectors.map(selector => {+ const element = document.querySelector(selector);+ return element ? element.href : null;+ }).filter(Boolean);+ };+ const getCodeScripts = () => {+ const codeBlocks = document.querySelectorAll('pre code, .highlight pre, .code-block');+ return Array.from(codeBlocks).map(block => ({+ language: block.className.match(/language-(\w+)/)?.[1] || 'text',+ code: cleanText(block.textContent)+ })).filter(script => script.code.length > 0);+ };+ return {+ url: window.location.href,+ title: document.title,+ author: getMetaContent('author'),+ publishDate: getMetaContent('article:published_time') || getMetaContent('date'),+ lastModified: document.lastModified,+ keywords: getMetaContent('keywords'),+ metaDescription: getMetaContent('description') || getMetaContent('og:description'),+ mainContent: getMainContent(),+ navigation: getNavigation(),+ images: getImages(),+ lists: getLists(),+ products: getProducts(),+ socialMediaLinks: getSocialMediaLinks(),+ codeScripts: getCodeScripts(),+ };+ }''')++ await browser.close()+ return result+ except PlaywrightTimeoutError as e:+ PrintStyle(font_color="yellow", padding=True).print(f"Attempt {attempt + 1} failed due to timeout: {str(e)}")+ except Exception as e:+ PrintStyle(font_color="red", padding=True).print(f"Attempt {attempt + 1} failed: {str(e)}")+-trim_com, trim_out = calculate_valid_match_lengths(- first, second, deviation_threshold=8, deviation_reset=2,- ignore_patterns = [- rb'\x1b\[\?\d{4}[a-zA-Z](?:> )?', # ANSI escape sequences- rb'\r', # Carriage return- rb'>\s', # Greater-than symbol- ],- debug=True)+async def scrape_url(url, headless):+ try:+ result = await scrape_page(url, headless)++ if result is None:+ raise ValueError("Scraping result is None")++ markdown_content = f"# {result.get('title', 'No Title')}\n\n"++ if result.get('url'):+ markdown_content += f"URL: {result['url']}\n\n"++ if result.get('author'):+ markdown_content += f"Author: {result['author']}\n\n"++ if result.get('publishDate'):+ markdown_content += f"Published: {result['publishDate']}\n\n"++ if result.get('keywords'):+ markdown_content += f"Keywords: {result['keywords']}\n\n"++ if result.get('metaDescription'):+ markdown_content += f"## Description\n\n{result['metaDescription']}\n\n"++ if result.get('mainContent'):+ markdown_content += "## Webpage Content:\n\n"+ markdown_content += result['mainContent'] + "\n\n"++ if result.get('lists'):+ markdown_content += "## Lists\n\n"+ for list_item in result['lists']:+ markdown_content += f"### {list_item['type'].upper()} List\n\n"+ for item in list_item['items']:+ markdown_content += f"- {item}\n"+ markdown_content += "\n"++ if result.get('products'):+ markdown_content += "## Products\n\n"+ for product in result['products']:+ markdown_content += f"### {product.get('title', 'Untitled Product')}\n\n"+ if product.get('price'):+ price = product['price']+ price = re.sub(r'(\$\d+(?:\.\d{2})?)\1+', r'\1', price)+ markdown_content += f"Price: {price}\n\n"+ if product.get('link'):+ markdown_content += f"[View Product]({product['link']})\n\n"+ if product.get('image'):+ markdown_content += f"\n\n"+ markdown_content += "---\n\n"++ if result.get('socialMediaLinks'):+ markdown_content += "## Social Media Links\n\n"+ for link in result['socialMediaLinks']:+ markdown_content += f"- [{link.split('.com')[0].split('/')[-1].capitalize()}]({link})\n"+ markdown_content += "\n"++ if result.get('codeScripts'):+ markdown_content += "## Code Snippet\n\n"+ for script in result['codeScripts']:+ markdown_content += f"```{script['language']}\n{script['code']}\n```\n\n"++ markdown_content = markdown_content.strip()++ return markdown_content+ except Exception as e:+ return f"Error: Failed to scrape URL. Reason: {str(e)}"-if(trim_com > 0 and trim_out > 0):- sec_tr = second[:trim_out]-else: sec_tr = "original"+async def fetch_page_content(url: str, max_retries: int = 2, headless: bool = True):+ for attempt in range(max_retries):+ try:+ content = await scrape_url(url, headless)+ if content.startswith("Error:"):+ raise Exception(content)+ return str(content)+ except Exception as e:+ PrintStyle(font_color="red", padding=True).print(f"Attempt {attempt + 1} failed: {str(e)}")++ raise Exception("Error: Webpage content is not available.")-print(sec_tr)\ No newline at end of file++async def test():+ url = "https://github.com/frdel/agent-zero"+ content = await fetch_page_content(url)+ print(content)++asyncio.run(test())\ No newline at end of filediff --git a/test2.py b/test2.pynew file mode 100644index 000000000..f32a057dd--- /dev/null+++ b/test2.py@@ -0,0 +1,120 @@+import requests+from bs4 import BeautifulSoup+import random+import re++# List of user agents+user_agents = [+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15',+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36',+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1',+ 'Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/91.0.4472.80 Mobile/15E148 Safari/604.1',+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59',+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'+]++def clean_text(text):+ """Utility function to clean HTML tags and whitespace."""+ text = re.sub(r'class="[^"]*"', '', text)+ text = re.sub(r'id="[^"]*"', '', text)+ text = re.sub(r'<[^>]+>', '', text)+ return re.sub(r'\s+', ' ', text).strip()++def scrape_page(url):+ headers = {+ 'User-Agent': random.choice(user_agents)+ }++ try:+ response = requests.get(url, headers=headers, timeout=30)+ response.raise_for_status() # Check if the request was successful+ soup = BeautifulSoup(response.content, 'html.parser')++ # Extract title+ title = soup.title.string if soup.title else 'No Title'++ # Extract meta information+ def get_meta_content(name):+ meta = soup.find('meta', attrs={'name': name}) or soup.find('meta', property=name)+ return meta['content'] if meta else None++ author = get_meta_content('author')+ publish_date = get_meta_content('article:published_time') or get_meta_content('date')+ keywords = get_meta_content('keywords')+ description = get_meta_content('description') or get_meta_content('og:description')++ # Extract main content+ def get_main_content():+ content_selectors = ['main', '#content', '#main-content', '.content', '.post-content', '.entry-content', '.article-content', '#mw-content-text', '.mw-parser-output']+ for selector in content_selectors:+ element = soup.select_one(selector)+ if element:+ return clean_text(element.get_text())+ return clean_text(soup.body.get_text() if soup.body else "")++ # Extract images+ def get_images():+ images = []+ for img in soup.find_all('img'):+ if not img.get('src', '').endswith('.svg'):+ images.append({+ 'src': img['src'],+ 'alt': img.get('alt', '')+ })+ return images++ # Extract product listings+ def get_products():+ products = []+ product_selectors = ['.s-result-item', '.product-item', '[data-component-type="s-search-result"]', '.sg-col-inner']+ for selector in product_selectors:+ elements = soup.select(selector)+ if elements:+ for product in elements[:10]:+ title_element = product.select_one('h2 a, .a-link-normal.a-text-normal')+ price_element = product.select_one('.a-price .a-offscreen, .a-price')+ image_element = product.select_one('img.s-image')+ price = clean_text(price_element.text) if price_element else None+ price = re.sub(r'(\$\d+(?:\.\d{2})?)\1+', r'\1', price) if price else None+ products.append({+ 'title': clean_text(title_element.text) if title_element else None,+ 'link': title_element['href'] if title_element else None,+ 'price': price,+ 'image': image_element['src'] if image_element else None+ })+ return products++ # Extract lists+ def get_lists():+ lists = []+ list_selectors = ['.mw-parser-output > ul', '.mw-parser-output > ol', 'main ul', 'main ol', '.content ul', '.content ol']+ for selector in list_selectors:+ elements = soup.select(selector)+ for element in elements:+ list_items = [clean_text(li.get_text()) for li in element.find_all('li') if len(li.get_text().strip()) > 10]+ if 3 <= len(list_items) <= 20:+ lists.append({'type': element.name, 'items': list_items[:10]})+ return lists++ # Gather all extracted data+ return {+ 'url': url,+ 'title': title,+ 'author': author,+ 'publishDate': publish_date,+ 'keywords': keywords,+ 'metaDescription': description,+ 'mainContent': get_main_content(),+ 'images': get_images(),+ 'products': get_products(),+ 'lists': get_lists()+ }++ except requests.exceptions.RequestException as e:+ return {"error": str(e)}++# Example usage:+result = scrape_page('https://github.com/frdel/agent-zero')+print(result)diff --git a/webui/index.css b/webui/index.cssindex c8838c543..1a4dfdfa8 100644--- a/webui/index.css+++ b/webui/index.css@@ -327,6 +327,14 @@ input:checked + .slider:before {.msg-thoughts{}+.message-temp {+ display: none;+}++.message-temp:last-child {+ display: block; /* or any style you want for visibility */+}+.connected{color: #6ec583;}diff --git a/webui/index.js b/webui/index.jsindex 934741af5..50bfd19ec 100644--- a/webui/index.js+++ b/webui/index.js@@ -55,7 +55,7 @@ chatInput.addEventListener('keydown', (e) => {sendButton.addEventListener('click', sendMessage);-function setMessage(id, type, heading, content, kvps = null) {+function setMessage(id, type, heading, content, temp, kvps = null) {// Search for the existing message container by idlet messageContainer = document.getElementById(`message-${id}`);@@ -68,10 +68,12 @@ function setMessage(id, type, heading, content, kvps = null) {messageContainer = document.createElement('div');messageContainer.id = `message-${id}`;messageContainer.classList.add('message-container', `${sender}-container`);+ if (temp) messageContainer.classList.add("message-temp")+}const handler = msgs.getHandler(type);- handler(messageContainer, id, type, heading, content, kvps);+ handler(messageContainer, id, type, heading, content, temp, kvps);// If the container was found, it was already in the DOM, no need to append againif (!document.getElementById(`message-${id}`)) {@@ -82,6 +84,7 @@ function setMessage(id, type, heading, content, kvps = null) {}+function adjustTextareaHeight() {chatInput.style.height = 'auto';chatInput.style.height = (chatInput.scrollHeight) + 'px';@@ -131,7 +134,7 @@ async function poll() {if (lastLogVersion != response.log_version) {for (const log of response.logs) {- setMessage(log.no, log.type, log.heading, log.content, log.kvps);+ setMessage(log.no, log.type, log.heading, log.content, log.temp, log.kvps);}}@@ -186,7 +189,7 @@ window.killChat = async function (id) {if (other) setContext(other.id)else setContext(generateGUID())}-+if (found) sendJsonData("/remove", { context: id });}diff --git a/webui/messages.js b/webui/messages.jsindex b2607da1d..206ebec8d 100644--- a/webui/messages.js+++ b/webui/messages.js@@ -1,6 +1,3 @@---export function getHandler(type) {switch (type) {case 'user':@@ -30,7 +27,8 @@ export function getHandler(type) {}}-export function drawMessageDefault(messageContainer, id, type, heading, content, kvps = null) {+export function _drawMessage(messageContainer, heading, content, temp, kvps = null, messageClasses = [], contentClasses = []) {+// if (type !== 'user') {// const agentStart = document.createElement('div');@@ -40,7 +38,7 @@ export function drawMessageDefault(messageContainer, id, type, heading, content,// }const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-default');+ messageDiv.classList.add('message', ...messageClasses);if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading@@ -50,9 +48,8 @@ export function drawMessageDefault(messageContainer, id, type, heading, content,textNode.textContent = content;textNode.style.whiteSpace = 'pre-wrap';textNode.style.wordBreak = 'break-word';- textNode.classList.add("msg-json");+ textNode.classList.add("message-content", ...contentClasses)messageDiv.appendChild(textNode);-messageContainer.appendChild(messageDiv);// if (type !== 'user') {@@ -62,181 +59,61 @@ export function drawMessageDefault(messageContainer, id, type, heading, content,// messageContainer.appendChild(actions);// }+ return messageDiv}-export function drawMessageAgent(messageContainer, id, type, heading, content, kvps = null) {--- // if (kvps && kvps['tool_name'] === 'response') {- // drawMessageResponse(messageContainer, id, type, heading, content, kvps)- // return- // }-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-agent');-- if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading+export function drawMessageDefault(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, kvps, ['message-ai', 'message-default'], ['msg-json']);+}+export function drawMessageAgent(messageContainer, id, type, heading, content, temp, kvps = null) {+ let kvpsFlat=nullif (kvps) {- const kvpsFlat = { ...kvps, ...kvps['tool_args'] || {} }+ kvpsFlat = { ...kvps, ...kvps['tool_args'] || {} }delete kvpsFlat['tool_args']- drawKvps(messageDiv, kvpsFlat);}- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- textNode.classList.add("msg-json");- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);-- // const actions = document.createElement('div');- // actions.classList.add('message-actions');- // actions.innerHTML = '<span class="message-action">Copy</span> · <span class="message-action">Retry</span> · <span class="message-action">Edit</span>';- // messageContainer.appendChild(actions);-+ _drawMessage(messageContainer, heading, content, temp, kvpsFlat, ['message-ai', 'message-agent'], ['msg-json']);}-export function drawMessageResponse(messageContainer, id, type, heading, content, kvps = null) {-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-agent-response', 'message-fw');-- if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading-- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);+export function drawMessageResponse(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, null, ['message-ai', 'message-agent-response', 'message-fw']);}-export function drawMessageDelegation(messageContainer, id, type, heading, content, kvps = null) {-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-agent', 'message-fw', 'message-agent-delegation');-- if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading-- const pars = kvps && kvps["tool_args"] ? kvps["tool_args"] : { text: "" }-- drawKvps(messageDiv, { "Thoughts": kvps["thoughts"], "Message": pars["text"], "Reset": pars["reset"] });--- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);+export function drawMessageDelegation(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, kvps, ['message-ai', 'message-agent', 'message-fw', 'message-agent-delegation']);}-export function drawMessageUser(messageContainer, id, type, heading, content, kvps = null) {-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-user');-- drawKvps(messageDiv, kvps);-- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);+export function drawMessageUser(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, kvps, ['message-user']);}--export function drawMessageTool(messageContainer, id, type, heading, content, kvps = null) {-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-tool', 'message-fw');-- if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading-- drawKvps(messageDiv, kvps);-- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- textNode.classList.add("msg-output");- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);-- // const actions = document.createElement('div');- // actions.classList.add('message-actions');- // actions.innerHTML = '<span class="message-action">Copy output</span>';- // messageContainer.appendChild(actions);-+export function drawMessageTool(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, kvps, ['message-ai', 'message-tool', 'message-fw'], ['msg-output']);}-export function drawMessageCodeExe(messageContainer, id, type, heading, content, kvps = null) {-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', 'message-code-exe', 'message-fw');-- if (heading) messageDiv.appendChild(document.createElement('h4')).textContent = heading-- drawKvps(messageDiv, kvps);-- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);-- // const actions = document.createElement('div');- // actions.classList.add('message-actions');- // actions.innerHTML = '<span class="message-action">Copy code</span> · <span class="message-action">Copy output</span>';- // messageContainer.appendChild(actions);+export function drawMessageCodeExe(messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, null, ['message-ai', 'message-code-exe', 'message-fw']);}-export function drawMessageAgentPlain(classes, messageContainer, id, type, heading, content, kvps = null) {-- // const agentStart = document.createElement('div');- // agentStart.classList.add('agent-start');- // agentStart.textContent = 'System warning...';- // messageContainer.appendChild(agentStart);-- const messageDiv = document.createElement('div');- messageDiv.classList.add('message', 'message-ai', ...classes);-- drawKvps(messageDiv, kvps);-- const textNode = document.createElement('pre');- textNode.textContent = content;- textNode.style.whiteSpace = 'pre-wrap';- textNode.style.wordBreak = 'break-word';- messageDiv.appendChild(textNode);-- messageContainer.appendChild(messageDiv);-+export function drawMessageAgentPlain(classes, messageContainer, id, type, heading, content, temp, kvps = null) {+ _drawMessage(messageContainer, heading, content, temp, null, ['message-ai', ...classes]);}-export function drawMessageAdhoc(messageContainer, id, type, heading, content, kvps = null) {- return drawMessageAgentPlain(['message-adhoc'], messageContainer, id, type, heading, content, kvps);+export function drawMessageAdhoc(messageContainer, id, type, heading, content, temp, kvps = null) {+ return drawMessageAgentPlain(['message-adhoc'], messageContainer, id, type, heading, content, temp, kvps);}-export function drawMessageInfo(messageContainer, id, type, heading, content, kvps = null) {- return drawMessageAgentPlain(['message-info'], messageContainer, id, type, heading, content, kvps);+export function drawMessageInfo(messageContainer, id, type, heading, content, temp, kvps = null) {+ return drawMessageAgentPlain(['message-info'], messageContainer, id, type, heading, content, temp, kvps);}-export function drawMessageWarning(messageContainer, id, type, heading, content, kvps = null) {- return drawMessageAgentPlain(['message-warning'], messageContainer, id, type, heading, content, kvps);+export function drawMessageWarning(messageContainer, id, type, heading, content, temp, kvps = null) {+ return drawMessageAgentPlain(['message-warning'], messageContainer, id, type, heading, content, temp, kvps);}-export function drawMessageError(messageContainer, id, type, heading, content, kvps = null) {- return drawMessageAgentPlain(['message-error'], messageContainer, id, type, heading, content, kvps);+export function drawMessageError(messageContainer, id, type, heading, content, temp, kvps = null) {+ return drawMessageAgentPlain(['message-error'], messageContainer, id, type, heading, content, temp, kvps);}function drawKvps(container, kvps) {