import {z} from 'zod' export type CreateRootRequest = {provider_item_id?: (string | null) | undefined, url?: (string | null) | undefined, s3_path?: (string | null) | undefined, resource_key?: (string | null) | undefined, shortcut_target_resource_key?: (string | null) | undefined}; export type CreateApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdRootsArgs = {kb_id: string, source_id: string, body: CreateRootRequest}; export type CreateApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdRootsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const rootResponse = z.object({id: z.string(), knowledge_base_source_id: z.string(), provider_item_id: z.string().max(255), item_type: z.string().max(50), display_name: z.string().max(500), source_url: z.string().max(1000).nullable().optional(), is_recursive: z.boolean().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Add Root * * POST /v1/knowledge-base/knowledge-bases/{kb_id}/sources/{source_id}/roots */ export const createApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdRoots = async (args: CreateApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdRootsArgs, options: CreateApiV1KnowledgeBaseKnowledgeBasesKbIdSourcesSourceIdRootsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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))}/roots${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rootResponse, await res.json()) };