@mastra/chroma 0.0.0-vector-query-sources-20250516172905 → 0.0.0-vector-query-tool-provider-options-20250828222356
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +361 -4
- package/LICENSE.md +12 -4
- package/README.md +47 -21
- package/dist/index.cjs +280 -157
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +277 -154
- package/dist/index.js.map +1 -0
- package/dist/vector/filter.d.ts +19 -0
- package/dist/vector/filter.d.ts.map +1 -0
- package/dist/vector/index.d.ts +70 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/dist/vector/prompt.d.ts +6 -0
- package/dist/vector/prompt.d.ts.map +1 -0
- package/package.json +18 -13
- package/src/vector/filter.test.ts +27 -19
- package/src/vector/filter.ts +28 -5
- package/src/vector/index.test.ts +142 -129
- package/src/vector/index.ts +306 -200
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +17 -0
- package/dist/_tsup-dts-rollup.d.cts +0 -126
- package/dist/_tsup-dts-rollup.d.ts +0 -126
- package/dist/index.d.cts +0 -2
package/src/vector/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
1
2
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
3
|
import type {
|
|
3
4
|
QueryResult,
|
|
@@ -5,65 +6,83 @@ import type {
|
|
|
5
6
|
CreateIndexParams,
|
|
6
7
|
UpsertVectorParams,
|
|
7
8
|
QueryVectorParams,
|
|
8
|
-
ParamsToArgs,
|
|
9
|
-
QueryVectorArgs,
|
|
10
|
-
UpsertVectorArgs,
|
|
11
9
|
DescribeIndexParams,
|
|
12
10
|
DeleteIndexParams,
|
|
13
11
|
DeleteVectorParams,
|
|
14
12
|
UpdateVectorParams,
|
|
15
13
|
} from '@mastra/core/vector';
|
|
16
|
-
|
|
17
|
-
import type {
|
|
18
|
-
import {
|
|
19
|
-
import type { UpdateRecordsParams, Collection } from 'chromadb';
|
|
14
|
+
import { ChromaClient, CloudClient } from 'chromadb';
|
|
15
|
+
import type { ChromaClientArgs, RecordSet, Where, WhereDocument, Collection, Metadata } from 'chromadb';
|
|
16
|
+
import type { ChromaVectorFilter } from './filter';
|
|
20
17
|
import { ChromaFilterTranslator } from './filter';
|
|
21
18
|
|
|
22
19
|
interface ChromaUpsertVectorParams extends UpsertVectorParams {
|
|
23
20
|
documents?: string[];
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
|
|
23
|
+
interface ChromaQueryVectorParams extends QueryVectorParams<ChromaVectorFilter> {
|
|
24
|
+
documentFilter?: WhereDocument | null;
|
|
25
|
+
}
|
|
27
26
|
|
|
28
|
-
interface
|
|
29
|
-
|
|
27
|
+
interface ChromaGetRecordsParams {
|
|
28
|
+
indexName: string;
|
|
29
|
+
ids?: string[];
|
|
30
|
+
filter?: ChromaVectorFilter;
|
|
31
|
+
documentFilter?: WhereDocument | null;
|
|
32
|
+
includeVector?: boolean;
|
|
33
|
+
limit?: number;
|
|
34
|
+
offset?: number;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
type
|
|
37
|
+
type MastraMetadata = {
|
|
38
|
+
dimension?: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type ChromaVectorArgs = ChromaClientArgs & { apiKey?: string };
|
|
33
42
|
|
|
34
|
-
|
|
43
|
+
const spaceMappings = {
|
|
44
|
+
cosine: 'cosine',
|
|
45
|
+
euclidean: 'l2',
|
|
46
|
+
dotproduct: 'ip',
|
|
47
|
+
l2: 'euclidean',
|
|
48
|
+
ip: 'dotproduct',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export class ChromaVector extends MastraVector<ChromaVectorFilter> {
|
|
35
52
|
private client: ChromaClient;
|
|
36
|
-
private collections: Map<string,
|
|
37
|
-
|
|
38
|
-
constructor({
|
|
39
|
-
path,
|
|
40
|
-
auth,
|
|
41
|
-
}: {
|
|
42
|
-
path: string;
|
|
43
|
-
auth?: {
|
|
44
|
-
provider: string;
|
|
45
|
-
credentials: string;
|
|
46
|
-
};
|
|
47
|
-
}) {
|
|
53
|
+
private collections: Map<string, Collection>;
|
|
54
|
+
|
|
55
|
+
constructor(chromaClientArgs?: ChromaVectorArgs) {
|
|
48
56
|
super();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
if (chromaClientArgs?.apiKey) {
|
|
58
|
+
this.client = new CloudClient({
|
|
59
|
+
apiKey: chromaClientArgs.apiKey,
|
|
60
|
+
tenant: chromaClientArgs.tenant,
|
|
61
|
+
database: chromaClientArgs.database,
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
this.client = new ChromaClient(chromaClientArgs);
|
|
65
|
+
}
|
|
53
66
|
this.collections = new Map();
|
|
54
67
|
}
|
|
55
68
|
|
|
56
|
-
async getCollection(indexName:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
async getCollection({ indexName, forceUpdate = false }: { indexName: string; forceUpdate?: boolean }) {
|
|
70
|
+
let collection = this.collections.get(indexName);
|
|
71
|
+
if (forceUpdate || !collection) {
|
|
72
|
+
try {
|
|
73
|
+
collection = await this.client.getCollection({ name: indexName });
|
|
74
|
+
this.collections.set(indexName, collection);
|
|
75
|
+
return collection;
|
|
76
|
+
} catch {
|
|
77
|
+
throw new MastraError({
|
|
78
|
+
id: 'CHROMA_COLLECTION_GET_FAILED',
|
|
79
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
80
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
81
|
+
details: { indexName },
|
|
82
|
+
});
|
|
63
83
|
}
|
|
64
|
-
return null;
|
|
65
84
|
}
|
|
66
|
-
return
|
|
85
|
+
return collection;
|
|
67
86
|
}
|
|
68
87
|
|
|
69
88
|
private validateVectorDimensions(vectors: number[][], dimension: number): void {
|
|
@@ -76,62 +95,67 @@ export class ChromaVector extends MastraVector {
|
|
|
76
95
|
}
|
|
77
96
|
}
|
|
78
97
|
|
|
79
|
-
async upsert(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const { indexName, vectors, metadata, ids, documents } = params;
|
|
83
|
-
|
|
84
|
-
const collection = await this.getCollection(indexName);
|
|
85
|
-
|
|
86
|
-
// Get index stats to check dimension
|
|
87
|
-
const stats = await this.describeIndex({ indexName });
|
|
88
|
-
|
|
89
|
-
// Validate vector dimensions
|
|
90
|
-
this.validateVectorDimensions(vectors, stats.dimension);
|
|
91
|
-
|
|
92
|
-
// Generate IDs if not provided
|
|
93
|
-
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
98
|
+
async upsert({ indexName, vectors, metadata, ids, documents }: ChromaUpsertVectorParams): Promise<string[]> {
|
|
99
|
+
try {
|
|
100
|
+
const collection = await this.getCollection({ indexName });
|
|
94
101
|
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
const stats = await this.describeIndex({ indexName });
|
|
103
|
+
this.validateVectorDimensions(vectors, stats.dimension);
|
|
104
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
97
105
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
await collection.upsert({
|
|
107
|
+
ids: generatedIds,
|
|
108
|
+
embeddings: vectors,
|
|
109
|
+
metadatas: metadata,
|
|
110
|
+
documents: documents,
|
|
111
|
+
});
|
|
104
112
|
|
|
105
|
-
|
|
113
|
+
return generatedIds;
|
|
114
|
+
} catch (error: any) {
|
|
115
|
+
if (error instanceof MastraError) throw error;
|
|
116
|
+
throw new MastraError(
|
|
117
|
+
{
|
|
118
|
+
id: 'CHROMA_VECTOR_UPSERT_FAILED',
|
|
119
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
120
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
121
|
+
details: { indexName },
|
|
122
|
+
},
|
|
123
|
+
error,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
106
126
|
}
|
|
107
127
|
|
|
108
|
-
|
|
109
|
-
cosine: 'cosine',
|
|
110
|
-
euclidean: 'l2',
|
|
111
|
-
dotproduct: 'ip',
|
|
112
|
-
l2: 'euclidean',
|
|
113
|
-
ip: 'dotproduct',
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
async createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void> {
|
|
117
|
-
const params = this.normalizeArgs<CreateIndexParams>('createIndex', args);
|
|
118
|
-
const { indexName, dimension, metric = 'cosine' } = params;
|
|
119
|
-
|
|
128
|
+
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
120
129
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
121
|
-
throw new
|
|
130
|
+
throw new MastraError({
|
|
131
|
+
id: 'CHROMA_VECTOR_CREATE_INDEX_INVALID_DIMENSION',
|
|
132
|
+
text: 'Dimension must be a positive integer',
|
|
133
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
134
|
+
category: ErrorCategory.USER,
|
|
135
|
+
details: { dimension },
|
|
136
|
+
});
|
|
122
137
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
138
|
+
|
|
139
|
+
const hnswSpace = spaceMappings[metric] as 'cosine' | 'l2' | 'ip' | undefined;
|
|
140
|
+
|
|
141
|
+
if (!hnswSpace || !['cosine', 'l2', 'ip'].includes(hnswSpace)) {
|
|
142
|
+
throw new MastraError({
|
|
143
|
+
id: 'CHROMA_VECTOR_CREATE_INDEX_INVALID_METRIC',
|
|
144
|
+
text: `Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`,
|
|
145
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
146
|
+
category: ErrorCategory.USER,
|
|
147
|
+
details: { metric },
|
|
148
|
+
});
|
|
126
149
|
}
|
|
150
|
+
|
|
127
151
|
try {
|
|
128
|
-
await this.client.createCollection({
|
|
152
|
+
const collection = await this.client.createCollection({
|
|
129
153
|
name: indexName,
|
|
130
|
-
metadata: {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
},
|
|
154
|
+
metadata: { dimension },
|
|
155
|
+
configuration: { hnsw: { space: hnswSpace } },
|
|
156
|
+
embeddingFunction: null,
|
|
134
157
|
});
|
|
158
|
+
this.collections.set(indexName, collection);
|
|
135
159
|
} catch (error: any) {
|
|
136
160
|
// Check for 'already exists' error
|
|
137
161
|
const message = error?.message || error?.toString();
|
|
@@ -140,101 +164,187 @@ export class ChromaVector extends MastraVector {
|
|
|
140
164
|
await this.validateExistingIndex(indexName, dimension, metric);
|
|
141
165
|
return;
|
|
142
166
|
}
|
|
143
|
-
throw
|
|
167
|
+
throw new MastraError(
|
|
168
|
+
{
|
|
169
|
+
id: 'CHROMA_VECTOR_CREATE_INDEX_FAILED',
|
|
170
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
171
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
172
|
+
details: { indexName },
|
|
173
|
+
},
|
|
174
|
+
error,
|
|
175
|
+
);
|
|
144
176
|
}
|
|
145
177
|
}
|
|
146
178
|
|
|
147
|
-
transformFilter(filter?:
|
|
179
|
+
transformFilter(filter?: ChromaVectorFilter) {
|
|
148
180
|
const translator = new ChromaFilterTranslator();
|
|
149
|
-
|
|
181
|
+
const translatedFilter = translator.translate(filter);
|
|
182
|
+
return translatedFilter ? (translatedFilter as Where) : undefined;
|
|
150
183
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
|
|
185
|
+
async query<T extends Metadata = Metadata>({
|
|
186
|
+
indexName,
|
|
187
|
+
queryVector,
|
|
188
|
+
topK = 10,
|
|
189
|
+
filter,
|
|
190
|
+
includeVector = false,
|
|
191
|
+
documentFilter,
|
|
192
|
+
}: ChromaQueryVectorParams): Promise<QueryResult[]> {
|
|
193
|
+
try {
|
|
194
|
+
const collection = await this.getCollection({ indexName });
|
|
195
|
+
|
|
196
|
+
const defaultInclude: ['documents', 'metadatas', 'distances'] = ['documents', 'metadatas', 'distances'];
|
|
197
|
+
|
|
198
|
+
const translatedFilter = this.transformFilter(filter);
|
|
199
|
+
const results = await collection.query<T>({
|
|
200
|
+
queryEmbeddings: [queryVector],
|
|
201
|
+
nResults: topK,
|
|
202
|
+
where: translatedFilter ?? undefined,
|
|
203
|
+
whereDocument: documentFilter ?? undefined,
|
|
204
|
+
include: includeVector ? [...defaultInclude, 'embeddings'] : defaultInclude,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return (results.ids[0] || []).map((id: string, index: number) => ({
|
|
208
|
+
id,
|
|
209
|
+
score: results.distances?.[0]?.[index] || 0,
|
|
210
|
+
metadata: results.metadatas?.[0]?.[index] || {},
|
|
211
|
+
document: results.documents?.[0]?.[index] ?? undefined,
|
|
212
|
+
...(includeVector && { vector: results.embeddings?.[0]?.[index] || [] }),
|
|
213
|
+
}));
|
|
214
|
+
} catch (error: any) {
|
|
215
|
+
if (error instanceof MastraError) throw error;
|
|
216
|
+
throw new MastraError(
|
|
217
|
+
{
|
|
218
|
+
id: 'CHROMA_VECTOR_QUERY_FAILED',
|
|
219
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
220
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
221
|
+
details: { indexName },
|
|
222
|
+
},
|
|
223
|
+
error,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async get<T extends Metadata = Metadata>({
|
|
229
|
+
indexName,
|
|
230
|
+
ids,
|
|
231
|
+
filter,
|
|
232
|
+
includeVector = false,
|
|
233
|
+
documentFilter,
|
|
234
|
+
offset,
|
|
235
|
+
limit,
|
|
236
|
+
}: ChromaGetRecordsParams) {
|
|
237
|
+
try {
|
|
238
|
+
const collection = await this.getCollection({ indexName });
|
|
239
|
+
|
|
240
|
+
const defaultInclude: ['documents', 'metadatas'] = ['documents', 'metadatas'];
|
|
241
|
+
const translatedFilter = this.transformFilter(filter);
|
|
242
|
+
|
|
243
|
+
const result = await collection.get<T>({
|
|
244
|
+
ids,
|
|
245
|
+
where: translatedFilter ?? undefined,
|
|
246
|
+
whereDocument: documentFilter ?? undefined,
|
|
247
|
+
offset,
|
|
248
|
+
limit,
|
|
249
|
+
include: includeVector ? [...defaultInclude, 'embeddings'] : defaultInclude,
|
|
250
|
+
});
|
|
251
|
+
return result.rows();
|
|
252
|
+
} catch (error: any) {
|
|
253
|
+
if (error instanceof MastraError) throw error;
|
|
254
|
+
throw new MastraError(
|
|
255
|
+
{
|
|
256
|
+
id: 'CHROMA_VECTOR_GET_FAILED',
|
|
257
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
258
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
259
|
+
details: { indexName },
|
|
260
|
+
},
|
|
261
|
+
error,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
177
264
|
}
|
|
178
265
|
|
|
179
266
|
async listIndexes(): Promise<string[]> {
|
|
180
|
-
|
|
181
|
-
|
|
267
|
+
try {
|
|
268
|
+
const collections = await this.client.listCollections();
|
|
269
|
+
return collections.map(collection => collection.name);
|
|
270
|
+
} catch (error: any) {
|
|
271
|
+
throw new MastraError(
|
|
272
|
+
{
|
|
273
|
+
id: 'CHROMA_VECTOR_LIST_INDEXES_FAILED',
|
|
274
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
275
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
276
|
+
},
|
|
277
|
+
error,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
182
280
|
}
|
|
183
281
|
|
|
184
282
|
/**
|
|
185
283
|
* Retrieves statistics about a vector index.
|
|
186
284
|
*
|
|
187
|
-
* @param
|
|
188
|
-
* @param params.indexName - The name of the index to describe
|
|
285
|
+
* @param {string} indexName - The name of the index to describe
|
|
189
286
|
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
190
287
|
*/
|
|
191
|
-
async describeIndex(
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
288
|
+
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
289
|
+
try {
|
|
290
|
+
const collection = await this.getCollection({ indexName });
|
|
291
|
+
const count = await collection.count();
|
|
292
|
+
const metadata = collection.metadata as MastraMetadata | undefined;
|
|
293
|
+
const space = collection.configuration.hnsw?.space || collection.configuration.spann?.space || undefined;
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
dimension: metadata?.dimension || 0,
|
|
297
|
+
count,
|
|
298
|
+
metric: space ? (spaceMappings[space] as 'cosine' | 'euclidean' | 'dotproduct') : undefined,
|
|
299
|
+
};
|
|
300
|
+
} catch (error: any) {
|
|
301
|
+
if (error instanceof MastraError) throw error;
|
|
302
|
+
throw new MastraError(
|
|
303
|
+
{
|
|
304
|
+
id: 'CHROMA_VECTOR_DESCRIBE_INDEX_FAILED',
|
|
305
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
306
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
307
|
+
details: { indexName },
|
|
308
|
+
},
|
|
309
|
+
error,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
206
312
|
}
|
|
207
313
|
|
|
208
|
-
async deleteIndex(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
314
|
+
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
315
|
+
try {
|
|
316
|
+
await this.client.deleteCollection({ name: indexName });
|
|
317
|
+
this.collections.delete(indexName);
|
|
318
|
+
} catch (error: any) {
|
|
319
|
+
throw new MastraError(
|
|
320
|
+
{
|
|
321
|
+
id: 'CHROMA_VECTOR_DELETE_INDEX_FAILED',
|
|
322
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
323
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
324
|
+
details: { indexName },
|
|
325
|
+
},
|
|
326
|
+
error,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
213
329
|
}
|
|
214
330
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
this.logger.warn(
|
|
233
|
-
`Deprecation Warning: updateIndexById() is deprecated.
|
|
234
|
-
Please use updateVector() instead.
|
|
235
|
-
updateIndexById() will be removed on May 20th, 2025.`,
|
|
236
|
-
);
|
|
237
|
-
await this.updateVector({ indexName, id, update });
|
|
331
|
+
async forkIndex({ indexName, newIndexName }: { indexName: string; newIndexName: string }): Promise<void> {
|
|
332
|
+
try {
|
|
333
|
+
const collection = await this.getCollection({ indexName, forceUpdate: true });
|
|
334
|
+
const forkedCollection = await collection.fork({ name: newIndexName });
|
|
335
|
+
this.collections.set(newIndexName, forkedCollection);
|
|
336
|
+
} catch (error: any) {
|
|
337
|
+
if (error instanceof MastraError) throw error;
|
|
338
|
+
throw new MastraError(
|
|
339
|
+
{
|
|
340
|
+
id: 'CHROMA_INDEX_FORK_FAILED',
|
|
341
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
342
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
343
|
+
details: { indexName },
|
|
344
|
+
},
|
|
345
|
+
error,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
238
348
|
}
|
|
239
349
|
|
|
240
350
|
/**
|
|
@@ -247,66 +357,62 @@ export class ChromaVector extends MastraVector {
|
|
|
247
357
|
* @returns A promise that resolves when the update is complete.
|
|
248
358
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
249
359
|
*/
|
|
250
|
-
async updateVector(
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
360
|
+
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
361
|
+
if (!update.vector && !update.metadata) {
|
|
362
|
+
throw new MastraError({
|
|
363
|
+
id: 'CHROMA_VECTOR_UPDATE_NO_PAYLOAD',
|
|
364
|
+
text: 'No updates provided for vector',
|
|
365
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
366
|
+
category: ErrorCategory.USER,
|
|
367
|
+
details: { indexName, id },
|
|
368
|
+
});
|
|
369
|
+
}
|
|
257
370
|
|
|
258
|
-
|
|
371
|
+
try {
|
|
372
|
+
const collection: Collection = await this.getCollection({ indexName });
|
|
259
373
|
|
|
260
|
-
const
|
|
374
|
+
const updateRecordSet: RecordSet = { ids: [id] };
|
|
261
375
|
|
|
262
376
|
if (update?.vector) {
|
|
263
377
|
const stats = await this.describeIndex({ indexName });
|
|
264
378
|
this.validateVectorDimensions([update.vector], stats.dimension);
|
|
265
|
-
|
|
379
|
+
updateRecordSet.embeddings = [update.vector];
|
|
266
380
|
}
|
|
267
381
|
|
|
268
382
|
if (update?.metadata) {
|
|
269
|
-
|
|
383
|
+
updateRecordSet.metadatas = [update.metadata];
|
|
270
384
|
}
|
|
271
385
|
|
|
272
|
-
return await collection.update(
|
|
386
|
+
return await collection.update(updateRecordSet);
|
|
273
387
|
} catch (error: any) {
|
|
274
|
-
|
|
388
|
+
if (error instanceof MastraError) throw error;
|
|
389
|
+
throw new MastraError(
|
|
390
|
+
{
|
|
391
|
+
id: 'CHROMA_VECTOR_UPDATE_FAILED',
|
|
392
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
393
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
394
|
+
details: { indexName, id },
|
|
395
|
+
},
|
|
396
|
+
error,
|
|
397
|
+
);
|
|
275
398
|
}
|
|
276
399
|
}
|
|
277
400
|
|
|
278
|
-
|
|
279
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
280
|
-
*
|
|
281
|
-
* Deletes a vector by its ID.
|
|
282
|
-
* @param indexName - The name of the index containing the vector.
|
|
283
|
-
* @param id - The ID of the vector to delete.
|
|
284
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
285
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
286
|
-
*/
|
|
287
|
-
async deleteIndexById(indexName: string, id: string): Promise<void> {
|
|
288
|
-
this.logger.warn(
|
|
289
|
-
`Deprecation Warning: deleteIndexById() is deprecated. Please use deleteVector() instead. deleteIndexById() will be removed on May 20th.`,
|
|
290
|
-
);
|
|
291
|
-
await this.deleteVector(indexName, id);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Deletes a vector by its ID.
|
|
296
|
-
* @param indexName - The name of the index containing the vector.
|
|
297
|
-
* @param id - The ID of the vector to delete.
|
|
298
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
299
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
300
|
-
*/
|
|
301
|
-
async deleteVector(...args: ParamsToArgs<DeleteVectorParams>): Promise<void> {
|
|
302
|
-
const params = this.normalizeArgs<DeleteVectorParams>('deleteVector', args);
|
|
303
|
-
|
|
304
|
-
const { indexName, id } = params;
|
|
401
|
+
async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
|
|
305
402
|
try {
|
|
306
|
-
const collection: Collection = await this.getCollection(indexName
|
|
403
|
+
const collection: Collection = await this.getCollection({ indexName });
|
|
307
404
|
await collection.delete({ ids: [id] });
|
|
308
405
|
} catch (error: any) {
|
|
309
|
-
|
|
406
|
+
if (error instanceof MastraError) throw error;
|
|
407
|
+
throw new MastraError(
|
|
408
|
+
{
|
|
409
|
+
id: 'CHROMA_VECTOR_DELETE_FAILED',
|
|
410
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
411
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
412
|
+
details: { indexName, id },
|
|
413
|
+
},
|
|
414
|
+
error,
|
|
415
|
+
);
|
|
310
416
|
}
|
|
311
417
|
}
|
|
312
418
|
}
|
package/tsconfig.json
CHANGED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { generateTypes } from '@internal/types-builder';
|
|
2
|
+
import { defineConfig } from 'tsup';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
entry: ['src/index.ts'],
|
|
6
|
+
format: ['esm', 'cjs'],
|
|
7
|
+
clean: true,
|
|
8
|
+
dts: false,
|
|
9
|
+
splitting: true,
|
|
10
|
+
treeshake: {
|
|
11
|
+
preset: 'smallest',
|
|
12
|
+
},
|
|
13
|
+
sourcemap: true,
|
|
14
|
+
onSuccess: async () => {
|
|
15
|
+
await generateTypes(process.cwd());
|
|
16
|
+
},
|
|
17
|
+
});
|