Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.transcription.ts

59 lines1.8 KB

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.data ?? 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 adapter,
43 audio,
44 language,
45 responseFormat,
46 modelOptions,
47 stream: true,
48 })
49 return toServerSentEventsResponse(stream, { abortController })
50 } catch (error: any) {
51 return new Response(JSON.stringify({ error: error.message }), {
52 status: 500,
53 headers: { 'Content-Type': 'application/json' },
54 })
55 }
56 },
57 },
58 },
59})