add hermes tts playback (#541)

This commit is contained in:
ekko
2026-05-08 15:34:11 +08:00
committed by GitHub
parent 10d2f892ff
commit d54f9479b9
8 changed files with 218 additions and 144 deletions
+34
View File
@@ -0,0 +1,34 @@
export interface TtsOptions {
text: string
lang?: string
}
export async function generateSpeech(opts: TtsOptions): Promise<{ audio: Blob; engine: string }> {
const res = await fetch(
`${localStorage.getItem('hermes_server_url') || ''}/api/hermes/tts`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('hermes_api_key') || ''}`,
},
body: JSON.stringify(opts),
},
)
if (!res.ok) {
throw new Error(`TTS request failed: ${res.status}`)
}
const audio = await res.blob()
const engine = res.headers.get('X-TTS-Engine') || 'unknown'
return { audio, engine }
}
export function playAudioBlob(blob: Blob): HTMLAudioElement {
const url = URL.createObjectURL(blob)
const audio = new Audio(url)
audio.play()
audio.onended = () => URL.revokeObjectURL(url)
return audio
}