mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-21 21:29:47 +00:00
* feat(voice): dynamic settings voice picker with safe fallback + preview * fix(voice): honor picker with configured agent and stop preview on unmount * fix(voice): apply PR review feedback for agent selection and preview cleanup
77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import type { VoiceSession } from './types'
|
|
import type { ElevenLabsLanguage } from '@/lib/languages'
|
|
|
|
let voiceSession: VoiceSession | null = null
|
|
let voiceSessionStarted = false
|
|
let currentSessionId: string | null = null
|
|
|
|
export async function startRealtimeSession(
|
|
sessionId: string,
|
|
initialContext?: string,
|
|
language?: ElevenLabsLanguage,
|
|
voiceId?: string
|
|
) {
|
|
if (!voiceSession) {
|
|
console.warn('[Voice] No voice session registered')
|
|
return
|
|
}
|
|
|
|
try {
|
|
currentSessionId = sessionId
|
|
await voiceSession.startSession({
|
|
sessionId,
|
|
initialContext,
|
|
language,
|
|
voiceId
|
|
})
|
|
voiceSessionStarted = true
|
|
} catch (error) {
|
|
console.error('[Voice] Failed to start realtime session:', error)
|
|
currentSessionId = null
|
|
voiceSessionStarted = false
|
|
}
|
|
}
|
|
|
|
export async function stopRealtimeSession() {
|
|
if (!voiceSession) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await voiceSession.endSession()
|
|
currentSessionId = null
|
|
voiceSessionStarted = false
|
|
} catch (error) {
|
|
console.error('[Voice] Failed to stop realtime session:', error)
|
|
}
|
|
}
|
|
|
|
export function resetRealtimeSessionState() {
|
|
currentSessionId = null
|
|
voiceSessionStarted = false
|
|
}
|
|
|
|
export function registerVoiceSession(session: VoiceSession) {
|
|
if (voiceSession) {
|
|
console.warn('[Voice] Voice session already registered, replacing with new one')
|
|
}
|
|
voiceSession = session
|
|
}
|
|
|
|
export function isVoiceSessionStarted(): boolean {
|
|
return voiceSessionStarted
|
|
}
|
|
|
|
export function getVoiceSession(): VoiceSession | null {
|
|
return voiceSession
|
|
}
|
|
|
|
export function getCurrentRealtimeSessionId(): string | null {
|
|
return currentSessionId
|
|
}
|
|
|
|
export function updateCurrentSessionId(sessionId: string | null) {
|
|
console.log(`[Voice] Realtime session ID updated: ${sessionId}`)
|
|
currentSessionId = sessionId
|
|
}
|