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

server-audio-adapters.ts

168 lines5.5 KB

1/**
2 * Server-side adapter factories for the audio example pages.
3 *
4 * Keeping these in one place lets the HTTP routes and the TanStack Start server
5 * functions share the same model choices without duplicating provider wiring.
6 */
7​
8import { openaiSpeech, openaiTranscription } from '@tanstack/ai-openai'
9import { geminiAudio, geminiSpeech } from '@tanstack/ai-gemini'
10import { falAudio, falSpeech, falTranscription } from '@tanstack/ai-fal'
11import {
12 elevenlabsAudio,
13 elevenlabsSpeech,
14 elevenlabsTranscription,
15} from '@tanstack/ai-elevenlabs'
16import { grokSpeech, grokTranscription } from '@tanstack/ai-grok'
17import { byteplusSpeech, byteplusTranscription } from '@tanstack/ai-byteplus'
18import type {
19 AnyAudioAdapter,
20 AnyTranscriptionAdapter,
21 AnyTTSAdapter,
22} from '@tanstack/ai'
23import {
24 AUDIO_PROVIDERS,
25 SPEECH_PROVIDERS,
26 TRANSCRIPTION_PROVIDERS,
27 type AudioProviderId,
28 type SpeechProviderId,
29 type TranscriptionProviderId,
30} from './audio-providers'
31​
32function findConfig<T extends { id: string }>(
33 list: ReadonlyArray<T>,
34 id: string,
35): T {
36 const match = list.find((entry) => entry.id === id)
37 if (!match) {
38 throw new UnknownProviderError(
39 id,
40 list.map((entry) => entry.id),
41 )
42 }
43 return match
45​
46export function buildSpeechAdapter(provider: SpeechProviderId): AnyTTSAdapter {
47 const config = findConfig(SPEECH_PROVIDERS, provider)
48 switch (config.id) {
49 case 'openai':
50 return openaiSpeech(config.model as 'tts-1')
51 case 'gemini':
52 return geminiSpeech(config.model as 'gemini-2.5-flash-preview-tts')
53 case 'fal':
54 return falSpeech(config.model)
55 case 'grok':
56 return grokSpeech(config.model as 'grok-tts')
57 case 'elevenlabs':
58 return elevenlabsSpeech(config.model as 'eleven_multilingual_v2')
59 case 'byteplus':
60 // Seed Speech TTS authenticates with BYTEPLUS_VOICE_API_KEY — a
61 // different product (and key) from the ARK_API_KEY used for chat.
62 return byteplusSpeech(config.model as 'seed-audio-1.0')
63 }
65​
66export function buildTranscriptionAdapter(
67 provider: TranscriptionProviderId,
68): AnyTranscriptionAdapter {
69 const config = findConfig(TRANSCRIPTION_PROVIDERS, provider)
70 switch (config.id) {
71 case 'openai':
72 return openaiTranscription(config.model as 'whisper-1')
73 case 'openai-diarize':
74 return openaiTranscription(config.model as 'gpt-4o-transcribe-diarize')
75 case 'fal':
76 return falTranscription(config.model)
77 case 'grok':
78 return grokTranscription(config.model as 'grok-stt')
79 case 'elevenlabs':
80 return elevenlabsTranscription(config.model as 'scribe_v1')
81 case 'byteplus':
82 // Seed Speech ASR shares the BYTEPLUS_VOICE_API_KEY key with TTS.
83 return byteplusTranscription(config.model as 'seed-asr')
84 }
86​
87export function buildAudioAdapter(
88 provider: AudioProviderId,
89 modelOverride?: string,
90): AnyAudioAdapter {
91 const config = findConfig(AUDIO_PROVIDERS, provider)
92 const model = resolveModel(config, modelOverride)
93 switch (config.id) {
94 case 'gemini-lyria':
95 return geminiAudio(
96 model as 'lyria-3-clip-preview' | 'lyria-3-pro-preview',
97 )
98 case 'fal-audio':
99 case 'fal-sfx':
100 return falAudio(model)
101 case 'elevenlabs-music':
102 case 'elevenlabs-sfx':
103 return elevenlabsAudio(model as 'music_v1')
104 }
106​
107/**
108 * Thrown when a caller supplies a `modelOverride` that is not present in the
109 * provider's allowed model list. HTTP routes map this to a 400 response so the
110 * user sees a clear rejection instead of silently getting output from the
111 * default model.
112 */
113export class InvalidModelOverrideError extends Error {
114 readonly code = 'invalid_model_override' as const
115 readonly providerId: string
116 readonly requestedModel: string
117 readonly allowedModels: ReadonlyArray<string>
118​
119 constructor(
120 providerId: string,
121 requestedModel: string,
122 allowedModels: ReadonlyArray<string>,
123 ) {
124 super(
125 `Invalid model override "${requestedModel}" for provider "${providerId}". Allowed models: ${
126 allowedModels.length > 0 ? allowedModels.join(', ') : '(none)'
127 }`,
128 )
129 this.name = 'InvalidModelOverrideError'
130 this.providerId = providerId
131 this.requestedModel = requestedModel
132 this.allowedModels = allowedModels
133 }
135​
136/**
137 * Thrown when `findConfig` is called with a provider id that isn't in the
138 * allowed list. In practice the route-level Zod enum schema already rejects
139 * unknown providers before we ever reach this builder, so this is
140 * defense-in-depth for callers that bypass Zod validation (e.g. server-fns
141 * whose input schemas could drift from the provider registries).
142 */
143export class UnknownProviderError extends Error {
144 readonly code = 'unknown_provider' as const
145 readonly providerId: string
146 readonly allowedProviders: ReadonlyArray<string>
147​
148 constructor(providerId: string, allowedProviders: ReadonlyArray<string>) {
149 super(
150 `Unknown provider "${providerId}". Allowed providers: ${
151 allowedProviders.length > 0 ? allowedProviders.join(', ') : '(none)'
152 }`,
153 )
154 this.name = 'UnknownProviderError'
155 this.providerId = providerId
156 this.allowedProviders = allowedProviders
157 }
159​
160function resolveModel(
161 config: (typeof AUDIO_PROVIDERS)[number],
162 modelOverride: string | undefined,
163): string {
164 if (!modelOverride) return config.model
165 const allowedModels = config.models?.map((m) => m.id) ?? []
166 if (allowedModels.includes(modelOverride)) return modelOverride
167 throw new InvalidModelOverrideError(config.id, modelOverride, allowedModels)