Skip to content

Agent Zero AI framework

API separation

frdelc0947e30c757
diff --git a/docker/exe/fs/exe/node_eval.js b/docker/exe/fs/exe/node_eval.js
index aab45b11e..52b7888e9 100644
--- a/docker/exe/fs/exe/node_eval.js
+++ b/docker/exe/fs/exe/node_eval.js
@@ -4,20 +4,25 @@ const vm = require('vm');
const path = require('path');
const Module = require('module');
-// Enhance `require` to search CWD first, then globally
+ // Enhance `require` to search CWD and parent directories first, then globally
function customRequire(moduleName) {
- try {
- // Try resolving from CWD's node_modules
- const cwdPath = path.resolve(process.cwd(), 'node_modules', moduleName);
- return require(cwdPath);
- } catch (cwdErr) {
+ let currentDir = process.cwd();
+ const root = path.parse(currentDir).root;
+
+ do {
try {
- // Try resolving as a global module
- return require(moduleName);
- } catch (globalErr) {
- console.error(`Cannot find module: ${moduleName}`);
- throw globalErr;
+ const modulePath = path.join(currentDir, 'node_modules', moduleName);
+ return require(modulePath);
+ } catch (err) {
+ currentDir = path.dirname(currentDir);
}
+ } while (currentDir !== root);
+
+ try {
+ return require(moduleName);
+ } catch (globalErr) {
+ console.error(`Cannot find module: ${moduleName}`);
+ throw globalErr;
}
}
diff --git a/python/api/chat_export.py b/python/api/chat_export.py
new file mode 100644
index 000000000..0a3f94076
--- /dev/null
+++ b/python/api/chat_export.py
@@ -0,0 +1,19 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import persist_chat
+
+class ExportChat(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ ctxid = input.get("ctxid", "")
+ if not ctxid:
+ raise Exception("No context id provided")
+
+ context = self.get_context(ctxid)
+ content = persist_chat.export_json_chat(context)
+
+ return {
+ "message": "Chats exported.",
+ "ctxid": context.id,
+ "content": content,
+ }
\ No newline at end of file
diff --git a/python/api/chat_load.py b/python/api/chat_load.py
new file mode 100644
index 000000000..9d3e4b48f
--- /dev/null
+++ b/python/api/chat_load.py
@@ -0,0 +1,17 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import persist_chat
+
+class LoadChats(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ chats = input.get("chats", [])
+ if not chats:
+ raise Exception("No chats provided")
+
+ ctxids = persist_chat.load_json_chats(chats)
+
+ return {
+ "message": "Chats loaded.",
+ "ctxids": ctxids,
+ }
diff --git a/python/api/chat_remove.py b/python/api/chat_remove.py
new file mode 100644
index 000000000..92482849c
--- /dev/null
+++ b/python/api/chat_remove.py
@@ -0,0 +1,18 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from agent import AgentContext
+from python.helpers import persist_chat
+
+
+class RemoveChat(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ ctxid = input.get("context", "")
+
+ # context instance - get or create
+ AgentContext.remove(ctxid)
+ persist_chat.remove_chat(ctxid)
+
+ return {
+ "message": "Context removed.",
+ }
diff --git a/python/api/chat_reset.py b/python/api/chat_reset.py
new file mode 100644
index 000000000..5f2417127
--- /dev/null
+++ b/python/api/chat_reset.py
@@ -0,0 +1,17 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import persist_chat
+
+class Reset(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ ctxid = input.get("context", "")
+
+ # context instance - get or create
+ context = self.get_context(ctxid)
+ context.reset()
+ persist_chat.save_tmp_chat(context)
+
+ return {
+ "message": "Agent restarted.",
+ }
diff --git a/python/api/delete_work_dir_file.py b/python/api/delete_work_dir_file.py
new file mode 100644
index 000000000..40721161b
--- /dev/null
+++ b/python/api/delete_work_dir_file.py
@@ -0,0 +1,23 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+
+
+class DeleteWorkDirFile(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ file_path = input.get('path', '')
+ current_path = input.get('currentPath', '')
+
+ work_dir = files.get_abs_path("work_dir")
+ browser = FileBrowser(work_dir)
+
+ if browser.delete_file(file_path):
+ # Get updated file list
+ result = browser.get_files(current_path)
+ return {
+ "data": result
+ }
+ else:
+ raise Exception("File not found or could not be deleted")
\ No newline at end of file
diff --git a/python/api/download_work_dir_file.py b/python/api/download_work_dir_file.py
new file mode 100644
index 000000000..4e1c47274
--- /dev/null
+++ b/python/api/download_work_dir_file.py
@@ -0,0 +1,27 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response, send_file
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+import os
+
+
+
+
+class DownloadWorkDirFile(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ file_path = request.args.get('path', '')
+ if not file_path:
+ raise ValueError("No file path provided")
+
+ work_dir = files.get_abs_path("work_dir")
+ browser = FileBrowser(work_dir)
+
+ full_path = browser.get_file_path(file_path)
+ if full_path:
+ return send_file(
+ full_path,
+ as_attachment=True,
+ download_name=os.path.basename(file_path)
+ )
+ raise Exception("File not found")
\ No newline at end of file
diff --git a/python/api/get_work_dir_files.py b/python/api/get_work_dir_files.py
new file mode 100644
index 000000000..447b368bf
--- /dev/null
+++ b/python/api/get_work_dir_files.py
@@ -0,0 +1,15 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+
+
+class GetWorkDirFiles(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ current_path = request.args.get("path", "")
+ work_dir = files.get_abs_path("work_dir")
+ browser = FileBrowser(work_dir)
+ result = browser.get_files(current_path)
+
+ return {"data": result}
diff --git a/python/api/health.py b/python/api/health.py
new file mode 100644
index 000000000..9fe81f56b
--- /dev/null
+++ b/python/api/health.py
@@ -0,0 +1,10 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import git
+
+class HealthCheck(ApiHandler):
+
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ gitinfo = git.get_git_info()
+ return {"gitinfo": gitinfo}
diff --git a/python/api/import_knowledge.py b/python/api/import_knowledge.py
new file mode 100644
index 000000000..fd760355d
--- /dev/null
+++ b/python/api/import_knowledge.py
@@ -0,0 +1,26 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+import os
+from werkzeug.utils import secure_filename
+
+
+class ImportKnowledge(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ if "files[]" not in request.files:
+ raise Exception("No files part")
+
+ file_list = request.files.getlist("files[]")
+ KNOWLEDGE_FOLDER = files.get_abs_path("knowledge/custom/main")
+
+ saved_filenames = []
+
+ for file in file_list:
+ if file:
+ filename = secure_filename(file.filename) # type: ignore
+ file.save(os.path.join(KNOWLEDGE_FOLDER, filename))
+ saved_filenames.append(filename)
+
+ return {"message": "Knowledge Imported", "filenames": saved_filenames}
diff --git a/python/api/message.py b/python/api/message.py
new file mode 100644
index 000000000..b7a8e970b
--- /dev/null
+++ b/python/api/message.py
@@ -0,0 +1,87 @@
+from agent import AgentContext
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import files
+import os
+from werkzeug.utils import secure_filename
+from python.helpers.defer import DeferredTask
+from python.helpers.print_style import PrintStyle
+
+
+class Message(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ task, context = await self.communicate(input=input, request=request)
+ return await self.respond(task, context)
+
+ async def respond(self, task: DeferredTask, context: AgentContext):
+ result = await task.result() # type: ignore
+ return {
+ "message": result,
+ "context": context.id,
+ }
+
+ async def communicate(self, input: dict, request: Request):
+ # Handle both JSON and multipart/form-data
+ if request.content_type.startswith("multipart/form-data"):
+ text = request.form.get("text", "")
+ ctxid = request.form.get("context", "")
+ message_id = request.form.get("message_id", None)
+ attachments = request.files.getlist("attachments")
+ attachment_paths = []
+
+ upload_folder = files.get_abs_path("work_dir/uploads")
+
+ if attachments:
+ os.makedirs(upload_folder, exist_ok=True)
+ for attachment in attachments:
+ if attachment.filename is None:
+ continue
+ filename = secure_filename(attachment.filename)
+ save_path = files.get_abs_path(upload_folder, filename)
+ attachment.save(save_path)
+ attachment_paths.append(save_path)
+ else:
+ # Handle JSON request as before
+ input_data = request.get_json()
+ text = input_data.get("text", "")
+ ctxid = input_data.get("context", "")
+ message_id = input_data.get("message_id", None)
+ attachment_paths = []
+
+ # Now process the message
+ message = text
+
+ # Obtain agent context
+ context = self.get_context(ctxid)
+
+ # Store attachments in agent data
+ context.agent0.set_data("attachments", attachment_paths)
+
+ # Prepare attachment filenames for logging
+ attachment_filenames = (
+ [os.path.basename(path) for path in attachment_paths]
+ if attachment_paths
+ else []
+ )
+
+ # Print to console and log
+ PrintStyle(
+ background_color="#6C3483", font_color="white", bold=True, padding=True
+ ).print(f"User message:")
+ PrintStyle(font_color="white", padding=False).print(f"> {message}")
+ if attachment_filenames:
+ PrintStyle(font_color="white", padding=False).print("Attachments:")
+ for filename in attachment_filenames:
+ PrintStyle(font_color="white", padding=False).print(f"- {filename}")
+
+ # Log the message with message_id and attachments
+ context.log.log(
+ type="user",
+ heading="User message",
+ content=message,
+ kvps={"attachments": attachment_filenames},
+ id=message_id,
+ )
+
+ return context.communicate(message), context
\ No newline at end of file
diff --git a/python/api/message_async.py b/python/api/message_async.py
new file mode 100644
index 000000000..22d9192ef
--- /dev/null
+++ b/python/api/message_async.py
@@ -0,0 +1,17 @@
+from agent import AgentContext
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import files
+import os
+from werkzeug.utils import secure_filename
+from python.helpers.defer import DeferredTask
+from python.api.message import Message
+
+
+class MessageAsync(Message):
+ async def respond(self, task: DeferredTask, context: AgentContext):
+ return {
+ "message": "Message received.",
+ "context": context.id,
+ }
diff --git a/python/api/pause.py b/python/api/pause.py
new file mode 100644
index 000000000..95fea1b07
--- /dev/null
+++ b/python/api/pause.py
@@ -0,0 +1,19 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+
+class Pause(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ # input data
+ paused = input.get("paused", False)
+ ctxid = input.get("context", "")
+
+ # context instance - get or create
+ context = self.get_context(ctxid)
+
+ context.paused = paused
+
+ return {
+ "message": "Agent paused." if paused else "Agent unpaused.",
+ "pause": paused,
+ }
diff --git a/python/api/poll.py b/python/api/poll.py
new file mode 100644
index 000000000..174a02107
--- /dev/null
+++ b/python/api/poll.py
@@ -0,0 +1,39 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from agent import AgentContext
+
+class Poll(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ ctxid = input.get("context", None)
+ from_no = input.get("log_from", 0)
+
+ # context instance - get or create
+ context = self.get_context(ctxid)
+
+ logs = context.log.output(start=from_no)
+
+ # loop AgentContext._contexts
+ ctxs = []
+ for ctx in AgentContext._contexts.values():
+ ctxs.append(
+ {
+ "id": ctx.id,
+ "no": ctx.no,
+ "log_guid": ctx.log.guid,
+ "log_version": len(ctx.log.updates),
+ "log_length": len(ctx.log.logs),
+ "paused": ctx.paused,
+ }
+ )
+
+ # data from this server
+ return {
+ "context": context.id,
+ "contexts": ctxs,
+ "logs": logs,
+ "log_guid": context.log.guid,
+ "log_version": len(context.log.updates),
+ "log_progress": context.log.progress,
+ "paused": context.paused,
+ }
\ No newline at end of file
diff --git a/python/api/rfc.py b/python/api/rfc.py
new file mode 100644
index 000000000..149910ae7
--- /dev/null
+++ b/python/api/rfc.py
@@ -0,0 +1,9 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import runtime
+
+class RFC(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ result = await runtime.handle_rfc(input) # type: ignore
+ return result
diff --git a/python/api/settings_get.py b/python/api/settings_get.py
new file mode 100644
index 000000000..2ecdbef38
--- /dev/null
+++ b/python/api/settings_get.py
@@ -0,0 +1,9 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import settings
+
+class GetSettings(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ set = settings.convert_out(settings.get_settings())
+ return {"settings": set}
diff --git a/python/api/settings_set.py b/python/api/settings_set.py
new file mode 100644
index 000000000..93440876f
--- /dev/null
+++ b/python/api/settings_set.py
@@ -0,0 +1,11 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import settings
+
+
+class SetSettings(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ set = settings.convert_in(input)
+ set = settings.set_settings(set)
+ return {"settings": set}
diff --git a/python/api/transcribe.py b/python/api/transcribe.py
new file mode 100644
index 000000000..4c2ae3d5b
--- /dev/null
+++ b/python/api/transcribe.py
@@ -0,0 +1,10 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers import runtime, whisper
+
+class Transcribe(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ audio = input.get("audio")
+ result = await whisper.transcribe(audio) # type: ignore
+ return result
diff --git a/python/api/upload.py b/python/api/upload.py
new file mode 100644
index 000000000..8413122a7
--- /dev/null
+++ b/python/api/upload.py
@@ -0,0 +1,28 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+from werkzeug.utils import secure_filename
+
+
+class UploadFile(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ if "file" not in request.files:
+ raise Exception("No file part")
+
+ file_list = request.files.getlist("file") # Handle multiple files
+ saved_filenames = []
+
+ for file in file_list:
+ if file and self.allowed_file(file.filename): # Check file type
+ filename = secure_filename(file.filename) # type: ignore
+ file.save(files.get_abs_path("work_dir/upload", filename))
+ saved_filenames.append(filename)
+
+ return {"filenames": saved_filenames} # Return saved filenames
+
+
+ def allowed_file(self,filename):
+ ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "txt", "pdf", "csv", "html", "json", "md"}
+ return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
\ No newline at end of file
diff --git a/python/api/upload_work_dir_files.py b/python/api/upload_work_dir_files.py
new file mode 100644
index 000000000..32ed52493
--- /dev/null
+++ b/python/api/upload_work_dir_files.py
@@ -0,0 +1,34 @@
+from python.helpers.api import ApiHandler
+from flask import Request, Response, send_file
+
+from python.helpers.file_browser import FileBrowser
+from python.helpers import files
+import os
+
+
+
+
+class UploadWorkDirFiles(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ if "files[]" not in request.files:
+ raise Exception("No files uploaded")
+
+ current_path = request.form.get('path', '')
+ uploaded_files = request.files.getlist("files[]")
+
+ work_dir = files.get_abs_path("work_dir")
+ browser = FileBrowser(work_dir)
+
+ successful, failed = browser.save_files(uploaded_files, current_path)
+
+ if not successful and failed:
+ raise Exception("All uploads failed")
+
+ result = browser.get_files(current_path)
+
+ return {
+ "message": "Files uploaded successfully" if not failed else "Some files failed to upload",
+ "data": result,
+ "successful": successful,
+ "failed": failed
+ }
\ No newline at end of file
diff --git a/python/helpers/api.py b/python/helpers/api.py
new file mode 100644
index 000000000..87bf6720f
--- /dev/null
+++ b/python/helpers/api.py
@@ -0,0 +1,57 @@
+from abc import abstractmethod
+import json
+import threading
+from flask import Request, Response, jsonify, Flask
+from agent import AgentContext
+from initialize import initialize
+from python.helpers.print_style import PrintStyle
+from python.helpers.errors import format_error
+
+
+class ApiHandler:
+ def __init__(self, app: Flask, thread_lock: threading.Lock):
+ self.app = app
+ self.thread_lock = thread_lock
+
+ @abstractmethod
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ pass
+
+ async def handle_request(self, request: Request) -> Response:
+ try:
+ # input data from request based on type
+ if request.is_json:
+ input = request.get_json()
+ else:
+ input = {"data": request.get_data(as_text=True)}
+
+ # process via handler
+ output = await self.process(input, request)
+
+ # return output based on type
+ if isinstance(output, Response):
+ return output
+ else:
+ response_json = json.dumps(output)
+ return Response(response=response_json, status=200, mimetype="application/json")
+
+ # return exceptions with 500
+ except Exception as e:
+ error = format_error(e)
+ PrintStyle.error(error)
+ return Response(response=error, status=500, mimetype="text/plain")
+
+
+
+ # get context to run agent zero in
+ def get_context(self, ctxid: str):
+ with self.thread_lock:
+ if not ctxid:
+ first = AgentContext.first()
+ if first:
+ return first
+ return AgentContext(config=initialize())
+ got = AgentContext.get(ctxid)
+ if got:
+ return got
+ return AgentContext(config=initialize(), id=ctxid)
\ No newline at end of file
diff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.py
index 1524fff2b..c654f9db2 100644
--- a/python/helpers/extract_tools.py
+++ b/python/helpers/extract_tools.py
@@ -51,7 +51,7 @@ def fix_json_string(json_string):
T = TypeVar('T') # Define a generic type variable
-def load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T]) -> list[Type[T]]:
+def load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T], one_per_file: bool = True) -> list[Type[T]]:
classes = []
abs_folder = get_abs_path(folder)
@@ -70,8 +70,11 @@ def load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T]
class_list = inspect.getmembers(module, inspect.isclass)
# Filter for classes that are subclasses of the given base_class
- for cls in class_list:
+ # iterate backwards to skip imported superclasses
+ for cls in reversed(class_list):
if cls[1] is not base_class and issubclass(cls[1], base_class):
classes.append(cls[1])
+ if one_per_file:
+ break
return classes
\ No newline at end of file
diff --git a/run_ui.py b/run_ui.py
index f75061a58..9fffa60bf 100644
--- a/run_ui.py
+++ b/run_ui.py
@@ -16,6 +16,8 @@ from python.helpers import persist_chat, settings, whisper, rfc, runtime, dotenv
import base64
from werkzeug.utils import secure_filename
from python.helpers.cloudflare_tunnel import CloudflareTunnel
+from python.helpers.extract_tools import load_classes_from_folder
+from python.helpers.api import ApiHandler
from python.helpers.file_browser import FileBrowser
@@ -29,21 +31,7 @@ lock = threading.Lock()
basic_auth = BasicAuth(app)
-# get context to run agent zero in
-def get_context(ctxid: str):
- with lock:
- if not ctxid:
- first = AgentContext.first()
- if first:
- return first
- return AgentContext(config=initialize())
- got = AgentContext.get(ctxid)
- if got:
- return got
- return AgentContext(config=initialize(), id=ctxid)
-
-
-# Now you can use @requires_auth function decorator to require login on certain pages
+# require authentication for handlers
def requires_auth(f):
@wraps(f)
async def decorated(*args, **kwargs):
@@ -63,621 +51,16 @@ def requires_auth(f):
return decorated
-UPLOAD_FOLDER = os.path.join(os.getcwd(), "work_dir", "uploads")
-
-
[email protected]("/upload", methods=["POST"])
-@requires_auth
-async def upload_file():
- if "file" not in request.files:
- return jsonify({"ok": False, "message": "No file part"}), 400
-
- files = request.files.getlist("file") # Handle multiple files
- saved_filenames = []
-
- for file in files:
- if file and allowed_file(file.filename): # Check file type
- filename = secure_filename(file.filename) # type: ignore
- file.save(os.path.join(UPLOAD_FOLDER, filename))
- saved_filenames.append(filename)
-
- return jsonify({"ok": True, "filenames": saved_filenames}) # Return saved filenames
-
-
-ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "txt", "pdf", "csv", "html", "json", "md"}
-
-
-def allowed_file(filename):
- return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
-
-
[email protected]("/import_knowledge", methods=["POST"])
-@requires_auth
-async def import_knowledge():
- if "files[]" not in request.files:
- return jsonify({"ok": False, "message": "No files part"}), 400
-
- files = request.files.getlist("files[]")
- KNOWLEDGE_FOLDER = os.path.join(os.getcwd(), "knowledge", "custom", "main")
-
- saved_filenames = []
-
- for file in files:
- if file:
- filename = secure_filename(file.filename) # type: ignore
- file.save(os.path.join(KNOWLEDGE_FOLDER, filename))
- saved_filenames.append(filename)
-
- return jsonify(
- {"ok": True, "message": "Knowledge Imported", "filenames": saved_filenames}
- )
-
-
[email protected]("/getWorkDirFiles", methods=["GET"])
-@requires_auth
-async def get_work_dir_files():
- try:
- current_path = request.args.get('path', '')
- work_dir = files.get_abs_path("work_dir")
- browser = FileBrowser(work_dir)
- result = browser.get_files(current_path)
-
- response = {
- "ok": True,
- "data": result
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e)
- }
- PrintStyle.error(str(e))
-
- return jsonify(response)
-
-
[email protected]("/uploadWorkDirFiles", methods=["POST"])
-@requires_auth
-async def upload_work_dir_files():
- try:
- if "files[]" not in request.files:
- return jsonify({"ok": False, "message": "No files uploaded"}), 400
-
- current_path = request.form.get('path', '')
- uploaded_files = request.files.getlist("files[]")
-
- work_dir = files.get_abs_path("work_dir")
- browser = FileBrowser(work_dir)
-
- successful, failed = browser.save_files(uploaded_files, current_path)
-
- if not successful and failed:
- return jsonify({
- "ok": False,
- "message": "All uploads failed",
- "failed": failed
- }), 400
-
- result = browser.get_files(current_path)
-
- response = {
- "ok": True,
- "message": "Files uploaded successfully" if not failed else "Some files failed to upload",
- "data": result,
- "successful": successful,
- "failed": failed
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e)
- }
- PrintStyle.error(str(e))
-
- return jsonify(response)
-
-
[email protected]("/downloadWorkDirFile", methods=["GET"])
-@requires_auth
-async def download_work_dir_file():
- try:
- file_path = request.args.get('path', '')
- if not file_path:
- raise ValueError("No file path provided")
-
- work_dir = files.get_abs_path("work_dir")
- browser = FileBrowser(work_dir)
-
- full_path = browser.get_file_path(file_path)
- if full_path:
- return send_file(
- full_path,
- as_attachment=True,
- download_name=os.path.basename(file_path)
- )
-
- return jsonify({
- "ok": False,
- "message": "File not found"
- }), 404
-
- except Exception as e:
- return jsonify({
- "ok": False,
- "message": str(e)
- }), 500
-
-
[email protected]("/deleteWorkDirFile", methods=["POST"])
-@requires_auth
-async def delete_work_dir_file():
- try:
- data = request.get_json()
- file_path = data.get('path', '')
- current_path = data.get('currentPath', '')
-
- work_dir = files.get_abs_path("work_dir")
- browser = FileBrowser(work_dir)
-
- if browser.delete_file(file_path):
- # Get updated file list
- result = browser.get_files(current_path)
- response = {
- "ok": True,
- "data": result
- }
- else:
- response = {
- "ok": False,
- "message": "File not found or could not be deleted"
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e)
- }
- PrintStyle.error(str(e))
-
- return jsonify(response)
-
-
# handle default address, load index
@app.route("/", methods=["GET"])
@requires_auth
async def serve_index():
gitinfo = git.get_git_info()
- return files.read_file("./webui/index.html", version_no=gitinfo["version"], version_time=gitinfo["commit_time"])
-
-
-# simple health check, just return OK to see the server is running
[email protected]("/ok", methods=["GET", "POST"])
-async def health_check():
- gitinfo = git.get_git_info()
- return jsonify({"ok": True, "gitinfo": gitinfo})
-
-
-# send message to agent (async UI)
[email protected]("/msg", methods=["POST"])
-@requires_auth
-async def handle_message_async():
- return await handle_message(False)
-
-
-# send message to agent (synchronous API)
[email protected]("/msg_sync", methods=["POST"])
-@requires_auth
-async def handle_msg_sync():
- return await handle_message(True)
-
-
-async def handle_message(sync: bool):
- try:
- # Handle both JSON and multipart/form-data
- if request.content_type.startswith("multipart/form-data"):
- text = request.form.get("text", "")
- ctxid = request.form.get("context", "")
- message_id = request.form.get("message_id", None)
- attachments = request.files.getlist("attachments")
- attachment_paths = []
-
- upload_folder = files.get_abs_path("work_dir/uploads")
-
- if attachments:
- os.makedirs(upload_folder, exist_ok=True)
- for attachment in attachments:
- if attachment.filename is None:
- continue
- filename = secure_filename(attachment.filename)
- save_path = files.get_abs_path(upload_folder, filename)
- attachment.save(save_path)
- attachment_paths.append(save_path)
- else:
- # Handle JSON request as before
- input_data = request.get_json()
- text = input_data.get("text", "")
- ctxid = input_data.get("context", "")
- message_id = input_data.get("message_id", None)
- attachment_paths = []
-
- # Now process the message
- message = text
-
- # Obtain agent context
- context = get_context(ctxid)
-
- # Store attachments in agent data
- context.agent0.set_data("attachments", attachment_paths)
-
- # Prepare attachment filenames for logging
- attachment_filenames = (
- [os.path.basename(path) for path in attachment_paths]
- if attachment_paths
- else []
- )
-
- # Print to console and log
- PrintStyle(
- background_color="#6C3483", font_color="white", bold=True, padding=True
- ).print(f"User message:")
- PrintStyle(font_color="white", padding=False).print(f"> {message}")
- if attachment_filenames:
- PrintStyle(font_color="white", padding=False).print("Attachments:")
- for filename in attachment_filenames:
- PrintStyle(font_color="white", padding=False).print(f"- {filename}")
-
- # Log the message with message_id and attachments
- context.log.log(
- type="user",
- heading="User message",
- content=message,
- kvps={"attachments": attachment_filenames},
- id=message_id,
- )
-
- if sync:
- context.communicate(message)
- result = await context.process.result() # type: ignore
- response = {
- "ok": True,
- "message": result,
- "context": context.id,
- }
- else:
- context.communicate(message)
- response = {
- "ok": True,
- "message": "Message received.",
- "context": context.id,
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# pausing/unpausing the agent
[email protected]("/pause", methods=["POST"])
-@requires_auth
-async def pause():
- try:
-
- # data sent to the server
- input = request.get_json()
- paused = input.get("paused", False)
- ctxid = input.get("context", "")
-
- # context instance - get or create
- context = get_context(ctxid)
-
- context.paused = paused
-
- response = {
- "ok": True,
- "message": "Agent paused." if paused else "Agent unpaused.",
- "pause": paused,
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# load chats from json
[email protected]("/loadChats", methods=["POST"])
-@requires_auth
-async def load_chats():
- try:
- # data sent to the server
- input = request.get_json()
- chats = input.get("chats", [])
- if not chats:
- raise Exception("No chats provided")
-
- ctxids = persist_chat.load_json_chats(chats)
-
- response = {
- "ok": True,
- "message": "Chats loaded.",
- "ctxids": ctxids,
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# save chats to json
[email protected]("/exportChat", methods=["POST"])
-@requires_auth
-async def export_chat():
- try:
- # data sent to the server
- input = request.get_json()
- ctxid = input.get("ctxid", "")
- if not ctxid:
- raise Exception("No context id provided")
-
- context = get_context(ctxid)
- content = persist_chat.export_json_chat(context)
-
- response = {
- "ok": True,
- "message": "Chats exported.",
- "ctxid": context.id,
- "content": content,
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# restarting with new agent0
[email protected]("/reset", methods=["POST"])
-@requires_auth
-async def reset():
- try:
-
- # data sent to the server
- input = request.get_json()
- ctxid = input.get("context", "")
-
- # context instance - get or create
- context = get_context(ctxid)
- context.reset()
- persist_chat.save_tmp_chat(context)
-
- response = {
- "ok": True,
- "message": "Agent restarted.",
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# killing context
[email protected]("/remove", methods=["POST"])
-@requires_auth
-async def remove():
- try:
-
- # data sent to the server
- input = request.get_json()
- ctxid = input.get("context", "")
-
- # context instance - get or create
- AgentContext.remove(ctxid)
- persist_chat.remove_chat(ctxid)
-
- response = {
- "ok": True,
- "message": "Context removed.",
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# Web UI polling
[email protected]("/poll", methods=["POST"])
-@requires_auth
-async def poll():
- try:
-
- # data sent to the server
- input = request.get_json()
- ctxid = input.get("context", None)
- from_no = input.get("log_from", 0)
-
- # context instance - get or create
- context = get_context(ctxid)
-
- logs = context.log.output(start=from_no)
-
- # loop AgentContext._contexts
- ctxs = []
- for ctx in AgentContext._contexts.values():
- ctxs.append(
- {
- "id": ctx.id,
- "no": ctx.no,
- "log_guid": ctx.log.guid,
- "log_version": len(ctx.log.updates),
- "log_length": len(ctx.log.logs),
- "paused": ctx.paused,
- }
- )
-
- # data from this server
- response = {
- "ok": True,
- "context": context.id,
- "contexts": ctxs,
- "logs": logs,
- "log_guid": context.log.guid,
- "log_version": len(context.log.updates),
- "log_progress": context.log.progress,
- "paused": context.paused,
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # serialize json with json.dumps to preserve OrderedDict order
- response_json = json.dumps(response)
- return Response(response=response_json, status=200, mimetype="application/json")
- # return jsonify(response)
-
-
-# get current settings
[email protected]("/getSettings", methods=["POST"])
-@requires_auth
-async def get_settings():
- try:
-
- # data sent to the server
- input = request.get_json()
-
- set = settings.convert_out(settings.get_settings())
-
- response = {"ok": True, "settings": set}
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# set current settings
[email protected]("/setSettings", methods=["POST"])
-@requires_auth
-async def set_settings():
- try:
-
- # data sent to the server
- input = request.get_json()
-
- set = settings.convert_in(input)
- set = settings.set_settings(set)
-
- response = {"ok": True, "settings": set}
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# transcribe audio
[email protected]("/transcribe", methods=["POST"])
-@requires_auth
-async def transcribe():
- try:
-
- # data sent to the server
- input = request.get_json()
- audio = input.get("audio")
-
- # transcribe audio
- result = await whisper.transcribe(audio)
-
- response = {
- "ok": True,
- "text": result["text"],
- }
-
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
-
- # respond with json
- return jsonify(response)
-
-
-# remote function call
[email protected]("/rfc", methods=["POST"])
-@requires_auth
-async def handle_rfc():
- try:
- # data sent to the server
- input = json.loads(request.get_json())
-
- # handle RFC
- result = await runtime.handle_rfc(input)
-
- response = {
- "ok": True,
- "result": result,
- }
-
- return jsonify(response)
- except Exception as e:
- response = {
- "ok": False,
- "message": str(e),
- }
- PrintStyle.error(str(e))
- return jsonify(response), 500
-
+ return files.read_file(
+ "./webui/index.html",
+ version_no=gitinfo["version"],
+ version_time=gitinfo["commit_time"],
+ )
def run():
print("Initializing framework...")
@@ -710,6 +93,27 @@ def run():
# initialize contexts from persisted chats
persist_chat.load_tmp_chats()
+ def register_api_handler(app, handler):
+ name = handler.__module__.split(".")[-1]
+ instance = handler(app, lock)
+ @requires_auth
+ async def handle_request():
+ return await instance.handle_request(request=request)
+ app.add_url_rule(
+ f"/{name}",
+ f"/{name}",
+ handle_request,
+ methods=["POST", "GET"],
+ )
+
+ # initialize and register API handlers
+ handlers = load_classes_from_folder("python/api", "*.py", ApiHandler)
+ for handler in handlers:
+ register_api_handler(app, handler)
+
+
+
+
try:
# Run Flask app
app.run(
diff --git a/webui/file_browser.js b/webui/file_browser.js
index e2b407738..8f8f1621c 100644
--- a/webui/file_browser.js
+++ b/webui/file_browser.js
@@ -17,16 +17,16 @@ const fileBrowserModalProxy = {
async openModal() {
const modalEl = document.getElementById('fileBrowserModal');
const modalAD = Alpine.$data(modalEl);
-
+
modalAD.isOpen = true;
modalAD.isLoading = true;
modalAD.history = []; // reset history when opening modal
-
+
// Initialize currentPath to root if it's empty
if (!modalAD.browser.currentPath) {
modalAD.browser.currentPath = "";
}
-
+
await modalAD.fetchFiles(modalAD.browser.currentPath);
},
@@ -39,15 +39,15 @@ const fileBrowserModalProxy = {
async fetchFiles(path = "") {
this.isLoading = true;
try {
- const response = await fetch(`/getWorkDirFiles?path=${encodeURIComponent(path)}`);
- const data = await response.json();
-
- if (data.ok) {
+ const response = await fetch(`/get_work_dir_files?path=${encodeURIComponent(path)}`);
+
+ if (response.ok) {
+ const data = await response.json();
this.browser.entries = data.data.entries;
this.browser.currentPath = data.data.current_path;
this.browser.parentPath = data.data.parent_path;
} else {
- console.error('Error fetching files:', data.message);
+ console.error('Error fetching files:', await response.text());
this.browser.entries = [];
}
} catch (error) {
@@ -108,7 +108,7 @@ const fileBrowserModalProxy = {
if (!confirm(`Are you sure you want to delete ${file.name}?`)) {
return;
}
-
+
try {
const response = await fetch('/deleteWorkDirFile', {
method: 'POST',
@@ -121,45 +121,46 @@ const fileBrowserModalProxy = {
})
});
- const data = await response.json();
- if (data.ok) {
+ if (response.ok) {
+ const data = await response.json();
this.browser.entries = this.browser.entries.filter(entry => entry.path !== file.path);
alert('File deleted successfully.');
} else {
- alert(`Error deleting file: ${data.message}`);
+ alert(`Error deleting file: ${await response.text()}`);
}
} catch (error) {
console.error('Error deleting file:', error);
alert('Error deleting file');
}
},
-
- handleFileUpload(event) {
- const files = event.target.files;
- if (!files.length) return;
-
- const formData = new FormData();
- formData.append('path', this.browser.currentPath);
-
- for (let i = 0; i < files.length; i++) {
- const ext = files[i].name.split('.').pop().toLowerCase();
- if (!['zip', 'tar', 'gz', 'rar', '7z'].includes(ext)) {
- if (files[i].size > 100 * 1024 * 1024) { // 100MB
- alert(`File ${files[i].name} exceeds the maximum allowed size of 100MB.`);
- continue;
+
+ async handleFileUpload(event) {
+ try {
+ const files = event.target.files;
+ if (!files.length) return;
+
+ const formData = new FormData();
+ formData.append('path', this.browser.currentPath);
+
+ for (let i = 0; i < files.length; i++) {
+ const ext = files[i].name.split('.').pop().toLowerCase();
+ if (!['zip', 'tar', 'gz', 'rar', '7z'].includes(ext)) {
+ if (files[i].size > 100 * 1024 * 1024) { // 100MB
+ alert(`File ${files[i].name} exceeds the maximum allowed size of 100MB.`);
+ continue;
+ }
}
+ formData.append('files[]', files[i]);
}
- formData.append('files[]', files[i]);
- }
-
- // Proceed with upload after validation
- fetch('/uploadWorkDirFiles', {
- method: 'POST',
- body: formData
- })
- .then(response => response.json())
- .then(data => {
- if (data.ok) {
+
+ // Proceed with upload after validation
+ const response = await fetch('/upload_work_dir_files', {
+ method: 'POST',
+ body: formData
+ });
+
+ if (response.ok) {
+ const data = await response.json();
// Update the file list with new data
this.browser.entries = data.data.entries.map(entry => ({
...entry,
@@ -167,48 +168,52 @@ const fileBrowserModalProxy = {
}));
this.browser.currentPath = data.data.current_path;
this.browser.parentPath = data.data.parent_path;
-
+
// Show success message
if (data.failed && data.failed.length > 0) {
const failedFiles = data.failed.map(file => `${file.name}: ${file.error}`).join('\n');
alert(`Some files failed to upload:\n${failedFiles}`);
}
} else {
+
alert(data.message);
}
- })
- .catch(error => {
+
+ } catch (error) {
console.error('Error uploading files:', error);
alert('Error uploading files');
- });
+ }
},
- downloadFile(file) {
+ async downloadFile(file) {
if (file.is_dir) return;
-
- const downloadUrl = `/downloadWorkDirFile?path=${encodeURIComponent(file.path)}`;
-
- fetch(downloadUrl)
-
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- return response.blob();
- })
- .then(blob => {
- const link = document.createElement('a');
- link.href = window.URL.createObjectURL(blob);
- link.download = file.name;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- window.URL.revokeObjectURL(link.href);
- })
- .catch(error => {
- console.error('Error downloading file:', error);
- alert('Error downloading file');
- });
+
+ try {
+
+ const downloadUrl = `/download_work_dir_file?path=${encodeURIComponent(file.path)}`;
+
+ const response = await fetch(downloadUrl)
+
+
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+
+ const blob = await response.blob();
+
+ const link = document.createElement('a');
+ link.href = window.URL.createObjectURL(blob);
+ link.download = file.name;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ window.URL.revokeObjectURL(link.href);
+
+ } catch (error) {
+
+ console.error('Error downloading file:', error);
+ alert('Error downloading file');
+ }
},
// Helper Functions
diff --git a/webui/index.css b/webui/index.css
index 3ab8c1b26..1967a740e 100644
--- a/webui/index.css
+++ b/webui/index.css
@@ -356,6 +356,7 @@ h4 {
color: var(--color-text);
opacity: 0.7;
font-size: 0.7rem;
+ user-select: all;
}
pre {
diff --git a/webui/index.js b/webui/index.js
index df17d4902..c8f9fe060 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -114,7 +114,7 @@ export async function sendMessage() {
formData.append('attachments', attachments[i].file);
}
- response = await fetch('/msg', {
+ response = await fetch('/message_async', {
method: 'POST',
body: formData
});
@@ -125,7 +125,7 @@ export async function sendMessage() {
context,
message_id: messageId
};
- response = await fetch('/msg', {
+ response = await fetch('/message_async', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@@ -138,13 +138,15 @@ export async function sendMessage() {
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 {
+ }
+ // else if (!jsonResponse.ok) {
+ // if (jsonResponse.message) {
+ // toast(jsonResponse.message, "error");
+ // } else {
+ // toast("Undefined error.", "error");
+ // }
+ // }
+ else {
setContext(jsonResponse.context);
}
@@ -257,10 +259,10 @@ window.loadKnowledge = async function () {
body: formData,
});
- const data = await response.json();
- if (!data.ok) {
- toast(data.message, "error");
+ if (!response.ok) {
+ toast(await response.text(), "error");
} else {
+ const data = await response.json();
toast("Knowledge files imported: " + data.filenames.join(", "), "success");
}
};
@@ -274,7 +276,7 @@ function adjustTextareaHeight() {
chatInput.style.height = (chatInput.scrollHeight) + 'px';
}
-window.sendJsonData = async function (url, data) {
+export const sendJsonData = async function (url, data) {
const response = await fetch(url, {
method: 'POST',
headers: {
@@ -284,11 +286,13 @@ window.sendJsonData = async function (url, data) {
});
if (!response.ok) {
- throw new Error('Network response was not 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) {
@@ -308,40 +312,35 @@ async function poll() {
const response = await sendJsonData("/poll", { log_from: lastLogVersion, context });
//console.log(response)
- if (response.ok) {
-
- 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)
+ }
- updateProgress(response.log_progress)
-
- //set ui model vars from backend
- const inputAD = Alpine.$data(inputSection);
- inputAD.paused = response.paused;
- const statusAD = Alpine.$data(statusSection);
- statusAD.connected = response.ok;
- const chatsAD = Alpine.$data(chatsSection);
- chatsAD.contexts = response.contexts;
-
- lastLogVersion = response.log_version;
- lastLogGuid = response.log_guid;
+ updateProgress(response.log_progress)
+ //set ui model vars from backend
+ const inputAD = Alpine.$data(inputSection);
+ inputAD.paused = response.paused;
+ const statusAD = Alpine.$data(statusSection);
+ statusAD.connected = true;
+ const chatsAD = Alpine.$data(chatsSection);
+ chatsAD.contexts = response.contexts;
- }
+ lastLogVersion = response.log_version;
+ lastLogGuid = response.log_guid;
} catch (error) {
console.error('Error:', error);
@@ -375,7 +374,7 @@ function speakMessages(logs) {
function updateProgress(progress) {
const defaultText = "Waiting for input"
if (!progress) progress = defaultText
-
+
if (progress == defaultText) {
removeClassFromElement(progressBar, "shiny-text")
} else {
@@ -406,7 +405,7 @@ window.pauseAgent = async function (paused) {
}
window.resetChat = async function () {
- const resp = await sendJsonData("/reset", { context });
+ const resp = await sendJsonData("/chat_reset", { context });
updateAfterScroll()
}
@@ -434,7 +433,7 @@ window.killChat = async function (id) {
else setContext(generateGUID())
}
- if (found) sendJsonData("/remove", { context: id });
+ if (found) sendJsonData("/chat_remove", { context: id });
updateAfterScroll()
}
@@ -534,17 +533,19 @@ function toggleCssProperty(selector, property, value) {
window.loadChats = async function () {
try {
const fileContents = await readJsonFiles();
- const response = await sendJsonData("/loadChats", { chats: fileContents });
+ 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 {
+ }
+ // 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")
}
@@ -556,17 +557,19 @@ window.loadChats = async function () {
window.saveChat = async function () {
try {
- const response = await sendJsonData("/exportChat", { ctxid: context });
+ 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 {
+ }
+ // 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")
}
diff --git a/webui/settings.js b/webui/settings.js
index 94493c163..7b66cf9ec 100644
--- a/webui/settings.js
+++ b/webui/settings.js
@@ -10,7 +10,7 @@ const settingsModalProxy = {
const modalAD = Alpine.$data(modalEl);
//get settings from backend
- const set = await sendJsonData("/getSettings", null);
+ const set = await sendJsonData("/settings_get", null);
const settings = {
"title": "Settings page",
@@ -43,7 +43,7 @@ const settingsModalProxy = {
const modalEl = document.getElementById('settingsModal');
const modalAD = Alpine.$data(modalEl);
- resp = await window.sendJsonData("/setSettings", modalAD.settings);
+ resp = await window.sendJsonData("/settings_set", modalAD.settings);
this.resolvePromise({
status: 'saved',
diff --git a/webui/speech.js b/webui/speech.js
index 4e8e9b5c3..1e8c221f3 100644
--- a/webui/speech.js
+++ b/webui/speech.js
@@ -1,4 +1,4 @@
-import { pipeline, read_audio } from './[email protected]';
+// import { pipeline, read_audio } from './[email protected]';
import { updateChatInput, sendMessage } from './index.js';
const microphoneButton = document.getElementById('microphone-button');