import {z} from 'zod' export type ParseMethod = 'default' | 'pypdf' | 'pdfplumber' | 'unstructured' | 'pymupdf' | 'deepdoc'; export type ChunkStrategy = 'recursive' | 'fixed_size' | 'markdown'; export type BodyIngestWebApiKbIngestWebPost = {collection: string, start_url: string, max_pages?: (number | null) | undefined, max_depth?: (number | null) | undefined, url_patterns?: (string | null) | undefined, exclude_patterns?: (string | null) | undefined, same_domain_only?: (boolean | null) | undefined, content_selector?: (string | null) | undefined, remove_selectors?: (string | null) | undefined, concurrent_requests?: (number | null) | undefined, request_delay?: (number | null) | undefined, timeout?: (number | null) | undefined, respect_robots_txt?: (boolean | null) | undefined, parse_method?: (ParseMethod | null) | undefined, chunk_strategy?: (ChunkStrategy | null) | undefined, chunk_size?: (number | null) | undefined, chunk_overlap?: (number | null) | undefined, separators?: (string | null) | undefined, embedding_model_id?: string | undefined, embedding_batch_size?: (number | null) | undefined, max_retries?: (number | null) | undefined, retry_delay?: (number | null) | undefined}; export type CreateApiApiKbIngestWebArgs = {body: BodyIngestWebApiKbIngestWebPost}; export type CreateApiApiKbIngestWebOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webIngestionResult = z.object({status: z.string(), collection: z.string(), total_urls_found: z.number().int().gte(0), pages_crawled: z.number().int().gte(0), pages_failed: z.number().int().gte(0), documents_created: z.number().int().gte(0), chunks_created: z.number().int().gte(0), embeddings_created: z.number().int().gte(0), crawled_urls: z.array(z.string()).optional(), failed_urls: z.record(z.string(), z.string()).optional(), message: z.string(), warnings: z.array(z.string()).optional(), side_effects_may_remain: z.boolean().optional(), elapsed_time_ms: z.number().int().gte(0)}); 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 }; /** * Ingest Web * * POST /api/kb/ingest-web */ export const createApiApiKbIngestWeb = async (args: CreateApiApiKbIngestWebArgs, options: CreateApiApiKbIngestWebOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/kb/ingest-web${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webIngestionResult, await res.json()) };