import {z} from 'zod' export type ShardTransferMethod = 'stream_records' | 'snapshot' | 'wal_delta' | 'resharding_stream_records'; export type MoveShard = {shard_id: number, to_peer_id: number, from_peer_id: number, method?: (ShardTransferMethod | unknown) | undefined}; export type MoveShardOperation = {move_shard: MoveShard}; export type ReplicateShard = {shard_id: number, to_peer_id: number, from_peer_id: number, method?: (ShardTransferMethod | unknown) | undefined}; export type ReplicateShardOperation = {replicate_shard: ReplicateShard}; export type AbortShardTransfer = {shard_id: number, to_peer_id: number, from_peer_id: number}; export type AbortTransferOperation = {abort_transfer: AbortShardTransfer}; export type Replica = {shard_id: number, peer_id: number}; export type DropReplicaOperation = {drop_replica: Replica}; export type ShardKey = string | number; export type ReplicaState = 'Active' | 'Dead' | 'Partial' | 'Initializing' | 'Listener' | 'PartialSnapshot' | 'Recovery' | 'Resharding' | 'ReshardingScaleDown' | 'ActiveRead' | 'ManualRecovery'; export type CreateShardingKey = {shard_key: ShardKey, shards_number?: (number | null) | undefined, replication_factor?: (number | null) | undefined, placement?: (Array | null) | undefined, initial_state?: (ReplicaState | unknown) | undefined}; export type CreateShardingKeyOperation = {create_sharding_key: CreateShardingKey}; export type DropShardingKey = {shard_key: ShardKey}; export type DropShardingKeyOperation = {drop_sharding_key: DropShardingKey}; export type RestartTransfer = {shard_id: number, from_peer_id: number, to_peer_id: number, method: ShardTransferMethod}; export type RestartTransferOperation = {restart_transfer: RestartTransfer}; export type ReshardingDirection = 'up' | 'down'; export type StartResharding = {direction: ReshardingDirection, peer_id?: (number | null) | undefined, shard_key?: (ShardKey | unknown) | undefined}; export type StartReshardingOperation = {start_resharding: StartResharding}; export type AbortResharding = Record; export type AbortReshardingOperation = {abort_resharding: AbortResharding}; export type ValueVariants = string | number | boolean; export type MatchValue = {value: ValueVariants}; export type MatchText = {text: string}; export type MatchTextAny = {text_any: string}; export type MatchPhrase = {phrase: string}; export type MatchPrefix = {prefix: string}; export type MatchSubstring = {substring: string}; export type AnyVariants = Array | Array; export type MatchAny = {any: AnyVariants}; export type MatchExcept = {except: AnyVariants}; export type Match = MatchValue | MatchText | MatchTextAny | MatchPhrase | MatchPrefix | MatchSubstring | MatchAny | MatchExcept; export type Range = {lt?: (number | null) | undefined, gt?: (number | null) | undefined, gte?: (number | null) | undefined, lte?: (number | null) | undefined}; export type DatetimeRange = {lt?: (string | null) | undefined, gt?: (string | null) | undefined, gte?: (string | null) | undefined, lte?: (string | null) | undefined}; export type RangeInterface = Range | DatetimeRange; export type GeoPoint = {lon: number, lat: number}; export type GeoBoundingBox = {top_left: GeoPoint, bottom_right: GeoPoint}; export type GeoRadius = {center: GeoPoint, radius: number}; export type GeoLineString = {points: Array}; export type GeoPolygon = {exterior: GeoLineString, interiors?: (Array | null) | undefined}; export type ValuesCount = {lt?: (number | null) | undefined, gt?: (number | null) | undefined, gte?: (number | null) | undefined, lte?: (number | null) | undefined}; export type FieldCondition = {key: string, match?: (Match | unknown) | undefined, range?: (RangeInterface | unknown) | undefined, geo_bounding_box?: (GeoBoundingBox | unknown) | undefined, geo_radius?: (GeoRadius | unknown) | undefined, geo_polygon?: (GeoPolygon | unknown) | undefined, values_count?: (ValuesCount | unknown) | undefined, is_empty?: (boolean | null) | undefined, is_null?: (boolean | null) | undefined}; export type PayloadField = {key: string}; export type IsEmptyCondition = {is_empty: PayloadField}; export type IsNullCondition = {is_null: PayloadField}; export type ExtendedPointId = number | string; export type HasIdCondition = {has_id: Array}; export type HasVectorCondition = {has_vector: string}; export type Slice = {total: number, index: number}; export type SliceCondition = {slice: Slice}; export type Nested = {key: string, filter: Filter}; export type NestedCondition = {nested: Nested}; export type Condition = FieldCondition | IsEmptyCondition | IsNullCondition | HasIdCondition | HasVectorCondition | SliceCondition | NestedCondition | Filter; export type MinShould = {conditions: Array, min_count: number}; export type Filter = {should?: (Condition | Array | unknown) | undefined, min_should?: (MinShould | unknown) | undefined, must?: (Condition | Array | unknown) | undefined, must_not?: (Condition | Array | unknown) | undefined}; export type ReplicatePoints = {filter?: (Filter | unknown) | undefined, from_shard_key: ShardKey, to_shard_key: ShardKey}; export type ReplicatePointsOperation = {replicate_points: ReplicatePoints}; export type ClusterOperations = MoveShardOperation | ReplicateShardOperation | AbortTransferOperation | DropReplicaOperation | CreateShardingKeyOperation | DropShardingKeyOperation | RestartTransferOperation | StartReshardingOperation | AbortReshardingOperation | ReplicatePointsOperation; export type CreateApiCollectionsCollectionNameClusterArgs = {collection_name: string, timeout?: number | undefined, body: ClusterOperations}; export type CreateApiCollectionsCollectionNameClusterOptions = {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 createApiCollectionsCollectionNameClusterResponse = z.object({usage: z.union([usage, z.unknown()]).optional(), time: z.number().optional(), status: z.string().optional(), result: z.boolean().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 }; /** * Update collection cluster setup * * POST /collections/{collection_name}/cluster * * Also accepts auth: bearerAuth */ export const createApiCollectionsCollectionNameCluster = async (args: CreateApiCollectionsCollectionNameClusterArgs, options?: CreateApiCollectionsCollectionNameClusterOptions) => { 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.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))}/cluster${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(createApiCollectionsCollectionNameClusterResponse, await res.json()) };