Open History to see the last commit
1import { BaseTTSAdapter } from '@tanstack/ai/adapters'
2import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
3import { generateId } from '@tanstack/ai-utils'
4import { resolveConfigFromEnv } from '../utils/config'
5import { outputToBase64, runModel } from '../utils/run'
6import type { TTSOptions, TTSResult } from '@tanstack/ai'
7import type { CloudflareConfig, CloudflareConfigInput } from '../utils/config'
8import type { CloudflareTTSModel } from '../utils/models'
9
10/** Text-to-speech inputs forwarded to the model (Deepgram Aura fields). */
11export interface CloudflareTTSProviderOptions {
12 speaker?: string
13 encoding?: 'linear16' | 'flac' | 'mulaw' | 'alaw' | 'mp3' | 'opus' | 'aac'
14 container?: 'none' | 'wav' | 'ogg'
15 sample_rate?: number
16 bit_rate?: number
17 [key: string]: unknown
18}
19
20const CONTENT_TYPES: Record<string, string> = {
21 mp3: 'audio/mpeg',
22 opus: 'audio/opus',
23 aac: 'audio/aac',
24 flac: 'audio/flac',
25 wav: 'audio/wav',
26 pcm: 'audio/pcm',
27}
28
29/**
30 * Cloudflare text-to-speech adapter for Workers AI models such as Deepgram
31 * Aura. `voice` maps to `speaker` and `format` to `encoding`; the audio comes
32 * back base64-encoded.
33 */
34export class CloudflareTTSAdapter<
35 TModel extends CloudflareTTSModel,
36> extends BaseTTSAdapter<TModel, CloudflareTTSProviderOptions> {
37 readonly name = 'cloudflare' as const
38
39 constructor(
40 private readonly cfConfig: CloudflareConfig,
41 model: TModel,
42 ) {
43 super(model, {})
44 }
45
46 async generateSpeech(
47 options: TTSOptions<CloudflareTTSProviderOptions>,
48 ): Promise<TTSResult> {
49 const { model, logger, text, voice, format = 'mp3' } = options
50 const inputs = {
51 ...(voice && { speaker: voice }),
52 // Aura takes the codec as `encoding` and the wrapper as `container`;
53 // `wav` and `pcm` are both linear16.
54 encoding: format === 'wav' || format === 'pcm' ? 'linear16' : format,
55 ...(format === 'wav' && { container: 'wav' }),
56 ...options.modelOptions,
57 text,
58 }
59 try {
60 logger.request(
61 `activity=tts provider=${this.name} model=${model} chars=${text.length}`,
62 { provider: this.name, model },
63 )
64 const output = await runModel(this.cfConfig, model, inputs, {
65 signal: options.abortSignal,
66 })
67 const audio =
68 output && typeof output === 'object' && 'audio' in output
69 ? (output as { audio: string }).audio
70 : await outputToBase64(output)
71 return {
72 id: generateId(this.name),
73 model,
74 audio,
75 format,
76 contentType: CONTENT_TYPES[format],
77 }
78 } catch (error: unknown) {
79 logger.errors(`${this.name}.generateSpeech fatal`, {
80 error: toRunErrorPayload(error, `${this.name}.generateSpeech failed`),
81 source: `${this.name}.generateSpeech`,
82 })
83 throw error
84 }
85 }
86}
87
88export function createCloudflareTTS<TModel extends CloudflareTTSModel>(
89 model: TModel,
90 config: CloudflareConfig,
91): CloudflareTTSAdapter<TModel> {
92 return new CloudflareTTSAdapter(config, model)
93}
94
95export function cloudflareTTS<TModel extends CloudflareTTSModel>(
96 model: TModel,
97 config?: CloudflareConfigInput,
98): CloudflareTTSAdapter<TModel> {
99 return new CloudflareTTSAdapter(resolveConfigFromEnv(config), model)
100}