Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { generateTranscription, toServerSentEventsResponse } from '@tanstack/ai'
3import type { TranscriptionResponseFormat } from '@tanstack/ai'
4import type { Provider } from '@/lib/types'
5import { createTranscriptionAdapter } from '@/lib/media-providers'
6
7export const Route = createFileRoute('/api/transcription')({
8 server: {
9 handlers: {
10 POST: async ({ request }) => {
11 await import('@/lib/llmock-server').then((m) => m.ensureLLMock())
12 const abortController = new AbortController()
13 const body = await request.json()
14 const data = body.forwardedProps ?? body
15 const {
16 audio,
17 language,
18 responseFormat,
19 modelOptions,
20 provider,
21 testId,
22 aimockPort,
23 } = data as {
24 audio: string
25 language?: string
26 responseFormat?: TranscriptionResponseFormat
27 modelOptions?: Record<string, any>
28 provider: Provider
29 testId?: string
30 aimockPort?: number
31 }
32
33 const adapter = createTranscriptionAdapter(
34 provider,
35 aimockPort,
36 testId,
37 { responseFormat, modelOptions },
38 )
39
40 try {
41 const stream = generateTranscription({
42 threadId: body.threadId,
43 adapter,
44 audio,
45 language,
46 responseFormat,
47 modelOptions,
48 stream: true,
49 })
50 return toServerSentEventsResponse(stream, { abortController })
51 } catch (error: any) {
52 return new Response(JSON.stringify({ error: error.message }), {
53 status: 500,
54 headers: { 'Content-Type': 'application/json' },
55 })
56 }
57 },
58 },
59 },
60})