import {z} from 'zod' export type GetApiV1DefaultBanksBankIdKnowledgeBaseTreeArgs = {bank_id: string, tags?: (Array | null) | undefined, tags_match?: ('any' | 'all' | 'any_strict' | 'all_strict' | 'exact') | undefined, tag_groups?: (string | null) | undefined, authorization?: (string | null) | undefined}; export type GetApiV1DefaultBanksBankIdKnowledgeBaseTreeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const budget = z.enum(["low", "mid", "high"]); export const tagGroupLeaf = z.object({tags: z.array(z.string()), match: z.enum(["any", "all", "any_strict", "all_strict", "exact"]).optional(), resolve: z.enum(["exact", "fuzzy"]).optional()}); export const tagGroupNotOutput: z.ZodType = z.object({not: z.union([tagGroupLeaf, z.lazy(() => tagGroupAndOutput), z.lazy(() => tagGroupOrOutput), z.lazy(() => tagGroupNotOutput)])}); export const tagGroupOrOutput: z.ZodType = z.object({or: z.array(z.union([tagGroupLeaf, z.lazy(() => tagGroupAndOutput), z.lazy(() => tagGroupOrOutput), tagGroupNotOutput]))}); export const tagGroupAndOutput: z.ZodType = z.object({and: z.array(z.union([tagGroupLeaf, z.lazy(() => tagGroupAndOutput), tagGroupOrOutput, tagGroupNotOutput]))}); export const mentalModelTriggerOutput = z.object({budget: budget.nullable().optional(), mode: z.enum(["full", "delta"]).optional(), refresh_after_consolidation: z.boolean().optional(), refresh_cron: z.string().nullable().optional(), full_refresh_cron: z.string().nullable().optional(), min_refresh_interval_seconds: z.number().int().gte(0).nullable().optional(), fact_types: z.array(z.enum(["world", "experience", "observation"])).nullable().optional(), exclude_mental_models: z.boolean().optional(), exclude_mental_model_ids: z.array(z.string()).nullable().optional(), tags_match: z.enum(["any", "all", "any_strict", "all_strict", "exact"]).nullable().optional(), tag_groups: z.array(z.union([tagGroupLeaf, tagGroupAndOutput, tagGroupOrOutput, tagGroupNotOutput])).nullable().optional(), include_chunks: z.boolean().nullable().optional(), recall_max_tokens: z.number().int().nullable().optional(), recall_chunks_max_tokens: z.number().int().nullable().optional(), reflect_search_observations_max_tokens: z.number().int().gte(1).nullable().optional(), reflect_search_observations_include_entities: z.boolean().nullable().optional(), response_schema: z.record(z.string(), z.unknown()).nullable().optional(), keep_trace: z.boolean().optional()}); export const knowledgeNode: z.ZodType = z.object({id: z.string(), kind: z.enum(["folder", "page"]), name: z.string(), parent_id: z.string().nullable().optional(), mental_model_id: z.string().nullable().optional(), managed: z.boolean().optional(), description: z.string().nullable().optional(), tags: z.array(z.string()).optional(), timestamp: z.string().nullable().optional(), is_stale: z.boolean().nullable().optional(), last_refresh_failed_at: z.string().nullable().optional(), trigger: mentalModelTriggerOutput.nullable().optional(), children: z.array(z.lazy(() => knowledgeNode)).optional()}); export const knowledgeTreeResponse = z.object({roots: z.array(knowledgeNode)}); 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 the knowledge-base tree * * GET /v1/default/banks/{bank_id}/knowledge-base/tree */ export const getApiV1DefaultBanksBankIdKnowledgeBaseTree = async (args: GetApiV1DefaultBanksBankIdKnowledgeBaseTreeArgs, options: GetApiV1DefaultBanksBankIdKnowledgeBaseTreeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.tags_match != null) query.append("tags_match", String(args.tags_match)) if (args.tag_groups != null) query.append("tag_groups", String(args.tag_groups)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/default/banks/${encodeURIComponent(String(args.bank_id))}/knowledge-base/tree${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(knowledgeTreeResponse, await res.json()) };