Open History to see the last commit
1import {
2 useTranscription,
3 fetchServerSentEvents,
4 fetchHttpStream,
5} from '@tanstack/ai-react'
6import { generateTranscriptionFn } from '@/lib/server-functions'
7import type { TranscriptionResult } from '@tanstack/ai'
8import type { Mode, Provider } from '@/lib/types'
9
10interface TranscriptionUIProps {
11 provider: Provider
12 mode: Mode
13 testId?: string
14 aimockPort?: number
15}
16
17// Minimal silent MP3 frame encoded as base64 — aimock matches on the decoded filename
18// The adapter will decode this to a File object named "audio.mp3" for the multipart upload
19const TEST_AUDIO_BASE64 = 'data:audio/mpeg;base64,SGVsbG8='
20
21export function TranscriptionUI({
22 provider,
23 mode,
24 testId,
25 aimockPort,
26}: TranscriptionUIProps) {
27 const connectionOptions = () => {
28 const body = { provider, testId, aimockPort }
29
30 if (mode === 'sse') {
31 return { connection: fetchServerSentEvents('/api/transcription'), body }
32 }
33 if (mode === 'http-stream') {
34 return { connection: fetchHttpStream('/api/transcription/stream'), body }
35 }
36 return {
37 fetcher: async (input: { audio: string; language?: string }) => {
38 return generateTranscriptionFn({
39 data: {
40 audio: input.audio,
41 language: input.language,
42 provider,
43 aimockPort,
44 testId,
45 },
46 }) as Promise<TranscriptionResult>
47 },
48 }
49 }
50
51 const { generate, result, isLoading, error, status } =
52 useTranscription(connectionOptions())
53
54 return (
55 <div className="p-4 space-y-4">
56 <button
57 data-testid="generate-button"
58 onClick={() => generate({ audio: TEST_AUDIO_BASE64, language: 'en' })}
59 disabled={isLoading}
60 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
61 >
62 Transcribe
63 </button>
64 <div data-testid="generation-status">
65 {status === 'idle'
66 ? 'idle'
67 : isLoading
68 ? 'loading'
69 : error
70 ? 'error'
71 : result
72 ? 'complete'
73 : 'idle'}
74 </div>
75 {error && (
76 <div data-testid="generation-error" className="text-red-400 text-sm">
77 {error.message}
78 </div>
79 )}
80 {result && (
81 <p data-testid="transcription-text" className="text-gray-200">
82 {result.text}
83 </p>
84 )}
85 </div>
86 )
87}