Agent Zero AI framework
Add frontend cache and integrate with extensions
frdel83d369e7a063
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex 7149278e9..3e9f25c2c 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -7,7 +7,6 @@ from python.helpers import files, rfc_exchange, projects, runtime, settingsfrom python.helpers.print_style import PrintStylefrom python.helpers.shell_local import LocalInteractiveSessionfrom python.helpers.shell_ssh import SSHInteractiveSession-from python.helpers.docker import DockerContainerManagerfrom python.helpers.strings import truncate_text as truncate_text_stringfrom python.helpers.messages import truncate_text as truncate_text_agentimport rediff --git a/webui/js/cache.js b/webui/js/cache.jsnew file mode 100644index 000000000..94bb87640--- /dev/null+++ b/webui/js/cache.js@@ -0,0 +1,125 @@+let enabledGlobal = true;++/** @type {Map<string, boolean>} */+const enabledAreas = new Map();++/** @type {Map<string, Map<string, any>>} */+const cache = new Map();++export function toggle_global(enabled) {+ enabledGlobal = !!enabled;+}++export function toggle_area(area, enabled) {+ enabledAreas.set(area, !!enabled);+}++export function add(area, key, data) {+ if (!isEnabled(area)) return;+ let areaCache = cache.get(area);+ if (!areaCache) {+ areaCache = new Map();+ cache.set(area, areaCache);+ }+ areaCache.set(key, data);+}++export function get(area, key, defaultValue = null) {+ if (!isEnabled(area)) return defaultValue;+ const areaCache = cache.get(area);+ if (!areaCache) return defaultValue;+ return areaCache.has(key) ? areaCache.get(key) : defaultValue;+}++export function remove(area, key) {+ if (!isEnabled(area)) return;+ const areaCache = cache.get(area);+ if (!areaCache) return;+ areaCache.delete(key);+}++export function clear(area) {+ if (hasGlob(area)) {+ const re = globToRegExp(area);+ for (const k of cache.keys()) {+ if (re.test(k)) cache.delete(k);+ }+ return;+ }+ cache.delete(area);+}++export function clear_all() {+ cache.clear();+}++function isEnabled(area) {+ if (!enabledGlobal) return false;+ const v = enabledAreas.get(area);+ return v === undefined ? true : v;+}++function hasGlob(pattern) {+ return /[\*\?\[]/.test(pattern);+}++function escapeRegExpChar(ch) {+ return /[\\^$.*+?()[\]{}|]/.test(ch) ? `\\${ch}` : ch;+}++function globToRegExp(glob) {+ let out = "^";+ for (let i = 0; i < glob.length; i++) {+ const ch = glob[i];++ if (ch === "*") {+ out += ".*";+ continue;+ }++ if (ch === "?") {+ out += ".";+ continue;+ }++ if (ch === "[") {+ const end = glob.indexOf("]", i + 1);+ if (end === -1) {+ out += "\\[";+ continue;+ }++ const content = glob.slice(i + 1, end);+ let cls = "";+ let j = 0;+ if (content[0] === "!" || content[0] === "^") {+ cls += "^";+ j++;+ }+ for (; j < content.length; j++) {+ const c = content[j];+ if (c === "\\") {+ cls += "\\\\";+ continue;+ }+ if (c === "]") {+ cls += "\\]";+ continue;+ }+ if (c === "-") {+ cls += "-";+ continue;+ }+ cls += escapeRegExpChar(c);+ }++ out += `[${cls}]`;+ i = end;+ continue;+ }++ out += escapeRegExpChar(ch);+ }+ out += "$";+ return new RegExp(out);+}diff --git a/webui/js/extensions.js b/webui/js/extensions.jsindex 9f75e2db0..1c190d8ec 100644--- a/webui/js/extensions.js+++ b/webui/js/extensions.js@@ -1,4 +1,5 @@import * as api from "./api.js";+import * as cache from "./cache.js";/*** @typedef {string} WebuiExtension@@ -14,29 +15,26 @@ import * as api from "./api.js";/*** @typedef {Object} JsExtensionImport* @property {string} path- * @property {{ default: (data: any) => (void|Promise<void>) }} module+ * @property {{ default: (...data: any[]) => (void|Promise<void>) }} module*/-/** @type {Map<string, JsExtensionImport[]>} */-const jsExtensionsCache = new Map();--/** @type {Map<string, string>} */-const htmlExtensionsCache = new Map();+const JS_CACHE_AREA = "frontend_extensions_js(extensions)(plugins)";+const HTML_CACHE_AREA = "frontend_extensions_html(extensions)(plugins)";export function invalidateCache() {- jsExtensionsCache.clear();- htmlExtensionsCache.clear();+ cache.clear(JS_CACHE_AREA);+ cache.clear(HTML_CACHE_AREA);}/*** Call all JS extensions for a given extension point.** @param {string} extensionPoint- * @param {any} data+ * @param {...any} data* @returns {Promise<void>}*/export async function callJsExtensions(extensionPoint, ...data){- const extensions = jsExtensionsCache.get(extensionPoint) || await loadJsExtensions(extensionPoint);+ const extensions = cache.get(JS_CACHE_AREA, extensionPoint, null) || await loadJsExtensions(extensionPoint);for(const extension of extensions){try{await extension.module.default(...data);@@ -54,6 +52,9 @@ export async function callJsExtensions(extensionPoint, ...data){*/export async function loadJsExtensions(extensionPoint) {try {+ const cached = cache.get(JS_CACHE_AREA, extensionPoint, null);+ if (cached != null) return cached;+/** @type {LoadWebuiExtensionsResponse} */const response = await api.callJsonApi(`/api/load_webui_extensions`, {extension_point: extensionPoint,@@ -66,7 +67,7 @@ export async function loadJsExtensions(extensionPoint) {module: await import(normalizePath(path))})));- jsExtensionsCache.set(extensionPoint, imports);+ cache.add(JS_CACHE_AREA, extensionPoint, imports);return imports;} catch (error) {console.error("Error loading JS extensions:", error);@@ -120,7 +121,7 @@ export async function loadHtmlExtensions(roots = [document.documentElement]) {*/export async function importHtmlExtensions(extensionPoint, targetElement) {try {- const cachedHtml = htmlExtensionsCache.get(extensionPoint);+ const cachedHtml = cache.get(HTML_CACHE_AREA, extensionPoint, null);if (cachedHtml != null) {targetElement.innerHTML = cachedHtml;return;@@ -136,11 +137,11 @@ export async function importHtmlExtensions(extensionPoint, targetElement) {const path = normalizePath(extension);combinedHTML += `<x-component path="${path}"></x-component>`;}- htmlExtensionsCache.set(extensionPoint, combinedHTML);+ cache.add(HTML_CACHE_AREA, extensionPoint, combinedHTML);targetElement.innerHTML = combinedHTML;} catch (error) {console.error("Error importing HTML extensions:", error);- return [];+ return;}}@@ -154,7 +155,7 @@ function normalizePath(path) {// Watch for DOM changes to dynamically load x-extensions/** @type {MutationCallback} */-const extensionObserver = new MutationObserver((mutations) => {+const extensionObserverCallback = (mutations) => {for (const mutation of mutations) {for (const node of mutation.addedNodes) {if (node.nodeType === 1) {@@ -164,11 +165,14 @@ const extensionObserver = new MutationObserver((mutations) => {if (el.matches?.("x-extension")) {const id = el.getAttribute("id");if (id) importHtmlExtensions(id, /** @type {HTMLElement} */ (el));- } else if (node.querySelectorAll) {- loadHtmlExtensions([node]);+ } else if (/** @type {any} */ (el)["querySelectorAll"]) {+ loadHtmlExtensions([el]);}}}}-});+};++/** @type {MutationObserver} */+const extensionObserver = new MutationObserver(extensionObserverCallback);extensionObserver.observe(document.body, { childList: true, subtree: true });diff --git a/webui/js/messages.js b/webui/js/messages.jsindex b4980410e..a4dbbb69f 100644--- a/webui/js/messages.js+++ b/webui/js/messages.js@@ -52,7 +52,7 @@ let _scrollOnNextProcessGroup = null;*//**- * @typedef {(args: MessageHandlerArgs & Record<string, any>) => MessageHandlerResult} MessageHandler+ * @typedef {(args: MessageHandlerArgs & Record<string, any>) => (MessageHandlerResult|Promise<MessageHandlerResult>)} MessageHandler*//**@@ -82,9 +82,9 @@ export function scrollOnNextProcessGroup() {* and may return a rich object `{ element, actionButtons?, ...additional }`.** @param {string} type- * @returns {MessageHandler}+ * @returns {Promise<MessageHandler>}*/-export function getMessageHandler(type) {+export async function getMessageHandler(type) {switch (type) {case "user":return drawMessageUser;@@ -117,10 +117,20 @@ export function getMessageHandler(type) {case "hint":return drawMessageHint;default:- return drawMessageDefault;+ return await getHandlerFromExtensions(type);+ }++ async function getHandlerFromExtensions(type){+ const extData = { type: type, handler: undefined }+ await callJsExtensions("getMessageHandler", extData);+ // return handler from extensions+ if(typeof extData.handler == "function") return extData.handler;+ //not set by extensions, return default+ return drawMessageDefault;}}+// entrypoint called from poll/WS communication, this is how all messages are rendered and updated// input is raw log formatexport async function setMessages(messages) {@@ -157,7 +167,7 @@ export async function setMessages(messages) {// process messagesfor (let i = 0; i < context.messages.length; i++) {_massRender = context.historyEmpty || (context.isLargeAppend && i < context.cutoff);- context.results.push(setMessage(context.messages[i]));+ context.results.push(await setMessage(context.messages[i]));}await callJsExtensions("set_messages_after_loop", context);@@ -181,9 +191,9 @@ export async function setMessages(messages) {// input is raw log format/*** @param {MessageHandlerArgs & Record<string, any>} param0- * @returns {SetMessageResult}+ * @returns {Promise<SetMessageResult>}*/-export function setMessage({+export async function setMessage({no,id,type,@@ -194,9 +204,9 @@ export function setMessage({agentno,...additional}) {- const handler = getMessageHandler(type);+ const handler = await getMessageHandler(type);// prefer log ID if set to match user message created on frontend with backend updates- const handlerResult = handler({+ const handlerResult = await handler({no,id: id || String(no) || "",type,