From 7f4a05373ddb09bfc71e195fe0d3a1f5e276511d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 12 Jun 2026 18:07:24 +0100 Subject: [PATCH] refac --- CHANGELOG.md | 11 ++ cptr/frontend/package.json | 2 +- .../lib/components/Admin/AudioSettings.svelte | 12 +- .../src/lib/components/GroupTabBar.svelte | 18 +- ...NoteModal.svelte => VoiceMemoModal.svelte} | 0 cptr/frontend/src/lib/stores/audio.ts | 10 +- cptr/frontend/src/lib/stores/keybindings.ts | 12 +- cptr/frontend/src/routes/+layout.svelte | 6 +- cptr/routers/audio.py | 183 ++++++++++++++++-- pyproject.toml | 2 +- 10 files changed, 207 insertions(+), 49 deletions(-) rename cptr/frontend/src/lib/components/{VoiceNoteModal.svelte => VoiceMemoModal.svelte} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 326725b..3a7f618 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cptr/frontend/package.json b/cptr/frontend/package.json index 923b45b..3b44424 100644 --- a/cptr/frontend/package.json +++ b/cptr/frontend/package.json @@ -1,7 +1,7 @@ { "name": "frontend", "private": true, - "version": "0.3.1", + "version": "0.3.2", "type": "module", "scripts": { "dev": "vite dev", diff --git a/cptr/frontend/src/lib/components/Admin/AudioSettings.svelte b/cptr/frontend/src/lib/components/Admin/AudioSettings.svelte index d98c05f..c102200 100644 --- a/cptr/frontend/src/lib/components/Admin/AudioSettings.svelte +++ b/cptr/frontend/src/lib/components/Admin/AudioSettings.svelte @@ -11,7 +11,7 @@ let saving = $state(false); // Config state - let voiceNotesEnabled = $state(false); + let voiceMemosEnabled = $state(false); let transcribeEnabled = $state(true); let sttBaseUrl = $state('https://api.openai.com/v1'); let sttApiKey = $state(''); @@ -21,7 +21,7 @@ onMount(async () => { try { const config = await getAdminConfig(); - voiceNotesEnabled = config['audio.voice_notes_enabled'] === true; + voiceMemosEnabled = config['audio.voice_memos_enabled'] === true; transcribeEnabled = config['audio.transcribe_enabled'] !== false; sttBaseUrl = (config['audio.stt_base_url'] as string) || 'https://api.openai.com/v1'; sttModel = (config['audio.stt_model'] as string) || 'whisper-1'; @@ -34,7 +34,7 @@ saving = true; try { const cfg: Record = { - 'audio.voice_notes_enabled': voiceNotesEnabled, + 'audio.voice_memos_enabled': voiceMemosEnabled, 'audio.transcribe_enabled': transcribeEnabled, 'audio.stt_base_url': sttBaseUrl, 'audio.stt_model': sttModel @@ -61,12 +61,12 @@
{:else} -

Voice Notes

+

Voice Memos

Record voice memos from the "+" menu. diff --git a/cptr/frontend/src/lib/components/GroupTabBar.svelte b/cptr/frontend/src/lib/components/GroupTabBar.svelte index 546a8c4..cf4e553 100644 --- a/cptr/frontend/src/lib/components/GroupTabBar.svelte +++ b/cptr/frontend/src/lib/components/GroupTabBar.svelte @@ -21,14 +21,14 @@ } from '$lib/stores'; import { openChatTab } from '$lib/stores'; import { chatEnabled, streamingChatTabs } from '$lib/stores/chat'; - import { voiceNotesEnabled, showVoiceNote } from '$lib/stores/audio'; + 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 VoiceNoteModal from './VoiceNoteModal.svelte'; + import VoiceMemoModal from './VoiceMemoModal.svelte'; interface Props { group: EditorGroup; @@ -162,14 +162,14 @@ openTerminalTab(group.id); } }, - ...($voiceNotesEnabled + ...($voiceMemosEnabled ? [ { - label: 'Voice Note', + label: 'Voice Memo', icon: 'microphone', - shortcut: formatChord($keybindings.voiceNote), + shortcut: formatChord($keybindings.voiceMemo), onclick: () => { - showVoiceNote.set(true); + showVoiceMemo.set(true); } } ] @@ -405,11 +405,11 @@ /> {/if} -{#if $showVoiceNote} - showVoiceNote.set(false)} + onclose={() => showVoiceMemo.set(false)} /> {/if} diff --git a/cptr/frontend/src/lib/components/VoiceNoteModal.svelte b/cptr/frontend/src/lib/components/VoiceMemoModal.svelte similarity index 100% rename from cptr/frontend/src/lib/components/VoiceNoteModal.svelte rename to cptr/frontend/src/lib/components/VoiceMemoModal.svelte diff --git a/cptr/frontend/src/lib/stores/audio.ts b/cptr/frontend/src/lib/stores/audio.ts index 75a9966..2a1f268 100644 --- a/cptr/frontend/src/lib/stores/audio.ts +++ b/cptr/frontend/src/lib/stores/audio.ts @@ -1,21 +1,21 @@ /** - * Audio state: voice notes feature flag, transcription toggle, and modal visibility. + * Audio state: voice memos feature flag, transcription toggle, and modal visibility. */ import { writable } from 'svelte/store'; import { fetchJSON } from '$lib/apis'; -export const voiceNotesEnabled = writable(false); +export const voiceMemosEnabled = writable(false); export const transcribeEnabled = writable(true); -export const showVoiceNote = writable(false); +export const showVoiceMemo = writable(false); export async function refreshAudioState() { try { const data = await fetchJSON<{ config: Record }>( '/api/admin/config/audio' ); - voiceNotesEnabled.set(data.config?.['audio.voice_notes_enabled'] === true); + voiceMemosEnabled.set(data.config?.['audio.voice_memos_enabled'] === true); transcribeEnabled.set(data.config?.['audio.transcribe_enabled'] !== false); } catch { - voiceNotesEnabled.set(false); + voiceMemosEnabled.set(false); } } diff --git a/cptr/frontend/src/lib/stores/keybindings.ts b/cptr/frontend/src/lib/stores/keybindings.ts index 47dccdd..b09a04b 100644 --- a/cptr/frontend/src/lib/stores/keybindings.ts +++ b/cptr/frontend/src/lib/stores/keybindings.ts @@ -39,7 +39,7 @@ export const ACTION_IDS = [ 'openSettings', 'toggleSplit', 'toggleSidebar', - 'voiceNote' + 'voiceMemo' ] as const; export type ActionId = (typeof ACTION_IDS)[number]; @@ -58,7 +58,7 @@ export const ACTION_LABELS: Record = { openSettings: 'Open Settings', toggleSplit: 'Toggle Split', toggleSidebar: 'Toggle Sidebar', - voiceNote: 'Voice Note' + voiceMemo: 'Voice Memo' }; // ── Default bindings (browser-safe) ───────────────────────────── @@ -75,7 +75,7 @@ export const DEFAULT_KEYBINDINGS: Record = { openSettings: 'Cmd+.', toggleSplit: 'Cmd+\\', toggleSidebar: 'Cmd+Shift+S', - voiceNote: 'Cmd+Shift+M' + voiceMemo: 'Cmd+Shift+M' }; // ── Store ─────────────────────────────────────────────────────── @@ -238,7 +238,7 @@ export function executeAction( toggleQuickOpen?: () => void; toggleSettings?: () => void; toggleSearch?: () => void; - toggleVoiceNote?: () => void; + toggleVoiceMemo?: () => void; } ): boolean { switch (action) { @@ -305,8 +305,8 @@ export function executeAction( sidebarOpen.update((v) => !v); return true; - case 'voiceNote': - callbacks?.toggleVoiceNote?.(); + case 'voiceMemo': + callbacks?.toggleVoiceMemo?.(); return true; default: diff --git a/cptr/frontend/src/routes/+layout.svelte b/cptr/frontend/src/routes/+layout.svelte index 58b17d7..9573a42 100644 --- a/cptr/frontend/src/routes/+layout.svelte +++ b/cptr/frontend/src/routes/+layout.svelte @@ -225,10 +225,10 @@ toggleSearch: () => { showSearch.update((v) => !v); }, - toggleVoiceNote: () => { - import('$lib/stores/audio').then(({ voiceNotesEnabled, showVoiceNote }) => { + toggleVoiceMemo: () => { + import('$lib/stores/audio').then(({ voiceMemosEnabled, showVoiceMemo }) => { import('svelte/store').then(({ get }) => { - if (get(voiceNotesEnabled)) showVoiceNote.update((v) => !v); + if (get(voiceMemosEnabled)) showVoiceMemo.update((v) => !v); }); }); } diff --git a/cptr/routers/audio.py b/cptr/routers/audio.py index fe53248..7f4e914 100644 --- a/cptr/routers/audio.py +++ b/cptr/routers/audio.py @@ -1,8 +1,16 @@ -"""Audio router: STT transcription via Whisper-compatible API.""" +"""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 @@ -13,9 +21,18 @@ 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: @@ -28,11 +45,96 @@ def _get_user(request: Request) -> str: 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. - Reads credentials from audio.stt_* config keys (set in Admin → Audio). + 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) @@ -47,27 +149,72 @@ async def transcribe(file: UploadFile, request: Request): 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" - data = await file.read() + 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: - async with httpx.AsyncClient(timeout=httpx.Timeout(60)) 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() + # 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], - ) + 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") - - return {"text": resp.json().get("text", "")} + 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 diff --git a/pyproject.toml b/pyproject.toml index fc5c4c4..197ccec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"