import {z} from 'zod' export type GetApiApiV1ContextIdDocumentsArgs = {context_id: string, status?: string | undefined, mimeType?: string | undefined, limit?: number | undefined, cursor?: string | undefined, count?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1ContextIdDocumentsOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const documentStatus = z.enum(["queued", "extracting", "chunking", "embedding", "keywording", "extracting_nodes", "ready", "failed"]); export const documentJson = z.object({contentHash: z.string(), createdAt: z.string(), error: z.string().nullable().optional(), id: z.string(), mimeType: z.string(), observedAt: z.string().nullable().optional(), processingCompletedAt: z.string().nullable().optional(), processingStartedAt: z.string().nullable().optional(), sizeBytes: z.number().int(), source: z.string(), status: documentStatus, title: z.string(), updatedAt: z.string(), version: z.number().int()}); export const pageMeta = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), totalSize: z.number().int().gte(0).nullable().optional()}); export const documentPageJson = z.object({documents: z.array(documentJson), page: pageMeta}); 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 }; /** * Lists Layer 0 knowledge documents * * GET /api/v1/{context_id}/documents */ export const getApiApiV1ContextIdDocuments = async (args: GetApiApiV1ContextIdDocumentsArgs, options: GetApiApiV1ContextIdDocumentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.mimeType != null) query.append("mimeType", String(args.mimeType)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.count != null) query.append("count", String(args.count)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.context_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(documentPageJson, await res.json()) };