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 } = useTranscription({
79 threadId: testId ?? 'transcription',
80 ...connectionOptions(),
81 })
82
83 return (
84 <div className="p-4 space-y-4">
85 <button
86 data-testid="generate-button"
87 onClick={() => generate(transcriptionInput)}
88 disabled={isLoading}
89 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
90 >
91 Transcribe
92 </button>
93 <div data-testid="generation-status">
94 {status === 'idle'
95 ? 'idle'
96 : isLoading
97 ? 'loading'
98 : error
99 ? 'error'
100 : result
101 ? 'complete'
102 : 'idle'}
103 </div>
104 {error && (
105 <div data-testid="generation-error" className="text-red-400 text-sm">
106 {error.message}
107 </div>
108 )}
109 {result && (
110 <div className="space-y-3">
111 <p data-testid="transcription-text" className="text-gray-200">
112 {result.text}
113 </p>
114 {result.segments && result.segments.length > 0 && (
115 <div data-testid="transcription-segments" className="space-y-2">
116 {result.segments.map((segment, index) => (
117 <div key={index} className="text-sm text-gray-200">
118 {segment.speaker && (
119 <span
120 data-testid={`transcription-speaker-${index}`}
121 className="mr-2 font-semibold text-orange-300"
122 >
123 {segment.speaker}
124 </span>
125 )}
126 <span>{segment.text}</span>
127 </div>
128 ))}
129 </div>
130 )}
131 </div>
132 )}
133 </div>
134 )
135}