import {z} from 'zod' export type DbControlDeletionProtection = string; export type DbControlIndexTags = {[key: string]: string}; export type DbControlReadCapacityOnDemandSpec = {mode: string}; export type DbControlScalingConfigManual = {replicas?: number | undefined, shards?: number | undefined}; export type DbControlReadCapacityDedicatedConfig = {node_type?: string | undefined, scaling?: string | undefined, manual?: DbControlScalingConfigManual | undefined}; export type DbControlReadCapacityDedicatedSpec = {mode: string, dedicated: DbControlReadCapacityDedicatedConfig} | {[key: string]: unknown}; export type DbControlReadCapacity = DbControlReadCapacityOnDemandSpec | DbControlReadCapacityDedicatedSpec; export type DbControlMetadataSchema = {fields: {[key: string]: {filterable?: boolean | undefined}}}; export type DbControlServerlessSpec = {cloud: string, region: string, read_capacity?: DbControlReadCapacity | undefined, source_collection?: string | undefined, schema?: DbControlMetadataSchema | undefined}; export type DbControlPodSpec = {environment: string, replicas?: number | undefined, shards?: number | undefined, pod_type: string, pods?: number | undefined, metadata_config?: ({indexed?: Array | undefined}) | undefined, source_collection?: string | undefined}; export type DbControlByocSpec = {environment: string, read_capacity?: DbControlReadCapacity | undefined, schema?: DbControlMetadataSchema | undefined}; export type DbControlIndexSpec = {serverless: DbControlServerlessSpec} | {pod: DbControlPodSpec} | {byoc: DbControlByocSpec}; export type DbControlCreateIndexRequest = {name: string, dimension?: number | undefined, metric?: string | undefined, deletion_protection?: DbControlDeletionProtection | undefined, tags?: DbControlIndexTags | undefined, spec: DbControlIndexSpec, vector_type?: string | undefined}; export type CreateApiIndexesArgs = {'X-Pinecone-Api-Version': string, body: DbControlCreateIndexRequest}; export type CreateApiIndexesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dbControlDeletionProtection = z.string(); export const dbControlIndexTags = z.record(z.string(), z.string()); export const dbControlModelIndexEmbed = z.object({model: z.string(), metric: z.string().optional(), dimension: z.number().int().gte(1).lte(20000).optional(), vector_type: z.string().optional(), field_map: z.object({}).optional(), read_parameters: z.object({}).optional(), write_parameters: z.object({}).optional()}); export const dbControlReadCapacityStatus = z.object({state: z.string(), current_replicas: z.number().int().optional(), current_shards: z.number().int().optional(), error_message: z.string().optional()}); export const dbControlReadCapacityOnDemandSpecResponse = z.object({mode: z.string(), status: dbControlReadCapacityStatus}); export const dbControlScalingConfigManual = z.object({replicas: z.number().int().gte(0).optional(), shards: z.number().int().gte(1).optional()}); export const dbControlReadCapacityDedicatedConfig = z.object({node_type: z.string().optional(), scaling: z.string().optional(), manual: dbControlScalingConfigManual.optional()}); export const dbControlReadCapacityDedicatedSpecResponse = z.object({mode: z.string(), dedicated: dbControlReadCapacityDedicatedConfig, status: dbControlReadCapacityStatus}); export const dbControlReadCapacityResponse = z.discriminatedUnion("mode", [dbControlReadCapacityOnDemandSpecResponse, dbControlReadCapacityDedicatedSpecResponse]); export const dbControlMetadataSchema = z.object({fields: z.record(z.string(), z.object({filterable: z.boolean().optional()}))}); export const dbControlServerlessSpecResponse = z.object({cloud: z.string(), region: z.string(), read_capacity: dbControlReadCapacityResponse, source_collection: z.string().optional(), schema: dbControlMetadataSchema.optional()}); export const dbControlPodSpec = z.object({environment: z.string(), replicas: z.number().int().gte(1).optional(), shards: z.number().int().gte(1).optional(), pod_type: z.string(), pods: z.number().int().gte(1).optional(), metadata_config: z.object({indexed: z.array(z.string()).optional()}).optional(), source_collection: z.string().optional()}); export const dbControlByocSpecResponse = z.object({environment: z.string(), read_capacity: dbControlReadCapacityResponse, schema: dbControlMetadataSchema.optional()}); export const dbControlIndexModel = z.object({name: z.string().min(1).max(45), dimension: z.number().int().gte(1).lte(20000).optional(), metric: z.string(), host: z.string(), private_host: z.string().optional(), deletion_protection: dbControlDeletionProtection.optional(), tags: dbControlIndexTags.optional(), embed: dbControlModelIndexEmbed.optional(), spec: z.union([z.object({serverless: dbControlServerlessSpecResponse}), z.object({pod: dbControlPodSpec}), z.object({byoc: dbControlByocSpecResponse})]), status: z.object({ready: z.boolean(), state: z.string()}), vector_type: z.string()}); 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 an index * * POST /indexes */ export const createApiIndexes = async (args: CreateApiIndexesArgs, options?: CreateApiIndexesOptions) => { const {baseUrl = "https://api.pinecone.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Pinecone-Api-Version'] != null) headers.set("X-Pinecone-Api-Version", String(args['X-Pinecone-Api-Version'])) if (ApiKeyAuth !== undefined) headers.set("Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/indexes${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(dbControlIndexModel, await res.json()) };