( options: UseRealtimeChatOptions, )
| 55 | * ``` |
| 56 | */ |
| 57 | export function useRealtimeChat( |
| 58 | options: UseRealtimeChatOptions, |
| 59 | ): UseRealtimeChatReturn { |
| 60 | // State |
| 61 | const [status, setStatus] = useState<RealtimeStatus>('idle') |
| 62 | const [mode, setMode] = useState<RealtimeMode>('idle') |
| 63 | const [messages, setMessages] = useState<Array<RealtimeMessage>>([]) |
| 64 | const [pendingUserTranscript, setPendingUserTranscript] = useState< |
| 65 | string | null |
| 66 | >(null) |
| 67 | const [pendingAssistantTranscript, setPendingAssistantTranscript] = useState< |
| 68 | string | null |
| 69 | >(null) |
| 70 | const [error, setError] = useState<Error | null>(null) |
| 71 | const [inputLevel, setInputLevel] = useState(0) |
| 72 | const [outputLevel, setOutputLevel] = useState(0) |
| 73 | const [vadMode, setVADModeState] = useState<'server' | 'semantic' | 'manual'>( |
| 74 | options.vadMode ?? 'server', |
| 75 | ) |
| 76 | |
| 77 | // Refs |
| 78 | const clientRef = useRef<RealtimeClient | null>(null) |
| 79 | const optionsRef = useRef(options) |
| 80 | optionsRef.current = options |
| 81 | const animationFrameRef = useRef<number | null>(null) |
| 82 | |
| 83 | // Create client instance - use ref to ensure we reuse the same instance |
| 84 | // This handles React StrictMode double-rendering |
| 85 | if (!clientRef.current) { |
| 86 | // Each optional source field is spread conditionally because the |
| 87 | // `RealtimeClientOptions` target declares strict optionals |
| 88 | // (`field?: T`) and `exactOptionalPropertyTypes` rejects passing |
| 89 | // `undefined` for absent values. |
| 90 | const initial = optionsRef.current |
| 91 | clientRef.current = new RealtimeClient({ |
| 92 | getToken: initial.getToken, |
| 93 | adapter: initial.adapter, |
| 94 | ...(initial.tools !== undefined && { tools: initial.tools }), |
| 95 | ...(initial.instructions !== undefined && { |
| 96 | instructions: initial.instructions, |
| 97 | }), |
| 98 | ...(initial.voice !== undefined && { voice: initial.voice }), |
| 99 | ...(initial.autoPlayback !== undefined && { |
| 100 | autoPlayback: initial.autoPlayback, |
| 101 | }), |
| 102 | ...(initial.autoCapture !== undefined && { |
| 103 | autoCapture: initial.autoCapture, |
| 104 | }), |
| 105 | ...(initial.vadMode !== undefined && { vadMode: initial.vadMode }), |
| 106 | ...(initial.outputModalities !== undefined && { |
| 107 | outputModalities: initial.outputModalities, |
| 108 | }), |
| 109 | ...(initial.temperature !== undefined && { |
| 110 | temperature: initial.temperature, |
| 111 | }), |
| 112 | ...(initial.maxOutputTokens !== undefined && { |
| 113 | maxOutputTokens: initial.maxOutputTokens, |
| 114 | }), |
no test coverage detected