mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-27 06:05:14 -07:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ac663fd0a | ||
|
|
52f8d8dd38 | ||
|
|
fb1e16d2ce | ||
|
|
f750596364 | ||
|
|
91cd6df108 | ||
|
|
484a39ad9f | ||
|
|
3bfcbdc819 | ||
|
|
190bc5e8a8 | ||
|
|
80af641b61 | ||
|
|
6936789a88 | ||
|
|
f3eca34d33 |
+3
-112
@@ -7,8 +7,9 @@ on:
|
|||||||
- main
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
quality:
|
frontend-quality:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
@@ -21,115 +22,5 @@ jobs:
|
|||||||
- name: Typecheck app + web
|
- name: Typecheck app + web
|
||||||
run: bun run typecheck
|
run: bun run typecheck
|
||||||
|
|
||||||
- name: Build web
|
- name: Build web smoke test
|
||||||
run: bun run build:web
|
run: bun run build:web
|
||||||
|
|
||||||
- name: Upload web build
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: web-dist
|
|
||||||
path: web/dist
|
|
||||||
retention-days: 1
|
|
||||||
|
|
||||||
unit:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 22
|
|
||||||
|
|
||||||
- name: Setup Bun
|
|
||||||
uses: oven-sh/setup-bun@v2
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
|
|
||||||
- name: Cache Playwright browsers
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ~/.cache/ms-playwright
|
|
||||||
key: playwright-${{ runner.os }}-${{ hashFiles('bun.lock') }}
|
|
||||||
|
|
||||||
- name: Install Chromium
|
|
||||||
run: bunx playwright install chromium --with-deps
|
|
||||||
|
|
||||||
- name: Vitest (unit + browser)
|
|
||||||
run: bunx vitest run
|
|
||||||
|
|
||||||
e2e:
|
|
||||||
# Informational while the suite beds in; flip to blocking once it has
|
|
||||||
# a sustained green run.
|
|
||||||
continue-on-error: true
|
|
||||||
needs: quality
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 22
|
|
||||||
|
|
||||||
- name: Setup Bun
|
|
||||||
uses: oven-sh/setup-bun@v2
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.12'
|
|
||||||
cache: pip
|
|
||||||
cache-dependency-path: backend/requirements-ci.txt
|
|
||||||
|
|
||||||
- name: Install backend (CPU)
|
|
||||||
run: |
|
|
||||||
pip install torch --index-url https://download.pytorch.org/whl/cpu
|
|
||||||
pip install -r backend/requirements-ci.txt
|
|
||||||
|
|
||||||
- name: Cache Playwright browsers
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ~/.cache/ms-playwright
|
|
||||||
key: playwright-${{ runner.os }}-${{ hashFiles('bun.lock') }}
|
|
||||||
|
|
||||||
- name: Install Chromium
|
|
||||||
run: bunx playwright install chromium --with-deps
|
|
||||||
|
|
||||||
- name: Playwright E2E
|
|
||||||
run: bunx playwright test -c e2e
|
|
||||||
env:
|
|
||||||
VOICEBOX_PYTHON: python
|
|
||||||
|
|
||||||
- name: Upload Playwright report
|
|
||||||
if: failure()
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: playwright-report
|
|
||||||
path: |
|
|
||||||
playwright-report
|
|
||||||
test-results
|
|
||||||
retention-days: 7
|
|
||||||
|
|
||||||
backend-tests:
|
|
||||||
# Informational: 30 pre-existing pytest files that have never run in CI.
|
|
||||||
continue-on-error: true
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.12'
|
|
||||||
cache: pip
|
|
||||||
cache-dependency-path: backend/requirements-ci.txt
|
|
||||||
|
|
||||||
- name: Install backend (CPU)
|
|
||||||
run: |
|
|
||||||
pip install torch --index-url https://download.pytorch.org/whl/cpu
|
|
||||||
pip install -r backend/requirements-ci.txt
|
|
||||||
pip install pytest pytest-asyncio
|
|
||||||
|
|
||||||
- name: Pytest
|
|
||||||
run: python -m pytest backend/tests -v --ignore=backend/tests/test_all_models_e2e.py
|
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -1 +0,0 @@
|
|||||||
22
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
+1
-1
@@ -133,7 +133,7 @@ bun run convert:assets
|
|||||||
This script:
|
This script:
|
||||||
- Converts PNG → WebP (better compression, same quality)
|
- Converts PNG → WebP (better compression, same quality)
|
||||||
- Converts MOV → WebM (VP9 codec, smaller file size)
|
- Converts MOV → WebM (VP9 codec, smaller file size)
|
||||||
- Processes files in `docs/public/`
|
- Processes files in `landing/public/` and `docs/public/`
|
||||||
- **Deletes original files** after successful conversion
|
- **Deletes original files** after successful conversion
|
||||||
|
|
||||||
**Requirements:** Install `webp` and `ffmpeg`:
|
**Requirements:** Install `webp` and `ffmpeg`:
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ COPY app/ ./app/
|
|||||||
COPY web/ ./web/
|
COPY web/ ./web/
|
||||||
|
|
||||||
# Strip workspaces not needed for web build, and fix trailing comma
|
# Strip workspaces not needed for web build, and fix trailing comma
|
||||||
RUN sed -i '/"tauri"/d' package.json && \
|
RUN sed -i '/"tauri"/d; /"landing"/d' package.json && \
|
||||||
sed -i -z 's/,\n ]/\n ]/' package.json
|
sed -i -z 's/,\n ]/\n ]/' package.json
|
||||||
RUN bun install --no-save
|
RUN bun install --no-save
|
||||||
# Build frontend (skip tsc — upstream has pre-existing type errors)
|
# Build frontend (skip tsc — upstream has pre-existing type errors)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@
|
|||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://voicebox.sh">
|
<a href="https://voicebox.sh">
|
||||||
<img src="docs/public/images/readme/app-screenshot-1.webp" alt="Voicebox App Screenshot" width="800" />
|
<img src="landing/public/assets/app-screenshot-1.webp" alt="Voicebox App Screenshot" width="800" />
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@@ -56,11 +56,11 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="docs/public/images/readme/app-screenshot-2.webp" alt="Voicebox Screenshot 2" width="800" />
|
<img src="landing/public/assets/app-screenshot-2.webp" alt="Voicebox Screenshot 2" width="800" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="docs/public/images/readme/app-screenshot-3.webp" alt="Voicebox Screenshot 3" width="800" />
|
<img src="landing/public/assets/app-screenshot-3.webp" alt="Voicebox Screenshot 3" width="800" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
@@ -442,6 +442,7 @@ voicebox/
|
|||||||
├── tauri/ # Desktop app (Tauri + Rust)
|
├── tauri/ # Desktop app (Tauri + Rust)
|
||||||
├── web/ # Web deployment
|
├── web/ # Web deployment
|
||||||
├── backend/ # Python FastAPI server
|
├── backend/ # Python FastAPI server
|
||||||
|
├── landing/ # Marketing website
|
||||||
└── scripts/ # Build & release scripts
|
└── scripts/ # Build & release scripts
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>voicebox</title>
|
||||||
|
<script>
|
||||||
|
(function () {
|
||||||
|
try {
|
||||||
|
var theme = 'system';
|
||||||
|
var raw = localStorage.getItem('voicebox-ui');
|
||||||
|
if (raw) {
|
||||||
|
var parsed = JSON.parse(raw);
|
||||||
|
if (parsed && parsed.state && parsed.state.theme) theme = parsed.state.theme;
|
||||||
|
}
|
||||||
|
var resolved = theme === 'system'
|
||||||
|
? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
|
||||||
|
: theme;
|
||||||
|
if (resolved === 'dark') document.documentElement.classList.add('dark');
|
||||||
|
} catch (_) {}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -4,7 +4,10 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||||
|
"preview": "vite preview",
|
||||||
"lint": "biome lint src",
|
"lint": "biome lint src",
|
||||||
"lint:fix": "biome lint --write src",
|
"lint:fix": "biome lint --write src",
|
||||||
"format": "biome format --write src",
|
"format": "biome format --write src",
|
||||||
|
|||||||
@@ -1,134 +0,0 @@
|
|||||||
import { mockIPC } from '@tauri-apps/api/mocks';
|
|
||||||
import { afterEach, beforeEach, expect, it } from 'vitest';
|
|
||||||
import App from '@/App';
|
|
||||||
import { createMockPlatform } from '@/test/mockPlatform';
|
|
||||||
import { buildModelStatus, buildProfile } from '@/test/msw/fixtures';
|
|
||||||
import {
|
|
||||||
captureHandlers,
|
|
||||||
effectsHandlers,
|
|
||||||
historyHandlers,
|
|
||||||
modelHandlers,
|
|
||||||
profileHandlers,
|
|
||||||
settingsHandlers,
|
|
||||||
storyHandlers,
|
|
||||||
taskHandlers,
|
|
||||||
} from '@/test/msw/handlers';
|
|
||||||
import { worker } from '@/test/msw/worker';
|
|
||||||
import { renderWithProviders } from '@/test/render';
|
|
||||||
|
|
||||||
const originalUrl = window.location.href;
|
|
||||||
|
|
||||||
// useChordSync and the permission gates call the Tauri IPC modules directly,
|
|
||||||
// outside the Platform abstraction. There is no Tauri runtime in the test
|
|
||||||
// browser, so `invoke`/`listen` would reject with a TypeError that some
|
|
||||||
// callers (e.g. useChordSync's `listen('dictate:warm-request')`) never get a
|
|
||||||
// chance to handle, surfacing as unhandled rejections. mockIPC installs the
|
|
||||||
// official in-memory IPC shim; `shouldMockEvents` covers listen/emit too.
|
|
||||||
//
|
|
||||||
// Reinstalled per test for a fresh listener map, but never cleared: the
|
|
||||||
// harness unmounts components after this file's afterEach, and those unmount
|
|
||||||
// cleanups still `unlisten` through the shim. The per-file iframe throws the
|
|
||||||
// window state away anyway.
|
|
||||||
beforeEach(() => {
|
|
||||||
mockIPC(
|
|
||||||
(cmd) => {
|
|
||||||
// Permission checks treat the result as a trusted boolean — grant
|
|
||||||
// them so no permission banners pop over the UI under test.
|
|
||||||
if (cmd.startsWith('check_')) return true;
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
{ shouldMockEvents: true },
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
window.history.replaceState(null, '', originalUrl);
|
|
||||||
delete window.__voiceboxServerStartedByApp;
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Everything the index route (MainEditor + app chrome) fetches on mount.
|
|
||||||
* Unstubbed requests fail the test loudly, so this is the full route budget.
|
|
||||||
*/
|
|
||||||
function useHappyPathHandlers() {
|
|
||||||
worker.use(
|
|
||||||
...profileHandlers([buildProfile({ name: 'Ada Lovelace' })]),
|
|
||||||
...historyHandlers([]),
|
|
||||||
...captureHandlers([]),
|
|
||||||
...settingsHandlers(),
|
|
||||||
...modelHandlers([buildModelStatus()]),
|
|
||||||
...storyHandlers([]),
|
|
||||||
...effectsHandlers([]),
|
|
||||||
...taskHandlers(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// App reads window.location at render time: `?view=dictate` picks the pill
|
|
||||||
// window, and the router matches the real browser path. Point the URL at the
|
|
||||||
// state under test before mounting; afterEach restores the runner's URL.
|
|
||||||
function setAppUrl(path: string) {
|
|
||||||
window.history.replaceState(null, '', path);
|
|
||||||
}
|
|
||||||
|
|
||||||
it('skips the startup gate outside Tauri and renders the router', async () => {
|
|
||||||
useHappyPathHandlers();
|
|
||||||
setAppUrl('/');
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<App />);
|
|
||||||
|
|
||||||
// Index route is MainEditor — the profile list proves the router mounted.
|
|
||||||
await expect.element(screen.getByText('Ada Lovelace')).toBeVisible();
|
|
||||||
|
|
||||||
// Web mode assumes an external server: no lifecycle management at all.
|
|
||||||
expect(screen.platform.lifecycle.startServer).not.toHaveBeenCalled();
|
|
||||||
expect(screen.platform.lifecycle.setupWindowCloseHandler).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('skips server auto-start in Tauri dev mode and still reaches the router', async () => {
|
|
||||||
// App gates auto-start on `import.meta.env.PROD`, which is false under
|
|
||||||
// vitest. The reachable Tauri branch is therefore the dev one: window
|
|
||||||
// close handler installed, auto-start skipped, serverReady forced true.
|
|
||||||
//
|
|
||||||
// The PROD-only branches — `lifecycle.startServer`, the health-check
|
|
||||||
// polling fallback, and the startup-error screen with its Retry button —
|
|
||||||
// are unreachable here without mocking import.meta.env, so they are
|
|
||||||
// intentionally not covered.
|
|
||||||
useHappyPathHandlers();
|
|
||||||
setAppUrl('/');
|
|
||||||
const platform = createMockPlatform({ metadata: { isTauri: true } });
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<App />, { platform });
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('Ada Lovelace')).toBeVisible();
|
|
||||||
|
|
||||||
expect(platform.lifecycle.startServer).not.toHaveBeenCalled();
|
|
||||||
expect(platform.lifecycle.setupWindowCloseHandler).toHaveBeenCalled();
|
|
||||||
// Startup syncs the keep-server-running setting into Rust.
|
|
||||||
expect(platform.lifecycle.setKeepServerRunning).toHaveBeenCalledWith(expect.any(Boolean));
|
|
||||||
// Auto-updater runs its mount check in Tauri.
|
|
||||||
expect(platform.updater.checkForUpdates).toHaveBeenCalled();
|
|
||||||
// Dev mode records that the app does not own the server process.
|
|
||||||
expect(window.__voiceboxServerStartedByApp).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('renders the dictate pill window for ?view=dictate without booting the main app', async () => {
|
|
||||||
// No route handlers on purpose: the dictate view must not touch any of the
|
|
||||||
// main app's endpoints, and an unhandled request would fail the test.
|
|
||||||
setAppUrl('/?view=dictate');
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<App />);
|
|
||||||
|
|
||||||
// DictateWindow forces the document transparent so the Tauri window takes
|
|
||||||
// the pill's shape — the observable signal that it mounted without
|
|
||||||
// throwing under the non-Tauri mock platform.
|
|
||||||
await expect.poll(() => document.body.style.background).toBe('transparent');
|
|
||||||
|
|
||||||
// The pill starts hidden: the wrapper renders but contains no CapturePill.
|
|
||||||
const wrapper = screen.container.firstElementChild as HTMLElement;
|
|
||||||
expect(wrapper.className).toContain('h-screen');
|
|
||||||
expect(wrapper.childElementCount).toBe(0);
|
|
||||||
|
|
||||||
// The startup gate never ran — no server lifecycle calls from this window.
|
|
||||||
expect(screen.platform.lifecycle.startServer).not.toHaveBeenCalled();
|
|
||||||
expect(screen.platform.lifecycle.setupWindowCloseHandler).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,675 @@
|
|||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { Check, CheckCircle2, Edit, Plus, Speaker, Trash2 } from 'lucide-react';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from '@/components/ui/dialog';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
import { apiClient } from '@/lib/api/client';
|
||||||
|
import { BOTTOM_SAFE_AREA_PADDING } from '@/lib/constants/ui';
|
||||||
|
import { cn } from '@/lib/utils/cn';
|
||||||
|
import { usePlatform } from '@/platform/PlatformContext';
|
||||||
|
import { usePlayerStore } from '@/stores/playerStore';
|
||||||
|
|
||||||
|
interface AudioDevice {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
is_default: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AudioTab() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const platform = usePlatform();
|
||||||
|
const [createDialogOpen, setCreateDialogOpen] = useState(false);
|
||||||
|
const [editingChannel, setEditingChannel] = useState<string | null>(null);
|
||||||
|
const [selectedChannelId, setSelectedChannelId] = useState<string | null>(null);
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const audioUrl = usePlayerStore((state) => state.audioUrl);
|
||||||
|
const isPlayerVisible = !!audioUrl;
|
||||||
|
|
||||||
|
const { data: channels, isLoading: channelsLoading } = useQuery({
|
||||||
|
queryKey: ['channels'],
|
||||||
|
queryFn: () => apiClient.listChannels(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: devices, isLoading: devicesLoading } = useQuery({
|
||||||
|
queryKey: ['audio-devices'],
|
||||||
|
queryFn: async () => {
|
||||||
|
if (!platform.metadata.isTauri) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return await platform.audio.listOutputDevices();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to list audio devices:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enabled: platform.metadata.isTauri,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: profiles } = useQuery({
|
||||||
|
queryKey: ['profiles'],
|
||||||
|
queryFn: () => apiClient.listProfiles(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createChannel = useMutation({
|
||||||
|
mutationFn: (data: { name: string; device_ids: string[] }) => apiClient.createChannel(data),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['channels'] });
|
||||||
|
setCreateDialogOpen(false);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateChannel = useMutation({
|
||||||
|
mutationFn: ({
|
||||||
|
channelId,
|
||||||
|
data,
|
||||||
|
}: {
|
||||||
|
channelId: string;
|
||||||
|
data: { name?: string; device_ids?: string[] };
|
||||||
|
}) => apiClient.updateChannel(channelId, data),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['channels'] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['profile-channels'] });
|
||||||
|
setEditingChannel(null);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteChannel = useMutation({
|
||||||
|
mutationFn: (channelId: string) => apiClient.deleteChannel(channelId),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['channels'] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['profile-channels'] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: channelVoices } = useQuery({
|
||||||
|
queryKey: ['channel-voices', editingChannel],
|
||||||
|
queryFn: async () => {
|
||||||
|
if (!editingChannel) return { profile_ids: [] };
|
||||||
|
return apiClient.getChannelVoices(editingChannel);
|
||||||
|
},
|
||||||
|
enabled: !!editingChannel,
|
||||||
|
});
|
||||||
|
|
||||||
|
const setChannelVoices = useMutation({
|
||||||
|
mutationFn: ({ channelId, profileIds }: { channelId: string; profileIds: string[] }) =>
|
||||||
|
apiClient.setChannelVoices(channelId, profileIds),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['channel-voices'] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['profile-channels'] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (channelsLoading || devicesLoading) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center h-full">
|
||||||
|
<div className="text-muted-foreground">{t('audioChannels.loading')}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleChannelDelete = async (e: React.MouseEvent, channelId: string) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (await confirm(t('audioChannels.confirmDelete'))) {
|
||||||
|
deleteChannel.mutate(channelId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const allChannels = channels || [];
|
||||||
|
const allDevices = devices || [];
|
||||||
|
const selectedChannel = selectedChannelId
|
||||||
|
? allChannels.find((c) => c.id === selectedChannelId)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="h-full flex flex-col">
|
||||||
|
<div className="flex items-center justify-between mb-6 shrink-0">
|
||||||
|
<h2 className="text-2xl font-bold">{t('audioChannels.title')}</h2>
|
||||||
|
<Button onClick={() => setCreateDialogOpen(true)}>
|
||||||
|
<Plus className="h-4 w-4 mr-2" />
|
||||||
|
{t('audioChannels.newChannel')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 h-full min-h-0">
|
||||||
|
{/* Left Column - Channels */}
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'flex flex-col min-h-0 overflow-y-auto',
|
||||||
|
isPlayerVisible && BOTTOM_SAFE_AREA_PADDING,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{allChannels.length === 0 ? (
|
||||||
|
<div className="flex flex-col items-center justify-center py-12 border-2 border-dashed border-muted rounded-md">
|
||||||
|
<Speaker className="h-12 w-12 text-muted-foreground mb-4" />
|
||||||
|
<p className="text-muted-foreground mb-4">{t('audioChannels.empty.message')}</p>
|
||||||
|
<Button onClick={() => setCreateDialogOpen(true)}>
|
||||||
|
<Plus className="h-4 w-4 mr-2" />
|
||||||
|
{t('audioChannels.empty.action')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3">
|
||||||
|
{allChannels.map((channel) => {
|
||||||
|
const isSelected = selectedChannelId === channel.id;
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={channel.id}
|
||||||
|
type="button"
|
||||||
|
className={cn(
|
||||||
|
'group border rounded-lg p-4 transition-colors cursor-pointer text-left w-full',
|
||||||
|
isSelected && 'ring-2 ring-primary bg-primary/5 border-primary',
|
||||||
|
)}
|
||||||
|
onClick={() => setSelectedChannelId(isSelected ? null : channel.id)}
|
||||||
|
>
|
||||||
|
<div className="flex items-start justify-between gap-4">
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="flex items-center gap-2 mb-3">
|
||||||
|
<div className="h-8 w-8 rounded-lg bg-muted flex items-center justify-center shrink-0">
|
||||||
|
<Speaker className="h-4 w-4 text-muted-foreground" />
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2 min-w-0">
|
||||||
|
<h3 className="font-semibold text-base truncate">{channel.name}</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2.5 ml-10">
|
||||||
|
<div>
|
||||||
|
<div className="text-xs font-medium text-muted-foreground mb-1">
|
||||||
|
{t('audioChannels.labels.outputDevices')}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap gap-1.5">
|
||||||
|
{channel.device_ids.length > 0
|
||||||
|
? channel.device_ids.map((deviceId) => {
|
||||||
|
const device = allDevices.find((d) => d.id === deviceId);
|
||||||
|
return (
|
||||||
|
<Badge
|
||||||
|
key={deviceId}
|
||||||
|
variant="outline"
|
||||||
|
className="text-xs font-normal"
|
||||||
|
>
|
||||||
|
{device?.name || deviceId}
|
||||||
|
</Badge>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
: (() => {
|
||||||
|
const defaultDevice = allDevices.find((d) => d.is_default);
|
||||||
|
return defaultDevice ? (
|
||||||
|
<Badge variant="outline" className="text-xs font-normal">
|
||||||
|
{defaultDevice.name}
|
||||||
|
</Badge>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<div className="text-xs font-medium text-muted-foreground mb-1">
|
||||||
|
{t('audioChannels.labels.assignedVoices')}
|
||||||
|
</div>
|
||||||
|
<ChannelVoicesList channelId={channel.id} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{!channel.is_default && (
|
||||||
|
<div className="flex gap-1 shrink-0 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="h-8 w-8 p-0"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
setEditingChannel(channel.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Edit className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="h-8 w-8 p-0"
|
||||||
|
onClick={(e) => handleChannelDelete(e, channel.id)}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Right Column - Available Devices */}
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'flex flex-col min-h-0 overflow-y-auto',
|
||||||
|
isPlayerVisible && BOTTOM_SAFE_AREA_PADDING,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="shrink-0 mb-4">
|
||||||
|
<h3 className="text-lg font-semibold">{t('audioChannels.devices.title')}</h3>
|
||||||
|
<p className="text-sm text-muted-foreground mt-1">
|
||||||
|
{selectedChannelId
|
||||||
|
? selectedChannel?.is_default
|
||||||
|
? t('audioChannels.devices.defaultNote')
|
||||||
|
: t('audioChannels.devices.toggleHint')
|
||||||
|
: t('audioChannels.devices.selectHint')}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{allDevices.length > 0 ? (
|
||||||
|
<div className="space-y-2">
|
||||||
|
{allDevices.map((device) => {
|
||||||
|
const isConnected =
|
||||||
|
selectedChannelId &&
|
||||||
|
selectedChannel &&
|
||||||
|
(selectedChannel.device_ids.length === 0
|
||||||
|
? device.is_default
|
||||||
|
: selectedChannel.device_ids.includes(device.id));
|
||||||
|
const canToggle =
|
||||||
|
selectedChannelId && selectedChannel && !selectedChannel.is_default;
|
||||||
|
|
||||||
|
const handleDeviceClick = () => {
|
||||||
|
if (!canToggle || !selectedChannel) return;
|
||||||
|
|
||||||
|
const currentDeviceIds = selectedChannel.device_ids;
|
||||||
|
const newDeviceIds = isConnected
|
||||||
|
? currentDeviceIds.filter((id) => id !== device.id)
|
||||||
|
: [...currentDeviceIds, device.id];
|
||||||
|
|
||||||
|
updateChannel.mutate({
|
||||||
|
channelId: selectedChannelId,
|
||||||
|
data: { device_ids: newDeviceIds },
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={device.id}
|
||||||
|
type="button"
|
||||||
|
onClick={handleDeviceClick}
|
||||||
|
disabled={!canToggle}
|
||||||
|
className={cn(
|
||||||
|
'flex items-center gap-2 text-sm p-3 rounded-lg border transition-colors text-left w-full',
|
||||||
|
isConnected
|
||||||
|
? 'bg-primary/10 border-primary ring-1 ring-primary/20'
|
||||||
|
: 'hover:bg-muted/50',
|
||||||
|
!canToggle && 'cursor-default opacity-60',
|
||||||
|
canToggle && 'cursor-pointer',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{canToggle ? (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'h-4 w-4 rounded border-2 flex items-center justify-center shrink-0',
|
||||||
|
isConnected ? 'bg-accent border-accent' : 'border-muted-foreground/30',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{isConnected && <Check className="h-3 w-3 text-accent-foreground" />}
|
||||||
|
</div>
|
||||||
|
) : device.is_default ? (
|
||||||
|
<CheckCircle2 className="h-4 w-4 text-primary shrink-0" />
|
||||||
|
) : null}
|
||||||
|
<span className={cn('truncate flex-1', device.is_default && 'font-medium')}>
|
||||||
|
{device.name}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex flex-col items-center justify-center py-12 border-2 border-dashed border-muted rounded-md">
|
||||||
|
<CheckCircle2 className="h-12 w-12 text-muted-foreground mb-4" />
|
||||||
|
<p className="text-muted-foreground text-center">
|
||||||
|
{platform.metadata.isTauri
|
||||||
|
? t('audioChannels.devices.empty')
|
||||||
|
: t('audioChannels.devices.requiresTauri')}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Create Channel Dialog */}
|
||||||
|
<CreateChannelDialog
|
||||||
|
open={createDialogOpen}
|
||||||
|
onOpenChange={setCreateDialogOpen}
|
||||||
|
devices={devices || []}
|
||||||
|
onCreate={(name, deviceIds) => {
|
||||||
|
createChannel.mutate({ name, device_ids: deviceIds });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Edit Channel Dialog */}
|
||||||
|
{editingChannel &&
|
||||||
|
(() => {
|
||||||
|
const channel = channels?.find((c) => c.id === editingChannel);
|
||||||
|
return channel ? (
|
||||||
|
<EditChannelDialog
|
||||||
|
open={!!editingChannel}
|
||||||
|
onOpenChange={(open) => !open && setEditingChannel(null)}
|
||||||
|
channel={channel}
|
||||||
|
devices={devices || []}
|
||||||
|
profiles={profiles || []}
|
||||||
|
channelVoices={channelVoices?.profile_ids || []}
|
||||||
|
onUpdate={(name, deviceIds) => {
|
||||||
|
updateChannel.mutate({
|
||||||
|
channelId: editingChannel,
|
||||||
|
data: { name, device_ids: deviceIds },
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onSetVoices={(profileIds) => {
|
||||||
|
setChannelVoices.mutate({
|
||||||
|
channelId: editingChannel,
|
||||||
|
profileIds,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ChannelVoicesList({ channelId }: { channelId: string }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { data: voices } = useQuery({
|
||||||
|
queryKey: ['channel-voices', channelId],
|
||||||
|
queryFn: () => apiClient.getChannelVoices(channelId),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: profiles } = useQuery({
|
||||||
|
queryKey: ['profiles'],
|
||||||
|
queryFn: () => apiClient.listProfiles(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const voiceNames =
|
||||||
|
voices?.profile_ids.map((id) => profiles?.find((p) => p.id === id)?.name).filter(Boolean) || [];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap gap-1.5">
|
||||||
|
{voiceNames.length > 0 ? (
|
||||||
|
voiceNames.map((name) => (
|
||||||
|
<Badge key={name} variant="outline" className="text-xs font-normal">
|
||||||
|
{name}
|
||||||
|
</Badge>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<span className="text-sm text-muted-foreground">{t('audioChannels.noVoicesAssigned')}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CreateChannelDialogProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (open: boolean) => void;
|
||||||
|
devices: AudioDevice[];
|
||||||
|
onCreate: (name: string, deviceIds: string[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateChannelDialog({ open, onOpenChange, devices, onCreate }: CreateChannelDialogProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [name, setName] = useState('');
|
||||||
|
const [selectedDevices, setSelectedDevices] = useState<string[]>([]);
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
if (name.trim()) {
|
||||||
|
onCreate(name.trim(), selectedDevices);
|
||||||
|
setName('');
|
||||||
|
setSelectedDevices([]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{t('audioChannels.createDialog.title')}</DialogTitle>
|
||||||
|
<DialogDescription>{t('audioChannels.createDialog.description')}</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="channel-name">{t('audioChannels.fields.name')}</Label>
|
||||||
|
<Input
|
||||||
|
id="channel-name"
|
||||||
|
value={name}
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
placeholder={t('audioChannels.fields.namePlaceholder')}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Label>{t('audioChannels.labels.outputDevices')}</Label>
|
||||||
|
<Select
|
||||||
|
value={selectedDevices[0] || ''}
|
||||||
|
onValueChange={(value) => {
|
||||||
|
if (value && !selectedDevices.includes(value)) {
|
||||||
|
setSelectedDevices([...selectedDevices, value]);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder={t('audioChannels.selectDevice')} />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{devices.map((device) => (
|
||||||
|
<SelectItem key={device.id} value={device.id}>
|
||||||
|
{device.name} {device.is_default && `(${t('audioChannels.defaultSuffix')})`}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{selectedDevices.length > 0 && (
|
||||||
|
<div className="mt-2 space-y-1">
|
||||||
|
{selectedDevices.map((deviceId) => {
|
||||||
|
const device = devices.find((d) => d.id === deviceId);
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={deviceId}
|
||||||
|
className="flex items-center justify-between text-sm bg-muted p-2 rounded"
|
||||||
|
>
|
||||||
|
<span>{device?.name || deviceId}</span>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() =>
|
||||||
|
setSelectedDevices(selectedDevices.filter((id) => id !== deviceId))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||||
|
{t('common.cancel')}
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleSubmit} disabled={!name.trim()}>
|
||||||
|
{t('audioChannels.createDialog.action')}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EditChannelDialogProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (open: boolean) => void;
|
||||||
|
channel: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
device_ids: string[];
|
||||||
|
};
|
||||||
|
devices: AudioDevice[];
|
||||||
|
profiles: Array<{ id: string; name: string }>;
|
||||||
|
channelVoices: string[];
|
||||||
|
onUpdate: (name: string, deviceIds: string[]) => void;
|
||||||
|
onSetVoices: (profileIds: string[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function EditChannelDialog({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
channel,
|
||||||
|
devices,
|
||||||
|
profiles,
|
||||||
|
channelVoices,
|
||||||
|
onUpdate,
|
||||||
|
onSetVoices,
|
||||||
|
}: EditChannelDialogProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [name, setName] = useState(channel.name);
|
||||||
|
const [selectedDevices, setSelectedDevices] = useState<string[]>(channel.device_ids);
|
||||||
|
const [selectedVoices, setSelectedVoices] = useState<string[]>(channelVoices);
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
if (name.trim()) {
|
||||||
|
onUpdate(name.trim(), selectedDevices);
|
||||||
|
onSetVoices(selectedVoices);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
<DialogContent className="max-w-2xl">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{t('audioChannels.editDialog.title')}</DialogTitle>
|
||||||
|
<DialogDescription>{t('audioChannels.editDialog.description')}</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="edit-channel-name">{t('audioChannels.fields.name')}</Label>
|
||||||
|
<Input id="edit-channel-name" value={name} onChange={(e) => setName(e.target.value)} />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Label>{t('audioChannels.labels.outputDevices')}</Label>
|
||||||
|
<Select
|
||||||
|
value=""
|
||||||
|
onValueChange={(value) => {
|
||||||
|
if (value && !selectedDevices.includes(value)) {
|
||||||
|
setSelectedDevices([...selectedDevices, value]);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder={t('audioChannels.addDevice')} />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{devices.map((device) => (
|
||||||
|
<SelectItem key={device.id} value={device.id}>
|
||||||
|
{device.name} {device.is_default && `(${t('audioChannels.defaultSuffix')})`}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{selectedDevices.length > 0 && (
|
||||||
|
<div className="mt-2 space-y-1">
|
||||||
|
{selectedDevices.map((deviceId) => {
|
||||||
|
const device = devices.find((d) => d.id === deviceId);
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={deviceId}
|
||||||
|
className="flex items-center justify-between text-sm bg-muted p-2 rounded"
|
||||||
|
>
|
||||||
|
<span>{device?.name || deviceId}</span>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() =>
|
||||||
|
setSelectedDevices(selectedDevices.filter((id) => id !== deviceId))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Label>{t('audioChannels.labels.assignedVoices')}</Label>
|
||||||
|
<Select
|
||||||
|
value=""
|
||||||
|
onValueChange={(value) => {
|
||||||
|
if (value && !selectedVoices.includes(value)) {
|
||||||
|
setSelectedVoices([...selectedVoices, value]);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder={t('audioChannels.addVoice')} />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{profiles.map((profile) => (
|
||||||
|
<SelectItem key={profile.id} value={profile.id}>
|
||||||
|
{profile.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{selectedVoices.length > 0 && (
|
||||||
|
<div className="mt-2 space-y-1">
|
||||||
|
{selectedVoices.map((profileId) => {
|
||||||
|
const profile = profiles.find((p) => p.id === profileId);
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={profileId}
|
||||||
|
className="flex items-center justify-between text-sm bg-muted p-2 rounded"
|
||||||
|
>
|
||||||
|
<span>{profile?.name || profileId}</span>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() =>
|
||||||
|
setSelectedVoices(selectedVoices.filter((id) => id !== profileId))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||||
|
{t('common.cancel')}
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleSubmit} disabled={!name.trim()}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
import { Link } from '@tanstack/react-router';
|
import { Link } from '@tanstack/react-router';
|
||||||
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
|
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
|
||||||
|
import { save } from '@tauri-apps/plugin-dialog';
|
||||||
|
import { writeFile, writeTextFile } from '@tauri-apps/plugin-fs';
|
||||||
import {
|
import {
|
||||||
Captions,
|
Captions,
|
||||||
Check,
|
Check,
|
||||||
@@ -25,14 +27,6 @@ import { AudioBars } from '@/components/AudioBars';
|
|||||||
import { CapturePill } from '@/components/CapturePill/CapturePill';
|
import { CapturePill } from '@/components/CapturePill/CapturePill';
|
||||||
import { CaptureInlinePlayer } from '@/components/CapturesTab/CaptureInlinePlayer';
|
import { CaptureInlinePlayer } from '@/components/CapturesTab/CaptureInlinePlayer';
|
||||||
import { DictationReadinessChecklist } from '@/components/CapturesTab/DictationReadinessChecklist';
|
import { DictationReadinessChecklist } from '@/components/CapturesTab/DictationReadinessChecklist';
|
||||||
import {
|
|
||||||
ListPane,
|
|
||||||
ListPaneHeader,
|
|
||||||
ListPaneScroll,
|
|
||||||
ListPaneSearch,
|
|
||||||
ListPaneTitle,
|
|
||||||
ListPaneTitleRow,
|
|
||||||
} from '@/components/ListPane';
|
|
||||||
import {
|
import {
|
||||||
AlertDialog,
|
AlertDialog,
|
||||||
AlertDialogAction,
|
AlertDialogAction,
|
||||||
@@ -54,6 +48,14 @@ import {
|
|||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '@/components/ui/dropdown-menu';
|
} from '@/components/ui/dropdown-menu';
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import {
|
||||||
|
ListPane,
|
||||||
|
ListPaneHeader,
|
||||||
|
ListPaneScroll,
|
||||||
|
ListPaneSearch,
|
||||||
|
ListPaneTitle,
|
||||||
|
ListPaneTitleRow,
|
||||||
|
} from '@/components/ListPane';
|
||||||
import { useToast } from '@/components/ui/use-toast';
|
import { useToast } from '@/components/ui/use-toast';
|
||||||
import { apiClient } from '@/lib/api/client';
|
import { apiClient } from '@/lib/api/client';
|
||||||
import type {
|
import type {
|
||||||
@@ -70,7 +72,6 @@ import { useCaptureSettings } from '@/lib/hooks/useSettings';
|
|||||||
import { cn } from '@/lib/utils/cn';
|
import { cn } from '@/lib/utils/cn';
|
||||||
import { formatAbsoluteDate, formatDate } from '@/lib/utils/format';
|
import { formatAbsoluteDate, formatDate } from '@/lib/utils/format';
|
||||||
import { displayLabelForKey, modifierSideHint } from '@/lib/utils/keyCodes';
|
import { displayLabelForKey, modifierSideHint } from '@/lib/utils/keyCodes';
|
||||||
import { usePlatform } from '@/platform/PlatformContext';
|
|
||||||
import { useGenerationStore } from '@/stores/generationStore';
|
import { useGenerationStore } from '@/stores/generationStore';
|
||||||
import { usePlayerStore } from '@/stores/playerStore';
|
import { usePlayerStore } from '@/stores/playerStore';
|
||||||
|
|
||||||
@@ -134,7 +135,6 @@ export function CapturesTab() {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const platform = usePlatform();
|
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
const uploadInputRef = useRef<HTMLInputElement>(null);
|
const uploadInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
@@ -202,7 +202,6 @@ export function CapturesTab() {
|
|||||||
// the race window between ``setSelectedId(new)`` and the refetched list
|
// the race window between ``setSelectedId(new)`` and the refetched list
|
||||||
// actually containing the new row.
|
// actually containing the new row.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!platform.metadata.isTauri) return;
|
|
||||||
const unlistens: Promise<UnlistenFn>[] = [];
|
const unlistens: Promise<UnlistenFn>[] = [];
|
||||||
unlistens.push(
|
unlistens.push(
|
||||||
listen<{ capture: CaptureResponse }>('capture:created', (event) => {
|
listen<{ capture: CaptureResponse }>('capture:created', (event) => {
|
||||||
@@ -226,7 +225,7 @@ export function CapturesTab() {
|
|||||||
return () => {
|
return () => {
|
||||||
for (const p of unlistens) p.then((fn) => fn()).catch(() => {});
|
for (const p of unlistens) p.then((fn) => fn()).catch(() => {});
|
||||||
};
|
};
|
||||||
}, [queryClient, platform.metadata.isTauri]);
|
}, [queryClient]);
|
||||||
|
|
||||||
const filtered = useMemo(() => {
|
const filtered = useMemo(() => {
|
||||||
const q = search.trim().toLowerCase();
|
const q = search.trim().toLowerCase();
|
||||||
@@ -244,7 +243,9 @@ export function CapturesTab() {
|
|||||||
// referenced profile was deleted) fall through to the first profile.
|
// referenced profile was deleted) fall through to the first profile.
|
||||||
const storedVoiceId = captureSettings?.default_playback_voice_id ?? null;
|
const storedVoiceId = captureSettings?.default_playback_voice_id ?? null;
|
||||||
const playAsVoice =
|
const playAsVoice =
|
||||||
(storedVoiceId && profiles?.find((p) => p.id === storedVoiceId)) || profiles?.[0] || null;
|
(storedVoiceId && profiles?.find((p) => p.id === storedVoiceId)) ||
|
||||||
|
profiles?.[0] ||
|
||||||
|
null;
|
||||||
const playAsVoiceId = playAsVoice?.id ?? null;
|
const playAsVoiceId = playAsVoice?.id ?? null;
|
||||||
|
|
||||||
const deleteMutation = useMutation({
|
const deleteMutation = useMutation({
|
||||||
@@ -254,22 +255,12 @@ export function CapturesTab() {
|
|||||||
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
||||||
},
|
},
|
||||||
onError: (err: Error) => {
|
onError: (err: Error) => {
|
||||||
toast({
|
toast({ title: t('captures.toast.deleteFailed'), description: err.message, variant: 'destructive' });
|
||||||
title: t('captures.toast.deleteFailed'),
|
|
||||||
description: err.message,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const playAsMutation = useMutation({
|
const playAsMutation = useMutation({
|
||||||
mutationFn: async ({
|
mutationFn: async ({ capture, voice }: { capture: CaptureResponse; voice: VoiceProfileResponse }) => {
|
||||||
capture,
|
|
||||||
voice,
|
|
||||||
}: {
|
|
||||||
capture: CaptureResponse;
|
|
||||||
voice: VoiceProfileResponse;
|
|
||||||
}) => {
|
|
||||||
const text = capture.transcript_refined || capture.transcript_raw;
|
const text = capture.transcript_refined || capture.transcript_raw;
|
||||||
if (!text.trim()) throw new Error(t('captures.noTranscriptError'));
|
if (!text.trim()) throw new Error(t('captures.noTranscriptError'));
|
||||||
const language = (capture.language || voice.language) as LanguageCode;
|
const language = (capture.language || voice.language) as LanguageCode;
|
||||||
@@ -277,13 +268,8 @@ export function CapturesTab() {
|
|||||||
// profile's stored engine preference. Cloned profiles without an
|
// profile's stored engine preference. Cloned profiles without an
|
||||||
// override fall through to whatever the backend picks.
|
// override fall through to whatever the backend picks.
|
||||||
const engine = voice.default_engine as
|
const engine = voice.default_engine as
|
||||||
| 'qwen'
|
| 'qwen' | 'qwen_custom_voice' | 'luxtts' | 'chatterbox'
|
||||||
| 'qwen_custom_voice'
|
| 'chatterbox_turbo' | 'tada' | 'kokoro'
|
||||||
| 'luxtts'
|
|
||||||
| 'chatterbox'
|
|
||||||
| 'chatterbox_turbo'
|
|
||||||
| 'tada'
|
|
||||||
| 'kokoro'
|
|
||||||
| undefined;
|
| undefined;
|
||||||
return apiClient.generateSpeech({
|
return apiClient.generateSpeech({
|
||||||
profile_id: voice.id,
|
profile_id: voice.id,
|
||||||
@@ -300,11 +286,7 @@ export function CapturesTab() {
|
|||||||
addPendingGeneration(result.id);
|
addPendingGeneration(result.id);
|
||||||
},
|
},
|
||||||
onError: (err: Error) => {
|
onError: (err: Error) => {
|
||||||
toast({
|
toast({ title: t('captures.toast.playAsFailed'), description: err.message, variant: 'destructive' });
|
||||||
title: t('captures.toast.playAsFailed'),
|
|
||||||
description: err.message,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -354,15 +336,16 @@ export function CapturesTab() {
|
|||||||
const handleExportAudio = async () => {
|
const handleExportAudio = async () => {
|
||||||
if (!selected) return;
|
if (!selected) return;
|
||||||
try {
|
try {
|
||||||
|
const dest = await save({
|
||||||
|
defaultPath: `capture_${selected.id.slice(0, 8)}.wav`,
|
||||||
|
filters: [{ name: 'Audio', extensions: ['wav'] }],
|
||||||
|
});
|
||||||
|
if (!dest) return;
|
||||||
const res = await fetch(apiClient.getCaptureAudioUrl(selected.id));
|
const res = await fetch(apiClient.getCaptureAudioUrl(selected.id));
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||||
const blob = new Blob([await res.arrayBuffer()], { type: 'audio/wav' });
|
const buf = new Uint8Array(await res.arrayBuffer());
|
||||||
const dest = await platform.filesystem.saveFile(
|
await writeFile(dest, buf);
|
||||||
`capture_${selected.id.slice(0, 8)}.wav`,
|
exportToastSuccess(dest);
|
||||||
blob,
|
|
||||||
[{ name: 'Audio', extensions: ['wav'] }],
|
|
||||||
);
|
|
||||||
if (dest) exportToastSuccess(dest);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
exportToastError(err);
|
exportToastError(err);
|
||||||
}
|
}
|
||||||
@@ -376,12 +359,13 @@ export function CapturesTab() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const dest = await platform.filesystem.saveFile(
|
const dest = await save({
|
||||||
`capture_${selected.id.slice(0, 8)}.txt`,
|
defaultPath: `capture_${selected.id.slice(0, 8)}.txt`,
|
||||||
new Blob([text], { type: 'text/plain' }),
|
filters: [{ name: 'Text', extensions: ['txt'] }],
|
||||||
[{ name: 'Text', extensions: ['txt'] }],
|
});
|
||||||
);
|
if (!dest) return;
|
||||||
if (dest) exportToastSuccess(dest);
|
await writeTextFile(dest, text);
|
||||||
|
exportToastSuccess(dest);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
exportToastError(err);
|
exportToastError(err);
|
||||||
}
|
}
|
||||||
@@ -392,8 +376,7 @@ export function CapturesTab() {
|
|||||||
lines.push(`# Capture ${capture.id}`, '');
|
lines.push(`# Capture ${capture.id}`, '');
|
||||||
lines.push(`- **Source:** ${capture.source}`);
|
lines.push(`- **Source:** ${capture.source}`);
|
||||||
lines.push(`- **Created:** ${capture.created_at}`);
|
lines.push(`- **Created:** ${capture.created_at}`);
|
||||||
if (capture.duration_ms != null)
|
if (capture.duration_ms != null) lines.push(`- **Duration:** ${formatDuration(capture.duration_ms)}`);
|
||||||
lines.push(`- **Duration:** ${formatDuration(capture.duration_ms)}`);
|
|
||||||
if (capture.language) lines.push(`- **Language:** ${capture.language}`);
|
if (capture.language) lines.push(`- **Language:** ${capture.language}`);
|
||||||
if (capture.stt_model) lines.push(`- **STT model:** ${capture.stt_model}`);
|
if (capture.stt_model) lines.push(`- **STT model:** ${capture.stt_model}`);
|
||||||
if (capture.llm_model) lines.push(`- **LLM model:** ${capture.llm_model}`);
|
if (capture.llm_model) lines.push(`- **LLM model:** ${capture.llm_model}`);
|
||||||
@@ -415,12 +398,13 @@ export function CapturesTab() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const dest = await platform.filesystem.saveFile(
|
const dest = await save({
|
||||||
`capture_${selected.id.slice(0, 8)}.md`,
|
defaultPath: `capture_${selected.id.slice(0, 8)}.md`,
|
||||||
new Blob([buildCaptureMarkdown(selected)], { type: 'text/markdown' }),
|
filters: [{ name: 'Markdown', extensions: ['md'] }],
|
||||||
[{ name: 'Markdown', extensions: ['md'] }],
|
});
|
||||||
);
|
if (!dest) return;
|
||||||
if (dest) exportToastSuccess(dest);
|
await writeTextFile(dest, buildCaptureMarkdown(selected));
|
||||||
|
exportToastSuccess(dest);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
exportToastError(err);
|
exportToastError(err);
|
||||||
}
|
}
|
||||||
@@ -502,48 +486,48 @@ export function CapturesTab() {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
filtered.map((capture) => {
|
filtered.map((capture) => {
|
||||||
const isActive = selectedId === capture.id;
|
const isActive = selectedId === capture.id;
|
||||||
const refined = !!capture.transcript_refined;
|
const refined = !!capture.transcript_refined;
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
key={capture.id}
|
key={capture.id}
|
||||||
onClick={() => setSelectedId(capture.id)}
|
onClick={() => setSelectedId(capture.id)}
|
||||||
className={cn(
|
className={cn(
|
||||||
'w-full text-left p-3 rounded-lg transition-colors block',
|
'w-full text-left p-3 rounded-lg transition-colors block',
|
||||||
isActive
|
isActive
|
||||||
? 'bg-muted/70 border border-border'
|
? 'bg-muted/70 border border-border'
|
||||||
: 'border border-transparent hover:bg-muted/30',
|
: 'border border-transparent hover:bg-muted/30',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2 mb-1.5">
|
||||||
|
<span className="text-[11px] text-muted-foreground font-medium">
|
||||||
|
{formatDate(capture.created_at)}
|
||||||
|
</span>
|
||||||
|
<div className="flex-1" />
|
||||||
|
<span className="text-[10px] text-muted-foreground/70 tabular-nums">
|
||||||
|
{formatDuration(capture.duration_ms)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="text-[13px] text-foreground/90 line-clamp-2 leading-snug mb-2">
|
||||||
|
{snippetOf(capture)}
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-1.5 flex-wrap">
|
||||||
|
<SourceBadge source={capture.source} />
|
||||||
|
{refined && (
|
||||||
|
<Badge
|
||||||
|
variant="secondary"
|
||||||
|
className="h-5 px-1.5 text-[10px] gap-1 font-medium bg-accent/10 text-accent border border-accent/20"
|
||||||
|
>
|
||||||
|
<Sparkles className="h-2.5 w-2.5" />
|
||||||
|
{t('captures.transcript.refined')}
|
||||||
|
</Badge>
|
||||||
)}
|
)}
|
||||||
>
|
</div>
|
||||||
<div className="flex items-center gap-2 mb-1.5">
|
</button>
|
||||||
<span className="text-[11px] text-muted-foreground font-medium">
|
);
|
||||||
{formatDate(capture.created_at)}
|
})
|
||||||
</span>
|
)}
|
||||||
<div className="flex-1" />
|
|
||||||
<span className="text-[10px] text-muted-foreground/70 tabular-nums">
|
|
||||||
{formatDuration(capture.duration_ms)}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div className="text-[13px] text-foreground/90 line-clamp-2 leading-snug mb-2">
|
|
||||||
{snippetOf(capture)}
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-1.5 flex-wrap">
|
|
||||||
<SourceBadge source={capture.source} />
|
|
||||||
{refined && (
|
|
||||||
<Badge
|
|
||||||
variant="secondary"
|
|
||||||
className="h-5 px-1.5 text-[10px] gap-1 font-medium bg-accent/10 text-accent border border-accent/20"
|
|
||||||
>
|
|
||||||
<Sparkles className="h-2.5 w-2.5" />
|
|
||||||
{t('captures.transcript.refined')}
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
})
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</ListPaneScroll>
|
</ListPaneScroll>
|
||||||
</ListPane>
|
</ListPane>
|
||||||
@@ -594,9 +578,7 @@ export function CapturesTab() {
|
|||||||
) : (
|
) : (
|
||||||
<Upload className="h-4 w-4 mr-2" />
|
<Upload className="h-4 w-4 mr-2" />
|
||||||
)}
|
)}
|
||||||
{session.isUploading
|
{session.isUploading ? t('captures.actions.importing') : t('captures.actions.import')}
|
||||||
? t('captures.actions.importing')
|
|
||||||
: t('captures.actions.import')}
|
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
@@ -766,7 +748,11 @@ export function CapturesTab() {
|
|||||||
</DropdownMenuLabel>
|
</DropdownMenuLabel>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
{profiles?.map((v) => (
|
{profiles?.map((v) => (
|
||||||
<DropdownMenuItem key={v.id} onClick={() => handlePlayAs(v)} className="py-2">
|
<DropdownMenuItem
|
||||||
|
key={v.id}
|
||||||
|
onClick={() => handlePlayAs(v)}
|
||||||
|
className="py-2"
|
||||||
|
>
|
||||||
<div className="flex-1 min-w-0">
|
<div className="flex-1 min-w-0">
|
||||||
<div className="text-sm font-medium truncate">{v.name}</div>
|
<div className="text-sm font-medium truncate">{v.name}</div>
|
||||||
<div className="text-[11px] text-muted-foreground truncate">
|
<div className="text-[11px] text-muted-foreground truncate">
|
||||||
@@ -878,7 +864,9 @@ export function CapturesTab() {
|
|||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
<p className="text-sm">{t('captures.empty.pressShortcut')}</p>
|
<p className="text-sm">
|
||||||
|
{t('captures.empty.pressShortcut')}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="max-w-sm mx-auto text-center space-y-3">
|
<div className="max-w-sm mx-auto text-center space-y-3">
|
||||||
@@ -900,9 +888,7 @@ export function CapturesTab() {
|
|||||||
<AlertDialogContent>
|
<AlertDialogContent>
|
||||||
<AlertDialogHeader>
|
<AlertDialogHeader>
|
||||||
<AlertDialogTitle>{t('captures.deleteDialog.title')}</AlertDialogTitle>
|
<AlertDialogTitle>{t('captures.deleteDialog.title')}</AlertDialogTitle>
|
||||||
<AlertDialogDescription>
|
<AlertDialogDescription>{t('captures.deleteDialog.description')}</AlertDialogDescription>
|
||||||
{t('captures.deleteDialog.description')}
|
|
||||||
</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
</AlertDialogHeader>
|
||||||
<AlertDialogFooter>
|
<AlertDialogFooter>
|
||||||
<AlertDialogCancel>{t('common.cancel')}</AlertDialogCancel>
|
<AlertDialogCancel>{t('common.cancel')}</AlertDialogCancel>
|
||||||
@@ -912,9 +898,7 @@ export function CapturesTab() {
|
|||||||
disabled={deleteMutation.isPending}
|
disabled={deleteMutation.isPending}
|
||||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||||
>
|
>
|
||||||
{deleteMutation.isPending
|
{deleteMutation.isPending ? t('captures.deleteDialog.deleting') : t('common.delete')}
|
||||||
? t('captures.deleteDialog.deleting')
|
|
||||||
: t('common.delete')}
|
|
||||||
</Button>
|
</Button>
|
||||||
</AlertDialogAction>
|
</AlertDialogAction>
|
||||||
</AlertDialogFooter>
|
</AlertDialogFooter>
|
||||||
|
|||||||
@@ -1,113 +0,0 @@
|
|||||||
import { expect, it, vi } from 'vitest';
|
|
||||||
import { ChordPicker } from '@/components/ChordPicker/ChordPicker';
|
|
||||||
import { renderWithProviders } from '@/test/render';
|
|
||||||
|
|
||||||
// ChordPicker listens on window in the capture phase and canonicalizes via
|
|
||||||
// `event.code`, so raw KeyboardEvents give exact control over which physical
|
|
||||||
// keys the picker sees (userEvent would depend on the host keyboard layout).
|
|
||||||
function press(code: string) {
|
|
||||||
window.dispatchEvent(new KeyboardEvent('keydown', { code, bubbles: true, cancelable: true }));
|
|
||||||
}
|
|
||||||
|
|
||||||
function release(code: string) {
|
|
||||||
window.dispatchEvent(new KeyboardEvent('keyup', { code, bubbles: true, cancelable: true }));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function renderPicker(initialKeys: string[] = []) {
|
|
||||||
const onSave = vi.fn();
|
|
||||||
const onCancel = vi.fn();
|
|
||||||
const screen = await renderWithProviders(
|
|
||||||
<ChordPicker
|
|
||||||
open
|
|
||||||
title="Push-to-talk shortcut"
|
|
||||||
initialKeys={initialKeys}
|
|
||||||
onSave={onSave}
|
|
||||||
onCancel={onCancel}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
return { screen, onSave, onCancel };
|
|
||||||
}
|
|
||||||
|
|
||||||
it('opens empty with save disabled and flags unsupported keys', async () => {
|
|
||||||
const { screen } = await renderPicker();
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('Press your shortcut')).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('No keys yet')).toBeVisible();
|
|
||||||
await expect.element(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
|
|
||||||
|
|
||||||
// NumpadEnter has no canonical chord name — the picker refuses it and
|
|
||||||
// stays empty instead of capturing garbage.
|
|
||||||
press('NumpadEnter');
|
|
||||||
await expect.element(screen.getByText(/isn't supported in chords/)).toBeVisible();
|
|
||||||
await expect.element(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('captures the held keys and saves them after release', async () => {
|
|
||||||
const { screen, onSave } = await renderPicker();
|
|
||||||
|
|
||||||
press('KeyJ');
|
|
||||||
await expect.element(screen.getByText('Capturing…')).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('J', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
press('KeyK');
|
|
||||||
await expect.element(screen.getByText('K', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
// Releasing everything freezes the peak so the user can save hands-free.
|
|
||||||
release('KeyK');
|
|
||||||
release('KeyJ');
|
|
||||||
await expect.element(screen.getByText('Press your shortcut')).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('J', { exact: true })).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('K', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Save' }).click();
|
|
||||||
expect(onSave).toHaveBeenCalledExactlyOnceWith(['KeyJ', 'KeyK']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('keeps the peak set when a key is released mid-chord', async () => {
|
|
||||||
const { screen, onSave } = await renderPicker();
|
|
||||||
|
|
||||||
press('KeyA');
|
|
||||||
press('KeyB');
|
|
||||||
press('KeyC');
|
|
||||||
await expect.element(screen.getByText('B', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
// Mid-chord the display tracks only the currently held keys...
|
|
||||||
release('KeyB');
|
|
||||||
await expect.element(screen.getByText('B', { exact: true })).not.toBeInTheDocument();
|
|
||||||
await expect.element(screen.getByText('A', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
// ...but the captured peak still includes the released key.
|
|
||||||
release('KeyA');
|
|
||||||
release('KeyC');
|
|
||||||
await expect.element(screen.getByText('B', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Save' }).click();
|
|
||||||
expect(onSave).toHaveBeenCalledExactlyOnceWith(['KeyA', 'KeyB', 'KeyC']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('replaces a longer saved chord with a fresh shorter one', async () => {
|
|
||||||
const { screen, onSave } = await renderPicker(['KeyA', 'KeyB', 'KeyC']);
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('A', { exact: true })).toBeVisible();
|
|
||||||
|
|
||||||
// The first key of a new sequence resets the peak, so a single key can
|
|
||||||
// beat the three-key seed.
|
|
||||||
press('KeyZ');
|
|
||||||
release('KeyZ');
|
|
||||||
await expect.element(screen.getByText('Z', { exact: true })).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('A', { exact: true })).not.toBeInTheDocument();
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Save' }).click();
|
|
||||||
expect(onSave).toHaveBeenCalledExactlyOnceWith(['KeyZ']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('cancel fires the cancel callback and never saves', async () => {
|
|
||||||
const { screen, onSave, onCancel } = await renderPicker(['KeyA']);
|
|
||||||
|
|
||||||
press('KeyQ');
|
|
||||||
release('KeyQ');
|
|
||||||
await screen.getByRole('button', { name: 'Cancel' }).click();
|
|
||||||
|
|
||||||
expect(onCancel).toHaveBeenCalledOnce();
|
|
||||||
expect(onSave).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
@@ -5,7 +5,6 @@ import { CapturePill } from '@/components/CapturePill/CapturePill';
|
|||||||
import { apiClient } from '@/lib/api/client';
|
import { apiClient } from '@/lib/api/client';
|
||||||
import type { FocusSnapshot } from '@/lib/api/types';
|
import type { FocusSnapshot } from '@/lib/api/types';
|
||||||
import { useCaptureRecordingSession } from '@/lib/hooks/useCaptureRecordingSession';
|
import { useCaptureRecordingSession } from '@/lib/hooks/useCaptureRecordingSession';
|
||||||
import { usePlatform } from '@/platform/PlatformContext';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Floating dictate surface shown in a separate transparent Tauri window.
|
* Floating dictate surface shown in a separate transparent Tauri window.
|
||||||
@@ -23,9 +22,6 @@ import { usePlatform } from '@/platform/PlatformContext';
|
|||||||
* ``dictate:hide`` so Rust tucks the window away.
|
* ``dictate:hide`` so Rust tucks the window away.
|
||||||
*/
|
*/
|
||||||
export function DictateWindow() {
|
export function DictateWindow() {
|
||||||
const platform = usePlatform();
|
|
||||||
const isTauri = platform.metadata.isTauri;
|
|
||||||
|
|
||||||
// Force the host document chrome to be transparent so the Tauri window
|
// Force the host document chrome to be transparent so the Tauri window
|
||||||
// takes on the pill's own shape.
|
// takes on the pill's own shape.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -39,17 +35,19 @@ export function DictateWindow() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Mirrored from the main window: true only when dictation is armed and the
|
// Snapshot of the focused UI element at chord-start, shipped over from
|
||||||
// user opted into keeping the microphone ready.
|
// Rust on the ``dictate:start`` payload. Held in a ref so it survives
|
||||||
const [micWarm, setMicWarm] = useState(false);
|
// the 1–2 s transcribe + refine window — the paste only fires once the
|
||||||
|
// final text comes back.
|
||||||
|
const focusRef = useRef<FocusSnapshot | null>(null);
|
||||||
|
|
||||||
const session = useCaptureRecordingSession({
|
const session = useCaptureRecordingSession({
|
||||||
keepMicWarm: micWarm,
|
onFinalText: async (text, _capture, allowAutoPaste) => {
|
||||||
onFinalText: async (text, _capture, allowAutoPaste, context) => {
|
const focus = focusRef.current;
|
||||||
// Focus is the snapshot taken at chord-start and threaded through as this
|
// Consume-once: a second chord before this fires would overwrite
|
||||||
// take's context, so it survives the 1–2 s transcribe + refine window and
|
// focusRef, but nulling it here guards against the late-arriving
|
||||||
// overlapping dictations can't paste into each other's target.
|
// refine-result firing a paste after the user has moved on.
|
||||||
const focus = context as FocusSnapshot | null;
|
focusRef.current = null;
|
||||||
if (!allowAutoPaste) return;
|
if (!allowAutoPaste) return;
|
||||||
if (!focus || !text.trim()) return;
|
if (!focus || !text.trim()) return;
|
||||||
try {
|
try {
|
||||||
@@ -74,41 +72,22 @@ export function DictateWindow() {
|
|||||||
sessionRef.current = session;
|
sessionRef.current = session;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isTauri) return;
|
const unlistens: Promise<UnlistenFn>[] = [];
|
||||||
let disposed = false;
|
unlistens.push(
|
||||||
const unlistens: UnlistenFn[] = [];
|
|
||||||
const registrations = [
|
|
||||||
listen<{ focus: FocusSnapshot | null }>('dictate:start', (event) => {
|
listen<{ focus: FocusSnapshot | null }>('dictate:start', (event) => {
|
||||||
sessionRef.current.startRecording(event.payload?.focus ?? null);
|
focusRef.current = event.payload?.focus ?? null;
|
||||||
|
sessionRef.current.startRecording();
|
||||||
}),
|
}),
|
||||||
|
);
|
||||||
|
unlistens.push(
|
||||||
listen('dictate:stop', () => {
|
listen('dictate:stop', () => {
|
||||||
// Forward stops that arrive while getUserMedia is still resolving.
|
if (sessionRef.current.isRecording) sessionRef.current.stopRecording();
|
||||||
sessionRef.current.stopRecording();
|
|
||||||
}),
|
}),
|
||||||
listen<boolean>('dictate:warm', (event) => {
|
);
|
||||||
setMicWarm(Boolean(event.payload));
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
Promise.all(registrations)
|
|
||||||
.then((registered) => {
|
|
||||||
if (disposed) {
|
|
||||||
for (const unlisten of registered) unlisten();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
unlistens.push(...registered);
|
|
||||||
emit('dictate:warm-request').catch(() => {});
|
|
||||||
})
|
|
||||||
.catch((err) => console.warn('[dictate] event listener registration failed:', err));
|
|
||||||
return () => {
|
return () => {
|
||||||
disposed = true;
|
for (const p of unlistens) p.then((fn) => fn()).catch(() => {});
|
||||||
for (const unlisten of unlistens) unlisten();
|
|
||||||
};
|
};
|
||||||
}, [isTauri]);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (micWarm) void session.prewarm();
|
|
||||||
else session.releaseWarm();
|
|
||||||
}, [micWarm, session.prewarm, session.releaseWarm]);
|
|
||||||
|
|
||||||
// --- Agent-speak cycle ---------------------------------------------------
|
// --- Agent-speak cycle ---------------------------------------------------
|
||||||
|
|
||||||
@@ -162,7 +141,9 @@ export function DictateWindow() {
|
|||||||
audio.onplaying = () => {
|
audio.onplaying = () => {
|
||||||
emit('dictate:show').catch(() => {});
|
emit('dictate:show').catch(() => {});
|
||||||
setSpeaking((prev) =>
|
setSpeaking((prev) =>
|
||||||
prev && prev.generationId === generationId ? { ...prev, startedAt: Date.now() } : prev,
|
prev && prev.generationId === generationId
|
||||||
|
? { ...prev, startedAt: Date.now() }
|
||||||
|
: prev,
|
||||||
);
|
);
|
||||||
setSpeakElapsed(0);
|
setSpeakElapsed(0);
|
||||||
};
|
};
|
||||||
@@ -174,7 +155,6 @@ export function DictateWindow() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isTauri) return;
|
|
||||||
const unlistens: Promise<UnlistenFn>[] = [];
|
const unlistens: Promise<UnlistenFn>[] = [];
|
||||||
|
|
||||||
// Rust emits the SSE payload as a JSON *string* (not a parsed object);
|
// Rust emits the SSE payload as a JSON *string* (not a parsed object);
|
||||||
@@ -269,7 +249,7 @@ export function DictateWindow() {
|
|||||||
for (const p of unlistens) p.then((fn) => fn()).catch(() => {});
|
for (const p of unlistens) p.then((fn) => fn()).catch(() => {});
|
||||||
dismissSpeak();
|
dismissSpeak();
|
||||||
};
|
};
|
||||||
}, [isTauri]);
|
}, []);
|
||||||
|
|
||||||
// Advance the pill's elapsed-time label while audio is playing. Paused
|
// Advance the pill's elapsed-time label while audio is playing. Paused
|
||||||
// during the pre-playback generation window (startedAt is null) so the
|
// during the pre-playback generation window (startedAt is null) so the
|
||||||
|
|||||||
@@ -1,185 +0,0 @@
|
|||||||
import { HttpResponse, http } from 'msw';
|
|
||||||
import { expect, it } from 'vitest';
|
|
||||||
import type { VoiceProfileResponse } from '@/lib/api/types';
|
|
||||||
import { useGenerationStore } from '@/stores/generationStore';
|
|
||||||
import { useUIStore } from '@/stores/uiStore';
|
|
||||||
import { buildGeneration, buildModelStatus, buildProfile } from '@/test/msw/fixtures';
|
|
||||||
import {
|
|
||||||
captureHandlers,
|
|
||||||
effectsHandlers,
|
|
||||||
historyHandlers,
|
|
||||||
modelHandlers,
|
|
||||||
profileHandlers,
|
|
||||||
settingsHandlers,
|
|
||||||
storyHandlers,
|
|
||||||
taskHandlers,
|
|
||||||
} from '@/test/msw/handlers';
|
|
||||||
import { worker } from '@/test/msw/worker';
|
|
||||||
import { renderRoute } from '@/test/render';
|
|
||||||
import { sseController } from '@/test/sse';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FloatingGenerateBox calls useMatchRoute, so it needs router context; the
|
|
||||||
* SSE completion loop (useGenerationProgress) lives in the router's root
|
|
||||||
* layout. Mounting the index route exercises the real wiring for both.
|
|
||||||
* History handlers are registered per test so requests can be counted.
|
|
||||||
*/
|
|
||||||
function stubAppRequests(profiles: VoiceProfileResponse[]) {
|
|
||||||
worker.use(
|
|
||||||
...profileHandlers(profiles),
|
|
||||||
...captureHandlers([]),
|
|
||||||
...settingsHandlers(),
|
|
||||||
...modelHandlers([buildModelStatus()]),
|
|
||||||
...storyHandlers([]),
|
|
||||||
...effectsHandlers([]),
|
|
||||||
...taskHandlers(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
it('renders the generate box wired to the selected profile', async () => {
|
|
||||||
const profile = buildProfile({ name: 'Ada Lovelace' });
|
|
||||||
stubAppRequests([profile]);
|
|
||||||
worker.use(...historyHandlers([]));
|
|
||||||
useUIStore.getState().setSelectedProfileId(profile.id);
|
|
||||||
|
|
||||||
const screen = await renderRoute('/');
|
|
||||||
|
|
||||||
await expect
|
|
||||||
.element(screen.getByPlaceholder('Generate speech using Ada Lovelace…'))
|
|
||||||
.toBeVisible();
|
|
||||||
await expect.element(screen.getByRole('button', { name: 'Generate speech' })).toBeEnabled();
|
|
||||||
expect(useUIStore.getState().selectedProfileId).toBe(profile.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('posts to /generate on submit and tracks the pending generation', async () => {
|
|
||||||
const profile = buildProfile({ name: 'Ada Lovelace' });
|
|
||||||
const generation = buildGeneration({
|
|
||||||
profile_id: profile.id,
|
|
||||||
status: 'generating',
|
|
||||||
audio_path: undefined,
|
|
||||||
});
|
|
||||||
const generateBodies: unknown[] = [];
|
|
||||||
const sse = sseController();
|
|
||||||
stubAppRequests([profile]);
|
|
||||||
worker.use(
|
|
||||||
...historyHandlers([]),
|
|
||||||
http.post('*/generate', async ({ request }) => {
|
|
||||||
generateBodies.push(await request.json());
|
|
||||||
return HttpResponse.json(generation);
|
|
||||||
}),
|
|
||||||
http.get('*/generate/:id/status', () => sse.response()),
|
|
||||||
);
|
|
||||||
useUIStore.getState().setSelectedProfileId(profile.id);
|
|
||||||
|
|
||||||
const screen = await renderRoute('/');
|
|
||||||
|
|
||||||
const input = screen.getByPlaceholder('Generate speech using Ada Lovelace…');
|
|
||||||
await input.fill('Hello from the browser test');
|
|
||||||
await screen.getByRole('button', { name: 'Generate speech' }).click();
|
|
||||||
|
|
||||||
await expect.poll(() => generateBodies.length).toBe(1);
|
|
||||||
expect(generateBodies[0]).toMatchObject({
|
|
||||||
profile_id: profile.id,
|
|
||||||
text: 'Hello from the browser test',
|
|
||||||
language: 'en',
|
|
||||||
engine: 'qwen',
|
|
||||||
});
|
|
||||||
await expect
|
|
||||||
.poll(() => useGenerationStore.getState().pendingGenerationIds.has(generation.id))
|
|
||||||
.toBe(true);
|
|
||||||
// The form resets as soon as the request is accepted.
|
|
||||||
await expect.element(input).toHaveValue('');
|
|
||||||
sse.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('clears pending state and refetches history when SSE reports completion', async () => {
|
|
||||||
const profile = buildProfile({ name: 'Ada Lovelace' });
|
|
||||||
const generation = buildGeneration({
|
|
||||||
profile_id: profile.id,
|
|
||||||
status: 'generating',
|
|
||||||
audio_path: undefined,
|
|
||||||
});
|
|
||||||
const sse = sseController();
|
|
||||||
let sseConnections = 0;
|
|
||||||
let historyGets = 0;
|
|
||||||
stubAppRequests([profile]);
|
|
||||||
worker.use(
|
|
||||||
http.get('*/history', () => {
|
|
||||||
historyGets += 1;
|
|
||||||
return HttpResponse.json({ items: [], total: 0 });
|
|
||||||
}),
|
|
||||||
http.post('*/generate', () => HttpResponse.json(generation)),
|
|
||||||
http.get('*/generate/:id/status', () => {
|
|
||||||
sseConnections += 1;
|
|
||||||
return sse.response();
|
|
||||||
}),
|
|
||||||
// Autoplay is off via settingsHandlers, but keep audio stubbed so a
|
|
||||||
// completion-triggered player fetch could never fail the run loudly.
|
|
||||||
http.get(
|
|
||||||
'*/audio/:id',
|
|
||||||
() =>
|
|
||||||
new HttpResponse(new Blob([new Uint8Array(64)]), {
|
|
||||||
headers: { 'Content-Type': 'audio/wav' },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
useUIStore.getState().setSelectedProfileId(profile.id);
|
|
||||||
|
|
||||||
const screen = await renderRoute('/');
|
|
||||||
|
|
||||||
await screen.getByPlaceholder('Generate speech using Ada Lovelace…').fill('Progress please');
|
|
||||||
await screen.getByRole('button', { name: 'Generate speech' }).click();
|
|
||||||
|
|
||||||
await expect
|
|
||||||
.poll(() => useGenerationStore.getState().pendingGenerationIds.has(generation.id))
|
|
||||||
.toBe(true);
|
|
||||||
await expect.poll(() => sseConnections).toBe(1);
|
|
||||||
// Initial mount fetch + post-submit invalidation — wait for both so the
|
|
||||||
// final count increase can only come from the SSE completion refetch.
|
|
||||||
await expect.poll(() => historyGets).toBe(2);
|
|
||||||
|
|
||||||
sse.push({ data: { id: generation.id, status: 'generating' } });
|
|
||||||
sse.push({ data: { id: generation.id, status: 'completed', duration: 1.5 } });
|
|
||||||
|
|
||||||
await expect.poll(() => useGenerationStore.getState().pendingGenerationIds.size).toBe(0);
|
|
||||||
await expect.poll(() => historyGets).toBe(3);
|
|
||||||
sse.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('disables the input and generate button when no profile is selected', async () => {
|
|
||||||
stubAppRequests([]);
|
|
||||||
worker.use(...historyHandlers([]));
|
|
||||||
|
|
||||||
const screen = await renderRoute('/');
|
|
||||||
|
|
||||||
await expect
|
|
||||||
.element(screen.getByRole('button', { name: 'Select a voice profile first' }))
|
|
||||||
.toBeDisabled();
|
|
||||||
await expect.element(screen.getByPlaceholder('Select a voice profile above…')).toBeDisabled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not post to /generate when the text is empty', async () => {
|
|
||||||
const profile = buildProfile({ name: 'Ada Lovelace' });
|
|
||||||
let generateCalls = 0;
|
|
||||||
stubAppRequests([profile]);
|
|
||||||
worker.use(
|
|
||||||
...historyHandlers([]),
|
|
||||||
http.post('*/generate', () => {
|
|
||||||
generateCalls += 1;
|
|
||||||
return HttpResponse.json(buildGeneration());
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
useUIStore.getState().setSelectedProfileId(profile.id);
|
|
||||||
|
|
||||||
const screen = await renderRoute('/');
|
|
||||||
|
|
||||||
const button = screen.getByRole('button', { name: 'Generate speech' });
|
|
||||||
await expect.element(button).toBeEnabled();
|
|
||||||
await button.click();
|
|
||||||
|
|
||||||
// Validation rejects empty text before any request is made — give a
|
|
||||||
// would-be submission ample time to surface, then assert it never did.
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
||||||
expect(generateCalls).toBe(0);
|
|
||||||
expect(useGenerationStore.getState().pendingGenerationIds.size).toBe(0);
|
|
||||||
});
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import { HttpResponse, http } from 'msw';
|
|
||||||
import { expect, it, vi } from 'vitest';
|
|
||||||
import { HistoryTable } from '@/components/History/HistoryTable';
|
|
||||||
import { usePlayerStore } from '@/stores/playerStore';
|
|
||||||
import { buildHistoryItem } from '@/test/msw/fixtures';
|
|
||||||
import { historyHandlers } from '@/test/msw/handlers';
|
|
||||||
import { worker } from '@/test/msw/worker';
|
|
||||||
import { renderWithProviders } from '@/test/render';
|
|
||||||
|
|
||||||
it('renders history rows with profile names and transcripts', async () => {
|
|
||||||
const ada = buildHistoryItem({
|
|
||||||
profile_name: 'Ada Lovelace',
|
|
||||||
text: 'The analytical engine speaks.',
|
|
||||||
});
|
|
||||||
const grace = buildHistoryItem({
|
|
||||||
profile_name: 'Grace Hopper',
|
|
||||||
text: 'A compiler for the spoken word.',
|
|
||||||
});
|
|
||||||
worker.use(...historyHandlers([ada, grace]));
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('Ada Lovelace')).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('Grace Hopper')).toBeVisible();
|
|
||||||
await expect
|
|
||||||
.element(screen.getByRole('textbox', { name: /Transcript for sample from Ada Lovelace/ }))
|
|
||||||
.toHaveValue('The analytical engine speaks.');
|
|
||||||
await expect
|
|
||||||
.element(screen.getByRole('textbox', { name: /Transcript for sample from Grace Hopper/ }))
|
|
||||||
.toHaveValue('A compiler for the spoken word.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('shows the empty state when there is no history', async () => {
|
|
||||||
worker.use(...historyHandlers([]));
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('No voice generations', { exact: false })).toBeVisible();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('loads a clicked row into the player store with auto-play intent', async () => {
|
|
||||||
const item = buildHistoryItem({ profile_name: 'Ada Lovelace', text: 'Play me back.' });
|
|
||||||
worker.use(...historyHandlers([item]));
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
// Click the profile-name cell — the row's mousedown handler ignores clicks
|
|
||||||
// that land on the transcript textarea.
|
|
||||||
await screen.getByText('Ada Lovelace').click();
|
|
||||||
|
|
||||||
await expect.poll(() => usePlayerStore.getState().audioId).toBe(item.id);
|
|
||||||
const player = usePlayerStore.getState();
|
|
||||||
expect(player.audioUrl).toContain(`/audio/${item.id}`);
|
|
||||||
expect(player.profileId).toBe(item.profile_id);
|
|
||||||
expect(player.shouldAutoPlay).toBe(true);
|
|
||||||
// isPlaying flips only once the AudioPlayer (not mounted here) starts playback.
|
|
||||||
expect(player.isPlaying).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('toggles favorite via POST and reflects the refetched state', async () => {
|
|
||||||
const item = buildHistoryItem({ profile_name: 'Ada Lovelace' });
|
|
||||||
let favorited = false;
|
|
||||||
const favoriteRequests: string[] = [];
|
|
||||||
worker.use(
|
|
||||||
http.get('*/history', () =>
|
|
||||||
HttpResponse.json({ items: [{ ...item, is_favorited: favorited }], total: 1 }),
|
|
||||||
),
|
|
||||||
http.post('*/history/:id/favorite', ({ params }) => {
|
|
||||||
favoriteRequests.push(params.id as string);
|
|
||||||
favorited = true;
|
|
||||||
return HttpResponse.json({ is_favorited: favorited });
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Favorite' }).click();
|
|
||||||
|
|
||||||
await expect.poll(() => favoriteRequests).toEqual([item.id]);
|
|
||||||
// History was invalidated and refetched — the star now reads as favorited.
|
|
||||||
await expect.element(screen.getByRole('button', { name: 'Unfavorite' })).toBeVisible();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('deletes a generation after confirming the dialog', async () => {
|
|
||||||
const item = buildHistoryItem({ profile_name: 'Ada Lovelace' });
|
|
||||||
let items = [item];
|
|
||||||
const deleteRequests: string[] = [];
|
|
||||||
worker.use(
|
|
||||||
http.get('*/history', () => HttpResponse.json({ items, total: items.length })),
|
|
||||||
http.delete('*/history/:id', ({ params }) => {
|
|
||||||
deleteRequests.push(params.id as string);
|
|
||||||
items = items.filter((i) => i.id !== params.id);
|
|
||||||
return HttpResponse.json({ status: 'deleted' });
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Actions' }).click();
|
|
||||||
await screen.getByRole('menuitem', { name: 'Delete' }).click();
|
|
||||||
await expect.element(screen.getByText('Delete Generation')).toBeVisible();
|
|
||||||
await screen.getByRole('button', { name: 'Delete' }).click();
|
|
||||||
|
|
||||||
await expect.poll(() => deleteRequests).toEqual([item.id]);
|
|
||||||
// The refetched (now empty) list replaces the row.
|
|
||||||
await expect.element(screen.getByText('No voice generations', { exact: false })).toBeVisible();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('exports audio through platform.filesystem.saveFile', async () => {
|
|
||||||
const item = buildHistoryItem({ profile_name: 'Ada Lovelace', text: 'Export me please' });
|
|
||||||
worker.use(
|
|
||||||
...historyHandlers([item]),
|
|
||||||
http.get(
|
|
||||||
'*/history/:id/export-audio',
|
|
||||||
() =>
|
|
||||||
new HttpResponse(new Blob([new Uint8Array(64)]), {
|
|
||||||
headers: { 'Content-Type': 'audio/wav' },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<HistoryTable />);
|
|
||||||
|
|
||||||
await screen.getByRole('button', { name: 'Actions' }).click();
|
|
||||||
await screen.getByRole('menuitem', { name: 'Export Audio' }).click();
|
|
||||||
|
|
||||||
const saveFile = vi.mocked(screen.platform.filesystem.saveFile);
|
|
||||||
await expect.poll(() => saveFile.mock.calls.length).toBe(1);
|
|
||||||
const [filename, blob, filters] = saveFile.mock.calls[0];
|
|
||||||
expect(filename).toBe('export-me-please.wav');
|
|
||||||
expect(blob).toBeInstanceOf(Blob);
|
|
||||||
expect(filters).toEqual([{ name: 'Audio File', extensions: ['wav'] }]);
|
|
||||||
});
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { expect, it } from 'vitest';
|
|
||||||
import { AboutPage } from '@/components/ServerTab/AboutPage';
|
|
||||||
import { createMockPlatform } from '@/test/mockPlatform';
|
|
||||||
import { renderWithProviders } from '@/test/render';
|
|
||||||
|
|
||||||
it('renders and shows the platform version', async () => {
|
|
||||||
const platform = createMockPlatform({
|
|
||||||
metadata: { getVersion: async () => '9.9.9-test', isTauri: false },
|
|
||||||
});
|
|
||||||
|
|
||||||
const screen = await renderWithProviders(<AboutPage />, { platform });
|
|
||||||
|
|
||||||
await expect.element(screen.getByAltText('Voicebox')).toBeVisible();
|
|
||||||
await expect.element(screen.getByText('9.9.9-test', { exact: false })).toBeVisible();
|
|
||||||
});
|
|
||||||
@@ -138,7 +138,6 @@ export function CapturesPage() {
|
|||||||
const allowAutoPaste = settings?.allow_auto_paste ?? true;
|
const allowAutoPaste = settings?.allow_auto_paste ?? true;
|
||||||
const defaultVoiceId = settings?.default_playback_voice_id ?? null;
|
const defaultVoiceId = settings?.default_playback_voice_id ?? null;
|
||||||
const hotkeyEnabled = settings?.hotkey_enabled ?? false;
|
const hotkeyEnabled = settings?.hotkey_enabled ?? false;
|
||||||
const keepMicWarm = settings?.keep_mic_warm ?? false;
|
|
||||||
const pushToTalkKeys = settings?.chord_push_to_talk_keys ?? defaultChordKeys('push');
|
const pushToTalkKeys = settings?.chord_push_to_talk_keys ?? defaultChordKeys('push');
|
||||||
const toggleToTalkKeys = settings?.chord_toggle_to_talk_keys ?? defaultChordKeys('toggle');
|
const toggleToTalkKeys = settings?.chord_toggle_to_talk_keys ?? defaultChordKeys('toggle');
|
||||||
|
|
||||||
@@ -222,22 +221,6 @@ export function CapturesPage() {
|
|||||||
<InputMonitoringNotice enabled={hotkeyEnabled} />
|
<InputMonitoringNotice enabled={hotkeyEnabled} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<SettingRow
|
|
||||||
title={t('settings.captures.dictation.keepMicWarm.title')}
|
|
||||||
description={t('settings.captures.dictation.keepMicWarm.description')}
|
|
||||||
htmlFor="keepMicWarm"
|
|
||||||
action={
|
|
||||||
<Toggle
|
|
||||||
id="keepMicWarm"
|
|
||||||
checked={keepMicWarm}
|
|
||||||
disabled={!hotkeyEnabled}
|
|
||||||
onCheckedChange={(v) => {
|
|
||||||
update({ keep_mic_warm: v });
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<SettingRow
|
<SettingRow
|
||||||
title={t('settings.captures.dictation.pushToTalk.title')}
|
title={t('settings.captures.dictation.pushToTalk.title')}
|
||||||
description={t('settings.captures.dictation.pushToTalk.description')}
|
description={t('settings.captures.dictation.pushToTalk.description')}
|
||||||
|
|||||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
@@ -0,0 +1,61 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { usePlatform } from '@/platform/PlatformContext';
|
||||||
|
import type { UpdateStatus } from '@/platform/types';
|
||||||
|
|
||||||
|
// Re-export UpdateStatus for backwards compatibility
|
||||||
|
export type { UpdateStatus };
|
||||||
|
|
||||||
|
interface UseAutoUpdaterOptions {
|
||||||
|
checkOnMount?: boolean;
|
||||||
|
showToast?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useAutoUpdater(options: boolean | UseAutoUpdaterOptions = false) {
|
||||||
|
const { checkOnMount } =
|
||||||
|
typeof options === 'boolean' ? { checkOnMount: options } : { checkOnMount: options.checkOnMount ?? false };
|
||||||
|
|
||||||
|
const platform = usePlatform();
|
||||||
|
const [status, setStatus] = useState<UpdateStatus>(platform.updater.getStatus());
|
||||||
|
const hasCheckedRef = useRef(false);
|
||||||
|
|
||||||
|
// Subscribe to updater status changes
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = platform.updater.subscribe((newStatus) => {
|
||||||
|
setStatus(newStatus);
|
||||||
|
});
|
||||||
|
return unsubscribe;
|
||||||
|
// Empty dependency array - platform is stable from context
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [platform.updater.subscribe]);
|
||||||
|
|
||||||
|
const checkForUpdates = useCallback(async () => {
|
||||||
|
await platform.updater.checkForUpdates();
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [platform.updater.checkForUpdates]);
|
||||||
|
|
||||||
|
const downloadAndInstall = useCallback(async () => {
|
||||||
|
await platform.updater.downloadAndInstall();
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [platform.updater.downloadAndInstall]);
|
||||||
|
|
||||||
|
const restartAndInstall = useCallback(async () => {
|
||||||
|
await platform.updater.restartAndInstall();
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [platform.updater.restartAndInstall]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (checkOnMount && platform.metadata.isTauri && !hasCheckedRef.current) {
|
||||||
|
hasCheckedRef.current = true;
|
||||||
|
checkForUpdates().catch((error) => {
|
||||||
|
console.error('Auto update check failed:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [checkOnMount, checkForUpdates, platform.metadata.isTauri]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status,
|
||||||
|
checkForUpdates,
|
||||||
|
downloadAndInstall,
|
||||||
|
restartAndInstall,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -887,10 +887,6 @@
|
|||||||
"title": "Global shortcut",
|
"title": "Global shortcut",
|
||||||
"description": "Hold the shortcut to record from anywhere on your machine. Release to transcribe."
|
"description": "Hold the shortcut to record from anywhere on your machine. Release to transcribe."
|
||||||
},
|
},
|
||||||
"keepMicWarm": {
|
|
||||||
"title": "Keep microphone ready",
|
|
||||||
"description": "Hold the microphone open while dictation is enabled so the first words are never clipped. The macOS microphone indicator stays lit while it's on."
|
|
||||||
},
|
|
||||||
"pushToTalk": {
|
"pushToTalk": {
|
||||||
"title": "Push-to-talk shortcut",
|
"title": "Push-to-talk shortcut",
|
||||||
"description": "Hold these keys anywhere on your system to record. Release to stop and transcribe.",
|
"description": "Hold these keys anywhere on your system to record. Release to stop and transcribe.",
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { ApiRequestOptions } from './ApiRequestOptions';
|
||||||
|
import type { ApiResult } from './ApiResult';
|
||||||
|
|
||||||
|
export class ApiError extends Error {
|
||||||
|
public readonly url: string;
|
||||||
|
public readonly status: number;
|
||||||
|
public readonly statusText: string;
|
||||||
|
public readonly body: any;
|
||||||
|
public readonly request: ApiRequestOptions;
|
||||||
|
|
||||||
|
constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
|
||||||
|
super(message);
|
||||||
|
|
||||||
|
this.name = 'ApiError';
|
||||||
|
this.url = response.url;
|
||||||
|
this.status = response.status;
|
||||||
|
this.statusText = response.statusText;
|
||||||
|
this.body = response.body;
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export type ApiRequestOptions = {
|
||||||
|
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
|
||||||
|
readonly url: string;
|
||||||
|
readonly path?: Record<string, any>;
|
||||||
|
readonly cookies?: Record<string, any>;
|
||||||
|
readonly headers?: Record<string, any>;
|
||||||
|
readonly query?: Record<string, any>;
|
||||||
|
readonly formData?: Record<string, any>;
|
||||||
|
readonly body?: any;
|
||||||
|
readonly mediaType?: string;
|
||||||
|
readonly responseHeader?: string;
|
||||||
|
readonly errors?: Record<number, string>;
|
||||||
|
};
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export type ApiResult = {
|
||||||
|
readonly url: string;
|
||||||
|
readonly ok: boolean;
|
||||||
|
readonly status: number;
|
||||||
|
readonly statusText: string;
|
||||||
|
readonly body: any;
|
||||||
|
};
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export class CancelError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'CancelError';
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isCancelled(): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OnCancel {
|
||||||
|
readonly isResolved: boolean;
|
||||||
|
readonly isRejected: boolean;
|
||||||
|
readonly isCancelled: boolean;
|
||||||
|
|
||||||
|
(cancelHandler: () => void): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CancelablePromise<T> implements Promise<T> {
|
||||||
|
#isResolved: boolean;
|
||||||
|
#isRejected: boolean;
|
||||||
|
#isCancelled: boolean;
|
||||||
|
readonly #cancelHandlers: (() => void)[];
|
||||||
|
readonly #promise: Promise<T>;
|
||||||
|
#resolve?: (value: T | PromiseLike<T>) => void;
|
||||||
|
#reject?: (reason?: any) => void;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
executor: (
|
||||||
|
resolve: (value: T | PromiseLike<T>) => void,
|
||||||
|
reject: (reason?: any) => void,
|
||||||
|
onCancel: OnCancel,
|
||||||
|
) => void,
|
||||||
|
) {
|
||||||
|
this.#isResolved = false;
|
||||||
|
this.#isRejected = false;
|
||||||
|
this.#isCancelled = false;
|
||||||
|
this.#cancelHandlers = [];
|
||||||
|
this.#promise = new Promise<T>((resolve, reject) => {
|
||||||
|
this.#resolve = resolve;
|
||||||
|
this.#reject = reject;
|
||||||
|
|
||||||
|
const onResolve = (value: T | PromiseLike<T>): void => {
|
||||||
|
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#isResolved = true;
|
||||||
|
if (this.#resolve) this.#resolve(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onReject = (reason?: any): void => {
|
||||||
|
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#isRejected = true;
|
||||||
|
if (this.#reject) this.#reject(reason);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onCancel = (cancelHandler: () => void): void => {
|
||||||
|
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#cancelHandlers.push(cancelHandler);
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, 'isResolved', {
|
||||||
|
get: (): boolean => this.#isResolved,
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, 'isRejected', {
|
||||||
|
get: (): boolean => this.#isRejected,
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, 'isCancelled', {
|
||||||
|
get: (): boolean => this.#isCancelled,
|
||||||
|
});
|
||||||
|
|
||||||
|
return executor(onResolve, onReject, onCancel as OnCancel);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get [Symbol.toStringTag]() {
|
||||||
|
return 'Cancellable Promise';
|
||||||
|
}
|
||||||
|
|
||||||
|
public then<TResult1 = T, TResult2 = never>(
|
||||||
|
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
|
||||||
|
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
|
||||||
|
): Promise<TResult1 | TResult2> {
|
||||||
|
return this.#promise.then(onFulfilled, onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public catch<TResult = never>(
|
||||||
|
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
|
||||||
|
): Promise<T | TResult> {
|
||||||
|
return this.#promise.catch(onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public finally(onFinally?: (() => void) | null): Promise<T> {
|
||||||
|
return this.#promise.finally(onFinally);
|
||||||
|
}
|
||||||
|
|
||||||
|
public cancel(): void {
|
||||||
|
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#isCancelled = true;
|
||||||
|
if (this.#cancelHandlers.length) {
|
||||||
|
try {
|
||||||
|
for (const cancelHandler of this.#cancelHandlers) {
|
||||||
|
cancelHandler();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Cancellation threw an error', error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.#cancelHandlers.length = 0;
|
||||||
|
if (this.#reject) this.#reject(new CancelError('Request aborted'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isCancelled(): boolean {
|
||||||
|
return this.#isCancelled;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { ApiRequestOptions } from './ApiRequestOptions';
|
||||||
|
|
||||||
|
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
||||||
|
type Headers = Record<string, string>;
|
||||||
|
|
||||||
|
export type OpenAPIConfig = {
|
||||||
|
BASE: string;
|
||||||
|
VERSION: string;
|
||||||
|
WITH_CREDENTIALS: boolean;
|
||||||
|
CREDENTIALS: 'include' | 'omit' | 'same-origin';
|
||||||
|
TOKEN?: string | Resolver<string> | undefined;
|
||||||
|
USERNAME?: string | Resolver<string> | undefined;
|
||||||
|
PASSWORD?: string | Resolver<string> | undefined;
|
||||||
|
HEADERS?: Headers | Resolver<Headers> | undefined;
|
||||||
|
ENCODE_PATH?: ((path: string) => string) | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const OpenAPI: OpenAPIConfig = {
|
||||||
|
BASE: '',
|
||||||
|
VERSION: '0.1.0',
|
||||||
|
WITH_CREDENTIALS: false,
|
||||||
|
CREDENTIALS: 'include',
|
||||||
|
TOKEN: undefined,
|
||||||
|
USERNAME: undefined,
|
||||||
|
PASSWORD: undefined,
|
||||||
|
HEADERS: undefined,
|
||||||
|
ENCODE_PATH: undefined,
|
||||||
|
};
|
||||||
@@ -0,0 +1,341 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import { ApiError } from './ApiError';
|
||||||
|
import type { ApiRequestOptions } from './ApiRequestOptions';
|
||||||
|
import type { ApiResult } from './ApiResult';
|
||||||
|
import { CancelablePromise } from './CancelablePromise';
|
||||||
|
import type { OnCancel } from './CancelablePromise';
|
||||||
|
import type { OpenAPIConfig } from './OpenAPI';
|
||||||
|
|
||||||
|
export const isDefined = <T>(
|
||||||
|
value: T | null | undefined,
|
||||||
|
): value is Exclude<T, null | undefined> => {
|
||||||
|
return value !== undefined && value !== null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isString = (value: any): value is string => {
|
||||||
|
return typeof value === 'string';
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isStringWithValue = (value: any): value is string => {
|
||||||
|
return isString(value) && value !== '';
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isBlob = (value: any): value is Blob => {
|
||||||
|
return (
|
||||||
|
typeof value === 'object' &&
|
||||||
|
typeof value.type === 'string' &&
|
||||||
|
typeof value.stream === 'function' &&
|
||||||
|
typeof value.arrayBuffer === 'function' &&
|
||||||
|
typeof value.constructor === 'function' &&
|
||||||
|
typeof value.constructor.name === 'string' &&
|
||||||
|
/^(Blob|File)$/.test(value.constructor.name) &&
|
||||||
|
/^(Blob|File)$/.test(value[Symbol.toStringTag])
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isFormData = (value: any): value is FormData => {
|
||||||
|
return value instanceof FormData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const base64 = (str: string): string => {
|
||||||
|
try {
|
||||||
|
return btoa(str);
|
||||||
|
} catch (err) {
|
||||||
|
// @ts-ignore
|
||||||
|
return Buffer.from(str).toString('base64');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getQueryString = (params: Record<string, any>): string => {
|
||||||
|
const qs: string[] = [];
|
||||||
|
|
||||||
|
const append = (key: string, value: any) => {
|
||||||
|
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const process = (key: string, value: any) => {
|
||||||
|
if (isDefined(value)) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((v) => {
|
||||||
|
process(key, v);
|
||||||
|
});
|
||||||
|
} else if (typeof value === 'object') {
|
||||||
|
Object.entries(value).forEach(([k, v]) => {
|
||||||
|
process(`${key}[${k}]`, v);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
append(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.entries(params).forEach(([key, value]) => {
|
||||||
|
process(key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (qs.length > 0) {
|
||||||
|
return `?${qs.join('&')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
|
||||||
|
const encoder = config.ENCODE_PATH || encodeURI;
|
||||||
|
|
||||||
|
const path = options.url
|
||||||
|
.replace('{api-version}', config.VERSION)
|
||||||
|
.replace(/{(.*?)}/g, (substring: string, group: string) => {
|
||||||
|
if (options.path?.hasOwnProperty(group)) {
|
||||||
|
return encoder(String(options.path[group]));
|
||||||
|
}
|
||||||
|
return substring;
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = `${config.BASE}${path}`;
|
||||||
|
if (options.query) {
|
||||||
|
return `${url}${getQueryString(options.query)}`;
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
|
||||||
|
if (options.formData) {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
const process = (key: string, value: any) => {
|
||||||
|
if (isString(value) || isBlob(value)) {
|
||||||
|
formData.append(key, value);
|
||||||
|
} else {
|
||||||
|
formData.append(key, JSON.stringify(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.entries(options.formData)
|
||||||
|
.filter(([_, value]) => isDefined(value))
|
||||||
|
.forEach(([key, value]) => {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((v) => process(key, v));
|
||||||
|
} else {
|
||||||
|
process(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
|
||||||
|
|
||||||
|
export const resolve = async <T>(
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
resolver?: T | Resolver<T>,
|
||||||
|
): Promise<T | undefined> => {
|
||||||
|
if (typeof resolver === 'function') {
|
||||||
|
return (resolver as Resolver<T>)(options);
|
||||||
|
}
|
||||||
|
return resolver;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getHeaders = async (
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
): Promise<Headers> => {
|
||||||
|
const [token, username, password, additionalHeaders] = await Promise.all([
|
||||||
|
resolve(options, config.TOKEN),
|
||||||
|
resolve(options, config.USERNAME),
|
||||||
|
resolve(options, config.PASSWORD),
|
||||||
|
resolve(options, config.HEADERS),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const headers = Object.entries({
|
||||||
|
Accept: 'application/json',
|
||||||
|
...additionalHeaders,
|
||||||
|
...options.headers,
|
||||||
|
})
|
||||||
|
.filter(([_, value]) => isDefined(value))
|
||||||
|
.reduce(
|
||||||
|
(headers, [key, value]) => ({
|
||||||
|
...headers,
|
||||||
|
[key]: String(value),
|
||||||
|
}),
|
||||||
|
{} as Record<string, string>,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isStringWithValue(token)) {
|
||||||
|
headers['Authorization'] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStringWithValue(username) && isStringWithValue(password)) {
|
||||||
|
const credentials = base64(`${username}:${password}`);
|
||||||
|
headers['Authorization'] = `Basic ${credentials}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.body !== undefined) {
|
||||||
|
if (options.mediaType) {
|
||||||
|
headers['Content-Type'] = options.mediaType;
|
||||||
|
} else if (isBlob(options.body)) {
|
||||||
|
headers['Content-Type'] = options.body.type || 'application/octet-stream';
|
||||||
|
} else if (isString(options.body)) {
|
||||||
|
headers['Content-Type'] = 'text/plain';
|
||||||
|
} else if (!isFormData(options.body)) {
|
||||||
|
headers['Content-Type'] = 'application/json';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Headers(headers);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRequestBody = (options: ApiRequestOptions): any => {
|
||||||
|
if (options.body !== undefined) {
|
||||||
|
if (options.mediaType?.includes('/json')) {
|
||||||
|
return JSON.stringify(options.body);
|
||||||
|
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||||
|
return options.body;
|
||||||
|
} else {
|
||||||
|
return JSON.stringify(options.body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const sendRequest = async (
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
url: string,
|
||||||
|
body: any,
|
||||||
|
formData: FormData | undefined,
|
||||||
|
headers: Headers,
|
||||||
|
onCancel: OnCancel,
|
||||||
|
): Promise<Response> => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
const request: RequestInit = {
|
||||||
|
headers,
|
||||||
|
body: body ?? formData,
|
||||||
|
method: options.method,
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (config.WITH_CREDENTIALS) {
|
||||||
|
request.credentials = config.CREDENTIALS;
|
||||||
|
}
|
||||||
|
|
||||||
|
onCancel(() => controller.abort());
|
||||||
|
|
||||||
|
return await fetch(url, request);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getResponseHeader = (
|
||||||
|
response: Response,
|
||||||
|
responseHeader?: string,
|
||||||
|
): string | undefined => {
|
||||||
|
if (responseHeader) {
|
||||||
|
const content = response.headers.get(responseHeader);
|
||||||
|
if (isString(content)) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getResponseBody = async (response: Response): Promise<any> => {
|
||||||
|
if (response.status !== 204) {
|
||||||
|
try {
|
||||||
|
const contentType = response.headers.get('Content-Type');
|
||||||
|
if (contentType) {
|
||||||
|
const jsonTypes = ['application/json', 'application/problem+json'];
|
||||||
|
const isJSON = jsonTypes.some((type) => contentType.toLowerCase().startsWith(type));
|
||||||
|
if (isJSON) {
|
||||||
|
return await response.json();
|
||||||
|
} else {
|
||||||
|
return await response.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
|
||||||
|
const errors: Record<number, string> = {
|
||||||
|
400: 'Bad Request',
|
||||||
|
401: 'Unauthorized',
|
||||||
|
403: 'Forbidden',
|
||||||
|
404: 'Not Found',
|
||||||
|
500: 'Internal Server Error',
|
||||||
|
502: 'Bad Gateway',
|
||||||
|
503: 'Service Unavailable',
|
||||||
|
...options.errors,
|
||||||
|
};
|
||||||
|
|
||||||
|
const error = errors[result.status];
|
||||||
|
if (error) {
|
||||||
|
throw new ApiError(options, result, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
const errorStatus = result.status ?? 'unknown';
|
||||||
|
const errorStatusText = result.statusText ?? 'unknown';
|
||||||
|
const errorBody = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.stringify(result.body, null, 2);
|
||||||
|
} catch (e) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
throw new ApiError(
|
||||||
|
options,
|
||||||
|
result,
|
||||||
|
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request method
|
||||||
|
* @param config The OpenAPI configuration object
|
||||||
|
* @param options The request options from the service
|
||||||
|
* @returns CancelablePromise<T>
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
export const request = <T>(
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
): CancelablePromise<T> => {
|
||||||
|
return new CancelablePromise(async (resolve, reject, onCancel) => {
|
||||||
|
try {
|
||||||
|
const url = getUrl(config, options);
|
||||||
|
const formData = getFormData(options);
|
||||||
|
const body = getRequestBody(options);
|
||||||
|
const headers = await getHeaders(config, options);
|
||||||
|
|
||||||
|
if (!onCancel.isCancelled) {
|
||||||
|
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
|
||||||
|
const responseBody = await getResponseBody(response);
|
||||||
|
const responseHeader = getResponseHeader(response, options.responseHeader);
|
||||||
|
|
||||||
|
const result: ApiResult = {
|
||||||
|
url,
|
||||||
|
ok: response.ok,
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
body: responseHeader ?? responseBody,
|
||||||
|
};
|
||||||
|
|
||||||
|
catchErrorCodes(options, result);
|
||||||
|
|
||||||
|
resolve(result.body);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export { ApiError } from './core/ApiError';
|
||||||
|
export { CancelablePromise, CancelError } from './core/CancelablePromise';
|
||||||
|
export { OpenAPI } from './core/OpenAPI';
|
||||||
|
export type { OpenAPIConfig } from './core/OpenAPI';
|
||||||
|
|
||||||
|
export type { Body_add_profile_sample_profiles__profile_id__samples_post } from './models/Body_add_profile_sample_profiles__profile_id__samples_post';
|
||||||
|
export type { Body_transcribe_audio_transcribe_post } from './models/Body_transcribe_audio_transcribe_post';
|
||||||
|
export type { GenerationRequest } from './models/GenerationRequest';
|
||||||
|
export type { GenerationResponse } from './models/GenerationResponse';
|
||||||
|
export type { HealthResponse } from './models/HealthResponse';
|
||||||
|
export type { HistoryListResponse } from './models/HistoryListResponse';
|
||||||
|
export type { HistoryResponse } from './models/HistoryResponse';
|
||||||
|
export type { HTTPValidationError } from './models/HTTPValidationError';
|
||||||
|
export type { ModelDownloadRequest } from './models/ModelDownloadRequest';
|
||||||
|
export type { ModelStatus } from './models/ModelStatus';
|
||||||
|
export type { ModelStatusListResponse } from './models/ModelStatusListResponse';
|
||||||
|
export type { ProfileSampleResponse } from './models/ProfileSampleResponse';
|
||||||
|
export type { TranscriptionResponse } from './models/TranscriptionResponse';
|
||||||
|
export type { ValidationError } from './models/ValidationError';
|
||||||
|
export type { VoiceProfileCreate } from './models/VoiceProfileCreate';
|
||||||
|
export type { VoiceProfileResponse } from './models/VoiceProfileResponse';
|
||||||
|
|
||||||
|
export { $Body_add_profile_sample_profiles__profile_id__samples_post } from './schemas/$Body_add_profile_sample_profiles__profile_id__samples_post';
|
||||||
|
export { $Body_transcribe_audio_transcribe_post } from './schemas/$Body_transcribe_audio_transcribe_post';
|
||||||
|
export { $GenerationRequest } from './schemas/$GenerationRequest';
|
||||||
|
export { $GenerationResponse } from './schemas/$GenerationResponse';
|
||||||
|
export { $HealthResponse } from './schemas/$HealthResponse';
|
||||||
|
export { $HistoryListResponse } from './schemas/$HistoryListResponse';
|
||||||
|
export { $HistoryResponse } from './schemas/$HistoryResponse';
|
||||||
|
export { $HTTPValidationError } from './schemas/$HTTPValidationError';
|
||||||
|
export { $ModelDownloadRequest } from './schemas/$ModelDownloadRequest';
|
||||||
|
export { $ModelStatus } from './schemas/$ModelStatus';
|
||||||
|
export { $ModelStatusListResponse } from './schemas/$ModelStatusListResponse';
|
||||||
|
export { $ProfileSampleResponse } from './schemas/$ProfileSampleResponse';
|
||||||
|
export { $TranscriptionResponse } from './schemas/$TranscriptionResponse';
|
||||||
|
export { $ValidationError } from './schemas/$ValidationError';
|
||||||
|
export { $VoiceProfileCreate } from './schemas/$VoiceProfileCreate';
|
||||||
|
export { $VoiceProfileResponse } from './schemas/$VoiceProfileResponse';
|
||||||
|
|
||||||
|
export { DefaultService } from './services/DefaultService';
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export type Body_add_profile_sample_profiles__profile_id__samples_post = {
|
||||||
|
file: Blob;
|
||||||
|
reference_text: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export type Body_transcribe_audio_transcribe_post = {
|
||||||
|
file: Blob;
|
||||||
|
language?: string | null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Request model for voice generation.
|
||||||
|
*/
|
||||||
|
export type GenerationRequest = {
|
||||||
|
profile_id: string;
|
||||||
|
text: string;
|
||||||
|
language?: string;
|
||||||
|
seed?: number | null;
|
||||||
|
model_size?: string | null;
|
||||||
|
instruct?: string | null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for voice generation.
|
||||||
|
*/
|
||||||
|
export type GenerationResponse = {
|
||||||
|
id: string;
|
||||||
|
profile_id: string;
|
||||||
|
text: string;
|
||||||
|
language: string;
|
||||||
|
audio_path: string;
|
||||||
|
duration: number;
|
||||||
|
seed: number | null;
|
||||||
|
instruct: string | null;
|
||||||
|
created_at: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { ValidationError } from './ValidationError';
|
||||||
|
export type HTTPValidationError = {
|
||||||
|
detail?: Array<ValidationError>;
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for health check.
|
||||||
|
*/
|
||||||
|
export type HealthResponse = {
|
||||||
|
status: string;
|
||||||
|
model_loaded: boolean;
|
||||||
|
model_downloaded?: boolean | null;
|
||||||
|
model_size?: string | null;
|
||||||
|
gpu_available: boolean;
|
||||||
|
vram_used_mb?: number | null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { HistoryResponse } from './HistoryResponse';
|
||||||
|
/**
|
||||||
|
* Response model for history list.
|
||||||
|
*/
|
||||||
|
export type HistoryListResponse = {
|
||||||
|
items: Array<HistoryResponse>;
|
||||||
|
total: number;
|
||||||
|
};
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for history entry (includes profile name).
|
||||||
|
*/
|
||||||
|
export type HistoryResponse = {
|
||||||
|
id: string;
|
||||||
|
profile_id: string;
|
||||||
|
profile_name: string;
|
||||||
|
text: string;
|
||||||
|
language: string;
|
||||||
|
audio_path: string;
|
||||||
|
duration: number;
|
||||||
|
seed: number | null;
|
||||||
|
instruct: string | null;
|
||||||
|
created_at: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Request model for triggering model download.
|
||||||
|
*/
|
||||||
|
export type ModelDownloadRequest = {
|
||||||
|
model_name: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for model status.
|
||||||
|
*/
|
||||||
|
export type ModelStatus = {
|
||||||
|
model_name: string;
|
||||||
|
display_name: string;
|
||||||
|
downloaded: boolean;
|
||||||
|
downloading?: boolean; // True if download is in progress
|
||||||
|
size_mb?: number | null;
|
||||||
|
loaded?: boolean;
|
||||||
|
};
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { ModelStatus } from './ModelStatus';
|
||||||
|
/**
|
||||||
|
* Response model for model status list.
|
||||||
|
*/
|
||||||
|
export type ModelStatusListResponse = {
|
||||||
|
models: Array<ModelStatus>;
|
||||||
|
};
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for profile sample.
|
||||||
|
*/
|
||||||
|
export type ProfileSampleResponse = {
|
||||||
|
id: string;
|
||||||
|
profile_id: string;
|
||||||
|
audio_path: string;
|
||||||
|
reference_text: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for transcription.
|
||||||
|
*/
|
||||||
|
export type TranscriptionResponse = {
|
||||||
|
text: string;
|
||||||
|
duration: number;
|
||||||
|
language?: string | null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export type ValidationError = {
|
||||||
|
loc: Array<string | number>;
|
||||||
|
msg: string;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Request model for creating a voice profile.
|
||||||
|
*/
|
||||||
|
export type VoiceProfileCreate = {
|
||||||
|
name: string;
|
||||||
|
description?: string | null;
|
||||||
|
language?: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
/**
|
||||||
|
* Response model for voice profile.
|
||||||
|
*/
|
||||||
|
export type VoiceProfileResponse = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
description: string | null;
|
||||||
|
language: string;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
};
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $Body_add_profile_sample_profiles__profile_id__samples_post = {
|
||||||
|
properties: {
|
||||||
|
file: {
|
||||||
|
type: 'binary',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'binary',
|
||||||
|
},
|
||||||
|
reference_text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $Body_transcribe_audio_transcribe_post = {
|
||||||
|
properties: {
|
||||||
|
file: {
|
||||||
|
type: 'binary',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'binary',
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $GenerationRequest = {
|
||||||
|
description: `Request model for voice generation.`,
|
||||||
|
properties: {
|
||||||
|
profile_id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
maxLength: 5000,
|
||||||
|
minLength: 1,
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'string',
|
||||||
|
pattern: '^(en|zh)$',
|
||||||
|
},
|
||||||
|
seed: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
model_size: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
pattern: '^(1\\.7B|0\\.6B)$',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $GenerationResponse = {
|
||||||
|
description: `Response model for voice generation.`,
|
||||||
|
properties: {
|
||||||
|
id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
profile_id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
audio_path: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
duration: {
|
||||||
|
type: 'number',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
seed: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'date-time',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $HTTPValidationError = {
|
||||||
|
properties: {
|
||||||
|
detail: {
|
||||||
|
type: 'array',
|
||||||
|
contains: {
|
||||||
|
type: 'ValidationError',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $HealthResponse = {
|
||||||
|
description: `Response model for health check.`,
|
||||||
|
properties: {
|
||||||
|
status: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
model_loaded: {
|
||||||
|
type: 'boolean',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
model_downloaded: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'boolean',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
model_size: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
gpu_available: {
|
||||||
|
type: 'boolean',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
vram_used_mb: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $HistoryListResponse = {
|
||||||
|
description: `Response model for history list.`,
|
||||||
|
properties: {
|
||||||
|
items: {
|
||||||
|
type: 'array',
|
||||||
|
contains: {
|
||||||
|
type: 'HistoryResponse',
|
||||||
|
},
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
total: {
|
||||||
|
type: 'number',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $HistoryResponse = {
|
||||||
|
description: `Response model for history entry (includes profile name).`,
|
||||||
|
properties: {
|
||||||
|
id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
profile_id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
profile_name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
audio_path: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
duration: {
|
||||||
|
type: 'number',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
seed: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'date-time',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $ModelDownloadRequest = {
|
||||||
|
description: `Request model for triggering model download.`,
|
||||||
|
properties: {
|
||||||
|
model_name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $ModelStatus = {
|
||||||
|
description: `Response model for model status.`,
|
||||||
|
properties: {
|
||||||
|
model_name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
display_name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
downloaded: {
|
||||||
|
type: 'boolean',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
size_mb: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
loaded: {
|
||||||
|
type: 'boolean',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $ModelStatusListResponse = {
|
||||||
|
description: `Response model for model status list.`,
|
||||||
|
properties: {
|
||||||
|
models: {
|
||||||
|
type: 'array',
|
||||||
|
contains: {
|
||||||
|
type: 'ModelStatus',
|
||||||
|
},
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $ProfileSampleResponse = {
|
||||||
|
description: `Response model for profile sample.`,
|
||||||
|
properties: {
|
||||||
|
id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
profile_id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
audio_path: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
reference_text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $TranscriptionResponse = {
|
||||||
|
description: `Response model for transcription.`,
|
||||||
|
properties: {
|
||||||
|
text: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
duration: {
|
||||||
|
type: 'number',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [{ type: 'string' }, { type: 'null' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $ValidationError = {
|
||||||
|
properties: {
|
||||||
|
loc: {
|
||||||
|
type: 'array',
|
||||||
|
contains: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
msg: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
type: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $VoiceProfileCreate = {
|
||||||
|
description: `Request model for creating a voice profile.`,
|
||||||
|
properties: {
|
||||||
|
name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
maxLength: 100,
|
||||||
|
minLength: 1,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
maxLength: 500,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'string',
|
||||||
|
pattern: '^(en|zh)$',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
export const $VoiceProfileResponse = {
|
||||||
|
description: `Response model for voice profile.`,
|
||||||
|
properties: {
|
||||||
|
id: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: 'any-of',
|
||||||
|
contains: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'null',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
language: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'date-time',
|
||||||
|
},
|
||||||
|
updated_at: {
|
||||||
|
type: 'string',
|
||||||
|
isRequired: true,
|
||||||
|
format: 'date-time',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
@@ -0,0 +1,459 @@
|
|||||||
|
/* generated using openapi-typescript-codegen -- do not edit */
|
||||||
|
/* istanbul ignore file */
|
||||||
|
/* tslint:disable */
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { Body_add_profile_sample_profiles__profile_id__samples_post } from '../models/Body_add_profile_sample_profiles__profile_id__samples_post';
|
||||||
|
import type { Body_transcribe_audio_transcribe_post } from '../models/Body_transcribe_audio_transcribe_post';
|
||||||
|
import type { GenerationRequest } from '../models/GenerationRequest';
|
||||||
|
import type { GenerationResponse } from '../models/GenerationResponse';
|
||||||
|
import type { HealthResponse } from '../models/HealthResponse';
|
||||||
|
import type { HistoryListResponse } from '../models/HistoryListResponse';
|
||||||
|
import type { HistoryResponse } from '../models/HistoryResponse';
|
||||||
|
import type { ModelDownloadRequest } from '../models/ModelDownloadRequest';
|
||||||
|
import type { ModelStatusListResponse } from '../models/ModelStatusListResponse';
|
||||||
|
import type { ProfileSampleResponse } from '../models/ProfileSampleResponse';
|
||||||
|
import type { TranscriptionResponse } from '../models/TranscriptionResponse';
|
||||||
|
import type { VoiceProfileCreate } from '../models/VoiceProfileCreate';
|
||||||
|
import type { VoiceProfileResponse } from '../models/VoiceProfileResponse';
|
||||||
|
import type { CancelablePromise } from '../core/CancelablePromise';
|
||||||
|
import { OpenAPI } from '../core/OpenAPI';
|
||||||
|
import { request as __request } from '../core/request';
|
||||||
|
export class DefaultService {
|
||||||
|
/**
|
||||||
|
* Root
|
||||||
|
* Root endpoint.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static rootGet(): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Health
|
||||||
|
* Health check endpoint.
|
||||||
|
* @returns HealthResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static healthHealthGet(): CancelablePromise<HealthResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/health',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* List Profiles
|
||||||
|
* List all voice profiles.
|
||||||
|
* @returns VoiceProfileResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static listProfilesProfilesGet(): CancelablePromise<Array<VoiceProfileResponse>> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/profiles',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Create Profile
|
||||||
|
* Create a new voice profile.
|
||||||
|
* @returns VoiceProfileResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static createProfileProfilesPost({
|
||||||
|
requestBody,
|
||||||
|
}: {
|
||||||
|
requestBody: VoiceProfileCreate;
|
||||||
|
}): CancelablePromise<VoiceProfileResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/profiles',
|
||||||
|
body: requestBody,
|
||||||
|
mediaType: 'application/json',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Profile
|
||||||
|
* Get a voice profile by ID.
|
||||||
|
* @returns VoiceProfileResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getProfileProfilesProfileIdGet({
|
||||||
|
profileId,
|
||||||
|
}: {
|
||||||
|
profileId: string;
|
||||||
|
}): CancelablePromise<VoiceProfileResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/profiles/{profile_id}',
|
||||||
|
path: {
|
||||||
|
profile_id: profileId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Update Profile
|
||||||
|
* Update a voice profile.
|
||||||
|
* @returns VoiceProfileResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static updateProfileProfilesProfileIdPut({
|
||||||
|
profileId,
|
||||||
|
requestBody,
|
||||||
|
}: {
|
||||||
|
profileId: string;
|
||||||
|
requestBody: VoiceProfileCreate;
|
||||||
|
}): CancelablePromise<VoiceProfileResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'PUT',
|
||||||
|
url: '/profiles/{profile_id}',
|
||||||
|
path: {
|
||||||
|
profile_id: profileId,
|
||||||
|
},
|
||||||
|
body: requestBody,
|
||||||
|
mediaType: 'application/json',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Delete Profile
|
||||||
|
* Delete a voice profile.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static deleteProfileProfilesProfileIdDelete({
|
||||||
|
profileId,
|
||||||
|
}: {
|
||||||
|
profileId: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'DELETE',
|
||||||
|
url: '/profiles/{profile_id}',
|
||||||
|
path: {
|
||||||
|
profile_id: profileId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Add Profile Sample
|
||||||
|
* Add a sample to a voice profile.
|
||||||
|
* @returns ProfileSampleResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static addProfileSampleProfilesProfileIdSamplesPost({
|
||||||
|
profileId,
|
||||||
|
formData,
|
||||||
|
}: {
|
||||||
|
profileId: string;
|
||||||
|
formData: Body_add_profile_sample_profiles__profile_id__samples_post;
|
||||||
|
}): CancelablePromise<ProfileSampleResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/profiles/{profile_id}/samples',
|
||||||
|
path: {
|
||||||
|
profile_id: profileId,
|
||||||
|
},
|
||||||
|
formData: formData,
|
||||||
|
mediaType: 'multipart/form-data',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Profile Samples
|
||||||
|
* Get all samples for a profile.
|
||||||
|
* @returns ProfileSampleResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getProfileSamplesProfilesProfileIdSamplesGet({
|
||||||
|
profileId,
|
||||||
|
}: {
|
||||||
|
profileId: string;
|
||||||
|
}): CancelablePromise<Array<ProfileSampleResponse>> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/profiles/{profile_id}/samples',
|
||||||
|
path: {
|
||||||
|
profile_id: profileId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Delete Profile Sample
|
||||||
|
* Delete a profile sample.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static deleteProfileSampleProfilesSamplesSampleIdDelete({
|
||||||
|
sampleId,
|
||||||
|
}: {
|
||||||
|
sampleId: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'DELETE',
|
||||||
|
url: '/profiles/samples/{sample_id}',
|
||||||
|
path: {
|
||||||
|
sample_id: sampleId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Generate Speech
|
||||||
|
* Generate speech from text using a voice profile.
|
||||||
|
* @returns GenerationResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static generateSpeechGeneratePost({
|
||||||
|
requestBody,
|
||||||
|
}: {
|
||||||
|
requestBody: GenerationRequest;
|
||||||
|
}): CancelablePromise<GenerationResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/generate',
|
||||||
|
body: requestBody,
|
||||||
|
mediaType: 'application/json',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* List History
|
||||||
|
* List generation history with optional filters.
|
||||||
|
* @returns HistoryListResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static listHistoryHistoryGet({
|
||||||
|
profileId,
|
||||||
|
search,
|
||||||
|
limit = 50,
|
||||||
|
offset,
|
||||||
|
}: {
|
||||||
|
profileId?: string | null;
|
||||||
|
search?: string | null;
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
}): CancelablePromise<HistoryListResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/history',
|
||||||
|
query: {
|
||||||
|
profile_id: profileId,
|
||||||
|
search: search,
|
||||||
|
limit: limit,
|
||||||
|
offset: offset,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Generation
|
||||||
|
* Get a generation by ID.
|
||||||
|
* @returns HistoryResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getGenerationHistoryGenerationIdGet({
|
||||||
|
generationId,
|
||||||
|
}: {
|
||||||
|
generationId: string;
|
||||||
|
}): CancelablePromise<HistoryResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/history/{generation_id}',
|
||||||
|
path: {
|
||||||
|
generation_id: generationId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Delete Generation
|
||||||
|
* Delete a generation.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static deleteGenerationHistoryGenerationIdDelete({
|
||||||
|
generationId,
|
||||||
|
}: {
|
||||||
|
generationId: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'DELETE',
|
||||||
|
url: '/history/{generation_id}',
|
||||||
|
path: {
|
||||||
|
generation_id: generationId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Stats
|
||||||
|
* Get generation statistics.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getStatsHistoryStatsGet(): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/history/stats',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Transcribe Audio
|
||||||
|
* Transcribe audio file to text.
|
||||||
|
* @returns TranscriptionResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static transcribeAudioTranscribePost({
|
||||||
|
formData,
|
||||||
|
}: {
|
||||||
|
formData: Body_transcribe_audio_transcribe_post;
|
||||||
|
}): CancelablePromise<TranscriptionResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/transcribe',
|
||||||
|
formData: formData,
|
||||||
|
mediaType: 'multipart/form-data',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Audio
|
||||||
|
* Serve generated audio file.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getAudioAudioGenerationIdGet({
|
||||||
|
generationId,
|
||||||
|
}: {
|
||||||
|
generationId: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/audio/{generation_id}',
|
||||||
|
path: {
|
||||||
|
generation_id: generationId,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Load Model
|
||||||
|
* Manually load TTS model.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static loadModelModelsLoadPost({
|
||||||
|
modelSize = '1.7B',
|
||||||
|
}: {
|
||||||
|
modelSize?: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/models/load',
|
||||||
|
query: {
|
||||||
|
model_size: modelSize,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Unload Model
|
||||||
|
* Unload TTS model to free memory.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static unloadModelModelsUnloadPost(): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/models/unload',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Model Progress
|
||||||
|
* Get model download progress via Server-Sent Events.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getModelProgressModelsProgressModelNameGet({
|
||||||
|
modelName,
|
||||||
|
}: {
|
||||||
|
modelName: string;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/models/progress/{model_name}',
|
||||||
|
path: {
|
||||||
|
model_name: modelName,
|
||||||
|
},
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get Model Status
|
||||||
|
* Get status of all available models.
|
||||||
|
* @returns ModelStatusListResponse Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static getModelStatusModelsStatusGet(): CancelablePromise<ModelStatusListResponse> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/models/status',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Trigger Model Download
|
||||||
|
* Trigger download of a specific model.
|
||||||
|
* @returns any Successful Response
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public static triggerModelDownloadModelsDownloadPost({
|
||||||
|
requestBody,
|
||||||
|
}: {
|
||||||
|
requestBody: ModelDownloadRequest;
|
||||||
|
}): CancelablePromise<any> {
|
||||||
|
return __request(OpenAPI, {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/models/download',
|
||||||
|
body: requestBody,
|
||||||
|
mediaType: 'application/json',
|
||||||
|
errors: {
|
||||||
|
422: `Validation Error`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -213,10 +213,6 @@ export interface CaptureSettings {
|
|||||||
/** Whether the global keyboard hotkey is armed. Off by default — turning
|
/** Whether the global keyboard hotkey is armed. Off by default — turning
|
||||||
* this on triggers the macOS Input Monitoring TCC prompt. */
|
* this on triggers the macOS Input Monitoring TCC prompt. */
|
||||||
hotkey_enabled: boolean;
|
hotkey_enabled: boolean;
|
||||||
/** Hold the mic open while dictation is enabled so push-to-talk doesn't clip
|
|
||||||
* the first words. Off by default — when on, the OS mic indicator stays lit
|
|
||||||
* the whole time dictation is enabled. */
|
|
||||||
keep_mic_warm: boolean;
|
|
||||||
/** keytap key names. Defaults are platform-specific right-hand modifiers. */
|
/** keytap key names. Defaults are platform-specific right-hand modifiers. */
|
||||||
chord_push_to_talk_keys: string[];
|
chord_push_to_talk_keys: string[];
|
||||||
/** keytap key names. Toggle adds Space to the platform-specific PTT chord. */
|
/** keytap key names. Toggle adds Space to the platform-specific PTT chord. */
|
||||||
@@ -262,6 +258,7 @@ export interface TranscriptionRequest {
|
|||||||
export interface TranscriptionResponse {
|
export interface TranscriptionResponse {
|
||||||
text: string;
|
text: string;
|
||||||
duration: number;
|
duration: number;
|
||||||
|
language?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HealthResponse {
|
export interface HealthResponse {
|
||||||
|
|||||||
@@ -4,45 +4,12 @@ import { convertToWav } from '@/lib/utils/audio';
|
|||||||
|
|
||||||
interface UseAudioRecordingOptions {
|
interface UseAudioRecordingOptions {
|
||||||
maxDurationSeconds?: number;
|
maxDurationSeconds?: number;
|
||||||
// ``context`` is whatever was handed to ``startRecording`` for this take,
|
onRecordingComplete?: (blob: Blob, duration?: number) => void;
|
||||||
// threaded back untouched so callers can correlate the result with the
|
|
||||||
// recording it came from (the dictate window pairs it with the focus
|
|
||||||
// snapshot captured at chord-start).
|
|
||||||
onRecordingComplete?: (blob: Blob, duration?: number, context?: unknown) => void;
|
|
||||||
/**
|
|
||||||
* Keep the microphone ``MediaStream`` open between recordings instead of
|
|
||||||
* tearing it down on every stop. This is what removes the "first words get
|
|
||||||
* clipped" problem on push-to-talk dictation: ``getUserMedia`` on macOS can
|
|
||||||
* take several hundred ms — up to a second cold — to hand back a stream, and
|
|
||||||
* ``MediaRecorder`` only starts capturing *after* it resolves, so everything
|
|
||||||
* spoken in that window is lost. With a warm stream already open, the next
|
|
||||||
* ``startRecording`` skips ``getUserMedia`` entirely.
|
|
||||||
*
|
|
||||||
* Off by default: the voice-clone sample recorders release the device
|
|
||||||
* immediately, and the dictation session only opts in when the user enables
|
|
||||||
* the "keep microphone ready" setting. While on, the warm stream stays open —
|
|
||||||
* and the OS mic-in-use indicator stays lit — until it's explicitly released
|
|
||||||
* (dictation disabled or the setting turned off), so the trade-off is visible
|
|
||||||
* and user-controlled rather than a background mic that's always warm.
|
|
||||||
*/
|
|
||||||
keepWarm?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Audio constraints for capture. Kept identical to the previous inline value so
|
|
||||||
// this change is purely about *when* the stream is opened, not *how*.
|
|
||||||
const AUDIO_CONSTRAINTS: MediaTrackConstraints = {
|
|
||||||
echoCancellation: true,
|
|
||||||
noiseSuppression: true,
|
|
||||||
autoGainControl: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const streamHasLiveAudio = (stream: MediaStream | null): stream is MediaStream =>
|
|
||||||
!!stream && stream.getAudioTracks().some((t) => t.readyState === 'live');
|
|
||||||
|
|
||||||
export function useAudioRecording({
|
export function useAudioRecording({
|
||||||
maxDurationSeconds,
|
maxDurationSeconds,
|
||||||
onRecordingComplete,
|
onRecordingComplete,
|
||||||
keepWarm = false,
|
|
||||||
}: UseAudioRecordingOptions = {}) {
|
}: UseAudioRecordingOptions = {}) {
|
||||||
const platform = usePlatform();
|
const platform = usePlatform();
|
||||||
const [isRecording, setIsRecording] = useState(false);
|
const [isRecording, setIsRecording] = useState(false);
|
||||||
@@ -50,392 +17,195 @@ export function useAudioRecording({
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
||||||
const chunksRef = useRef<Blob[]>([]);
|
const chunksRef = useRef<Blob[]>([]);
|
||||||
// The stream currently backing the MediaRecorder. When ``keepWarm`` is set
|
|
||||||
// this is the same object as ``warmStreamRef`` and is *not* torn down on
|
|
||||||
// stop; otherwise it's stopped as soon as the recording completes.
|
|
||||||
const streamRef = useRef<MediaStream | null>(null);
|
const streamRef = useRef<MediaStream | null>(null);
|
||||||
// Persistent pre-opened stream reused across recordings when ``keepWarm``.
|
|
||||||
const warmStreamRef = useRef<MediaStream | null>(null);
|
|
||||||
const timerRef = useRef<number | null>(null);
|
const timerRef = useRef<number | null>(null);
|
||||||
const startTimeRef = useRef<number | null>(null);
|
const startTimeRef = useRef<number | null>(null);
|
||||||
const cancelledRef = useRef<boolean>(false);
|
const cancelledRef = useRef<boolean>(false);
|
||||||
// Mirror of ``isRecording`` for reads inside callbacks that would otherwise
|
|
||||||
// close over a stale render.
|
|
||||||
const isRecordingRef = useRef(false);
|
|
||||||
// A ``getUserMedia`` call in flight, shared so concurrent acquirers (prewarm
|
|
||||||
// plus an immediate chord) coalesce onto one stream instead of each opening —
|
|
||||||
// and orphaning — their own.
|
|
||||||
const acquiringRef = useRef<Promise<MediaStream> | null>(null);
|
|
||||||
// True from ``startRecording`` entry until the recorder is actually running
|
|
||||||
// (or has failed), so a stop that arrives mid-acquisition can be deferred.
|
|
||||||
const startingRef = useRef(false);
|
|
||||||
// True from MediaRecorder.stop() until onstop has snapshotted the take's
|
|
||||||
// shared refs. React state and MediaRecorder.state both flip before onstop,
|
|
||||||
// so without this gate a rapid next chord can clear chunks/duration/cancel
|
|
||||||
// state out from under the recorder that is still finalising.
|
|
||||||
const finishingRef = useRef(false);
|
|
||||||
const pendingStopRef = useRef(false);
|
|
||||||
// Bumped per recording so a stale recorder's ``onstop`` can tell it's no
|
|
||||||
// longer the active one before it touches the shared stream refs.
|
|
||||||
const recordingCounterRef = useRef(0);
|
|
||||||
// Bumped whenever the warm stream is released/aborted so a ``getUserMedia``
|
|
||||||
// still in flight can tell its result is stale and stop it instead of
|
|
||||||
// adopting a live mic after disable/unmount.
|
|
||||||
const acquireGenRef = useRef(0);
|
|
||||||
// Set when a release is requested mid-recording; the onstop path performs the
|
|
||||||
// deferred release once capture finishes rather than yanking the device now.
|
|
||||||
const releaseAfterStopRef = useRef(false);
|
|
||||||
|
|
||||||
// Keeps the ref in lockstep with the state so the synchronous stop path reads
|
const startRecording = useCallback(async () => {
|
||||||
// a fresh value without waiting for a rerender.
|
try {
|
||||||
const setRecording = useCallback((next: boolean) => {
|
setError(null);
|
||||||
isRecordingRef.current = next;
|
chunksRef.current = [];
|
||||||
setIsRecording(next);
|
cancelledRef.current = false;
|
||||||
}, []);
|
setDuration(0);
|
||||||
|
|
||||||
const releaseWarmStream = useCallback(() => {
|
// Check if getUserMedia is available
|
||||||
// Invalidate any getUserMedia still in flight so its stream is stopped on
|
// In Tauri, navigator.mediaDevices might not be available immediately
|
||||||
// resolve rather than adopted as the warm stream.
|
if (typeof navigator === 'undefined') {
|
||||||
acquireGenRef.current += 1;
|
const errorMsg =
|
||||||
// Don't tear the device out from under an active/starting recording — the
|
'Navigator API is not available. This might be a Tauri configuration issue.';
|
||||||
// warm stream is the one backing it; defer to the onstop path instead.
|
setError(errorMsg);
|
||||||
if (isRecordingRef.current || startingRef.current) {
|
throw new Error(errorMsg);
|
||||||
releaseAfterStopRef.current = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
warmStreamRef.current?.getTracks().forEach((track) => {
|
|
||||||
track.stop();
|
|
||||||
});
|
|
||||||
warmStreamRef.current = null;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Assert that getUserMedia is reachable, mirroring the previous inline guard
|
|
||||||
// (Tauri webviews occasionally expose ``navigator.mediaDevices`` a beat late).
|
|
||||||
const assertMediaDevices = useCallback(async () => {
|
|
||||||
if (typeof navigator === 'undefined') {
|
|
||||||
throw new Error('Navigator API is not available. This might be a Tauri configuration issue.');
|
|
||||||
}
|
|
||||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
||||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
|
||||||
throw new Error(
|
|
||||||
platform.metadata.isTauri
|
|
||||||
? 'Microphone access is not available. Please ensure:\n1. The app has microphone permissions in System Settings (macOS: System Settings > Privacy & Security > Microphone)\n2. You restart the app after granting permissions\n3. You are using Tauri v2 with a webview that supports getUserMedia'
|
|
||||||
: 'Microphone access is not available. Please ensure you are using a secure context (HTTPS or localhost) and that your browser has microphone permissions enabled.',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}, [platform.metadata.isTauri]);
|
|
||||||
|
|
||||||
// Return a live capture stream, reusing the warm one when available so the
|
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
// hot path (chord-down → record) never waits on getUserMedia.
|
// Try waiting a bit for Tauri webview to initialize
|
||||||
const acquireStream = useCallback(async (): Promise<MediaStream> => {
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||||
// Captured separately so it stays typed as the full stream after the live
|
|
||||||
// check narrows ``warmStreamRef.current`` itself.
|
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||||
const existing = warmStreamRef.current;
|
console.error('MediaDevices check:', {
|
||||||
if (streamHasLiveAudio(warmStreamRef.current)) {
|
hasNavigator: typeof navigator !== 'undefined',
|
||||||
return warmStreamRef.current;
|
hasMediaDevices: !!navigator?.mediaDevices,
|
||||||
}
|
hasGetUserMedia: !!navigator?.mediaDevices?.getUserMedia,
|
||||||
// Coalesce concurrent acquirers onto one getUserMedia call so prewarm and
|
isTauri: platform.metadata.isTauri,
|
||||||
// an immediate chord can't open two streams.
|
});
|
||||||
if (acquiringRef.current) return acquiringRef.current;
|
|
||||||
// A dead warm stream (device unplugged / tracks ended) — drop it and reopen.
|
const errorMsg = platform.metadata.isTauri
|
||||||
if (existing) {
|
? 'Microphone access is not available. Please ensure:\n1. The app has microphone permissions in System Settings (macOS: System Settings > Privacy & Security > Microphone)\n2. You restart the app after granting permissions\n3. You are using Tauri v2 with a webview that supports getUserMedia'
|
||||||
existing.getTracks().forEach((track) => {
|
: 'Microphone access is not available. Please ensure you are using a secure context (HTTPS or localhost) and that your browser has microphone permissions enabled.';
|
||||||
track.stop();
|
setError(errorMsg);
|
||||||
});
|
throw new Error(errorMsg);
|
||||||
warmStreamRef.current = null;
|
}
|
||||||
}
|
}
|
||||||
const gen = acquireGenRef.current;
|
|
||||||
const acquisition = (async () => {
|
// Request microphone access
|
||||||
await assertMediaDevices();
|
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({
|
const stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: AUDIO_CONSTRAINTS,
|
audio: {
|
||||||
|
echoCancellation: true,
|
||||||
|
noiseSuppression: true,
|
||||||
|
autoGainControl: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
// Released / disabled / unmounted while acquiring — this stream is stale,
|
|
||||||
// so stop it instead of leaving a live mic open, and abort the caller.
|
streamRef.current = stream;
|
||||||
if (gen !== acquireGenRef.current) {
|
|
||||||
stream.getTracks().forEach((track) => {
|
// Create MediaRecorder with preferred MIME type
|
||||||
|
const options: MediaRecorderOptions = {
|
||||||
|
mimeType: 'audio/webm;codecs=opus',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fallback to default if webm not supported
|
||||||
|
if (!MediaRecorder.isTypeSupported(options.mimeType!)) {
|
||||||
|
delete options.mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mediaRecorder = new MediaRecorder(stream, options);
|
||||||
|
mediaRecorderRef.current = mediaRecorder;
|
||||||
|
|
||||||
|
mediaRecorder.ondataavailable = (event) => {
|
||||||
|
if (event.data.size > 0) {
|
||||||
|
chunksRef.current.push(event.data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
mediaRecorder.onstop = async () => {
|
||||||
|
// Snapshot the cancellation flag and recorded duration immediately —
|
||||||
|
// cancelRecording() clears chunks and sets cancelledRef synchronously
|
||||||
|
// before this async handler runs, so we must check it first.
|
||||||
|
const wasCancelled = cancelledRef.current;
|
||||||
|
const recordedDuration = startTimeRef.current
|
||||||
|
? (Date.now() - startTimeRef.current) / 1000
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const webmBlob = new Blob(chunksRef.current, { type: 'audio/webm' });
|
||||||
|
|
||||||
|
// Stop all tracks now that we have the data
|
||||||
|
streamRef.current?.getTracks().forEach((track) => {
|
||||||
track.stop();
|
track.stop();
|
||||||
});
|
});
|
||||||
throw new Error('microphone acquisition aborted');
|
streamRef.current = null;
|
||||||
}
|
|
||||||
if (keepWarm) warmStreamRef.current = stream;
|
|
||||||
return stream;
|
|
||||||
})();
|
|
||||||
acquiringRef.current = acquisition;
|
|
||||||
try {
|
|
||||||
return await acquisition;
|
|
||||||
} finally {
|
|
||||||
if (acquiringRef.current === acquisition) acquiringRef.current = null;
|
|
||||||
}
|
|
||||||
}, [assertMediaDevices, keepWarm]);
|
|
||||||
|
|
||||||
/**
|
// Don't fire completion callback if the recording was cancelled
|
||||||
* Open the microphone ahead of the first recording so the initial dictation
|
if (wasCancelled) return;
|
||||||
* doesn't clip. No-op unless ``keepWarm`` is set. Safe to call repeatedly and
|
|
||||||
* safe to fail (e.g. permission not yet granted) — ``startRecording`` still
|
|
||||||
* surfaces a real error if capture is genuinely unavailable.
|
|
||||||
*/
|
|
||||||
const prewarm = useCallback(async () => {
|
|
||||||
if (!keepWarm) return;
|
|
||||||
try {
|
|
||||||
await acquireStream();
|
|
||||||
} catch {
|
|
||||||
// Permission missing / device busy / aborted — recording will report a
|
|
||||||
// real error if capture is genuinely unavailable.
|
|
||||||
}
|
|
||||||
}, [keepWarm, acquireStream]);
|
|
||||||
|
|
||||||
const startRecording = useCallback(
|
// Convert to WAV format to avoid needing ffmpeg on backend
|
||||||
async (context?: unknown) => {
|
try {
|
||||||
// A second chord can arrive while the first one is still waiting on
|
const wavBlob = await convertToWav(webmBlob);
|
||||||
// getUserMedia. Never create overlapping MediaRecorders on the same
|
onRecordingComplete?.(wavBlob, recordedDuration);
|
||||||
// coalesced stream; the original take will honor any deferred stop.
|
} catch (err) {
|
||||||
if (
|
console.error('Error converting audio to WAV:', err);
|
||||||
startingRef.current ||
|
// Fallback to original blob if conversion fails
|
||||||
finishingRef.current ||
|
onRecordingComplete?.(webmBlob, recordedDuration);
|
||||||
mediaRecorderRef.current?.state === 'recording'
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
startingRef.current = true;
|
|
||||||
pendingStopRef.current = false;
|
|
||||||
// A new recording supersedes any release deferred from a prior take.
|
|
||||||
releaseAfterStopRef.current = false;
|
|
||||||
const recordingId = ++recordingCounterRef.current;
|
|
||||||
try {
|
|
||||||
setError(null);
|
|
||||||
chunksRef.current = [];
|
|
||||||
cancelledRef.current = false;
|
|
||||||
setDuration(0);
|
|
||||||
|
|
||||||
// Reuse the warm stream when present (instant); otherwise open one now.
|
|
||||||
const stream = await acquireStream();
|
|
||||||
streamRef.current = stream;
|
|
||||||
|
|
||||||
// Create MediaRecorder with preferred MIME type
|
|
||||||
const options: MediaRecorderOptions = {
|
|
||||||
mimeType: 'audio/webm;codecs=opus',
|
|
||||||
};
|
|
||||||
|
|
||||||
// Fallback to default if webm not supported
|
|
||||||
if (!MediaRecorder.isTypeSupported(options.mimeType!)) {
|
|
||||||
delete options.mimeType;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const mediaRecorder = new MediaRecorder(stream, options);
|
mediaRecorder.onerror = (event) => {
|
||||||
mediaRecorderRef.current = mediaRecorder;
|
setError('Recording error occurred');
|
||||||
|
console.error('MediaRecorder error:', event);
|
||||||
|
};
|
||||||
|
|
||||||
mediaRecorder.ondataavailable = (event) => {
|
// WebKit's MediaRecorder drops the WebM EBML header from chunks when
|
||||||
if (event.data.size > 0) {
|
// started with a timeslice, so concatenated blobs fail to parse in
|
||||||
chunksRef.current.push(event.data);
|
// both AudioContext and ffmpeg. Starting with no timeslice produces
|
||||||
}
|
// exactly one dataavailable on stop() with a valid container.
|
||||||
};
|
mediaRecorder.start();
|
||||||
|
setIsRecording(true);
|
||||||
|
startTimeRef.current = Date.now();
|
||||||
|
|
||||||
mediaRecorder.onstop = async () => {
|
// Start timer
|
||||||
// Whether this recorder is still the active one. A stale onstop (an
|
timerRef.current = window.setInterval(() => {
|
||||||
// older recorder stopping after a newer startRecording) must not touch
|
if (startTimeRef.current) {
|
||||||
// the shared stream refs.
|
const elapsed = (Date.now() - startTimeRef.current) / 1000;
|
||||||
const isCurrent = recordingCounterRef.current === recordingId;
|
setDuration(elapsed);
|
||||||
// Snapshot the cancellation flag and recorded duration immediately —
|
|
||||||
// cancelRecording() clears chunks and sets cancelledRef synchronously
|
|
||||||
// before this async handler runs, so we must check it first.
|
|
||||||
const wasCancelled = cancelledRef.current;
|
|
||||||
const recordedDuration = startTimeRef.current
|
|
||||||
? (Date.now() - startTimeRef.current) / 1000
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const webmBlob = new Blob(chunksRef.current, { type: 'audio/webm' });
|
// Auto-stop at max duration when the caller opts in — dictation
|
||||||
|
// sessions pass undefined and run until the user releases the
|
||||||
// Release the device unless we're keeping it warm for the next capture.
|
// chord or hits stop; voice-clone sample recorders pass 29s to
|
||||||
// Act on this recorder's own stream; only touch the shared refs when
|
// keep reference clips short.
|
||||||
// this is still the current recording.
|
if (maxDurationSeconds !== undefined && elapsed >= maxDurationSeconds) {
|
||||||
if (keepWarm) {
|
if (mediaRecorderRef.current && mediaRecorderRef.current.state !== 'inactive') {
|
||||||
if (isCurrent) {
|
mediaRecorderRef.current.stop();
|
||||||
streamRef.current = null;
|
setIsRecording(false);
|
||||||
// A release requested mid-recording (dictation disabled) is
|
if (timerRef.current !== null) {
|
||||||
// honored now that capture has finished; otherwise the warm
|
clearInterval(timerRef.current);
|
||||||
// stream stays open for the next take.
|
timerRef.current = null;
|
||||||
if (releaseAfterStopRef.current) {
|
|
||||||
releaseAfterStopRef.current = false;
|
|
||||||
releaseWarmStream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
stream.getTracks().forEach((track) => {
|
|
||||||
track.stop();
|
|
||||||
});
|
|
||||||
if (isCurrent) streamRef.current = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// All shared per-take refs have now been snapshotted and stream
|
|
||||||
// cleanup is complete. A new take may begin while WAV conversion and
|
|
||||||
// upload continue using the local values above.
|
|
||||||
finishingRef.current = false;
|
|
||||||
|
|
||||||
// Don't fire completion callback if the recording was cancelled
|
|
||||||
if (wasCancelled) return;
|
|
||||||
|
|
||||||
// Convert to WAV format to avoid needing ffmpeg on backend
|
|
||||||
try {
|
|
||||||
const wavBlob = await convertToWav(webmBlob);
|
|
||||||
onRecordingComplete?.(wavBlob, recordedDuration, context);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error converting audio to WAV:', err);
|
|
||||||
// Fallback to original blob if conversion fails
|
|
||||||
onRecordingComplete?.(webmBlob, recordedDuration, context);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
mediaRecorder.onerror = (event) => {
|
|
||||||
setError('Recording error occurred');
|
|
||||||
console.error('MediaRecorder error:', event);
|
|
||||||
};
|
|
||||||
|
|
||||||
// WebKit's MediaRecorder drops the WebM EBML header from chunks when
|
|
||||||
// started with a timeslice, so concatenated blobs fail to parse in
|
|
||||||
// both AudioContext and ffmpeg. Starting with no timeslice produces
|
|
||||||
// exactly one dataavailable on stop() with a valid container.
|
|
||||||
mediaRecorder.start();
|
|
||||||
setRecording(true);
|
|
||||||
startTimeRef.current = Date.now();
|
|
||||||
startingRef.current = false;
|
|
||||||
|
|
||||||
// A stop (chord release) that landed while the mic was still opening —
|
|
||||||
// honor it now that capture has actually begun.
|
|
||||||
if (pendingStopRef.current) {
|
|
||||||
pendingStopRef.current = false;
|
|
||||||
finishingRef.current = true;
|
|
||||||
mediaRecorder.stop();
|
|
||||||
setRecording(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start timer
|
|
||||||
timerRef.current = window.setInterval(() => {
|
|
||||||
if (startTimeRef.current) {
|
|
||||||
const elapsed = (Date.now() - startTimeRef.current) / 1000;
|
|
||||||
setDuration(elapsed);
|
|
||||||
|
|
||||||
// Auto-stop at max duration when the caller opts in — dictation
|
|
||||||
// sessions pass undefined and run until the user releases the
|
|
||||||
// chord or hits stop; voice-clone sample recorders pass 29s to
|
|
||||||
// keep reference clips short.
|
|
||||||
if (maxDurationSeconds !== undefined && elapsed >= maxDurationSeconds) {
|
|
||||||
if (mediaRecorderRef.current && mediaRecorderRef.current.state !== 'inactive') {
|
|
||||||
finishingRef.current = true;
|
|
||||||
mediaRecorderRef.current.stop();
|
|
||||||
setRecording(false);
|
|
||||||
if (timerRef.current !== null) {
|
|
||||||
clearInterval(timerRef.current);
|
|
||||||
timerRef.current = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 100);
|
|
||||||
} catch (err) {
|
|
||||||
const errorMessage =
|
|
||||||
err instanceof Error
|
|
||||||
? err.message
|
|
||||||
: 'Failed to access microphone. Please check permissions.';
|
|
||||||
// A fresh (non-warm) stream opened before the failure must be released
|
|
||||||
// so the mic doesn't stay lit; a warm stream is reusable, so it's kept.
|
|
||||||
if (!keepWarm) {
|
|
||||||
streamRef.current?.getTracks().forEach((track) => {
|
|
||||||
track.stop();
|
|
||||||
});
|
|
||||||
streamRef.current = null;
|
|
||||||
}
|
}
|
||||||
startingRef.current = false;
|
}, 100);
|
||||||
finishingRef.current = false;
|
} catch (err) {
|
||||||
pendingStopRef.current = false;
|
const errorMessage =
|
||||||
setError(errorMessage);
|
err instanceof Error
|
||||||
setRecording(false);
|
? err.message
|
||||||
}
|
: 'Failed to access microphone. Please check permissions.';
|
||||||
},
|
setError(errorMessage);
|
||||||
[
|
setIsRecording(false);
|
||||||
maxDurationSeconds,
|
}
|
||||||
onRecordingComplete,
|
}, [maxDurationSeconds, onRecordingComplete]);
|
||||||
acquireStream,
|
|
||||||
keepWarm,
|
|
||||||
releaseWarmStream,
|
|
||||||
setRecording,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const stopRecording = useCallback(() => {
|
const stopRecording = useCallback(() => {
|
||||||
// The recorder's own state is the lifecycle authority — React ``isRecording``
|
if (mediaRecorderRef.current && isRecording) {
|
||||||
// lags a render behind ``mediaRecorder.start()``, so a chord release in that
|
mediaRecorderRef.current.stop();
|
||||||
// window would otherwise be dropped.
|
setIsRecording(false);
|
||||||
const recorder = mediaRecorderRef.current;
|
|
||||||
if (recorder && recorder.state === 'recording') {
|
|
||||||
finishingRef.current = true;
|
|
||||||
recorder.stop();
|
|
||||||
setRecording(false);
|
|
||||||
|
|
||||||
if (timerRef.current !== null) {
|
if (timerRef.current !== null) {
|
||||||
clearInterval(timerRef.current);
|
clearInterval(timerRef.current);
|
||||||
timerRef.current = null;
|
timerRef.current = null;
|
||||||
}
|
}
|
||||||
} else if (startingRef.current) {
|
|
||||||
// Stop arrived before capture began (mic still opening) — defer it so
|
|
||||||
// startRecording stops as soon as the recorder goes live.
|
|
||||||
pendingStopRef.current = true;
|
|
||||||
}
|
}
|
||||||
}, [setRecording]);
|
}, [isRecording]);
|
||||||
|
|
||||||
const cancelRecording = useCallback(() => {
|
const cancelRecording = useCallback(() => {
|
||||||
cancelledRef.current = true; // Must be set before stop() triggers onstop
|
if (mediaRecorderRef.current) {
|
||||||
const recorder = mediaRecorderRef.current;
|
cancelledRef.current = true; // Must be set before stop() triggers onstop
|
||||||
if (recorder && recorder.state !== 'inactive') {
|
|
||||||
chunksRef.current = [];
|
chunksRef.current = [];
|
||||||
finishingRef.current = true;
|
mediaRecorderRef.current.stop();
|
||||||
recorder.stop();
|
setIsRecording(false);
|
||||||
setRecording(false);
|
|
||||||
setDuration(0);
|
setDuration(0);
|
||||||
} else if (startingRef.current) {
|
|
||||||
// Cancel during mic acquisition — stop as soon as capture begins; the
|
|
||||||
// cancelled flag suppresses the completion callback.
|
|
||||||
pendingStopRef.current = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the device warm for the next capture when opted in; otherwise stop
|
// Stop all tracks
|
||||||
// the tracks so the mic is released immediately.
|
streamRef.current?.getTracks().forEach((track) => {
|
||||||
if (keepWarm) {
|
track.stop();
|
||||||
streamRef.current = null;
|
});
|
||||||
if (releaseAfterStopRef.current) {
|
streamRef.current = null;
|
||||||
releaseAfterStopRef.current = false;
|
|
||||||
releaseWarmStream();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
streamRef.current?.getTracks().forEach((track) => {
|
|
||||||
track.stop();
|
|
||||||
});
|
|
||||||
streamRef.current = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timerRef.current !== null) {
|
if (timerRef.current !== null) {
|
||||||
clearInterval(timerRef.current);
|
clearInterval(timerRef.current);
|
||||||
timerRef.current = null;
|
timerRef.current = null;
|
||||||
}
|
}
|
||||||
}, [keepWarm, releaseWarmStream, setRecording]);
|
}, []);
|
||||||
|
|
||||||
// Cleanup on unmount — always fully release the device, warm or not.
|
// Cleanup on unmount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
// Invalidate any in-flight acquisition so a stream resolving after unmount
|
|
||||||
// stops itself instead of leaking a live mic.
|
|
||||||
acquireGenRef.current += 1;
|
|
||||||
if (timerRef.current !== null) {
|
if (timerRef.current !== null) {
|
||||||
clearInterval(timerRef.current);
|
clearInterval(timerRef.current);
|
||||||
}
|
}
|
||||||
streamRef.current?.getTracks().forEach((track) => {
|
streamRef.current?.getTracks().forEach((track) => {
|
||||||
track.stop();
|
track.stop();
|
||||||
});
|
});
|
||||||
warmStreamRef.current?.getTracks().forEach((track) => {
|
|
||||||
track.stop();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -446,7 +216,5 @@ export function useAudioRecording({
|
|||||||
startRecording,
|
startRecording,
|
||||||
stopRecording,
|
stopRecording,
|
||||||
cancelRecording,
|
cancelRecording,
|
||||||
prewarm,
|
|
||||||
releaseWarm: releaseWarmStream,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,15 +54,11 @@ const SHORT_RECORDING_MESSAGE = 'Recording too short, canceled';
|
|||||||
export type CapturePillState = PillState | 'hidden';
|
export type CapturePillState = PillState | 'hidden';
|
||||||
|
|
||||||
export interface UseCaptureRecordingSessionOptions {
|
export interface UseCaptureRecordingSessionOptions {
|
||||||
/** Keep the microphone stream open between dictations when explicitly
|
|
||||||
* enabled. Off by default so normal recorders release the device. */
|
|
||||||
keepMicWarm?: boolean;
|
|
||||||
/**
|
/**
|
||||||
* Fired after a capture row is created on the server. Callers can use this
|
* Fired after a capture row is created on the server. Callers can use this
|
||||||
* to select the new capture or emit a Tauri event to a sibling window.
|
* to select the new capture or emit a Tauri event to a sibling window.
|
||||||
* ``context`` is whatever was passed to ``startRecording`` for this take.
|
|
||||||
*/
|
*/
|
||||||
onCaptureCreated?: (capture: CaptureResponse, context?: unknown) => void;
|
onCaptureCreated?: (capture: CaptureResponse) => void;
|
||||||
/**
|
/**
|
||||||
* Fired with the final delivered text — refined if ``auto_refine`` was on
|
* Fired with the final delivered text — refined if ``auto_refine`` was on
|
||||||
* for this capture, raw transcript otherwise. Used by the floating
|
* for this capture, raw transcript otherwise. Used by the floating
|
||||||
@@ -70,14 +66,12 @@ export interface UseCaptureRecordingSessionOptions {
|
|||||||
*
|
*
|
||||||
* ``allowAutoPaste`` snapshots the setting at chord-start so a refine that
|
* ``allowAutoPaste`` snapshots the setting at chord-start so a refine that
|
||||||
* lands after the user flips the toggle still uses the value the capture
|
* lands after the user flips the toggle still uses the value the capture
|
||||||
* was created under. ``context`` is the value passed to ``startRecording``
|
* was created under.
|
||||||
* for this take, so overlapping dictations can't cross their targets.
|
|
||||||
*/
|
*/
|
||||||
onFinalText?: (
|
onFinalText?: (
|
||||||
text: string,
|
text: string,
|
||||||
capture: CaptureResponse,
|
capture: CaptureResponse,
|
||||||
allowAutoPaste: boolean,
|
allowAutoPaste: boolean,
|
||||||
context?: unknown,
|
|
||||||
) => void;
|
) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,14 +82,12 @@ export interface UseCaptureRecordingSessionResult {
|
|||||||
isRecording: boolean;
|
isRecording: boolean;
|
||||||
isUploading: boolean;
|
isUploading: boolean;
|
||||||
isRefining: boolean;
|
isRefining: boolean;
|
||||||
startRecording: (context?: unknown) => void;
|
startRecording: () => void;
|
||||||
stopRecording: () => void;
|
stopRecording: () => void;
|
||||||
toggleRecording: () => void;
|
toggleRecording: () => void;
|
||||||
dismissError: () => void;
|
dismissError: () => void;
|
||||||
uploadFile: (file: File, source: CaptureSource) => void;
|
uploadFile: (file: File, source: CaptureSource) => void;
|
||||||
refine: (captureId: string) => void;
|
refine: (captureId: string) => void;
|
||||||
prewarm: () => Promise<void>;
|
|
||||||
releaseWarm: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,13 +123,10 @@ export function useCaptureRecordingSession(
|
|||||||
const onFinalTextRef = useRef(options.onFinalText);
|
const onFinalTextRef = useRef(options.onFinalText);
|
||||||
onFinalTextRef.current = options.onFinalText;
|
onFinalTextRef.current = options.onFinalText;
|
||||||
|
|
||||||
// Per-capture recording context and its ``allow_auto_paste`` snapshot, keyed
|
// Snapshot of ``allow_auto_paste`` from the capture-create response —
|
||||||
// by capture id so a refine that resolves after another dictation started
|
// held so the refine onSuccess (which only sees the plain CaptureResponse)
|
||||||
// still delivers to the right target with the setting the capture was created
|
// can still pass the original setting through to onFinalText.
|
||||||
// under. Populated on capture-create and consumed once the final text lands.
|
const allowAutoPasteRef = useRef<boolean>(true);
|
||||||
const captureDeliveryRef = useRef<Map<string, { context: unknown; allowAutoPaste: boolean }>>(
|
|
||||||
new Map(),
|
|
||||||
);
|
|
||||||
|
|
||||||
const clearRestTimer = useCallback(() => {
|
const clearRestTimer = useCallback(() => {
|
||||||
if (restTimerRef.current !== null) {
|
if (restTimerRef.current !== null) {
|
||||||
@@ -203,34 +192,20 @@ export function useCaptureRecordingSession(
|
|||||||
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
||||||
broadcastUpdated(captureId);
|
broadcastUpdated(captureId);
|
||||||
if (pillStateRef.current === 'refining') scheduleHidePill();
|
if (pillStateRef.current === 'refining') scheduleHidePill();
|
||||||
const delivery = captureDeliveryRef.current.get(captureId);
|
|
||||||
captureDeliveryRef.current.delete(captureId);
|
|
||||||
const finalText = data.transcript_refined ?? data.transcript_raw;
|
const finalText = data.transcript_refined ?? data.transcript_raw;
|
||||||
if (finalText) {
|
if (finalText) {
|
||||||
onFinalTextRef.current?.(
|
onFinalTextRef.current?.(finalText, data, allowAutoPasteRef.current);
|
||||||
finalText,
|
|
||||||
data,
|
|
||||||
delivery?.allowAutoPaste ?? true,
|
|
||||||
delivery?.context,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (err: Error, captureId) => {
|
onError: (err: Error) => {
|
||||||
captureDeliveryRef.current.delete(captureId);
|
|
||||||
showError(err.message || 'Refinement failed');
|
showError(err.message || 'Refinement failed');
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const uploadMutation = useMutation({
|
const uploadMutation = useMutation({
|
||||||
mutationFn: async ({
|
mutationFn: async ({ file, source }: { file: File; source: CaptureSource }) =>
|
||||||
file,
|
apiClient.createCapture(file, { source }),
|
||||||
source,
|
onSuccess: (capture) => {
|
||||||
}: {
|
|
||||||
file: File;
|
|
||||||
source: CaptureSource;
|
|
||||||
context?: unknown;
|
|
||||||
}) => apiClient.createCapture(file, { source }),
|
|
||||||
onSuccess: (capture, { context }) => {
|
|
||||||
queryClient.setQueryData<CaptureListResponse>(['captures'], (prev) => {
|
queryClient.setQueryData<CaptureListResponse>(['captures'], (prev) => {
|
||||||
if (!prev) return prev;
|
if (!prev) return prev;
|
||||||
if (prev.items.some((c) => c.id === capture.id)) return prev;
|
if (prev.items.some((c) => c.id === capture.id)) return prev;
|
||||||
@@ -238,12 +213,9 @@ export function useCaptureRecordingSession(
|
|||||||
});
|
});
|
||||||
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
queryClient.invalidateQueries({ queryKey: ['captures'] });
|
||||||
broadcastCreated(capture);
|
broadcastCreated(capture);
|
||||||
onCaptureCreatedRef.current?.(capture, context);
|
onCaptureCreatedRef.current?.(capture);
|
||||||
|
allowAutoPasteRef.current = capture.allow_auto_paste;
|
||||||
if (capture.auto_refine) {
|
if (capture.auto_refine) {
|
||||||
captureDeliveryRef.current.set(capture.id, {
|
|
||||||
context,
|
|
||||||
allowAutoPaste: capture.allow_auto_paste,
|
|
||||||
});
|
|
||||||
setPillState('refining');
|
setPillState('refining');
|
||||||
refineMutation.mutate(capture.id);
|
refineMutation.mutate(capture.id);
|
||||||
} else {
|
} else {
|
||||||
@@ -253,7 +225,6 @@ export function useCaptureRecordingSession(
|
|||||||
capture.transcript_raw,
|
capture.transcript_raw,
|
||||||
capture,
|
capture,
|
||||||
capture.allow_auto_paste,
|
capture.allow_auto_paste,
|
||||||
context,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -278,11 +249,8 @@ export function useCaptureRecordingSession(
|
|||||||
startRecording: beginAudioRecording,
|
startRecording: beginAudioRecording,
|
||||||
stopRecording,
|
stopRecording,
|
||||||
error: recordError,
|
error: recordError,
|
||||||
prewarm,
|
|
||||||
releaseWarm,
|
|
||||||
} = useAudioRecording({
|
} = useAudioRecording({
|
||||||
keepWarm: options.keepMicWarm ?? false,
|
onRecordingComplete: (blob, recordedDuration) => {
|
||||||
onRecordingComplete: (blob, recordedDuration, context) => {
|
|
||||||
// Trigger-happy tap — MediaRecorder hasn't emitted a usable chunk yet
|
// Trigger-happy tap — MediaRecorder hasn't emitted a usable chunk yet
|
||||||
// so the blob is empty or unparseable. Surface it as a transient pill
|
// so the blob is empty or unparseable. Surface it as a transient pill
|
||||||
// so the user sees their recording was recognised and canceled.
|
// so the user sees their recording was recognised and canceled.
|
||||||
@@ -300,7 +268,7 @@ export function useCaptureRecordingSession(
|
|||||||
const file = new File([blob], `dictation-${Date.now()}.${extension}`, {
|
const file = new File([blob], `dictation-${Date.now()}.${extension}`, {
|
||||||
type: blob.type,
|
type: blob.type,
|
||||||
});
|
});
|
||||||
uploadMutation.mutate({ file, source: 'dictation', context });
|
uploadMutation.mutate({ file, source: 'dictation' });
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -310,16 +278,13 @@ export function useCaptureRecordingSession(
|
|||||||
}
|
}
|
||||||
}, [recordError, showError]);
|
}, [recordError, showError]);
|
||||||
|
|
||||||
const startRecording = useCallback(
|
const startRecording = useCallback(() => {
|
||||||
(context?: unknown) => {
|
if (isRecording) return;
|
||||||
if (isRecording) return;
|
clearRestTimer();
|
||||||
clearRestTimer();
|
setFrozenElapsedMs(0);
|
||||||
setFrozenElapsedMs(0);
|
setPillState('recording');
|
||||||
setPillState('recording');
|
beginAudioRecording();
|
||||||
beginAudioRecording(context);
|
}, [isRecording, beginAudioRecording, clearRestTimer]);
|
||||||
},
|
|
||||||
[isRecording, beginAudioRecording, clearRestTimer],
|
|
||||||
);
|
|
||||||
|
|
||||||
const toggleRecording = useCallback(() => {
|
const toggleRecording = useCallback(() => {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
@@ -359,7 +324,5 @@ export function useCaptureRecordingSession(
|
|||||||
dismissError,
|
dismissError,
|
||||||
uploadFile,
|
uploadFile,
|
||||||
refine,
|
refine,
|
||||||
prewarm,
|
|
||||||
releaseWarm,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { invoke } from '@tauri-apps/api/core';
|
import { invoke } from '@tauri-apps/api/core';
|
||||||
import { emit, listen } from '@tauri-apps/api/event';
|
import { useEffect } from 'react';
|
||||||
import { useEffect, useRef } from 'react';
|
|
||||||
import { useDictationReadiness } from '@/lib/hooks/useDictationReadiness';
|
import { useDictationReadiness } from '@/lib/hooks/useDictationReadiness';
|
||||||
import { useCaptureSettings } from '@/lib/hooks/useSettings';
|
import { useCaptureSettings } from '@/lib/hooks/useSettings';
|
||||||
import { usePlatform } from '@/platform/PlatformContext';
|
import { usePlatform } from '@/platform/PlatformContext';
|
||||||
@@ -31,45 +30,21 @@ export function useChordSync() {
|
|||||||
const { settings } = useCaptureSettings();
|
const { settings } = useCaptureSettings();
|
||||||
const { canRecord } = useDictationReadiness();
|
const { canRecord } = useDictationReadiness();
|
||||||
const enabled = settings?.hotkey_enabled;
|
const enabled = settings?.hotkey_enabled;
|
||||||
const keepMicWarm = settings?.keep_mic_warm;
|
|
||||||
const pushKeys = settings?.chord_push_to_talk_keys;
|
const pushKeys = settings?.chord_push_to_talk_keys;
|
||||||
const toggleKeys = settings?.chord_toggle_to_talk_keys;
|
const toggleKeys = settings?.chord_toggle_to_talk_keys;
|
||||||
|
|
||||||
// Latest warm state, so the dictate window's mount-time request can be
|
|
||||||
// answered even between the dep-driven emits below.
|
|
||||||
const shouldWarmRef = useRef(false);
|
|
||||||
|
|
||||||
// The floating dictate window holds the mic warm ahead of the first chord to
|
|
||||||
// avoid clipping, but it's a separate webview with no view of settings. Mirror
|
|
||||||
// the decision to it: warm only when dictation is armed AND the user enabled
|
|
||||||
// "keep microphone ready". Gating here is what stops the always-mounted pill
|
|
||||||
// from opening the mic — or prompting for access — when the user hasn't asked.
|
|
||||||
useEffect(() => {
|
|
||||||
if (!platform.metadata.isTauri) return;
|
|
||||||
const unlisten = listen('dictate:warm-request', () => {
|
|
||||||
emit('dictate:warm', shouldWarmRef.current).catch(() => {});
|
|
||||||
});
|
|
||||||
return () => {
|
|
||||||
unlisten.then((fn) => fn()).catch(() => {});
|
|
||||||
};
|
|
||||||
}, [platform.metadata.isTauri]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!platform.metadata.isTauri) return;
|
if (!platform.metadata.isTauri) return;
|
||||||
if (enabled === undefined || !pushKeys || !toggleKeys) return;
|
if (enabled === undefined || !pushKeys || !toggleKeys) return;
|
||||||
const shouldArm = enabled && canRecord;
|
const shouldArm = enabled && canRecord;
|
||||||
const shouldWarm = shouldArm && (keepMicWarm ?? false);
|
|
||||||
shouldWarmRef.current = shouldWarm;
|
|
||||||
const command = shouldArm ? 'enable_hotkey' : 'disable_hotkey';
|
const command = shouldArm ? 'enable_hotkey' : 'disable_hotkey';
|
||||||
const args = shouldArm ? { pushToTalk: pushKeys, toggleToTalk: toggleKeys } : {};
|
const args = shouldArm ? { pushToTalk: pushKeys, toggleToTalk: toggleKeys } : {};
|
||||||
invoke(command, args).catch((err) => {
|
invoke(command, args).catch((err) => {
|
||||||
console.warn(`[chord-sync] ${command} failed:`, err);
|
console.warn(`[chord-sync] ${command} failed:`, err);
|
||||||
});
|
});
|
||||||
emit('dictate:warm', shouldWarm).catch(() => {});
|
|
||||||
}, [
|
}, [
|
||||||
platform.metadata.isTauri,
|
platform.metadata.isTauri,
|
||||||
enabled,
|
enabled,
|
||||||
keepMicWarm,
|
|
||||||
canRecord,
|
canRecord,
|
||||||
// Stringify so a referentially-new array with the same content
|
// Stringify so a referentially-new array with the same content
|
||||||
// doesn't fire a redundant invoke on every settings refetch.
|
// doesn't fire a redundant invoke on every settings refetch.
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { QueryClientProvider } from '@tanstack/react-query';
|
||||||
|
// import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
||||||
|
import React from 'react';
|
||||||
|
import ReactDOM from 'react-dom/client';
|
||||||
|
import App from './App';
|
||||||
|
import './i18n';
|
||||||
|
import './index.css';
|
||||||
|
import { queryClient } from './lib/queryClient';
|
||||||
|
|
||||||
|
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<App />
|
||||||
|
{/* <ReactQueryDevtools initialIsOpen={false} /> */}
|
||||||
|
</QueryClientProvider>
|
||||||
|
</React.StrictMode>,
|
||||||
|
);
|
||||||
@@ -9,8 +9,7 @@ export interface FileFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface PlatformFilesystem {
|
export interface PlatformFilesystem {
|
||||||
/** Returns the saved path (or filename on web), or null if the user cancelled. */
|
saveFile(filename: string, blob: Blob, filters?: FileFilter[]): Promise<void>;
|
||||||
saveFile(filename: string, blob: Blob, filters?: FileFilter[]): Promise<string | null>;
|
|
||||||
openPath(path: string): Promise<void>;
|
openPath(path: string): Promise<void>;
|
||||||
pickDirectory(title: string): Promise<string | null>;
|
pickDirectory(title: string): Promise<string | null>;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -113,7 +113,7 @@ const voicesRoute = createRoute({
|
|||||||
component: VoicesTab,
|
component: VoicesTab,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Captures route
|
// Captures route (prototype — will replace AudioTab once the new flow is ready)
|
||||||
const capturesRoute = createRoute({
|
const capturesRoute = createRoute({
|
||||||
getParentRoute: () => rootRoute,
|
getParentRoute: () => rootRoute,
|
||||||
path: '/captures',
|
path: '/captures',
|
||||||
@@ -199,8 +199,8 @@ const serverRedirectRoute = createRoute({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Route tree — exported so tests can build routers over memory history
|
// Route tree
|
||||||
export const routeTree = rootRoute.addChildren([
|
const routeTree = rootRoute.addChildren([
|
||||||
indexRoute,
|
indexRoute,
|
||||||
storiesRoute,
|
storiesRoute,
|
||||||
capturesRoute,
|
capturesRoute,
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
import { describe, expect, it, vi } from 'vitest';
|
|
||||||
import { queryClient } from '@/lib/queryClient';
|
|
||||||
import { isLoopbackVoiceboxServerUrl, useServerStore } from '@/stores/serverStore';
|
|
||||||
|
|
||||||
describe('serverStore', () => {
|
|
||||||
it('invalidates all queries when the server url changes', () => {
|
|
||||||
const spy = vi.spyOn(queryClient, 'invalidateQueries');
|
|
||||||
|
|
||||||
useServerStore.getState().setServerUrl('http://10.0.0.5:17493');
|
|
||||||
|
|
||||||
expect(useServerStore.getState().serverUrl).toBe('http://10.0.0.5:17493');
|
|
||||||
expect(spy).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not invalidate queries when the url is unchanged', () => {
|
|
||||||
const url = useServerStore.getState().serverUrl;
|
|
||||||
const spy = vi.spyOn(queryClient, 'invalidateQueries');
|
|
||||||
|
|
||||||
useServerStore.getState().setServerUrl(url);
|
|
||||||
|
|
||||||
expect(spy).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('isLoopbackVoiceboxServerUrl', () => {
|
|
||||||
it('matches loopback hosts on the voicebox port', () => {
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('http://127.0.0.1:17493')).toBe(true);
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('http://localhost:17493')).toBe(true);
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('http://[::1]:17493')).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('rejects other hosts, ports, and junk', () => {
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('http://10.0.0.5:17493')).toBe(false);
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('http://127.0.0.1:8000')).toBe(false);
|
|
||||||
expect(isLoopbackVoiceboxServerUrl('not a url')).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
|
||||||
import { useUIStore } from '@/stores/uiStore';
|
|
||||||
|
|
||||||
describe('uiStore', () => {
|
|
||||||
it('applies the dark class when theme is set to dark', () => {
|
|
||||||
useUIStore.getState().setTheme('dark');
|
|
||||||
|
|
||||||
expect(useUIStore.getState().theme).toBe('dark');
|
|
||||||
expect(document.documentElement.classList.contains('dark')).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('removes the dark class when theme is set to light', () => {
|
|
||||||
useUIStore.getState().setTheme('dark');
|
|
||||||
useUIStore.getState().setTheme('light');
|
|
||||||
|
|
||||||
expect(document.documentElement.classList.contains('dark')).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('persists only theme and selectedProfileId', () => {
|
|
||||||
useUIStore.getState().setTheme('light');
|
|
||||||
useUIStore.getState().setSidebarOpen(false);
|
|
||||||
useUIStore.getState().setSelectedEngine('kokoro');
|
|
||||||
|
|
||||||
const persisted = JSON.parse(localStorage.getItem('voicebox-ui') ?? '{}');
|
|
||||||
expect(persisted.state).toEqual({ selectedProfileId: null, theme: 'light' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
import { http } from 'msw';
|
|
||||||
import { expect, it } from 'vitest';
|
|
||||||
import { buildModelStatus, buildProfile } from './msw/fixtures';
|
|
||||||
import {
|
|
||||||
captureHandlers,
|
|
||||||
effectsHandlers,
|
|
||||||
historyHandlers,
|
|
||||||
modelHandlers,
|
|
||||||
profileHandlers,
|
|
||||||
settingsHandlers,
|
|
||||||
storyHandlers,
|
|
||||||
taskHandlers,
|
|
||||||
} from './msw/handlers';
|
|
||||||
import { worker } from './msw/worker';
|
|
||||||
import { renderRoute } from './render';
|
|
||||||
import { sseController } from './sse';
|
|
||||||
|
|
||||||
function useHappyPathHandlers() {
|
|
||||||
worker.use(
|
|
||||||
...profileHandlers([buildProfile({ name: 'Ada Lovelace' })]),
|
|
||||||
...historyHandlers([]),
|
|
||||||
...captureHandlers([]),
|
|
||||||
...settingsHandlers(),
|
|
||||||
...modelHandlers([buildModelStatus()]),
|
|
||||||
...storyHandlers([]),
|
|
||||||
...effectsHandlers([]),
|
|
||||||
...taskHandlers(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
it('renders the /voices route with the full app chrome', async () => {
|
|
||||||
useHappyPathHandlers();
|
|
||||||
|
|
||||||
const screen = await renderRoute('/voices');
|
|
||||||
|
|
||||||
await expect.element(screen.getByText('Ada Lovelace')).toBeVisible();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('feeds EventSource through the SSE controller', async () => {
|
|
||||||
const sse = sseController();
|
|
||||||
worker.use(http.get('*/generate/:id/status', () => sse.response()));
|
|
||||||
|
|
||||||
const source = new EventSource('/generate/gen-1/status');
|
|
||||||
const statuses: string[] = [];
|
|
||||||
source.onmessage = (message) => {
|
|
||||||
statuses.push((JSON.parse(message.data) as { status: string }).status);
|
|
||||||
};
|
|
||||||
await new Promise((resolve) => {
|
|
||||||
source.onopen = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
sse.push({ data: { status: 'generating' } });
|
|
||||||
sse.push({ data: { status: 'completed' } });
|
|
||||||
|
|
||||||
await expect.poll(() => statuses).toEqual(['generating', 'completed']);
|
|
||||||
source.close();
|
|
||||||
sse.close();
|
|
||||||
});
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
import { vi } from 'vitest';
|
|
||||||
import type { Platform, UpdateStatus } from '@/platform/types';
|
|
||||||
|
|
||||||
export interface MockPlatform extends Platform {
|
|
||||||
/** Push a new updater status to all subscribers, as the real updater would. */
|
|
||||||
emitUpdateStatus(status: UpdateStatus): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MockPlatformOverrides {
|
|
||||||
filesystem?: Partial<Platform['filesystem']>;
|
|
||||||
updater?: Partial<Platform['updater']>;
|
|
||||||
audio?: Partial<Platform['audio']>;
|
|
||||||
lifecycle?: Partial<Platform['lifecycle']>;
|
|
||||||
metadata?: Partial<Platform['metadata']>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const INITIAL_UPDATE_STATUS: UpdateStatus = {
|
|
||||||
checking: false,
|
|
||||||
available: false,
|
|
||||||
downloading: false,
|
|
||||||
installing: false,
|
|
||||||
readyToInstall: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const TEST_SERVER_URL = 'http://127.0.0.1:17493';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A fully spy-able Platform. Every method is a vi.fn with a benign default
|
|
||||||
* (browser-like: no system audio, isTauri false), so tests can assert calls
|
|
||||||
* or override behavior per section via `overrides`.
|
|
||||||
*/
|
|
||||||
export function createMockPlatform(overrides: MockPlatformOverrides = {}): MockPlatform {
|
|
||||||
let updateStatus = { ...INITIAL_UPDATE_STATUS };
|
|
||||||
const subscribers = new Set<(status: UpdateStatus) => void>();
|
|
||||||
|
|
||||||
return {
|
|
||||||
filesystem: {
|
|
||||||
saveFile: vi.fn(async (filename: string) => filename),
|
|
||||||
openPath: vi.fn(async () => {}),
|
|
||||||
pickDirectory: vi.fn(async () => null),
|
|
||||||
...overrides.filesystem,
|
|
||||||
},
|
|
||||||
updater: {
|
|
||||||
checkForUpdates: vi.fn(async () => {}),
|
|
||||||
downloadAndInstall: vi.fn(async () => {}),
|
|
||||||
restartAndInstall: vi.fn(async () => {}),
|
|
||||||
getStatus: vi.fn(() => ({ ...updateStatus })),
|
|
||||||
subscribe: vi.fn((callback: (status: UpdateStatus) => void) => {
|
|
||||||
subscribers.add(callback);
|
|
||||||
callback(updateStatus);
|
|
||||||
return () => {
|
|
||||||
subscribers.delete(callback);
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
...overrides.updater,
|
|
||||||
},
|
|
||||||
audio: {
|
|
||||||
isSystemAudioSupported: vi.fn(async () => false),
|
|
||||||
startSystemAudioCapture: vi.fn(async () => {}),
|
|
||||||
stopSystemAudioCapture: vi.fn(async () => new Blob()),
|
|
||||||
listOutputDevices: vi.fn(async () => []),
|
|
||||||
playToDevices: vi.fn(async () => {}),
|
|
||||||
stopPlayback: vi.fn(),
|
|
||||||
...overrides.audio,
|
|
||||||
},
|
|
||||||
lifecycle: {
|
|
||||||
startServer: vi.fn(async () => TEST_SERVER_URL),
|
|
||||||
stopServer: vi.fn(async () => {}),
|
|
||||||
restartServer: vi.fn(async () => TEST_SERVER_URL),
|
|
||||||
setKeepServerRunning: vi.fn(async () => {}),
|
|
||||||
setBackendOverride: vi.fn(async () => {}),
|
|
||||||
setupWindowCloseHandler: vi.fn(async () => {}),
|
|
||||||
subscribeToServerLogs: vi.fn(() => () => {}),
|
|
||||||
...overrides.lifecycle,
|
|
||||||
},
|
|
||||||
metadata: {
|
|
||||||
getVersion: vi.fn(async () => '0.0.0-test'),
|
|
||||||
isTauri: false,
|
|
||||||
...overrides.metadata,
|
|
||||||
},
|
|
||||||
emitUpdateStatus(status: UpdateStatus) {
|
|
||||||
updateStatus = { ...status };
|
|
||||||
for (const callback of subscribers) callback(updateStatus);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,216 +0,0 @@
|
|||||||
import type {
|
|
||||||
CaptureListResponse,
|
|
||||||
CaptureReadinessResponse,
|
|
||||||
CaptureResponse,
|
|
||||||
CaptureSettings,
|
|
||||||
EffectPresetResponse,
|
|
||||||
GenerationResponse,
|
|
||||||
GenerationSettings,
|
|
||||||
HealthResponse,
|
|
||||||
HistoryListResponse,
|
|
||||||
HistoryResponse,
|
|
||||||
ModelStatus,
|
|
||||||
StoryDetailResponse,
|
|
||||||
StoryItemDetail,
|
|
||||||
StoryResponse,
|
|
||||||
VoiceProfileResponse,
|
|
||||||
} from '@/lib/api/types';
|
|
||||||
|
|
||||||
// Deterministic id counter — no randomness so failures reproduce exactly.
|
|
||||||
let seq = 0;
|
|
||||||
export function nextId(prefix: string): string {
|
|
||||||
seq += 1;
|
|
||||||
return `${prefix}-${String(seq).padStart(4, '0')}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const CREATED_AT = '2026-01-01T00:00:00Z';
|
|
||||||
|
|
||||||
export function buildProfile(overrides: Partial<VoiceProfileResponse> = {}): VoiceProfileResponse {
|
|
||||||
return {
|
|
||||||
id: nextId('profile'),
|
|
||||||
name: 'Test Voice',
|
|
||||||
language: 'en',
|
|
||||||
voice_type: 'cloned',
|
|
||||||
generation_count: 0,
|
|
||||||
sample_count: 1,
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
updated_at: CREATED_AT,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildGeneration(overrides: Partial<GenerationResponse> = {}): GenerationResponse {
|
|
||||||
return {
|
|
||||||
id: nextId('gen'),
|
|
||||||
profile_id: 'profile-0001',
|
|
||||||
text: 'Hello from the test suite.',
|
|
||||||
language: 'en',
|
|
||||||
status: 'completed',
|
|
||||||
audio_path: '/audio/fake.wav',
|
|
||||||
duration: 1.5,
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildHistoryItem(overrides: Partial<HistoryResponse> = {}): HistoryResponse {
|
|
||||||
return {
|
|
||||||
...buildGeneration(),
|
|
||||||
profile_name: 'Test Voice',
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildHistoryList(items: HistoryResponse[]): HistoryListResponse {
|
|
||||||
return { items, total: items.length };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildCapture(overrides: Partial<CaptureResponse> = {}): CaptureResponse {
|
|
||||||
return {
|
|
||||||
id: nextId('capture'),
|
|
||||||
audio_path: '/captures/fake.wav',
|
|
||||||
source: 'dictation',
|
|
||||||
language: 'en',
|
|
||||||
duration_ms: 2400,
|
|
||||||
transcript_raw: 'raw transcript text',
|
|
||||||
transcript_refined: 'Refined transcript text.',
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildCaptureList(items: CaptureResponse[]): CaptureListResponse {
|
|
||||||
return { items, total: items.length };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildCaptureSettings(overrides: Partial<CaptureSettings> = {}): CaptureSettings {
|
|
||||||
return {
|
|
||||||
stt_model: 'turbo',
|
|
||||||
language: 'en',
|
|
||||||
auto_refine: true,
|
|
||||||
llm_model: '0.6B',
|
|
||||||
smart_cleanup: true,
|
|
||||||
self_correction: true,
|
|
||||||
preserve_technical: true,
|
|
||||||
allow_auto_paste: false,
|
|
||||||
default_playback_voice_id: null,
|
|
||||||
hotkey_enabled: false,
|
|
||||||
keep_mic_warm: false,
|
|
||||||
chord_push_to_talk_keys: [],
|
|
||||||
chord_toggle_to_talk_keys: [],
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildCaptureReadiness(
|
|
||||||
overrides: Partial<CaptureReadinessResponse> = {},
|
|
||||||
): CaptureReadinessResponse {
|
|
||||||
return {
|
|
||||||
stt: {
|
|
||||||
ready: true,
|
|
||||||
model_name: 'whisper-turbo',
|
|
||||||
display_name: 'Whisper Turbo',
|
|
||||||
size: '1.6 GB',
|
|
||||||
},
|
|
||||||
llm: {
|
|
||||||
ready: true,
|
|
||||||
model_name: 'qwen3-0.6b',
|
|
||||||
display_name: 'Qwen3 0.6B',
|
|
||||||
size: '600 MB',
|
|
||||||
},
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildGenerationSettings(
|
|
||||||
overrides: Partial<GenerationSettings> = {},
|
|
||||||
): GenerationSettings {
|
|
||||||
return {
|
|
||||||
max_chunk_chars: 400,
|
|
||||||
crossfade_ms: 60,
|
|
||||||
normalize_audio: true,
|
|
||||||
autoplay_on_generate: false,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildModelStatus(overrides: Partial<ModelStatus> = {}): ModelStatus {
|
|
||||||
return {
|
|
||||||
model_name: 'qwen-tts-1.7b',
|
|
||||||
display_name: 'Qwen TTS 1.7B',
|
|
||||||
downloaded: true,
|
|
||||||
downloading: false,
|
|
||||||
loaded: false,
|
|
||||||
size_mb: 3400,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildStory(overrides: Partial<StoryResponse> = {}): StoryResponse {
|
|
||||||
return {
|
|
||||||
id: nextId('story'),
|
|
||||||
name: 'Test Story',
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
updated_at: CREATED_AT,
|
|
||||||
item_count: 0,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildStoryItem(overrides: Partial<StoryItemDetail> = {}): StoryItemDetail {
|
|
||||||
return {
|
|
||||||
id: nextId('story-item'),
|
|
||||||
story_id: 'story-0001',
|
|
||||||
generation_id: 'gen-0001',
|
|
||||||
start_time_ms: 0,
|
|
||||||
track: 0,
|
|
||||||
trim_start_ms: 0,
|
|
||||||
trim_end_ms: 0,
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
profile_id: 'profile-0001',
|
|
||||||
profile_name: 'Test Voice',
|
|
||||||
text: 'Hello from the test suite.',
|
|
||||||
language: 'en',
|
|
||||||
audio_path: '/audio/fake.wav',
|
|
||||||
duration: 1.5,
|
|
||||||
volume: 1,
|
|
||||||
generation_created_at: CREATED_AT,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildStoryDetail(
|
|
||||||
overrides: Partial<StoryDetailResponse> = {},
|
|
||||||
): StoryDetailResponse {
|
|
||||||
return {
|
|
||||||
id: 'story-0001',
|
|
||||||
name: 'Test Story',
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
updated_at: CREATED_AT,
|
|
||||||
items: [],
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildEffectPreset(
|
|
||||||
overrides: Partial<EffectPresetResponse> = {},
|
|
||||||
): EffectPresetResponse {
|
|
||||||
return {
|
|
||||||
id: nextId('preset'),
|
|
||||||
name: 'Test Preset',
|
|
||||||
effects_chain: [{ type: 'reverb', enabled: true, params: { wet: 0.3 } }],
|
|
||||||
is_builtin: false,
|
|
||||||
created_at: CREATED_AT,
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildHealth(overrides: Partial<HealthResponse> = {}): HealthResponse {
|
|
||||||
return {
|
|
||||||
status: 'ok',
|
|
||||||
model_loaded: false,
|
|
||||||
gpu_available: false,
|
|
||||||
backend_variant: 'cpu',
|
|
||||||
...overrides,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
import type { HttpHandler } from 'msw';
|
|
||||||
import { HttpResponse, http } from 'msw';
|
|
||||||
import type {
|
|
||||||
CaptureResponse,
|
|
||||||
CaptureSettings,
|
|
||||||
EffectPresetResponse,
|
|
||||||
GenerationSettings,
|
|
||||||
HistoryResponse,
|
|
||||||
ModelStatus,
|
|
||||||
StoryDetailResponse,
|
|
||||||
StoryResponse,
|
|
||||||
VoiceProfileResponse,
|
|
||||||
} from '@/lib/api/types';
|
|
||||||
import { buildCaptureReadiness, buildCaptureSettings, buildGenerationSettings } from '../fixtures';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Happy-path handlers for one domain each. Tests compose what they need:
|
|
||||||
* worker.use(...profileHandlers([buildProfile()]), ...historyHandlers([]))
|
|
||||||
* Anything not stubbed fails loudly via onUnhandledRequest: 'error'.
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function profileHandlers(profiles: VoiceProfileResponse[]): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/profiles', () => HttpResponse.json(profiles)),
|
|
||||||
http.get('*/profiles/presets/:engine', () => HttpResponse.json([])),
|
|
||||||
http.get('*/profiles/:id', ({ params }) => {
|
|
||||||
const profile = profiles.find((p) => p.id === params.id);
|
|
||||||
return profile ? HttpResponse.json(profile) : new HttpResponse(null, { status: 404 });
|
|
||||||
}),
|
|
||||||
http.get('*/profiles/:id/channels', () => HttpResponse.json([])),
|
|
||||||
http.get('*/profiles/:id/samples', () => HttpResponse.json([])),
|
|
||||||
http.get('*/channels', () => HttpResponse.json([])),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function historyHandlers(items: HistoryResponse[]): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/history', () => HttpResponse.json({ items, total: items.length })),
|
|
||||||
http.get('*/history/:id', ({ params }) => {
|
|
||||||
const item = items.find((i) => i.id === params.id);
|
|
||||||
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 });
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function captureHandlers(
|
|
||||||
items: CaptureResponse[],
|
|
||||||
settings: CaptureSettings = buildCaptureSettings(),
|
|
||||||
): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/captures', () => HttpResponse.json({ items, total: items.length })),
|
|
||||||
http.get('*/capture/readiness', () => HttpResponse.json(buildCaptureReadiness())),
|
|
||||||
http.get('*/settings/captures', () => HttpResponse.json(settings)),
|
|
||||||
http.put('*/settings/captures', async ({ request }) => {
|
|
||||||
const update = (await request.json()) as Partial<CaptureSettings>;
|
|
||||||
return HttpResponse.json({ ...settings, ...update });
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function settingsHandlers(
|
|
||||||
generation: GenerationSettings = buildGenerationSettings(),
|
|
||||||
): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/settings/generation', () => HttpResponse.json(generation)),
|
|
||||||
http.put('*/settings/generation', async ({ request }) => {
|
|
||||||
const update = (await request.json()) as Partial<GenerationSettings>;
|
|
||||||
return HttpResponse.json({ ...generation, ...update });
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function modelHandlers(models: ModelStatus[]): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/models/status', () => HttpResponse.json({ models })),
|
|
||||||
http.get('*/models/cache-dir', () => HttpResponse.json({ cache_dir: '/tmp/models' })),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function storyHandlers(
|
|
||||||
stories: StoryResponse[],
|
|
||||||
details: StoryDetailResponse[] = [],
|
|
||||||
): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/stories', () => HttpResponse.json(stories)),
|
|
||||||
http.get('*/stories/:id', ({ params }) => {
|
|
||||||
const detail = details.find((d) => d.id === params.id);
|
|
||||||
return detail ? HttpResponse.json(detail) : new HttpResponse(null, { status: 404 });
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function effectsHandlers(presets: EffectPresetResponse[]): HttpHandler[] {
|
|
||||||
return [
|
|
||||||
http.get('*/effects/available', () => HttpResponse.json({ effects: [] })),
|
|
||||||
http.get('*/effects/presets', () => HttpResponse.json(presets)),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function taskHandlers(): HttpHandler[] {
|
|
||||||
return [http.get('*/tasks/active', () => HttpResponse.json({ downloads: [], generations: [] }))];
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import { type HttpHandler, HttpResponse, http } from 'msw';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Baseline handlers for endpoints nearly every screen touches. The health
|
|
||||||
* payload mirrors backend/routes/health.py closely enough for the UI's
|
|
||||||
* checks (`status`, `model_loaded`, backend variant fields).
|
|
||||||
*/
|
|
||||||
export const serverHandlers: HttpHandler[] = [
|
|
||||||
http.get('*/health', () =>
|
|
||||||
HttpResponse.json({
|
|
||||||
status: 'ok',
|
|
||||||
model_loaded: false,
|
|
||||||
device: 'cpu',
|
|
||||||
backend_variant: 'cpu',
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { setupWorker } from 'msw/browser';
|
|
||||||
import { serverHandlers } from './handlers/server';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Browser-mode MSW worker. Individual tests layer route-specific handlers
|
|
||||||
* on top with `worker.use(...)`; `setup.browser.ts` resets them after each
|
|
||||||
* test. Only the health/baseline handlers are registered globally.
|
|
||||||
*/
|
|
||||||
export const worker = setupWorker(...serverHandlers);
|
|
||||||
@@ -1,346 +0,0 @@
|
|||||||
/* eslint-disable */
|
|
||||||
/* tslint:disable */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mock Service Worker.
|
|
||||||
* @see https://github.com/mswjs/msw
|
|
||||||
* - Please do NOT modify this file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const PACKAGE_VERSION = '2.15.0';
|
|
||||||
const INTEGRITY_CHECKSUM = '03cb67ac84128e63d7cd722a6e5b7f1e';
|
|
||||||
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse');
|
|
||||||
const activeClientIds = new Set();
|
|
||||||
|
|
||||||
addEventListener('install', () => {
|
|
||||||
self.skipWaiting();
|
|
||||||
});
|
|
||||||
|
|
||||||
addEventListener('activate', (event) => {
|
|
||||||
event.waitUntil(self.clients.claim());
|
|
||||||
});
|
|
||||||
|
|
||||||
addEventListener('message', async (event) => {
|
|
||||||
const clientId = Reflect.get(event.source || {}, 'id');
|
|
||||||
|
|
||||||
if (!clientId || !self.clients) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = await self.clients.get(clientId);
|
|
||||||
|
|
||||||
if (!client) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const allClients = await self.clients.matchAll({
|
|
||||||
type: 'window',
|
|
||||||
});
|
|
||||||
|
|
||||||
switch (event.data) {
|
|
||||||
case 'KEEPALIVE_REQUEST': {
|
|
||||||
sendToClient(client, {
|
|
||||||
type: 'KEEPALIVE_RESPONSE',
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'INTEGRITY_CHECK_REQUEST': {
|
|
||||||
sendToClient(client, {
|
|
||||||
type: 'INTEGRITY_CHECK_RESPONSE',
|
|
||||||
payload: {
|
|
||||||
packageVersion: PACKAGE_VERSION,
|
|
||||||
checksum: INTEGRITY_CHECKSUM,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'MOCK_ACTIVATE': {
|
|
||||||
activeClientIds.add(clientId);
|
|
||||||
|
|
||||||
sendToClient(client, {
|
|
||||||
type: 'MOCKING_ENABLED',
|
|
||||||
payload: {
|
|
||||||
client: {
|
|
||||||
id: client.id,
|
|
||||||
frameType: client.frameType,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'CLIENT_CLOSED': {
|
|
||||||
activeClientIds.delete(clientId);
|
|
||||||
|
|
||||||
const remainingClients = allClients.filter((client) => {
|
|
||||||
return client.id !== clientId;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Unregister itself when there are no more clients
|
|
||||||
if (remainingClients.length === 0) {
|
|
||||||
self.registration.unregister();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
addEventListener('fetch', (event) => {
|
|
||||||
const requestInterceptedAt = Date.now();
|
|
||||||
|
|
||||||
// Bypass navigation requests.
|
|
||||||
if (event.request.mode === 'navigate') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Opening the DevTools triggers the "only-if-cached" request
|
|
||||||
// that cannot be handled by the worker. Bypass such requests.
|
|
||||||
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bypass all requests when there are no active clients.
|
|
||||||
// Prevents the self-unregistered worked from handling requests
|
|
||||||
// after it's been terminated (still remains active until the next reload).
|
|
||||||
if (activeClientIds.size === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestId = crypto.randomUUID();
|
|
||||||
event.respondWith(handleRequest(event, requestId, requestInterceptedAt));
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {FetchEvent} event
|
|
||||||
* @param {string} requestId
|
|
||||||
* @param {number} requestInterceptedAt
|
|
||||||
*/
|
|
||||||
async function handleRequest(event, requestId, requestInterceptedAt) {
|
|
||||||
const client = await resolveMainClient(event);
|
|
||||||
const requestCloneForEvents = event.request.clone();
|
|
||||||
const response = await getResponse(event, client, requestId, requestInterceptedAt);
|
|
||||||
|
|
||||||
// Send back the response clone for the "response:*" life-cycle events.
|
|
||||||
// Ensure MSW is active and ready to handle the message, otherwise
|
|
||||||
// this message will pend indefinitely.
|
|
||||||
if (client && activeClientIds.has(client.id)) {
|
|
||||||
const serializedRequest = await serializeRequest(requestCloneForEvents);
|
|
||||||
|
|
||||||
// Omit the body of server-sent event stream responses.
|
|
||||||
// Cloning such responses would prevent client-side stream cancelations
|
|
||||||
// from reaching the original stream (a teed stream only cancels its
|
|
||||||
// source once both of its branches cancel) and would buffer the
|
|
||||||
// entire stream into the unconsumed clone indefinitely.
|
|
||||||
const isEventStreamResponse = response.headers
|
|
||||||
.get('content-type')
|
|
||||||
?.toLowerCase()
|
|
||||||
.startsWith('text/event-stream');
|
|
||||||
|
|
||||||
// Clone the response so both the client and the library could consume it.
|
|
||||||
const responseClone = isEventStreamResponse ? null : response.clone();
|
|
||||||
|
|
||||||
sendToClient(
|
|
||||||
client,
|
|
||||||
{
|
|
||||||
type: 'RESPONSE',
|
|
||||||
payload: {
|
|
||||||
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
||||||
request: {
|
|
||||||
id: requestId,
|
|
||||||
...serializedRequest,
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
type: response.type,
|
|
||||||
status: response.status,
|
|
||||||
statusText: response.statusText,
|
|
||||||
headers: Object.fromEntries(response.headers.entries()),
|
|
||||||
body: responseClone ? responseClone.body : null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
responseClone && responseClone.body ? [serializedRequest.body, responseClone.body] : [],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve the main client for the given event.
|
|
||||||
* Client that issues a request doesn't necessarily equal the client
|
|
||||||
* that registered the worker. It's with the latter the worker should
|
|
||||||
* communicate with during the response resolving phase.
|
|
||||||
* @param {FetchEvent} event
|
|
||||||
* @returns {Promise<Client | undefined>}
|
|
||||||
*/
|
|
||||||
async function resolveMainClient(event) {
|
|
||||||
const client = await self.clients.get(event.clientId);
|
|
||||||
|
|
||||||
if (activeClientIds.has(event.clientId)) {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client?.frameType === 'top-level') {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
const allClients = await self.clients.matchAll({
|
|
||||||
type: 'window',
|
|
||||||
});
|
|
||||||
|
|
||||||
return allClients
|
|
||||||
.filter((client) => {
|
|
||||||
// Get only those clients that are currently visible.
|
|
||||||
return client.visibilityState === 'visible';
|
|
||||||
})
|
|
||||||
.find((client) => {
|
|
||||||
// Find the client ID that's recorded in the
|
|
||||||
// set of clients that have registered the worker.
|
|
||||||
return activeClientIds.has(client.id);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {FetchEvent} event
|
|
||||||
* @param {Client | undefined} client
|
|
||||||
* @param {string} requestId
|
|
||||||
* @param {number} requestInterceptedAt
|
|
||||||
* @returns {Promise<Response>}
|
|
||||||
*/
|
|
||||||
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
|
||||||
// Clone the request because it might've been already used
|
|
||||||
// (i.e. its body has been read and sent to the client).
|
|
||||||
const requestClone = event.request.clone();
|
|
||||||
|
|
||||||
function passthrough() {
|
|
||||||
// Cast the request headers to a new Headers instance
|
|
||||||
// so the headers can be manipulated with.
|
|
||||||
const headers = new Headers(requestClone.headers);
|
|
||||||
|
|
||||||
// Remove the "accept" header value that marked this request as passthrough.
|
|
||||||
// This prevents request alteration and also keeps it compliant with the
|
|
||||||
// user-defined CORS policies.
|
|
||||||
const acceptHeader = headers.get('accept');
|
|
||||||
if (acceptHeader) {
|
|
||||||
const values = acceptHeader.split(',').map((value) => value.trim());
|
|
||||||
const filteredValues = values.filter((value) => value !== 'msw/passthrough');
|
|
||||||
|
|
||||||
if (filteredValues.length > 0) {
|
|
||||||
headers.set('accept', filteredValues.join(', '));
|
|
||||||
} else {
|
|
||||||
headers.delete('accept');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(requestClone, { headers });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bypass mocking when the client is not active.
|
|
||||||
if (!client) {
|
|
||||||
return passthrough();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bypass initial page load requests (i.e. static assets).
|
|
||||||
// The absence of the immediate/parent client in the map of the active clients
|
|
||||||
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
|
||||||
// and is not ready to handle requests.
|
|
||||||
if (!activeClientIds.has(client.id)) {
|
|
||||||
return passthrough();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Notify the client that a request has been intercepted.
|
|
||||||
const serializedRequest = await serializeRequest(event.request);
|
|
||||||
const clientMessage = await sendToClient(
|
|
||||||
client,
|
|
||||||
{
|
|
||||||
type: 'REQUEST',
|
|
||||||
payload: {
|
|
||||||
id: requestId,
|
|
||||||
interceptedAt: requestInterceptedAt,
|
|
||||||
...serializedRequest,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
[serializedRequest.body],
|
|
||||||
);
|
|
||||||
|
|
||||||
switch (clientMessage.type) {
|
|
||||||
case 'MOCK_RESPONSE': {
|
|
||||||
return respondWithMock(clientMessage.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'PASSTHROUGH': {
|
|
||||||
return passthrough();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return passthrough();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Client} client
|
|
||||||
* @param {any} message
|
|
||||||
* @param {Array<Transferable>} transferrables
|
|
||||||
* @returns {Promise<any>}
|
|
||||||
*/
|
|
||||||
function sendToClient(client, message, transferrables = []) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const channel = new MessageChannel();
|
|
||||||
|
|
||||||
channel.port1.onmessage = (event) => {
|
|
||||||
if (event.data && event.data.error) {
|
|
||||||
return reject(event.data.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(event.data);
|
|
||||||
};
|
|
||||||
|
|
||||||
client.postMessage(message, [channel.port2, ...transferrables.filter(Boolean)]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Response} response
|
|
||||||
* @returns {Response}
|
|
||||||
*/
|
|
||||||
function respondWithMock(response) {
|
|
||||||
// Setting response status code to 0 is a no-op.
|
|
||||||
// However, when responding with a "Response.error()", the produced Response
|
|
||||||
// instance will have status code set to 0. Since it's not possible to create
|
|
||||||
// a Response instance with status code 0, handle that use-case separately.
|
|
||||||
if (response.status === 0) {
|
|
||||||
return Response.error();
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockedResponse = new Response(response.body, response);
|
|
||||||
|
|
||||||
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
|
||||||
value: true,
|
|
||||||
enumerable: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return mockedResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Request} request
|
|
||||||
*/
|
|
||||||
async function serializeRequest(request) {
|
|
||||||
return {
|
|
||||||
url: request.url,
|
|
||||||
mode: request.mode,
|
|
||||||
method: request.method,
|
|
||||||
headers: Object.fromEntries(request.headers.entries()),
|
|
||||||
cache: request.cache,
|
|
||||||
credentials: request.credentials,
|
|
||||||
destination: request.destination,
|
|
||||||
integrity: request.integrity,
|
|
||||||
redirect: request.redirect,
|
|
||||||
referrer: request.referrer,
|
|
||||||
referrerPolicy: request.referrerPolicy,
|
|
||||||
body: await request.arrayBuffer(),
|
|
||||||
keepalive: request.keepalive,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
||||||
import { createMemoryHistory, createRouter, RouterProvider } from '@tanstack/react-router';
|
|
||||||
import type { ReactNode } from 'react';
|
|
||||||
import { render } from 'vitest-browser-react';
|
|
||||||
import { PlatformProvider } from '@/platform/PlatformContext';
|
|
||||||
import { routeTree } from '@/router';
|
|
||||||
import { createMockPlatform, type MockPlatform } from './mockPlatform';
|
|
||||||
|
|
||||||
export function createTestQueryClient(): QueryClient {
|
|
||||||
return new QueryClient({
|
|
||||||
defaultOptions: {
|
|
||||||
// Retries and interval refetching are disabled so tests are
|
|
||||||
// deterministic — polling components get their data exactly once.
|
|
||||||
queries: {
|
|
||||||
retry: false,
|
|
||||||
refetchInterval: false,
|
|
||||||
refetchOnWindowFocus: false,
|
|
||||||
gcTime: Number.POSITIVE_INFINITY,
|
|
||||||
},
|
|
||||||
mutations: { retry: false },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RenderWithProvidersOptions {
|
|
||||||
platform?: MockPlatform;
|
|
||||||
queryClient?: QueryClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every client handed to a render is drained on teardown so in-flight
|
|
||||||
// queries can't fire after MSW handlers reset (noisy unhandled-request
|
|
||||||
// errors between tests).
|
|
||||||
const activeQueryClients: QueryClient[] = [];
|
|
||||||
|
|
||||||
export async function drainQueryClients(): Promise<void> {
|
|
||||||
for (const client of activeQueryClients) {
|
|
||||||
await client.cancelQueries();
|
|
||||||
client.clear();
|
|
||||||
}
|
|
||||||
activeQueryClients.length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function renderWithProviders(ui: ReactNode, options: RenderWithProvidersOptions = {}) {
|
|
||||||
const platform = options.platform ?? createMockPlatform();
|
|
||||||
const queryClient = options.queryClient ?? createTestQueryClient();
|
|
||||||
activeQueryClients.push(queryClient);
|
|
||||||
|
|
||||||
const result = await render(
|
|
||||||
<QueryClientProvider client={queryClient}>
|
|
||||||
<PlatformProvider platform={platform}>{ui}</PlatformProvider>
|
|
||||||
</QueryClientProvider>,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Object.assign keeps the render result's prototype methods (locators)
|
|
||||||
// intact — spreading would drop them.
|
|
||||||
return Object.assign(result, { platform, queryClient });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mount the real route tree at `route` over memory history — full app chrome
|
|
||||||
* (sidebar, frame, toasts) included. A throwaway router per call keeps route
|
|
||||||
* state from leaking between tests.
|
|
||||||
*/
|
|
||||||
export async function renderRoute(route: string, options: RenderWithProvidersOptions = {}) {
|
|
||||||
const router = createRouter({
|
|
||||||
routeTree,
|
|
||||||
history: createMemoryHistory({ initialEntries: [route] }),
|
|
||||||
});
|
|
||||||
const result = await renderWithProviders(<RouterProvider router={router} />, options);
|
|
||||||
return Object.assign(result, { router });
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { queryClient } from '@/lib/queryClient';
|
|
||||||
import { useAudioChannelStore } from '@/stores/audioChannelStore';
|
|
||||||
import { useEffectsStore } from '@/stores/effectsStore';
|
|
||||||
import { useGenerationStore } from '@/stores/generationStore';
|
|
||||||
import { useLogStore } from '@/stores/logStore';
|
|
||||||
import { usePlayerStore } from '@/stores/playerStore';
|
|
||||||
import { useServerStore } from '@/stores/serverStore';
|
|
||||||
import { useStoryStore } from '@/stores/storyStore';
|
|
||||||
import { useUIStore } from '@/stores/uiStore';
|
|
||||||
|
|
||||||
const stores = [
|
|
||||||
useAudioChannelStore,
|
|
||||||
useEffectsStore,
|
|
||||||
useGenerationStore,
|
|
||||||
useLogStore,
|
|
||||||
usePlayerStore,
|
|
||||||
useServerStore,
|
|
||||||
useStoryStore,
|
|
||||||
useUIStore,
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
// Snapshot pristine state at module load, before any test mutates anything.
|
|
||||||
const snapshots = stores.map((store) => store.getState());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Restore every zustand store to its initial state and clear persisted
|
|
||||||
* copies so tests can't leak state into each other. Persisted stores write
|
|
||||||
* through to localStorage on setState, so localStorage is cleared last.
|
|
||||||
*/
|
|
||||||
export function resetAllStores(): void {
|
|
||||||
stores.forEach((store, i) => {
|
|
||||||
store.setState(snapshots[i] as never, true);
|
|
||||||
});
|
|
||||||
queryClient.clear();
|
|
||||||
localStorage.clear();
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { afterEach, beforeAll } from 'vitest';
|
|
||||||
import { cleanup } from 'vitest-browser-react';
|
|
||||||
import { worker } from './msw/worker';
|
|
||||||
import { drainQueryClients } from './render';
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
await worker.start({ onUnhandledRequest: 'error', quiet: true });
|
|
||||||
return () => worker.stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Registered after setup.ts, so this runs first (afterEach is LIFO):
|
|
||||||
// unmount → cancel in-flight queries → reset handlers, then setup.ts
|
|
||||||
// restores stores and mocks.
|
|
||||||
afterEach(async () => {
|
|
||||||
await cleanup();
|
|
||||||
await drainQueryClients();
|
|
||||||
worker.resetHandlers();
|
|
||||||
});
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import '@/i18n';
|
|
||||||
import { afterEach, vi } from 'vitest';
|
|
||||||
import { resetAllStores } from './resetStores';
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
vi.restoreAllMocks();
|
|
||||||
resetAllStores();
|
|
||||||
});
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
import { HttpResponse } from 'msw';
|
|
||||||
|
|
||||||
export interface SseEvent {
|
|
||||||
data: unknown;
|
|
||||||
event?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SSE_HEADERS = {
|
|
||||||
'Content-Type': 'text/event-stream',
|
|
||||||
'Cache-Control': 'no-cache',
|
|
||||||
Connection: 'keep-alive',
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
|
||||||
|
|
||||||
function frame({ data, event }: SseEvent): Uint8Array {
|
|
||||||
const payload = typeof data === 'string' ? data : JSON.stringify(data);
|
|
||||||
const lines = event ? `event: ${event}\ndata: ${payload}\n\n` : `data: ${payload}\n\n`;
|
|
||||||
return encoder.encode(lines);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An MSW response streaming the given events immediately, then staying open
|
|
||||||
* (EventSource reconnects on close, so a closed stream would loop the test).
|
|
||||||
*/
|
|
||||||
export function sseResponse(events: SseEvent[]): Response {
|
|
||||||
const stream = new ReadableStream<Uint8Array>({
|
|
||||||
start(controller) {
|
|
||||||
for (const event of events) controller.enqueue(frame(event));
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return new HttpResponse(stream, { headers: SSE_HEADERS });
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SseController {
|
|
||||||
/** Hand this to an MSW resolver: `http.get(url, () => sse.response())`. */
|
|
||||||
response(): Response;
|
|
||||||
/** Push one event to every open stream. */
|
|
||||||
push(event: SseEvent): void;
|
|
||||||
/** End all open streams. */
|
|
||||||
close(): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Imperative SSE feed for tests that interleave user actions with server
|
|
||||||
* events (generation progress, download progress). Each call to `response()`
|
|
||||||
* opens a stream that receives subsequent `push`es — matching EventSource
|
|
||||||
* reconnect behavior.
|
|
||||||
*/
|
|
||||||
export function sseController(): SseController {
|
|
||||||
const controllers = new Set<ReadableStreamDefaultController<Uint8Array>>();
|
|
||||||
|
|
||||||
return {
|
|
||||||
response() {
|
|
||||||
let own: ReadableStreamDefaultController<Uint8Array>;
|
|
||||||
const stream = new ReadableStream<Uint8Array>({
|
|
||||||
start(controller) {
|
|
||||||
own = controller;
|
|
||||||
controllers.add(controller);
|
|
||||||
},
|
|
||||||
cancel() {
|
|
||||||
controllers.delete(own);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return new HttpResponse(stream, { headers: SSE_HEADERS });
|
|
||||||
},
|
|
||||||
push(event: SseEvent) {
|
|
||||||
for (const controller of controllers) controller.enqueue(frame(event));
|
|
||||||
},
|
|
||||||
close() {
|
|
||||||
for (const controller of controllers) {
|
|
||||||
try {
|
|
||||||
controller.close();
|
|
||||||
} catch {
|
|
||||||
// already closed by cancel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
controllers.clear();
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import path from 'node:path';
|
||||||
|
import tailwindcss from '@tailwindcss/vite';
|
||||||
|
import react from '@vitejs/plugin-react';
|
||||||
|
import { defineConfig } from 'vite';
|
||||||
|
import { changelogPlugin } from './plugins/changelog';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [tailwindcss(), react(), changelogPlugin(path.resolve(__dirname, '..'))],
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@': path.resolve(__dirname, './src'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
+3
-3
@@ -360,15 +360,15 @@ async def _run_shutdown() -> None:
|
|||||||
"""Unload models on lifespan exit."""
|
"""Unload models on lifespan exit."""
|
||||||
logger.info("Voicebox server shutting down...")
|
logger.info("Voicebox server shutting down...")
|
||||||
try:
|
try:
|
||||||
await tts.unload_tts_model()
|
tts.unload_tts_model()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to unload TTS model")
|
logger.exception("Failed to unload TTS model")
|
||||||
try:
|
try:
|
||||||
await transcribe.unload_whisper_model()
|
transcribe.unload_whisper_model()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to unload Whisper model")
|
logger.exception("Failed to unload Whisper model")
|
||||||
try:
|
try:
|
||||||
await llm.unload_llm_model()
|
llm.unload_llm_model()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to unload LLM model")
|
logger.exception("Failed to unload LLM model")
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ and a model config registry that eliminates per-engine dispatch maps.
|
|||||||
# HF_HUB_OFFLINE=1 and on network failures.
|
# HF_HUB_OFFLINE=1 and on network failures.
|
||||||
from ..utils import hf_offline_patch # noqa: F401
|
from ..utils import hf_offline_patch # noqa: F401
|
||||||
|
|
||||||
import os
|
|
||||||
import threading
|
import threading
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Protocol, Optional, Tuple, List
|
from typing import Protocol, Optional, Tuple, List
|
||||||
@@ -22,6 +21,15 @@ import numpy as np
|
|||||||
DEFAULT_LLM_MAX_TOKENS = 512
|
DEFAULT_LLM_MAX_TOKENS = 512
|
||||||
DEFAULT_LLM_TEMPERATURE = 0.7
|
DEFAULT_LLM_TEMPERATURE = 0.7
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class TranscriptionResult:
|
||||||
|
"""Text and language metadata returned by an STT backend."""
|
||||||
|
|
||||||
|
text: str
|
||||||
|
language: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
from ..utils.platform_detect import get_backend_type
|
from ..utils.platform_detect import get_backend_type
|
||||||
|
|
||||||
LANGUAGE_CODE_TO_NAME = {
|
LANGUAGE_CODE_TO_NAME = {
|
||||||
@@ -155,6 +163,15 @@ class STTBackend(Protocol):
|
|||||||
"""
|
"""
|
||||||
...
|
...
|
||||||
|
|
||||||
|
async def transcribe_with_metadata(
|
||||||
|
self,
|
||||||
|
audio_path: str,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
model_size: Optional[str] = None,
|
||||||
|
) -> TranscriptionResult:
|
||||||
|
"""Transcribe audio and return text with the resolved language."""
|
||||||
|
...
|
||||||
|
|
||||||
def unload_model(self) -> None:
|
def unload_model(self) -> None:
|
||||||
"""Unload model to free memory."""
|
"""Unload model to free memory."""
|
||||||
...
|
...
|
||||||
@@ -164,6 +181,26 @@ class STTBackend(Protocol):
|
|||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
async def transcribe_with_metadata(
|
||||||
|
backend: STTBackend,
|
||||||
|
audio_path: str,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
model_size: Optional[str] = None,
|
||||||
|
) -> TranscriptionResult:
|
||||||
|
"""Use STT metadata when available while retaining legacy backends."""
|
||||||
|
metadata_method = getattr(backend, "transcribe_with_metadata", None)
|
||||||
|
if callable(metadata_method):
|
||||||
|
result = await metadata_method(audio_path, language, model_size)
|
||||||
|
if isinstance(result, TranscriptionResult):
|
||||||
|
return result
|
||||||
|
if isinstance(result, str):
|
||||||
|
return TranscriptionResult(text=result.strip(), language=language)
|
||||||
|
raise TypeError("STT metadata method returned an unsupported result")
|
||||||
|
|
||||||
|
text = await backend.transcribe(audio_path, language, model_size)
|
||||||
|
return TranscriptionResult(text=text.strip(), language=language)
|
||||||
|
|
||||||
|
|
||||||
@runtime_checkable
|
@runtime_checkable
|
||||||
class LLMBackend(Protocol):
|
class LLMBackend(Protocol):
|
||||||
"""Protocol for local LLM (chat/completion) backend implementations."""
|
"""Protocol for local LLM (chat/completion) backend implementations."""
|
||||||
@@ -548,21 +585,7 @@ async def ensure_model_cached_or_raise(engine: str, model_size: str = "default")
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def unload_backend(backend) -> None:
|
def unload_model_by_config(config: ModelConfig) -> bool:
|
||||||
"""Free a backend's model, serialized onto the MLX worker when it has one.
|
|
||||||
|
|
||||||
MLX backends expose an async ``unload`` that runs the free on the dedicated
|
|
||||||
MLX thread so it can't collide with an in-flight load/generate. Other
|
|
||||||
backends only carry the synchronous ``unload_model``.
|
|
||||||
"""
|
|
||||||
unload = getattr(backend, "unload", None)
|
|
||||||
if unload is not None:
|
|
||||||
await unload()
|
|
||||||
else:
|
|
||||||
backend.unload_model()
|
|
||||||
|
|
||||||
|
|
||||||
async def unload_model_by_config(config: ModelConfig) -> bool:
|
|
||||||
"""Unload a model given its config. Returns True if it was loaded, False otherwise."""
|
"""Unload a model given its config. Returns True if it was loaded, False otherwise."""
|
||||||
from . import get_tts_backend_for_engine
|
from . import get_tts_backend_for_engine
|
||||||
from ..services import tts, transcribe, llm as llm_service
|
from ..services import tts, transcribe, llm as llm_service
|
||||||
@@ -570,7 +593,7 @@ async def unload_model_by_config(config: ModelConfig) -> bool:
|
|||||||
if config.engine == "whisper":
|
if config.engine == "whisper":
|
||||||
whisper_model = transcribe.get_whisper_model()
|
whisper_model = transcribe.get_whisper_model()
|
||||||
if whisper_model.is_loaded() and whisper_model.model_size == config.model_size:
|
if whisper_model.is_loaded() and whisper_model.model_size == config.model_size:
|
||||||
await unload_backend(whisper_model)
|
transcribe.unload_whisper_model()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -578,7 +601,7 @@ async def unload_model_by_config(config: ModelConfig) -> bool:
|
|||||||
backend = llm_service.get_llm_model()
|
backend = llm_service.get_llm_model()
|
||||||
loaded_size = getattr(backend, "_current_model_size", None) or getattr(backend, "model_size", None)
|
loaded_size = getattr(backend, "_current_model_size", None) or getattr(backend, "model_size", None)
|
||||||
if backend.is_loaded() and loaded_size == config.model_size:
|
if backend.is_loaded() and loaded_size == config.model_size:
|
||||||
await unload_backend(backend)
|
backend.unload_model()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -586,7 +609,7 @@ async def unload_model_by_config(config: ModelConfig) -> bool:
|
|||||||
tts_model = tts.get_tts_model()
|
tts_model = tts.get_tts_model()
|
||||||
loaded_size = getattr(tts_model, "_current_model_size", None) or getattr(tts_model, "model_size", None)
|
loaded_size = getattr(tts_model, "_current_model_size", None) or getattr(tts_model, "model_size", None)
|
||||||
if tts_model.is_loaded() and loaded_size == config.model_size:
|
if tts_model.is_loaded() and loaded_size == config.model_size:
|
||||||
await unload_backend(tts_model)
|
tts.unload_tts_model()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -594,14 +617,14 @@ async def unload_model_by_config(config: ModelConfig) -> bool:
|
|||||||
backend = get_tts_backend_for_engine(config.engine)
|
backend = get_tts_backend_for_engine(config.engine)
|
||||||
loaded_size = getattr(backend, "_current_model_size", None) or getattr(backend, "model_size", None)
|
loaded_size = getattr(backend, "_current_model_size", None) or getattr(backend, "model_size", None)
|
||||||
if backend.is_loaded() and loaded_size == config.model_size:
|
if backend.is_loaded() and loaded_size == config.model_size:
|
||||||
await unload_backend(backend)
|
backend.unload_model()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# All other TTS engines
|
# All other TTS engines
|
||||||
backend = get_tts_backend_for_engine(config.engine)
|
backend = get_tts_backend_for_engine(config.engine)
|
||||||
if backend.is_loaded():
|
if backend.is_loaded():
|
||||||
await unload_backend(backend)
|
backend.unload_model()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -679,13 +702,6 @@ def get_tts_backend_for_engine(engine: str) -> TTSBackend:
|
|||||||
"""
|
"""
|
||||||
global _tts_backends
|
global _tts_backends
|
||||||
|
|
||||||
# Test mode: every engine resolves to the fake backend so the full
|
|
||||||
# generation pipeline runs without model weights (see fake_backend.py).
|
|
||||||
if os.environ.get("VOICEBOX_FAKE_TTS") == "1":
|
|
||||||
from .fake_backend import get_fake_backend
|
|
||||||
|
|
||||||
return get_fake_backend()
|
|
||||||
|
|
||||||
# Fast path: check without lock
|
# Fast path: check without lock
|
||||||
if engine in _tts_backends:
|
if engine in _tts_backends:
|
||||||
return _tts_backends[engine]
|
return _tts_backends[engine]
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
"""Fake TTS backend for UI and E2E testing.
|
|
||||||
|
|
||||||
Activated by ``VOICEBOX_FAKE_TTS=1``. Every engine resolves to this backend,
|
|
||||||
which synthesizes a quiet sine tone sized to the input text — so the full
|
|
||||||
generation pipeline (task queue, SSE progress, database rows, audio serving)
|
|
||||||
runs exactly as in production, minus model weights and GPU time.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
from typing import ClassVar, Optional
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
SAMPLE_RATE = 24_000
|
|
||||||
SECONDS_PER_CHAR = 0.02
|
|
||||||
MIN_DURATION_S = 0.25
|
|
||||||
TONE_HZ = 440.0
|
|
||||||
AMPLITUDE = 0.1
|
|
||||||
|
|
||||||
|
|
||||||
class FakeTTSBackend:
|
|
||||||
"""Implements the TTSBackend protocol without any model."""
|
|
||||||
|
|
||||||
MODEL_CONFIGS: ClassVar[list] = []
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self._loaded = False
|
|
||||||
|
|
||||||
async def load_model(self, model_size: str = "default") -> None:
|
|
||||||
if self._loaded:
|
|
||||||
return
|
|
||||||
# Brief pause so the UI's loading_model state is observable.
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
self._loaded = True
|
|
||||||
logger.info("Fake TTS backend loaded (VOICEBOX_FAKE_TTS)")
|
|
||||||
|
|
||||||
async def load_model_async(self, model_size: str = "default") -> None:
|
|
||||||
# Qwen engines are loaded through this variant (see load_engine_model).
|
|
||||||
await self.load_model(model_size)
|
|
||||||
|
|
||||||
async def create_voice_prompt(
|
|
||||||
self,
|
|
||||||
audio_path: str,
|
|
||||||
reference_text: str,
|
|
||||||
use_cache: bool = True,
|
|
||||||
) -> tuple[dict, bool]:
|
|
||||||
return ({"fake": True, "audio_path": audio_path, "reference_text": reference_text}, False)
|
|
||||||
|
|
||||||
async def combine_voice_prompts(
|
|
||||||
self,
|
|
||||||
audio_paths: list[str],
|
|
||||||
reference_texts: list[str],
|
|
||||||
) -> tuple[np.ndarray, str]:
|
|
||||||
combined_text = " ".join(reference_texts)
|
|
||||||
return np.zeros(SAMPLE_RATE, dtype=np.float32), combined_text
|
|
||||||
|
|
||||||
async def generate(
|
|
||||||
self,
|
|
||||||
text: str,
|
|
||||||
voice_prompt: dict,
|
|
||||||
language: str = "en",
|
|
||||||
seed: Optional[int] = None,
|
|
||||||
instruct: Optional[str] = None,
|
|
||||||
) -> tuple[np.ndarray, int]:
|
|
||||||
duration_s = max(MIN_DURATION_S, len(text) * SECONDS_PER_CHAR)
|
|
||||||
# Yield once so cancellation has a window, mirroring real inference.
|
|
||||||
await asyncio.sleep(0.05)
|
|
||||||
t = np.linspace(0.0, duration_s, int(SAMPLE_RATE * duration_s), endpoint=False)
|
|
||||||
audio = (AMPLITUDE * np.sin(2.0 * np.pi * TONE_HZ * t)).astype(np.float32)
|
|
||||||
return audio, SAMPLE_RATE
|
|
||||||
|
|
||||||
def unload_model(self) -> None:
|
|
||||||
self._loaded = False
|
|
||||||
|
|
||||||
def is_loaded(self) -> bool:
|
|
||||||
return self._loaded
|
|
||||||
|
|
||||||
def _get_model_path(self, model_size: str) -> str:
|
|
||||||
return "fake"
|
|
||||||
|
|
||||||
|
|
||||||
_fake_backend: Optional[FakeTTSBackend] = None
|
|
||||||
|
|
||||||
|
|
||||||
def get_fake_backend() -> FakeTTSBackend:
|
|
||||||
global _fake_backend
|
|
||||||
if _fake_backend is None:
|
|
||||||
_fake_backend = FakeTTSBackend()
|
|
||||||
return _fake_backend
|
|
||||||
@@ -3,6 +3,7 @@ MLX backend implementation for TTS and STT using mlx-audio.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Optional, List, Tuple
|
from typing import Optional, List, Tuple
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -16,9 +17,14 @@ from ..utils.hf_offline_patch import patch_huggingface_hub_offline, ensure_origi
|
|||||||
patch_huggingface_hub_offline()
|
patch_huggingface_hub_offline()
|
||||||
ensure_original_qwen_config_cached()
|
ensure_original_qwen_config_cached()
|
||||||
|
|
||||||
from . import TTSBackend, STTBackend, LANGUAGE_CODE_TO_NAME, WHISPER_HF_REPOS
|
from . import (
|
||||||
|
LANGUAGE_CODE_TO_NAME,
|
||||||
|
STTBackend,
|
||||||
|
TTSBackend,
|
||||||
|
TranscriptionResult,
|
||||||
|
WHISPER_HF_REPOS,
|
||||||
|
)
|
||||||
from .base import is_model_cached, combine_voice_prompts as _combine_voice_prompts, model_load_progress
|
from .base import is_model_cached, combine_voice_prompts as _combine_voice_prompts, model_load_progress
|
||||||
from ..services.mlx_thread import run_on_mlx_thread, clear_mlx_cache
|
|
||||||
from ..utils.cache import get_cache_key, get_cached_voice_prompt, cache_voice_prompt
|
from ..utils.cache import get_cache_key, get_cached_voice_prompt, cache_voice_prompt
|
||||||
|
|
||||||
|
|
||||||
@@ -63,22 +69,6 @@ class MLXTTSBackend:
|
|||||||
weight_extensions=(".safetensors", ".bin", ".npz"),
|
weight_extensions=(".safetensors", ".bin", ".npz"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _ensure_loaded_sync(self, model_size: Optional[str]):
|
|
||||||
"""Load the model if the requested size isn't already resident.
|
|
||||||
|
|
||||||
Runs on the MLX worker thread so it stays serialized with generation.
|
|
||||||
"""
|
|
||||||
if model_size is None:
|
|
||||||
model_size = self.model_size
|
|
||||||
|
|
||||||
if self.model is not None and self._current_model_size == model_size:
|
|
||||||
return
|
|
||||||
|
|
||||||
if self.model is not None and self._current_model_size != model_size:
|
|
||||||
self.unload_model()
|
|
||||||
|
|
||||||
self._load_model_sync(model_size)
|
|
||||||
|
|
||||||
async def load_model_async(self, model_size: Optional[str] = None):
|
async def load_model_async(self, model_size: Optional[str] = None):
|
||||||
"""
|
"""
|
||||||
Lazy load the MLX TTS model.
|
Lazy load the MLX TTS model.
|
||||||
@@ -86,15 +76,23 @@ class MLXTTSBackend:
|
|||||||
Args:
|
Args:
|
||||||
model_size: Model size to load (1.7B or 0.6B)
|
model_size: Model size to load (1.7B or 0.6B)
|
||||||
"""
|
"""
|
||||||
await run_on_mlx_thread(self._ensure_loaded_sync, model_size)
|
if model_size is None:
|
||||||
|
model_size = self.model_size
|
||||||
|
|
||||||
|
# If already loaded with correct size, return
|
||||||
|
if self.model is not None and self._current_model_size == model_size:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Unload existing model if different size requested
|
||||||
|
if self.model is not None and self._current_model_size != model_size:
|
||||||
|
self.unload_model()
|
||||||
|
|
||||||
|
# Run blocking load in thread pool
|
||||||
|
await asyncio.to_thread(self._load_model_sync, model_size)
|
||||||
|
|
||||||
# Alias for compatibility
|
# Alias for compatibility
|
||||||
load_model = load_model_async
|
load_model = load_model_async
|
||||||
|
|
||||||
async def unload(self):
|
|
||||||
"""Free the model, serialized onto the MLX worker thread."""
|
|
||||||
await run_on_mlx_thread(self.unload_model)
|
|
||||||
|
|
||||||
def _load_model_sync(self, model_size: str):
|
def _load_model_sync(self, model_size: str):
|
||||||
"""Synchronous model loading."""
|
"""Synchronous model loading."""
|
||||||
model_path = self._get_model_path(model_size)
|
model_path = self._get_model_path(model_size)
|
||||||
@@ -118,7 +116,6 @@ class MLXTTSBackend:
|
|||||||
del self.model
|
del self.model
|
||||||
self.model = None
|
self.model = None
|
||||||
self._current_model_size = None
|
self._current_model_size = None
|
||||||
clear_mlx_cache()
|
|
||||||
logger.info("MLX TTS model unloaded")
|
logger.info("MLX TTS model unloaded")
|
||||||
|
|
||||||
async def create_voice_prompt(
|
async def create_voice_prompt(
|
||||||
@@ -196,6 +193,8 @@ class MLXTTSBackend:
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple of (audio_array, sample_rate)
|
Tuple of (audio_array, sample_rate)
|
||||||
"""
|
"""
|
||||||
|
await self.load_model_async(None)
|
||||||
|
|
||||||
logger.info("Generating audio for text: %s", text)
|
logger.info("Generating audio for text: %s", text)
|
||||||
|
|
||||||
def _generate_sync():
|
def _generate_sync():
|
||||||
@@ -265,13 +264,8 @@ class MLXTTSBackend:
|
|||||||
|
|
||||||
return audio, sample_rate
|
return audio, sample_rate
|
||||||
|
|
||||||
# Load-if-needed and inference run as one job on the MLX worker so a
|
# Run blocking inference in thread pool
|
||||||
# concurrent unload or different-size load can't land between them.
|
audio, sample_rate = await asyncio.to_thread(_generate_sync)
|
||||||
def _load_and_generate():
|
|
||||||
self._ensure_loaded_sync(None)
|
|
||||||
return _generate_sync()
|
|
||||||
|
|
||||||
audio, sample_rate = await run_on_mlx_thread(_load_and_generate)
|
|
||||||
|
|
||||||
return audio, sample_rate
|
return audio, sample_rate
|
||||||
|
|
||||||
@@ -291,19 +285,6 @@ class MLXSTTBackend:
|
|||||||
hf_repo = WHISPER_HF_REPOS.get(model_size, f"openai/whisper-{model_size}")
|
hf_repo = WHISPER_HF_REPOS.get(model_size, f"openai/whisper-{model_size}")
|
||||||
return is_model_cached(hf_repo, weight_extensions=(".safetensors", ".bin", ".npz"))
|
return is_model_cached(hf_repo, weight_extensions=(".safetensors", ".bin", ".npz"))
|
||||||
|
|
||||||
def _ensure_loaded_sync(self, model_size: Optional[str]):
|
|
||||||
"""Load the model if the requested size isn't already resident.
|
|
||||||
|
|
||||||
Runs on the MLX worker thread so it stays serialized with transcription.
|
|
||||||
"""
|
|
||||||
if model_size is None:
|
|
||||||
model_size = self.model_size
|
|
||||||
|
|
||||||
if self.model is not None and self.model_size == model_size:
|
|
||||||
return
|
|
||||||
|
|
||||||
self._load_model_sync(model_size)
|
|
||||||
|
|
||||||
async def load_model_async(self, model_size: Optional[str] = None):
|
async def load_model_async(self, model_size: Optional[str] = None):
|
||||||
"""
|
"""
|
||||||
Lazy load the MLX Whisper model.
|
Lazy load the MLX Whisper model.
|
||||||
@@ -311,15 +292,18 @@ class MLXSTTBackend:
|
|||||||
Args:
|
Args:
|
||||||
model_size: Model size (tiny, base, small, medium, large)
|
model_size: Model size (tiny, base, small, medium, large)
|
||||||
"""
|
"""
|
||||||
await run_on_mlx_thread(self._ensure_loaded_sync, model_size)
|
if model_size is None:
|
||||||
|
model_size = self.model_size
|
||||||
|
|
||||||
|
if self.model is not None and self.model_size == model_size:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Run blocking load in thread pool
|
||||||
|
await asyncio.to_thread(self._load_model_sync, model_size)
|
||||||
|
|
||||||
# Alias for compatibility
|
# Alias for compatibility
|
||||||
load_model = load_model_async
|
load_model = load_model_async
|
||||||
|
|
||||||
async def unload(self):
|
|
||||||
"""Free the model, serialized onto the MLX worker thread."""
|
|
||||||
await run_on_mlx_thread(self.unload_model)
|
|
||||||
|
|
||||||
def _load_model_sync(self, model_size: str):
|
def _load_model_sync(self, model_size: str):
|
||||||
"""Synchronous model loading."""
|
"""Synchronous model loading."""
|
||||||
progress_model_name = f"whisper-{model_size}"
|
progress_model_name = f"whisper-{model_size}"
|
||||||
@@ -341,7 +325,6 @@ class MLXSTTBackend:
|
|||||||
if self.model is not None:
|
if self.model is not None:
|
||||||
del self.model
|
del self.model
|
||||||
self.model = None
|
self.model = None
|
||||||
clear_mlx_cache()
|
|
||||||
logger.info("MLX Whisper model unloaded")
|
logger.info("MLX Whisper model unloaded")
|
||||||
|
|
||||||
async def transcribe(
|
async def transcribe(
|
||||||
@@ -350,6 +333,15 @@ class MLXSTTBackend:
|
|||||||
language: Optional[str] = None,
|
language: Optional[str] = None,
|
||||||
model_size: Optional[str] = None,
|
model_size: Optional[str] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
result = await self.transcribe_with_metadata(audio_path, language, model_size)
|
||||||
|
return result.text
|
||||||
|
|
||||||
|
async def transcribe_with_metadata(
|
||||||
|
self,
|
||||||
|
audio_path: str,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
model_size: Optional[str] = None,
|
||||||
|
) -> TranscriptionResult:
|
||||||
"""
|
"""
|
||||||
Transcribe audio to text.
|
Transcribe audio to text.
|
||||||
|
|
||||||
@@ -359,8 +351,10 @@ class MLXSTTBackend:
|
|||||||
model_size: Optional model size override
|
model_size: Optional model size override
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Transcribed text
|
Transcribed text and resolved language
|
||||||
"""
|
"""
|
||||||
|
await self.load_model_async(model_size)
|
||||||
|
|
||||||
def _transcribe_sync():
|
def _transcribe_sync():
|
||||||
"""Run synchronous transcription in thread pool."""
|
"""Run synchronous transcription in thread pool."""
|
||||||
# MLX Whisper transcription using generate method
|
# MLX Whisper transcription using generate method
|
||||||
@@ -374,20 +368,26 @@ class MLXSTTBackend:
|
|||||||
# regression this revert fixes (issue #462).
|
# regression this revert fixes (issue #462).
|
||||||
result = self.model.generate(str(audio_path), **decode_options)
|
result = self.model.generate(str(audio_path), **decode_options)
|
||||||
|
|
||||||
# Extract text from result
|
# mlx-audio's Whisper output carries the detected language when
|
||||||
|
# auto-detection is used. Preserve it instead of collapsing the
|
||||||
|
# result to a bare string.
|
||||||
if isinstance(result, str):
|
if isinstance(result, str):
|
||||||
return result.strip()
|
text = result
|
||||||
|
detected_language = language
|
||||||
elif isinstance(result, dict):
|
elif isinstance(result, dict):
|
||||||
return result.get("text", "").strip()
|
text = result.get("text", "")
|
||||||
|
detected_language = result.get("language") or language
|
||||||
elif hasattr(result, "text"):
|
elif hasattr(result, "text"):
|
||||||
return result.text.strip()
|
text = result.text
|
||||||
|
detected_language = getattr(result, "language", None) or language
|
||||||
else:
|
else:
|
||||||
return str(result).strip()
|
text = str(result)
|
||||||
|
detected_language = language
|
||||||
|
|
||||||
# Load-if-needed and transcription run as one job on the MLX worker so
|
return TranscriptionResult(
|
||||||
# a concurrent unload or load can't land between them.
|
text=text.strip(),
|
||||||
def _load_and_transcribe():
|
language=detected_language,
|
||||||
self._ensure_loaded_sync(model_size)
|
)
|
||||||
return _transcribe_sync()
|
|
||||||
|
|
||||||
return await run_on_mlx_thread(_load_and_transcribe)
|
# Run blocking transcription in thread pool
|
||||||
|
return await asyncio.to_thread(_transcribe_sync)
|
||||||
|
|||||||
@@ -10,7 +10,13 @@ import numpy as np
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
from . import TTSBackend, STTBackend, LANGUAGE_CODE_TO_NAME, WHISPER_HF_REPOS
|
from . import (
|
||||||
|
LANGUAGE_CODE_TO_NAME,
|
||||||
|
STTBackend,
|
||||||
|
TTSBackend,
|
||||||
|
TranscriptionResult,
|
||||||
|
WHISPER_HF_REPOS,
|
||||||
|
)
|
||||||
from .base import (
|
from .base import (
|
||||||
is_model_cached,
|
is_model_cached,
|
||||||
get_torch_device,
|
get_torch_device,
|
||||||
@@ -23,6 +29,14 @@ from ..utils.cache import get_cache_key, get_cached_voice_prompt, cache_voice_pr
|
|||||||
from ..utils.audio import load_audio
|
from ..utils.audio import load_audio
|
||||||
|
|
||||||
|
|
||||||
|
def whisper_language_code_from_token_id(generation_config, token_id: int) -> Optional[str]:
|
||||||
|
"""Resolve a Whisper language token ID to its canonical language code."""
|
||||||
|
for token, candidate_id in getattr(generation_config, "lang_to_id", {}).items():
|
||||||
|
if candidate_id == token_id and token.startswith("<|") and token.endswith("|>"):
|
||||||
|
return token[2:-2]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class PyTorchTTSBackend:
|
class PyTorchTTSBackend:
|
||||||
"""PyTorch-based TTS backend using Qwen3-TTS."""
|
"""PyTorch-based TTS backend using Qwen3-TTS."""
|
||||||
|
|
||||||
@@ -320,6 +334,15 @@ class PyTorchSTTBackend:
|
|||||||
language: Optional[str] = None,
|
language: Optional[str] = None,
|
||||||
model_size: Optional[str] = None,
|
model_size: Optional[str] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
result = await self.transcribe_with_metadata(audio_path, language, model_size)
|
||||||
|
return result.text
|
||||||
|
|
||||||
|
async def transcribe_with_metadata(
|
||||||
|
self,
|
||||||
|
audio_path: str,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
model_size: Optional[str] = None,
|
||||||
|
) -> TranscriptionResult:
|
||||||
"""
|
"""
|
||||||
Transcribe audio to text.
|
Transcribe audio to text.
|
||||||
|
|
||||||
@@ -329,7 +352,7 @@ class PyTorchSTTBackend:
|
|||||||
model_size: Optional model size override
|
model_size: Optional model size override
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Transcribed text
|
Transcribed text and resolved language
|
||||||
"""
|
"""
|
||||||
await self.load_model_async(model_size)
|
await self.load_model_async(model_size)
|
||||||
|
|
||||||
@@ -350,9 +373,23 @@ class PyTorchSTTBackend:
|
|||||||
)
|
)
|
||||||
inputs = inputs.to(self.device)
|
inputs = inputs.to(self.device)
|
||||||
|
|
||||||
# Generate transcription
|
# Resolve the language before generation so auto-detection can be
|
||||||
# If language is provided, force it; otherwise let Whisper auto-detect
|
# persisted alongside the transcript instead of being discarded.
|
||||||
|
resolved_language = language
|
||||||
|
if resolved_language is None:
|
||||||
|
language_token = self.model.detect_language(
|
||||||
|
input_features=inputs["input_features"],
|
||||||
|
generation_config=self.model.generation_config,
|
||||||
|
)[0].item()
|
||||||
|
resolved_language = whisper_language_code_from_token_id(
|
||||||
|
self.model.generation_config,
|
||||||
|
language_token,
|
||||||
|
)
|
||||||
|
|
||||||
generate_kwargs = {}
|
generate_kwargs = {}
|
||||||
|
# Preserve Whisper's existing auto-detection behavior during
|
||||||
|
# generation. The separately detected code above is metadata only;
|
||||||
|
# force a decoder language solely when the caller requested one.
|
||||||
if language:
|
if language:
|
||||||
forced_decoder_ids = self.processor.get_decoder_prompt_ids(
|
forced_decoder_ids = self.processor.get_decoder_prompt_ids(
|
||||||
language=language,
|
language=language,
|
||||||
@@ -372,7 +409,10 @@ class PyTorchSTTBackend:
|
|||||||
skip_special_tokens=True,
|
skip_special_tokens=True,
|
||||||
)[0]
|
)[0]
|
||||||
|
|
||||||
return transcription.strip()
|
return TranscriptionResult(
|
||||||
|
text=transcription.strip(),
|
||||||
|
language=resolved_language,
|
||||||
|
)
|
||||||
|
|
||||||
# Run blocking transcription in thread pool
|
# Run blocking transcription in thread pool
|
||||||
return await asyncio.to_thread(_transcribe_sync)
|
return await asyncio.to_thread(_transcribe_sync)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ from .base import (
|
|||||||
manual_seed,
|
manual_seed,
|
||||||
model_load_progress,
|
model_load_progress,
|
||||||
)
|
)
|
||||||
from ..services.mlx_thread import run_on_mlx_thread, clear_mlx_cache
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -206,11 +205,7 @@ class MLXQwenLLMBackend:
|
|||||||
weight_extensions=(".safetensors", ".bin", ".npz"),
|
weight_extensions=(".safetensors", ".bin", ".npz"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _ensure_loaded_sync(self, model_size: Optional[str]) -> None:
|
async def load_model(self, model_size: Optional[str] = None) -> None:
|
||||||
"""Load the model if the requested size isn't already resident.
|
|
||||||
|
|
||||||
Runs on the MLX worker thread so it stays serialized with generation.
|
|
||||||
"""
|
|
||||||
if model_size is None:
|
if model_size is None:
|
||||||
model_size = self.model_size
|
model_size = self.model_size
|
||||||
|
|
||||||
@@ -220,14 +215,7 @@ class MLXQwenLLMBackend:
|
|||||||
if self.model is not None and self._current_model_size != model_size:
|
if self.model is not None and self._current_model_size != model_size:
|
||||||
self.unload_model()
|
self.unload_model()
|
||||||
|
|
||||||
self._load_model_sync(model_size)
|
await asyncio.to_thread(self._load_model_sync, model_size)
|
||||||
|
|
||||||
async def load_model(self, model_size: Optional[str] = None) -> None:
|
|
||||||
await run_on_mlx_thread(self._ensure_loaded_sync, model_size)
|
|
||||||
|
|
||||||
async def unload(self) -> None:
|
|
||||||
"""Free the model, serialized onto the MLX worker thread."""
|
|
||||||
await run_on_mlx_thread(self.unload_model)
|
|
||||||
|
|
||||||
def _load_model_sync(self, model_size: str) -> None:
|
def _load_model_sync(self, model_size: str) -> None:
|
||||||
from mlx_lm import load as mlx_load
|
from mlx_lm import load as mlx_load
|
||||||
@@ -258,7 +246,6 @@ class MLXQwenLLMBackend:
|
|||||||
self.model = None
|
self.model = None
|
||||||
self.tokenizer = None
|
self.tokenizer = None
|
||||||
self._current_model_size = None
|
self._current_model_size = None
|
||||||
clear_mlx_cache()
|
|
||||||
logger.info("Qwen3 (MLX) unloaded")
|
logger.info("Qwen3 (MLX) unloaded")
|
||||||
|
|
||||||
async def generate(
|
async def generate(
|
||||||
@@ -270,13 +257,10 @@ class MLXQwenLLMBackend:
|
|||||||
model_size: Optional[str] = None,
|
model_size: Optional[str] = None,
|
||||||
examples: Optional[list[tuple[str, str]]] = None,
|
examples: Optional[list[tuple[str, str]]] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
# Load-if-needed and inference run as one job on the MLX worker so a
|
await self.load_model(model_size)
|
||||||
# concurrent unload or different-size load can't land between them.
|
return await asyncio.to_thread(
|
||||||
def _load_and_generate() -> str:
|
self._generate_sync, prompt, system, max_tokens, temperature, examples
|
||||||
self._ensure_loaded_sync(model_size)
|
)
|
||||||
return self._generate_sync(prompt, system, max_tokens, temperature, examples)
|
|
||||||
|
|
||||||
return await run_on_mlx_thread(_load_and_generate)
|
|
||||||
|
|
||||||
def _generate_sync(
|
def _generate_sync(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -243,13 +243,6 @@ def _migrate_capture_settings(engine, inspector, tables: set[str]) -> None:
|
|||||||
"hotkey_enabled BOOLEAN NOT NULL DEFAULT 0",
|
"hotkey_enabled BOOLEAN NOT NULL DEFAULT 0",
|
||||||
"hotkey_enabled",
|
"hotkey_enabled",
|
||||||
)
|
)
|
||||||
if "keep_mic_warm" not in columns:
|
|
||||||
_add_column(
|
|
||||||
engine,
|
|
||||||
"capture_settings",
|
|
||||||
"keep_mic_warm BOOLEAN NOT NULL DEFAULT 0",
|
|
||||||
"keep_mic_warm",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _migrate_mcp_bindings(engine, inspector, tables: set[str]) -> None:
|
def _migrate_mcp_bindings(engine, inspector, tables: set[str]) -> None:
|
||||||
|
|||||||
@@ -210,10 +210,6 @@ class CaptureSettings(Base):
|
|||||||
# "Voicebox would like to receive keystrokes from any application" dialog
|
# "Voicebox would like to receive keystrokes from any application" dialog
|
||||||
# before they've even opened the Captures tab.
|
# before they've even opened the Captures tab.
|
||||||
hotkey_enabled = Column(Boolean, nullable=False, default=False)
|
hotkey_enabled = Column(Boolean, nullable=False, default=False)
|
||||||
# Hold the microphone open while dictation is enabled so push-to-talk
|
|
||||||
# doesn't clip the first words. Off by default — when on, the OS mic-in-use
|
|
||||||
# indicator stays lit the whole time dictation is enabled.
|
|
||||||
keep_mic_warm = Column(Boolean, nullable=False, default=False)
|
|
||||||
# Lists of keytap key names (e.g. "MetaRight", "ControlRight"). Right-hand
|
# Lists of keytap key names (e.g. "MetaRight", "ControlRight"). Right-hand
|
||||||
# modifiers by default so they don't collide with left-hand shortcuts.
|
# modifiers by default so they don't collide with left-hand shortcuts.
|
||||||
chord_push_to_talk_keys = Column(
|
chord_push_to_talk_keys = Column(
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
"""Canonical language handling for Voicebox captures."""
|
||||||
|
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
# Canonical OpenAI Whisper language codes. The capture UI intentionally offers
|
||||||
|
# a smaller curated subset, but API validation must not break existing captures
|
||||||
|
# or persisted settings that use the rest of Whisper's supported languages.
|
||||||
|
CAPTURE_LANGUAGE_CODES: Final[tuple[str, ...]] = (
|
||||||
|
"af",
|
||||||
|
"am",
|
||||||
|
"ar",
|
||||||
|
"as",
|
||||||
|
"az",
|
||||||
|
"ba",
|
||||||
|
"be",
|
||||||
|
"bg",
|
||||||
|
"bn",
|
||||||
|
"bo",
|
||||||
|
"br",
|
||||||
|
"bs",
|
||||||
|
"ca",
|
||||||
|
"cs",
|
||||||
|
"cy",
|
||||||
|
"da",
|
||||||
|
"de",
|
||||||
|
"el",
|
||||||
|
"en",
|
||||||
|
"es",
|
||||||
|
"et",
|
||||||
|
"eu",
|
||||||
|
"fa",
|
||||||
|
"fi",
|
||||||
|
"fo",
|
||||||
|
"fr",
|
||||||
|
"gl",
|
||||||
|
"gu",
|
||||||
|
"ha",
|
||||||
|
"haw",
|
||||||
|
"he",
|
||||||
|
"hi",
|
||||||
|
"hr",
|
||||||
|
"ht",
|
||||||
|
"hu",
|
||||||
|
"hy",
|
||||||
|
"id",
|
||||||
|
"is",
|
||||||
|
"it",
|
||||||
|
"ja",
|
||||||
|
"jw",
|
||||||
|
"ka",
|
||||||
|
"kk",
|
||||||
|
"km",
|
||||||
|
"kn",
|
||||||
|
"ko",
|
||||||
|
"la",
|
||||||
|
"lb",
|
||||||
|
"ln",
|
||||||
|
"lo",
|
||||||
|
"lt",
|
||||||
|
"lv",
|
||||||
|
"mg",
|
||||||
|
"mi",
|
||||||
|
"mk",
|
||||||
|
"ml",
|
||||||
|
"mn",
|
||||||
|
"mr",
|
||||||
|
"ms",
|
||||||
|
"mt",
|
||||||
|
"my",
|
||||||
|
"ne",
|
||||||
|
"nl",
|
||||||
|
"nn",
|
||||||
|
"no",
|
||||||
|
"oc",
|
||||||
|
"pa",
|
||||||
|
"pl",
|
||||||
|
"ps",
|
||||||
|
"pt",
|
||||||
|
"ro",
|
||||||
|
"ru",
|
||||||
|
"sa",
|
||||||
|
"sd",
|
||||||
|
"si",
|
||||||
|
"sk",
|
||||||
|
"sl",
|
||||||
|
"sn",
|
||||||
|
"so",
|
||||||
|
"sq",
|
||||||
|
"sr",
|
||||||
|
"su",
|
||||||
|
"sv",
|
||||||
|
"sw",
|
||||||
|
"ta",
|
||||||
|
"te",
|
||||||
|
"tg",
|
||||||
|
"th",
|
||||||
|
"tk",
|
||||||
|
"tl",
|
||||||
|
"tr",
|
||||||
|
"tt",
|
||||||
|
"uk",
|
||||||
|
"ur",
|
||||||
|
"uz",
|
||||||
|
"vi",
|
||||||
|
"yi",
|
||||||
|
"yo",
|
||||||
|
"yue",
|
||||||
|
"zh",
|
||||||
|
)
|
||||||
|
_CAPTURE_LANGUAGE_SET = frozenset(CAPTURE_LANGUAGE_CODES)
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_capture_language(language: str | None) -> str | None:
|
||||||
|
"""Normalize a capture language, treating ``auto`` as auto-detection.
|
||||||
|
|
||||||
|
Only languages exposed by the capture UI are accepted. This keeps raw API
|
||||||
|
input out of Whisper decoder hints and refinement instructions.
|
||||||
|
"""
|
||||||
|
if language is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
normalized = language.strip().lower()
|
||||||
|
if normalized == "auto":
|
||||||
|
return None
|
||||||
|
if normalized not in _CAPTURE_LANGUAGE_SET:
|
||||||
|
supported = ", ".join(("auto", *CAPTURE_LANGUAGE_CODES))
|
||||||
|
raise ValueError(f"Unsupported capture language '{language}'. Expected one of: {supported}")
|
||||||
|
return normalized
|
||||||
@@ -284,11 +284,13 @@ def _speak_response(
|
|||||||
async def _transcribe_file(
|
async def _transcribe_file(
|
||||||
path: Path, language: str | None, model: str | None
|
path: Path, language: str | None, model: str | None
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
from ..backends import WHISPER_HF_REPOS
|
from ..backends import WHISPER_HF_REPOS, transcribe_with_metadata
|
||||||
|
from ..languages import normalize_capture_language
|
||||||
from ..services import transcribe as transcribe_service
|
from ..services import transcribe as transcribe_service
|
||||||
from ..utils.audio import load_audio
|
from ..utils.audio import load_audio
|
||||||
|
|
||||||
whisper = transcribe_service.get_whisper_model()
|
whisper = transcribe_service.get_whisper_model()
|
||||||
|
language = normalize_capture_language(language)
|
||||||
model_size = model or whisper.model_size
|
model_size = model or whisper.model_size
|
||||||
valid = list(WHISPER_HF_REPOS.keys())
|
valid = list(WHISPER_HF_REPOS.keys())
|
||||||
if model_size not in valid:
|
if model_size not in valid:
|
||||||
@@ -308,10 +310,12 @@ async def _transcribe_file(
|
|||||||
"Voicebox → Settings → Models to download it first."
|
"Voicebox → Settings → Models to download it first."
|
||||||
)
|
)
|
||||||
|
|
||||||
text = await whisper.transcribe(str(path), language, model_size)
|
transcription = await transcribe_with_metadata(
|
||||||
|
whisper, str(path), language, model_size
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
"text": text,
|
"text": transcription.text,
|
||||||
"duration": duration,
|
"duration": duration,
|
||||||
"language": language,
|
"language": transcription.language,
|
||||||
"model": model_size,
|
"model": model_size,
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-4
@@ -2,7 +2,7 @@
|
|||||||
Pydantic models for request/response validation.
|
Pydantic models for request/response validation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field, field_validator
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
@@ -10,6 +10,15 @@ from .utils.capture_chords import (
|
|||||||
default_push_to_talk_chord,
|
default_push_to_talk_chord,
|
||||||
default_toggle_to_talk_chord,
|
default_toggle_to_talk_chord,
|
||||||
)
|
)
|
||||||
|
from .languages import normalize_capture_language
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_capture_language_setting(language: str | None) -> str | None:
|
||||||
|
"""Canonicalize requests while preserving the public ``auto`` sentinel."""
|
||||||
|
if language is None:
|
||||||
|
return None
|
||||||
|
normalized = normalize_capture_language(language)
|
||||||
|
return "auto" if normalized is None else normalized
|
||||||
|
|
||||||
|
|
||||||
class VoiceProfileCreate(BaseModel):
|
class VoiceProfileCreate(BaseModel):
|
||||||
@@ -180,6 +189,7 @@ class TranscriptionResponse(BaseModel):
|
|||||||
|
|
||||||
text: str
|
text: str
|
||||||
duration: float
|
duration: float
|
||||||
|
language: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class RefinementFlagsModel(BaseModel):
|
class RefinementFlagsModel(BaseModel):
|
||||||
@@ -242,7 +252,12 @@ class CaptureRetranscribeRequest(BaseModel):
|
|||||||
"""Request to re-run STT on a capture's audio with a different model."""
|
"""Request to re-run STT on a capture's audio with a different model."""
|
||||||
|
|
||||||
model: Optional[str] = Field(None, pattern="^(base|small|medium|large|turbo)$")
|
model: Optional[str] = Field(None, pattern="^(base|small|medium|large|turbo)$")
|
||||||
language: Optional[str] = Field(None, pattern="^(en|zh|ja|ko|de|fr|ru|pt|es|it)$")
|
language: Optional[str] = None
|
||||||
|
|
||||||
|
@field_validator("language")
|
||||||
|
@classmethod
|
||||||
|
def validate_language(cls, value: str | None) -> str | None:
|
||||||
|
return _validate_capture_language_setting(value)
|
||||||
|
|
||||||
|
|
||||||
class CaptureSettingsResponse(BaseModel):
|
class CaptureSettingsResponse(BaseModel):
|
||||||
@@ -258,7 +273,6 @@ class CaptureSettingsResponse(BaseModel):
|
|||||||
allow_auto_paste: bool = True
|
allow_auto_paste: bool = True
|
||||||
default_playback_voice_id: Optional[str] = None
|
default_playback_voice_id: Optional[str] = None
|
||||||
hotkey_enabled: bool = False
|
hotkey_enabled: bool = False
|
||||||
keep_mic_warm: bool = False
|
|
||||||
chord_push_to_talk_keys: List[str] = Field(
|
chord_push_to_talk_keys: List[str] = Field(
|
||||||
default_factory=default_push_to_talk_chord
|
default_factory=default_push_to_talk_chord
|
||||||
)
|
)
|
||||||
@@ -283,10 +297,14 @@ class CaptureSettingsUpdate(BaseModel):
|
|||||||
allow_auto_paste: Optional[bool] = None
|
allow_auto_paste: Optional[bool] = None
|
||||||
default_playback_voice_id: Optional[str] = None
|
default_playback_voice_id: Optional[str] = None
|
||||||
hotkey_enabled: Optional[bool] = None
|
hotkey_enabled: Optional[bool] = None
|
||||||
keep_mic_warm: Optional[bool] = None
|
|
||||||
chord_push_to_talk_keys: Optional[List[str]] = Field(default=None, min_length=1, max_length=6)
|
chord_push_to_talk_keys: Optional[List[str]] = Field(default=None, min_length=1, max_length=6)
|
||||||
chord_toggle_to_talk_keys: Optional[List[str]] = Field(default=None, min_length=1, max_length=6)
|
chord_toggle_to_talk_keys: Optional[List[str]] = Field(default=None, min_length=1, max_length=6)
|
||||||
|
|
||||||
|
@field_validator("language")
|
||||||
|
@classmethod
|
||||||
|
def validate_language(cls, value: str | None) -> str | None:
|
||||||
|
return _validate_capture_language_setting(value)
|
||||||
|
|
||||||
|
|
||||||
class GenerationSettingsResponse(BaseModel):
|
class GenerationSettingsResponse(BaseModel):
|
||||||
"""Server-persisted defaults for the generation flow."""
|
"""Server-persisted defaults for the generation flow."""
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
# Minimal dependency set to boot the backend on a CPU-only CI runner.
|
|
||||||
# No TTS/STT model libraries — inference is covered by the fake TTS
|
|
||||||
# backend (VOICEBOX_FAKE_TTS=1). Install CPU torch first on Linux:
|
|
||||||
# pip install torch --index-url https://download.pytorch.org/whl/cpu
|
|
||||||
# then: pip install -r backend/requirements-ci.txt
|
|
||||||
|
|
||||||
fastapi>=0.109.0
|
|
||||||
uvicorn[standard]>=0.27.0
|
|
||||||
pydantic>=2.5.0
|
|
||||||
sqlalchemy>=2.0.0
|
|
||||||
alembic>=1.13.0
|
|
||||||
torch>=2.2.0
|
|
||||||
huggingface_hub>=0.20.0
|
|
||||||
numpy
|
|
||||||
soundfile
|
|
||||||
python-multipart
|
|
||||||
sse-starlette
|
|
||||||
psutil
|
|
||||||
requests
|
|
||||||
httpx
|
|
||||||
fastmcp
|
|
||||||
librosa
|
|
||||||
pillow
|
|
||||||
pydub
|
|
||||||
pedalboard
|
|
||||||
@@ -222,6 +222,8 @@ async def retranscribe_capture_endpoint(
|
|||||||
)
|
)
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
raise HTTPException(status_code=410, detail=str(e))
|
raise HTTPException(status_code=410, detail=str(e))
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Retranscribe failed for capture %s", capture_id)
|
logger.exception("Retranscribe failed for capture %s", capture_id)
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ async def unload_model():
|
|||||||
from ..services import tts
|
from ..services import tts
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await tts.unload_tts_model()
|
tts.unload_tts_model()
|
||||||
return {"message": "Model unloaded successfully"}
|
return {"message": "Model unloaded successfully"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
@@ -82,7 +82,7 @@ async def unload_model_by_name(model_name: str):
|
|||||||
raise HTTPException(status_code=400, detail=f"Unknown model: {model_name}")
|
raise HTTPException(status_code=400, detail=f"Unknown model: {model_name}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
was_loaded = await unload_model_by_config(config)
|
was_loaded = unload_model_by_config(config)
|
||||||
if not was_loaded:
|
if not was_loaded:
|
||||||
return {"message": f"Model {model_name} is not loaded"}
|
return {"message": f"Model {model_name} is not loaded"}
|
||||||
return {"message": f"Model {model_name} unloaded successfully"}
|
return {"message": f"Model {model_name} unloaded successfully"}
|
||||||
@@ -457,7 +457,7 @@ async def delete_model(model_name: str):
|
|||||||
hf_repo_id = config.hf_repo_id
|
hf_repo_id = config.hf_repo_id
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await unload_model_by_config(config)
|
unload_model_by_config(config)
|
||||||
|
|
||||||
cache_dir = hf_constants.HF_HUB_CACHE
|
cache_dir = hf_constants.HF_HUB_CACHE
|
||||||
repo_cache_dir = Path(cache_dir) / ("models--" + hf_repo_id.replace("/", "--"))
|
repo_cache_dir = Path(cache_dir) / ("models--" + hf_repo_id.replace("/", "--"))
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ from pathlib import Path
|
|||||||
from fastapi import APIRouter, File, Form, HTTPException, UploadFile
|
from fastapi import APIRouter, File, Form, HTTPException, UploadFile
|
||||||
|
|
||||||
from .. import models
|
from .. import models
|
||||||
|
from ..backends import transcribe_with_metadata
|
||||||
|
from ..languages import normalize_capture_language
|
||||||
from ..services import transcribe
|
from ..services import transcribe
|
||||||
from ..services.task_queue import create_background_task
|
from ..services.task_queue import create_background_task
|
||||||
from ..utils.tasks import get_task_manager
|
from ..utils.tasks import get_task_manager
|
||||||
@@ -39,6 +41,7 @@ async def transcribe_audio(
|
|||||||
from ..utils.audio import load_audio
|
from ..utils.audio import load_audio
|
||||||
from ..backends import WHISPER_HF_REPOS
|
from ..backends import WHISPER_HF_REPOS
|
||||||
|
|
||||||
|
language = normalize_capture_language(language)
|
||||||
audio, sr = await asyncio.to_thread(load_audio, tmp_path)
|
audio, sr = await asyncio.to_thread(load_audio, tmp_path)
|
||||||
duration = len(audio) / sr
|
duration = len(audio) / sr
|
||||||
|
|
||||||
@@ -76,15 +79,20 @@ async def transcribe_audio(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
text = await whisper_model.transcribe(tmp_path, language, model_size)
|
transcription = await transcribe_with_metadata(
|
||||||
|
whisper_model, tmp_path, language, model_size
|
||||||
|
)
|
||||||
|
|
||||||
return models.TranscriptionResponse(
|
return models.TranscriptionResponse(
|
||||||
text=text,
|
text=transcription.text,
|
||||||
duration=duration,
|
duration=duration,
|
||||||
|
language=transcription.language,
|
||||||
)
|
)
|
||||||
|
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ import soundfile as sf
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from .. import config
|
from .. import config
|
||||||
|
from ..backends import transcribe_with_metadata
|
||||||
from ..database import Capture as DBCapture
|
from ..database import Capture as DBCapture
|
||||||
|
from ..languages import normalize_capture_language
|
||||||
from ..models import CaptureResponse, RefinementFlagsModel
|
from ..models import CaptureResponse, RefinementFlagsModel
|
||||||
from ..utils.audio import load_audio
|
from ..utils.audio import load_audio
|
||||||
from .refinement import RefinementFlags, refine_transcript
|
from .refinement import RefinementFlags, refine_transcript
|
||||||
@@ -67,6 +69,7 @@ async def create_capture(
|
|||||||
db: Session,
|
db: Session,
|
||||||
) -> CaptureResponse:
|
) -> CaptureResponse:
|
||||||
"""Persist raw audio, run STT, store the row."""
|
"""Persist raw audio, run STT, store the row."""
|
||||||
|
language = normalize_capture_language(language)
|
||||||
if source not in VALID_SOURCES:
|
if source not in VALID_SOURCES:
|
||||||
raise ValueError(f"Invalid source '{source}'. Must be one of {sorted(VALID_SOURCES)}")
|
raise ValueError(f"Invalid source '{source}'. Must be one of {sorted(VALID_SOURCES)}")
|
||||||
|
|
||||||
@@ -119,15 +122,17 @@ async def create_capture(
|
|||||||
|
|
||||||
whisper = get_whisper_model()
|
whisper = get_whisper_model()
|
||||||
resolved_stt = stt_model or whisper.model_size
|
resolved_stt = stt_model or whisper.model_size
|
||||||
transcript = await whisper.transcribe(str(audio_path), language, resolved_stt)
|
transcription = await transcribe_with_metadata(
|
||||||
|
whisper, str(audio_path), language, resolved_stt
|
||||||
|
)
|
||||||
|
|
||||||
row = DBCapture(
|
row = DBCapture(
|
||||||
id=capture_id,
|
id=capture_id,
|
||||||
audio_path=config.to_storage_path(audio_path),
|
audio_path=config.to_storage_path(audio_path),
|
||||||
source=source,
|
source=source,
|
||||||
language=language,
|
language=transcription.language,
|
||||||
duration_ms=duration_ms,
|
duration_ms=duration_ms,
|
||||||
transcript_raw=transcript,
|
transcript_raw=transcription.text,
|
||||||
stt_model=resolved_stt,
|
stt_model=resolved_stt,
|
||||||
)
|
)
|
||||||
db.add(row)
|
db.add(row)
|
||||||
@@ -195,6 +200,7 @@ async def refine_capture(
|
|||||||
row.transcript_raw or "",
|
row.transcript_raw or "",
|
||||||
flags,
|
flags,
|
||||||
model_size=model_size,
|
model_size=model_size,
|
||||||
|
language=row.language,
|
||||||
)
|
)
|
||||||
|
|
||||||
row.transcript_refined = refined
|
row.transcript_refined = refined
|
||||||
@@ -211,6 +217,7 @@ async def retranscribe_capture(
|
|||||||
language: Optional[str],
|
language: Optional[str],
|
||||||
db: Session,
|
db: Session,
|
||||||
) -> Optional[CaptureResponse]:
|
) -> Optional[CaptureResponse]:
|
||||||
|
language = normalize_capture_language(language)
|
||||||
row = db.query(DBCapture).filter(DBCapture.id == capture_id).first()
|
row = db.query(DBCapture).filter(DBCapture.id == capture_id).first()
|
||||||
if not row:
|
if not row:
|
||||||
return None
|
return None
|
||||||
@@ -221,12 +228,13 @@ async def retranscribe_capture(
|
|||||||
|
|
||||||
whisper = get_whisper_model()
|
whisper = get_whisper_model()
|
||||||
resolved_stt = stt_model or whisper.model_size
|
resolved_stt = stt_model or whisper.model_size
|
||||||
transcript = await whisper.transcribe(str(resolved), language, resolved_stt)
|
transcription = await transcribe_with_metadata(
|
||||||
|
whisper, str(resolved), language, resolved_stt
|
||||||
|
)
|
||||||
|
|
||||||
row.transcript_raw = transcript
|
row.transcript_raw = transcription.text
|
||||||
row.stt_model = resolved_stt
|
row.stt_model = resolved_stt
|
||||||
if language:
|
row.language = transcription.language
|
||||||
row.language = language
|
|
||||||
# Refined text is stale after a fresh STT pass — force a re-refine.
|
# Refined text is stale after a fresh STT pass — force a re-refine.
|
||||||
row.transcript_refined = None
|
row.transcript_refined = None
|
||||||
row.llm_model = None
|
row.llm_model = None
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
LLM inference module - delegates to backend abstraction layer.
|
LLM inference module - delegates to backend abstraction layer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ..backends import LLMBackend, get_llm_backend, unload_backend
|
from ..backends import get_llm_backend, LLMBackend
|
||||||
|
|
||||||
|
|
||||||
def get_llm_model() -> LLMBackend:
|
def get_llm_model() -> LLMBackend:
|
||||||
@@ -10,6 +10,6 @@ def get_llm_model() -> LLMBackend:
|
|||||||
return get_llm_backend()
|
return get_llm_backend()
|
||||||
|
|
||||||
|
|
||||||
async def unload_llm_model() -> None:
|
def unload_llm_model() -> None:
|
||||||
"""Unload LLM model to free memory, serialized onto the MLX worker."""
|
"""Unload LLM model to free memory."""
|
||||||
await unload_backend(get_llm_backend())
|
get_llm_backend().unload_model()
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user