Open History to see the last commit
1import { useState } from 'react'
2import {
3 useGenerateSpeech,
4 fetchServerSentEvents,
5 fetchHttpStream,
6} from '@tanstack/ai-react'
7import { generateSpeechFn } from '@/lib/server-functions'
8import type { TTSResult } from '@tanstack/ai'
9import type { Mode, Provider } from '@/lib/types'
10
11interface TTSUIProps {
12 provider: Provider
13 mode: Mode
14 testId?: string
15 aimockPort?: number
16}
17
18// ElevenLabs requires a voice on every TTS call. The other providers infer
19// one from their adapter config, so we only attach this when needed.
20const ELEVENLABS_DEFAULT_VOICE = '21m00Tcm4TlvDq8ikWAM'
21
22export function TTSUI({ provider, mode, testId, aimockPort }: TTSUIProps) {
23 const [text, setText] = useState('')
24 const defaultVoice =
25 provider === 'elevenlabs' ? ELEVENLABS_DEFAULT_VOICE : undefined
26
27 const connectionOptions = () => {
28 const body = { provider, testId, aimockPort }
29
30 if (mode === 'sse') {
31 return { connection: fetchServerSentEvents('/api/tts'), body }
32 }
33 if (mode === 'http-stream') {
34 return { connection: fetchHttpStream('/api/tts/stream'), body }
35 }
36 return {
37 fetcher: async (input: { text: string; voice?: string }) => {
38 return generateSpeechFn({
39 data: {
40 text: input.text,
41 voice: input.voice,
42 provider,
43 aimockPort,
44 testId,
45 },
46 }) as Promise<TTSResult>
47 },
48 }
49 }
50
51 const { generate, result, isLoading, error, status } =
52 useGenerateSpeech(connectionOptions())
53
54 return (
55 <div className="p-4 space-y-4">
56 <div className="flex gap-2">
57 <input
58 data-testid="text-input"
59 type="text"
60 value={text}
61 onChange={(e) => setText(e.target.value)}
62 placeholder="Text to speak..."
63 className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
64 />
65 <button
66 data-testid="generate-button"
67 onClick={() => generate({ text, voice: defaultVoice })}
68 disabled={!text.trim() || isLoading}
69 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
70 >
71 Generate
72 </button>
73 </div>
74 <div data-testid="generation-status">
75 {status === 'idle'
76 ? 'idle'
77 : isLoading
78 ? 'loading'
79 : error
80 ? 'error'
81 : result
82 ? 'complete'
83 : 'idle'}
84 </div>
85 {error && (
86 <div data-testid="generation-error" className="text-red-400 text-sm">
87 {error.message}
88 </div>
89 )}
90 {result && (
91 <audio
92 data-testid="generated-audio"
93 src={`data:audio/${result.format || 'mp3'};base64,${result.audio}`}
94 controls
95 />
96 )}
97 </div>
98 )
99}