import {z} from 'zod' export type CreateApiV1LibrariesLibraryIdDocumentsArgs = {library_id: string, body: BodyInit}; export type CreateApiV1LibrariesLibraryIdDocumentsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const processStatus = z.enum(["self_managed", "missing_content", "noop", "done", "todo", "in_progress", "error", "waiting_for_capacity"]); export const document = z.object({id: z.string(), library_id: z.string(), hash: z.string().nullable(), mime_type: z.string().nullable(), extension: z.string().nullable(), size: z.number().int().nullable(), name: z.string(), summary: z.string().nullable().optional(), created_at: z.string(), last_processed_at: z.string().nullable().optional(), number_of_pages: z.number().int().nullable().optional(), process_status: processStatus, uploaded_by_id: z.string().nullable(), uploaded_by_type: z.string(), tokens_processing_main_content: z.number().int().nullable().optional(), tokens_processing_summary: z.number().int().nullable().optional(), url: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).nullable().optional(), expires_at: z.string().nullable().optional(), processing_status: z.string(), tokens_processing_total: z.number().int()}); 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 }; /** * Upload a new document. * * POST /v1/libraries/{library_id}/documents */ export const createApiV1LibrariesLibraryIdDocuments = async (args: CreateApiV1LibrariesLibraryIdDocumentsArgs, options?: CreateApiV1LibrariesLibraryIdDocumentsOptions) => { 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/libraries/${encodeURIComponent(String(args.library_id))}/documents${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(document, await res.json()) };