import {z} from 'zod' export type ApiZone = 'global' | 'us' | 'eu'; export type GetApiV1AdminUsageArgs = {month?: (string | null) | undefined, year?: (string | null) | undefined, workspace_id?: (string | null) | undefined, api_zone?: (ApiZone | null) | undefined}; export type GetApiV1AdminUsageOptions = {baseUrl?: string | undefined, AdminApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const basicModelUsageDataJSON = z.object({models: z.record(z.string(), z.record(z.string(), z.array(z.record(z.string(), z.unknown()))))}); export const librariesAPIUsageDataJSON = z.object({pages: basicModelUsageDataJSON, tokens: basicModelUsageDataJSON, audio_seconds: basicModelUsageDataJSON}); export const fineTuningDataJSON = z.object({training: z.record(z.string(), z.record(z.string(), z.array(z.record(z.string(), z.unknown())))), storage: z.record(z.string(), z.number().int())}); export const vibeCodeUsageDataJSON = z.object({completion: basicModelUsageDataJSON, ocr: basicModelUsageDataJSON, connectors: basicModelUsageDataJSON, audio: basicModelUsageDataJSON, audio_characters: basicModelUsageDataJSON}); export const lagoEventType = z.enum(["api_tokens", "api_pages", "api_audio_seconds", "api_audio_characters", "api_connectors", "api_libraries_tokens", "api_libraries_pages", "api_libraries_audio", "deployment_tokens", "gpu_hour", "reserved_instance", "vibe_tokens", "vibe_connectors", "vibe_pages", "vibe_audio_seconds", "vibe_audio_characters"]); export const apiZone = z.enum(["global", "us", "eu"]); export const serviceTier = z.enum(["standard", "priority"]); export const priceData = z.object({event_type: lagoEventType.nullable(), billing_metric: z.string(), billing_group: z.string(), api_zone: apiZone, service_tier: serviceTier, price: z.union([z.number(), z.string()])}); export const usageOUTJSON = z.object({completion: basicModelUsageDataJSON, ocr: basicModelUsageDataJSON, connectors: basicModelUsageDataJSON, libraries_api: librariesAPIUsageDataJSON, fine_tuning: fineTuningDataJSON, audio: basicModelUsageDataJSON, audio_characters: basicModelUsageDataJSON, audio_agents: basicModelUsageDataJSON, vibe_usage: z.number(), vibe_code: vibeCodeUsageDataJSON, chat: basicModelUsageDataJSON, date: z.string(), previous_month: z.string().nullable(), next_month: z.string().nullable(), start_date: z.string(), end_date: z.string(), currency: z.string().nullable(), currency_symbol: z.string().nullable(), prices: z.array(priceData).nullable()}); 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 }; /** * Monthly Usage and Pricing * * GET /v1/admin/usage */ export const getApiV1AdminUsage = async (args: GetApiV1AdminUsageArgs, options?: GetApiV1AdminUsageOptions) => { 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 (args.month != null) query.append("month", String(args.month)) if (args.year != null) query.append("year", String(args.year)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args.api_zone != null) query.append("api_zone", String(args.api_zone)) if (AdminApiKey !== undefined) headers.set('authorization', `Bearer ${AdminApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/usage${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(usageOUTJSON, await res.json()) };