UNPKG

@tanstack/ai

Version:

Core TanStack AI library - Open source AI SDK

83 lines (82 loc) 3.1 kB
import { TTSAdapter } from './adapter.js'; import { StreamChunk, TTSResult } from '../../types.js'; /** The adapter kind this activity handles */ export declare const kind: "tts"; /** * Extract provider options from a TTSAdapter via ~types. */ export type TTSProviderOptions<TAdapter> = TAdapter extends TTSAdapter<any, any> ? TAdapter['~types']['providerOptions'] : object; /** * Options for the TTS activity. * The model is extracted from the adapter's model property. * * @template TAdapter - The TTS adapter type * @template TStream - Whether to stream the output */ export interface TTSActivityOptions<TAdapter extends TTSAdapter<string, object>, TStream extends boolean = false> { /** The TTS adapter to use (must be created with a model) */ adapter: TAdapter & { kind: typeof kind; }; /** The text to convert to speech */ text: string; /** The voice to use for generation */ voice?: string; /** The output audio format */ format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm'; /** The speed of the generated audio (0.25 to 4.0) */ speed?: number; /** Provider-specific options for TTS generation */ modelOptions?: TTSProviderOptions<TAdapter>; /** * Whether to stream the generation result. * When true, returns an AsyncIterable<StreamChunk> for streaming transport. * When false or not provided, returns a Promise<TTSResult>. * * @default false */ stream?: TStream; } /** * Result type for the TTS activity. * - If stream is true: AsyncIterable<StreamChunk> * - Otherwise: Promise<TTSResult> */ export type TTSActivityResult<TStream extends boolean = false> = TStream extends true ? AsyncIterable<StreamChunk> : Promise<TTSResult>; /** * TTS activity - generates speech from text. * * Uses AI text-to-speech models to create audio from natural language text. * * @example Generate speech from text * ```ts * import { generateSpeech } from '@tanstack/ai' * import { openaiTTS } from '@tanstack/ai-openai' * * const result = await generateSpeech({ * adapter: openaiTTS('tts-1-hd'), * text: 'Hello, welcome to TanStack AI!', * voice: 'nova' * }) * * console.log(result.audio) // base64-encoded audio * ``` * * @example With format and speed options * ```ts * const result = await generateSpeech({ * adapter: openaiTTS('tts-1'), * text: 'This is slower speech.', * voice: 'alloy', * format: 'wav', * speed: 0.8 * }) * ``` */ export declare function generateSpeech<TAdapter extends TTSAdapter<string, object>, TStream extends boolean = false>(options: TTSActivityOptions<TAdapter, TStream>): TTSActivityResult<TStream>; /** * Create typed options for the generateSpeech() function without executing. */ export declare function createSpeechOptions<TAdapter extends TTSAdapter<string, object>, TStream extends boolean = false>(options: TTSActivityOptions<TAdapter, TStream>): TTSActivityOptions<TAdapter, TStream>; export type { TTSAdapter, TTSAdapterConfig, AnyTTSAdapter } from './adapter.js'; export { BaseTTSAdapter } from './adapter.js';