import {z} from 'zod' export type GetApiV1LibrariesLibraryIdDocumentsArgs = {library_id: string, search?: (string | null) | undefined, page_size?: number | undefined, page?: number | undefined, filters_attributes?: (string | null) | undefined, sort_by?: string | undefined, sort_order?: string | undefined}; export type GetApiV1LibrariesLibraryIdDocumentsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({total_items: z.number().int(), total_pages: z.number().int(), current_page: z.number().int(), page_size: z.number().int(), has_more: z.boolean()}); 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 listDocumentsResponse = z.object({pagination: paginationInfo, data: z.array(document)}); 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 documents in a given library. * * GET /v1/libraries/{library_id}/documents */ export const getApiV1LibrariesLibraryIdDocuments = async (args: GetApiV1LibrariesLibraryIdDocumentsArgs, options?: GetApiV1LibrariesLibraryIdDocumentsOptions) => { 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.search != null) query.append("search", String(args.search)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (args.filters_attributes != null) query.append("filters_attributes", String(args.filters_attributes)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listDocumentsResponse, await res.json()) };