import * as plugins from './plugins.js';
import { Readable } from 'stream';

export interface IOpenAiTtsOptions {
  apiKey: string;
  text: string;
  voice?: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer';
  model?: 'tts-1' | 'tts-1-hd';
  responseFormat?: 'mp3' | 'opus' | 'aac' | 'flac';
  speed?: number;
}

export async function textToSpeech(options: IOpenAiTtsOptions): Promise<NodeJS.ReadableStream> {
  const client = new plugins.OpenAI({ apiKey: options.apiKey });
  const result = await client.audio.speech.create({
    model: options.model ?? 'tts-1',
    voice: options.voice ?? 'alloy',
    input: options.text,
    response_format: options.responseFormat ?? 'mp3',
    speed: options.speed ?? 1,
  });
  const stream = result.body;
  return Readable.fromWeb(stream as any);
}
