import {z} from 'zod' export type GetApiApiV0KnowledgeBasesKbIdGraphNodesArgs = {kb_id: string, tier?: ('representation' | 'domain' | null) | undefined, type_id?: (string | null) | undefined}; export type GetApiApiV0KnowledgeBasesKbIdGraphNodesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const nodeSelector = z.record(z.string(), z.unknown()); export const nodeResponse = z.object({id: z.string(), knowledge_base_id: z.string(), tier: z.enum(["representation", "domain"]), type_id: z.string(), name: z.string(), attributes: z.record(z.string(), z.unknown()), derived: z.record(z.string(), z.unknown()).optional(), selector: nodeSelector.nullable().optional(), reads_fields: z.boolean().optional(), confidence: z.number().nullable().optional(), source: z.string(), created_at: z.string(), updated_at: z.string()}); export const getApiApiV0KnowledgeBasesKbIdGraphNodesResponse = z.array(nodeResponse); 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 nodes in a KG * * GET /api/v0/knowledge-bases/{kb_id}/graph/nodes */ export const getApiApiV0KnowledgeBasesKbIdGraphNodes = async (args: GetApiApiV0KnowledgeBasesKbIdGraphNodesArgs, options: GetApiApiV0KnowledgeBasesKbIdGraphNodesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tier != null) query.append("tier", String(args.tier)) if (args.type_id != null) query.append("type_id", String(args.type_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/knowledge-bases/${encodeURIComponent(String(args.kb_id))}/graph/nodes${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(getApiApiV0KnowledgeBasesKbIdGraphNodesResponse, await res.json()) };