import {z} from 'zod' export type GetApiV1FilesFileIdArgs = {file_id: string}; export type GetApiV1FilesFileIdOptions = {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 getFileResponse = 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(), deleted: z.boolean()}); 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 }; /** * Retrieve File * * GET /v1/files/{file_id} */ export const getApiV1FilesFileId = async (args: GetApiV1FilesFileIdArgs, options?: GetApiV1FilesFileIdOptions) => { 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 (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/files/${encodeURIComponent(String(args.file_id))}${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(getFileResponse, await res.json()) };