Skip to content

Agent Zero AI framework

microphone setting, ui polishing

frdel0b9e2aa186c4
diff --git a/python/api/synthesize.py b/python/api/synthesize.py
index 0a91833a0..2c1c75b1a 100644
--- a/python/api/synthesize.py
+++ b/python/api/synthesize.py
@@ -13,7 +13,7 @@ class Synthesize(ApiHandler):
context = self.get_context(ctxid)
if not await kokoro_tts.is_downloaded():
- context.log.log(type="info", content="Kokoro TTS model is currently being downloaded, please wait...")
+ context.log.log(type="info", content="Kokoro TTS model is currently being initialized, please wait...")
try:
# # Clean and chunk text for long responses
@@ -32,7 +32,7 @@ class Synthesize(ApiHandler):
# audio_parts.append(chunk_audio)
# return {"audio_parts": audio_parts, "success": True}
-
+ # audio is chunked on the frontend for better flow
audio = await kokoro_tts.synthesize_sentences([text])
return {"audio": audio, "success": True}
except Exception as e:
diff --git a/python/api/transcribe.py b/python/api/transcribe.py
index f81e6aa3d..dd6573c8d 100644
--- a/python/api/transcribe.py
+++ b/python/api/transcribe.py
@@ -10,7 +10,7 @@ class Transcribe(ApiHandler):
context = self.get_context(ctxid)
if not await whisper.is_downloaded():
- context.log.log(type="info", content="Whisper STT model is currently being downloaded, please wait...")
+ context.log.log(type="info", content="Whisper STT model is currently being initialized, please wait...")
set = settings.get_settings()
result = await whisper.transcribe(set["stt_model_size"], audio) # type: ignore
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index feec1a84d..522e7268d 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -71,7 +71,7 @@ class Settings(TypedDict):
stt_silence_duration: int
stt_waiting_timeout: int
- tts_enabled: bool
+ tts_kokoro: bool
mcp_servers: str
mcp_client_init_timeout: int
@@ -94,7 +94,7 @@ class SettingsField(TypedDict, total=False):
title: str
description: str
type: Literal[
- "text", "number", "select", "range", "textarea", "password", "switch", "button"
+ "text", "number", "select", "range", "textarea", "password", "switch", "button", "html"
]
value: Any
min: float
@@ -632,11 +632,21 @@ def convert_out(settings: Settings) -> SettingsOutput:
# Speech to text section
stt_fields: list[SettingsField] = []
+ stt_fields.append(
+ {
+ "id": "stt_microphone_section",
+ "title": "Microphone device",
+ "description": "Select the microphone device to use for speech-to-text.",
+ "value": "<x-component path='/settings/speech/microphone.html' />",
+ "type": "html",
+ }
+ )
+
stt_fields.append(
{
"id": "stt_model_size",
- "title": "Model Size",
- "description": "Select the speech recognition model size",
+ "title": "Speech-to-text model size",
+ "description": "Select the speech-to-text model size",
"type": "select",
"value": settings["stt_model_size"],
"options": [
@@ -653,7 +663,7 @@ def convert_out(settings: Settings) -> SettingsOutput:
stt_fields.append(
{
"id": "stt_language",
- "title": "Language Code",
+ "title": "Speech-to-text language code",
"description": "Language code (e.g. en, fr, it)",
"type": "text",
"value": settings["stt_language"],
@@ -663,8 +673,8 @@ def convert_out(settings: Settings) -> SettingsOutput:
stt_fields.append(
{
"id": "stt_silence_threshold",
- "title": "Silence threshold",
- "description": "Silence detection threshold. Lower values are more sensitive.",
+ "title": "Microphone silence threshold",
+ "description": "Silence detection threshold. Lower values are more sensitive to noise.",
"type": "range",
"min": 0,
"max": 1,
@@ -676,8 +686,8 @@ def convert_out(settings: Settings) -> SettingsOutput:
stt_fields.append(
{
"id": "stt_silence_duration",
- "title": "Silence duration (ms)",
- "description": "Duration of silence before the server considers speaking to have ended.",
+ "title": "Microphone silence duration (ms)",
+ "description": "Duration of silence before the system considers speaking to have ended.",
"type": "text",
"value": settings["stt_silence_duration"],
}
@@ -686,8 +696,8 @@ def convert_out(settings: Settings) -> SettingsOutput:
stt_fields.append(
{
"id": "stt_waiting_timeout",
- "title": "Waiting timeout (ms)",
- "description": "Duration before the server closes the microphone.",
+ "title": "Microphone waiting timeout (ms)",
+ "description": "Duration of silence before the system closes the microphone.",
"type": "text",
"value": settings["stt_waiting_timeout"],
}
@@ -698,11 +708,11 @@ def convert_out(settings: Settings) -> SettingsOutput:
tts_fields.append(
{
- "id": "tts_enabled",
+ "id": "tts_kokoro",
"title": "Enable Kokoro TTS",
- "description": "Enable server-side AI text-to-speech (Kokoro)",
+ "description": "Enable higher quality server-side AI (Kokoro) instead of browser-based text-to-speech.",
"type": "switch",
- "value": settings["tts_enabled"],
+ "value": settings["tts_kokoro"],
}
)
@@ -1031,7 +1041,7 @@ def get_default_settings() -> Settings:
stt_silence_threshold=0.3,
stt_silence_duration=1000,
stt_waiting_timeout=2000,
- tts_enabled=False,
+ tts_kokoro=True,
mcp_servers='{\n "mcpServers": {}\n}',
mcp_client_init_timeout=10,
mcp_client_tool_timeout=120,
diff --git a/webui/components/_examples/_example-component.html b/webui/components/_examples/_example-component.html
new file mode 100644
index 000000000..9b1b7848d
--- /dev/null
+++ b/webui/components/_examples/_example-component.html
@@ -0,0 +1,36 @@
+<html>
+
+<head>
+ <title>Example component or modal</title>
+
+ <!-- Import the alpine store -->
+ <script type="module">
+ import { store } from "/components/_examples/_example-store.js";
+ </script>
+</head>
+
+<body>
+
+ <!-- This construct of x-data + x-if is used to ensure the component is only rendered when the store is available -->
+ <div x-data>
+ <template x-if="$store.exampleStore">
+
+ <!-- Keep in mind that <template> can have only one root element inside -->
+ <div>
+ <p x-text="$store.exampleStore.example1"></p>
+ <p x-text="$store.exampleStore.example2"></p>
+ </div>
+
+ </template>
+ </div>
+
+ <!-- Optional style for the component -->
+ <style>
+ #example-component {
+ width: 100%;
+ }
+ </style>
+
+</body>
+
+</html>
\ No newline at end of file
diff --git a/webui/components/_examples/_example-store.js b/webui/components/_examples/_example-store.js
new file mode 100644
index 000000000..480e2f070
--- /dev/null
+++ b/webui/components/_examples/_example-store.js
@@ -0,0 +1,19 @@
+import { createStore } from "/js/AlpineStore.js";
+
+// define the model object holding data and functions
+const model = {
+ example1:"Example 1",
+ example2:"Example 2",
+
+ // gets called when the store is created
+ init(){
+ console.log("Example store initialized");
+ }
+
+};
+
+// convert it to alpine store
+const store = createStore("_exampleStore", model);
+
+// export for use in other files
+export { store };
diff --git a/webui/components/chat/attachments/attachmentsStore.js b/webui/components/chat/attachments/attachmentsStore.js
index c1019ed94..bb159d2d1 100644
--- a/webui/components/chat/attachments/attachmentsStore.js
+++ b/webui/components/chat/attachments/attachmentsStore.js
@@ -220,7 +220,7 @@ const model = {
Array.from(files).forEach((file) => {
console.log("Processing file:", file.name, file.type);
const ext = file.name.split(".").pop().toLowerCase();
- const isImage = ["jpg", "jpeg", "png", "bmp", "gif", "webp"].includes(
+ const isImage = ["jpg", "jpeg", "png", "bmp", "gif", "webp", "svg"].includes(
ext
);
diff --git a/webui/components/chat/speech/speech-store.js b/webui/components/chat/speech/speech-store.js
index b9a776ee1..75537e43f 100644
--- a/webui/components/chat/speech/speech-store.js
+++ b/webui/components/chat/speech/speech-store.js
@@ -1,6 +1,7 @@
import { createStore } from "/js/AlpineStore.js";
import { updateChatInput, sendMessage } from "/index.js";
import { sleep } from "/js/sleep.js";
+import { store as microphoneSettingStore } from "/components/settings/speech/microphone-setting-store.js";
const Status = {
INACTIVE: "inactive",
@@ -21,7 +22,7 @@ const model = {
stt_waiting_timeout: 2000,
// TTS Settings
- tts_enabled: false,
+ tts_kokoro: false,
// TTS State
isSpeaking: false,
@@ -36,6 +37,7 @@ const model = {
// STT State
microphoneInput: null,
isProcessingClick: false,
+ selectedDevice: null,
// Getter for micStatus - delegates to microphoneInput
get micStatus() {
@@ -62,6 +64,15 @@ const model = {
if (this.isProcessingClick) return;
this.isProcessingClick = true;
try {
+
+ // reset mic input if device has changed in settings
+ const device = microphoneSettingStore.getSelectedDevice();
+ if(device!=this.selectedDevice){
+ this.selectedDevice = device;
+ this.microphoneInput = null;
+ console.log("Device changed, microphoneInput reset");
+ }
+
if (!this.microphoneInput) {
await this.initMicrophone();
}
@@ -220,7 +231,7 @@ const model = {
// speak wrapper
async _speak(text, waitForPrevious, terminator) {
// default browser speech
- if (!this.tts_enabled)
+ if (!this.tts_kokoro)
return await this.speakWithBrowser(text, waitForPrevious, terminator);
// kokoro tts
@@ -347,6 +358,7 @@ const model = {
this.browserUtterance.onend = () => {
this.isSpeaking = false;
};
+
this.synth.speak(this.browserUtterance);
},
@@ -587,8 +599,12 @@ class MicrophoneInput {
// Set status to activating at the start of initialization
this.status = Status.ACTIVATING;
try {
+ // get selected device from microphone settings
+ const selectedDevice = microphoneSettingStore.getSelectedDevice();
+
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
+ deviceId: selectedDevice && selectedDevice.deviceId ? { exact: selectedDevice.deviceId } : undefined,
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
diff --git a/webui/components/settings/speech/microphone-setting-store.js b/webui/components/settings/speech/microphone-setting-store.js
new file mode 100644
index 000000000..15e623f80
--- /dev/null
+++ b/webui/components/settings/speech/microphone-setting-store.js
@@ -0,0 +1,53 @@
+import { createStore } from "/js/AlpineStore.js";
+
+const model = {
+
+
+ devices: [],
+ selectedDevice: "",
+
+ async init() {
+ // Load selected device from localStorage if present
+ const saved = localStorage.getItem('microphoneSelectedDevice');
+ await this.loadDevices();
+ if (saved && this.devices.some(d => d.deviceId === saved)) {
+ this.selectedDevice = saved;
+ }
+ },
+
+ async loadDevices() {
+ // Get media devices
+ const devices = await navigator.mediaDevices.enumerateDevices();
+ // Filter for audio input (microphones)
+ this.devices = devices.filter(d => d.kind === "audioinput" && d.deviceId);
+ // Set selected device to first available, if any
+ this.selectedDevice = this.devices.length > 0 ? this.devices[0].deviceId : "";
+ },
+
+ requestPermission() {
+ navigator.mediaDevices.getUserMedia({ audio: true });
+ this.loadDevices();
+ },
+
+ async selectDevice(deviceId) {
+ this.selectedDevice = deviceId;
+ this.onSelectDevice();
+ },
+
+ async onSelectDevice() {
+ localStorage.setItem('microphoneSelectedDevice', this.selectedDevice);
+ },
+
+ getSelectedDevice() {
+ let device = this.devices.find(d => d.deviceId === this.selectedDevice);
+ if (!device && this.devices.length > 0) {
+ device = this.devices.find(d => d.deviceId === "default") || this.devices[0];
+ }
+ return device;
+ }
+
+};
+
+const store = createStore("microphoneSetting", model);
+
+export { store };
diff --git a/webui/components/settings/speech/microphone.html b/webui/components/settings/speech/microphone.html
new file mode 100644
index 000000000..2a6b97a16
--- /dev/null
+++ b/webui/components/settings/speech/microphone.html
@@ -0,0 +1,41 @@
+<html>
+
+<head>
+ <title>Microhone settings</title>
+
+ <!-- Import the alpine store -->
+ <script type="module">
+ import { store } from "/components/settings/speech/microphone-setting-store.js";
+ console.log("microphone-setting-store.js loaded");
+ </script>
+</head>
+
+<body>
+
+ <!-- This construct of x-data + x-if is used to ensure the component is only rendered when the store is available -->
+ <div x-data>
+ <template x-if="$store.microphoneSetting">
+
+ <div>
+ <select x-model="$store.microphoneSetting.selectedDevice"
+ @change="$store.microphoneSetting.onSelectDevice()"
+ x-show="$store.microphoneSetting.devices.length > 0">
+ <template x-for="option in $store.microphoneSetting.devices" :key="option.deviceId">
+ <option :value="option.deviceId" x-text="option.label"
+ :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>
+ </div>
+
+ </template>
+ </div>
+
+ <!-- Optional style for the component -->
+ <style>
+ </style>
+
+</body>
+
+</html>
\ No newline at end of file
diff --git a/webui/index.css b/webui/index.css
index edbde1443..36f1dcf90 100644
--- a/webui/index.css
+++ b/webui/index.css
@@ -938,6 +938,7 @@ pre {
border-radius: 8px;
max-width: 600px; /* Limits to ~5 columns at 120px each */
overflow: visible;
+ justify-items: end;
}
.attachment-item {
@@ -981,6 +982,8 @@ pre {
height: 100%;
object-fit: cover;
border-radius: 10px;
+ background: repeating-linear-gradient(45deg, #fff 0 10px, #e0e0e0 10px 20px);
+
}
/* File attachment styling */
@@ -1017,6 +1020,8 @@ pre {
height: 100%;
object-fit: cover;
border-radius: 10px;
+ background: repeating-linear-gradient(45deg, #fff 0 10px, #e0e0e0 10px 20px);
+
}
.attachment-image .attachment-preview {
diff --git a/webui/index.html b/webui/index.html
index d07691fbd..50a1c7611 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -586,9 +586,9 @@
<template x-for="(field, fieldIndex) in section.fields.filter(f => !f.hidden)" :key="fieldIndex">
<div :class="{'field': true, 'field-full': field.type === 'textarea'}">
- <div class="field-label">
- <div class="field-title" x-text="field.title"></div>
- <div class="field-description" x-html="field.description || ''"></div>
+ <div class="field-label" x-show="field.title || field.description">
+ <div class="field-title" x-text="field.title" x-show="field.title"></div>
+ <div class="field-description" x-html="field.description || ''" x-show="field.description"></div>
</div>
<div class="field-control">
@@ -661,6 +661,11 @@
</template>
</select>
</template>
+
+ <!-- HTML field -->
+ <template x-if="field.type === 'html'">
+ <div :class="field.classes" x-html="field.value"></div>
+ </template>
</div>
</div>
</template>
diff --git a/webui/js/components.js b/webui/js/components.js
index 819edfd1e..b1031b042 100644
--- a/webui/js/components.js
+++ b/webui/js/components.js
@@ -5,7 +5,22 @@
// cache object to store loaded components
const componentCache = {};
+// Lock map to prevent multiple simultaneous imports of the same component
+const importLocks = new Map();
+
export async function importComponent(path, targetElement) {
+ // Create a unique key for this import based on the target element
+ const lockKey = targetElement.id || targetElement.getAttribute('data-component-id') || targetElement;
+
+ // If this component is already being loaded, return early
+ if (importLocks.get(lockKey)) {
+ console.log(`Component ${path} is already being loaded for target`, targetElement);
+ return;
+ }
+
+ // Set the lock
+ importLocks.set(lockKey, true);
+
try {
if (!targetElement) {
throw new Error("Target element is required");
@@ -163,6 +178,9 @@ export async function importComponent(path, targetElement) {
} catch (error) {
console.error("Error importing component:", error);
throw error;
+ } finally {
+ // Release the lock when done, regardless of success or failure
+ importLocks.delete(lockKey);
}
}