import {z} from 'zod' export type GetApiApiV1ContextIdDocumentsIdChunksArgs = {context_id: string, id: string, limit?: number | undefined, cursor?: string | undefined, count?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1ContextIdDocumentsIdChunksOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const chunkJson = z.object({charEnd: z.number().int(), charStart: z.number().int(), document: z.string(), id: z.string(), position: z.number().int(), section: z.string().nullable().optional(), text: z.string(), tokenCount: z.number().int().nullable().optional()}); export const pageMeta = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), totalSize: z.number().int().gte(0).nullable().optional()}); export const chunkPageJson = z.object({chunks: z.array(chunkJson), 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 chunks for a document * * GET /api/v1/{context_id}/documents/{id}/chunks */ export const getApiApiV1ContextIdDocumentsIdChunks = async (args: GetApiApiV1ContextIdDocumentsIdChunksArgs, options: GetApiApiV1ContextIdDocumentsIdChunksOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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/${encodeURIComponent(String(args.id))}/chunks${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(chunkPageJson, await res.json()) };