From 63b610c2638e446a9cca4f0dca50c97010c94abf Mon Sep 17 00:00:00 2001 From: Austin DeNoble Date: Mon, 11 Sep 2023 18:04:27 -0400 Subject: [PATCH 1/2] remove typebox type inference and replace with direct typescript definitions --- src/control/configureIndex.ts | 5 ++--- src/control/createCollection.ts | 6 ++---- src/control/createIndex.ts | 14 ++++++++++++-- src/control/types.ts | 6 +++--- src/data/deleteMany.ts | 12 ++++++------ src/data/describeIndexStats.ts | 7 ++----- src/data/query.ts | 15 +++++++++++---- src/data/types.ts | 17 ++++++++++++----- 8 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/control/configureIndex.ts b/src/control/configureIndex.ts index d195e880..acb1a13d 100644 --- a/src/control/configureIndex.ts +++ b/src/control/configureIndex.ts @@ -4,7 +4,7 @@ import { buildValidator } from '../validator'; import type { IndexName } from './types'; import { handleIndexRequestError } from './utils'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; import { ReplicasSchema, PodTypeSchema, IndexNameSchema } from './types'; const ConfigureIndexOptionsSchema = Type.Object( @@ -14,8 +14,7 @@ const ConfigureIndexOptionsSchema = Type.Object( }, { additionalProperties: false } ); - -export type ConfigureIndexOptions = Static; +export type ConfigureIndexOptions = { replicas?: number; podType?: string }; export const configureIndex = (api: IndexOperationsApi) => { const indexNameValidator = buildValidator( diff --git a/src/control/createCollection.ts b/src/control/createCollection.ts index 7106bd60..96a98d5f 100644 --- a/src/control/createCollection.ts +++ b/src/control/createCollection.ts @@ -2,7 +2,7 @@ import { IndexOperationsApi } from '../pinecone-generated-ts-fetch'; import { buildConfigValidator } from '../validator'; import { handleIndexRequestError } from './utils'; import { CollectionNameSchema, IndexNameSchema } from './types'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; const CreateCollectionOptionsSchema = Type.Object( { @@ -12,9 +12,7 @@ const CreateCollectionOptionsSchema = Type.Object( { additionalProperties: false } ); -export type CreateCollectionOptions = Static< - typeof CreateCollectionOptionsSchema ->; +export type CreateCollectionOptions = { name: string; source: string }; export const createCollection = (api: IndexOperationsApi) => { const validator = buildConfigValidator( diff --git a/src/control/createIndex.ts b/src/control/createIndex.ts index f3222f9b..05bb5f71 100644 --- a/src/control/createIndex.ts +++ b/src/control/createIndex.ts @@ -3,7 +3,7 @@ import { buildConfigValidator } from '../validator'; import { debugLog } from '../utils'; import { handleApiError } from '../errors'; import { handleIndexRequestError } from './utils'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; import { IndexNameSchema, DimensionSchema, @@ -15,7 +15,17 @@ import { CollectionNameSchema, } from './types'; -export type CreateIndexOptions = Static; +export type CreateIndexOptions = { + name: string; + dimension: number; + metric?: string; + pods?: number; + replicas?: number; + podType?: string; + metadataConfig?: { indexed: Array }; + sourceCollection?: string; + waitUntilReady?: boolean; +}; const CreateIndexOptionsSchema = Type.Object( { diff --git a/src/control/types.ts b/src/control/types.ts index cbffd488..f7b332be 100644 --- a/src/control/types.ts +++ b/src/control/types.ts @@ -1,4 +1,4 @@ -import { Type, Static } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; const nonemptyString = Type.String({ minLength: 1 }); const positiveInteger = Type.Integer({ minimum: 1 }); @@ -9,7 +9,7 @@ const positiveInteger = Type.Integer({ minimum: 1 }); // no descriptive information is returned for an index named empty // string. To avoid this confusing case, we require lenth > 1. export const IndexNameSchema = nonemptyString; -export type IndexName = Static; +export type IndexName = string; export const PodTypeSchema = nonemptyString; export const ReplicasSchema = positiveInteger; @@ -29,4 +29,4 @@ export const MetadataConfigSchema = Type.Object( // no descriptive information is returned for an collection named empty // string. To avoid this confusing case, we require lenth > 1. export const CollectionNameSchema = nonemptyString; -export type CollectionName = Static; +export type CollectionName = string; diff --git a/src/data/deleteMany.ts b/src/data/deleteMany.ts index 5d26d411..b5784413 100644 --- a/src/data/deleteMany.ts +++ b/src/data/deleteMany.ts @@ -1,7 +1,7 @@ import { VectorOperationsProvider } from './vectorOperationsProvider'; import { handleApiError } from '../errors'; import { buildConfigValidator } from '../validator'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; import type { DeleteRequest } from '../pinecone-generated-ts-fetch/models/DeleteRequest'; import { RecordIdSchema } from './types'; @@ -17,11 +17,11 @@ const DeleteManySchema = Type.Union([ DeleteManyByFilterSchema, ]); -export type DeleteManyByVectorIdOptions = Static< - typeof DeleteManyByRecordIdSchema ->; -export type DeleteManyByFilterOptions = Static; -export type DeleteManyOptions = Static; +export type DeleteManyByVectorIdOptions = Array; +export type DeleteManyByFilterOptions = object; +export type DeleteManyOptions = + | DeleteManyByVectorIdOptions + | DeleteManyByFilterOptions; export const deleteMany = ( apiProvider: VectorOperationsProvider, diff --git a/src/data/describeIndexStats.ts b/src/data/describeIndexStats.ts index e9034bb8..6633f28f 100644 --- a/src/data/describeIndexStats.ts +++ b/src/data/describeIndexStats.ts @@ -1,6 +1,6 @@ import { handleApiError } from '../errors'; import { buildConfigValidator } from '../validator'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; import { VectorOperationsProvider } from './vectorOperationsProvider'; export type IndexStatsNamespaceSummary = { @@ -20,10 +20,7 @@ const DescribeIndexStatsOptionsSchema = Type.Object( }, { additionalProperties: false } ); - -export type DescribeIndexStatsOptions = Static< - typeof DescribeIndexStatsOptionsSchema ->; +export type DescribeIndexStatsOptions = { filter: object }; export const describeIndexStats = (apiProvider: VectorOperationsProvider) => { const validator = buildConfigValidator( diff --git a/src/data/query.ts b/src/data/query.ts index 6b4023f6..cef8e188 100644 --- a/src/data/query.ts +++ b/src/data/query.ts @@ -3,10 +3,11 @@ import { buildConfigValidator } from '../validator'; import { RecordIdSchema, RecordSparseValuesSchema, + RecordValues, RecordValuesSchema, } from './types'; import type { PineconeRecord, RecordMetadata } from './types'; -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; import { VectorOperationsProvider } from './vectorOperationsProvider'; const shared = { @@ -38,9 +39,15 @@ const QueryByVectorValues = Type.Object( const QuerySchema = Type.Union([QueryByRecordId, QueryByVectorValues]); -export type QueryByRecordId = Static; -export type QueryByVectorValues = Static; -export type QueryOptions = Static; +type QueryShared = { + topK: number; + includeValues?: boolean; + includeMetadata?: boolean; + filter?: object; +}; +export type QueryByRecordId = QueryShared & { id: string }; +export type QueryByVectorValues = QueryShared & { vector: RecordValues }; +export type QueryOptions = QueryByRecordId | QueryByVectorValues; export interface ScoredPineconeRecord extends PineconeRecord { diff --git a/src/data/types.ts b/src/data/types.ts index 989cd9dd..20a18aa2 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -1,4 +1,4 @@ -import { Static, Type } from '@sinclair/typebox'; +import { Type } from '@sinclair/typebox'; export const PineconeConfigurationSchema = Type.Object( { @@ -8,7 +8,11 @@ export const PineconeConfigurationSchema = Type.Object( }, { additionalProperties: false } ); -export type PineconeConfiguration = Static; +export type PineconeConfiguration = { + environment: string; + apiKey: string; + projectId?: string; +}; export const RecordIdSchema = Type.String({ minLength: 1 }); export const RecordValuesSchema = Type.Array(Type.Number()); @@ -29,9 +33,12 @@ export const PineconeRecordSchema = Type.Object( { additionalProperties: false } ); -export type RecordId = Static; -export type RecordValues = Static; -export type RecordSparseValues = Static; +export type RecordId = string; +export type RecordValues = number[]; +export type RecordSparseValues = { + indices: Array; + values: Array; +}; export type RecordMetadataValue = string | boolean | number | Array; export type RecordMetadata = Record; export type PineconeRecord = { From a46399b2946d052118736550374728791e4e0e4e Mon Sep 17 00:00:00 2001 From: Austin DeNoble Date: Mon, 11 Sep 2023 19:09:12 -0400 Subject: [PATCH 2/2] use the central types for the various record, index, etc strings --- src/control/createCollection.ts | 6 +++++- src/control/createIndex.ts | 3 ++- src/data/deleteMany.ts | 3 ++- src/data/types.ts | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/control/createCollection.ts b/src/control/createCollection.ts index 96a98d5f..447becd1 100644 --- a/src/control/createCollection.ts +++ b/src/control/createCollection.ts @@ -2,6 +2,7 @@ import { IndexOperationsApi } from '../pinecone-generated-ts-fetch'; import { buildConfigValidator } from '../validator'; import { handleIndexRequestError } from './utils'; import { CollectionNameSchema, IndexNameSchema } from './types'; +import type { CollectionName, IndexName } from './types'; import { Type } from '@sinclair/typebox'; const CreateCollectionOptionsSchema = Type.Object( @@ -12,7 +13,10 @@ const CreateCollectionOptionsSchema = Type.Object( { additionalProperties: false } ); -export type CreateCollectionOptions = { name: string; source: string }; +export type CreateCollectionOptions = { + name: CollectionName; + source: IndexName; +}; export const createCollection = (api: IndexOperationsApi) => { const validator = buildConfigValidator( diff --git a/src/control/createIndex.ts b/src/control/createIndex.ts index 05bb5f71..25ba7adf 100644 --- a/src/control/createIndex.ts +++ b/src/control/createIndex.ts @@ -14,9 +14,10 @@ import { MetadataConfigSchema, CollectionNameSchema, } from './types'; +import type { IndexName } from './types'; export type CreateIndexOptions = { - name: string; + name: IndexName; dimension: number; metric?: string; pods?: number; diff --git a/src/data/deleteMany.ts b/src/data/deleteMany.ts index b5784413..6ea3e510 100644 --- a/src/data/deleteMany.ts +++ b/src/data/deleteMany.ts @@ -4,6 +4,7 @@ import { buildConfigValidator } from '../validator'; import { Type } from '@sinclair/typebox'; import type { DeleteRequest } from '../pinecone-generated-ts-fetch/models/DeleteRequest'; import { RecordIdSchema } from './types'; +import type { RecordId } from './types'; const DeleteManyByRecordIdSchema = Type.Array(RecordIdSchema); @@ -17,7 +18,7 @@ const DeleteManySchema = Type.Union([ DeleteManyByFilterSchema, ]); -export type DeleteManyByVectorIdOptions = Array; +export type DeleteManyByVectorIdOptions = Array; export type DeleteManyByFilterOptions = object; export type DeleteManyOptions = | DeleteManyByVectorIdOptions diff --git a/src/data/types.ts b/src/data/types.ts index 20a18aa2..c4e77f14 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -34,7 +34,7 @@ export const PineconeRecordSchema = Type.Object( ); export type RecordId = string; -export type RecordValues = number[]; +export type RecordValues = Array; export type RecordSparseValues = { indices: Array; values: Array;