import {z} from 'zod' export type VoiceGender = 'female' | 'male' | 'neutral'; export type GetApiV2AudioVoicesArgs = {page_size?: number | undefined, page_token?: (string | null) | undefined, type?: ('all' | 'custom' | 'preset') | undefined, gender?: (Array | null) | undefined, language?: (Array | null) | undefined, query?: (string | null) | undefined}; export type GetApiV2AudioVoicesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const voiceKind = z.enum(["ivc", "pvc"]); export const voiceResponse = z.object({name: z.string(), slug: z.string().nullable().optional(), languages: z.array(z.string()).optional(), gender: z.string().nullable().optional(), age: z.number().int().nullable().optional(), tags: z.array(z.string()).nullable().optional(), color: z.string().nullable().optional(), description: z.string().max(500).nullable().optional(), retention_notice: z.number().int().optional(), id: z.string(), created_at: z.string(), user_id: z.string().nullable(), trimmed_seconds: z.number().nullable().optional(), kind: voiceKind.optional(), type: z.enum(["preset", "custom"])}); export const voiceListPage = z.object({data: z.array(voiceResponse), next_page_token: z.string().nullable().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 }; /** * List voices, cursor-paginated * * GET /v2/audio/voices */ export const getApiV2AudioVoices = async (args: GetApiV2AudioVoicesArgs, options?: GetApiV2AudioVoicesOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.type != null) query.append("type", String(args.type)) for (const item of args.gender ?? []) query.append("gender", String(item)) for (const item of args.language ?? []) query.append("language", String(item)) if (args.query != null) query.append("query", String(args.query)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/audio/voices${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(voiceListPage, await res.json()) };