Skip to content

Agent Zero AI framework

refactor global actions to module imports

Alessandrof5bea00516bd
diff --git a/webui/components/sidebar/chats/chats-store.js b/webui/components/sidebar/chats/chats-store.js
index 3feb3d103..72b09556a 100644
--- a/webui/components/sidebar/chats/chats-store.js
+++ b/webui/components/sidebar/chats/chats-store.js
@@ -1,5 +1,15 @@
import { createStore } from "/js/AlpineStore.js";
-import { sendJsonData } from "/index.js";
+import {
+ sendJsonData,
+ getContext,
+ setContext,
+ poll as triggerPoll,
+ updateAfterScroll,
+ toastFetchError,
+ toast,
+ justToast,
+ getConnectionStatus,
+} from "/index.js";
import { store as notificationStore } from "/components/notifications/notification-store.js";
const model = {
@@ -30,26 +40,20 @@ const model = {
// Select a chat
async selectChat(id) {
- const currentContext = globalThis.getContext?.();
+ const currentContext = getContext();
if (id === currentContext) return; // already selected
-
+
// Proceed with context selection
- if (globalThis.setContext) {
- globalThis.setContext(id);
- }
-
+ setContext(id);
+
// Update selection state (will also persist to localStorage)
this.setSelected(id);
-
+
// Trigger immediate poll
- if (globalThis.poll) {
- globalThis.poll();
- }
-
+ triggerPoll();
+
// Update scroll
- if (globalThis.updateAfterScroll) {
- globalThis.updateAfterScroll();
- }
+ updateAfterScroll();
},
// Delete a chat
@@ -81,14 +85,10 @@ const model = {
this.contexts = [...updatedContexts];
// Show success notification
- if (globalThis.justToast) {
- globalThis.justToast("Chat deleted successfully", "success", 1000, "chat-removal");
- }
+ justToast("Chat deleted successfully", "success", 1000, "chat-removal");
} catch (e) {
console.error("Error deleting chat:", e);
- if (globalThis.toastFetchError) {
- globalThis.toastFetchError("Error deleting chat", e);
- }
+ toastFetchError("Error deleting chat", e);
}
},
@@ -107,16 +107,14 @@ const model = {
await this.selectChat(alternateChat.id);
} else {
// If no other chats, create a new empty context
- if (globalThis.newChat) {
- await globalThis.newChat();
- }
+ await this.newChat();
}
},
// Reset current chat
async resetChat(ctxid = null) {
try {
- const context = ctxid || this.selected || globalThis.getContext?.();
+ const context = ctxid || this.selected || getContext();
await sendJsonData("/chat_reset", {
context
});
@@ -126,13 +124,9 @@ const model = {
globalThis.resetCounter = globalThis.resetCounter + 1;
}
- if (globalThis.updateAfterScroll) {
- globalThis.updateAfterScroll();
- }
+ updateAfterScroll();
} catch (e) {
- if (globalThis.toastFetchError) {
- globalThis.toastFetchError("Error resetting chat", e);
- }
+ toastFetchError("Error resetting chat", e);
}
},
@@ -160,9 +154,7 @@ const model = {
// // UX: scroll-to-top
// requestAnimationFrame(() => this._scrollChatsToTop());
} catch (e) {
- if (globalThis.toastFetchError) {
- globalThis.toastFetchError("Error creating new chat", e);
- }
+ toastFetchError("Error creating new chat", e);
}
},
@@ -180,37 +172,33 @@ const model = {
const response = await sendJsonData("/chat_load", { chats: fileContents });
if (!response) {
- if (globalThis.toast) globalThis.toast("No response returned.", "error");
+ toast("No response returned.", "error");
} else {
// Set context to first loaded chat
- if (globalThis.setContext && response.ctxids?.[0]) {
- globalThis.setContext(response.ctxids[0]);
+ if (response.ctxids?.[0]) {
+ setContext(response.ctxids[0]);
}
- if (globalThis.toast) globalThis.toast("Chats loaded.", "success");
+ toast("Chats loaded.", "success");
}
} catch (e) {
- if (globalThis.toastFetchError) {
- globalThis.toastFetchError("Error loading chats", e);
- }
+ toastFetchError("Error loading chats", e);
}
},
// Save current chat
async saveChat() {
try {
- const context = this.selected || globalThis.getContext?.();
+ const context = this.selected || getContext();
const response = await sendJsonData("/chat_export", { ctxid: context });
if (!response) {
- if (globalThis.toast) globalThis.toast("No response returned.", "error");
+ toast("No response returned.", "error");
} else {
this.downloadFile(response.ctxid + ".json", response.content);
- if (globalThis.toast) globalThis.toast("Chat file downloaded.", "success");
+ toast("Chat file downloaded.", "success");
}
} catch (e) {
- if (globalThis.toastFetchError) {
- globalThis.toastFetchError("Error saving chat", e);
- }
+ toastFetchError("Error saving chat", e);
}
},
@@ -285,7 +273,7 @@ const model = {
async restart() {
try {
// Check connection status
- const connectionStatus = globalThis.getConnectionStatus?.();
+ const connectionStatus = getConnectionStatus();
if (connectionStatus === false) {
await notificationStore.frontendError(
"Backend disconnected, cannot restart.",
diff --git a/webui/components/sidebar/tasks/tasks-store.js b/webui/components/sidebar/tasks/tasks-store.js
index 7aabe1705..f014207f0 100644
--- a/webui/components/sidebar/tasks/tasks-store.js
+++ b/webui/components/sidebar/tasks/tasks-store.js
@@ -1,4 +1,5 @@
import { createStore } from "/js/AlpineStore.js";
+import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
// Tasks sidebar store: tasks list and selected task id
const model = {
@@ -45,9 +46,7 @@ const model = {
// Action methods for task management
selectTask(taskId) {
this.setSelected(taskId);
- if (globalThis.selectChat) {
- globalThis.selectChat(taskId);
- }
+ chatsStore.selectChat(taskId);
},
openDetail(taskId) {
@@ -57,9 +56,7 @@ const model = {
},
reset(taskId) {
- if (globalThis.resetChat) {
- globalThis.resetChat(taskId);
- }
+ chatsStore.resetChat(taskId);
},
deleteTask(taskId) {
diff --git a/webui/components/welcome/welcome-screen.html b/webui/components/welcome/welcome-screen.html
index 9d3bcaa04..f262a8419 100644
--- a/webui/components/welcome/welcome-screen.html
+++ b/webui/components/welcome/welcome-screen.html
@@ -161,14 +161,14 @@
}
.welcome-action-card:hover {
- border-color: var(--color-accent-dark);
+ border-color: var(--color-primary);
background: var(--color-message-bg);
}
.welcome-action-icon {
font-size: 2rem;
margin-bottom: 0;
- color: var(--color-accent-dark);
+ color: var(--color-highlight-dark);
}
.welcome-action-description {
diff --git a/webui/components/welcome/welcome-store.js b/webui/components/welcome/welcome-store.js
index bc4cac2c1..cae5a4b18 100644
--- a/webui/components/welcome/welcome-store.js
+++ b/webui/components/welcome/welcome-store.js
@@ -1,4 +1,6 @@
import { createStore } from "/js/AlpineStore.js";
+import { getContext } from "/index.js";
+import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
const model = {
// State
@@ -16,7 +18,7 @@ const model = {
// Update visibility based on current context
updateVisibility() {
- const hasContext = !!(globalThis.getContext && globalThis.getContext());
+ const hasContext = !!getContext();
this.isVisible = !hasContext;
},
@@ -34,9 +36,7 @@ const model = {
executeAction(actionId) {
switch (actionId) {
case "new-chat":
- if (globalThis.newChat) {
- globalThis.newChat();
- }
+ chatsStore.newChat();
break;
case "settings":
// Open settings modal
diff --git a/webui/index.js b/webui/index.js
index 875a03431..83ed2d699 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -111,7 +111,7 @@ export async function sendMessage() {
}
globalThis.sendMessage = sendMessage;
-function toastFetchError(text, error) {
+export function toastFetchError(text, error) {
console.error(text, error);
// Use new frontend error notification system (async, but we don't need to wait)
const errorMessage = error?.message || error?.toString() || "Unknown error";
@@ -234,7 +234,7 @@ function generateGUID() {
});
}
-function getConnectionStatus() {
+export function getConnectionStatus() {
return chatTopStore.connected;
}
globalThis.getConnectionStatus = getConnectionStatus;
@@ -256,7 +256,7 @@ let lastLogVersion = 0;
let lastLogGuid = "";
let lastSpokenNo = 0;
-async function poll() {
+export async function poll() {
let updated = false;
try {
// Get timezone from navigator
@@ -448,22 +448,6 @@ globalThis.pauseAgent = async function (paused) {
await inputStore.pauseAgent(paused);
};
-globalThis.resetChat = async function (ctxid = null) {
- await chatsStore.resetChat(ctxid);
-};
-
-globalThis.newChat = async function () {
- await chatsStore.newChat();
-};
-
-globalThis.killChat = async function (id) {
- await chatsStore.killChat(id);
-};
-
-globalThis.selectChat = async function (id) {
- await chatsStore.selectChat(id);
-};
-
function generateShortId() {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -527,14 +511,6 @@ export const getChatBasedId = function (id) {
return context + "-" + globalThis.resetCounter + "-" + id;
};
-globalThis.loadChats = async function () {
- await chatsStore.loadChats();
-};
-
-globalThis.saveChat = async function () {
- await chatsStore.saveChat();
-};
-
function addClassToElement(element, className) {
element.classList.add(className);
}
@@ -543,12 +519,12 @@ function removeClassFromElement(element, className) {
element.classList.remove(className);
}
-function justToast(text, type = "info", timeout = 5000, group = "") {
+export function justToast(text, type = "info", timeout = 5000, group = "") {
notificationStore.addFrontendToastOnly(type, text, "", timeout / 1000, group);
}
globalThis.justToast = justToast;
-function toast(text, type = "info", timeout = 5000) {
+export function toast(text, type = "info", timeout = 5000) {
// Convert timeout from milliseconds to seconds for new notification system
const display_time = Math.max(timeout / 1000, 1); // Minimum 1 second
@@ -574,7 +550,7 @@ function scrollChanged(isAtBottom) {
preferencesStore.autoScroll = isAtBottom;
}
-function updateAfterScroll() {
+export function updateAfterScroll() {
// const toleranceEm = 1; // Tolerance in em units
// const tolerancePx = toleranceEm * parseFloat(getComputedStyle(document.documentElement).fontSize); // Convert em to pixels
const tolerancePx = 10;
@@ -637,18 +613,6 @@ document.addEventListener("DOMContentLoaded", function () {
chatHistory.addEventListener("scroll", updateAfterScroll);
}
- // Restore previously selected context (if any) before polling begins
- let storedContext = null;
- try {
- storedContext = localStorage.getItem("lastSelectedChat");
- } catch (_e) {
- storedContext = null;
- }
-
- if (storedContext && storedContext !== "null" && storedContext !== "") {
- setContext(storedContext);
- }
-
// Start polling for updates
startPolling();
});