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 } =
67 useGenerateSpeech(connectionOptions())
68
69 return (
70 <div className="p-4 space-y-4">
71 <div className="flex gap-2">
72 <input
73 data-testid="text-input"
74 type="text"
75 value={text}
76 onChange={(e) => setText(e.target.value)}
77 placeholder="Text to speak..."
78 className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
79 />
80 <button
81 data-testid="generate-dialogue-button"
82 onClick={() => generate({ turns: DIALOGUE_TURNS, timestamps: true })}
83 disabled={isLoading}
84 className="px-4 py-2 bg-blue-500 text-white rounded text-sm font-medium disabled:opacity-50"
85 >
86 Generate dialogue
87 </button>
88 <button
89 data-testid="generate-button"
90 onClick={() => generate({ text, voice: defaultVoice })}
91 disabled={!text.trim() || isLoading}
92 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
93 >
94 Generate
95 </button>
96 </div>
97 <div data-testid="generation-status">
98 {status === 'idle'
99 ? 'idle'
100 : isLoading
101 ? 'loading'
102 : error
103 ? 'error'
104 : result
105 ? 'complete'
106 : 'idle'}
107 </div>
108 {error && (
109 <div data-testid="generation-error" className="text-red-400 text-sm">
110 {error.message}
111 </div>
112 )}
113 {result?.alignment && (
114 <div data-testid="alignment-unit">{result.alignment.unit}</div>
115 )}
116 {result?.alignment && (
117 <div data-testid="alignment-end">
118 {result.alignment.endSeconds.at(-1)}
119 </div>
120 )}
121 {result?.segments && (
122 <div data-testid="segment-count">{result.segments.length}</div>
123 )}
124 {result && (
125 <audio
126 data-testid="generated-audio"
127 src={`data:audio/${result.format || 'mp3'};base64,${result.audio}`}
128 controls
129 />
130 )}
131 </div>
132 )
133}