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 { TranscriptionGenerateInput } from '@tanstack/ai-client'
9import type { Feature, Mode, Provider } from '@/lib/types'
10
11type TranscriptionFeature = Extract<
12 Feature,
13 'transcription' | 'transcription-diarization'
14>
15
16interface TranscriptionUIProps {
17 provider: Provider
18 feature: TranscriptionFeature
19 mode: Mode
20 testId?: string
21 aimockPort?: number
22}
23
24// Minimal silent MP3 frame encoded as base64 — aimock matches on the decoded filename
25// The adapter will decode this to a File object named "audio.mp3" for the multipart upload
26const TEST_AUDIO_BASE64 = 'data:audio/mpeg;base64,SGVsbG8='
27
28export function TranscriptionUI({
29 provider,
30 feature,
31 mode,
32 testId,
33 aimockPort,
34}: TranscriptionUIProps) {
35 const isDiarization = feature === 'transcription-diarization'
36 const transcriptionInput: TranscriptionGenerateInput = {
37 audio: TEST_AUDIO_BASE64,
38 language: 'en',
39 ...(isDiarization
40 ? {
41 modelOptions: {
42 response_format: 'diarized_json',
43 chunking_strategy: 'auto',
44 known_speaker_names: ['agent', 'customer'],
45 known_speaker_references: [TEST_AUDIO_BASE64, TEST_AUDIO_BASE64],
46 },
47 }
48 : {}),
49 }
50
51 const connectionOptions = () => {
52 const body = { provider, feature, testId, aimockPort }
53
54 if (mode === 'sse') {
55 return { connection: fetchServerSentEvents('/api/transcription'), body }
56 }
57 if (mode === 'http-stream') {
58 return { connection: fetchHttpStream('/api/transcription/stream'), body }
59 }
60 return {
61 fetcher: async (input: TranscriptionGenerateInput) => {
62 return generateTranscriptionFn({
63 data: {
64 audio: input.audio as string,
65 language: input.language,
66 responseFormat: input.responseFormat,
67 modelOptions: input.modelOptions,
68 provider,
69 feature,
70 aimockPort,
71 testId,
72 },
73 }) as Promise<TranscriptionResult>
74 },
75 }
76 }
77
78 const { generate, result, isLoading, error, status } =
79 useTranscription(connectionOptions())
80
81 return (
82 <div className="p-4 space-y-4">
83 <button
84 data-testid="generate-button"
85 onClick={() => generate(transcriptionInput)}
86 disabled={isLoading}
87 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
88 >
89 Transcribe
90 </button>
91 <div data-testid="generation-status">
92 {status === 'idle'
93 ? 'idle'
94 : isLoading
95 ? 'loading'
96 : error
97 ? 'error'
98 : result
99 ? 'complete'
100 : 'idle'}
101 </div>
102 {error && (
103 <div data-testid="generation-error" className="text-red-400 text-sm">
104 {error.message}
105 </div>
106 )}
107 {result && (
108 <div className="space-y-3">
109 <p data-testid="transcription-text" className="text-gray-200">
110 {result.text}
111 </p>
112 {result.segments && result.segments.length > 0 && (
113 <div data-testid="transcription-segments" className="space-y-2">
114 {result.segments.map((segment, index) => (
115 <div key={index} className="text-sm text-gray-200">
116 {segment.speaker && (
117 <span
118 data-testid={`transcription-speaker-${index}`}
119 className="mr-2 font-semibold text-orange-300"
120 >
121 {segment.speaker}
122 </span>
123 )}
124 <span>{segment.text}</span>
125 </div>
126 ))}
127 </div>
128 )}
129 </div>
130 )}
131 </div>
132 )
133}