Skip to content

Agent Zero AI framework

Support webui JS/HTML extensions and plugin paths

frdel3bfdf91637b8
diff --git a/agent.py b/agent.py
index a0833fb13..183cf6d5f 100644
--- a/agent.py
+++ b/agent.py
@@ -491,6 +491,8 @@ class Agent:
if tools_result: # final response of message loop available
return tools_result # break the execution if the task is done
+ error_retries = 0 # reset retry counter on successful iteration
+
# exceptions inside message loop:
except InterventionException as e:
error_retries = 0 # reset retry counter on user intervention
@@ -515,7 +517,7 @@ class Agent:
"message_loop_end", loop_data=self.loop_data
)
- error_retries = 0 # reset retry counter on successful iteration
+
# exceptions outside message loop:
except InterventionException as e:
diff --git a/jsconfig.json b/jsconfig.json
index 91123b807..4132a955e 100644
--- a/jsconfig.json
+++ b/jsconfig.json
@@ -7,5 +7,5 @@
"/usr/plugins/*": ["usr/plugins/*"]
}
},
- "include": ["webui/**/*.js"]
+ "include": ["webui/**/*.js", "plugins/**/*.js", "usr/plugins/**/*.js"]
}
\ No newline at end of file
diff --git a/plugins/memory/extensions/backend/embedding_model_changed/_10_memory_reload.py b/plugins/memory/extensions/python/embedding_model_changed/_10_memory_reload.py
similarity index 100%
rename from plugins/memory/extensions/backend/embedding_model_changed/_10_memory_reload.py
rename to plugins/memory/extensions/python/embedding_model_changed/_10_memory_reload.py
diff --git a/plugins/memory/extensions/backend/message_loop_prompts_after/_50_recall_memories.py b/plugins/memory/extensions/python/message_loop_prompts_after/_50_recall_memories.py
similarity index 100%
rename from plugins/memory/extensions/backend/message_loop_prompts_after/_50_recall_memories.py
rename to plugins/memory/extensions/python/message_loop_prompts_after/_50_recall_memories.py
diff --git a/plugins/memory/extensions/backend/message_loop_prompts_after/_91_recall_wait.py b/plugins/memory/extensions/python/message_loop_prompts_after/_91_recall_wait.py
similarity index 100%
rename from plugins/memory/extensions/backend/message_loop_prompts_after/_91_recall_wait.py
rename to plugins/memory/extensions/python/message_loop_prompts_after/_91_recall_wait.py
diff --git a/plugins/memory/extensions/backend/monologue_end/_50_memorize_fragments.py b/plugins/memory/extensions/python/monologue_end/_50_memorize_fragments.py
similarity index 100%
rename from plugins/memory/extensions/backend/monologue_end/_50_memorize_fragments.py
rename to plugins/memory/extensions/python/monologue_end/_50_memorize_fragments.py
diff --git a/plugins/memory/extensions/backend/monologue_end/_51_memorize_solutions.py b/plugins/memory/extensions/python/monologue_end/_51_memorize_solutions.py
similarity index 100%
rename from plugins/memory/extensions/backend/monologue_end/_51_memorize_solutions.py
rename to plugins/memory/extensions/python/monologue_end/_51_memorize_solutions.py
diff --git a/plugins/memory/extensions/backend/monologue_start/_10_memory_init.py b/plugins/memory/extensions/python/monologue_start/_10_memory_init.py
similarity index 100%
rename from plugins/memory/extensions/backend/monologue_start/_10_memory_init.py
rename to plugins/memory/extensions/python/monologue_start/_10_memory_init.py
diff --git a/plugins/memory/extensions/backend/system_prompt/_20_behaviour_prompt.py b/plugins/memory/extensions/python/system_prompt/_20_behaviour_prompt.py
similarity index 100%
rename from plugins/memory/extensions/backend/system_prompt/_20_behaviour_prompt.py
rename to plugins/memory/extensions/python/system_prompt/_20_behaviour_prompt.py
diff --git a/plugins/memory/extensions/webui/set_messages_before_loop/testingext.js b/plugins/memory/extensions/webui/set_messages_before_loop/testingext.js
new file mode 100644
index 000000000..6de142e53
--- /dev/null
+++ b/plugins/memory/extensions/webui/set_messages_before_loop/testingext.js
@@ -0,0 +1,4 @@
+
+export default function extension(context){
+ console.log("set_messages_before_loop extension called - textingext.js - REMOVE ME", context);
+}
\ No newline at end of file
diff --git a/python/api/load_webui_extensions.py b/python/api/load_webui_extensions.py
index 7c7c44fa5..c993651e9 100644
--- a/python/api/load_webui_extensions.py
+++ b/python/api/load_webui_extensions.py
@@ -10,10 +10,11 @@ class LoadWebuiExtensions(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
extension_point = input.get("extension_point", [])
+ filters = input.get("filters", [])
if not extension_point:
return Response(status=400, response="Missing extension_point")
- exts = plugins.get_webui_extensions(extension_point)
+ exts = plugins.get_webui_extensions(extension_point, filters)
return {"extensions": exts or []}
diff --git a/python/helpers/extension.py b/python/helpers/extension.py
index f6d0d3c00..40665ec35 100644
--- a/python/helpers/extension.py
+++ b/python/helpers/extension.py
@@ -32,8 +32,8 @@ async def call_extensions(
# search for extension folders in all agent's paths
paths = subagents.get_paths(agent, "extensions", extension_point, default_root="python")
- # Add plugin backend extension paths (plugins/*/extensions/backend/{extension_point})
- plugin_paths = plugins.get_plugin_paths("extensions", "backend", extension_point)
+ # Add plugin backend extension paths (plugins/*/extensions/python/{extension_point})
+ plugin_paths = plugins.get_plugin_paths("extensions", "python", extension_point)
paths.extend(p for p in plugin_paths if p not in paths)
all_exts = [cls for path in paths for cls in _get_extensions(path)]
diff --git a/python/helpers/plugins.py b/python/helpers/plugins.py
index bb33f8998..aa3c3e339 100644
--- a/python/helpers/plugins.py
+++ b/python/helpers/plugins.py
@@ -74,21 +74,33 @@ def get_plugin_paths(*subpaths: str) -> List[str]:
return paths
-def get_webui_extensions(extension_point:str) -> List[Dict[str, Any]]:
+def get_webui_extensions(extension_point:str, filters:List[str]|None=None) -> List[Dict[str, Any]]:
entries: List[Dict[str, Any]] = []
+ effective_filters = filters or ["*"]
for plugin in list_plugins():
frontend_dir = plugin.path / "extensions" / "webui" / extension_point
if not frontend_dir.is_dir():
continue
- for html_file in sorted(frontend_dir.rglob("*.html"), key=lambda p: p.name):
+ matched_files: List[Path] = []
+ seen: set[str] = set()
+ for pattern in effective_filters:
+ for p in frontend_dir.rglob(pattern):
+ if not p.is_file():
+ continue
+ p_str = str(p)
+ if p_str in seen:
+ continue
+ seen.add(p_str)
+ matched_files.append(p)
+
+ for ext_file in sorted(matched_files, key=lambda p: p.name):
try:
- rel_path = html_file.relative_to(plugin.path).as_posix()
+ rel_path = files.deabsolute_path(str(ext_file))
entry: Dict[str, Any] = {
"plugin_id": plugin.id,
- "component_url": f"{plugin.path}/{rel_path}",
- "html": html_file.read_text(encoding="utf-8"),
+ "path": rel_path,
}
entries.append(entry)
except Exception:
- print_style.PrintStyle.error(f"Failed to load frontend extension file {html_file}")
+ print_style.PrintStyle.error(f"Failed to load frontend extension file {ext_file}")
return entries
diff --git a/webui/components/chat/input/chat-bar.html b/webui/components/chat/input/chat-bar.html
index 9ab9a9678..addcb19d5 100644
--- a/webui/components/chat/input/chat-bar.html
+++ b/webui/components/chat/input/chat-bar.html
@@ -14,7 +14,7 @@
<!-- Attachment Preview section -->
<div>
- <x-component path="/chat/attachments/inputPreview.html" />
+ <x-component path="chat/attachments/inputPreview.html" />
</div>
<x-component path="chat/input/chat-bar-input.html"></x-component>
diff --git a/webui/components/settings/agent/speech.html b/webui/components/settings/agent/speech.html
index 7f0192840..c775e1911 100644
--- a/webui/components/settings/agent/speech.html
+++ b/webui/components/settings/agent/speech.html
@@ -18,7 +18,7 @@
<div class="field-description">Select the microphone device to use for speech-to-text.</div>
</div>
<div class="field-control">
- <x-component path="/settings/speech/microphone.html"></x-component>
+ <x-component path="settings/speech/microphone.html"></x-component>
</div>
</div>
diff --git a/webui/index.js b/webui/index.js
index e117e3124..a25cc3b6d 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -77,7 +77,7 @@ export async function sendMessage() {
: "";
// Render user message with attachments
- setMessages([{ id: messageId, type: "user", heading, content: message, kvps: {
+ await setMessages([{ id: messageId, type: "user", heading, content: message, kvps: {
// attachments: attachmentsWithUrls, // skip here, let the backend properly log them
}}]);
@@ -211,8 +211,8 @@ async function updateUserTime() {
updateUserTime();
setInterval(updateUserTime, 1000);
-function setMessages(...params) {
- return msgs.setMessages(...params);
+async function setMessages(...params) {
+ return await msgs.setMessages(...params);
}
globalThis.loadKnowledge = async function () {
@@ -332,7 +332,7 @@ export async function applySnapshot(snapshot, options = {}) {
if (lastLogVersion != snapshot.log_version) {
updated = true;
- setMessages(snapshot.logs);
+ await setMessages(snapshot.logs);
afterMessagesUpdate(snapshot.logs);
}
diff --git a/webui/js/components.js b/webui/js/components.js
index 328ee55d0..282a7a9f4 100644
--- a/webui/js/components.js
+++ b/webui/js/components.js
@@ -30,8 +30,7 @@ export async function importComponent(path, targetElement) {
targetElement.innerHTML = '<div class="loading"></div>';
// full component url
- const trimmedPath = path.replace(/^\/+/, "");
- const componentUrl = trimmedPath.startsWith("components/") ? trimmedPath : "components/" + trimmedPath;
+ const componentUrl = path.startsWith("/") ? path : (path.startsWith("components/") ? path : "components/" + path);
// get html from cache or fetch it
let html;
diff --git a/webui/js/extensions.js b/webui/js/extensions.js
index 37a83f399..db73fe30c 100644
--- a/webui/js/extensions.js
+++ b/webui/js/extensions.js
@@ -1,12 +1,95 @@
import * as api from "./api.js";
+/**
+ * @typedef {Object} WebuiExtension
+ * @property {string} path
+ */
+
+
+
+/**
+ * @typedef {Object} LoadWebuiExtensionsResponse
+ * @property {WebuiExtension[]} extensions
+ */
+
+/**
+ * @typedef {Object} JsExtensionImport
+ * @property {string} path
+ * @property {{ default: (data: any) => (void|Promise<void>) }} module
+ */
+
+/** @type {Map<string, JsExtensionImport[]>} */
+const jsExtensionsCache = new Map();
+
+/** @type {Map<string, string>} */
+const htmlExtensionsCache = new Map();
+
+export function invalidateCache() {
+ jsExtensionsCache.clear();
+ htmlExtensionsCache.clear();
+}
+
+/**
+ * Call all JS extensions for a given extension point.
+ *
+ * @param {string} extensionPoint
+ * @param {any} data
+ * @returns {Promise<void>}
+ */
+export async function callJsExtensions(extensionPoint, data){
+ const extensions = jsExtensionsCache.get(extensionPoint) || await loadJsExtensions(extensionPoint);
+ for(const extension of extensions){
+ try{
+ await extension.module.default(data);
+ }catch(error){
+ console.error(`Error calling extension: ${extension.path}`, error);
+ }
+ }
+}
+
+/**
+ * Load JS extension modules for an extension point.
+ *
+ * @param {string} extensionPoint
+ * @returns {Promise<JsExtensionImport[]>}
+ */
+export async function loadJsExtensions(extensionPoint) {
+ try {
+ /** @type {LoadWebuiExtensionsResponse} */
+ const response = await api.callJsonApi(`/api/load_webui_extensions`, {
+ extension_point: extensionPoint,
+ filters: ["*.js", "*.mjs"],
+ });
+ /** @type {JsExtensionImport[]} */
+ const imports = await Promise.all(
+ response.extensions.map(async extension => ({
+ path: extension.path,
+ module: await import(normalizePath(extension.path))
+ }))
+ );
+ jsExtensionsCache.set(extensionPoint, imports);
+ return imports;
+ } catch (error) {
+ console.error("Error loading JS extensions:", error);
+ return [];
+ }
+}
+
// Load all x-component tags starting from root elements
-export async function loadExtensions(roots = [document.documentElement]) {
+/**
+ * Load and render all HTML extensions in the given DOM roots.
+ *
+ * @param {Element | Document | Array<Element | Document>} [roots]
+ * @returns {Promise<void>}
+ */
+export async function loadHtmlExtensions(roots = [document.documentElement]) {
try {
// Convert single root to array if needed
+ /** @type {Array<Element | Document>} */
const rootElements = Array.isArray(roots) ? roots : [roots];
// Find all top-level components and load them in parallel
+ /** @type {Element[]} */
const extensions = rootElements.flatMap((root) =>
Array.from(root.querySelectorAll("x-extension")),
);
@@ -20,41 +103,70 @@ export async function loadExtensions(roots = [document.documentElement]) {
console.error("x-extension missing id attribute:", extension);
return;
}
- await importExtensions(path, extension);
+ await importHtmlExtensions(path, /** @type {HTMLElement} */ (extension));
}),
);
} catch (error) {
- console.error("Error loading extensions:", error);
+ console.error("Error loading HTML extensions:", error);
}
}
// import all extensions for extension point via backend api
-export async function importExtensions(extensionPointId, targetElement) {
+/**
+ * Import all HTML extensions for an extension point and inject them as `<x-component>` tags.
+ *
+ * @param {string} extensionPoint
+ * @param {HTMLElement} targetElement
+ * @returns {Promise<void>}
+ */
+export async function importHtmlExtensions(extensionPoint, targetElement) {
try {
+ const cachedHtml = htmlExtensionsCache.get(extensionPoint);
+ if (cachedHtml != null) {
+ targetElement.innerHTML = cachedHtml;
+ return;
+ }
+
+ /** @type {LoadWebuiExtensionsResponse} */
const response = await api.callJsonApi(`/api/load_webui_extensions`, {
- extension_point: extensionPointId,
+ extension_point: extensionPoint,
+ filters: ["*.html", "*.htm", "*.xhtml"],
});
let combinedHTML = "";
for (const extension of response.extensions) {
- combinedHTML += extension.html.trim();
+ const path = normalizePath(extension.path);
+ combinedHTML += `<x-component path="${path}"></x-component>`;
}
+ htmlExtensionsCache.set(extensionPoint, combinedHTML);
targetElement.innerHTML = combinedHTML;
} catch (error) {
- console.error("Error importing extensions:", error);
+ console.error("Error importing HTML extensions:", error);
+ return [];
}
}
+/**
+ * @param {string} path
+ * @returns {string}
+ */
+function normalizePath(path) {
+ return path.startsWith("/") ? path : "/" + path;
+}
+
// Watch for DOM changes to dynamically load x-extensions
+/** @type {MutationCallback} */
const extensionObserver = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType === 1) {
// ELEMENT_NODE
// Check if this node or its descendants contain x-extension(s)
- if (node.matches?.("x-extension")) {
- importExtensions(node.getAttribute("id"), node);
+ const el = /** @type {Element} */ (node);
+ if (el.matches?.("x-extension")) {
+ const id = el.getAttribute("id");
+ if (id) importHtmlExtensions(id, /** @type {HTMLElement} */ (el));
} else if (node.querySelectorAll) {
- loadExtensions([node]);
+ loadHtmlExtensions([node]);
}
}
}
diff --git a/webui/js/messages.js b/webui/js/messages.js
index 85bf9c3a4..b910b5dce 100644
--- a/webui/js/messages.js
+++ b/webui/js/messages.js
@@ -12,6 +12,7 @@ import { store as stepDetailStore } from "/components/modals/process-step-detail
import { store as preferencesStore } from "/components/sidebar/bottom/preferences/preferences-store.js";
import { formatDuration } from "./time-utils.js";
import { Scroller } from "./scroller.js";
+import { callJsExtensions } from "/js/extensions.js";
// Delay before collapsing previous steps when a new step is added
const STEP_COLLAPSE_DELAY = {
@@ -72,39 +73,52 @@ export function getMessageHandler(type) {
// entrypoint called from poll/WS communication, this is how all messages are rendered and updated
// input is raw log format
-export function setMessages(messages) {
- // set _massRender flag for handlers to know how to behave
- const history = getChatHistoryEl();
- const historyEmpty = !history || history.childElementCount === 0;
- const isLargeAppend = !historyEmpty && messages.length > 10;
- const cutoff = isLargeAppend ? Math.max(0, messages.length - 2) : 0;
- const massRender = historyEmpty || isLargeAppend;
-
- const mainScroller = new Scroller(history, {
- smooth: !massRender,
- toleranceRem: 4,
- reapplyDelayMs: 1000,
- applyStabilization: true,
- });
+export async function setMessages(messages) {
+ const context = {
+ messages,
+ history: getChatHistoryEl(),
+ historyEmpty: false,
+ isLargeAppend: false,
+ cutoff: 0,
+ massRender: false,
+ scrollerOptions: {
+ smooth: true,
+ toleranceRem: 4,
+ reapplyDelayMs: 1000,
+ applyStabilization: true,
+ },
+ mainScroller: null,
+ results: [],
+ };
- const results = [];
+ context.historyEmpty = !context.history || context.history.childElementCount === 0;
+ context.isLargeAppend = !context.historyEmpty && context.messages.length > 10;
+ context.cutoff = context.isLargeAppend ? Math.max(0, context.messages.length - 2) : 0;
+ context.massRender = context.historyEmpty || context.isLargeAppend;
+ context.scrollerOptions.smooth = !context.massRender;
+
+ await callJsExtensions("set_messages_before_loop", context);
+
+ context.mainScroller = new Scroller(context.history, context.scrollerOptions);
// process messages
- for (let i = 0; i < messages.length; i++) {
- _massRender = historyEmpty || (isLargeAppend && i < cutoff);
- results.push(setMessage(messages[i]) || {});
+ for (let i = 0; i < context.messages.length; i++) {
+ _massRender = context.historyEmpty || (context.isLargeAppend && i < context.cutoff);
+ context.results.push(setMessage(context.messages[i]) || {});
}
+ await callJsExtensions("set_messages_after_loop", context);
+
// reset _massRender flag
_massRender = false;
- const shouldScroll = historyEmpty || !results[results.length - 1]?.dontScroll;
+ const shouldScroll = context.historyEmpty || !context.results[context.results.length - 1]?.dontScroll;
- if (shouldScroll) mainScroller.reApplyScroll();
+ if (shouldScroll) context.mainScroller.reApplyScroll();
if (_scrollOnNextProcessGroup === "scroll") {
requestAnimationFrame(() => {
- mainScroller.scrollToBottom();
+ context.mainScroller.scrollToBottom();
_scrollOnNextProcessGroup = null;
});
}
diff --git a/webui/js/plugins.js b/webui/js/plugins.js
deleted file mode 100644
index dff2e4d5e..000000000
--- a/webui/js/plugins.js
+++ /dev/null
@@ -1,47 +0,0 @@
-// Plugin frontend auto-injection.
-// Imported once in initFw.js. The backend resolves plugin components and their
-// injection targets (parsed from <meta name="plugin-target"> server-side).
-// This module simply creates <x-component> elements at the declared targets;
-// the standard components.js MutationObserver handles loading automatically.
-import { callJsonApi } from "/js/api.js";
-
-const injected = new Set();
-
-function tryInject(entry) {
- const key = `${entry.component_url}|${entry.target}`;
- if (injected.has(key)) return true;
- const host = document.querySelector(entry.target);
- if (!host) return false;
- injected.add(key);
- const el = document.createElement("x-component");
- el.setAttribute("path", `../${entry.component_url.replace(/^\/+/, "")}`);
- el.className = "plugin-slot-entry";
- host.appendChild(el);
- return true;
-}
-
-(async () => {
- let res;
- try {
- res = await callJsonApi("/plugins_resolve", {});
- } catch (e) {
- console.warn("Plugin resolve failed:", e);
- return;
- }
- if (!res.ok || !Array.isArray(res.data)) return;
-
- // Only auto-inject entries that declare a target; others are standalone (modals etc.)
- const pending = res.data.filter(e => e?.component_url && e?.target);
- const remaining = pending.filter(e => !tryInject(e));
-
- // Retry for targets that load after initial render (e.g. sidebar components)
- if (remaining.length > 0) {
- const obs = new MutationObserver(() => {
- for (let i = remaining.length - 1; i >= 0; i--) {
- if (tryInject(remaining[i])) remaining.splice(i, 1);
- }
- if (remaining.length === 0) obs.disconnect();
- });
- obs.observe(document.body, { childList: true, subtree: true });
- }
-})();