Agent Zero AI framework
restart
frdel961dbc405af8
diff --git a/python/api/restart.py b/python/api/restart.pynew file mode 100644index 000000000..864e16968--- /dev/null+++ b/python/api/restart.py@@ -0,0 +1,9 @@+from python.helpers.api import ApiHandler+from flask import Request, Response++from python.helpers import process++class Restart(ApiHandler):+ async def process(self, input: dict, request: Request) -> dict | Response:+ process.reload()+ return Response(status=200)\ No newline at end of filediff --git a/python/helpers/api.py b/python/helpers/api.pyindex 87bf6720f..c7133eba3 100644--- a/python/helpers/api.py+++ b/python/helpers/api.py@@ -6,6 +6,9 @@ from agent import AgentContextfrom initialize import initializefrom python.helpers.print_style import PrintStylefrom python.helpers.errors import format_error+from werkzeug.serving import make_server++class ApiHandler:diff --git a/python/helpers/process.py b/python/helpers/process.pynew file mode 100644index 000000000..e30662d3b--- /dev/null+++ b/python/helpers/process.py@@ -0,0 +1,35 @@+import os+import sys+from python.helpers import runtime++_server = None++def set_server(server):+ global _server+ _server = server++def get_server(server):+ global _server+ return _server++def stop_server():+ global _server+ if _server:+ _server.shutdown()+ _server = None++def reload():+ stop_server()+ if runtime.is_dockerized():+ exit_process()+ else:+ restart_process()++def restart_process():+ print("Restarting process...")+ python = sys.executable+ os.execv(python, [python] + sys.argv)++def exit_process():+ print("Exiting process...")+ sys.exit(0)\ No newline at end of filediff --git a/run_ui.py b/run_ui.pyindex 117e0e774..73df32bb9 100644--- a/run_ui.py+++ b/run_ui.py@@ -5,7 +5,7 @@ from flask import Flask, request, Responsefrom flask_basicauth import BasicAuthfrom python.helpers import files, gitfrom python.helpers.files import get_abs_path-from python.helpers import persist_chat, runtime, dotenv+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@@ -51,20 +51,22 @@ async def serve_index():version_no=gitinfo["version"],version_time=gitinfo["commit_time"],)+def run():print("Initializing framework...")# Suppress only request logs but keep the startup messagesfrom werkzeug.serving import WSGIRequestHandler+ from werkzeug.serving import make_serverclass NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):def log_request(self, code="-", size="-"):pass # Override to suppress request logging# Get configuration from environment- port = runtime.get_arg("port") or int(os.environ.get("WEB_UI_PORT", 0)) or None- host = runtime.get_arg("host") or os.environ.get("WEB_UI_HOST") or None+ port = runtime.get_arg("port") or int(os.environ.get("WEB_UI_PORT", 0)) or 5000+ host = runtime.get_arg("host") or os.environ.get("WEB_UI_HOST") or "localhost"use_cloudflare = (runtime.get_arg("cloudflare_tunnel")or os.environ.get("USE_CLOUDFLARE", "false").lower() == "true"@@ -83,6 +85,8 @@ def run():# initialize contexts from persisted chatspersist_chat.load_tmp_chats()+ server = None+def register_api_handler(app, handler):name = handler.__module__.split(".")[-1]instance = handler(app, lock)@@ -101,14 +105,14 @@ def run():for handler in handlers:register_api_handler(app, handler)---try:+ server = make_server(host=host, port=port, app=app, request_handler=NoRequestLoggingWSGIRequestHandler, threaded=True)+ process.set_server(server)+ server.serve_forever()# Run Flask app- app.run(- request_handler=NoRequestLoggingWSGIRequestHandler, port=port, host=host- )+ # app.run(+ # request_handler=NoRequestLoggingWSGIRequestHandler, port=port, host=host+ # )finally:# Clean up tunnel if it was startedif tunnel:diff --git a/webui/index.css b/webui/index.cssindex b2778a191..e9a15a1bd 100644--- a/webui/index.css+++ b/webui/index.css@@ -780,7 +780,9 @@ font-size: var(--font-size-small)margin-bottom: var(--spacing-xs);padding: var(--spacing-sm) 0.75rem;text-wrap: nowrap;- width: 48%;+ width: calc(50% - var(--spacing-xs));+ float:left;+ margin-right: var(--spacing-xs);max-height: 2.3rem;-webkit-transition: all var(--transition-speed), transform 0.1s ease-in-out;transition: all var(--transition-speed), transform 0.1s ease-in-out;diff --git a/webui/index.html b/webui/index.htmlindex e62897f1d..fc46ab212 100644--- a/webui/index.html+++ b/webui/index.html@@ -20,10 +20,10 @@</script><script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"></script>- <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>+ <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>-<script src="https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ace.js"></script>-<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/ace.min.css" rel="stylesheet">+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ace.js"></script>+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/css/ace.min.css" rel="stylesheet"><!-- KaTeX CSS --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">@@ -73,6 +73,7 @@<button class="config-button" id="newChat" @click="newChat()">New Chat</button><button class="config-button" id="loadChats" @click="loadChats()">Load Chat</button><button class="config-button" id="loadChat" @click="saveChat()">Save Chat</button>+ <button class="config-button" id="restart" @click="restart()">Restart</button><button class="config-button" id="settings" @click="settingsModalProxy.openModal()"><svgxmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="-5.0 -17.0 110.0 135.0"fill="currentColor" width="24" height="24">@@ -83,6 +84,7 @@d="m50.301 66.5c-9 0-16.398-7.3008-16.398-16.398 0-9.1016 7.3008-16.398 16.398-16.398 9.1016 0 16.398 7.3008 16.398 16.398 0 9.0977-7.3984 16.398-16.398 16.398zm0-25.5c-5 0-9.1016 4.1016-9.1016 9.1016s4.1016 9.1016 9.1016 9.1016 9.1016-4.1016 9.1016-9.1016c-0.003906-5-4.1016-9.1016-9.1016-9.1016z"></path></svg>Settings</button>+</div><!-- Chats List --><div class="config-section" id="chats-section" x-data="{ contexts: [], selected: '' }"@@ -389,9 +391,14 @@</button><button class="text-button" id="nudges_window" @click="nudge()">- <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 58" fill="currentColor">- <path d="m11.97,16.32c-.46,0-.91-.25-1.15-.68-.9-1.63-1.36-3.34-1.36-5.1C9.45,4.73,14.18,0,20,0s10.55,4.73,10.55,10.55c0,.87-.13,1.75-.41,2.76-.19.7-.9,1.13-1.62.93-.7-.19-1.12-.92-.93-1.62.21-.79.31-1.44.31-2.07,0-4.36-3.55-7.91-7.91-7.91s-7.91,3.55-7.91,7.91c0,1.3.35,2.59,1.03,3.82.36.64.13,1.44-.51,1.79-.21.11-.42.17-.64.17Z" stroke-width="0.5" stroke="currentColor"/>- <path d="m34.5,58h-6.18c-3.17,0-6.15-1.23-8.39-3.47L1.16,35.75c-1.54-1.54-1.54-4.05,0-5.59,2.4-2.4,6.27-2.68,8.99-.64l4.58,3.44V10.55c0-2.91,2.36-5.27,5.27-5.27s5.27,2.36,5.27,5.27v8.62c.78-.45,1.68-.71,2.64-.71,2.3,0,4.26,1.48,4.98,3.53.84-.56,1.85-.89,2.93-.89,2.3,0,4.26,1.48,4.98,3.53.84-.56,1.85-.89,2.93-.89,2.91,0,5.27,2.36,5.27,5.27v14.5c0,8-6.51,14.5-14.5,14.5ZM6.03,30.79c-1.1,0-2.19.42-3.01,1.23-.51.51-.51,1.35,0,1.86l18.77,18.78c1.74,1.74,4.06,2.7,6.53,2.7h6.18c6.54,0,11.86-5.32,11.86-11.86v-14.5c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v1.32c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32v-3.95c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v3.95c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32v-6.59c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v6.59c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32V10.55c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v25.05c0,.5-.28.95-.73,1.18s-.98.18-1.38-.12l-6.69-5.02c-.75-.56-1.65-.84-2.54-.84Z" stroke-width="0.5" stroke="currentColor"/>+ <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 49 58"+ fill="currentColor">+ <path+ d="m11.97,16.32c-.46,0-.91-.25-1.15-.68-.9-1.63-1.36-3.34-1.36-5.1C9.45,4.73,14.18,0,20,0s10.55,4.73,10.55,10.55c0,.87-.13,1.75-.41,2.76-.19.7-.9,1.13-1.62.93-.7-.19-1.12-.92-.93-1.62.21-.79.31-1.44.31-2.07,0-4.36-3.55-7.91-7.91-7.91s-7.91,3.55-7.91,7.91c0,1.3.35,2.59,1.03,3.82.36.64.13,1.44-.51,1.79-.21.11-.42.17-.64.17Z"+ stroke-width="0.5" stroke="currentColor" />+ <path+ d="m34.5,58h-6.18c-3.17,0-6.15-1.23-8.39-3.47L1.16,35.75c-1.54-1.54-1.54-4.05,0-5.59,2.4-2.4,6.27-2.68,8.99-.64l4.58,3.44V10.55c0-2.91,2.36-5.27,5.27-5.27s5.27,2.36,5.27,5.27v8.62c.78-.45,1.68-.71,2.64-.71,2.3,0,4.26,1.48,4.98,3.53.84-.56,1.85-.89,2.93-.89,2.3,0,4.26,1.48,4.98,3.53.84-.56,1.85-.89,2.93-.89,2.91,0,5.27,2.36,5.27,5.27v14.5c0,8-6.51,14.5-14.5,14.5ZM6.03,30.79c-1.1,0-2.19.42-3.01,1.23-.51.51-.51,1.35,0,1.86l18.77,18.78c1.74,1.74,4.06,2.7,6.53,2.7h6.18c6.54,0,11.86-5.32,11.86-11.86v-14.5c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v1.32c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32v-3.95c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v3.95c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32v-6.59c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v6.59c0,.73-.59,1.32-1.32,1.32s-1.32-.59-1.32-1.32V10.55c0-1.45-1.18-2.64-2.64-2.64s-2.64,1.18-2.64,2.64v25.05c0,.5-.28.95-.73,1.18s-.98.18-1.38-.12l-6.69-5.02c-.75-.56-1.65-.84-2.54-.84Z"+ stroke-width="0.5" stroke="currentColor" /></svg><p>Nudge</p></button>diff --git a/webui/index.js b/webui/index.jsindex 9d5886d8d..22ea4c6cb 100644--- a/webui/index.js+++ b/webui/index.js@@ -497,6 +497,27 @@ window.nudge = async function () {}}+window.restart = async function () {+ try {+ const resp = await sendJsonData("/restart", {});+ } catch (e) {+ //error expected here+ toast("Restarting...", "info", 0)+ while (true) {+ try {+ // try health check until server is back up again+ const resp = await sendJsonData("/health", {});+ await new Promise(resolve => setTimeout(resolve, 250));+ toast("Restarted", "success", 2000)+ return;+ } catch (e) {+ continue;+ }+ }++ }+}+// Modify this partdocument.addEventListener('DOMContentLoaded', () => {const isDarkMode = localStorage.getItem('darkMode') !== 'false';@@ -658,7 +679,7 @@ function removeClassFromElement(element, className) {}-function toast(text, type = 'info') {+function toast(text, type = 'info', timeout = 5000) {const toast = document.getElementById('toast');// Update the toast content and type@@ -694,12 +715,14 @@ function toast(text, type = 'info') {};// Clear any existing timeout- clearTimeout(toast.timeoutId);+ if (toast.timeoutId)+ clearTimeout(toast.timeoutId);// Automatically close the toast after 10 seconds- toast.timeoutId = setTimeout(() => {- hideToast();- }, 10000);+ if (timeout)+ toast.timeoutId = setTimeout(() => {+ hideToast();+ }, timeout);}function hideToast() {