import {z} from 'zod' export type T2AVoiceSetting = {speed?: number | undefined, vol?: number | undefined, pitch?: number | undefined, voice_id: string, emotion?: string | undefined, latex_read?: boolean | undefined, add_silence?: number | undefined, english_normalization?: boolean | undefined, text_normalization?: boolean | undefined}; export type T2AAudioSetting = {sample_rate?: number | undefined, bitrate?: number | undefined, format?: string | undefined, channel?: number | undefined, force_cbr?: boolean | undefined}; export type PronunciationDict = {tone?: Array | undefined, phonetic?: Array | undefined}; export type TimbreWeights = {weight?: number | undefined, voice_id?: string | undefined}; export type VoiceModify = {modify_type?: string | undefined, modify_value?: number | undefined}; export type T2AStreamOption = {include_usage?: boolean | undefined}; export type T2aV2Req = {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, voice_setting?: T2AVoiceSetting | undefined, audio_setting?: T2AAudioSetting | undefined, pronunciation_dict?: PronunciationDict | undefined, timber_weights?: Array | undefined, speed_rate?: number | undefined, stream?: boolean | undefined, language_boost?: string | undefined, subtitle_enable?: boolean | undefined, output_format?: string | undefined, voice_modify?: VoiceModify | undefined, stream_options?: T2AStreamOption | undefined, subtitle_type?: string | undefined, aigc_watermark?: boolean | undefined, with_propagator_meta?: boolean | undefined, timbre_weights?: Array | undefined}; export type CreateApiV1T2aV2Args = {body: T2aV2Req}; export type CreateApiV1T2aV2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const t2ASubtitle = z.object({text: z.string().optional(), start_time: z.number().optional(), end_time: z.number().optional()}); export const streamData = z.object({audio: z.string().optional(), status: z.number().int().optional(), ced: z.string().optional(), subtitle_file: z.string().optional(), subtitle: t2ASubtitle.optional()}); export const t2AUsage = z.object({total_characters: z.number().int().optional(), total_tokens: z.number().int().optional()}); export const t2AExtraInfo = z.object({usage: t2AUsage.optional()}); export const t2aV2Resp = z.object({data: streamData.optional(), extra_info: t2AExtraInfo.optional(), trace_id: z.string().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 V2 * * POST /v1/t2a_v2 */ export const createApiV1T2aV2 = async (args: CreateApiV1T2aV2Args, options?: CreateApiV1T2aV2Options) => { 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_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(t2aV2Resp, await res.json()) };