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, TTSTurn } 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
22// Deliberately synthetic voice ids: the dialogue mounts only check that each
23// turn carries *a* voice, and fake-but-plausible provider ids get copied into
24// docs as if they were real.
25const DIALOGUE_TURNS: Array<TTSTurn> = [
26 { text: 'welcome to the guitar store', voice: 'e2e-voice-a' },
27 { text: 'what can I play for you', voice: 'e2e-voice-b' },
28]
29
30export function TTSUI({ provider, mode, testId, aimockPort }: TTSUIProps) {
31 const [text, setText] = useState('')
32 const defaultVoice =
33 provider === 'elevenlabs' ? ELEVENLABS_DEFAULT_VOICE : undefined
34
35 const connectionOptions = () => {
36 const body = { provider, testId, aimockPort }
37
38 if (mode === 'sse') {
39 return { connection: fetchServerSentEvents('/api/tts'), body }
40 }
41 if (mode === 'http-stream') {
42 return { connection: fetchHttpStream('/api/tts/stream'), body }
43 }
44 return {
45 fetcher: async (input: {
46 text?: string
47 turns?: Array<TTSTurn>
48 timestamps?: boolean
49 voice?: string
50 }) => {
51 return generateSpeechFn({
52 data: {
53 text: input.text,
54 turns: input.turns,
55 timestamps: input.timestamps,
56 voice: input.voice,
57 provider,
58 aimockPort,
59 testId,
60 },
61 }) as Promise<TTSResult>
62 },
63 }
64 }
65
66 const { generate, result, isLoading, error, status } = useGenerateSpeech({
67 threadId: testId ?? 'tts',
68 ...connectionOptions(),
69 })
70
71 return (
72 <div className="p-4 space-y-4">
73 <div className="flex gap-2">
74 <input
75 data-testid="text-input"
76 type="text"
77 value={text}
78 onChange={(e) => setText(e.target.value)}
79 placeholder="Text to speak..."
80 className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
81 />
82 <button
83 data-testid="generate-dialogue-button"
84 onClick={() => generate({ turns: DIALOGUE_TURNS, timestamps: true })}
85 disabled={isLoading}
86 className="px-4 py-2 bg-blue-500 text-white rounded text-sm font-medium disabled:opacity-50"
87 >
88 Generate dialogue
89 </button>
90 <button
91 data-testid="generate-button"
92 onClick={() => generate({ text, voice: defaultVoice })}
93 disabled={!text.trim() || isLoading}
94 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
95 >
96 Generate
97 </button>
98 </div>
99 <div data-testid="generation-status">
100 {status === 'idle'
101 ? 'idle'
102 : isLoading
103 ? 'loading'
104 : error
105 ? 'error'
106 : result
107 ? 'complete'
108 : 'idle'}
109 </div>
110 {error && (
111 <div data-testid="generation-error" className="text-red-400 text-sm">
112 {error.message}
113 </div>
114 )}
115 {result?.alignment && (
116 <div data-testid="alignment-unit">{result.alignment.unit}</div>
117 )}
118 {result?.alignment && (
119 <div data-testid="alignment-end">
120 {result.alignment.endSeconds.at(-1)}
121 </div>
122 )}
123 {result?.segments && (
124 <div data-testid="segment-count">{result.segments.length}</div>
125 )}
126 {result && (
127 <audio
128 data-testid="generated-audio"
129 src={`data:audio/${result.format || 'mp3'};base64,${result.audio}`}
130 controls
131 />
132 )}
133 </div>
134 )
135}