import {z} from 'zod' export type WriteOrdering = 'weak' | 'medium' | 'strong'; export type PayloadSchemaType = 'keyword' | 'integer' | 'float' | 'geo' | 'text' | 'bool' | 'datetime' | 'uuid'; export type KeywordIndexType = 'keyword'; export type Memory = 'cold' | 'cached' | 'pinned'; export type KeywordIndexParams = {type: KeywordIndexType, is_tenant?: (boolean | null) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, prefix?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type IntegerIndexType = 'integer'; export type IntegerIndexParams = {type: IntegerIndexType, lookup?: (boolean | null) | undefined, range?: (boolean | null) | undefined, is_principal?: (boolean | null) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type FloatIndexType = 'float'; export type FloatIndexParams = {type: FloatIndexType, is_principal?: (boolean | null) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type GeoIndexType = 'geo'; export type GeoIndexParams = {type: GeoIndexType, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type TextIndexType = 'text'; export type TokenizerType = 'prefix' | 'whitespace' | 'word' | 'multilingual' | 'code'; export type Language = 'arabic' | 'azerbaijani' | 'basque' | 'bengali' | 'catalan' | 'chinese' | 'danish' | 'dutch' | 'english' | 'finnish' | 'french' | 'german' | 'greek' | 'hebrew' | 'hinglish' | 'hungarian' | 'indonesian' | 'italian' | 'japanese' | 'kazakh' | 'nepali' | 'norwegian' | 'portuguese' | 'romanian' | 'russian' | 'slovene' | 'spanish' | 'swedish' | 'tajik' | 'turkish'; export type StopwordsSet = {languages?: (Array | null) | undefined, custom?: (Array | null) | undefined}; export type StopwordsInterface = Language | StopwordsSet; export type Snowball = 'snowball'; export type SnowballLanguage = 'arabic' | 'armenian' | 'danish' | 'dutch' | 'english' | 'finnish' | 'french' | 'german' | 'greek' | 'hungarian' | 'italian' | 'norwegian' | 'portuguese' | 'romanian' | 'russian' | 'spanish' | 'swedish' | 'tamil' | 'turkish'; export type SnowballParams = {type: Snowball, language: SnowballLanguage}; export type NoStemmer = 'none'; export type DisabledStemmerParams = {type: NoStemmer}; export type StemmingAlgorithm = SnowballParams | DisabledStemmerParams; export type TextScoringType = 'bm25'; export type TextScoringParams = {type: TextScoringType}; export type TextScoringInterface = boolean | TextScoringParams; export type TextIndexParams = {type: TextIndexType, tokenizer?: TokenizerType | undefined, min_token_len?: (number | null) | undefined, max_token_len?: (number | null) | undefined, lowercase?: (boolean | null) | undefined, ascii_folding?: (boolean | null) | undefined, phrase_matching?: (boolean | null) | undefined, stopwords?: (StopwordsInterface | unknown) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, stemmer?: (StemmingAlgorithm | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, scoring?: (TextScoringInterface | unknown) | undefined, nested?: (string | null) | undefined}; export type BoolIndexType = 'bool'; export type BoolIndexParams = {type: BoolIndexType, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type DatetimeIndexType = 'datetime'; export type DatetimeIndexParams = {type: DatetimeIndexType, is_principal?: (boolean | null) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type UuidIndexType = 'uuid'; export type UuidIndexParams = {type: UuidIndexType, is_tenant?: (boolean | null) | undefined, on_disk?: (boolean | null) | undefined, memory?: (Memory | unknown) | undefined, enable_hnsw?: (boolean | null) | undefined, nested?: (string | null) | undefined}; export type PayloadSchemaParams = KeywordIndexParams | IntegerIndexParams | FloatIndexParams | GeoIndexParams | TextIndexParams | BoolIndexParams | DatetimeIndexParams | UuidIndexParams; export type PayloadFieldSchema = PayloadSchemaType | PayloadSchemaParams; export type CreateFieldIndex = {field_name: string, field_schema?: (PayloadFieldSchema | unknown) | undefined}; export type UpdateApiCollectionsCollectionNameIndexArgs = {collection_name: string, wait?: boolean | undefined, ordering?: WriteOrdering | undefined, timeout?: number | undefined, body: CreateFieldIndex}; export type UpdateApiCollectionsCollectionNameIndexOptions = {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 updateStatus = z.enum(["acknowledged", "completed", "wait_timeout"]); export const updateResult = z.object({operation_id: z.number().int().gte(0).nullable().optional(), status: updateStatus}); export const updateApiCollectionsCollectionNameIndexResponse = z.object({usage: z.union([usage, z.unknown()]).optional(), time: z.number().optional(), status: z.string().optional(), result: updateResult.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 }; /** * Create index for field in collection * * PUT /collections/{collection_name}/index * * Also accepts auth: bearerAuth */ export const updateApiCollectionsCollectionNameIndex = async (args: UpdateApiCollectionsCollectionNameIndexArgs, options?: UpdateApiCollectionsCollectionNameIndexOptions) => { 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.wait != null) query.append("wait", String(args.wait)) if (args.ordering != null) query.append("ordering", String(args.ordering)) 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))}/index${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiCollectionsCollectionNameIndexResponse, await res.json()) };