import {z} from 'zod' export type GetApiV1KnowledgeBaseDocumentsDocIdArgs = {doc_id: string}; export type GetApiV1KnowledgeBaseDocumentsDocIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const documentResponse = z.object({id: z.string(), knowledge_base_id: z.string(), organization_id: z.string(), title: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), created_at: z.string(), updated_at: z.string(), active_version_id: z.string().nullable().optional(), version_number: z.number().int().nullable().optional(), state: z.string().nullable().optional(), scan_status: z.string().nullable().optional(), source_type: z.string().nullable().optional(), chunk_count: z.number().int().nullable().optional(), token_count: z.number().int().nullable().optional(), segment_count: z.number().int().nullable().optional(), version_count: z.number().int().optional(), download_url: z.string().nullable().optional(), normalized_download_url: z.string().nullable().optional(), sensitivity: z.string().optional(), sensitivity_level: z.number().int().optional(), pii_status: z.string().nullable().optional(), safe_title: z.string().nullable().optional(), pii_sensitivity: z.string().nullable().optional(), pii_coverage_status: z.string().nullable().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 }; /** * Get a document * * GET /v1/knowledge-base/documents/{doc_id} */ export const getApiV1KnowledgeBaseDocumentsDocId = async (args: GetApiV1KnowledgeBaseDocumentsDocIdArgs, options: GetApiV1KnowledgeBaseDocumentsDocIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/knowledge-base/documents/${encodeURIComponent(String(args.doc_id))}${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(documentResponse, await res.json()) };