import {z} from 'zod' export type AttributeType = string; export type Language = 'arabic' | 'danish' | 'dutch' | 'english' | 'finnish' | 'french' | 'german' | 'greek' | 'hungarian' | 'italian' | 'norwegian' | 'portuguese' | 'romanian' | 'russian' | 'spanish' | 'swedish' | 'tamil' | 'turkish'; export type Tokenizer = 'pre_tokenized_array' | 'word_v0' | 'word_v1' | 'word_v2' | 'word_v3' | 'word_v4'; export type FullTextSearchConfig = {k1?: number | undefined, b?: number | undefined, language?: Language | undefined, stemming?: boolean | undefined, remove_stopwords?: boolean | undefined, ascii_folding?: boolean | undefined, case_sensitive?: boolean | undefined, max_token_length?: number | undefined, tokenizer?: Tokenizer | undefined}; export type FullTextSearch = boolean | FullTextSearchConfig; export type DistanceMetric = 'cosine_distance' | 'euclidean_squared'; export type AnnConfig = {distance_metric?: DistanceMetric | undefined, late_interaction?: boolean | undefined}; export type Ann = boolean | AnnConfig; export type SparseDistanceMetric = 'dot_product'; export type SparseKnn = {distance_metric: SparseDistanceMetric}; export type AttributeEmbedConfig = {attribute?: string | undefined, model: string, dims?: number | undefined}; export type AttributeEmbed = string | AttributeEmbedConfig | null; export type AttributeSchemaConfig = {type: AttributeType, filterable?: boolean | undefined, regex?: boolean | undefined, glob?: boolean | undefined, fuzzy?: boolean | undefined, full_text_search?: FullTextSearch | undefined, ann?: Ann | undefined, sparse_knn?: SparseKnn | undefined, embed?: AttributeEmbed | undefined}; export type AttributeSchemaDrop = {drop: boolean}; export type AttributeSchema = AttributeType | AttributeSchemaConfig | AttributeSchemaDrop; export type CreateApiV1NamespacesNamespaceSchemaArgs = {namespace: string, body: {[key: string]: AttributeSchema}}; export type CreateApiV1NamespacesNamespaceSchemaOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attributeType = z.string(); export const language = z.union([z.literal("arabic"), z.literal("danish"), z.literal("dutch"), z.literal("english"), z.literal("finnish"), z.literal("french"), z.literal("german"), z.literal("greek"), z.literal("hungarian"), z.literal("italian"), z.literal("norwegian"), z.literal("portuguese"), z.literal("romanian"), z.literal("russian"), z.literal("spanish"), z.literal("swedish"), z.literal("tamil"), z.literal("turkish")]); export const tokenizer = z.union([z.literal("pre_tokenized_array"), z.literal("word_v0"), z.literal("word_v1"), z.literal("word_v2"), z.literal("word_v3"), z.literal("word_v4")]); export const fullTextSearchConfig = z.object({k1: z.number().optional(), b: z.number().optional(), language: language.optional(), stemming: z.boolean().optional(), remove_stopwords: z.boolean().optional(), ascii_folding: z.boolean().optional(), case_sensitive: z.boolean().optional(), max_token_length: z.number().int().gte(0).optional(), tokenizer: tokenizer.optional()}); export const fullTextSearch = z.union([z.boolean(), fullTextSearchConfig]); export const distanceMetric = z.union([z.literal("cosine_distance"), z.literal("euclidean_squared")]); export const annConfig = z.object({distance_metric: distanceMetric.optional(), late_interaction: z.boolean().optional()}); export const ann = z.union([z.boolean(), annConfig]); export const sparseDistanceMetric = z.literal("dot_product"); export const sparseKnn = z.object({distance_metric: sparseDistanceMetric}); export const attributeEmbedConfig = z.object({attribute: z.string().optional(), model: z.string(), dims: z.number().int().gte(0).optional()}); export const attributeEmbed = z.union([z.string(), attributeEmbedConfig]).nullable(); export const attributeSchemaConfig = z.object({type: attributeType, filterable: z.boolean().optional(), regex: z.boolean().optional(), glob: z.boolean().optional(), fuzzy: z.boolean().optional(), full_text_search: fullTextSearch.optional(), ann: ann.optional(), sparse_knn: sparseKnn.optional(), embed: attributeEmbed.optional()}); export const createApiV1NamespacesNamespaceSchemaResponse = z.record(z.string(), attributeSchemaConfig); 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 namespace schema. * * POST /v1/namespaces/{namespace}/schema */ export const createApiV1NamespacesNamespaceSchema = async (args: CreateApiV1NamespacesNamespaceSchemaArgs, options?: CreateApiV1NamespacesNamespaceSchemaOptions) => { 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(/\/+$/, '')}/v1/namespaces/${encodeURIComponent(String(args.namespace))}/schema${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(createApiV1NamespacesNamespaceSchemaResponse, await res.json()) };