Agent Zero AI framework
Updates for v0.6
frdel9c258b77a222
diff --git a/.gitignore b/.gitignoreindex 3a26ac969..cd5e94f72 100644--- a/.gitignore+++ b/.gitignore@@ -20,4 +20,14 @@ memory/*# Ignore all contents of the directory "logs"logs/*# But do not ignore the directory itself-!logs/.gitkeep\ No newline at end of file+!logs/.gitkeep++# Ignore all contents of the directory "tmp"+tmp/*+# But do not ignore the directory itself+!tmp/.gitkeep++# Ignore all contents of the directory "knowledge"+knowledge/*+# But do not ignore the directory itself+!knowledge/.gitkeep\ No newline at end of filediff --git a/.vscode/launch.json b/.vscode/launch.jsonindex a01eceec5..37ab9f096 100644--- a/.vscode/launch.json+++ b/.vscode/launch.json@@ -2,10 +2,18 @@"version": "0.2.0","configurations": [{- "name": "Debug main.py",+ "name": "Debug run_ui.py","type": "debugpy","request": "launch",- "program": "./main.py",+ "program": "./run_ui.py",+ "console": "integratedTerminal",+ "args": ["-Xfrozen_modules=off"]+ },+ {+ "name": "Debug run_cli.py",+ "type": "debugpy",+ "request": "launch",+ "program": "./run_cli.py","console": "integratedTerminal","args": ["-Xfrozen_modules=off"]},diff --git a/README.md b/README.mdindex 4d58cb93c..d571112c1 100644--- a/README.md+++ b/README.md@@ -1,6 +1,6 @@# Agent Zero-[](https://discord.gg/B8KZKNsPpj) [](https://www.youtube.com/@AgentZeroFW) [](https://www.linkedin.com/in/jan-tomasek/) [](https://x.com/JanTomasekDev)+[](https://www.skool.com/agent-zero) [](https://discord.gg/B8KZKNsPpj) [](https://www.youtube.com/@AgentZeroFW) [](https://www.linkedin.com/in/jan-tomasek/) [](https://x.com/JanTomasekDev)@@ -23,12 +23,12 @@3. **Multi-agent cooperation**- Every agent has a superior agent giving it tasks and instructions. Every agent then reports back to its superior.-- In the case of the first agent, the superior is the human user; the agent sees no difference.+- In the case of the first agent in the chain (Agent 0), the superior is the human user; the agent sees no difference.- Every agent can create its subordinate agent to help break down and solve subtasks. This helps all agents keep their context clean and focused.4. **Completely customizable and extensible**- Almost nothing in this framework is hard-coded. Nothing is hidden. Everything can be extended or changed by the user.-- The whole behavior is defined by a system prompt in the **prompts/agent.system.md** file. Change this prompt and change the framework dramatically.+- The whole behavior is defined by a system prompt in the **prompts/default/agent.system.md** file. Change this prompt and change the framework dramatically.- The framework does not guide or limit the agent in any way. There are no hard-coded rails that agents have to follow.- Every prompt, every small message template sent to the agent in its communication loop, can be found in the **prompts/** folder and changed.- Every default tool can be found in the **python/tools/** folder and changed or copied to create new predefined tools.@@ -81,10 +81,10 @@ If you cannot provide all the necessary conditions or API keys, just change theUpdate: [Guide by CheezChat for Windows](./docs/win_installation_guide.txt)1. **Required API keys:**-- At the moment, the only recommended API key is for https://www.perplexity.ai/ API. Perplexity is used as a convenient web search tool and has not yet been replaced by an open-source alternative. If you do not have an API key for Perplexity, leave it empty in the .env file and Perplexity will not be used.-- Chat models and embedding models can be executed locally via Ollama and HuggingFace or via API as well.+- No API keys are required. Models can run locally. The only recommended API key is for https://www.perplexity.ai/ API. Perplexity is used as a convenient web search tool and has not yet been replaced by an open-source alternative in Agent Zero. If you do not have an API key for Perplexity, leave it empty in the .env file and Perplexity will not be used.+- Chat models and embedding models can be executed locally via Ollama, LMStudio and HuggingFace or via API as well.-2. **Enter your API keys:**+2. **Enter your API keys if needed:**- You can enter your API keys into the **.env** file, which you can copy from **example.env**- Or you can export your API keys in the terminal session:~~~bash@@ -97,9 +97,9 @@ export API_KEY_OPENAI="your-api-key-here"pip install -r requirements.txt~~~-3. **Choose your chat, utility and embeddings model:**-- In the **main.py** file, right at the start of the **chat()** function, you can see how the chat model and embedding model are set.-- You can choose between online models (OpenAI, Anthropic, Groq) or offline (Ollama, HuggingFace) for both.+3. **Choose your chat, utility and embeddings model and check other configuration options:**+- In the **initialize.py** file, you can see how the chat model and embedding model are set.+- You can choose between online models (OpenAI, Anthropic, Groq) or offline (Ollama, LMStudio, HuggingFace) for both.4. **run Docker:**- Easiest way is to install Docker Desktop application and just run it. The rest will be handled by the framework itself.diff --git a/agent.py b/agent.pyindex 1aaa7d4ae..b279a639f 100644--- a/agent.py+++ b/agent.py@@ -9,18 +9,24 @@ from langchain_core.messages import HumanMessage, SystemMessagefrom langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.language_models.llms import BaseLLMfrom langchain_core.embeddings import Embeddings+from concurrent.futures import Future+from python.helpers.log import Log+from python.helpers.dirty_json import DirtyJson+@dataclassclass AgentConfig:chat_model: BaseChatModel | BaseLLMutility_model: BaseChatModel | BaseLLMembeddings_model:Embeddings+ prompts_subdir: str = ""memory_subdir: str = ""+ knowledge_subdir: str = ""auto_memory_count: int = 3auto_memory_skip: int = 2rate_limit_seconds: int = 60rate_limit_requests: int = 15- rate_limit_input_tokens: int = 1000000+ rate_limit_input_tokens: int = 0rate_limit_output_tokens: int = 0msgs_keep_max: int = 25msgs_keep_start: int = 5@@ -54,23 +60,35 @@ class Agent:self.number = numberself.agent_name = f"Agent {self.number}"- self.system_prompt = files.read_file("./prompts/agent.system.md", agent_name=self.agent_name)- self.tools_prompt = files.read_file("./prompts/agent.tools.md")-self.history = []self.last_message = ""self.intervention_message = ""self.intervention_status = Falseself.rate_limiter = rate_limiter.RateLimiter(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+ self.future: Future|None = None+os.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir++ def communicate(self, msg: str):+ Agent.paused=False #unpause if paused+ if not self.future or self.future.done():+ return self.message_loop(msg)+ else:+ if Agent.streaming_agent: current_agent = Agent.streaming_agent+ else: current_agent = self++ current_agent.intervention_message = msg #intervene current agent+ if self.future: return self.future.result() #wait for original agent+ else: return ""def message_loop(self, msg: str):try:+ self.future = Future()printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)- user_message = files.read_file("./prompts/fw.user_message.md", message=msg)+ user_message = self.read_prompt("fw.user_message.md", message=msg)self.append_message(user_message, human=True) # Append the user's input to the historymemories = self.fetch_memories(True)@@ -81,7 +99,7 @@ class Agent:try:- system = self.system_prompt + "\n\n" + self.tools_prompt+ system = self.read_prompt("agent.system.md", agent_name=self.agent_name) + "\n\n" + self.read_prompt("agent.tools.md")memories = self.fetch_memories()if memories: system+= "\n\n"+memories@@ -97,8 +115,9 @@ class Agent: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}: Starting a message:")-+ PrintStyle(bold=True, font_color="green", padding=True, background_color="white").print(f"{self.agent_name}: Generating:")+ log = Log(type="agent", heading=f"{self.agent_name}: Generating:")+for chunk in chain.stream(inputs):if self.handle_intervention(agent_response): break # wait for intervention and handle it, if paused@@ -107,33 +126,50 @@ class Agent:else: content = str(chunk)if content:- printer.stream(content) # output the agent response stream+ printer.stream(content) # output the agent response streamagent_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))if not self.handle_intervention(agent_response):if self.last_message == agent_response: #if assistant_response is the same as last message in history, let him knowself.append_message(agent_response) # Append the assistant's response to the history- warning_msg = files.read_file("./prompts/fw.msg_repeat.md")+ warning_msg = self.read_prompt("fw.msg_repeat.md")self.append_message(warning_msg, human=True) # Append warning message to the historyPrintStyle(font_color="orange", padding=True).print(warning_msg)+ Log.log(type="warning", content=warning_msg)else: #otherwise proceed with toolself.append_message(agent_response) # Append the assistant's response to the historytools_result = self.process_tools(agent_response) # process tools requested in agent message- if tools_result: return tools_result #break the execution if the task is done+ if tools_result: #final response of message loop available+ self.future.set_result(tools_result) #set result to future+ return tools_result #break the execution if the task is done- # Forward errors to the LLM, maybe he can fix them+ # Forward errors to the LLM, maybe it can fix themexcept Exception as e:error_message = errors.format_error(e)- msg_response = files.read_file("./prompts/fw.error.md", error=error_message) # error message template+ msg_response = self.read_prompt("fw.error.md", error=error_message) # error message templateself.append_message(msg_response, human=True)PrintStyle(font_color="red", padding=True).print(msg_response)+ Log.log(type="error", content=msg_response)+ self.future.set_exception(e) #set result to futurefinally:Agent.streaming_agent = None # unset current streamer+ 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)+ except Exception as e:+ pass+ if not content:+ content = files.read_file(files.get_abs_path(f"./prompts/default/{file}"), **kwargs)+ return content+def get_data(self, field:str):return self.data.get(field, None)@@ -162,10 +198,12 @@ class Agent:chain = prompt | self.config.utility_modelresponse = ""printer = None+ logger = Noneif 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)+ printer = PrintStyle(italic=True, font_color="orange", padding=False)+ logger = Log(type="adhoc", heading=f"{self.agent_name}: {output_label}:")formatted_inputs = prompt.format()tokens = int(len(formatted_inputs)/4)@@ -180,6 +218,7 @@ class Agent:if printer: printer.stream(content)response+=content+ if logger: logger.update(content=response)self.rate_limiter.set_output_tokens(int(len(response)/4))@@ -190,7 +229,7 @@ class Agent:return self.history[-1]def replace_middle_messages(self,middle_messages):- cleanup_prompt = files.read_file("./prompts/fw.msg_cleanup.md")+ cleanup_prompt = self.read_prompt("fw.msg_cleanup.md")summary = self.send_adhoc_message(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]@@ -225,7 +264,7 @@ class Agent:while self.paused: time.sleep(0.1) # wait if pausedif self.intervention_message and not self.intervention_status: # if there is an intervention message, but not yet processedif progress.strip(): self.append_message(progress) # append the response generated so far- user_msg = files.read_file("./prompts/fw.intervention.md", user_message=self.intervention_message) # format the user intervention template+ user_msg = self.read_prompt("fw.intervention.md", user_message=self.intervention_message) # format the user intervention templateself.append_message(user_msg,human=True) # append the intervention messageself.intervention_message = "" # reset the intervention messageself.intervention_status = True@@ -253,9 +292,10 @@ class Agent:if self.handle_intervention(): return # wait if paused and handle intervention message if neededif response.break_loop: return response.messageelse:- msg = files.read_file("prompts/fw.msg_misformat.md")+ msg = self.read_prompt("fw.msg_misformat.md")self.append_message(msg, human=True)PrintStyle(font_color="red", padding=True).print(msg)+ Log.log(type="error", content=f"{self.agent_name}: Message misformat:")def get_tool(self, name: str, args: dict, message: str, **kwargs):@@ -290,9 +330,17 @@ class Agent:"conversation_history" : messages,"raw_memories": memories}- cleanup_prompt = files.read_file("./prompts/msg.memory_cleanup.md").replace("{", "{{")+ cleanup_prompt = self.read_prompt("msg.memory_cleanup.md").replace("{", "{{")clean_memories = self.send_adhoc_message(cleanup_prompt,json.dumps(input), output_label="Memory injection")return clean_memories+ def log_from_stream(self, stream: str, log: Log):+ try:+ if len(stream) < 25: return # no reason to try+ response = DirtyJson.parse_string(stream)+ if isinstance(response, dict): log.update(content=stream, kvps=response) #log if result is a dictionary already+ except Exception as e:+ pass+def call_extension(self, name: str, **kwargs) -> Any:pass\ No newline at end of filediff --git a/docs/win_installation_guide.txt b/docs/win_installation_guide.txtindex 6ef481f65..61b579073 100644--- a/docs/win_installation_guide.txt+++ b/docs/win_installation_guide.txt@@ -49,7 +49,7 @@ pip install -r requirements.txt## Run the program- Just run the **main.py** file in Python:~~~bash-python main.py+python run_ui.py~~~- Or run it in debug mode in VS Code using the **debug** button in the top right corner of the editor. I have provided config files for VS Code for this purpose.@@ -140,7 +140,7 @@ PYDEVD_DISABLE_FILE_VALIDATION=16. Configure API PreferencesIf you entered an openAI API key earlier, you may skip this step. If you entered an alternative key,-* Right Click on the file **main.py** in the Agent-Zero root folder+* Right Click on the file **initialize.py** in the Agent-Zero root folder* Select **Open With*** Select **Notepad** or your preferred text editor* Scroll about 20 lines down from the top until you see lines that look like this: *chat_llm = models.get_*@@ -182,11 +182,11 @@ If all of the requirements installed succesfully, you can proceed to run the pro* Activate the Agent-Zero environment by double clicking on its name* click Open Terminal* Navigate to the agent-zero folder-* Type **python main.py** and press enter+* Type **python run_ui.py** and press enter```(Agent-Zero) C:\Users\yourUserName>cd c:\projects\agent-zero-(Agent-Zero) C:\projects\agent-zero\python main.py+(Agent-Zero) C:\projects\agent-zero\python run_ui.pyInitializing framework...User message ('e' to leave):diff --git a/example.env b/example.envindex 74160b831..327dac344 100644--- a/example.env+++ b/example.env@@ -9,14 +9,11 @@ API_KEY_OPENAI_AZURE=OPENAI_AZURE_ENDPOINT=OPENAI_API_VERSION=-TOKENIZERS_PARALLELISM=true-PYDEVD_DISABLE_FILE_VALIDATION=1-HF_TOKEN=-EDGE_TTS_MODEL="fr-FR-RemyMultilingualNeural"-# en-US-AndrewMultilingualNeural-# de-DE-FlorianMultilingualNeural-# es-ES-AlvaroNeural-# zh-CN-shaanxi-XiaoniNeural-# more check https://huggingface.co/spaces/innoai/Edge-TTS-Text-to-Speech and at bottom use via api++WEB_UI_PORT=50001+++TOKENIZERS_PARALLELISM=true+PYDEVD_DISABLE_FILE_VALIDATION=1\ No newline at end of filediff --git a/initialize.py b/initialize.pynew file mode 100644index 000000000..ff4a42664--- /dev/null+++ b/initialize.py@@ -0,0 +1,59 @@+import models+from agent import Agent, AgentConfig++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_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_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)++ # utility model used for helper functions (cheaper, faster)+ utility_llm = chat_llm # change if you want to use a different utility model++ # embedding model used for memory+ embedding_llm = models.get_openai_embedding(model_name="text-embedding-3-small")+ # embedding_llm = models.get_ollama_embedding(model_name="nomic-embed-text")+ # embedding_llm = models.get_huggingface_embedding(model_name="sentence-transformers/all-MiniLM-L6-v2")++ # agent configuration+ config = AgentConfig(+ chat_model = chat_llm,+ utility_model = utility_llm,+ embeddings_model = embedding_llm,+ prompts_subdir = "custom",+ # memory_subdir = "",+ auto_memory_count = 0,+ # auto_memory_skip = 2,+ # rate_limit_seconds = 60,+ rate_limit_requests = 15,+ # rate_limit_input_tokens = 0,+ # rate_limit_output_tokens = 0,+ # msgs_keep_max = 25,+ # msgs_keep_start = 5,+ # msgs_keep_end = 10,+ max_tool_response_length = 3000,+ # response_timeout_seconds = 60,+ code_exec_docker_enabled = True,+ # code_exec_docker_name = "agent-zero-exe",+ # code_exec_docker_image = "frdel/agent-zero-exe:latest",+ # code_exec_docker_ports = { "22/tcp": 50022 }+ # code_exec_docker_volumes = { files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"} }+ code_exec_ssh_enabled = True,+ # code_exec_ssh_addr = "localhost",+ # code_exec_ssh_port = 50022,+ # code_exec_ssh_user = "root",+ # code_exec_ssh_pass = "toor",+ # additional = {},+ )++ # create the first agent+ agent0 = Agent( number = 0, config = config )++ # return initialized agent+ return agent0\ No newline at end of filediff --git a/memory/.gitkeep b/knowledge/.gitkeepsimilarity index 100%rename from memory/.gitkeeprename to knowledge/.gitkeepdiff --git a/prompts/agent.memory.md b/prompts/default/agent.memory.mdsimilarity index 100%rename from prompts/agent.memory.mdrename to prompts/default/agent.memory.mddiff --git a/prompts/agent.system.md b/prompts/default/agent.system.mdsimilarity index 100%rename from prompts/agent.system.mdrename to prompts/default/agent.system.mddiff --git a/prompts/agent.tools.md b/prompts/default/agent.tools.mdsimilarity index 100%rename from prompts/agent.tools.mdrename to prompts/default/agent.tools.mddiff --git a/prompts/fw.code_no_output.md b/prompts/default/fw.code_no_output.mdsimilarity index 100%rename from prompts/fw.code_no_output.mdrename to prompts/default/fw.code_no_output.mddiff --git a/prompts/fw.code_runtime_wrong.md b/prompts/default/fw.code_runtime_wrong.mdsimilarity index 100%rename from prompts/fw.code_runtime_wrong.mdrename to prompts/default/fw.code_runtime_wrong.mddiff --git a/prompts/fw.error.md b/prompts/default/fw.error.mdsimilarity index 100%rename from prompts/fw.error.mdrename to prompts/default/fw.error.mddiff --git a/prompts/fw.intervention.md b/prompts/default/fw.intervention.mdsimilarity index 100%rename from prompts/fw.intervention.mdrename to prompts/default/fw.intervention.mddiff --git a/prompts/fw.memories_deleted.md b/prompts/default/fw.memories_deleted.mdsimilarity index 100%rename from prompts/fw.memories_deleted.mdrename to prompts/default/fw.memories_deleted.mddiff --git a/prompts/fw.memories_not_found.md b/prompts/default/fw.memories_not_found.mdsimilarity index 100%rename from prompts/fw.memories_not_found.mdrename to prompts/default/fw.memories_not_found.mddiff --git a/prompts/fw.memory_saved.md b/prompts/default/fw.memory_saved.mdsimilarity index 100%rename from prompts/fw.memory_saved.mdrename to prompts/default/fw.memory_saved.mddiff --git a/prompts/fw.msg_cleanup.md b/prompts/default/fw.msg_cleanup.mdsimilarity index 100%rename from prompts/fw.msg_cleanup.mdrename to prompts/default/fw.msg_cleanup.mddiff --git a/prompts/fw.msg_from_subordinate.md b/prompts/default/fw.msg_from_subordinate.mdsimilarity index 100%rename from prompts/fw.msg_from_subordinate.mdrename to prompts/default/fw.msg_from_subordinate.mddiff --git a/prompts/fw.msg_misformat.md b/prompts/default/fw.msg_misformat.mdsimilarity index 100%rename from prompts/fw.msg_misformat.mdrename to prompts/default/fw.msg_misformat.mddiff --git a/prompts/fw.msg_repeat.md b/prompts/default/fw.msg_repeat.mdsimilarity index 100%rename from prompts/fw.msg_repeat.mdrename to prompts/default/fw.msg_repeat.mddiff --git a/prompts/fw.msg_timeout.md b/prompts/default/fw.msg_timeout.mdsimilarity index 100%rename from prompts/fw.msg_timeout.mdrename to prompts/default/fw.msg_timeout.mddiff --git a/prompts/fw.msg_truncated.md b/prompts/default/fw.msg_truncated.mdsimilarity index 100%rename from prompts/fw.msg_truncated.mdrename to prompts/default/fw.msg_truncated.mddiff --git a/prompts/fw.tool_not_found.md b/prompts/default/fw.tool_not_found.mdsimilarity index 100%rename from prompts/fw.tool_not_found.mdrename to prompts/default/fw.tool_not_found.mddiff --git a/prompts/fw.tool_response.md b/prompts/default/fw.tool_response.mdsimilarity index 100%rename from prompts/fw.tool_response.mdrename to prompts/default/fw.tool_response.mddiff --git a/prompts/fw.user_message.md b/prompts/default/fw.user_message.mdsimilarity index 100%rename from prompts/fw.user_message.mdrename to prompts/default/fw.user_message.mddiff --git a/prompts/msg.memory_cleanup.md b/prompts/default/msg.memory_cleanup.mdsimilarity index 100%rename from prompts/msg.memory_cleanup.mdrename to prompts/default/msg.memory_cleanup.mddiff --git a/prompts/tool.knowledge.response.md b/prompts/default/tool.knowledge.response.mdsimilarity index 100%rename from prompts/tool.knowledge.response.mdrename to prompts/default/tool.knowledge.response.mddiff --git a/python/helpers/docker.py b/python/helpers/docker.pyindex 3a40f1c33..02a961c3d 100644--- a/python/helpers/docker.py+++ b/python/helpers/docker.py@@ -5,6 +5,7 @@ from typing import Optionalfrom python.helpers.files import get_abs_pathfrom python.helpers.errors import format_errorfrom python.helpers.print_style import PrintStyle+from python.helpers.log import Logclass DockerContainerManager:def __init__(self, image: str, name: str, ports: Optional[dict[str, int]] = None, volumes: Optional[dict[str, dict[str, str]]] = None):@@ -24,7 +25,9 @@ class DockerContainerManager:err = format_error(e)if ("ConnectionRefusedError(61," in err or "Error while fetching server API version" in err):PrintStyle.hint("Connection to Docker failed. Is docker or Docker Desktop running?") # hint for user+ Log.log(type="hint", content="Connection to Docker failed. Is docker or Docker Desktop running?")PrintStyle.error(err)+ Log.log(type="error", content=err)time.sleep(5) # try again in 5 secondselse: raisereturn self.client@@ -35,8 +38,11 @@ class DockerContainerManager:self.container.stop()self.container.remove()print(f"Stopped and removed the container: {self.container.id}")+ Log.log(type="info", content=f"Stopped and removed the container: {self.container.id}")except Exception as e:print(f"Failed to stop and remove the container: {e}")+ Log.log(type="error", content=f"Failed to stop and remove the container: {e}")+def start_container(self) -> None:if not self.client: self.client = self.init_docker()@@ -49,6 +55,8 @@ class DockerContainerManager:if existing_container:if existing_container.status != 'running':print(f"Starting existing container: {self.name} for safe code execution...")+ Log.log(type="info", content=f"Starting existing container: {self.name} for safe code execution...")+existing_container.start()self.container = existing_containertime.sleep(2) # this helps to get SSH ready@@ -58,13 +66,16 @@ 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...")+ Log.log(type="info", content=f"Initializing docker container {self.name} for safe code execution...")+self.container = self.client.containers.run(self.image,detach=True,- ports=self.ports,+ ports=self.ports, # type: ignorename=self.name,- volumes=self.volumes,- )+ volumes=self.volumes, # type: ignore+ )atexit.register(self.cleanup_container)print(f"Started container with ID: {self.container.id}")+ Log.log(type="info", content=f"Started container with ID: {self.container.id}")time.sleep(5) # this helps to get SSH readydiff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.pyindex 86289cc1b..fd1559a3d 100644--- a/python/helpers/extract_tools.py+++ b/python/helpers/extract_tools.py@@ -39,7 +39,6 @@ def extract_json_string(content):# Return the matched JSON stringreturn match.group(0)else:- print("No JSON content found.")return ""def fix_json_string(json_string):diff --git a/python/helpers/knowledge_import.py b/python/helpers/knowledge_import.pynew file mode 100644index 000000000..2d08ca8ef--- /dev/null+++ b/python/helpers/knowledge_import.py@@ -0,0 +1,87 @@+import glob+import os+import hashlib+import json+from typing import Any, Dict, Literal, TypedDict+from langchain_community.document_loaders import (+ CSVLoader, JSONLoader, PyPDFLoader, TextLoader, UnstructuredHTMLLoader,+ UnstructuredMarkdownLoader+)+from python.helpers import files+from python.helpers.log import Log++text_loader_kwargs = {'autodetect_encoding': True}+++class KnowledgeImport(TypedDict):+ file: str+ checksum: str+ ids: list[str]+ state: Literal["changed", "original", "removed"]+ documents: list[Any]+++def calculate_checksum(file_path: str) -> str:+ hasher = hashlib.md5()+ with open(file_path, 'rb') as f:+ buf = f.read()+ hasher.update(buf)+ return hasher.hexdigest()++def load_knowledge(knowledge_dir: str, index: Dict[str, KnowledgeImport]) -> Dict[str, KnowledgeImport]:+ knowledge_dir = files.get_abs_path(knowledge_dir)+++ # Mapping file extensions to corresponding loader classes+ file_types_loaders = {+ 'txt': TextLoader,+ 'pdf': PyPDFLoader,+ 'csv': CSVLoader,+ 'html': UnstructuredHTMLLoader,+ 'json': JSONLoader,+ 'md': UnstructuredMarkdownLoader+ }++ cnt_files = 0+ cnt_docs = 0++ # Fetch all files in the directory with specified extensions+ kn_files = glob.glob(knowledge_dir + '/**/*', recursive=True)+ if kn_files:+ print(f"Found {len(kn_files)} knowledge files in {knowledge_dir}, processing...")+ Log.log(type="info", content=f"Found {len(kn_files)} knowledge files in {knowledge_dir}, processing...")++ for file_path in kn_files:+ ext = file_path.split('.')[-1].lower()+ if ext in file_types_loaders:+ checksum = calculate_checksum(file_path)+ file_key = os.path.relpath(file_path, knowledge_dir)++ # Load existing data from the index or create a new entry+ file_data = index.get(file_key, {})++ if file_data.get('checksum') == checksum:+ file_data['state'] = 'original'+ else:+ file_data['state'] = 'changed'++ if file_data['state'] == 'changed':+ file_data['checksum'] = checksum+ loader_cls = file_types_loaders[ext]+ loader = loader_cls(file_path, **(text_loader_kwargs if ext in ['txt', 'csv', 'html', 'md'] else {}))+ file_data['documents'] = loader.load_and_split()+ cnt_files += 1+ cnt_docs += len(file_data['documents'])+ # print(f"Imported {len(file_data['documents'])} documents from {file_path}")++ # Update the index+ index[file_key] = file_data # type: ignore++ # loop index where state is not set and mark it as removed+ for file_key, file_data in index.items():+ if not file_data.get('state', ''):+ index[file_key]['state'] = 'removed'++ print(f"Processed {cnt_docs} documents from {cnt_files} files.")+ Log.log(type="info", content=f"Processed {cnt_docs} documents from {cnt_files} files.")+ return indexdiff --git a/python/helpers/log.py b/python/helpers/log.pynew file mode 100644index 000000000..1b6149f91--- /dev/null+++ b/python/helpers/log.py@@ -0,0 +1,58 @@+from dataclasses import dataclass+from typing import Optional, Dict+import uuid++@dataclass+class LogItem:+ no: int+ type: str+ heading: str+ content: str+ kvps: Optional[Dict] = None+++class Log:++ guid = uuid.uuid4()+ version: int = 0+ last_updated: int = 0+ logs: list = []++ def __init__(self, type: str="placeholder", heading: str="", content: str="", kvps: dict|None = None):+ self.item = Log.log(type, heading, content, kvps) # create placeholder log item that will be updated++ def update(self, type: Optional[str] = None, heading: str|None = None, content: str|None = None, kvps: dict|None = None):+ Log.edit(self.item.no, type=type, heading=heading, content=content, kvps=kvps)++ @staticmethod+ def reset():+ Log.guid = uuid.uuid4()+ Log.version = 0+ Log.last_updated = 0+ Log.logs = []++ @staticmethod+ def log(type: str, heading: str|None = None, content: str|None = None, kvps: dict|None = None):+ item = LogItem(len(Log.logs), type, heading or "", content or "", kvps)+ Log.logs.append(item)+ Log.version += 1+ Log.last_updated = item.no+ return item++ @staticmethod+ def edit(no: int, type: Optional[str] = None, heading: str|None = None, content: str|None = None, kvps: dict|None = None):+ if 0 <= no < len(Log.logs):+ item = Log.logs[no]+ if type is not None:+ item.type = type+ if heading is not None:+ item.heading = heading+ if content is not None:+ item.content = content+ if kvps is not None:+ item.kvps = kvps++ Log.version += 1+ Log.last_updated = no+ else:+ raise IndexError("Log item number out of range")diff --git a/python/helpers/messages.py b/python/helpers/messages.pyindex e91d41a74..fceccb132 100644--- a/python/helpers/messages.py+++ b/python/helpers/messages.py@@ -1,12 +1,12 @@-from . import files+# from . import files--def truncate_text(output, threshold=1000):+def truncate_text(agent, output, threshold=1000):if len(output) <= threshold:return output# Adjust the file path as needed- placeholder = files.read_file("./prompts/fw.msg_truncated.md", removed_chars=(len(output) - threshold))+ placeholder = agent.read_prompt("fw.msg_truncated.md", removed_chars=(len(output) - threshold))+ # placeholder = files.read_file("./prompts/default/fw.msg_truncated.md", removed_chars=(len(output) - threshold))start_len = (threshold - len(placeholder)) // 2end_len = threshold - len(placeholder) - start_lendiff --git a/python/helpers/rate_limiter.py b/python/helpers/rate_limiter.pyindex cfe75b367..ee5fb6896 100644--- a/python/helpers/rate_limiter.py+++ b/python/helpers/rate_limiter.py@@ -3,6 +3,7 @@ from collections import dequefrom dataclasses import dataclassfrom typing import List, Tuplefrom .print_style import PrintStyle+from .log import Log@dataclassclass CallRecord:@@ -48,6 +49,7 @@ class RateLimiter:wait_time = oldest_record.timestamp + self.window_seconds - current_timeif wait_time > 0:PrintStyle(font_color="yellow", padding=True).print(f"Rate limit exceeded. Waiting for {wait_time:.2f} seconds due to: {', '.join(wait_reasons)}")+ Log.log("rate_limit","Rate limit exceeded",f"Rate limit exceeded. Waiting for {wait_time:.2f} seconds due to: {', '.join(wait_reasons)}")time.sleep(wait_time)current_time = time.time()@@ -62,17 +64,3 @@ class RateLimiter:if self.call_records:self.call_records[-1].output_tokens += output_token_countreturn self--# Example usage-rate_limiter = RateLimiter(max_calls=5, max_input_tokens=1000, max_output_tokens=2000)--def rate_limited_function(input_token_count: int, output_token_count: int):- # First, limit the call and input tokens (this may wait)- rate_limiter.limit_call_and_input(input_token_count)-- # Your function logic here- print(f"Function called with {input_token_count} input tokens")-- # After processing, set the output tokens (this doesn't wait)- rate_limiter.set_output_tokens(output_token_count)- print(f"Function completed with {output_token_count} output tokens")diff --git a/python/helpers/shell_ssh.py b/python/helpers/shell_ssh.pyindex 3879cba9e..0d4e9c177 100644--- a/python/helpers/shell_ssh.py+++ b/python/helpers/shell_ssh.py@@ -2,12 +2,13 @@ import paramikoimport timeimport refrom typing import Optional, Tuple+from python.helpers.log import Log+from python.helpers.strings import calculate_valid_match_lengthsclass SSHInteractiveSession:- end_comment = "# @@==>> SSHInteractiveSession End-of-Command <<==@@"-- ps1_label = "SSHInteractiveSession CLI>"+ # end_comment = "# @@==>> SSHInteractiveSession End-of-Command <<==@@"+ # ps1_label = "SSHInteractiveSession CLI>"def __init__(self, hostname: str, port: int, username: str, password: str):self.hostname = hostname@@ -18,6 +19,9 @@ class SSHInteractiveSession:self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())self.shell = Noneself.full_output = b''+ self.last_command = b''+ self.trimmed_command_length = 0 # Initialize trimmed_command_length+def connect(self):# try 3 times with wait and then except@@ -27,15 +31,17 @@ class SSHInteractiveSession:self.client.connect(self.hostname, self.port, self.username, self.password)self.shell = self.client.invoke_shell(width=160,height=48)# self.shell.send(f'PS1="{SSHInteractiveSession.ps1_label}"'.encode())- return- # while True: # wait for end of initial output- # full, part = self.read_output()- # if full and not part: return- # time.sleep(0.1)+ # return+ while True: # wait for end of initial output+ full, part = self.read_output()+ if full and not part: return+ time.sleep(0.1)except Exception as e:errors += 1if errors < 3:print(f"SSH Connection attempt {errors}...")+ Log.log(type="info", content=f"SSH Connection attempt {errors}...")+time.sleep(5)else:raise e@@ -50,15 +56,32 @@ class SSHInteractiveSession:if not self.shell:raise Exception("Shell not connected")self.full_output = b""- self.shell.send((command + " \\\n" +SSHInteractiveSession.end_comment + "\n").encode())+ # if len(command) > 10: # if command is long, add end_comment to split output+ # command = (command + " \\\n" +SSHInteractiveSession.end_comment + "\n")+ # else:+ command = command + "\n"+ self.last_command = command.encode()+ self.trimmed_command_length = 0+ self.shell.send(self.last_command)def read_output(self) -> Tuple[str, str]:if not self.shell:raise Exception("Shell not connected")partial_output = b''+while self.shell.recv_ready():data = self.shell.recv(1024)++ # Trim own command from output+ if self.last_command and len(self.last_command) > self.trimmed_command_length:+ command_to_trim = self.last_command[self.trimmed_command_length:]++ trim_com, trim_out = calculate_valid_match_lengths(command_to_trim, data, deviation_threshold=8, deviation_reset=2, ignore_patterns=[rb'\x1b\[\?\d{4}[a-zA-Z]',rb'\r',rb'>'])+ if(trim_com > 0 and trim_out > 0):+ data = data[trim_out:]+ self.trimmed_command_length += trim_com+partial_output += dataself.full_output += datatime.sleep(0.1) # Prevent busy waiting@@ -66,15 +89,15 @@ class SSHInteractiveSession:# Decode once at the enddecoded_partial_output = partial_output.decode('utf-8', errors='replace')decoded_full_output = self.full_output.decode('utf-8', errors='replace')-+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")-+ # # 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_output@@ -85,5 +108,14 @@ class SSHInteractiveSession:# Replace '\r\n' with '\n'cleaned = cleaned.replace('\r\n', '\n')-- return cleaned\ No newline at end of file++ # Split the string by newline characters to process each segment separately+ lines = cleaned.split('\n')++ for i in range(len(lines)):+ # Handle carriage returns '\r' by splitting and taking the last part+ parts = [part for part in lines[i].split('\r') if part.strip()]+ if parts: lines[i] = parts[-1].rstrip() # Overwrite with the last part after the last '\r'++ return '\n'.join(lines)+diff --git a/python/helpers/strings.py b/python/helpers/strings.pynew file mode 100644index 000000000..ae03692a8--- /dev/null+++ b/python/helpers/strings.py@@ -0,0 +1,125 @@+# def calculate_valid_match_lengths(first: bytes | str, second: bytes | str, deviation_threshold: int = 5, deviation_reset: int = 5) -> tuple[int, int]:+# first_length = len(first)+# second_length = len(second)++# i, j = 0, 0+# deviations = 0+# matched_since_deviation = 0+# last_matched_i, last_matched_j = 0, 0 # Track the last matched index++# while i < first_length and j < second_length:+# if first[i] == second[j]:+# last_matched_i, last_matched_j = i + 1, j + 1 # Update last matched position+# i += 1+# j += 1+# matched_since_deviation += 1++# # Reset the deviation counter if we've matched enough characters since the last deviation+# if matched_since_deviation >= deviation_reset:+# deviations = 0+# matched_since_deviation = 0+# else:+# # Determine the look-ahead based on the remaining deviation threshold+# look_ahead = deviation_threshold - deviations++# # Look ahead to find the best match within the remaining deviation allowance+# best_match = None+# for k in range(1, look_ahead + 1):+# if i + k < first_length and first[i + k] == second[j]:+# best_match = ('i', k)+# break+# if j + k < second_length and first[i] == second[j + k]:+# best_match = ('j', k)+# break++# if best_match:+# if best_match[0] == 'i':+# i += best_match[1]+# elif best_match[0] == 'j':+# j += best_match[1]+# else:+# i += 1+# j += 1++# deviations += 1+# matched_since_deviation = 0++# if deviations > deviation_threshold:+# break++# # Return the last matched positions instead of the current indices+# return last_matched_i, last_matched_j++import re++def calculate_valid_match_lengths(first: bytes | str, second: bytes | str,+ deviation_threshold: int = 5,+ deviation_reset: int = 5,+ ignore_patterns: list[bytes|str] = []) -> tuple[int, int]:++ first_length = len(first)+ second_length = len(second)++ i, j = 0, 0+ deviations = 0+ matched_since_deviation = 0+ last_matched_i, last_matched_j = 0, 0 # Track the last matched index++ def skip_ignored_patterns(s, index):+ """Skip characters in `s` that match any pattern in `ignore_patterns` starting from `index`."""+ while index < len(s):+ for pattern in ignore_patterns:+ match = re.match(pattern, s[index:])+ if match:+ index += len(match.group(0))+ break+ else:+ break+ return index++ while i < first_length and j < second_length:+ # Skip ignored patterns+ i = skip_ignored_patterns(first, i)+ j = skip_ignored_patterns(second, j)++ if i < first_length and j < second_length and first[i] == second[j]:+ last_matched_i, last_matched_j = i + 1, j + 1 # Update last matched position+ i += 1+ j += 1+ matched_since_deviation += 1++ # Reset the deviation counter if we've matched enough characters since the last deviation+ if matched_since_deviation >= deviation_reset:+ deviations = 0+ matched_since_deviation = 0+ else:+ # Determine the look-ahead based on the remaining deviation threshold+ look_ahead = deviation_threshold - deviations++ # Look ahead to find the best match within the remaining deviation allowance+ best_match = None+ for k in range(1, look_ahead + 1):+ if i + k < first_length and first[i + k] == second[j]:+ best_match = ('i', k)+ break+ if j + k < second_length and first[i] == second[j + k]:+ best_match = ('j', k)+ break++ if best_match:+ if best_match[0] == 'i':+ i += best_match[1]+ elif best_match[0] == 'j':+ j += best_match[1]+ else:+ i += 1+ j += 1++ deviations += 1+ matched_since_deviation = 0++ if deviations > deviation_threshold:+ break++ # Return the last matched positions instead of the current indices+ return last_matched_i, last_matched_j\ No newline at end of filediff --git a/python/helpers/tool.py b/python/helpers/tool.pyindex 42b8c0df4..0481fbc24 100644--- a/python/helpers/tool.py+++ b/python/helpers/tool.py@@ -3,6 +3,7 @@ from typing import TypedDictfrom agent import Agentfrom python.helpers.print_style import PrintStylefrom python.helpers import files, messages+from python.helpers.log import Logclass Response:def __init__(self, message: str, break_loop: bool) -> None:@@ -24,6 +25,7 @@ class Tool:def before_execution(self, **kwargs):if self.agent.handle_intervention(): return # wait for intervention and handle it, if pausedPrintStyle(font_color="#1B4F72", padding=True, background_color="white", bold=True).print(f"{self.agent.agent_name}: Using tool '{self.name}':")+ self.log = Log(type="tool", 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)+": ")@@ -31,12 +33,13 @@ class Tool:PrintStyle().print()def after_execution(self, response: Response, **kwargs):- text = messages.truncate_text(response.message.strip(), self.agent.config.max_tool_response_length)- msg_response = files.read_file("./prompts/fw.tool_response.md", tool_name=self.name, tool_response=text)+ text = messages.truncate_text(self.agent, response.message.strip(), self.agent.config.max_tool_response_length)+ msg_response = self.agent.read_prompt("fw.tool_response.md", tool_name=self.name, tool_response=text)if self.agent.handle_intervention(): return # wait for intervention and handle it, if pausedself.agent.append_message(msg_response, human=True)PrintStyle(font_color="#1B4F72", background_color="white", padding=True, bold=True).print(f"{self.agent.agent_name}: Response from tool '{self.name}':")PrintStyle(font_color="#85C1E9").print(response.message)+ self.log.update(content=response.message)def nice_key(self, key:str):words = key.split('_')diff --git a/python/helpers/vdb.py b/python/helpers/vdb.pydeleted file mode 100644index 325112091..000000000--- a/python/helpers/vdb.py+++ /dev/null@@ -1,69 +0,0 @@-from langchain.storage import InMemoryByteStore, LocalFileStore-from langchain.embeddings import CacheBackedEmbeddings-from langchain_core.embeddings import Embeddings--from langchain_chroma import Chroma-import chromadb-from chromadb.config import Settings--from . import files-from langchain_core.documents import Document-import uuid---class VectorDB:-- def __init__(self, embeddings_model:Embeddings, in_memory=False, cache_dir="./cache"):- print("Initializing VectorDB...")- self.embeddings_model = embeddings_model-- db_cache = files.get_abs_path(cache_dir,"database")-- self.client =chromadb.PersistentClient(path=db_cache)- self.collection = self.client.create_collection("my_collection")- self.collection--- def search(self, query:str, results=2):- emb = self.embeddings_model.embed_query(query)- res = self.collection.query(query_embeddings=[emb],n_results=results)- best = res["documents"][0][0] # type: ignore-- # def delete_documents(self, query):- # score_limit = 1- # k = 2- # tot = 0- # while True:- # # Perform similarity search with score- # docs = self.db.similarity_search_with_score(query, k=k)-- # # Extract document IDs and filter based on score- # document_ids = [result[0].metadata["id"] for result in docs if result[1] < score_limit]-- # # Delete documents with IDs over the threshold score- # if document_ids:- # fnd = self.db.get(where={"id": {"$in": document_ids}})- # if fnd["ids"]: self.db.delete(ids=fnd["ids"])- # tot += len(fnd["ids"])-- # # If fewer than K document IDs, break the loop- # if len(document_ids) < k:- # break-- # return tot-- def insert(self, data:str):-- id = str(uuid.uuid4())- emb = self.embeddings_model.embed_documents([data])[0]-- self.collection.add(- ids=[id],- embeddings=[emb],- documents=[data],- )-- return id---diff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.pyindex 4af4c4089..0b712cdc6 100644--- a/python/helpers/vector_db.py+++ b/python/helpers/vector_db.py@@ -1,25 +1,33 @@from langchain.storage import InMemoryByteStore, LocalFileStorefrom langchain.embeddings import CacheBackedEmbeddings-from langchain_chroma import Chroma+# from langchain_chroma import Chroma+from langchain_community.vectorstores import FAISS+import faiss+from langchain_community.docstore.in_memory import InMemoryDocstore+import os, jsonfrom . import filesfrom langchain_core.documents import Documentimport uuid-+from python.helpers import knowledge_import+from python.helpers.log import Logclass VectorDB:- def __init__(self, embeddings_model, in_memory=False, cache_dir="./cache"):+ def __init__(self, embeddings_model, in_memory=False, memory_dir="./memory", knowledge_dir="./knowledge"):print("Initializing VectorDB...")+ Log.log("info", content="Initializing VectorDB...")+self.embeddings_model = embeddings_model- em_cache = files.get_abs_path(cache_dir,"embeddings")- db_cache = files.get_abs_path(cache_dir,"database")+ self.em_dir = files.get_abs_path(memory_dir,"embeddings")+ self.db_dir = files.get_abs_path(memory_dir,"database")+ self.kn_dir = files.get_abs_path(knowledge_dir) if knowledge_dir else ""if in_memory:self.store = InMemoryByteStore()else:- self.store = LocalFileStore(em_cache)+ self.store = LocalFileStore(self.em_dir)#here we setup the embeddings model with the chosen cache storage@@ -28,9 +36,63 @@ class VectorDB:self.store,namespace=getattr(embeddings_model, 'model', getattr(embeddings_model, 'model_name', "default")) )+ # self.db = Chroma(+ # embedding_function=self.embedder,+ # persist_directory=db_dir)- self.db = Chroma(embedding_function=self.embedder,persist_directory=db_cache)+ # if db folder exists and is not empty:+ if os.path.exists(self.db_dir) and len(os.listdir(self.db_dir)) > 0:+ self.db = FAISS.load_local(+ folder_path=self.db_dir,+ embeddings=self.embedder,+ allow_dangerous_deserialization=True+ )+ else:+ index = faiss.IndexFlatL2(len(self.embedder.embed_query("example text")))++ self.db = FAISS(+ embedding_function=self.embedder,+ index=index,+ docstore=InMemoryDocstore(),+ index_to_docstore_id={})++ #preload knowledge files+ if self.kn_dir:+ self.preload_knowledge(self.kn_dir, self.db_dir)+++ def preload_knowledge(self, kn_dir:str, db_dir:str):++ # Load the index file if it exists+ index_path = files.get_abs_path(db_dir, "knowledge_import.json")++ #make sure directory exists+ if 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:+ index = json.load(f)++ index = knowledge_import.load_knowledge(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++ # remove index where state="removed"+ index = {k: v for k, v in index.items() if v['state'] != 'removed'}++ # strip state and documents from index and save it+ for 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)def search_similarity(self, query, results=3):return self.db.similarity_search(query,results)@@ -64,7 +126,8 @@ class VectorDB:# 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) # persistreturn totdef delete_documents_by_ids(self, ids:list[str]):@@ -72,13 +135,20 @@ class VectorDB: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) #persistreturn len(ids)- def insert_document(self, data):+ def insert_text(self, text):id = str(uuid.uuid4())- self.db.add_documents(documents=[ Document(data, metadata={"id": id}) ], ids=[id])-+ self.db.add_documents(documents=[ Document(text, metadata={"id": id}) ], ids=[id])+ self.db.save_local(folder_path=self.db_dir) #persistreturn id-++ 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+ self.db.add_documents(documents=docs, ids=ids)+ self.db.save_local(folder_path=self.db_dir) #persist+ return idsdiff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex 8bf07a6bc..a6f4d75bb 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -11,6 +11,7 @@ from python.helpers.print_style import PrintStylefrom python.helpers.shell_local import LocalInteractiveSessionfrom python.helpers.shell_ssh import SSHInteractiveSessionfrom python.helpers.docker import DockerContainerManager+from python.helpers.log import Log@dataclassclass State:@@ -38,13 +39,23 @@ class CodeExecution(Tool):elif runtime == "output":response = self.get_terminal_output()else:- response = files.read_file("./prompts/fw.code_runtime_wrong.md", runtime=runtime)+ response = self.agent.read_prompt("fw.code_runtime_wrong.md", runtime=runtime)- if not response: response = files.read_file("./prompts/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)+ def before_execution(self, **kwargs):+ if self.agent.handle_intervention(): return # 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 = 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().print()+def after_execution(self, response, **kwargs):- msg_response = files.read_file("./prompts/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)self.agent.append_message(msg_response, human=True)def prepare_state(self):@@ -98,6 +109,7 @@ class CodeExecution(Tool):if partial_output:PrintStyle(font_color="#85C1E9").stream(partial_output)+ self.log.update(content=full_output)idle=0else:idle+=1diff --git a/python/tools/knowledge_tool.py b/python/tools/knowledge_tool.pyindex 22f64ec2c..97ce01dd5 100644--- a/python/tools/knowledge_tool.py+++ b/python/tools/knowledge_tool.py@@ -1,6 +1,5 @@import osfrom agent import Agent-from . import online_knowledge_toolfrom python.helpers import perplexity_searchfrom python.helpers import duckduckgo_search@@ -10,6 +9,7 @@ import concurrent.futuresfrom python.helpers.tool import Tool, Responsefrom python.helpers import filesfrom python.helpers.print_style import PrintStyle+from python.helpers.log import Logclass Knowledge(Tool):def execute(self, question="", **kwargs):@@ -21,6 +21,7 @@ class Knowledge(Tool):perplexity = executor.submit(perplexity_search.perplexity_search, question)else:PrintStyle.hint("No API key provided for Perplexity. Skipping Perplexity search.")+ Log(type="hint", content="No API key provided for Perplexity. Skipping Perplexity search.")perplexity = None@@ -46,7 +47,7 @@ class Knowledge(Tool):except Exception as e:memory_result = "Memory search failed: " + str(e)- msg = files.read_file("prompts/tool.knowledge.response.md",+ msg = self.agent.read_prompt("tool.knowledge.response.md",online_sources = ((perplexity_result + "\n\n") if perplexity else "") + str(duckduckgo_result),memory = memory_result )diff --git a/python/tools/memory_tool.py b/python/tools/memory_tool.pyindex c0c0f55ac..a2ef0477c 100644--- a/python/tools/memory_tool.py+++ b/python/tools/memory_tool.py@@ -6,9 +6,10 @@ import os, jsonfrom python.helpers.tool import Tool, Responsefrom python.helpers.print_style import PrintStylefrom chromadb.errors import InvalidDimensionException+from python.helpers.log import Log-# TODO multiple DBs at once-db: VectorDB | None= None+# databases based on subdirectories from agent config+dbs = {}class Memory(Tool):def execute(self,**kwargs):@@ -28,39 +29,48 @@ class Memory(Tool):except InvalidDimensionException as e:# hint about embedding change with existing databasePrintStyle.hint("If you changed your embedding model, you will need to remove contents of /memory directory.")+ Log(type="hint", content="If you changed your embedding model, you will need to remove contents of /memory directory.")raise# 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):- initialize(agent)+ db = get_db(agent)+ # docs = db.search_similarity(query,count) # type: ignoredocs = db.search_similarity_threshold(query,count,threshold) # type: ignore- if len(docs)==0: return files.read_file("./prompts/fw.memories_not_found.md", query=query)+ if len(docs)==0: return agent.read_prompt("fw.memories_not_found.md", query=query)else: return str(docs)def save(agent:Agent, text:str):- initialize(agent)- id = db.insert_document(text) # type: ignore- return files.read_file("./prompts/fw.memory_saved.md", memory_id=id)+ db = get_db(agent)+ id = db.insert_text(text) # type: ignore+ return agent.read_prompt("fw.memory_saved.md", memory_id=id)def delete(agent:Agent, ids_str:str):- initialize(agent)+ db = get_db(agent)ids = extract_guids(ids_str)deleted = db.delete_documents_by_ids(ids) # type: ignore- return files.read_file("./prompts/fw.memories_deleted.md", memory_count=deleted)+ return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)def forget(agent:Agent, query:str):- initialize(agent)+ db = get_db(agent)deleted = db.delete_documents_by_query(query) # type: ignore- return files.read_file("./prompts/fw.memories_deleted.md", memory_count=deleted)+ return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)-def initialize(agent:Agent):- global db- if not db:- dir = os.path.join("memory",agent.config.memory_subdir)- db = VectorDB(embeddings_model=agent.config.embeddings_model, in_memory=False, cache_dir=dir)+def get_db(agent: Agent):+ mem_dir = os.path.join("memory", agent.config.memory_subdir)+ kn_dir = os.path.join("knowledge", agent.config.knowledge_subdir)+ key = (mem_dir, kn_dir)+ if key not in dbs:+ db = VectorDB(embeddings_model=agent.config.embeddings_model, in_memory=False, memory_dir=mem_dir, knowledge_dir=kn_dir)+ dbs[key] = db+ else:+ db = dbs[key]++ return db+def extract_guids(text):pattern = r'\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\b'return re.findall(pattern, text)\ No newline at end of filediff --git a/python/tools/online_knowledge_tool.py b/python/tools/online_knowledge_tool.pydeleted file mode 100644index 852116e25..000000000--- a/python/tools/online_knowledge_tool.py+++ /dev/null@@ -1,13 +0,0 @@-from agent import Agent-from python.helpers import perplexity_search-from python.helpers.tool import Tool, Response--class OnlineKnowledge(Tool):- def execute(self,**kwargs):- return Response(- message=process_question(self.args["question"]),- break_loop=False,- )--def process_question(question):- return str(perplexity_search.perplexity_search(question))\ No newline at end of filediff --git a/python/tools/response.py b/python/tools/response.pyindex f4c1cdde9..f81fdcb2b 100644--- a/python/tools/response.py+++ b/python/tools/response.py@@ -6,6 +6,7 @@ from agent import Agentfrom python.helpers.tool import Tool, Responsefrom python.helpers import filesfrom python.helpers.print_style import PrintStyle+from python.helpers.log import Logclass ResponseTool(Tool):@@ -14,7 +15,8 @@ class ResponseTool(Tool):return Response(message=self.args["text"], break_loop=True)def before_execution(self, **kwargs):- pass # do not add anything to the history or output+ self.log = Log(type="response", heading=f"{self.agent.agent_name}: Responding:", content=self.args.get("text", ""))+def after_execution(self, response, **kwargs):pass # do not add anything to the history or output\ No newline at end of filediff --git a/python/tools/task_done.py b/python/tools/task_done.pyindex 5ee0a38cc..689e43a99 100644--- a/python/tools/task_done.py+++ b/python/tools/task_done.py@@ -6,6 +6,7 @@ from agent import Agentfrom python.helpers.tool import Tool, Responsefrom python.helpers import filesfrom python.helpers.print_style import PrintStyle+from python.helpers.log import Logclass TaskDone(Tool):@@ -14,7 +15,7 @@ class TaskDone(Tool):return Response(message=self.args["text"], break_loop=True)def before_execution(self, **kwargs):- pass # do not add anything to the history or output+ self.log = Log(type="response", heading=f"{self.agent.agent_name}: Task done:", content=self.args.get("text", ""))def after_execution(self, response, **kwargs):pass # do add anything to the history or output\ No newline at end of filediff --git a/python/tools/unknown.py b/python/tools/unknown.pyindex d0a1552de..d0dc3f504 100644--- a/python/tools/unknown.py+++ b/python/tools/unknown.py@@ -4,8 +4,8 @@ from python.helpers import filesclass Unknown(Tool):def execute(self, **kwargs):return Response(- message=files.read_file("prompts/fw.tool_not_found.md",+ message=self.agent.read_prompt("fw.tool_not_found.md",tool_name=self.name,- tools_prompt=files.read_file("prompts/agent.tools.md")),+ tools_prompt=self.agent.read_prompt("agent.tools.md")),break_loop=False)diff --git a/requirements.txt b/requirements.txtindex 4499dcde5..63d179149 100644--- a/requirements.txt+++ b/requirements.txt@@ -5,7 +5,6 @@ langchain-huggingface==0.0.3langchain-openai==0.1.15langchain-community==0.2.7langchain-anthropic==0.1.19-langchain-chroma==0.1.2langchain-google-genai==1.0.7webcolors==24.6.0sentence-transformers==3.0.1@@ -16,4 +15,8 @@ inputimeout==1.0.4newspaper3k==0.2.8beautifulsoup4==4.12.3lxml_html_clean==0.2.0-pynput==1.7.7\ No newline at end of file+pynput==1.7.7+pypdf==4.3.1+Flask[async]==3.0.3+Flask-BasicAuth==0.2.0+faiss-cpu==1.8.0.post1\ No newline at end of filediff --git a/main.py b/run_cli.pysimilarity index 52%rename from main.pyrename to run_cli.pyindex bd7a56b68..597019846 100644--- a/main.py+++ b/run_cli.py@@ -1,5 +1,4 @@-import threading, time, models, tts, stt, os, sys, argparse-from dotenv import load_dotenv+import threading, time, models, osfrom ansio import application_keypad, mouse_input, raw_inputfrom ansio.input import InputEvent, get_input_eventfrom agent import Agent, AgentConfig@@ -7,105 +6,42 @@ from python.helpers.print_style import PrintStylefrom python.helpers.files import read_filefrom python.helpers import filesimport python.helpers.timed_input as timed_input-from pynput import keyboard+from initialize import initialize-load_dotenv() # take environment variables from.env.input_lock = threading.Lock()os.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir-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_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_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)-- # utility model used for helper functions (cheaper, faster)- utility_llm = chat_llm # change if you want to use a different utility model-- # embedding model used for memory- embedding_llm = models.get_openai_embedding(model_name="text-embedding-3-small")- # embedding_llm = models.get_ollama_embedding(model_name="nomic-embed-text")- # embedding_llm = models.get_huggingface_embedding(model_name="sentence-transformers/all-MiniLM-L6-v2")-- # agent configuration- config = AgentConfig(- chat_model = chat_llm,- utility_model = utility_llm,- embeddings_model = embedding_llm,- # memory_subdir = "",- auto_memory_count = 0,- # auto_memory_skip = 2,- # rate_limit_seconds = 60,- # rate_limit_requests = 30,- # rate_limit_input_tokens = 0,- # rate_limit_output_tokens = 0,- # msgs_keep_max = 25,- # msgs_keep_start = 5,- # msgs_keep_end = 10,- # max_tool_response_length = 3000,- # response_timeout_seconds = 60,- code_exec_docker_enabled = True,- # code_exec_docker_name = "agent-zero-exe",- # code_exec_docker_image = "frdel/agent-zero-exe:latest",- # code_exec_docker_ports = { "22/tcp": 50022 }- # code_exec_docker_volumes = { files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"} }- code_exec_ssh_enabled = True,- # code_exec_ssh_addr = "localhost",- # code_exec_ssh_port = 50022,- # code_exec_ssh_user = "root",- # code_exec_ssh_pass = "toor",- # additional = {},- )-- # create the first agent- agent0 = Agent( number = 0, config = config )-- # start the chat loop- chat(agent0)--# Main conversation loopdef chat(agent:Agent):-+# start the conversation loopwhile True:# ask user for messagewith input_lock:timeout = agent.get_data("timeout") # how long the agent is willing to waitif not timeout: # if agent wants to wait for user input forever- PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('alt_gr' for mic, 'e' to leave):")+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('e' to leave):")import readline # this fixes arrow keys in terminal- listener = keyboard.Listener(on_press=on_press)- listener.start()user_input = input("> ")- listener.stop()PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")else: # otherwise wait for user input with a timeout- PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'alt_gr' for mic, 'e' to leave):")+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'e' to leave):")import readline # this fixes arrow keys in terminal- listener = keyboard.Listener(on_press=on_press)- listener.start()+ # user_input = timed_input("> ", timeout=timeout)user_input = timeout_input("> ", timeout=timeout)- listener.stop()+if not user_input:- user_input = read_file("prompts/fw.msg_timeout.md")+ user_input = agent.read_prompt("fw.msg_timeout.md")PrintStyle(font_color="white", padding=False).stream(f"{user_input}")else:user_input = user_input.strip()if user_input.lower()=="w": # the user needs more time- listener = keyboard.Listener(on_press=on_press)- listener.start()- user_input = input("> ")- listener.stop()- PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")+ user_input = input("> ").strip()+ PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")++# exit the conversation when the user types 'exit'if user_input.lower() == 'e': break@@ -115,8 +51,8 @@ def chat(agent:Agent):# print agent0 responsePrintStyle(font_color="white",background_color="#1D8348", bold=True, padding=True).print(f"{agent.agent_name}: reponse:")- PrintStyle(font_color="white").print(f"{assistant_response}")- if (os.getenv('EDGE_TTS_MODEL')) : tts.speech(assistant_response)+ PrintStyle(font_color="white").print(f"{assistant_response}")+# User intervention during agent streamingdef intervention():@@ -132,10 +68,6 @@ def intervention():if user_input: Agent.streaming_agent.intervention_message = user_input # set intervention message if non-emptyAgent.paused = False # continue agent streaming-def on_press(key):- if key == keyboard.Key.alt_gr:- # Lancer le speech to text- stt.record()# Capture keyboard input to trigger user interventiondef capture_keys():@@ -164,5 +96,6 @@ if __name__ == "__main__":# Start the key capture thread for user intervention during agent streamingthreading.Thread(target=capture_keys, daemon=True).start()- # Start the chat- initialize()\ No newline at end of file+ # initialize and start the chat+ agent0 = initialize()+ chat(agent0)\ No newline at end of filediff --git a/run_ui.py b/run_ui.pynew file mode 100644index 000000000..8d9f39481--- /dev/null+++ b/run_ui.py@@ -0,0 +1,192 @@+from functools import wraps+import os+from pathlib import Path+import threading+from flask import Flask, request, jsonify, Response+from flask_basicauth import BasicAuth+from agent import Agent+from initialize import initialize+from python.helpers.files import get_abs_path+from python.helpers.print_style import PrintStyle+from python.helpers.log import Log+from dotenv import load_dotenv++#global agent instance+agent0: Agent|None = None++#initialize the internal Flask server+app = Flask("app",static_folder=get_abs_path("./webui"),static_url_path="/")++# Set up basic authentication, name and password from .env variables+app.config['BASIC_AUTH_USERNAME'] = os.environ.get('BASIC_AUTH_USERNAME') or "admin" #default name+app.config['BASIC_AUTH_PASSWORD'] = os.environ.get('BASIC_AUTH_PASSWORD') or "admin" #default pass+basic_auth = BasicAuth(app)++# get global agent+def get_agent(reset: bool = False) -> Agent:+ global agent0+ if agent0 is None or reset:+ agent0 = initialize()+ return agent0++# Now you can use @requires_auth function decorator to require login on certain pages+def requires_auth(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ auth = request.authorization+ if not auth or not (auth.username == app.config['BASIC_AUTH_USERNAME'] and auth.password == app.config['BASIC_AUTH_PASSWORD']):+ return Response(+ 'Could not verify your access level for that URL.\n'+ 'You have to login with proper credentials', 401,+ {'WWW-Authenticate': 'Basic realm="Login Required"'})+ return await f(*args, **kwargs)+ return decorated++# handle default address, show demo html page from ./test_form.html[email protected]('/', methods=['GET'])+async def test_form():+ return Path(get_abs_path("./webui/index.html")).read_text()++# simple health check, just return OK to see the server is running[email protected]('/ok', methods=['GET','POST'])+async def health_check():+ return "OK"++# # secret page, requires authentication+# @app.route('/secret', methods=['GET'])+# @requires_auth+# async def secret_page():+# return Path("./secret_page.html").read_text()++# send message to agent[email protected]('/msg', methods=['POST'])+async def handle_message():+ try:++ #agent instance+ agent = get_agent()++ #data sent to the server+ input = request.get_json()+ text = input.get("text", "")++ # print to console and log+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message:")+ PrintStyle(font_color="white", padding=False).print(f"> {text}")+ Log.log(type="user", heading="User message", content=text)++ #pass the message to the agent+ threading.Thread(target=agent.communicate, args=(text,)).start()++ #data from this server+ response = {+ "ok": True,+ "message": "Message received.",+ }++ except Exception as e:+ response = {+ "ok": False,+ "message": str(e),+ }++ #respond with json+ return jsonify(response)++# pausing/unpausing the agent[email protected]('/pause', methods=['POST'])+async def pause():+ try:++ #data sent to the server+ input = request.get_json()+ paused = input.get("paused", False)++ Agent.paused = paused++ response = {+ "ok": True,+ "message": "Agent paused." if paused else "Agent unpaused.",+ "pause": paused+ }++ except Exception as e:+ response = {+ "ok": False,+ "message": str(e),+ }++ #respond with json+ return jsonify(response)++# restarting with new agent0[email protected]('/reset', methods=['POST'])+async def reset():+ try:++ agent = get_agent(reset=True)+ Log.reset()++ response = {+ "ok": True,+ "message": "Agent restarted.",+ }++ except Exception as e:+ response = {+ "ok": False,+ "message": str(e),+ }++ #respond with json+ return jsonify(response)++# Web UI polling[email protected]('/poll', methods=['POST'])+async def poll():+ try:++ #data sent to the server+ input = request.get_json()+ from_no = input.get("log_from", "")++ logs = Log.logs[int(from_no):]+ to = Log.last_updated #max(0, len(Log.logs)-1)++ #data from this server+ response = {+ "ok": True,+ "logs": logs,+ "log_to": to,+ "log_guid": Log.guid,+ "log_version": Log.version,+ "paused": Agent.paused+ }++ except Exception as e:+ response = {+ "ok": False,+ "message": str(e),+ }++ #respond with json+ return jsonify(response)++++#run the internal server+if __name__ == "__main__":++ load_dotenv()++ get_agent() #initialize++ # Suppress only request logs but keep the startup messages+ from werkzeug.serving import WSGIRequestHandler+ class NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):+ def log_request(self, code='-', size='-'):+ pass # Override to suppress request logging++ # run the server on port from .env+ port = int(os.environ.get("WEB_UI_PORT", 0)) or None+ app.run(request_handler=NoRequestLoggingWSGIRequestHandler,port=port)diff --git a/stt.py b/stt.pydeleted file mode 100644index 637e8ebd3..000000000--- a/stt.py+++ /dev/null@@ -1,83 +0,0 @@-import os, sys-from dotenv import load_dotenv-from groq import Groq-import pyaudio-import wave-from pynput.keyboard import Key, Controller-import numpy as np-from python.helpers.print_style import PrintStyle--load_dotenv() # take environment variables from.env.--# Utility function to get API keys from environment variables-def get_api_key(service):- return os.getenv(f"API_KEY_{service.upper()}") or os.getenv(f"{service.upper()}_API_KEY")--def record():- client = Groq(- api_key=get_api_key("groq")- )-- CHUNK = 1024- FORMAT = pyaudio.paInt16- CHANNELS = 2- RATE = 44100- WAVE_OUTPUT_FILENAME = "tmp_stt.wav"-- p = pyaudio.PyAudio()-- stream = p.open(format=FORMAT,- channels=CHANNELS,- rate=RATE,- input=True,- frames_per_buffer=CHUNK)-- PrintStyle(background_color="red", font_color="white", bold=True, padding=True).print("Recording")-- frames = []- silence_duration = 0- silence_threshold = 0.5 # seuil de silence (en secondes)- max_silence_duration = 3 # durée maximale de silence (en secondes)-- while True:- data = stream.read(CHUNK)- frames.append(data)-- # Analyse du signal audio pour détecter le silence- audio_data = np.frombuffer(data, dtype=np.int16)- audio_level = np.mean(np.abs(audio_data))- if audio_level < 100: # seuil de détection de silence (à ajuster)- silence_duration += CHUNK / RATE- if silence_duration > max_silence_duration:- break- else:- silence_duration = 0-- PrintStyle(background_color="Yellow", font_color="black", bold=True, padding=True).print("Translation")-- stream.stop_stream()- stream.close()- p.terminate()-- filename = os.path.dirname(__file__) + "/tmp/" + WAVE_OUTPUT_FILENAME-- wf = wave.open(filename, 'wb')- wf.setnchannels(CHANNELS)- wf.setsampwidth(p.get_sample_size(FORMAT))- wf.setframerate(RATE)- wf.writeframes(b''.join(frames))- wf.close()-- with open(filename, "rb") as file:- transcription = client.audio.transcriptions.create(- file=(filename, file.read()),- model="whisper-large-v3",- prompt="Human talking to personnal ia agent", # Optional- response_format="json", # Optional- language="fr", # Optional- temperature=0.0 # Optional- )- keyboard = Controller()- PrintStyle(background_color="green", font_color="white", bold=True, padding=True).print("Done")- keyboard.type(transcription.text)- keyboard.press(Key.enter)\ No newline at end of filediff --git a/test.py b/test.pynew file mode 100644index 000000000..d1fab12ba--- /dev/null+++ b/test.py@@ -0,0 +1,12 @@+from python.helpers.dirty_json import DirtyJson+++json_string = """+{"key1": "value1",+ "key2": "value2",+ "key3": "value3"+}+"""++json = DirtyJson.parse_string(json_string)+print(json)\ No newline at end of filediff --git a/tmp/.gitkeep b/tmp/.gitkeepnew file mode 100644index 000000000..e69de29bbdiff --git a/tmp/tmp_stt.wav b/tmp/tmp_stt.wavdeleted file mode 100644index 3688fc568..000000000Binary files a/tmp/tmp_stt.wav and /dev/null differdiff --git a/tts.py b/tts.pydeleted file mode 100644index 3320f7e0c..000000000--- a/tts.py+++ /dev/null@@ -1,21 +0,0 @@-from gradio_client import Client-import os-from dotenv import load_dotenv-os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"-import pygame.mixer--load_dotenv() # take environment variables from.env.-client = Client("innoai/Edge-TTS-Text-to-Speech")-pygame.mixer.init()--def speech(text):- speech = client.predict(- text=text,- voice=os.getenv('EDGE_TTS_MODEL'),- rate=20,- pitch=0,- api_name="/predict"- )- pygame.mixer.music.load(speech[0]) # chargement de la musique- pygame.mixer.music.play() # la musique est jouée-\ No newline at end of filediff --git a/webui/index.css b/webui/index.cssnew file mode 100644index 000000000..fde78f239--- /dev/null+++ b/webui/index.css@@ -0,0 +1,325 @@+body,+html {+ margin: 0;+ padding: 0;+ height: 100%;+ font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;+ background-color: #1e1e1e;+ color: #e0e0e0;+}+.container {+ display: flex;+ height: 100%;+}+.panel {+ height: 100%;+ overflow: auto;+}+#left-panel {+ width: 300px;+ background-color: #2d2d2d;+ border-right: 0.0625em solid #444; /* 1px */+ padding: 1.25em; /* 20px */+ box-sizing: border-box;+ flex-shrink: 0; /* Ensure the left panel does not shrink */+}+#right-panel {+ flex-grow: 1;+ display: flex;+ flex-direction: column;+}+.splitter {+ width: 0.625em; /* 10px */+ background-color: #444;+ cursor: col-resize;+ transition: background-color 0.3s;+ flex-shrink: 0; /* Prevents the splitter from shrinking */+}+.splitter:hover {+ background-color: #666;+}+h2,+h3 {+ margin: 1.25em 0 0.625em; /* 20px 0 10px */+ color: #bb86fc;+}+h4 {+ margin: 0.2em;+}+#chat-history {+ flex-grow: 1;+ overflow-y: auto;+ padding: 1.25em; /* 20px */+ display: flex;+ flex-direction: column;+}+.message-container {+ margin-bottom: 0.5em;+ animation: fadeIn 0.5s;+}+.message {+ padding: 0.625em 0.9375em; /* 10px 15px */+ border-radius: 1.125em; /* 18px */+}+@keyframes fadeIn {+ from {+ opacity: 0;+ transform: translateY(0.625em); /* 10px */+ }+ to {+ opacity: 1;+ transform: translateY(0);+ }+}+.user-container {+ align-self: flex-end;+ margin-top: 2em;+ margin-bottom: 1.5em;+}++.ai-container {+ align-self: flex-start;+}++.message-user {+ background-color: #4a4a4a;+ border-bottom-right-radius: 0.3125em; /* 5px */+}++.message-ai {+ border-bottom-left-radius: 0.3125em; /* 5px */+}++.message-fw{+ margin-left: 2em;+ border-radius: 1.125em; /* 18px */+ border-top-left-radius: 0.3125em; /* 5px */+}++.message-default {+ background-color: #002d3b;+}++.message-agent {+ margin-top: 2em;+ background-color: #003444;+}++.message-agent-response {+ background-color: #1f5327;+}++.message-agent-delegation {+ background-color: #1f534a;+}++.message-tool {+ background-color: #001b4b;+}++.message-code-exe {+ background-color: #2f1a60;+}++.message-adhoc {+ background-color: #124850;+}++.message-info {+ background-color: #2d2d2d;+}++.message-warning {+ background-color: #826100;+}++.message-error {+ background-color: #5e1212;+}++.agent-start {+ font-size: 0.8em;+ color: #ffffff;+ margin-bottom: 0.3125em; /* 5px */+ opacity: 0.7;+}+.ai-info {+ font-size: 0.9em;+ margin-bottom: 0.625em; /* 10px */+ border-collapse: collapse;+ width: 100%;+}+.ai-info th,+.ai-info td {+ text-align: left;+ padding: 0.25em; /* 4px */+ border-bottom: 0.0625em solid rgba(255, 255, 255, 0.1); /* 1px */+}+.ai-info th {+ color: #bb86fc;+ width: 40%;+}+.message-actions {+ margin-top: 0.3125em; /* 5px */+ font-size: 0.8em;+ color: #ffffff;+}+.message-action {+ cursor: pointer;+ opacity: 0.7;+ transition: opacity 0.3s;+}+.message-action:hover {+ opacity: 1;+}+#input-section {+ display: flex;+ padding: 1.25em; /* 20px */+ background-color: #2d2d2d;+}+#chat-input {+ flex-grow: 1;+ resize: none;+ /* min-height: 3.125em; 50px */+ max-height: 9.375em; /* 150px */+ padding: 0.625em 0.9375em; /* 10px 15px */+ border: none;+ border-radius: 1.5625em; /* 25px */+ background-color: #3d3d3d;+ color: #e0e0e0;+ font-size: 1em; /* 16px */+ transition: background-color 0.3s;+}+#chat-input:focus {+ outline: none;+ background-color: #4d4d4d;+}+.chat-button {+ color: #1e1e1e;+ border: none;+ border-radius: 50%;+ width: 3.125em; /* 50px */+ height: 3.125em; /* 50px */+ margin-left: 0.625em; /* 10px */+ cursor: pointer;+ transition: background-color 0.3s, transform 0.1s;+}+.chat-button:active {+ transform: scale(0.95);+}++#send-button {+ background-color: #bb86fc;+}+#send-button:hover {+ background-color: #c397ff;+}++.pause-button{+ background-color: #3270e2;+}+.pause-button:hover{+ background-color: #3270e2;+}++.config-section {+ margin-bottom: 1.25em; /* 20px */+}+.config-button {+ background-color: #3700b3;+ color: #ffffff;+ border: none;+ padding: 0.5em 0.75em; /* 8px 12px */+ margin: 0.3125em; /* 5px */+ border-radius: 0.3125em; /* 5px */+ cursor: pointer;+ transition: background-color 0.3s;+}+.config-button:hover {+ background-color: #5c2bd1;+}+.config-list {+ list-style-type: none;+ padding: 0;+}+.config-list li {+ display: flex;+ justify-content: space-between;+ align-items: center;+ padding: 0.3125em 0; /* 5px 0 */+ border-bottom: 0.0625em solid #444; /* 1px */+}+.edit-button {+ background-color: transparent;+ color: #bb86fc;+ border: 0.0625em solid #bb86fc; /* 1px */+ padding: 0.125em 0.5em; /* 2px 8px */+ border-radius: 0.1875em; /* 3px */+ cursor: pointer;+ transition: background-color 0.3s;+}+.edit-button:hover {+ background-color: rgba(187, 134, 252, 0.2);+}+.switch {+ position: relative;+ display: inline-block;+ width: 3.125em; /* 50px */+ height: 1.5em; /* 24px */+}+.switch input {+ opacity: 0;+ width: 0;+ height: 0;+}+.slider {+ position: absolute;+ cursor: pointer;+ top: 0;+ left: 0;+ right: 0;+ bottom: 0;+ background-color: #4a4a4a;+ transition: 0.4s;+ border-radius: 1.5em; /* 24px */+}+.slider:before {+ position: absolute;+ content: "";+ height: 1em; /* 16px */+ width: 1em; /* 16px */+ left: 0.25em; /* 4px */+ bottom: 0.25em; /* 4px */+ background-color: white;+ transition: 0.4s;+ border-radius: 50%;+}+input:checked + .slider {+ background-color: #bb86fc;+}+input:checked + .slider:before {+ transform: translateX(1.625em); /* 26px */+}++.kvps-key {+ font-weight: bold;+}++.kvps-val {+ white-space: pre-wrap;+}++.msg-json{+ display:none;+}++.msg-thoughts{+}++.connected{+color: #6ec583;+}++.disconnected{+ color: #d87979;+}\ No newline at end of filediff --git a/webui/index.html b/webui/index.htmlnew file mode 100644index 000000000..4d23c1131--- /dev/null+++ b/webui/index.html@@ -0,0 +1,99 @@+<!DOCTYPE html>+<html lang="en">++<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>Agent Zero</title>+ <link rel="stylesheet" href="index.css">+ <script>+ window.safeCall = function(name,...args){+ if (window[name]) window[name](...args)+ }+ </script>+ <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>+ <script type="module" src="index.js"></script>++</head>++<body>+ <div class="container">+ <div id="left-panel" class="panel">+ <!-- <h2>Configuration</h2> -->++ <div class="config-section" id="status-section" x-data="{ connected: true }">+ <h3>Status</h3>+ <h4 class="connected" x-show="connected">✔ Connected</h4>+ <h4 class="disconnected" x-show="!connected">✖ Disconnected</h4>+ </div>++ <div class="config-section" x-data="{ showQuickActions: true }">+ <h3>Quick Actions</h3>+ <button class="config-button" id="resetChat" @click="resetChat()">New Chat</button>+ </div>+ <!--+ <div class="config-section">+ <h3>Model Settings</h3>+ <ul class="config-list">+ <li>+ <span>GPT-4</span>+ <button class="edit-button">Edit</button>+ </li>+ <li>+ <span>Temperature: 0.7</span>+ <button class="edit-button">Edit</button>+ </li>+ <li>+ <span>Max Tokens: 2048</span>+ <button class="edit-button">Edit</button>+ </li>+ </ul>+ </div>+ -->+ <div class="config-section">+ <h3>Preferences</h3>+ <ul class="config-list">+ <li x-data="{ autoScroll: true }">+ <span>Autoscroll</span>+ <label class="switch">+ <input type="checkbox" x-model="autoScroll" x-effect="window.safeCall('toggleAutoScroll',autoScroll)">+ <span class="slider"></span>+ </label>+ </li>++ <li x-data="{ showThoughts: true }">+ <span>Show thoughts</span>+ <label class="switch">+ <input type="checkbox" x-model="showThoughts" x-effect="window.safeCall('toggleThoughts',showThoughts)">+ <span class="slider"></span>+ </label>+ </li>++ <li x-data="{ showJson: false }">+ <span>Show JSON</span>+ <label class="switch">+ <input type="checkbox" x-model="showJson" x-effect="window.safeCall('toggleJson',showJson)">+ <span class="slider"></span>+ </label>+ </li>++ </ul>+ </div>+ </div>+ <div class="splitter" id="splitter"></div>+ <div id="right-panel" class="panel">+ <!--<h2>Chat</h2>-->+ <div id="chat-history">++ </div>+ <div id="input-section" x-data="{ paused: false }">+ <textarea id="chat-input" placeholder="Type your message here..." rows="1"></textarea>+ <button class="chat-button" id="send-button">➤</button>+ <button class="chat-button pause-button" id="pause-button" @click="pauseAgent(true)" x-show="!paused">⏸</button>+ <button class="chat-button pause-button" id="unpause-button" @click="pauseAgent(false)" x-show="paused">▶</button>+ </div>+ </div>+ </div>+</body>++</html>\ No newline at end of filediff --git a/webui/index.js b/webui/index.jsnew file mode 100644index 000000000..a312fc11b--- /dev/null+++ b/webui/index.js@@ -0,0 +1,192 @@+import * as msgs from "./messages.js"++const splitter = document.getElementById('splitter');+const leftPanel = document.getElementById('left-panel');+const container = document.querySelector('.container');+const chatInput = document.getElementById('chat-input');+const chatHistory = document.getElementById('chat-history');+const sendButton = document.getElementById('send-button');+const inputSection = document.getElementById('input-section');+const statusSection = document.getElementById('status-section');++let isResizing = false;+let autoScroll = true;+++splitter.addEventListener('mousedown', (e) => {+ isResizing = true;+ document.addEventListener('mousemove', resize);+ document.addEventListener('mouseup', stopResize);+});++function resize(e) {+ if (isResizing) {+ const newWidth = e.clientX - container.offsetLeft;+ leftPanel.style.width = `${newWidth}px`;+ }+}++function stopResize() {+ isResizing = false;+ document.removeEventListener('mousemove', resize);+}++async function sendMessage() {+ const message = chatInput.value.trim();+ if (message) {++ const response = await sendJsonData("/msg", { text: message });++ //setMessage('user', message);+ chatInput.value = '';+ adjustTextareaHeight();+ }+}++chatInput.addEventListener('keydown', (e) => {+ if (e.key === 'Enter' && !e.shiftKey) {+ e.preventDefault();+ sendMessage();+ }+});++sendButton.addEventListener('click', sendMessage);++function setMessage(id, type, heading, content, kvps = null) {+ // Search for the existing message container by id+ let messageContainer = document.getElementById(`message-${id}`);++ if (messageContainer) {+ // Clear the existing container's content if found+ messageContainer.innerHTML = '';+ } else {+ // Create a new container if not found+ const sender = type === 'user' ? 'user' : 'ai';+ messageContainer = document.createElement('div');+ messageContainer.id = `message-${id}`;+ messageContainer.classList.add('message-container', `${sender}-container`);+ }++ const handler = msgs.getHandler(type);+ handler(messageContainer, id, type, heading, content, kvps);++ // If the container was found, it was already in the DOM, no need to append again+ if (!document.getElementById(`message-${id}`)) {+ chatHistory.appendChild(messageContainer);+ }++ if(autoScroll) chatHistory.scrollTop = chatHistory.scrollHeight;+}+++function adjustTextareaHeight() {+ chatInput.style.height = 'auto';+ chatInput.style.height = (chatInput.scrollHeight) + 'px';+}++async function sendJsonData(url, data) {+ const response = await fetch(url, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json'+ },+ body: JSON.stringify(data)+ });++ if (!response.ok) {+ throw new Error('Network response was not ok');+ }++ const jsonResponse = await response.json();+ return jsonResponse;+}++let lastLog = 0;+let lastLogVersion = 0;+let lastLogGuid = ""++async function poll() {+ try{+ const response = await sendJsonData("/poll", { log_from: lastLog });+ // console.log(response)++ if (response.ok) {++ if (lastLogGuid != response.log_guid) {+ chatHistory.innerHTML = ""+ }++ if (lastLogVersion != response.log_version) {+ for (const log of response.logs) {+ setMessage(log.no, log.type, log.heading, log.content, log.kvps);+ }+ }++ //set ui model vars from backend+ const inputAD = Alpine.$data(inputSection);+ inputAD.paused = response.paused;+ const statusAD = Alpine.$data(statusSection);+ statusAD.connected = response.ok;++ lastLog = response.log_to;+ lastLogVersion = response.log_version;+ lastLogGuid = response.log_guid;+ }++ } catch (error) {+ console.error('Error:', error);+ const statusAD = Alpine.$data(statusSection);+ statusAD.connected = false;+ }+}++window.pauseAgent = async function (paused) {+ const resp = await sendJsonData("/pause", { paused: paused });+}++window.resetChat = async function () {+ const resp = await sendJsonData("/reset", {});+}++window.toggleAutoScroll = async function (_autoScroll) {+ autoScroll = _autoScroll;+}++window.toggleJson = async function (showJson) {+ // add display:none to .msg-json class definition+ toggleCssProperty('.msg-json', 'display', showJson ? 'block' : 'none');+}++window.toggleThoughts = async function (showThoughts) {+ // add display:none to .msg-json class definition+ toggleCssProperty('.msg-thoughts', 'display', showThoughts ? undefined : 'none');+}+++function toggleCssProperty(selector, property, value) {+ // Get the stylesheet that contains the class+ const styleSheets = document.styleSheets;++ // Iterate through all stylesheets to find the class+ for (let i = 0; i < styleSheets.length; i++) {+ const styleSheet = styleSheets[i];+ const rules = styleSheet.cssRules || styleSheet.rules;++ for (let j = 0; j < rules.length; j++) {+ const rule = rules[j];+ if (rule.selectorText == selector) {+ // Check if the property is already applied+ if (value===undefined) {+ rule.style.removeProperty(property); // Remove the property+ } else {+ rule.style.setProperty(property, value); // Add the property (you can customize the value)+ }+ return;+ }+ }+ }+}++chatInput.addEventListener('input', adjustTextareaHeight);++setInterval(poll, 250);\ No newline at end of filediff --git a/webui/messages.js b/webui/messages.jsnew file mode 100644index 000000000..b2607da1d--- /dev/null+++ b/webui/messages.js@@ -0,0 +1,275 @@++++export function getHandler(type) {+ switch (type) {+ case 'user':+ return drawMessageUser;+ case 'agent':+ return drawMessageAgent;+ case 'response':+ return drawMessageResponse;+ case 'tool':+ return drawMessageTool;+ case 'code_exe':+ return drawMessageCodeExe;+ case 'warning':+ return drawMessageWarning;+ case 'rate_limit':+ return drawMessageWarning;+ case 'error':+ return drawMessageError;+ case 'info':+ return drawMessageInfo;+ case 'adhoc':+ return drawMessageAdhoc;+ case 'hint':+ return drawMessageInfo;+ default:+ return drawMessageDefault;+ }+}++export function drawMessageDefault(messageContainer, id, type, heading, content, kvps = null) {++ // if (type !== 'user') {+ // const agentStart = document.createElement('div');+ // agentStart.classList.add('agent-start');+ // agentStart.textContent = 'Agent 0 starts a message...';+ // messageContainer.appendChild(agentStart);+ // }++ const messageDiv = document.createElement('div');+ messageDiv.classList.add('message', 'message-ai', 'message-default');++ 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-json");+ messageDiv.appendChild(textNode);++ messageContainer.appendChild(messageDiv);++ // if (type !== 'user') {+ // 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);+ // }++}++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++ if (kvps) {+ const 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);++}++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 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 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 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 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 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 drawMessageAdhoc(messageContainer, id, type, heading, content, kvps = null) {+ return drawMessageAgentPlain(['message-adhoc'], messageContainer, id, type, heading, content, kvps);+}++export function drawMessageInfo(messageContainer, id, type, heading, content, kvps = null) {+ return drawMessageAgentPlain(['message-info'], messageContainer, id, type, heading, content, kvps);+}++export function drawMessageWarning(messageContainer, id, type, heading, content, kvps = null) {+ return drawMessageAgentPlain(['message-warning'], messageContainer, id, type, heading, content, kvps);+}++export function drawMessageError(messageContainer, id, type, heading, content, kvps = null) {+ return drawMessageAgentPlain(['message-error'], messageContainer, id, type, heading, content, kvps);+}++function drawKvps(container, kvps) {+ if (kvps) {+ const table = document.createElement('table');+ table.classList.add('ai-info');+ for (let [key, value] of Object.entries(kvps)) {+ const row = table.insertRow();+ if (key == "thoughts") row.classList.add('msg-thoughts');++ const th = row.insertCell();+ th.textContent = convertToTitleCase(key);+ th.classList.add('kvps-key');++ const td = row.insertCell();+ const pre = document.createElement('pre');++ // if value is array, join it with new line+ if (Array.isArray(value)) value = value.join('\n');++ pre.textContent = value;+ pre.classList.add('kvps-val');+ td.appendChild(pre);+ }+ container.appendChild(table);+ }+}++function convertToTitleCase(str) {+ return str+ .replace(/_/g, ' ') // Replace underscores with spaces+ .toLowerCase() // Convert the entire string to lowercase+ .replace(/\b\w/g, function (match) {+ return match.toUpperCase(); // Capitalize the first letter of each word+ });+}\ No newline at end of file