import {z} from 'zod' export type GetApiV1AdminSpendLimitOptions = {baseUrl?: string | undefined, AdminApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const usageLimits = z.object({no_monthly_limit: z.boolean().optional(), monthly_limit_reached: z.boolean(), usage: z.number().nullable().optional(), vibe_usage: z.number().nullable().optional(), total_usage: z.number().nullable().optional(), usage_limit: z.number().nullable().optional()}); export const limitsContext = z.object({completion: usageLimits, last_payment_failure: z.boolean(), last_payment_failure_protection: z.boolean().nullable(), currency: z.string()}); export const limitsOUT = z.object({limits: limitsContext}); 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 }; /** * Get Spend Limits * * GET /v1/admin/spend-limit */ export const getApiV1AdminSpendLimit = async (options?: GetApiV1AdminSpendLimitOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, AdminApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (AdminApiKey !== undefined) headers.set('authorization', `Bearer ${AdminApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/spend-limit${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(limitsOUT, await res.json()) };