import {z} from 'zod' export type T2AAsyncV2VoiceSetting = {voice_id: string, speed?: number | undefined, vol?: number | undefined, pitch?: number | undefined, emotion?: string | undefined, english_normalization?: boolean | undefined, add_silence?: number | undefined, text_normalization?: boolean | undefined}; export type Channel = 'Mono' | 'Binaural'; export type T2AAsyncV2AudioSetting = {audio_sample_rate?: number | undefined, bitrate?: number | undefined, format?: string | undefined, channel?: Channel | undefined, force_cbr?: boolean | undefined}; export type T2AAsyncV2PronunciationDict = {tone?: Array | undefined}; export type VoiceModify = {modify_type?: string | undefined, modify_value?: number | undefined}; export type T2AAsyncV2Req = {model: 'speech-2.5-hd-preview' | 'speech-2.5-turbo-preview' | 'speech-02-hd' | 'speech-02-turbo' | 'speech-01-hd' | 'speech-01-turbo', text?: string | undefined, text_file_id?: number | undefined, voice_setting: T2AAsyncV2VoiceSetting, audio_setting?: T2AAsyncV2AudioSetting | undefined, pronunciation_dict?: T2AAsyncV2PronunciationDict | undefined, language_boost?: string | undefined, callback_url?: string | undefined, voice_modify?: VoiceModify | undefined, aigc_watermark?: boolean | undefined}; export type CreateApiV1T2aAsyncV2Args = {body: T2AAsyncV2Req}; export type CreateApiV1T2aAsyncV2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const t2AAsyncV2Resp = z.object({task_id: z.number().int().optional(), task_token: z.string().optional(), file_id: z.number().int().optional(), usage_characters: z.number().int().optional()}); 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 }; /** * Text to Audio Async V2 * * POST /v1/t2a_async_v2 */ export const createApiV1T2aAsyncV2 = async (args: CreateApiV1T2aAsyncV2Args, options?: CreateApiV1T2aAsyncV2Options) => { const {baseUrl = "https://api.minimax.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/t2a_async_v2${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(t2AAsyncV2Resp, await res.json()) };