Merge pull request #416 from chriswritescode-dev/fix/tts-overlap-and-cache-race

fix(tts): prevent overlapping playback and corrupted audio cache
This commit is contained in:
Chris Scott authored and GitHub committed 2026-10-11 00:19:15 -04:00
commit 51e92d8cf2
9 files changed
+1145 -167

No files matched your search

+156 -54
View File
@@ -2,11 +2,11 @@ import { Hono } from 'hono'
import { z } from 'zod'
import { Database } from 'bun:sqlite'
import { createHash } from 'crypto'
import { readFile, writeFile, readdir, stat, unlink } from 'fs/promises'
import { readFile, readdir, stat, unlink } from 'fs/promises'
import { join } from 'path'
import { SettingsService } from '../services/settings'
import { logger } from '../utils/logger'
import { mkdirSafe } from '../utils/fs-safe'
import { mkdirSafe, writeFileAtomic } from '../utils/fs-safe'
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
import {
normalizeToBaseUrl,
@@ -18,14 +18,31 @@ const TTS_CACHE_DIR = join(getWorkspacePath(), 'cache', 'tts')
const CACHE_TTL_MS = 24 * 60 * 60 * 1000
const MAX_CACHE_SIZE_MB = 200
const MAX_CACHE_SIZE_BYTES = MAX_CACHE_SIZE_MB * 1024 * 1024
const TTS_SYNTHESIS_TIMEOUT_MS = 60_000
const TTSRequestSchema = z.object({
text: z.string().min(1).max(4096),
})
function generateCacheKey(text: string, voice: string, model: string, speed: number): string {
interface SynthesisParams {
text: string
endpoint: string
apiKey: string
voice: string
model: string
speed: number
}
function generateCacheKey(params: SynthesisParams): string {
const hash = createHash('sha256')
hash.update(`${text}|${voice}|${model}|${speed}`)
hash.update(JSON.stringify([
params.text,
params.voice,
params.model,
params.speed,
normalizeToBaseUrl(params.endpoint),
params.apiKey,
]))
return hash.digest('hex')
}
@@ -107,7 +124,91 @@ async function cacheAudio(cacheKey: string, audioData: Buffer): Promise<void> {
await cleanupOldestFiles(audioData.length)
}
await writeFile(filePath, audioData)
await writeFileAtomic(filePath, audioData)
}
class TTSUpstreamError extends Error {
readonly status: 400 | 500
readonly details: string
constructor(status: 400 | 500, details: string) {
super('TTS API request failed')
this.name = 'TTSUpstreamError'
this.status = status
this.details = details
}
}
const inFlightSynthesis = new Map<string, Promise<Buffer>>()
function parseErrorDetails(errorText: string): string {
try {
const errorJson = JSON.parse(errorText)
if (errorJson.detail?.error?.message) return errorJson.detail.error.message
if (errorJson.detail?.message) return errorJson.detail.message
if (errorJson.message) return errorJson.message
} catch {
return errorText
}
return errorText
}
async function fetchSynthesizedAudio(params: SynthesisParams): Promise<Buffer> {
const { text, endpoint, apiKey, voice, model, speed } = params
const baseUrl = normalizeToBaseUrl(endpoint)
const speechEndpoint = `${baseUrl}/v1/audio/speech`
try {
const response = await fetch(speechEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model,
voice,
input: text,
speed,
response_format: 'mp3',
}),
signal: AbortSignal.timeout(TTS_SYNTHESIS_TIMEOUT_MS),
})
if (!response.ok) {
const errorText = await response.text()
logger.error(`TTS API error: ${response.status} - ${errorText}`)
const status = response.status >= 400 && response.status < 600 ? response.status as 400 | 500 : 500
throw new TTSUpstreamError(status, parseErrorDetails(errorText))
}
return Buffer.from(await response.arrayBuffer())
} catch (error) {
if (error instanceof Error && error.name === 'TimeoutError') {
throw new TTSUpstreamError(500, 'TTS upstream request timed out')
}
throw error
}
}
function getOrStartSynthesis(cacheKey: string, params: SynthesisParams): Promise<Buffer> {
const existing = inFlightSynthesis.get(cacheKey)
if (existing) return existing
const promise = (async () => {
const audioBuffer = await fetchSynthesizedAudio(params)
try {
await cacheAudio(cacheKey, audioBuffer)
} catch (error) {
logger.error('TTS cache write failed:', error)
}
return audioBuffer
})()
inFlightSynthesis.set(cacheKey, promise)
const cleanup = () => inFlightSynthesis.delete(cacheKey)
promise.finally(cleanup).catch(() => undefined)
return promise
}
@@ -220,7 +321,32 @@ export async function getCacheStats(): Promise<{ count: number; sizeBytes: numbe
}
}
export { generateCacheKey, ensureCacheDir, getCachedAudio, cacheAudio, getCacheSize, cleanupOldestFiles }
async function clearCache(): Promise<number> {
let files: string[]
try {
files = await readdir(TTS_CACHE_DIR)
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return 0
throw error
}
let cleared = 0
for (const file of files) {
if (!file.endsWith('.mp3')) continue
try {
await unlink(join(TTS_CACHE_DIR, file))
cleared++
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue
throw error
}
}
return cleared
}
export { generateCacheKey, ensureCacheDir, getCachedAudio, cacheAudio, getCacheSize, cleanupOldestFiles, clearCache }
export function createTTSRoutes(db: Database) {
const app = new Hono()
@@ -251,7 +377,8 @@ export function createTTSRoutes(db: Database) {
}
const { endpoint, apiKey, voice, model, speed } = ttsConfig
const cacheKey = generateCacheKey(text, voice, model, speed)
const synthesisParams: SynthesisParams = { text, endpoint, apiKey, voice, model, speed }
const cacheKey = generateCacheKey(synthesisParams)
await ensureCacheDir()
@@ -272,56 +399,21 @@ export function createTTSRoutes(db: Database) {
logger.info(`TTS cache miss, calling API: ${cacheKey.substring(0, 8)}...`)
const baseUrl = normalizeToBaseUrl(endpoint)
const speechEndpoint = `${baseUrl}/v1/audio/speech`
const response = await fetch(speechEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model,
voice,
input: text,
speed,
response_format: 'mp3',
}),
signal: abortController.signal,
})
if (!response.ok) {
const errorText = await response.text()
logger.error(`TTS API error: ${response.status} - ${errorText}`)
const status = response.status >= 400 && response.status < 600 ? response.status as 400 | 500 : 500
// Try to parse error details for better frontend display
let errorDetails = errorText
try {
const errorJson = JSON.parse(errorText)
if (errorJson.detail?.error?.message) {
errorDetails = errorJson.detail.error.message
} else if (errorJson.detail?.message) {
errorDetails = errorJson.detail.message
} else if (errorJson.message) {
errorDetails = errorJson.message
}
} catch {
// Use raw error text if parsing fails
let audioBuffer: Buffer
try {
audioBuffer = await getOrStartSynthesis(cacheKey, synthesisParams)
} catch (error) {
if (error instanceof TTSUpstreamError) {
return c.json({
error: 'TTS API request failed',
details: error.details,
voice: voice,
availableVoices: ttsConfig?.availableVoices || []
}, error.status)
}
return c.json({
error: 'TTS API request failed',
details: errorDetails,
voice: voice,
availableVoices: ttsConfig?.availableVoices || []
}, status)
throw error
}
const audioBuffer = Buffer.from(await response.arrayBuffer())
await cacheAudio(cacheKey, audioBuffer)
logger.info(`TTS audio cached: ${cacheKey.substring(0, 8)}...`)
return new Response(audioBuffer, {
@@ -437,5 +529,15 @@ export function createTTSRoutes(db: Database) {
})
})
app.delete('/cache', async (c) => {
try {
const cleared = await clearCache()
return c.json({ cleared })
} catch (error) {
logger.error('TTS cache clear failed:', error)
return c.json({ error: 'Failed to clear TTS cache' }, 500)
}
})
return app
}
+1 -1
View File
@@ -26,7 +26,7 @@ export function canonicalPathSync(target: string): string {
}
}
export async function writeFileAtomic(filePath: string, content: string, options: { mode?: number } = {}): Promise<void> {
export async function writeFileAtomic(filePath: string, content: string | Buffer, options: { mode?: number } = {}): Promise<void> {
const dir = path.dirname(filePath)
await mkdirSafe(dir)
const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`)
+318 -22
View File
@@ -3,15 +3,19 @@ import * as fs from 'fs/promises'
vi.mock('fs/promises', () => ({
readFile: vi.fn(),
writeFile: vi.fn(),
readdir: vi.fn(),
stat: vi.fn(),
unlink: vi.fn(),
}))
const { mockWriteFileAtomic } = vi.hoisted(() => ({
mockWriteFileAtomic: vi.fn(),
}))
vi.mock('../../src/utils/fs-safe', () => ({
mkdirSafe: vi.fn().mockResolvedValue(undefined),
mkdirSyncSafe: vi.fn(),
writeFileAtomic: mockWriteFileAtomic,
}))
vi.mock('bun:sqlite', () => ({
@@ -54,9 +58,7 @@ const mockReaddir = fs.readdir as any
const mockStat = fs.stat as any
const mockUnlink = fs.unlink as any
import { createTTSRoutes, cleanupExpiredCache, getCacheStats, generateCacheKey, ensureCacheDir, getCachedAudio, getCacheSize, cleanupOldestFiles, cacheAudio } from '../../src/routes/tts'
const mockWriteFile = fs.writeFile as any
import { createTTSRoutes, cleanupExpiredCache, getCacheStats, generateCacheKey, ensureCacheDir, getCachedAudio, getCacheSize, cleanupOldestFiles, cacheAudio, clearCache } from '../../src/routes/tts'
function createTtsConfig(overrides: Record<string, unknown> = {}) {
return {
@@ -89,27 +91,112 @@ describe('TTS Routes', () => {
})
describe('generateCacheKey', () => {
const params = {
text: 'Hello world',
endpoint: 'https://tts.example.com',
apiKey: 'test-api-key',
voice: 'alloy',
model: 'tts-1',
speed: 1.0,
}
it('should generate consistent cache keys for identical inputs', () => {
const text = 'Hello world'
const voice = 'alloy'
const model = 'tts-1'
const speed = 1.0
const key1 = generateCacheKey(text, voice, model, speed)
const key2 = generateCacheKey(text, voice, model, speed)
const key1 = generateCacheKey(params)
const key2 = generateCacheKey({ ...params })
expect(key1).toBe(key2)
expect(key1).toMatch(/^[a-f0-9]{64}$/)
})
it('should generate different cache keys for different inputs', () => {
const key1 = generateCacheKey('Hello', 'alloy', 'tts-1', 1.0)
const key2 = generateCacheKey('World', 'alloy', 'tts-1', 1.0)
it('should generate different cache keys for different text', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, text: 'Other' }))
})
it('should generate different cache keys for different voices', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, voice: 'echo' }))
})
it('should generate different cache keys for different models', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, model: 'tts-1-hd' }))
})
it('should generate different cache keys for different speeds', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, speed: 1.5 }))
})
it('should generate different cache keys for different api keys', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, apiKey: 'other-api-key' }))
})
it('should generate different cache keys for different endpoints', () => {
expect(generateCacheKey(params)).not.toBe(generateCacheKey({ ...params, endpoint: 'https://other.example.com' }))
})
it('should normalize endpoints so trailing slashes share a key', () => {
expect(generateCacheKey(params)).toBe(generateCacheKey({ ...params, endpoint: 'https://tts.example.com/' }))
})
it('should avoid delimiter ambiguity between concatenated fields', () => {
const key1 = generateCacheKey({ ...params, text: 'a', voice: 'b|c' })
const key2 = generateCacheKey({ ...params, text: 'a|b', voice: 'c' })
expect(key1).not.toBe(key2)
})
})
describe('clearCache', () => {
it('should delete every .mp3 file and count them', async () => {
mockReaddir.mockResolvedValue(['a.mp3', 'b.mp3'] as any)
mockUnlink.mockResolvedValue(undefined)
const cleared = await clearCache()
expect(cleared).toBe(2)
expect(mockUnlink).toHaveBeenCalledTimes(2)
expect(mockUnlink).toHaveBeenCalledWith(expect.stringContaining('a.mp3'))
expect(mockUnlink).toHaveBeenCalledWith(expect.stringContaining('b.mp3'))
})
it('should tolerate a missing cache directory', async () => {
mockReaddir.mockRejectedValue(Object.assign(new Error('missing'), { code: 'ENOENT' }))
const cleared = await clearCache()
expect(cleared).toBe(0)
expect(mockUnlink).not.toHaveBeenCalled()
})
it('should tolerate a file removed by a race and keep clearing the rest', async () => {
mockReaddir.mockResolvedValue(['gone.mp3', 'present.mp3'] as any)
mockUnlink
.mockRejectedValueOnce(Object.assign(new Error('gone'), { code: 'ENOENT' }))
.mockResolvedValueOnce(undefined)
const cleared = await clearCache()
expect(cleared).toBe(1)
expect(mockUnlink).toHaveBeenCalledTimes(2)
})
it('should throw unexpected filesystem errors', async () => {
mockReaddir.mockResolvedValue(['a.mp3'] as any)
mockUnlink.mockRejectedValue(Object.assign(new Error('denied'), { code: 'EACCES' }))
await expect(clearCache()).rejects.toThrow('denied')
})
it('should leave .tmp, atomic temp and unrelated files untouched', async () => {
mockReaddir.mockResolvedValue(['a.mp3', 'pending.tmp', '.a.mp3.ocm-tmp-1-2', 'notes.txt'] as any)
mockUnlink.mockResolvedValue(undefined)
const cleared = await clearCache()
expect(cleared).toBe(1)
expect(mockUnlink).toHaveBeenCalledTimes(1)
expect(mockUnlink).toHaveBeenCalledWith(expect.stringContaining('a.mp3'))
})
})
describe('ensureCacheDir', () => {
it('should create cache directory when it does not exist', async () => {
await expect(ensureCacheDir()).resolves.toBeUndefined()
@@ -316,6 +403,8 @@ describe('TTS route handlers', () => {
it('synthesizes and caches audio on a cache miss', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockResolvedValue(undefined)
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
arrayBuffer: async () => Buffer.from('fresh-audio'),
@@ -336,14 +425,152 @@ describe('TTS route handlers', () => {
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ Authorization: 'Bearer test-api-key' }),
signal: expect.any(AbortSignal),
}),
)
expect(mockWriteFile).toHaveBeenCalledWith(
expect.stringContaining('.mp3'),
expect(mockWriteFileAtomic).toHaveBeenCalledWith(
expect.stringMatching(/\.mp3$/),
expect.any(Buffer),
)
})
it('returns synthesized audio when the cache write fails', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockRejectedValue(new Error('disk full'))
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
arrayBuffer: async () => Buffer.from('fresh-audio'),
})
vi.stubGlobal('fetch', fetchMock)
const res = await app.request('/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello' }),
})
expect(res.status).toBe(200)
expect(res.headers.get('X-Cache')).toBe('MISS')
expect(Buffer.from(await res.arrayBuffer()).toString()).toBe('fresh-audio')
})
it('deduplicates concurrent synthesis requests for the same text', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockResolvedValue(undefined)
let resolveFetch: ((value: { ok: boolean; arrayBuffer: () => Promise<Buffer> }) => void) | undefined
const fetchMock = vi.fn().mockImplementation(
() => new Promise((resolve) => {
resolveFetch = resolve
}),
)
vi.stubGlobal('fetch', fetchMock)
const request = () => app.request('/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello' }),
})
const first = request()
const second = request()
await new Promise((resolve) => setTimeout(resolve, 0))
expect(fetchMock).toHaveBeenCalledTimes(1)
resolveFetch!({
ok: true,
arrayBuffer: async () => Buffer.from('shared-audio'),
})
const [firstRes, secondRes] = await Promise.all([first, second])
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(firstRes.status).toBe(200)
expect(secondRes.status).toBe(200)
expect(Buffer.from(await firstRes.arrayBuffer()).toString()).toBe('shared-audio')
expect(Buffer.from(await secondRes.arrayBuffer()).toString()).toBe('shared-audio')
})
it('does not share an in-flight synthesis across different endpoints or API keys', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockResolvedValue(undefined)
mockGetSettings.mockImplementation((userId: string) => ({
preferences: {
tts: createTtsConfig(
userId === 'first'
? { endpoint: 'https://first.example.com', apiKey: 'first-key' }
: { endpoint: 'https://second.example.com', apiKey: 'second-key' },
),
},
}))
const resolvers: Array<(value: { ok: boolean; arrayBuffer: () => Promise<Buffer> }) => void> = []
const fetchMock = vi.fn().mockImplementation(
() => new Promise((resolve) => {
resolvers.push(resolve)
}),
)
vi.stubGlobal('fetch', fetchMock)
const request = (userId: string) => app.request('/synthesize?userId=' + userId, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Isolated text' }),
})
const first = request('first')
const second = request('second')
await new Promise((resolve) => setTimeout(resolve, 0))
expect(fetchMock).toHaveBeenCalledTimes(2)
resolvers[0]!({ ok: true, arrayBuffer: async () => Buffer.from('first-audio') })
resolvers[1]!({ ok: true, arrayBuffer: async () => Buffer.from('second-audio') })
const [firstRes, secondRes] = await Promise.all([first, second])
expect(firstRes.status).toBe(200)
expect(secondRes.status).toBe(200)
expect(Buffer.from(await firstRes.arrayBuffer()).toString()).toBe('first-audio')
expect(Buffer.from(await secondRes.arrayBuffer()).toString()).toBe('second-audio')
})
it('isolates the disk cache across providers for identical text', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockResolvedValue(undefined)
mockGetSettings.mockImplementation((userId: string) => ({
preferences: {
tts: createTtsConfig(
userId === 'first'
? { endpoint: 'https://first.example.com', apiKey: 'first-key' }
: { endpoint: 'https://second.example.com', apiKey: 'second-key' },
),
},
}))
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
arrayBuffer: async () => Buffer.from('audio'),
}))
const request = (userId: string) => app.request('/synthesize?userId=' + userId, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Shared text' }),
})
await request('first')
await request('second')
const statPaths = mockStat.mock.calls.map((call: unknown[]) => call[0] as string)
expect(statPaths).toHaveLength(2)
expect(statPaths[0]).not.toBe(statPaths[1])
})
it('returns the upstream error details when synthesis fails', async () => {
mockStat.mockRejectedValue(new Error('not found'))
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
@@ -367,6 +594,25 @@ describe('TTS route handlers', () => {
})
})
it('returns an upstream error when synthesis times out', async () => {
mockStat.mockRejectedValue(new Error('not found'))
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new DOMException('The operation timed out.', 'TimeoutError')))
const res = await app.request('/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello' }),
})
expect(res.status).toBe(500)
expect(await res.json()).toEqual({
error: 'TTS API request failed',
details: 'TTS upstream request timed out',
voice: 'alloy',
availableVoices: [],
})
})
it('lists models and stores them when not cached', async () => {
mockDiscoverModelsCached.mockResolvedValue({ models: ['tts-1', 'tts-1-hd'], cached: false })
@@ -451,6 +697,36 @@ describe('TTS route handlers', () => {
cache: { count: 1, sizeBytes: 1024, sizeMB: 0, maxSizeMB: 200, ttlHours: 24 },
})
})
it('clears the cache through the DELETE /cache route', async () => {
mockReaddir.mockResolvedValue(['a.mp3', 'b.mp3', 'pending.tmp', '.a.mp3.ocm-tmp-1-2'] as any)
mockUnlink.mockResolvedValue(undefined)
const res = await app.request('/cache', { method: 'DELETE' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ cleared: 2 })
expect(mockUnlink).toHaveBeenCalledTimes(2)
})
it('returns zero when the cache directory is missing', async () => {
mockReaddir.mockRejectedValue(Object.assign(new Error('missing'), { code: 'ENOENT' }))
const res = await app.request('/cache', { method: 'DELETE' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ cleared: 0 })
})
it('returns 500 when clearing the cache fails unexpectedly', async () => {
mockReaddir.mockResolvedValue(['a.mp3'] as any)
mockUnlink.mockRejectedValue(Object.assign(new Error('denied'), { code: 'EACCES' }))
const res = await app.request('/cache', { method: 'DELETE' })
expect(res.status).toBe(500)
expect(await res.json()).toEqual({ error: 'Failed to clear TTS cache' })
})
})
describe('cacheAudio', () => {
@@ -462,12 +738,12 @@ describe('cacheAudio', () => {
mockReaddir.mockResolvedValue(['big.mp3'] as any)
mockStat.mockResolvedValue({ size: 200 * 1024 * 1024, mtimeMs: 1000 } as any)
mockUnlink.mockResolvedValue(undefined)
mockWriteFile.mockResolvedValue(undefined)
mockWriteFileAtomic.mockResolvedValue(undefined)
await cacheAudio('cache-key', Buffer.from('audio'))
expect(mockUnlink).toHaveBeenCalledWith(expect.stringContaining('big.mp3'))
expect(mockWriteFile).toHaveBeenCalledWith(
expect(mockWriteFileAtomic).toHaveBeenCalledWith(
expect.stringContaining('cache-key.mp3'),
expect.any(Buffer),
)
@@ -476,14 +752,34 @@ describe('cacheAudio', () => {
it('writes audio without cleanup when the cache has room', async () => {
mockReaddir.mockResolvedValue(['small.mp3'] as any)
mockStat.mockResolvedValue({ size: 1024, mtimeMs: 1000 } as any)
mockWriteFile.mockResolvedValue(undefined)
mockWriteFileAtomic.mockResolvedValue(undefined)
await cacheAudio('cache-key', Buffer.from('audio'))
expect(mockUnlink).not.toHaveBeenCalled()
expect(mockWriteFile).toHaveBeenCalledWith(
expect(mockWriteFileAtomic).toHaveBeenCalledWith(
expect.stringContaining('cache-key.mp3'),
expect.any(Buffer),
)
})
it('writes the audio buffer atomically to the final .mp3 path', async () => {
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockResolvedValue(undefined)
await cacheAudio('cache-key', Buffer.from('audio'))
expect(mockWriteFileAtomic).toHaveBeenCalledTimes(1)
const [writtenPath, writtenData] = mockWriteFileAtomic.mock.calls[0]!
expect(writtenPath).toContain('cache-key.mp3')
expect(writtenData).toEqual(Buffer.from('audio'))
})
it('propagates an atomic write failure', async () => {
mockReaddir.mockResolvedValue([] as any)
mockWriteFileAtomic.mockRejectedValue(new Error('rename failed'))
await expect(cacheAudio('cache-key', Buffer.from('audio'))).rejects.toThrow('rename failed')
})
})
+35 -2
View File
@@ -1,8 +1,8 @@
import { mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises'
import { mkdir, mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { canonicalPath, canonicalPathSync, writeFileAtomicSync } from '../../src/utils/fs-safe'
import { canonicalPath, canonicalPathSync, writeFileAtomic, writeFileAtomicSync } from '../../src/utils/fs-safe'
describe('canonicalPath', () => {
let workDir = ''
@@ -67,3 +67,36 @@ describe('writeFileAtomicSync', () => {
await expect(readdir(workDir)).resolves.toEqual(['file.txt'])
})
})
describe('writeFileAtomic', () => {
let workDir = ''
beforeEach(async () => {
workDir = await mkdtemp(path.join(tmpdir(), 'fs-safe-atomic-async-'))
})
afterEach(async () => {
await rm(workDir, { recursive: true, force: true })
})
it('writes Buffer bytes unchanged and leaves no temp file behind', async () => {
const target = path.join(workDir, 'audio.mp3')
const bytes = Buffer.from([0xff, 0x00, 0xfe, 0x80])
await writeFileAtomic(target, bytes)
expect(await readFile(target)).toEqual(bytes)
expect((await stat(target)).mode & 0o777).toBe(0o600)
await expect(readdir(workDir)).resolves.toEqual(['audio.mp3'])
})
it('removes the temp file and rethrows when the rename fails', async () => {
const target = path.join(workDir, 'target')
await mkdir(target)
await writeFile(path.join(target, 'blocker'), 'x')
await expect(writeFileAtomic(target, 'content')).rejects.toThrow()
await expect(readdir(workDir)).resolves.toEqual(['target'])
})
})
+10
View File
@@ -11,6 +11,10 @@ export interface TTSVoicesResponse {
cached: boolean
}
export interface TTSClearCacheResponse {
cleared: number
}
export interface TTSStatusResponse {
enabled: boolean
configured: boolean
@@ -42,6 +46,12 @@ export const ttsApi = {
})
},
clearCache: async (): Promise<TTSClearCacheResponse> => {
return fetchWrapper(`${API_BASE_URL}/api/tts/cache`, {
method: 'DELETE',
})
},
synthesize: async (text: string, userId = 'default', signal?: AbortSignal): Promise<Blob> => {
return fetchWrapperBlob(`${API_BASE_URL}/api/tts/synthesize`, {
method: 'POST',
@@ -0,0 +1,181 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { ReactNode } from 'react'
import { TTSSettings } from './TTSSettings'
import { useSettings } from '@/hooks/useSettings'
import { useTTS } from '@/hooks/useTTS'
import { useTTSModels, useTTSVoices, useTTSDiscovery } from '@/hooks/useTTSDiscovery'
import { getAvailableVoiceNames, isWebSpeechSupported } from '@/lib/webSpeechSynthesizer'
import { createUseSettingsMock } from '@/test/test-utils'
import type { UserPreferences } from '@/api/types/settings'
vi.mock('@/hooks/useSettings')
vi.mock('@/hooks/useTTS')
vi.mock('@/hooks/useTTSDiscovery')
vi.mock('@/lib/webSpeechSynthesizer')
const fetchMock = vi.fn()
const baseTts: NonNullable<UserPreferences['tts']> = {
enabled: true,
provider: 'external',
autoPlay: false,
endpoint: 'https://api.openai.com',
apiKey: 'sk-test',
voice: 'alloy',
model: 'tts-1',
speed: 1,
availableVoices: [],
availableModels: [],
lastVoicesFetch: 0,
lastModelsFetch: 0,
}
function buildPreferences(overrides: Partial<UserPreferences> = {}): UserPreferences {
return {
theme: 'dark',
mode: 'build',
autoScroll: true,
expandDiffs: true,
expandToolCalls: false,
showReasoning: false,
simpleChatMode: false,
keyboardShortcuts: {},
customCommands: [],
tts: baseTts,
...overrides,
}
}
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
})
}
function renderTTSSettings() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
})
function Wrapper({ children }: { children: ReactNode }) {
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}
return render(<TTSSettings />, { wrapper: Wrapper })
}
function mockHooks(preferences: UserPreferences = buildPreferences()) {
vi.mocked(useSettings).mockReturnValue(createUseSettingsMock({ preferences }))
vi.mocked(useTTS).mockReturnValue({
speak: vi.fn(),
speakWithConfig: vi.fn(),
speakMessage: vi.fn(),
stop: vi.fn(),
state: 'idle',
error: null,
currentText: null,
originalText: null,
activeMessageId: null,
isEnabled: true,
isPlaying: false,
isLoading: false,
isIdle: true,
})
vi.mocked(useTTSDiscovery).mockReturnValue({
refreshModels: vi.fn(),
refreshVoices: vi.fn(),
refreshAll: vi.fn(),
invalidateModels: vi.fn(),
invalidateVoices: vi.fn(),
invalidateAll: vi.fn(),
})
vi.mocked(useTTSModels).mockReturnValue({
data: undefined,
isLoading: false,
refetch: vi.fn(),
} as unknown as ReturnType<typeof useTTSModels>)
vi.mocked(useTTSVoices).mockReturnValue({
data: undefined,
isLoading: false,
refetch: vi.fn(),
} as unknown as ReturnType<typeof useTTSVoices>)
vi.mocked(isWebSpeechSupported).mockReturnValue(true)
vi.mocked(getAvailableVoiceNames).mockResolvedValue(['Alex'])
}
describe('TTSSettings audio cache', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubGlobal('fetch', fetchMock)
mockHooks()
})
afterEach(() => {
vi.unstubAllGlobals()
})
it('clears the server cache via DELETE /api/tts/cache and reports the count', async () => {
const user = userEvent.setup()
fetchMock.mockResolvedValueOnce(jsonResponse({ cleared: 3 }))
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 3 cached audio file(s).')
const [url, options] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(String(url)).toContain('/api/tts/cache')
expect(options).toMatchObject({ method: 'DELETE' })
})
it('disables the button and shows a pending label while clearing', async () => {
const user = userEvent.setup()
let resolveResponse: (response: Response) => void = () => {}
fetchMock.mockReturnValueOnce(
new Promise<Response>((resolve) => {
resolveResponse = resolve
}),
)
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
const pendingButton = screen.getByRole('button', { name: /clearing/i })
expect(pendingButton).toBeDisabled()
resolveResponse(jsonResponse({ cleared: 1 }))
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 1 cached audio file(s).')
})
it('shows an actionable error and allows retrying after a failure', async () => {
const user = userEvent.setup()
fetchMock
.mockResolvedValueOnce(jsonResponse({ error: 'cache unavailable' }, 500))
.mockResolvedValueOnce(jsonResponse({ cleared: 2 }))
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
expect(await screen.findByRole('alert')).toHaveTextContent('cache unavailable')
const retryButton = screen.getByRole('button', { name: 'Clear audio cache' })
expect(retryButton).toBeEnabled()
await user.click(retryButton)
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 2 cached audio file(s).')
expect(fetchMock).toHaveBeenCalledTimes(2)
})
it('stays available when TTS is disabled or the builtin provider is selected', async () => {
mockHooks(buildPreferences({ tts: { ...baseTts, enabled: false } }))
const disabledRender = renderTTSSettings()
expect(screen.getByRole('button', { name: 'Clear audio cache' })).toBeInTheDocument()
disabledRender.unmount()
mockHooks(buildPreferences({ tts: { ...baseTts, provider: 'builtin', voice: 'Alex' } }))
renderTTSSettings()
expect(screen.getByRole('button', { name: 'Clear audio cache' })).toBeInTheDocument()
expect(await screen.findByText('1 voices available in your browser')).toBeInTheDocument()
})
})
@@ -1,12 +1,13 @@
import { useEffect, useState, useRef } from 'react'
import { useForm } from 'react-hook-form'
import { useMutation } from '@tanstack/react-query'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
import { useSettings } from '@/hooks/useSettings'
import { useTTS } from '@/hooks/useTTS'
import { useTTSModels, useTTSVoices, useTTSDiscovery } from '@/hooks/useTTSDiscovery'
import { getAvailableVoiceNames, isWebSpeechSupported } from '@/lib/webSpeechSynthesizer'
import { Loader2, Volume2, XCircle, RefreshCw, MonitorSpeaker, Globe, CheckCircle2 } from 'lucide-react'
import { Loader2, Volume2, XCircle, RefreshCw, MonitorSpeaker, Globe, CheckCircle2, Trash2 } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { SquareFill } from '@/components/ui/square-fill'
import { Input } from '@/components/ui/input'
@@ -14,6 +15,7 @@ import { Switch } from '@/components/ui/switch'
import { Slider } from '@/components/ui/slider'
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Combobox } from '@/components/ui/combobox'
import { ttsApi } from '@/api/tts'
import { DEFAULT_TTS_CONFIG } from '@/api/types/settings'
const TEST_PHRASE = 'Text to speech is working correctly.'
@@ -240,6 +242,15 @@ export function TTSSettings() {
stop()
}
const clearCacheMutation = useMutation({
mutationFn: () => ttsApi.clearCache(),
})
const clearCacheError =
clearCacheMutation.error instanceof Error
? clearCacheMutation.error.message
: 'Unable to clear the audio cache'
return (
<div className="bg-card border border-border rounded-lg p-6">
<div className="flex items-center justify-between mb-6">
@@ -681,6 +692,50 @@ export function TTSSettings() {
</div>
</>
)}
<div className="flex flex-col gap-3 rounded-lg border border-border p-4 sm:flex-row sm:items-center sm:justify-between">
<div className="space-y-0.5">
<div className="text-base font-medium">Clear audio cache</div>
<p className="text-sm text-muted-foreground">
Removes all cached server TTS audio. Audio is regenerated on next use, and active playback or synthesis may repopulate the cache.
</p>
<p className="text-xs text-muted-foreground">
Does not affect model or voice discovery.
</p>
{clearCacheMutation.isSuccess && (
<p role="status" className="text-sm text-success flex items-center gap-1">
<CheckCircle2 className="h-4 w-4" />
Cleared {clearCacheMutation.data?.cleared ?? 0} cached audio file(s).
</p>
)}
{clearCacheMutation.isError && (
<p role="alert" className="text-sm text-destructive flex items-center gap-1">
<XCircle className="h-4 w-4" />
Could not clear cached audio: {clearCacheError}. Try again.
</p>
)}
</div>
<Button
type="button"
variant="outline"
size="sm"
className="self-start sm:self-auto"
onClick={() => clearCacheMutation.mutate()}
disabled={clearCacheMutation.isPending}
>
{clearCacheMutation.isPending ? (
<>
<Loader2 className="h-4 w-4 animate-spin mr-2" />
Clearing...
</>
) : (
<>
<Trash2 className="h-4 w-4 mr-2" />
Clear audio cache
</>
)}
</Button>
</div>
</form>
</Form>
</div>
+296
View File
@@ -0,0 +1,296 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, act, waitFor } from '@testing-library/react'
import { TTSProvider } from './TTSContext'
import { useTTS } from '@/hooks/useTTS'
import type { TTSConfig, TTSContextValue } from './tts-context'
import type { WebSpeechSynthesizer } from '@/lib/webSpeechSynthesizer'
const mocks = vi.hoisted(() => ({
useSettings: vi.fn(),
}))
const webSpeech = vi.hoisted(() => ({
stop: vi.fn(),
clearCallbacks: vi.fn(),
waitForVoices: vi.fn(() => Promise.resolve()),
onEnd: vi.fn(),
onError: vi.fn(),
speakChunked: vi.fn(() => Promise.resolve()),
}))
vi.mock('@/hooks/useSettings', () => ({
useSettings: mocks.useSettings,
}))
vi.mock('@/lib/webSpeechSynthesizer', () => ({
getWebSpeechSynthesizer: () => webSpeech as unknown as WebSpeechSynthesizer,
isWebSpeechSupported: () => true,
}))
class FakeAudio {
static instances: FakeAudio[] = []
src: string
paused = true
onended: (() => void) | null = null
onerror: (() => void) | null = null
constructor(src: string) {
this.src = src
FakeAudio.instances.push(this)
}
play = vi.fn(() => {
this.paused = false
return Promise.resolve()
})
pause = vi.fn(() => {
this.paused = true
})
load = vi.fn()
removeAttribute = vi.fn((name: string) => {
if (name === 'src') this.src = ''
})
}
const ttsRef: { current: TTSContextValue | null } = { current: null }
function Consumer() {
ttsRef.current = useTTS()
return null
}
let fetchBodies: string[] = []
let objectUrlCounter = 0
const createObjectURL = vi.fn(() => `blob:fake-${++objectUrlCounter}`)
const revokeObjectURL = vi.fn()
const textA = 'Alpha one. Alpha two. Alpha three. Alpha four. Alpha five.'
const textB = 'Bravo one. Bravo two. Bravo three. Bravo four. Bravo five.'
function setPreferences(overrides: Partial<TTSConfig> = {}) {
mocks.useSettings.mockReturnValue({
preferences: {
tts: {
enabled: true,
autoPlay: false,
provider: 'external',
endpoint: 'https://api.openai.com',
apiKey: 'test-key',
voice: 'alloy',
model: 'tts-1',
speed: 1.0,
...overrides,
},
},
updateSettings: vi.fn(),
})
}
const externalConfig: TTSConfig = {
enabled: true,
provider: 'external',
endpoint: 'https://api.openai.com',
apiKey: 'test-key',
voice: 'alloy',
model: 'tts-1',
speed: 1.0,
}
const builtinConfig: TTSConfig = {
enabled: true,
provider: 'builtin',
endpoint: '',
apiKey: '',
voice: '',
model: '',
speed: 1.0,
}
function renderProvider() {
return render(
<TTSProvider>
<Consumer />
</TTSProvider>,
)
}
beforeEach(() => {
vi.clearAllMocks()
FakeAudio.instances = []
fetchBodies = []
objectUrlCounter = 0
ttsRef.current = null
const fetchMock = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
const body = JSON.parse(String(init?.body ?? '{}')) as { text: string }
fetchBodies.push(body.text)
const blob = new Blob([`audio:${body.text}`], { type: 'audio/mpeg' })
return {
ok: true,
status: 200,
headers: {
get: (name: string) => (name.toLowerCase() === 'content-type' ? 'audio/mpeg' : null),
},
blob: async () => blob,
json: async () => ({}),
} as unknown as Response
})
vi.stubGlobal('Audio', FakeAudio)
vi.stubGlobal('fetch', fetchMock)
URL.createObjectURL = createObjectURL
URL.revokeObjectURL = revokeObjectURL
})
afterEach(() => {
vi.unstubAllGlobals()
})
describe('TTSProvider playback isolation', () => {
it('does not start a concurrent chain when a stale onended fires after a new playback begins', async () => {
setPreferences()
renderProvider()
await act(async () => {
await ttsRef.current!.speakMessage('A', textA)
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(1))
const firstAudio = FakeAudio.instances[0]
await waitFor(() => expect(firstAudio.paused).toBe(false))
const staleOnended = firstAudio.onended
expect(staleOnended).toBeTypeOf('function')
await act(async () => {
await ttsRef.current!.speakMessage('B', textB)
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(2))
const secondAudio = FakeAudio.instances[1]
await waitFor(() => expect(secondAudio.paused).toBe(false))
await waitFor(() => expect(fetchBodies).toContain('Bravo three. Bravo four.'))
expect(firstAudio.paused).toBe(true)
await act(async () => {
staleOnended!()
await new Promise((resolve) => setTimeout(resolve, 0))
})
const playing = FakeAudio.instances.filter((audio) => !audio.paused)
expect(playing).toHaveLength(1)
expect(playing[0]).toBe(secondAudio)
expect(FakeAudio.instances).toHaveLength(2)
})
it('ignores a stale onerror after cleanup without orphaning the new audio or corrupting state', async () => {
setPreferences()
renderProvider()
await act(async () => {
await ttsRef.current!.speakMessage('A', textA)
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(1))
const firstAudio = FakeAudio.instances[0]
await waitFor(() => expect(firstAudio.paused).toBe(false))
const staleOnerror = firstAudio.onerror
expect(staleOnerror).toBeTypeOf('function')
await act(async () => {
await ttsRef.current!.speakMessage('B', textB)
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(2))
const secondAudio = FakeAudio.instances[1]
await waitFor(() => expect(secondAudio.paused).toBe(false))
await act(async () => {
staleOnerror!()
})
expect(ttsRef.current!.state).toBe('playing')
expect(ttsRef.current!.error).toBeNull()
expect(secondAudio.paused).toBe(false)
})
it('cancels active Web Speech playback when an external playback starts', async () => {
setPreferences({ provider: 'external' })
renderProvider()
await act(async () => {
await ttsRef.current!.speakWithConfig('Hello from web speech.', builtinConfig)
})
expect(webSpeech.speakChunked).toHaveBeenCalled()
webSpeech.stop.mockClear()
await act(async () => {
await ttsRef.current!.speakWithConfig('Now external audio.', externalConfig)
})
expect(webSpeech.stop).toHaveBeenCalled()
})
it('fetches each chunk at most once when playback outruns an in-flight prefetch', async () => {
setPreferences()
renderProvider()
const slowChunkText = 'Alpha three. Alpha four.'
let releaseSlowChunk: () => void = () => {}
const slowChunkGate = new Promise<void>((resolve) => {
releaseSlowChunk = resolve
})
const baseFetch = globalThis.fetch
vi.stubGlobal(
'fetch',
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const body = JSON.parse(String(init?.body ?? '{}')) as { text: string }
if (body.text === slowChunkText) {
await slowChunkGate
}
return baseFetch(input, init)
}),
)
await act(async () => {
await ttsRef.current!.speakMessage('A', textA)
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(1))
const firstAudio = FakeAudio.instances[0]
await waitFor(() => expect(firstAudio.paused).toBe(false))
await waitFor(() => expect(fetchBodies).toContain('Alpha one. Alpha two.'))
expect(fetchBodies).not.toContain(slowChunkText)
await act(async () => {
firstAudio.onended!()
await new Promise((resolve) => setTimeout(resolve, 0))
})
await act(async () => {
releaseSlowChunk()
await new Promise((resolve) => setTimeout(resolve, 0))
})
await waitFor(() => expect(FakeAudio.instances.length).toBeGreaterThanOrEqual(2))
const secondAudio = FakeAudio.instances[1]
await waitFor(() => expect(secondAudio.paused).toBe(false))
expect(fetchBodies.filter((text) => text === slowChunkText)).toHaveLength(1)
const fetchCounts = new Map<string, number>()
for (const text of fetchBodies) {
fetchCounts.set(text, (fetchCounts.get(text) ?? 0) + 1)
}
for (const count of fetchCounts.values()) {
expect(count).toBe(1)
}
})
})
+92 -87
View File
@@ -37,11 +37,11 @@ export function TTSProvider({ children }: TTSProviderProps) {
const abortControllerRef = useRef<AbortController | null>(null)
const audioRef = useRef<HTMLAudioElement | null>(null)
const stoppedRef = useRef(false)
const playbackIdRef = useRef(0)
const currentObjectUrlRef = useRef<string | null>(null)
const chunksRef = useRef<string[]>([])
const chunkIndexRef = useRef(0)
const prefetchedBlobsRef = useRef<Map<number, Blob>>(new Map())
const fetchingIndexRef = useRef<number>(-1)
const chunkAudioRef = useRef<Map<number, Promise<Blob | null>>>(new Map())
// Web Speech API reference
const webSpeechSynthRef = useRef<ReturnType<typeof getWebSpeechSynthesizer> | null>(null)
@@ -66,34 +66,36 @@ export function TTSProvider({ children }: TTSProviderProps) {
}, []);
const cleanup = useCallback(() => {
// Stop external audio
if (audioRef.current) {
audioRef.current.onended = null
audioRef.current.onerror = null
audioRef.current.pause()
audioRef.current.src = ''
audioRef.current.removeAttribute('src')
audioRef.current.load()
audioRef.current = null
}
// Stop Web Speech API
if (webSpeechSynthRef.current && isBuiltin) {
if (currentObjectUrlRef.current) {
URL.revokeObjectURL(currentObjectUrlRef.current)
currentObjectUrlRef.current = null
}
if (webSpeechSynthRef.current) {
webSpeechSynthRef.current.stop()
webSpeechSynthRef.current.clearCallbacks()
}
if (abortControllerRef.current) {
abortControllerRef.current.abort()
abortControllerRef.current = null
}
prefetchedBlobsRef.current.forEach((_, key) => {
prefetchedBlobsRef.current.delete(key)
})
prefetchedBlobsRef.current.clear()
chunkAudioRef.current.clear()
chunksRef.current = []
chunkIndexRef.current = 0
fetchingIndexRef.current = -1
}, [isBuiltin])
}, [])
const stop = useCallback(() => {
stoppedRef.current = true
playbackIdRef.current += 1
cleanup()
setState('idle')
setCurrentText(null)
@@ -104,14 +106,14 @@ export function TTSProvider({ children }: TTSProviderProps) {
useEffect(() => {
return () => {
stoppedRef.current = true
playbackIdRef.current += 1
cleanup()
}
}, [cleanup])
// External API synthesis
const synthesizeExternal = useCallback(async (text: string, signal?: AbortSignal): Promise<Blob | null> => {
if (stoppedRef.current) return null
const synthesizeExternal = useCallback(async (text: string, signal: AbortSignal | undefined, playbackId: number): Promise<Blob | null> => {
if (playbackIdRef.current !== playbackId) return null
try {
const response = await fetch(`${API_BASE_URL}/api/tts/synthesize`, {
@@ -121,7 +123,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
signal,
})
if (stoppedRef.current) return null
if (playbackIdRef.current !== playbackId) return null
if (!response.ok) {
let errorMessage = 'TTS request failed'
@@ -155,32 +157,31 @@ export function TTSProvider({ children }: TTSProviderProps) {
}
}, [])
const fetchNextChunk = useCallback(async (index: number) => {
if (stoppedRef.current) return
if (index >= chunksRef.current.length) return
if (prefetchedBlobsRef.current.has(index)) {
fetchNextChunk(index + 1)
return
}
fetchingIndexRef.current = index
try {
const blob = await synthesizeExternal(chunksRef.current[index], abortControllerRef.current?.signal)
if (blob && !stoppedRef.current) {
prefetchedBlobsRef.current.set(index, blob)
fetchNextChunk(index + 1)
}
} catch {
if (stoppedRef.current) return
}
fetchingIndexRef.current = -1
const getChunkAudio = useCallback((index: number, playbackId: number): Promise<Blob | null> => {
const existing = chunkAudioRef.current.get(index)
if (existing) return existing
const promise = synthesizeExternal(chunksRef.current[index], abortControllerRef.current?.signal, playbackId)
chunkAudioRef.current.set(index, promise)
return promise
}, [synthesizeExternal])
const playChunk = useCallback(async (index: number) => {
if (stoppedRef.current || index >= chunksRef.current.length) {
if (!stoppedRef.current) {
const prefetchChunk = useCallback((index: number, playbackId: number) => {
if (playbackIdRef.current !== playbackId) return
if (index >= chunksRef.current.length) return
if (chunkAudioRef.current.has(index)) return
getChunkAudio(index, playbackId)
.then((blob) => {
if (!blob || playbackIdRef.current !== playbackId) return
prefetchChunk(index + 1, playbackId)
})
.catch(() => {})
}, [getChunkAudio])
const playChunk = useCallback(async (index: number, playbackId: number) => {
if (playbackIdRef.current !== playbackId || index >= chunksRef.current.length) {
if (playbackIdRef.current === playbackId) {
setState('idle')
setCurrentText(null)
setActiveMessageId(null)
@@ -191,50 +192,54 @@ export function TTSProvider({ children }: TTSProviderProps) {
chunkIndexRef.current = index
try {
let blob: Blob | undefined = prefetchedBlobsRef.current.get(index)
if (!blob) {
if (!chunkAudioRef.current.has(index)) {
setState('loading')
const fetched = await synthesizeExternal(chunksRef.current[index], abortControllerRef.current?.signal)
if (fetched && !stoppedRef.current) {
blob = fetched
}
fetchNextChunk(index + 1)
}
if (!blob || stoppedRef.current) return
prefetchedBlobsRef.current.delete(index)
const blob = await getChunkAudio(index, playbackId)
if (!blob || playbackIdRef.current !== playbackId) return
chunkAudioRef.current.delete(index)
prefetchChunk(index + 1, playbackId)
const url = URL.createObjectURL(blob)
currentObjectUrlRef.current = url
const audio = new Audio(url)
audioRef.current = audio
audio.onended = () => {
URL.revokeObjectURL(url)
audioRef.current = null
if (!stoppedRef.current) {
playChunk(index + 1)
if (playbackIdRef.current !== playbackId) return
if (currentObjectUrlRef.current === url) {
URL.revokeObjectURL(url)
currentObjectUrlRef.current = null
}
if (audioRef.current === audio) {
audioRef.current = null
}
playChunk(index + 1, playbackId)
}
audio.onerror = () => {
URL.revokeObjectURL(url)
audioRef.current = null
if (!stoppedRef.current) {
setError('Audio playback failed')
setState('error')
if (playbackIdRef.current !== playbackId) return
if (currentObjectUrlRef.current === url) {
URL.revokeObjectURL(url)
currentObjectUrlRef.current = null
}
if (audioRef.current === audio) {
audioRef.current = null
}
setError('Audio playback failed')
setState('error')
}
setState('playing')
await audio.play()
} catch (err) {
if (stoppedRef.current) return
if (playbackIdRef.current !== playbackId) return
setError(err instanceof Error ? err.message : 'TTS failed')
setState('error')
}
}, [synthesizeExternal, fetchNextChunk])
}, [getChunkAudio, prefetchChunk])
// Builtin Web Speech synthesis - takes explicit config and optional messageId
const speakBuiltinWithConfig = useCallback(async (text: string, config: TTSConfig, messageId: string | null = null): Promise<boolean> => {
@@ -259,7 +264,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
}
stop()
stoppedRef.current = false
const playbackId = playbackIdRef.current
setError(null)
setOriginalText(text)
@@ -274,38 +279,38 @@ export function TTSProvider({ children }: TTSProviderProps) {
const synth = getSynthesizer()
await synth.waitForVoices()
if (playbackIdRef.current !== playbackId) return false
const voiceName = config.voice || ''
synth.clearCallbacks()
synth.onEnd(() => {
if (!stoppedRef.current) {
setState('idle')
setCurrentText(null)
setActiveMessageId(null)
}
if (playbackIdRef.current !== playbackId) return
setState('idle')
setCurrentText(null)
setActiveMessageId(null)
})
synth.onError((err) => {
if (!stoppedRef.current) {
setError(err)
setState('error')
}
if (playbackIdRef.current !== playbackId) return
setError(err)
setState('error')
})
try {
setState('playing')
const rate = config.speed || 1.0
await synth.speakChunked(sanitizedText, 200, {
voice: voiceName || undefined,
rate: rate,
})
return true
} catch (err) {
if (stoppedRef.current) return false
if (playbackIdRef.current !== playbackId) return false
setError(err instanceof Error ? err.message : 'TTS failed')
setState('error')
return false
@@ -315,7 +320,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
// Internal helper to start external TTS playback
const startExternalPlayback = useCallback((sanitizedText: string, original: string, messageId: string | null): boolean => {
stop()
stoppedRef.current = false
const playbackId = playbackIdRef.current
setError(null)
setOriginalText(original)
@@ -328,9 +333,9 @@ export function TTSProvider({ children }: TTSProviderProps) {
abortControllerRef.current = new AbortController()
chunksRef.current = splitIntoChunks(sanitizedText)
playChunk(0)
playChunk(0, playbackId)
return true
}, [stop, playChunk])