Agent Zero AI framework
Scheduler polishing 1
frdelb7127616504e
diff --git a/agent.py b/agent.pyindex 4bad52ec2..4a40523f5 100644--- a/agent.py+++ b/agent.py@@ -163,10 +163,10 @@ class AgentContext:tool_name="call_subordinate", tool_result=msg # type: ignore))- response = await agent.monologue()+ response = await agent.monologue() # type: ignoresuperior = agent.data.get(Agent.DATA_NAME_SUPERIOR, None)if superior:- response = await self._process_chain(superior, response, False)+ response = await self._process_chain(superior, response, False) # type: ignorereturn responseexcept Exception as e:agent.handle_critical_exception(e)@@ -313,7 +313,7 @@ class Agent:agent_response = await self.call_chat_model(prompt, callback=stream_callback- )+ ) # type: ignoreawait self.handle_intervention(agent_response)@@ -499,13 +499,9 @@ class Agent:system_message=message.system_message)- # remove empty attachments from template- if (- isinstance(content, dict)- and "attachments" in content- and not content["attachments"]- ):- del content["attachments"]+ # remove empty parts from template+ if isinstance(content, dict):+ content = {k: v for k, v in content.items() if v}# add to historymsg = self.hist_add_message(False, content=content) # type: ignorediff --git a/docker/run/Dockerfile b/docker/run/Dockerfileindex 4502b077c..8506db066 100644--- a/docker/run/Dockerfile+++ b/docker/run/Dockerfile@@ -43,8 +43,5 @@ EXPOSE 22 80RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh-# This is important or cron will not execute the file-RUN chmod 0644 /etc/cron.d/scheduler_tick-# initialize runtime and switch to supervisordCMD ["/exe/initialize.sh", "$BRANCH"]diff --git a/docker/run/DockerfileKali b/docker/run/DockerfileKaliindex c456ae4e2..c9e540dea 100644--- a/docker/run/DockerfileKali+++ b/docker/run/DockerfileKali@@ -44,8 +44,5 @@ EXPOSE 22 80RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh-# This is important or cron will not execute the file-RUN chmod 0644 /etc/cron.d/scheduler_tick-# initialize runtime and switch to supervisordCMD ["/exe/initialize.sh", "$BRANCH"]diff --git a/docker/run/fs/etc/cron.d/scheduler_tick b/docker/run/fs/etc/cron.d/scheduler_tickdeleted file mode 100644index 2d96643c5..000000000--- a/docker/run/fs/etc/cron.d/scheduler_tick+++ /dev/null@@ -1,5 +0,0 @@-# TaskScheduler CronJob--* * * * * root curl http://127.0.0.1:80/scheduler_tick--# ENDdiff --git a/docker/run/fs/ins/pre_install.sh b/docker/run/fs/ins/pre_install.shindex 44221cc42..a99c5dbe5 100644--- a/docker/run/fs/ins/pre_install.sh+++ b/docker/run/fs/ins/pre_install.sh@@ -1,5 +1,8 @@#!/bin/bash+# fix permissions for cron files+chmod 0644 /etc/cron.d/*+# Update and install necessary packagesapt-get update && apt-get install -y \python3 \diff --git a/python/api/poll.py b/python/api/poll.pyindex 4e9c1c411..e9d012b3e 100644--- a/python/api/poll.py+++ b/python/api/poll.py@@ -32,7 +32,7 @@ class Poll(ApiHandler):scheduler = TaskScheduler.get()# Always reload the scheduler on each poll to ensure we have the latest task state- await scheduler.reload()+ # await scheduler.reload() # TODO:SCHEDULER must optimize this# loop AgentContext._contexts and divide into contexts and tasksdiff --git a/python/extensions/message_loop_prompts/_60_include_current_datetime.py b/python/extensions/message_loop_prompts/_60_include_current_datetime.pyindex 53a47951e..306443653 100644--- a/python/extensions/message_loop_prompts/_60_include_current_datetime.py+++ b/python/extensions/message_loop_prompts/_60_include_current_datetime.py@@ -7,16 +7,17 @@ from python.helpers.localization import Localizationclass IncludeCurrentDatetime(Extension):async def execute(self, loop_data: LoopData = LoopData(), **kwargs):# get current datetime- current_datetime = Localization.get().utc_dt_to_localtime_str(datetime.now(timezone.utc), sep=" ", timespec="seconds")+ current_datetime = Localization.get().utc_dt_to_localtime_str(+ datetime.now(timezone.utc), sep=" ", timespec="seconds"+ )# remove timezone offset- current_datetime = current_datetime.split("+")[0]-- # remove old current datetime from loop data- if "current_datetime" in loop_data.extras_temporary:- del loop_data.extras_temporary["current_datetime"]+ if current_datetime and "+" in current_datetime:+ current_datetime = current_datetime.split("+")[0]# read prompt- datetime_prompt = self.agent.read_prompt("agent.system.datetime.md", date_time=current_datetime)+ datetime_prompt = self.agent.read_prompt(+ "agent.system.datetime.md", date_time=current_datetime+ )# add current datetime to the loop dataloop_data.extras_temporary["current_datetime"] = datetime_promptdiff --git a/python/extensions/system_prompt/_10_system_prompt.py b/python/extensions/system_prompt/_10_system_prompt.pyindex 3a8a1d949..f9fdcb14a 100644--- a/python/extensions/system_prompt/_10_system_prompt.py+++ b/python/extensions/system_prompt/_10_system_prompt.py@@ -15,26 +15,11 @@ class SystemPrompt(Extension):def get_main_prompt(agent: Agent):- return get_prompt("agent.system.main.md", agent)+ return agent.read_prompt("agent.system.main.md")def get_tools_prompt(agent: Agent):- prompt = get_prompt("agent.system.tools.md", agent)+ prompt = agent.read_prompt("agent.system.tools.md")if agent.config.chat_model.vision:- prompt += '\n' + get_prompt("agent.system.tools_vision.md", agent)- return prompt---def get_prompt(file: str, agent: Agent):- # variables for system prompts- # TODO: move variables to the end of chain- # variables in system prompt would break prompt caching, better to add them to the last message in conversation- # get current datetime- current_datetime = Localization.get().utc_dt_to_localtime_str(datetime.now(timezone.utc), sep=" ", timespec="seconds")- # remove timezone offset- current_datetime = current_datetime.split("+")[0]- vars = {- "date_time": current_datetime,- "agent_name": agent.agent_name,- }- return agent.read_prompt(file, **vars)+ prompt += '\n' + agent.read_prompt("agent.system.tools_vision.md")+ return prompt\ No newline at end of filediff --git a/python/helpers/job_loop.py b/python/helpers/job_loop.pynew file mode 100644index 000000000..9ef541989--- /dev/null+++ b/python/helpers/job_loop.py@@ -0,0 +1,20 @@+import asyncio+from python.helpers.task_scheduler import TaskScheduler+from python.helpers.print_style import PrintStyle+from python.helpers import errors+++async def run_loop():+ while True:+ try:+ await scheduler_tick()+ except Exception as e:+ PrintStyle().error(errors.format_error(e))+ await asyncio.sleep(60)+++async def scheduler_tick():+ # Get the task scheduler instance and print detailed debug info+ scheduler = TaskScheduler.get()+ # Run the scheduler tick+ await scheduler.tick()\ No newline at end of filediff --git a/python/tools/input.py b/python/tools/input.pyindex b23c9adee..d17282ca4 100644--- a/python/tools/input.py+++ b/python/tools/input.py@@ -12,7 +12,7 @@ class Input(Tool):# forward keyboard input to code execution toolargs = {"runtime": "terminal", "code": keyboard}- cot = CodeExecution(self.agent, "code_execution_tool", args, self.message)+ cot = CodeExecution(self.agent, "code_execution_tool", "", args, self.message)cot.log = self.logreturn await cot.execute(**args)diff --git a/run_ui.py b/run_ui.pyindex 6e8427b1c..2e549f6a9 100644--- a/run_ui.py+++ b/run_ui.py@@ -15,11 +15,13 @@ from python.helpers import persist_chat, runtime, dotenv, processfrom python.helpers.cloudflare_tunnel import CloudflareTunnelfrom python.helpers.extract_tools import load_classes_from_folderfrom python.helpers.api import ApiHandler+from python.helpers.job_loop import run_loopfrom python.helpers.print_style import PrintStylefrom python.helpers.task_scheduler import TaskScheduler+from python.helpers.defer import DeferredTask# Set the new timezone to 'UTC'-os.environ['TZ'] = 'UTC'+os.environ["TZ"] = "UTC"# Apply the timezone changetime.tzset()@@ -35,8 +37,10 @@ basic_auth = BasicAuth(app)def is_loopback_address(address):loopback_checker = {- socket.AF_INET: lambda x: struct.unpack('!I', socket.inet_aton(x))[0] >> (32 - 8) == 127,- socket.AF_INET6: lambda x: x == '::1'+ socket.AF_INET: lambda x: struct.unpack("!I", socket.inet_aton(x))[0]+ >> (32 - 8)+ == 127,+ socket.AF_INET6: lambda x: x == "::1",}address_type = "hostname"try:@@ -79,6 +83,7 @@ def requires_api_key(f):else:return Response("API key required", 401)return await f(*args, **kwargs)+return decorated@@ -93,6 +98,7 @@ def requires_loopback(f):{},)return await f(*args, **kwargs)+return decorated@@ -142,6 +148,10 @@ def run():from werkzeug.serving import WSGIRequestHandlerfrom werkzeug.serving import make_server+ PrintStyle().print("Starting job loop...")+ job_loop = DeferredTask().start_task(run_loop)++ PrintStyle().print("Starting server...")class NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):def log_request(self, code="-", size="-"):pass # Override to suppress request logging@@ -188,17 +198,23 @@ def run():instance = handler(app, lock)if handler.requires_loopback():+@requires_loopbackasync def handle_request():return await instance.handle_request(request=request)+elif handler.requires_auth():+@requires_authasync def handle_request():return await instance.handle_request(request=request)+elif handler.requires_api_key():+@requires_api_keyasync def handle_request():return await instance.handle_request(request=request)+else:# Fallback to requires_auth@requires_auth@@ -232,7 +248,8 @@ def run():nonlocal tunnel, server, printerwith lock:printer.print("Caught signal, stopping server...")- server.shutdown()+ if server:+ server.shutdown()process.stop_server()if tunnel:tunnel.stop()diff --git a/test_scheduler.py b/test_scheduler.pydeleted file mode 100644index e0cae14ad..000000000--- a/test_scheduler.py+++ /dev/null@@ -1,16 +0,0 @@-from python.helpers.task_scheduler import ScheduledTask, TaskSchedule, SchedulerTaskList, TaskState-import asyncio--slist = SchedulerTaskList.get()--print(slist.model_dump_json(indent=4))--for task in slist.tasks:- t = slist.get_task_by_uuid(task.uuid)- t.update(state=TaskState.DISABLED)- print("-" * 100)- print(t.model_dump_json(indent=4))--print("-" * 100)--print(slist.model_dump_json(indent=4))diff --git a/webui/css/settings.css b/webui/css/settings.cssindex 76f69b533..895ae951d 100644--- a/webui/css/settings.css+++ b/webui/css/settings.css@@ -329,21 +329,21 @@ nav ul li a img {.settings-tab.active {border-color: var(--color-border);- box-shadow:+ /* box-shadow:0 -4px 8px -2px var(--color-border),4px 0 8px -2px var(--color-border),- -4px 0 8px -2px var(--color-border);+ -4px 0 8px -2px var(--color-border); */font-weight: bold;background-color: var(--color-panel);}/* Light mode overrides */.light-mode .settings-tab.active {- color: var(--color-border);- box-shadow:+ /* color: var(--color-border); */+ /* box-shadow:0 -4px 8px -2px var(--color-border),4px 0 8px -2px var(--color-border),- -4px 0 8px -2px var(--color-border);+ -4px 0 8px -2px var(--color-border); */}.light-mode .settings-tab:not(.active) {diff --git a/webui/index.css b/webui/index.cssindex e812ec14f..f81d65ec0 100644--- a/webui/index.css+++ b/webui/index.css@@ -2259,7 +2259,7 @@ a:active {.light-mode .tab.active::after {background-color: var(--highlight-pink);- box-shadow: 0 0 8px var(--highlight-pink);+ /* box-shadow: 0 0 8px var(--highlight-pink); */}/* Tabs styling */.tabs-container {@@ -2300,10 +2300,10 @@ a:active {.tab.active {border-color: var(--color-border);- box-shadow:+ /* box-shadow:0 -4px 8px -2px var(--color-border),4px 0 8px -2px var(--color-border),- -4px 0 8px -2px var(--color-border);+ -4px 0 8px -2px var(--color-border); */font-weight: bold;background-color: var(--color-panel);}