import {z} from 'zod' export type ReadConsistencyType = 'majority' | 'quorum' | 'all'; export type ReadConsistency = number | ReadConsistencyType; export type ShardKey = string | number; export type ShardKeyWithFallback = {target: ShardKey, fallback: ShardKey}; export type ShardKeySelector = ShardKey | Array | ShardKeyWithFallback; export type ExtendedPointId = number | string; export type PayloadSelectorInclude = {include: Array}; export type PayloadSelectorExclude = {exclude: Array}; export type PayloadSelector = PayloadSelectorInclude | PayloadSelectorExclude; export type WithPayloadInterface = boolean | Array | PayloadSelector; export type WithVector = boolean | Array; export type PointRequest = {shard_key?: (ShardKeySelector | unknown) | undefined, ids: Array, with_payload?: (WithPayloadInterface | unknown) | undefined, with_vector?: WithVector | undefined}; export type CreateApiCollectionsCollectionNamePointsArgs = {collection_name: string, consistency?: ReadConsistency | undefined, timeout?: number | undefined, body: PointRequest}; export type CreateApiCollectionsCollectionNamePointsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const hardwareUsage = z.object({cpu: z.number().int().gte(0), payload_io_read: z.number().int().gte(0), payload_io_write: z.number().int().gte(0), payload_index_io_read: z.number().int().gte(0), payload_index_io_write: z.number().int().gte(0), vector_io_read: z.number().int().gte(0), vector_io_write: z.number().int().gte(0)}); export const modelUsage = z.object({tokens: z.number().int().gte(0)}); export const inferenceUsage = z.object({models: z.record(z.string(), modelUsage)}); export const usage = z.object({hardware: z.union([hardwareUsage, z.unknown()]).optional(), inference: z.union([inferenceUsage, z.unknown()]).optional()}); export const extendedPointId = z.union([z.number().int().gte(0), z.string()]); export const payload = z.record(z.string(), z.unknown()); export const sparseVector = z.object({indices: z.array(z.number().int().gte(0)), values: z.array(z.number())}); export const vectorOutput = z.union([z.array(z.number()), sparseVector, z.array(z.array(z.number()))]); export const vectorStructOutput = z.union([z.array(z.number()), z.array(z.array(z.number())), z.record(z.string(), vectorOutput)]); export const shardKey = z.union([z.string(), z.number().int().gte(0)]); export const orderValue = z.union([z.number().int(), z.number()]); export const record = z.object({id: extendedPointId, payload: z.union([payload, z.unknown()]).optional(), vector: z.union([vectorStructOutput, z.unknown()]).optional(), shard_key: z.union([shardKey, z.unknown()]).optional(), order_value: z.union([orderValue, z.unknown()]).optional()}); export const createApiCollectionsCollectionNamePointsResponse = z.object({usage: z.union([usage, z.unknown()]).optional(), time: z.number().optional(), status: z.string().optional(), result: z.array(record).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 }; /** * Get points * * POST /collections/{collection_name}/points * * Also accepts auth: bearerAuth */ export const createApiCollectionsCollectionNamePoints = async (args: CreateApiCollectionsCollectionNamePointsArgs, options?: CreateApiCollectionsCollectionNamePointsOptions) => { const {baseUrl = "http://localhost:6333", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.consistency != null) query.append("consistency", String(args.consistency)) if (args.timeout != null) query.append("timeout", String(args.timeout)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/collections/${encodeURIComponent(String(args.collection_name))}/points${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(createApiCollectionsCollectionNamePointsResponse, await res.json()) };