import {z} from 'zod' export type MetadataDict = {[key: string]: unknown}; export type IPAPhoneme = 'a' | 'aɪ' | 'aʊ' | 'b' | 'd' | 'dʒ' | 'e' | 'eɪ' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'oʊ' | 'p' | 's' | 't' | 'tʃ' | 'u' | 'v' | 'w' | 'z' | 'æ' | 'ð' | 'ŋ' | 'ɑ' | 'ɔ' | 'ɔɪ' | 'ɖ' | 'ə' | 'ɚ' | 'ɛ' | 'ɜ' | 'ɪ' | 'ɱ' | 'ɹ' | 'ɾ' | 'ʃ' | 'ʈ' | 'ʊ' | 'ʋ' | 'ʌ' | 'ʒ' | 'ʔ' | 'θ'; export type SpeechOutputFormat = 'pcm' | 'wav' | 'mp3' | 'flac' | 'opus'; export type SpeechRequest = {model?: (string | null) | undefined, metadata?: (MetadataDict | null) | undefined, stream?: boolean | undefined, prompt_cache_key?: (string | null) | undefined, voice_id?: (string | null) | undefined, ref_audio?: (string | string | null) | undefined, input: string, phoneme_hints?: ({[key: string]: Array} | null) | undefined, response_format?: SpeechOutputFormat | undefined} | {[key: string]: unknown}; export type CreateApiV1AudioSpeechArgs = {body: SpeechRequest}; export type CreateApiV1AudioSpeechOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1AudioSpeechResponse = z.object({audio_data: 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 }; /** * Speech * * POST /v1/audio/speech */ export const createApiV1AudioSpeech = async (args: CreateApiV1AudioSpeechArgs, options?: CreateApiV1AudioSpeechOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/audio/speech${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(createApiV1AudioSpeechResponse, await res.json()) };