import {z} from 'zod' export type VectorEncoding = 'float' | 'base64'; export type IncludeAttributes = boolean | Array; export type DistanceMetric = 'cosine_distance' | 'euclidean_squared'; export type Limit = {total: number, per?: {attributes: Array, limit: number} | undefined}; export type CreateApiV2NamespacesNamespaceExplainQueryArgs = {namespace: string, body: {vector_encoding?: VectorEncoding | undefined, consistency?: ({level: 'strong'} | {level: 'eventual', max_staleness_seconds?: number | undefined}) | undefined, rank_by: unknown, top_k?: number | undefined, offset?: number | undefined, filters: unknown, include_attributes?: IncludeAttributes | undefined, exclude_attributes?: Array | undefined, aggregate_by?: {[key: string]: unknown} | undefined, group_by?: Array | undefined, compute_attributes?: {[key: string]: unknown} | undefined, distance_metric?: DistanceMetric | undefined, limit?: (number | Limit) | undefined}}; export type CreateApiV2NamespacesNamespaceExplainQueryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2NamespacesNamespaceExplainQueryResponse = z.object({plan_text: z.string().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 }; /** * Explain a query plan. * * POST /v2/namespaces/{namespace}/explain_query */ export const createApiV2NamespacesNamespaceExplainQuery = async (args: CreateApiV2NamespacesNamespaceExplainQueryArgs, options?: CreateApiV2NamespacesNamespaceExplainQueryOptions) => { const {baseUrl = "https://undefined.turbopuffer.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/namespaces/${encodeURIComponent(String(args.namespace))}/explain_query${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(createApiV2NamespacesNamespaceExplainQueryResponse, await res.json()) };