Open History to see the last commit
1import OpenAI from 'openai'
2import { BaseTTSAdapter } from '@tanstack/ai/adapters'
3import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
4import { arrayBufferToBase64, generateId } from '@tanstack/ai-utils'
5import {
6 getLovableApiKeyFromEnv,
7 openaiRequestOptions,
8 withLovableDefaults,
9} from '../utils/client'
10import type { TTSOptions, TTSResult } from '@tanstack/ai'
11import type OpenAI_SDK from 'openai'
12import type { LovableTTSModel } from '../model-meta'
13import type { LovableTTSProviderOptions } from '../audio/tts-provider-options'
14import type { LovableClientConfig } from '../utils/client'
15
16export interface LovableTTSConfig extends LovableClientConfig {}
17
18const CONTENT_TYPES: Record<string, string> = {
19 mp3: 'audio/mpeg',
20 opus: 'audio/opus',
21 aac: 'audio/aac',
22 flac: 'audio/flac',
23 wav: 'audio/wav',
24 pcm: 'audio/pcm',
25}
26
27export class LovableTTSAdapter<
28 TModel extends LovableTTSModel,
29> extends BaseTTSAdapter<TModel, LovableTTSProviderOptions> {
30 readonly name = 'lovable' as const
31
32 protected client: OpenAI
33
34 constructor(config: LovableTTSConfig, model: TModel) {
35 super(model, {})
36 this.client = new OpenAI(withLovableDefaults(config))
37 }
38
39 async generateSpeech(
40 options: TTSOptions<LovableTTSProviderOptions>,
41 ): Promise<TTSResult> {
42 const { model, text, voice, format, speed, modelOptions } = options
43
44 const request: OpenAI_SDK.Audio.SpeechCreateParams = {
45 model,
46 input: text,
47 voice: voice || 'alloy',
48 response_format: format,
49 ...(speed !== undefined && { speed }),
50 ...(modelOptions ?? {}),
51 }
52
53 try {
54 options.logger.request(
55 `activity=tts provider=${this.name} model=${model} format=${request.response_format ?? 'default'} voice=${request.voice}`,
56 { provider: this.name, model },
57 )
58 const response = await this.client.audio.speech.create(
59 request,
60 openaiRequestOptions(options.abortSignal),
61 )
62 const arrayBuffer = await response.arrayBuffer()
63 const base64 = arrayBufferToBase64(arrayBuffer)
64 const outputFormat = (request.response_format as string) || 'mp3'
65 const contentType = CONTENT_TYPES[outputFormat] || 'audio/mpeg'
66
67 return {
68 id: generateId(this.name),
69 model,
70 audio: base64,
71 format: outputFormat,
72 contentType,
73 }
74 } catch (error: unknown) {
75 options.logger.errors(`${this.name}.generateSpeech fatal`, {
76 error: toRunErrorPayload(error, `${this.name}.generateSpeech failed`),
77 source: `${this.name}.generateSpeech`,
78 })
79 throw error
80 }
81 }
82}
83
84export function createLovableSpeech<TModel extends LovableTTSModel>(
85 model: TModel,
86 apiKey: string,
87 config?: Omit<LovableTTSConfig, 'apiKey'>,
88): LovableTTSAdapter<TModel> {
89 return new LovableTTSAdapter({ apiKey, ...config }, model)
90}
91
92export function lovableSpeech<TModel extends LovableTTSModel>(
93 model: TModel,
94 config?: Omit<LovableTTSConfig, 'apiKey'>,
95): LovableTTSAdapter<TModel> {
96 return createLovableSpeech(model, getLovableApiKeyFromEnv(), config)
97}