Download src/hooks/useAutoTTS.ts from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/chenbhao/codev/resolve/4e7af9b948a3e032faef957075f162278e7d094a/src/hooks/useAutoTTS.ts
- Command line
-
hf download hf://chenbhao/codev@4e7af9b948a3e032faef957075f162278e7d094a/src/hooks/useAutoTTS.ts
-
curl -L -o useAutoTTS.ts https://huggingface.co/chenbhao/codev/resolve/4e7af9b948a3e032faef957075f162278e7d094a/src/hooks/useAutoTTS.ts
1.49 kB
| import { useEffect, useRef } from 'react' | |
| import { speakWithEdgeTTS, playAudioFile } from '../services/voice/edgeTTS.js' | |
| import { getInitialSettings } from '../utils/settings/settings.js' | |
| import type { RenderableMessage } from '../types/message.js' | |
| export function useAutoTTS(messages: RenderableMessage[]): void { | |
| const triggeredIdsRef = useRef<Set<string>>(new Set()) | |
| const pendingPlayRef = useRef<Promise<void> | null>(null) | |
| useEffect(() => { | |
| const settings = getInitialSettings() | |
| if (!settings.voiceAutoTTS || !settings.voiceEnabled) return | |
| for (const msg of messages) { | |
| if (msg.type !== 'assistant') continue | |
| if (triggeredIdsRef.current.has(msg.uuid)) continue | |
| const content = msg.message.content[0] | |
| if (content?.type !== 'text') continue | |
| if (!content.text.trim()) continue | |
| triggeredIdsRef.current.add(msg.uuid) | |
| const text = content.text | |
| const voice = settings.voiceTTSVoice || 'en-US-JennyNeural' | |
| const run = async () => { | |
| const result = await speakWithEdgeTTS(text, { | |
| voice, | |
| pythonPath: settings.voiceTTSCommand || undefined, | |
| }) | |
| if (result.success && result.audioPath) { | |
| await playAudioFile(result.audioPath) | |
| } | |
| } | |
| if (pendingPlayRef.current) { | |
| pendingPlayRef.current = pendingPlayRef.current.then(run, run).catch(() => {}) | |
| } else { | |
| pendingPlayRef.current = run().catch(() => {}) | |
| } | |
| } | |
| }, [messages]) | |
| } |