import {z} from 'zod' export type GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdPagesArgs = {kb_id: string, source_id: string, page_size?: number | undefined, start_cursor?: (string | null) | undefined}; export type GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdPagesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const browsePageItem = z.object({provider_item_id: z.string().max(255), title: z.string().max(500), icon: z.record(z.string(), z.unknown()).nullable().optional(), url: z.string().max(1000).nullable().optional(), parent_type: z.string().max(50).nullable().optional(), parent_title: z.string().max(500).nullable().optional(), last_edited_time: z.string().nullable().optional()}); export const browseResponse = z.object({items: z.array(browsePageItem), next_cursor: z.string().nullable().optional(), has_more: z.boolean().optional()}); 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 }; /** * Browse Pages * * GET /v1/knowledge-base/knowledge-bases/{kb_id}/sources/{source_id}/pages */ export const getApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdPages = async (args: GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdPagesArgs, options: GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdPagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.start_cursor != null) query.append("start_cursor", String(args.start_cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/knowledge-base/knowledge-bases/${encodeURIComponent(String(args.kb_id))}/sources/${encodeURIComponent(String(args.source_id))}/pages${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(browseResponse, await res.json()) };