import {z} from 'zod' export type SearchRequest = {query: string, document_id?: string | undefined, table_id?: string | undefined, tags?: Array | undefined, metadata?: {[key: string]: unknown} | undefined, max_depth?: number | undefined, beam_width?: number | undefined, min_confidence?: number | undefined, limit?: number | undefined}; export type CreateApiV1SearchArgs = {body: SearchRequest}; export type CreateApiV1SearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pathNode = z.object({node_id: z.string().optional(), title: z.string().optional(), reasoning: z.string().optional()}); export const searchResult = z.object({node_id: z.string().optional(), document_id: z.string().optional(), path: z.array(pathNode).optional(), content: z.string().optional(), answer: z.string().optional(), confidence: z.number().optional()}); export const searchStats = z.object({nodes_visited: z.number().int().optional(), nodes_pruned: z.number().int().optional(), llm_calls: z.number().int().optional(), total_time_ms: z.number().int().optional()}); export const searchResponse = z.object({results: z.array(searchResult).optional(), stats: searchStats.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 }; /** * Search with LLM-guided tree traversal * * POST /v1/search */ export const createApiV1Search = async (args: CreateApiV1SearchArgs, options?: CreateApiV1SearchOptions) => { const {baseUrl = "https://your-reasondb-host", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/search${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(searchResponse, await res.json()) };