import {z} from 'zod' export type GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesArgs = {kb_id: string, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceResponse = z.object({id: z.string(), knowledge_base_id: z.string(), provider: z.string().optional(), integration_connection_id: z.string().nullable().optional(), config: z.record(z.string(), z.unknown()).nullable().optional(), status: z.string(), last_sync_job_id: z.string().nullable().optional(), integration_key: z.string().nullable().optional(), connection_nickname: z.string().nullable().optional(), connection_metadata: z.record(z.string(), z.unknown()).optional(), auto_sync_enabled: z.boolean().optional(), sync_interval_seconds: z.number().int().nullable().optional(), next_sync_at: z.string().nullable().optional(), last_auto_sync_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const cursorPageSourceResponse = z.object({items: z.array(sourceResponse), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 Sources * * GET /v1/knowledge-base/knowledge-bases/{kb_id}/sources */ export const getApiV1KnowledgeBaseKnowledgeBasesKbIdSources = async (args: GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesArgs, options: GetApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = 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 (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/knowledge-base/knowledge-bases/${encodeURIComponent(String(args.kb_id))}/sources${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(cursorPageSourceResponse, await res.json()) };