import {z} from 'zod' export type APIVersion = 'v1'; export type DevToggles = {disable_token_validation?: boolean | undefined, disable_postgresql?: boolean | undefined, disable_mongodb?: boolean | undefined, disable_rate_limiter?: boolean | undefined, disable_concurrency_limiter?: boolean | undefined, disable_pubsub?: boolean | undefined, disable_language_detector?: boolean | undefined, disable_sentence_splitter?: boolean | undefined, disable_normalizer?: boolean | undefined, disable_phonemizer?: boolean | undefined, disable_stp?: boolean | undefined, disable_xcodec?: boolean | undefined, disable_transcript_validation?: boolean | undefined, disable_stp_short_target_standalone?: (boolean | null) | undefined}; export type AIModelVersion = 'async_flash_v1.0' | 'async_flash_v1.5' | 'async_pro_v1.0'; export type LanguageEnum = 'en' | 'fr' | 'it' | 'de' | 'es' | 'pt' | 'ar' | 'ru' | 'ro' | 'ja' | 'he' | 'hy' | 'tr' | 'hi' | 'zh' | 'cmn' | 'ur'; export type LangDetectionMode = 'llm' | 'fasttext'; export type VoiceIdSpecifier = {mode: 'id', id: string, __experimental_controls?: ({[key: string]: unknown} | null) | undefined}; export type RawOutputFormat = {container: 'raw', encoding?: ('pcm_f32le' | 'pcm_s16le' | 'pcm_mulaw') | undefined, sample_rate: number, bit_rate?: (number | null) | undefined}; export type WavOutputFormat = {container: 'wav', encoding?: ('pcm_f32le' | 'pcm_s16le') | undefined, sample_rate: number, bit_rate?: (number | null) | undefined}; export type Mp3OutputFormat = {container: 'mp3', encoding?: ('pcm_f32le' | 'pcm_s16le') | undefined, bit_rate?: number | undefined, sample_rate: number}; export type TTSRequest = {__dev_toggles?: (DevToggles | null) | undefined, model_id?: AIModelVersion | undefined, normalize?: boolean | undefined, experimental_streaming_mode?: boolean | undefined, experimental_stp_v2?: boolean | undefined, transcript: string, previous_request_id?: (string | null) | undefined, language?: (LanguageEnum | null) | undefined, lang_detection_mode?: LangDetectionMode | undefined, voice: VoiceIdSpecifier, output_format?: (RawOutputFormat | WavOutputFormat | Mp3OutputFormat) | undefined, sentence_splitter_chunk_size?: (number | null) | undefined, duration?: (number | null) | undefined, stability?: number | undefined, speed_control?: number | undefined}; export type CreateApiTextToSpeechWithSignedUrlOutputArgs = {version?: APIVersion | undefined, 'X-Api-Key'?: string | undefined, body: TTSRequest}; export type CreateApiTextToSpeechWithSignedUrlOutputOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const syncTTSWithSUAudioResponse = z.object({signed_url: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Sync Text To Speech With Signed Url Output * * POST /text_to_speech/with_signed_url_output */ export const createApiTextToSpeechWithSignedUrlOutput = async (args: CreateApiTextToSpeechWithSignedUrlOutputArgs, options: CreateApiTextToSpeechWithSignedUrlOutputOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.version != null) headers.set("version", String(args.version)) if (args['X-Api-Key'] != null) headers.set("X-Api-Key", String(args['X-Api-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/text_to_speech/with_signed_url_output${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(syncTTSWithSUAudioResponse, await res.json()) };