import {z} from 'zod' export type AttributeValueDto = {string?: (string | null) | undefined, guid?: (string | null) | undefined, bool?: (boolean | null) | undefined, int64?: (number | null) | undefined, float64?: (number | null) | undefined, dateTime?: (string | null) | undefined}; export type AttributeDto = {key?: string | undefined, value?: AttributeValueDto | undefined}; export type DenseVectorDto = {isSparse?: boolean | undefined, coordinates?: Array | undefined}; export type SparseVectorDto = {isSparse?: boolean | undefined, coordinates?: Array | undefined}; export type SearchQueryDto = {splitFilter?: (Array | null) | undefined, queryVectors?: (Array) | undefined, k?: number | undefined, retrieveVectors?: boolean | undefined}; export type GetApiApiV1SearchIndexNameIndexVersionArgs = {indexName: string, indexVersion: string, body: SearchQueryDto}; export type GetApiApiV1SearchIndexNameIndexVersionOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const denseVectorDto = z.object({isSparse: z.boolean().optional(), coordinates: z.array(z.number()).optional()}); export const sparseVectorDto = z.object({isSparse: z.boolean().optional(), coordinates: z.array(z.number()).optional()}); export const attributeValueDto = z.object({string: z.string().nullable().optional(), guid: z.string().nullable().optional(), bool: z.boolean().nullable().optional(), int64: z.number().int().nullable().optional(), float64: z.number().nullable().optional(), dateTime: z.string().nullable().optional()}); export const attributeDto = z.object({key: z.string().optional(), value: attributeValueDto.optional()}); export const foundDataPointDto = z.object({vector: z.union([denseVectorDto, sparseVectorDto]).nullable().optional(), attributes: z.array(attributeDto).optional(), distance: z.number().optional()}); export const searchResultDto = z.object({queryVector: z.union([denseVectorDto, sparseVectorDto]).optional(), nearestDataPoints: z.array(foundDataPointDto).optional()}); export const getApiApiV1SearchIndexNameIndexVersionResponse = z.array(searchResultDto); 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 /api/v1/search/{indexName}/{indexVersion} */ export const getApiApiV1SearchIndexNameIndexVersion = async (args: GetApiApiV1SearchIndexNameIndexVersionArgs, options: GetApiApiV1SearchIndexNameIndexVersionOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/search/${encodeURIComponent(String(args.indexName))}/${encodeURIComponent(String(args.indexVersion))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(getApiApiV1SearchIndexNameIndexVersionResponse, await res.json()) };