import {z} from 'zod' export type AllowedOutputFormats = 'mp3_22050_32' | 'mp3_24000_48' | 'mp3_44100_32' | 'mp3_44100_64' | 'mp3_44100_96' | 'mp3_44100_128' | 'mp3_44100_192' | 'pcm_8000' | 'pcm_16000' | 'pcm_22050' | 'pcm_24000' | 'pcm_32000' | 'pcm_44100' | 'pcm_48000' | 'ulaw_8000' | 'alaw_8000' | 'opus_48000_32' | 'opus_48000_64' | 'opus_48000_96' | 'opus_48000_128' | 'opus_48000_192'; export type VoiceDesignRequestModel = {voice_description: string, model_id?: ('eleven_multilingual_ttv_v2' | 'eleven_ttv_v3') | undefined, text?: (string | null) | undefined, auto_generate_text?: boolean | undefined, loudness?: number | undefined, seed?: (number | null) | undefined, guidance_scale?: number | undefined, stream_previews?: boolean | undefined, should_enhance?: boolean | undefined, remixing_session_id?: (string | null) | undefined, remixing_session_iteration_id?: (string | null) | undefined, quality?: (number | null) | undefined, reference_audio_base64?: (string | null) | undefined, prompt_strength?: (number | null) | undefined}; export type CreateApiV1TextToVoiceDesignArgs = {output_format?: AllowedOutputFormats | undefined, 'xi-api-key'?: (string | null) | undefined, body: VoiceDesignRequestModel}; export type CreateApiV1TextToVoiceDesignOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const voicePreviewResponseModel = z.object({audio_base_64: z.string(), generated_voice_id: z.string(), media_type: z.string(), duration_secs: z.number(), language: z.string().nullable()}); export const voicePreviewsResponseModel = z.object({previews: z.array(voicePreviewResponseModel), text: 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 }; /** * Design A Voice. * * POST /v1/text-to-voice/design */ export const createApiV1TextToVoiceDesign = async (args: CreateApiV1TextToVoiceDesignArgs, options: CreateApiV1TextToVoiceDesignOptions) => { 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.output_format != null) query.append("output_format", String(args.output_format)) if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/text-to-voice/design${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(voicePreviewsResponseModel, await res.json()) };