mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
fix(tts): key the audio cache by provider and add a settings clear action
- Hash text, voice, model, speed, normalized endpoint and API key into one SHA-256 key shared by the disk cache and in-flight synthesis, so different providers cannot reuse each other's audio and old keys are not read as fallback. - Add DELETE /api/tts/cache to remove completed cached audio files, keep temporary files, tolerate missing files and report real failures. - Add a Clear audio cache action to TTS settings, available even when TTS is disabled or the builtin provider is selected, with pending, success and retryable error states.
This commit is contained in:
1 parent
1eba33c6f1
commit
169ee25a8a
5 files changed
+511
-35
No files matched your search
+57
-21
@@ -24,9 +24,25 @@ 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')
|
||||
}
|
||||
|
||||
@@ -119,15 +135,6 @@ async function cacheAudio(cacheKey: string, audioData: Buffer): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
interface SynthesisParams {
|
||||
text: string
|
||||
endpoint: string
|
||||
apiKey: string
|
||||
voice: string
|
||||
model: string
|
||||
speed: number
|
||||
}
|
||||
|
||||
class TTSUpstreamError extends Error {
|
||||
readonly status: 400 | 500
|
||||
readonly details: string
|
||||
@@ -322,7 +329,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()
|
||||
@@ -353,7 +385,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()
|
||||
|
||||
@@ -376,14 +409,7 @@ export function createTTSRoutes(db: Database) {
|
||||
|
||||
let audioBuffer: Buffer
|
||||
try {
|
||||
audioBuffer = await getOrStartSynthesis(cacheKey, {
|
||||
text,
|
||||
endpoint,
|
||||
apiKey,
|
||||
voice,
|
||||
model,
|
||||
speed,
|
||||
})
|
||||
audioBuffer = await getOrStartSynthesis(cacheKey, synthesisParams)
|
||||
} catch (error) {
|
||||
if (error instanceof TTSUpstreamError) {
|
||||
return c.json({
|
||||
@@ -511,5 +537,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
|
||||
}
|
||||
Reference in new issue
Block a user