Skip to content

Agent Zero AI framework

api cleanup, microphone permission

frdel99b4accd7b5e
diff --git a/python/api/backup_create.py b/python/api/backup_create.py
index d66aa4dcd..307fb62c0 100644
--- a/python/api/backup_create.py
+++ b/python/api/backup_create.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response, send_file
+from python.helpers.api import ApiHandler, Request, Response, send_file
from python.helpers.backup import BackupService
from python.helpers.persist_chat import save_tmp_chats
diff --git a/python/api/backup_get_defaults.py b/python/api/backup_get_defaults.py
index 7b9bf2652..3a3f4cf9e 100644
--- a/python/api/backup_get_defaults.py
+++ b/python/api/backup_get_defaults.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.backup import BackupService
diff --git a/python/api/backup_inspect.py b/python/api/backup_inspect.py
index 72cb1fe2e..97e247e0e 100644
--- a/python/api/backup_inspect.py
+++ b/python/api/backup_inspect.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.backup import BackupService
from werkzeug.datastructures import FileStorage
diff --git a/python/api/backup_preview_grouped.py b/python/api/backup_preview_grouped.py
index 17eedbda6..bcaf9a981 100644
--- a/python/api/backup_preview_grouped.py
+++ b/python/api/backup_preview_grouped.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.backup import BackupService
from typing import Dict, Any
diff --git a/python/api/backup_restore.py b/python/api/backup_restore.py
index 1b38cb79c..86bf2a82a 100644
--- a/python/api/backup_restore.py
+++ b/python/api/backup_restore.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from werkzeug.datastructures import FileStorage
from python.helpers.backup import BackupService
from python.helpers.persist_chat import load_tmp_chats
diff --git a/python/api/backup_restore_preview.py b/python/api/backup_restore_preview.py
index a73dbf2ad..d5d6e1bb5 100644
--- a/python/api/backup_restore_preview.py
+++ b/python/api/backup_restore_preview.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from werkzeug.datastructures import FileStorage
from python.helpers.backup import BackupService
import json
diff --git a/python/api/backup_test.py b/python/api/backup_test.py
index fbaee7665..d0234a784 100644
--- a/python/api/backup_test.py
+++ b/python/api/backup_test.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.backup import BackupService
diff --git a/python/api/download_work_dir_file.py b/python/api/download_work_dir_file.py
index eca4c2507..09ca4019f 100644
--- a/python/api/download_work_dir_file.py
+++ b/python/api/download_work_dir_file.py
@@ -1,9 +1,7 @@
import base64
from io import BytesIO
-from python.helpers.api import ApiHandler, Input, Output, Request, Response
-from flask import send_file
-
+from python.helpers.api import ApiHandler, Input, Output, Request, Response, send_file
from python.helpers import files, runtime
from python.api import file_info
import os
diff --git a/python/api/get_work_dir_files.py b/python/api/get_work_dir_files.py
index bfe6f8e12..4f640e8aa 100644
--- a/python/api/get_work_dir_files.py
+++ b/python/api/get_work_dir_files.py
@@ -1,8 +1,6 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.file_browser import FileBrowser
-from python.helpers import files, runtime
+from python.helpers import runtime
class GetWorkDirFiles(ApiHandler):
diff --git a/python/api/health.py b/python/api/health.py
index 7cf0965f4..72a6b83f8 100644
--- a/python/api/health.py
+++ b/python/api/health.py
@@ -1,8 +1,5 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-from python.helpers import errors
-
-from python.helpers import git
+from python.helpers.api import ApiHandler, Request, Response
+from python.helpers import errors, git
class HealthCheck(ApiHandler):
diff --git a/python/api/history_get.py b/python/api/history_get.py
index 9b1359c49..7ff40e3fe 100644
--- a/python/api/history_get.py
+++ b/python/api/history_get.py
@@ -1,6 +1,4 @@
-from python.helpers import tokens
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
class GetHistory(ApiHandler):
diff --git a/python/api/image_get.py b/python/api/image_get.py
index acca85dc7..8c6e3dd9c 100644
--- a/python/api/image_get.py
+++ b/python/api/image_get.py
@@ -1,11 +1,7 @@
import base64
import os
-import re
-from typing import override
-from python.helpers.api import ApiHandler
-from python.helpers import files
-from flask import Request, Response, send_file
-from python.helpers import runtime
+from python.helpers.api import ApiHandler, Request, Response, send_file
+from python.helpers import files, runtime
import io
from mimetypes import guess_type
diff --git a/python/api/import_knowledge.py b/python/api/import_knowledge.py
index 30d2d8716..667964075 100644
--- a/python/api/import_knowledge.py
+++ b/python/api/import_knowledge.py
@@ -1,7 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-
-from python.helpers.file_browser import FileBrowser
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import files, memory
import os
from werkzeug.utils import secure_filename
diff --git a/python/api/mcp_server_get_detail.py b/python/api/mcp_server_get_detail.py
index e5e385aac..0608aadc5 100644
--- a/python/api/mcp_server_get_detail.py
+++ b/python/api/mcp_server_get_detail.py
@@ -1,7 +1,4 @@
-from math import log
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-
+from python.helpers.api import ApiHandler, Request, Response
from typing import Any
from python.helpers.mcp_handler import MCPConfig
diff --git a/python/api/mcp_server_get_log.py b/python/api/mcp_server_get_log.py
index eadf25bb8..f9488c7df 100644
--- a/python/api/mcp_server_get_log.py
+++ b/python/api/mcp_server_get_log.py
@@ -1,6 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-
+from python.helpers.api import ApiHandler, Request, Response
from typing import Any
from python.helpers.mcp_handler import MCPConfig
diff --git a/python/api/mcp_servers_apply.py b/python/api/mcp_servers_apply.py
index f5a5aef4b..08f513652 100644
--- a/python/api/mcp_servers_apply.py
+++ b/python/api/mcp_servers_apply.py
@@ -1,6 +1,5 @@
import time
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from typing import Any
diff --git a/python/api/mcp_servers_status.py b/python/api/mcp_servers_status.py
index 8d25dcb7b..be2654930 100644
--- a/python/api/mcp_servers_status.py
+++ b/python/api/mcp_servers_status.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from typing import Any
diff --git a/python/api/message.py b/python/api/message.py
index 69e2db12a..2f88f4784 100644
--- a/python/api/message.py
+++ b/python/api/message.py
@@ -1,6 +1,5 @@
from agent import AgentContext, UserMessage
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import files
import os
diff --git a/python/api/message_async.py b/python/api/message_async.py
index 22d9192ef..f3e85dfb5 100644
--- a/python/api/message_async.py
+++ b/python/api/message_async.py
@@ -1,10 +1,4 @@
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
diff --git a/python/api/nudge.py b/python/api/nudge.py
index 62a497853..64683da07 100644
--- a/python/api/nudge.py
+++ b/python/api/nudge.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
class Nudge(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
diff --git a/python/api/pause.py b/python/api/pause.py
index 95fea1b07..e4b20ecfb 100644
--- a/python/api/pause.py
+++ b/python/api/pause.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
class Pause(ApiHandler):
diff --git a/python/api/poll.py b/python/api/poll.py
index df3e22c80..05af6df8b 100644
--- a/python/api/poll.py
+++ b/python/api/poll.py
@@ -1,7 +1,4 @@
-import time
-from datetime import datetime
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from agent import AgentContext
diff --git a/python/api/restart.py b/python/api/restart.py
index 864e16968..678fe7490 100644
--- a/python/api/restart.py
+++ b/python/api/restart.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import process
diff --git a/python/api/rfc.py b/python/api/rfc.py
index 82fa2f739..f2fa112d1 100644
--- a/python/api/rfc.py
+++ b/python/api/rfc.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import runtime
diff --git a/python/api/settings_get.py b/python/api/settings_get.py
index 2ecdbef38..f807bc60a 100644
--- a/python/api/settings_get.py
+++ b/python/api/settings_get.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import settings
diff --git a/python/api/settings_set.py b/python/api/settings_set.py
index 6700aace5..c24a3cc66 100644
--- a/python/api/settings_set.py
+++ b/python/api/settings_set.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import settings
diff --git a/python/api/synthesize.py b/python/api/synthesize.py
index 2c1c75b1a..15fc2d7c9 100644
--- a/python/api/synthesize.py
+++ b/python/api/synthesize.py
@@ -1,8 +1,6 @@
# api/synthesize.py
-import re
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import runtime, settings, kokoro_tts
diff --git a/python/api/transcribe.py b/python/api/transcribe.py
index dd6573c8d..5ad4ade63 100644
--- a/python/api/transcribe.py
+++ b/python/api/transcribe.py
@@ -1,5 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import runtime, settings, whisper
diff --git a/python/api/tunnel.py b/python/api/tunnel.py
index 8279330f1..ca5d64f42 100644
--- a/python/api/tunnel.py
+++ b/python/api/tunnel.py
@@ -1,6 +1,5 @@
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import runtime
-from python.helpers.api import ApiHandler
from python.helpers.tunnel_manager import TunnelManager
class Tunnel(ApiHandler):
diff --git a/python/api/tunnel_proxy.py b/python/api/tunnel_proxy.py
index efcd39a77..c8b1bd75b 100644
--- a/python/api/tunnel_proxy.py
+++ b/python/api/tunnel_proxy.py
@@ -1,6 +1,5 @@
-from flask import Request, Response
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import dotenv, runtime
-from python.helpers.api import ApiHandler
from python.helpers.tunnel_manager import TunnelManager
import requests
diff --git a/python/api/upload.py b/python/api/upload.py
index 02aaa7186..4a596a577 100644
--- a/python/api/upload.py
+++ b/python/api/upload.py
@@ -1,6 +1,4 @@
-from python.helpers.api import ApiHandler
-from flask import Request, Response
-
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers import files
from werkzeug.utils import secure_filename
diff --git a/python/api/upload_work_dir_files.py b/python/api/upload_work_dir_files.py
index 74035b02d..8bf85e664 100644
--- a/python/api/upload_work_dir_files.py
+++ b/python/api/upload_work_dir_files.py
@@ -1,8 +1,6 @@
import base64
from werkzeug.datastructures import FileStorage
-from python.helpers.api import ApiHandler
-from flask import Request, Response, send_file
-
+from python.helpers.api import ApiHandler, Request, Response
from python.helpers.file_browser import FileBrowser
from python.helpers import files, runtime
from python.api import get_work_dir_files
diff --git a/python/helpers/api.py b/python/helpers/api.py
index 8110386c3..5d0e171f5 100644
--- a/python/helpers/api.py
+++ b/python/helpers/api.py
@@ -3,7 +3,7 @@ import json
import threading
from typing import Union, TypedDict, Dict, Any
from attr import dataclass
-from flask import Request, Response, jsonify, Flask, session, request
+from flask import Request, Response, jsonify, Flask, session, request, send_file
from agent import AgentContext
from initialize import initialize_agent
from python.helpers.print_style import PrintStyle
diff --git a/webui/components/settings/speech/microphone-setting-store.js b/webui/components/settings/speech/microphone-setting-store.js
index ddc45e4e0..4d079d53c 100644
--- a/webui/components/settings/speech/microphone-setting-store.js
+++ b/webui/components/settings/speech/microphone-setting-store.js
@@ -24,9 +24,44 @@ const model = {
this.selectedDevice = this.devices.length > 0 ? this.devices[0].deviceId : "";
},
- requestPermission() {
- navigator.mediaDevices.getUserMedia({ audio: true });
- setTimeout(() => this.loadDevices(), 1000);
+ // track permission request state
+ requestingPermission: false,
+ permissionTimer: null,
+ permissionAttempts: 0,
+
+ // request microphone permission and poll for devices
+ async requestPermission() {
+ // set flag first so UI can update immediately
+ clearTimeout(this.permissionTimer);
+ this.requestingPermission = true;
+ this.permissionAttempts = 0;
+
+ // request permission in next tick to allow UI to update
+ setTimeout(async () => {
+ try {
+ await navigator.mediaDevices.getUserMedia({ audio: true });
+ // start polling for devices
+ this.pollForDevices();
+ } catch (err) {
+ console.error("Microphone permission denied");
+ this.requestingPermission = false;
+ }
+ }, 0);
+ },
+
+ // poll for devices until found or timeout (60s)
+ async pollForDevices() {
+ await this.loadDevices();
+
+ // check if we found devices with valid IDs
+ if (this.devices.some(d => d.deviceId && d.deviceId !== "") || this.permissionAttempts >= 60) {
+ this.requestingPermission = false;
+ return;
+ }
+
+ // continue polling
+ this.permissionAttempts++;
+ this.permissionTimer = setTimeout(() => this.pollForDevices(), 1000);
},
async selectDevice(deviceId) {
diff --git a/webui/components/settings/speech/microphone.html b/webui/components/settings/speech/microphone.html
index 2a6b97a16..7f4154afd 100644
--- a/webui/components/settings/speech/microphone.html
+++ b/webui/components/settings/speech/microphone.html
@@ -25,8 +25,14 @@
:selected="option.deviceId === $store.microphoneSetting.selectedDevice"></option>
</template>
</select>
- <button class="btn btn-field" x-show="$store.microphoneSetting.devices.length == 0"
- @click="$store.microphoneSetting.requestPermission()">Request permission</button>
+ <button class="btn btn-field"
+ x-show="$store.microphoneSetting.devices.length == 0 && !$store.microphoneSetting.requestingPermission"
+ @click="$store.microphoneSetting.requestPermission()">Request permission to select device</button>
+ <button class="btn btn-field"
+ x-show="$store.microphoneSetting.requestingPermission"
+ @click="$store.microphoneSetting.requestPermission()">
+ <span>Waiting for devices... [retry]</span>
+ </button>
</div>
</template>