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

server-audio-adapters.ts

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