import {z} from 'zod' export type SampleType = 'pretrain' | 'instruct' | 'batch_request' | 'batch_result' | 'batch_error'; export type Source = 'upload' | 'repository' | 'mistral'; export type FilePurpose = 'fine-tune' | 'batch' | 'ocr' | 'agentic_safety'; export type GetApiV1FilesArgs = {page?: number | undefined, page_size?: number | undefined, include_total?: boolean | undefined, sample_type?: (Array | null) | undefined, source?: (Array | null) | undefined, search?: (string | null) | undefined, purpose?: (FilePurpose | null) | undefined, mimetypes?: (Array | null) | undefined}; export type GetApiV1FilesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const filePurpose = z.enum(["fine-tune", "batch", "ocr", "agentic_safety"]); export const sampleType = z.enum(["pretrain", "instruct", "batch_request", "batch_result", "batch_error"]); export const source = z.enum(["upload", "repository", "mistral"]); export const fileVisibility = z.enum(["workspace", "user"]); export const fileSchema = z.object({id: z.string(), object: z.string(), bytes: z.number().int(), created_at: z.number().int(), filename: z.string(), purpose: filePurpose, sample_type: sampleType, num_lines: z.number().int().nullable().optional(), mimetype: z.string().nullable().optional(), source: source, signature: z.string().nullable().optional(), expires_at: z.number().int().nullable().optional(), visibility: fileVisibility.nullable().optional()}); export const listFilesResponse = z.object({data: z.array(fileSchema), object: z.string(), total: z.number().int().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 Files * * GET /v1/files */ export const getApiV1Files = async (args: GetApiV1FilesArgs, options?: GetApiV1FilesOptions) => { 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 != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.include_total != null) query.append("include_total", String(args.include_total)) for (const item of args.sample_type ?? []) query.append("sample_type", String(item)) for (const item of args.source ?? []) query.append("source", String(item)) if (args.search != null) query.append("search", String(args.search)) if (args.purpose != null) query.append("purpose", String(args.purpose)) for (const item of args.mimetypes ?? []) query.append("mimetypes", String(item)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/files${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(listFilesResponse, await res.json()) };