Merge pull request #38 from open-webui/main

0.3.2
This commit is contained in:
Tim Baek authored and GitHub committed 2026-06-12 12:13:45 -05:00
commit c20fc3deed
15 files changed
+799 -20

No files matched your search

+11
View File
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.3.2] - 2026-06-12
### Added
- 🎙️ **Voice memos.** Record audio memos directly from the "+" menu or ⌘⇧M. Recordings are saved to your workspace as audio files with an auto-generated markdown transcript. Uses a save-first architecture with IndexedDB backup so your recording is never lost, even if the network drops. Transcription is powered by any OpenAI-compatible STT API (Whisper, etc.), configurable in Settings > Audio.
- 🔊 **Audio admin settings.** New Audio tab in admin settings with separate controls for enabling voice memos, toggling auto-transcription, and configuring STT credentials (base URL, API key, model).
### Changed
- 🎨 **Consistent admin input styling.** Standardized all input fields in the Configuration admin panel to match the Browser settings design. Uniform height, background, focus states, and label sizing across all admin tabs.
## [0.3.1] - 2026-06-12
### Added
+2
View File
@@ -7,6 +7,7 @@ from fastapi.staticfiles import StaticFiles
from cptr.routers import (
admin_router,
audio_router,
auth_router,
automations_router,
bridge_router,
@@ -209,6 +210,7 @@ async def get_config():
# Routers
app.include_router(admin_router)
app.include_router(audio_router)
app.include_router(auth_router)
app.include_router(automations_router)
app.include_router(bridge_router)
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "frontend",
"private": true,
"version": "0.3.1",
"version": "0.3.2",
"type": "module",
"scripts": {
"dev": "vite dev",
@@ -0,0 +1,152 @@
<script lang="ts">
import { toast } from 'svelte-sonner';
import ToggleSwitch from '../common/ToggleSwitch.svelte';
import Spinner from '../common/Spinner.svelte';
import { onMount } from 'svelte';
import { getAdminConfig, updateConfig } from '$lib/apis/admin';
import { t } from '$lib/i18n';
import { refreshAudioState } from '$lib/stores/audio';
let loading = $state(true);
let saving = $state(false);
// Config state
let voiceMemosEnabled = $state(false);
let transcribeEnabled = $state(true);
let quality = $state<'high' | 'medium' | 'low'>('high');
let sttBaseUrl = $state('https://api.openai.com/v1');
let sttApiKey = $state('');
let sttModel = $state('whisper-1');
let hasExistingKey = $state(false);
onMount(async () => {
try {
const config = await getAdminConfig();
voiceMemosEnabled = config['audio.voice_memos_enabled'] === true;
transcribeEnabled = config['audio.transcribe_enabled'] !== false;
const q = config['audio.recording_quality'];
if (q === 'medium' || q === 'low') quality = q;
else quality = 'high';
sttBaseUrl = (config['audio.stt_base_url'] as string) || 'https://api.openai.com/v1';
sttModel = (config['audio.stt_model'] as string) || 'whisper-1';
hasExistingKey = !!config['audio.stt_api_key'];
} catch {}
loading = false;
});
async function save() {
saving = true;
try {
const cfg: Record<string, unknown> = {
'audio.voice_memos_enabled': voiceMemosEnabled,
'audio.transcribe_enabled': transcribeEnabled,
'audio.recording_quality': quality,
'audio.stt_base_url': sttBaseUrl,
'audio.stt_model': sttModel
};
if (sttApiKey) {
cfg['audio.stt_api_key'] = sttApiKey;
}
await updateConfig(cfg);
if (sttApiKey) hasExistingKey = true;
toast.success($t('settings.saved'));
refreshAudioState();
} catch {
toast.error('Failed to save audio settings');
} finally {
saving = false;
}
}
</script>
<div class="flex flex-col min-h-full">
<h2 class="text-sm font-medium text-gray-900 dark:text-white mb-4">Audio</h2>
{#if loading}
<div class="flex justify-center py-8"><Spinner size={16} /></div>
{:else}
<!-- Voice Notes -->
<h3 class="text-xs text-gray-400 dark:text-gray-600 mb-2">Voice Memos</h3>
<div class="flex flex-col gap-2.5">
<label class="flex items-center justify-between cursor-pointer">
<span class="text-xs text-gray-600 dark:text-gray-400">Enable Voice Memos</span>
<ToggleSwitch value={voiceMemosEnabled} onchange={(v) => { voiceMemosEnabled = v; }} />
</label>
<p class="text-[11px] text-gray-400 dark:text-gray-600 -mt-1">
Record voice memos from the "+" menu.
</p>
<label class="flex items-center justify-between cursor-pointer">
<span class="text-xs text-gray-600 dark:text-gray-400">Auto-transcribe</span>
<ToggleSwitch value={transcribeEnabled} onchange={(v) => { transcribeEnabled = v; }} />
</label>
<p class="text-[11px] text-gray-400 dark:text-gray-600 -mt-1">
{transcribeEnabled ? 'Recordings are transcribed to markdown via STT.' : 'Recordings are saved as audio only.'}
</p>
<div class="flex items-center justify-between">
<span class="text-xs text-gray-600 dark:text-gray-400">Recording quality</span>
<select
bind:value={quality}
class="bg-transparent text-xs text-gray-600 dark:text-gray-400 outline-none cursor-pointer"
>
<option value="high">High (128kbps)</option>
<option value="medium">Medium (64kbps)</option>
<option value="low">Low (32kbps)</option>
</select>
</div>
<p class="text-[11px] text-gray-400 dark:text-gray-600 -mt-1">
{quality === 'high' ? 'Best quality, larger files.' : quality === 'medium' ? 'Balanced quality and size.' : 'Smallest files, optimized for speech.'}
</p>
</div>
<!-- Speech-to-Text -->
<h3 class="text-xs text-gray-400 dark:text-gray-600 mb-2 mt-5">Speech-to-Text</h3>
<div class="flex flex-col gap-2.5">
<div>
<label class="text-xs text-gray-600 dark:text-gray-400" for="stt-base-url">Base URL</label>
<input
id="stt-base-url"
type="text"
bind:value={sttBaseUrl}
placeholder="https://api.openai.com/v1"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
/>
</div>
<div>
<label class="text-xs text-gray-600 dark:text-gray-400" for="stt-api-key">API Key</label>
<input
id="stt-api-key"
type="password"
bind:value={sttApiKey}
placeholder={hasExistingKey ? '••••••••' : 'sk-...'}
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
/>
</div>
<div>
<label class="text-xs text-gray-600 dark:text-gray-400" for="stt-model">Model</label>
<input
id="stt-model"
type="text"
bind:value={sttModel}
placeholder="whisper-1"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
/>
</div>
<p class="text-[11px] text-gray-400 dark:text-gray-600">
Compatible with OpenAI's audio/transcriptions API.
</p>
</div>
<!-- Save -->
<div class="mt-auto pt-6 flex justify-end">
<button
class="text-[13px] text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white transition-colors duration-100 disabled:opacity-50"
onclick={() => save()}
disabled={saving}
>{$t('settings.save')}</button>
</div>
{/if}
</div>
@@ -127,12 +127,12 @@
<p class="text-[11px] text-gray-400 dark:text-gray-600 mt-2">{$t('admin.webAutoHint')}</p>
{:else if provider === 'exa'}
<div class="mt-3">
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webExaKey')}</label
>
<input
type="password"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="exa-..."
bind:value={exaKey}
onblur={() => saveKey('web.exa_api_key', exaKey)}
@@ -144,12 +144,12 @@
</div>
{:else if provider === 'tavily'}
<div class="mt-3">
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webTavilyKey')}</label
>
<input
type="password"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="tvly-..."
bind:value={tavilyKey}
onblur={() => saveKey('web.tavily_api_key', tavilyKey)}
@@ -161,12 +161,12 @@
</div>
{:else if provider === 'brave'}
<div class="mt-3">
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webBraveKey')}</label
>
<input
type="password"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="BSA..."
bind:value={braveKey}
onblur={() => saveKey('web.brave_api_key', braveKey)}
@@ -182,12 +182,12 @@
</p>
{:else if provider === 'perplexity'}
<div class="mt-3">
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webPerplexityKey')}</label
>
<input
type="password"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="pplx-..."
bind:value={perplexityKey}
onblur={() => saveKey('web.perplexity_api_key', perplexityKey)}
@@ -200,12 +200,12 @@
{:else if provider === 'chat_completions'}
<div class="mt-3 space-y-3">
<div>
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webCcBaseUrl')}</label
>
<input
type="text"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="https://api.perplexity.ai/v1"
bind:value={ccBaseUrl}
onblur={() => saveKey('web.chat_completions_base_url', ccBaseUrl)}
@@ -213,12 +213,12 @@
/>
</div>
<div>
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webCcKey')}</label
>
<input
type="password"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="sk-..."
bind:value={ccKey}
onblur={() => saveKey('web.chat_completions_api_key', ccKey)}
@@ -226,12 +226,12 @@
/>
</div>
<div>
<label class="block text-[13px] text-gray-700 dark:text-gray-300 mb-1"
<label class="text-xs text-gray-600 dark:text-gray-400 mb-1"
>{$t('admin.webCcModel')}</label
>
<input
type="text"
class="w-full text-[13px] bg-gray-50 dark:bg-white/4 border border-gray-200 dark:border-white/8 rounded-lg px-2.5 py-1.5 outline-none text-gray-700 dark:text-gray-300 placeholder:text-gray-400 dark:placeholder:text-gray-600"
class="w-full mt-1 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors"
placeholder="sonar-pro"
bind:value={ccModel}
onblur={() => saveKey('web.chat_completions_model', ccModel)}
@@ -21,12 +21,14 @@
} from '$lib/stores';
import { openChatTab } from '$lib/stores';
import { chatEnabled, streamingChatTabs } from '$lib/stores/chat';
import { voiceMemosEnabled, showVoiceMemo } from '$lib/stores/audio';
import { keybindings, formatChord } from '$lib/stores/keybindings';
import Icon from './Icon.svelte';
import Spinner from './common/Spinner.svelte';
import DropdownMenu from './DropdownMenu.svelte';
import { tooltip } from '$lib/tooltip';
import { t } from '$lib/i18n';
import VoiceMemoModal from './VoiceMemoModal.svelte';
interface Props {
group: EditorGroup;
@@ -159,7 +161,19 @@
onclick: () => {
openTerminalTab(group.id);
}
}
},
...($voiceMemosEnabled
? [
{
label: 'Voice Memo',
icon: 'microphone',
shortcut: formatChord($keybindings.voiceMemo),
onclick: () => {
showVoiceMemo.set(true);
}
}
]
: [])
]);
const contextMenuItems = $derived.by(() => {
@@ -391,6 +405,14 @@
/>
{/if}
{#if $showVoiceMemo}
<VoiceMemoModal
workspace={$activeWorkspace?.path ?? ''}
directory={$activeWorkspace?.fileBrowserCwd ?? $activeWorkspace?.path ?? ''}
onclose={() => showVoiceMemo.set(false)}
/>
{/if}
<style>
@reference "../../app.css";
@@ -338,5 +338,12 @@
<path d="M13 2.04932C13 2.04932 16 6 16 12C16 18 13 21.9507 13 21.9507" />
<path d="M11 21.9507C11 21.9507 8 18 8 12C8 6 11 2.04932 11 2.04932" />
<path d="M2 12H22" />
{:else if name === 'microphone'}
<path d="M12 1C10.3431 1 9 2.34315 9 4V12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12V4C15 2.34315 13.6569 1 12 1Z" />
<path d="M5 10V12C5 15.866 8.13401 19 12 19C15.866 19 19 15.866 19 12V10" />
<line x1="12" y1="19" x2="12" y2="23" />
<line x1="8" y1="23" x2="16" y2="23" />
{:else if name === 'stop'}
<rect x="6" y="6" width="12" height="12" rx="1" fill="currentColor" />
{/if}
</svg>
@@ -10,6 +10,7 @@
import Connections from './Admin/Connections.svelte';
import Models from './Admin/Models.svelte';
import Messaging from './Admin/Messaging.svelte';
import AudioSettings from './Admin/AudioSettings.svelte';
import AdminSettings from './Admin/Settings.svelte';
import { session } from '$lib/session';
import { t } from '$lib/i18n';
@@ -24,6 +25,7 @@
| 'connections'
| 'models'
| 'messaging'
| 'audio'
| 'admin_settings';
interface Props {
@@ -49,6 +51,7 @@
{ id: 'connections', label: $t('admin.connections'), icon: 'plug' },
{ id: 'models', label: $t('admin.models'), icon: 'cube' },
{ id: 'messaging', label: $t('admin.messaging'), icon: 'chat-bubble' },
{ id: 'audio', label: 'Audio', icon: 'microphone' },
{ id: 'browser', label: 'Browser', icon: 'browser' },
{ id: 'admin_settings', label: $t('settings.configuration'), icon: 'shield' }
]);
@@ -123,6 +126,8 @@
<Models />
{:else if activeTab === 'messaging'}
<Messaging />
{:else if activeTab === 'audio'}
<AudioSettings />
{:else if activeTab === 'admin_settings'}
<AdminSettings />
{/if}
@@ -0,0 +1,306 @@
<script lang="ts">
import { onMount } from 'svelte';
import { get } from 'svelte/store';
import { toast } from 'svelte-sonner';
import Modal from './Modal.svelte';
import Spinner from './common/Spinner.svelte';
import { writeFile } from '$lib/apis/files';
import { fetchJSON } from '$lib/apis';
import { transcribeEnabled, recordingQuality, QUALITY_BITRATES } from '$lib/stores/audio';
interface Props {
workspace: string;
directory: string;
onclose: () => void;
}
let { workspace, directory, onclose }: Props = $props();
type Phase = 'recording' | 'processing' | 'naming' | 'done';
let phase = $state<Phase>('recording');
let elapsed = $state(0);
let transcript = $state('');
let fileName = $state('');
let audioBlob = $state<Blob | null>(null);
let waveformBars = $state<number[]>(new Array(40).fill(2));
let mediaRecorder: MediaRecorder | null = null;
let audioContext: AudioContext | null = null;
let analyser: AnalyserNode | null = null;
let animFrame = 0;
let timerInterval = 0;
let chunks: Blob[] = [];
let cancelled = false;
let filenameInput: HTMLInputElement | undefined = $state();
// ── IndexedDB ───────────────────────────────────────────────
const IDB_NAME = 'cptr-voice-notes';
const IDB_STORE = 'pending';
function openIDB(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const req = indexedDB.open(IDB_NAME, 1);
req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE, { keyPath: 'id' });
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
async function saveToIDB(id: string, blob: Blob) {
const db = await openIDB();
const tx = db.transaction(IDB_STORE, 'readwrite');
tx.objectStore(IDB_STORE).put({ id, blob, directory, createdAt: Date.now() });
return new Promise<void>((r, j) => {
tx.oncomplete = () => r();
tx.onerror = () => j(tx.error);
});
}
async function clearFromIDB(id: string) {
const db = await openIDB();
db.transaction(IDB_STORE, 'readwrite').objectStore(IDB_STORE).delete(id);
}
// ── Helpers ──────────────────────────────────────────────────
function generateId(): string {
const d = new Date();
const p = (n: number) => n.toString().padStart(2, '0');
return `recording-${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
}
function fmt(s: number): string {
return `${Math.floor(s / 60)}:${(s % 60).toString().padStart(2, '0')}`;
}
function mimeType(): string {
for (const t of ['audio/webm;codecs=opus', 'audio/webm', 'audio/mp4', 'audio/ogg'])
if (MediaRecorder.isTypeSupported(t)) return t;
return 'audio/webm';
}
function ext(blob: Blob): string {
return blob.type.includes('mp4') ? 'mp4' : blob.type.includes('ogg') ? 'ogg' : 'webm';
}
// ── Recording ───────────────────────────────────────────────
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
analyser.fftSize = 64;
audioContext.createMediaStreamSource(stream).connect(analyser);
const buf = new Uint8Array(analyser.frequencyBinCount);
const tick = () => {
if (!analyser) return;
analyser.getByteFrequencyData(buf);
waveformBars = Array.from({ length: 40 }, (_, i) =>
Math.max(2, (buf[Math.floor((i / 40) * buf.length)] / 255) * 20)
);
animFrame = requestAnimationFrame(tick);
};
tick();
chunks = [];
mediaRecorder = new MediaRecorder(stream, {
mimeType: mimeType(),
audioBitsPerSecond: QUALITY_BITRATES[get(recordingQuality)]
});
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) chunks.push(e.data);
};
mediaRecorder.onstop = () => {
stream.getTracks().forEach((t) => t.stop());
cancelAnimationFrame(animFrame);
audioContext?.close();
if (cancelled) return;
audioBlob = new Blob(chunks, { type: mediaRecorder?.mimeType || 'audio/webm' });
handleComplete();
};
mediaRecorder.start(250);
elapsed = 0;
timerInterval = window.setInterval(() => elapsed++, 1000);
} catch {
toast.error('Could not access microphone');
onclose();
}
}
function stop() {
if (timerInterval) clearInterval(timerInterval);
if (mediaRecorder?.state !== 'inactive') mediaRecorder?.stop();
}
function cancel() {
cancelled = true;
stop();
onclose();
}
// ── Save-first flow ─────────────────────────────────────────
async function handleComplete() {
if (!audioBlob) return;
const id = generateId();
fileName = id;
phase = 'processing';
// 1. IndexedDB safety
try {
await saveToIDB(id, audioBlob);
} catch {}
// 2. Upload audio
const audioName = `${id}.${ext(audioBlob)}`;
const form = new FormData();
form.append('directory', directory);
form.append('file', new File([audioBlob], audioName, { type: audioBlob.type }));
try {
await fetchJSON('/api/workspace/files/upload', { method: 'POST', body: form });
await clearFromIDB(id).catch(() => {});
} catch {
toast.error('Audio saved locally. Server upload failed.');
}
// 3. Transcribe (if enabled)
if (get(transcribeEnabled)) {
try {
const tf = new FormData();
tf.append('file', new File([audioBlob], audioName, { type: audioBlob.type }));
const res = await fetchJSON<{ text: string }>('/api/audio/transcribe', {
method: 'POST',
body: tf
});
transcript = res.text || '';
} catch (err: any) {
transcript = '';
const msg = err?.message || '';
if (msg.includes('not configured')) {
toast.error('STT not configured. Set up in Settings → Audio.');
}
}
}
phase = 'naming';
await new Promise((r) => setTimeout(r, 50));
filenameInput?.focus();
filenameInput?.select();
}
async function save() {
if (!fileName.trim()) return;
const n = fileName.trim();
const audioFile = `${n}.${ext(audioBlob!)}`;
const mdPath = `${directory}/${n}.md`;
const date = new Date().toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit'
});
let md = `# ${date}\n\n*${fmt(elapsed)}*\n\n`;
if (transcript) md += `${transcript}\n\n`;
md += `---\n[🔊 ${audioFile}](${audioFile})\n`;
try {
await writeFile(mdPath, md);
phase = 'done';
setTimeout(onclose, 500);
} catch {
toast.error('Failed to write transcript. Audio is safe.');
}
}
onMount(() => {
startRecording();
return () => {
if (timerInterval) clearInterval(timerInterval);
cancelAnimationFrame(animFrame);
if (mediaRecorder?.state !== 'inactive') mediaRecorder?.stop();
audioContext?.close().catch(() => {});
};
});
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<Modal onclose={phase === 'recording' ? cancel : onclose} class="w-full max-w-[260px] mx-4">
<div
class="px-4 py-3.5"
onkeydown={(e) => {
if (phase === 'naming' && e.key === 'Enter') {
e.preventDefault();
save();
}
}}
>
{#if phase === 'recording'}
<div class="flex items-center gap-2.5 mt-0.5">
<div class="flex items-end gap-px h-4 flex-1">
{#each waveformBars as h}
<div
class="flex-1 rounded-full bg-gray-300 dark:bg-gray-700 transition-all duration-75"
style="height: {h}px"
></div>
{/each}
</div>
<span class="text-xs font-mono text-gray-400 dark:text-gray-600 tabular-nums shrink-0"
>{fmt(elapsed)}</span
>
</div>
<div class="flex items-center justify-end gap-3 mt-3">
<button
class="text-[13px] text-gray-400 dark:text-gray-600 hover:text-gray-600 dark:hover:text-gray-400 transition-colors"
onclick={cancel}>Cancel</button
>
<button
class="text-[13px] text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white transition-colors duration-100"
onclick={stop}>Done →</button
>
</div>
{:else if phase === 'processing'}
<div class="flex items-center gap-2">
<Spinner size={12} />
<span class="text-xs text-gray-400 dark:text-gray-600">Processing…</span>
</div>
{:else if phase === 'naming'}
{#if transcript}
<p class="text-xs text-gray-400 dark:text-gray-600 line-clamp-2 mb-2">
{transcript.slice(0, 140)}{transcript.length > 140 ? '…' : ''}
</p>
{/if}
<label class="text-[10px] text-gray-400 dark:text-gray-600">Filename</label>
<input
bind:this={filenameInput}
type="text"
bind:value={fileName}
placeholder="recording-name"
autocomplete="off"
spellcheck="false"
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5"
/>
<p class="text-[10px] text-gray-300 dark:text-gray-700 mt-0.5">
{directory.split('/').pop()}
</p>
<div class="flex justify-end mt-3">
<button
onclick={save}
disabled={!fileName.trim()}
class="text-[13px] text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white transition-colors duration-100 disabled:opacity-30 disabled:pointer-events-none"
>Save →</button
>
</div>
{:else if phase === 'done'}
<p class="text-xs text-gray-400 dark:text-gray-600">Saved ✓</p>
{/if}
</div>
</Modal>
+33
View File
@@ -0,0 +1,33 @@
/**
* Audio state: voice memos feature flag, transcription toggle, recording quality, and modal visibility.
*/
import { writable } from 'svelte/store';
import { fetchJSON } from '$lib/apis';
export type RecordingQuality = 'high' | 'medium' | 'low';
export const QUALITY_BITRATES: Record<RecordingQuality, number> = {
high: 128000,
medium: 64000,
low: 32000
};
export const voiceMemosEnabled = writable<boolean>(false);
export const transcribeEnabled = writable<boolean>(true);
export const recordingQuality = writable<RecordingQuality>('high');
export const showVoiceMemo = writable<boolean>(false);
export async function refreshAudioState() {
try {
const data = await fetchJSON<{ config: Record<string, unknown> }>(
'/api/admin/config/audio'
);
voiceMemosEnabled.set(data.config?.['audio.voice_memos_enabled'] === true);
transcribeEnabled.set(data.config?.['audio.transcribe_enabled'] !== false);
const q = data.config?.['audio.recording_quality'];
if (q === 'medium' || q === 'low') recordingQuality.set(q);
else recordingQuality.set('high');
} catch {
voiceMemosEnabled.set(false);
}
}
+11 -3
View File
@@ -38,7 +38,8 @@ export const ACTION_IDS = [
'searchAll',
'openSettings',
'toggleSplit',
'toggleSidebar'
'toggleSidebar',
'voiceMemo'
] as const;
export type ActionId = (typeof ACTION_IDS)[number];
@@ -56,7 +57,8 @@ export const ACTION_LABELS: Record<ActionId, string> = {
searchAll: 'Search',
openSettings: 'Open Settings',
toggleSplit: 'Toggle Split',
toggleSidebar: 'Toggle Sidebar'
toggleSidebar: 'Toggle Sidebar',
voiceMemo: 'Voice Memo'
};
// ── Default bindings (browser-safe) ─────────────────────────────
@@ -72,7 +74,8 @@ export const DEFAULT_KEYBINDINGS: Record<ActionId, string> = {
searchAll: 'Cmd+Shift+F',
openSettings: 'Cmd+.',
toggleSplit: 'Cmd+\\',
toggleSidebar: 'Cmd+Shift+S'
toggleSidebar: 'Cmd+Shift+S',
voiceMemo: 'Cmd+Shift+M'
};
// ── Store ───────────────────────────────────────────────────────
@@ -235,6 +238,7 @@ export function executeAction(
toggleQuickOpen?: () => void;
toggleSettings?: () => void;
toggleSearch?: () => void;
toggleVoiceMemo?: () => void;
}
): boolean {
switch (action) {
@@ -301,6 +305,10 @@ export function executeAction(
sidebarOpen.update((v) => !v);
return true;
case 'voiceMemo':
callbacks?.toggleVoiceMemo?.();
return true;
default:
return false;
}
+11
View File
@@ -42,6 +42,7 @@
import { gitStatusStore } from '$lib/stores/gitStatus.svelte';
import { t } from '$lib/i18n';
import { refreshChatState, bindGlobalChatListener } from '$lib/stores/chat';
import { refreshAudioState } from '$lib/stores/audio';
let { children } = $props();
let showSettings = $state(false);
@@ -147,6 +148,7 @@
authState = 'authenticated';
initState();
refreshChatState();
refreshAudioState();
// Check for version updates (admin only, after session is set)
checkForUpdates();
@@ -160,6 +162,7 @@
authState = 'authenticated';
initState();
refreshChatState();
refreshAudioState();
}
}
@@ -177,6 +180,7 @@
authState = 'authenticated';
initState();
refreshChatState();
refreshAudioState();
return;
}
} catch {}
@@ -220,6 +224,13 @@
},
toggleSearch: () => {
showSearch.update((v) => !v);
},
toggleVoiceMemo: () => {
import('$lib/stores/audio').then(({ voiceMemosEnabled, showVoiceMemo }) => {
import('svelte/store').then(({ get }) => {
if (get(voiceMemosEnabled)) showVoiceMemo.update((v) => !v);
});
});
}
});
}
+2
View File
@@ -1,6 +1,7 @@
"""cptr API routers."""
from cptr.routers.admin import router as admin_router
from cptr.routers.audio import router as audio_router
from cptr.routers.auth import router as auth_router
from cptr.routers.automations import router as automations_router
from cptr.routers.bridge import router as bridge_router
@@ -18,6 +19,7 @@ from cptr.routers.workspace import router as workspace_router
__all__ = [
"admin_router",
"audio_router",
"auth_router",
"automations_router",
"bridge_router",
+220
View File
@@ -0,0 +1,220 @@
"""Audio router: STT transcription via Whisper-compatible API.
Supports optional pydub/ffmpeg for compressing and splitting long recordings.
If pydub is not installed, raw audio is sent directly (works for < 25MB files).
"""
from __future__ import annotations
import asyncio
import logging
import os
import tempfile
from pathlib import Path
import httpx
from fastapi import APIRouter, HTTPException, Request, UploadFile
from cptr.models import Config
from cptr.utils.config import _get_jwt_secret, check_access
from cptr.utils.crypto import decrypt_key
logger = logging.getLogger(__name__)
# Optional pydub for long audio handling
try:
from pydub import AudioSegment # type: ignore[import-untyped]
HAS_PYDUB = True
except ImportError:
HAS_PYDUB = False
router = APIRouter(prefix="/api/audio", tags=["audio"])
COOKIE_NAME = "cptr_session"
MAX_FILE_SIZE = 25 * 1024 * 1024 # 25MB Whisper API limit
def _get_user(request: Request) -> str:
"""Extract user_id from cookie, raise 401 if not authenticated."""
token = request.cookies.get(COOKIE_NAME)
client_host = request.client.host if request.client else "127.0.0.1"
auth = check_access(client_host=client_host, jwt_token=token)
if not auth or not auth.user_id:
raise HTTPException(401, "authentication required")
return auth.user_id
# ── Audio processing (optional, requires pydub + ffmpeg) ─────────
def compress_audio(file_path: str) -> str:
"""Compress audio to 16kHz mono 32kbps MP3 if larger than MAX_FILE_SIZE."""
if not HAS_PYDUB or os.path.getsize(file_path) <= MAX_FILE_SIZE:
return file_path
audio = AudioSegment.from_file(file_path)
audio = audio.set_frame_rate(16000).set_channels(1)
base, _ = os.path.splitext(file_path)
compressed_path = f"{base}_compressed.mp3"
audio.export(compressed_path, format="mp3", bitrate="32k")
return compressed_path
def split_audio(file_path: str, max_bytes: int) -> list[str]:
"""Split audio into chunks not exceeding max_bytes.
Returns a list of chunk file paths. If audio fits, returns [file_path].
"""
file_size = os.path.getsize(file_path)
if file_size <= max_bytes:
return [file_path]
if not HAS_PYDUB:
return [file_path] # Can't split without pydub
audio = AudioSegment.from_file(file_path)
duration_ms = len(audio)
approx_chunk_ms = max(int(duration_ms * (max_bytes / file_size)) - 1000, 1000)
chunks: list[str] = []
start = 0
i = 0
base, _ = os.path.splitext(file_path)
while start < duration_ms:
end = min(start + approx_chunk_ms, duration_ms)
chunk = audio[start:end]
chunk_path = f"{base}_chunk_{i}.mp3"
chunk.export(chunk_path, format="mp3", bitrate="32k")
# Halve chunk duration if still too large
while os.path.getsize(chunk_path) > max_bytes and (end - start) > 5000:
end = start + ((end - start) // 2)
chunk = audio[start:end]
chunk.export(chunk_path, format="mp3", bitrate="32k")
if os.path.getsize(chunk_path) > max_bytes:
os.remove(chunk_path)
raise RuntimeError("Audio chunk cannot be reduced below max file size.")
chunks.append(chunk_path)
start = end
i += 1
return chunks
# ── Transcription ────────────────────────────────────────────────
async def _transcribe_chunk(
data: bytes,
filename: str,
content_type: str,
base_url: str,
api_key: str,
model: str,
) -> str:
"""Send a single audio chunk to the STT API."""
async with httpx.AsyncClient(timeout=httpx.Timeout(120)) as client:
resp = await client.post(
f"{base_url}/audio/transcriptions",
headers={"Authorization": f"Bearer {api_key}"},
files={"file": (filename, data, content_type)},
data={"model": model},
)
resp.raise_for_status()
return resp.json().get("text", "")
@router.post("/transcribe")
async def transcribe(file: UploadFile, request: Request):
"""Send audio to a Whisper-compatible STT API, return transcript text.
If pydub + ffmpeg are available and the file is large, it will be
compressed and split into chunks that are transcribed concurrently.
"""
_get_user(request)
api_key_encrypted = await Config.get("audio.stt_api_key")
if not api_key_encrypted:
raise HTTPException(
400,
"Speech-to-text not configured. Set up in Settings → Audio.",
)
api_key = decrypt_key(api_key_encrypted, _get_jwt_secret())
base_url = (await Config.get("audio.stt_base_url")) or "https://api.openai.com/v1"
model = (await Config.get("audio.stt_model")) or "whisper-1"
raw_data = await file.read()
filename = file.filename or "audio.webm"
content_type = file.content_type or "audio/webm"
# Small file: send directly, no temp files needed
if len(raw_data) <= MAX_FILE_SIZE or not HAS_PYDUB:
try:
text = await _transcribe_chunk(raw_data, filename, content_type, base_url, api_key, model)
return {"text": text}
except httpx.HTTPStatusError as exc:
detail = f"STT API error: {exc.response.status_code}"
if len(raw_data) > MAX_FILE_SIZE and not HAS_PYDUB:
detail += ". Recording is too large. Install ffmpeg and pydub for automatic splitting."
logger.warning("[transcribe] %s: %s", detail, exc.response.text[:500])
raise HTTPException(502, detail)
except httpx.ConnectError:
raise HTTPException(502, "Could not connect to STT API")
# Large file: compress → split → transcribe chunks concurrently
tmp_dir = tempfile.mkdtemp(prefix="cptr-stt-")
file_path = os.path.join(tmp_dir, filename)
Path(file_path).write_bytes(raw_data)
chunk_paths: list[str] = []
try:
# Compress
compressed = await asyncio.to_thread(compress_audio, file_path)
# Split
chunk_paths = await asyncio.to_thread(split_audio, compressed, MAX_FILE_SIZE)
# Transcribe all chunks concurrently
async def _do_chunk(path: str) -> str:
chunk_data = await asyncio.to_thread(Path(path).read_bytes)
chunk_name = os.path.basename(path)
return await _transcribe_chunk(chunk_data, chunk_name, "audio/mpeg", base_url, api_key, model)
tasks = [_do_chunk(p) for p in chunk_paths]
# Use gather to preserve order (as_completed doesn't guarantee it)
results = await asyncio.gather(*tasks)
return {"text": " ".join(r for r in results if r)}
except httpx.HTTPStatusError as exc:
logger.warning("[transcribe] STT API error %s: %s", exc.response.status_code, exc.response.text[:500])
raise HTTPException(502, f"STT API error: {exc.response.status_code}")
except httpx.ConnectError:
raise HTTPException(502, "Could not connect to STT API")
except RuntimeError as exc:
raise HTTPException(400, str(exc))
finally:
# Clean up temp files
for p in chunk_paths:
if p != file_path and os.path.isfile(p):
try:
os.remove(p)
except OSError:
pass
# Clean up compressed file if different from original
if "compressed" in locals() and compressed != file_path and os.path.isfile(compressed):
try:
os.remove(compressed)
except OSError:
pass
try:
os.remove(file_path)
os.rmdir(tmp_dir)
except OSError:
pass
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "cptr"
version = "0.3.1"
version = "0.3.2"
description = "Your computer, from anywhere. Code, manage, and control your machine from the web."
license = {file = "LICENSE"}
readme = "README.md"