@mastra/chroma 0.0.0-trigger-playground-ui-package-20250506151043 → 0.0.0-tsconfig-compile-20250703214351
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 +348 -4
- package/dist/_tsup-dts-rollup.d.cts +49 -22
- package/dist/_tsup-dts-rollup.d.ts +49 -22
- package/dist/index.cjs +214 -75
- package/dist/index.js +213 -74
- package/package.json +13 -10
- package/src/vector/filter.test.ts +24 -19
- package/src/vector/filter.ts +35 -4
- package/src/vector/index.test.ts +89 -138
- package/src/vector/index.ts +229 -110
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var error = require('@mastra/core/error');
|
|
3
4
|
var vector = require('@mastra/core/vector');
|
|
4
5
|
var chromadb = require('chromadb');
|
|
5
6
|
var filter = require('@mastra/core/vector/filter');
|
|
@@ -124,21 +125,32 @@ var ChromaVector = class extends vector.MastraVector {
|
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
|
-
async upsert(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
128
|
+
async upsert({ indexName, vectors, metadata, ids, documents }) {
|
|
129
|
+
try {
|
|
130
|
+
const collection = await this.getCollection(indexName);
|
|
131
|
+
const stats = await this.describeIndex({ indexName });
|
|
132
|
+
this.validateVectorDimensions(vectors, stats.dimension);
|
|
133
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
134
|
+
const normalizedMetadata = metadata || vectors.map(() => ({}));
|
|
135
|
+
await collection.upsert({
|
|
136
|
+
ids: generatedIds,
|
|
137
|
+
embeddings: vectors,
|
|
138
|
+
metadatas: normalizedMetadata,
|
|
139
|
+
documents
|
|
140
|
+
});
|
|
141
|
+
return generatedIds;
|
|
142
|
+
} catch (error$1) {
|
|
143
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
144
|
+
throw new error.MastraError(
|
|
145
|
+
{
|
|
146
|
+
id: "CHROMA_VECTOR_UPSERT_FAILED",
|
|
147
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
148
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
149
|
+
details: { indexName }
|
|
150
|
+
},
|
|
151
|
+
error$1
|
|
152
|
+
);
|
|
153
|
+
}
|
|
142
154
|
}
|
|
143
155
|
HnswSpaceMap = {
|
|
144
156
|
cosine: "cosine",
|
|
@@ -147,88 +159,215 @@ var ChromaVector = class extends vector.MastraVector {
|
|
|
147
159
|
l2: "euclidean",
|
|
148
160
|
ip: "dotproduct"
|
|
149
161
|
};
|
|
150
|
-
async createIndex(
|
|
151
|
-
const params = this.normalizeArgs("createIndex", args);
|
|
152
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
162
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
153
163
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
154
|
-
throw new
|
|
164
|
+
throw new error.MastraError({
|
|
165
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_INVALID_DIMENSION",
|
|
166
|
+
text: "Dimension must be a positive integer",
|
|
167
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
168
|
+
category: error.ErrorCategory.USER,
|
|
169
|
+
details: { dimension }
|
|
170
|
+
});
|
|
155
171
|
}
|
|
156
172
|
const hnswSpace = this.HnswSpaceMap[metric];
|
|
157
|
-
if (!["cosine", "l2", "ip"].includes(hnswSpace)) {
|
|
158
|
-
throw new
|
|
173
|
+
if (!hnswSpace || !["cosine", "l2", "ip"].includes(hnswSpace)) {
|
|
174
|
+
throw new error.MastraError({
|
|
175
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_INVALID_METRIC",
|
|
176
|
+
text: `Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`,
|
|
177
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
178
|
+
category: error.ErrorCategory.USER,
|
|
179
|
+
details: { metric }
|
|
180
|
+
});
|
|
159
181
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
182
|
+
try {
|
|
183
|
+
await this.client.createCollection({
|
|
184
|
+
name: indexName,
|
|
185
|
+
metadata: {
|
|
186
|
+
dimension,
|
|
187
|
+
"hnsw:space": hnswSpace
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
} catch (error$1) {
|
|
191
|
+
const message = error$1?.message || error$1?.toString();
|
|
192
|
+
if (message && message.toLowerCase().includes("already exists")) {
|
|
193
|
+
await this.validateExistingIndex(indexName, dimension, metric);
|
|
194
|
+
return;
|
|
165
195
|
}
|
|
166
|
-
|
|
196
|
+
throw new error.MastraError(
|
|
197
|
+
{
|
|
198
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_FAILED",
|
|
199
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
200
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
201
|
+
details: { indexName }
|
|
202
|
+
},
|
|
203
|
+
error$1
|
|
204
|
+
);
|
|
205
|
+
}
|
|
167
206
|
}
|
|
168
207
|
transformFilter(filter) {
|
|
169
208
|
const translator = new ChromaFilterTranslator();
|
|
170
209
|
return translator.translate(filter);
|
|
171
210
|
}
|
|
172
|
-
async query(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
211
|
+
async query({
|
|
212
|
+
indexName,
|
|
213
|
+
queryVector,
|
|
214
|
+
topK = 10,
|
|
215
|
+
filter,
|
|
216
|
+
includeVector = false,
|
|
217
|
+
documentFilter
|
|
218
|
+
}) {
|
|
219
|
+
try {
|
|
220
|
+
const collection = await this.getCollection(indexName, true);
|
|
221
|
+
const defaultInclude = ["documents", "metadatas", "distances"];
|
|
222
|
+
const translatedFilter = this.transformFilter(filter);
|
|
223
|
+
const results = await collection.query({
|
|
224
|
+
queryEmbeddings: [queryVector],
|
|
225
|
+
nResults: topK,
|
|
226
|
+
where: translatedFilter,
|
|
227
|
+
whereDocument: documentFilter,
|
|
228
|
+
include: includeVector ? [...defaultInclude, "embeddings"] : defaultInclude
|
|
229
|
+
});
|
|
230
|
+
return (results.ids[0] || []).map((id, index) => ({
|
|
231
|
+
id,
|
|
232
|
+
score: results.distances?.[0]?.[index] || 0,
|
|
233
|
+
metadata: results.metadatas?.[0]?.[index] || {},
|
|
234
|
+
document: results.documents?.[0]?.[index],
|
|
235
|
+
...includeVector && { vector: results.embeddings?.[0]?.[index] || [] }
|
|
236
|
+
}));
|
|
237
|
+
} catch (error$1) {
|
|
238
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
239
|
+
throw new error.MastraError(
|
|
240
|
+
{
|
|
241
|
+
id: "CHROMA_VECTOR_QUERY_FAILED",
|
|
242
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
243
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
244
|
+
details: { indexName }
|
|
245
|
+
},
|
|
246
|
+
error$1
|
|
247
|
+
);
|
|
248
|
+
}
|
|
192
249
|
}
|
|
193
250
|
async listIndexes() {
|
|
194
|
-
|
|
195
|
-
|
|
251
|
+
try {
|
|
252
|
+
const collections = await this.client.listCollections();
|
|
253
|
+
return collections.map((collection) => collection);
|
|
254
|
+
} catch (error$1) {
|
|
255
|
+
throw new error.MastraError(
|
|
256
|
+
{
|
|
257
|
+
id: "CHROMA_VECTOR_LIST_INDEXES_FAILED",
|
|
258
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
259
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
260
|
+
},
|
|
261
|
+
error$1
|
|
262
|
+
);
|
|
263
|
+
}
|
|
196
264
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
265
|
+
/**
|
|
266
|
+
* Retrieves statistics about a vector index.
|
|
267
|
+
*
|
|
268
|
+
* @param {string} indexName - The name of the index to describe
|
|
269
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
270
|
+
*/
|
|
271
|
+
async describeIndex({ indexName }) {
|
|
272
|
+
try {
|
|
273
|
+
const collection = await this.getCollection(indexName);
|
|
274
|
+
const count = await collection.count();
|
|
275
|
+
const metadata = collection.metadata;
|
|
276
|
+
const hnswSpace = metadata?.["hnsw:space"];
|
|
277
|
+
return {
|
|
278
|
+
dimension: metadata?.dimension || 0,
|
|
279
|
+
count,
|
|
280
|
+
metric: this.HnswSpaceMap[hnswSpace]
|
|
281
|
+
};
|
|
282
|
+
} catch (error$1) {
|
|
283
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
284
|
+
throw new error.MastraError(
|
|
285
|
+
{
|
|
286
|
+
id: "CHROMA_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
287
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
288
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
289
|
+
details: { indexName }
|
|
290
|
+
},
|
|
291
|
+
error$1
|
|
292
|
+
);
|
|
293
|
+
}
|
|
207
294
|
}
|
|
208
|
-
async deleteIndex(indexName) {
|
|
209
|
-
|
|
210
|
-
|
|
295
|
+
async deleteIndex({ indexName }) {
|
|
296
|
+
try {
|
|
297
|
+
await this.client.deleteCollection({ name: indexName });
|
|
298
|
+
this.collections.delete(indexName);
|
|
299
|
+
} catch (error$1) {
|
|
300
|
+
throw new error.MastraError(
|
|
301
|
+
{
|
|
302
|
+
id: "CHROMA_VECTOR_DELETE_INDEX_FAILED",
|
|
303
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
304
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
305
|
+
details: { indexName }
|
|
306
|
+
},
|
|
307
|
+
error$1
|
|
308
|
+
);
|
|
309
|
+
}
|
|
211
310
|
}
|
|
212
|
-
|
|
311
|
+
/**
|
|
312
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
313
|
+
* @param indexName - The name of the index containing the vector.
|
|
314
|
+
* @param id - The ID of the vector to update.
|
|
315
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
316
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
317
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
318
|
+
* @returns A promise that resolves when the update is complete.
|
|
319
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
320
|
+
*/
|
|
321
|
+
async updateVector({ indexName, id, update }) {
|
|
213
322
|
if (!update.vector && !update.metadata) {
|
|
214
|
-
throw new
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
323
|
+
throw new error.MastraError({
|
|
324
|
+
id: "CHROMA_VECTOR_UPDATE_NO_PAYLOAD",
|
|
325
|
+
text: "No updates provided for vector",
|
|
326
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
327
|
+
category: error.ErrorCategory.USER,
|
|
328
|
+
details: { indexName, id }
|
|
329
|
+
});
|
|
220
330
|
}
|
|
221
|
-
|
|
222
|
-
|
|
331
|
+
try {
|
|
332
|
+
const collection = await this.getCollection(indexName, true);
|
|
333
|
+
const updateOptions = { ids: [id] };
|
|
334
|
+
if (update?.vector) {
|
|
335
|
+
const stats = await this.describeIndex({ indexName });
|
|
336
|
+
this.validateVectorDimensions([update.vector], stats.dimension);
|
|
337
|
+
updateOptions.embeddings = [update.vector];
|
|
338
|
+
}
|
|
339
|
+
if (update?.metadata) {
|
|
340
|
+
updateOptions.metadatas = [update.metadata];
|
|
341
|
+
}
|
|
342
|
+
return await collection.update(updateOptions);
|
|
343
|
+
} catch (error$1) {
|
|
344
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
345
|
+
throw new error.MastraError(
|
|
346
|
+
{
|
|
347
|
+
id: "CHROMA_VECTOR_UPDATE_FAILED",
|
|
348
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
349
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
350
|
+
details: { indexName, id }
|
|
351
|
+
},
|
|
352
|
+
error$1
|
|
353
|
+
);
|
|
223
354
|
}
|
|
224
|
-
return await collection.update(updateOptions);
|
|
225
355
|
}
|
|
226
|
-
async
|
|
356
|
+
async deleteVector({ indexName, id }) {
|
|
227
357
|
try {
|
|
228
358
|
const collection = await this.getCollection(indexName, true);
|
|
229
359
|
await collection.delete({ ids: [id] });
|
|
230
|
-
} catch (error) {
|
|
231
|
-
|
|
360
|
+
} catch (error$1) {
|
|
361
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
362
|
+
throw new error.MastraError(
|
|
363
|
+
{
|
|
364
|
+
id: "CHROMA_VECTOR_DELETE_FAILED",
|
|
365
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
366
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
367
|
+
details: { indexName, id }
|
|
368
|
+
},
|
|
369
|
+
error$1
|
|
370
|
+
);
|
|
232
371
|
}
|
|
233
372
|
}
|
|
234
373
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
1
2
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
3
|
import { ChromaClient } from 'chromadb';
|
|
3
4
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
@@ -122,21 +123,32 @@ var ChromaVector = class extends MastraVector {
|
|
|
122
123
|
}
|
|
123
124
|
}
|
|
124
125
|
}
|
|
125
|
-
async upsert(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
126
|
+
async upsert({ indexName, vectors, metadata, ids, documents }) {
|
|
127
|
+
try {
|
|
128
|
+
const collection = await this.getCollection(indexName);
|
|
129
|
+
const stats = await this.describeIndex({ indexName });
|
|
130
|
+
this.validateVectorDimensions(vectors, stats.dimension);
|
|
131
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
132
|
+
const normalizedMetadata = metadata || vectors.map(() => ({}));
|
|
133
|
+
await collection.upsert({
|
|
134
|
+
ids: generatedIds,
|
|
135
|
+
embeddings: vectors,
|
|
136
|
+
metadatas: normalizedMetadata,
|
|
137
|
+
documents
|
|
138
|
+
});
|
|
139
|
+
return generatedIds;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (error instanceof MastraError) throw error;
|
|
142
|
+
throw new MastraError(
|
|
143
|
+
{
|
|
144
|
+
id: "CHROMA_VECTOR_UPSERT_FAILED",
|
|
145
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
146
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
147
|
+
details: { indexName }
|
|
148
|
+
},
|
|
149
|
+
error
|
|
150
|
+
);
|
|
151
|
+
}
|
|
140
152
|
}
|
|
141
153
|
HnswSpaceMap = {
|
|
142
154
|
cosine: "cosine",
|
|
@@ -145,88 +157,215 @@ var ChromaVector = class extends MastraVector {
|
|
|
145
157
|
l2: "euclidean",
|
|
146
158
|
ip: "dotproduct"
|
|
147
159
|
};
|
|
148
|
-
async createIndex(
|
|
149
|
-
const params = this.normalizeArgs("createIndex", args);
|
|
150
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
160
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
151
161
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
152
|
-
throw new
|
|
162
|
+
throw new MastraError({
|
|
163
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_INVALID_DIMENSION",
|
|
164
|
+
text: "Dimension must be a positive integer",
|
|
165
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
166
|
+
category: ErrorCategory.USER,
|
|
167
|
+
details: { dimension }
|
|
168
|
+
});
|
|
153
169
|
}
|
|
154
170
|
const hnswSpace = this.HnswSpaceMap[metric];
|
|
155
|
-
if (!["cosine", "l2", "ip"].includes(hnswSpace)) {
|
|
156
|
-
throw new
|
|
171
|
+
if (!hnswSpace || !["cosine", "l2", "ip"].includes(hnswSpace)) {
|
|
172
|
+
throw new MastraError({
|
|
173
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_INVALID_METRIC",
|
|
174
|
+
text: `Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`,
|
|
175
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
176
|
+
category: ErrorCategory.USER,
|
|
177
|
+
details: { metric }
|
|
178
|
+
});
|
|
157
179
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
180
|
+
try {
|
|
181
|
+
await this.client.createCollection({
|
|
182
|
+
name: indexName,
|
|
183
|
+
metadata: {
|
|
184
|
+
dimension,
|
|
185
|
+
"hnsw:space": hnswSpace
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const message = error?.message || error?.toString();
|
|
190
|
+
if (message && message.toLowerCase().includes("already exists")) {
|
|
191
|
+
await this.validateExistingIndex(indexName, dimension, metric);
|
|
192
|
+
return;
|
|
163
193
|
}
|
|
164
|
-
|
|
194
|
+
throw new MastraError(
|
|
195
|
+
{
|
|
196
|
+
id: "CHROMA_VECTOR_CREATE_INDEX_FAILED",
|
|
197
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
198
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
199
|
+
details: { indexName }
|
|
200
|
+
},
|
|
201
|
+
error
|
|
202
|
+
);
|
|
203
|
+
}
|
|
165
204
|
}
|
|
166
205
|
transformFilter(filter) {
|
|
167
206
|
const translator = new ChromaFilterTranslator();
|
|
168
207
|
return translator.translate(filter);
|
|
169
208
|
}
|
|
170
|
-
async query(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
209
|
+
async query({
|
|
210
|
+
indexName,
|
|
211
|
+
queryVector,
|
|
212
|
+
topK = 10,
|
|
213
|
+
filter,
|
|
214
|
+
includeVector = false,
|
|
215
|
+
documentFilter
|
|
216
|
+
}) {
|
|
217
|
+
try {
|
|
218
|
+
const collection = await this.getCollection(indexName, true);
|
|
219
|
+
const defaultInclude = ["documents", "metadatas", "distances"];
|
|
220
|
+
const translatedFilter = this.transformFilter(filter);
|
|
221
|
+
const results = await collection.query({
|
|
222
|
+
queryEmbeddings: [queryVector],
|
|
223
|
+
nResults: topK,
|
|
224
|
+
where: translatedFilter,
|
|
225
|
+
whereDocument: documentFilter,
|
|
226
|
+
include: includeVector ? [...defaultInclude, "embeddings"] : defaultInclude
|
|
227
|
+
});
|
|
228
|
+
return (results.ids[0] || []).map((id, index) => ({
|
|
229
|
+
id,
|
|
230
|
+
score: results.distances?.[0]?.[index] || 0,
|
|
231
|
+
metadata: results.metadatas?.[0]?.[index] || {},
|
|
232
|
+
document: results.documents?.[0]?.[index],
|
|
233
|
+
...includeVector && { vector: results.embeddings?.[0]?.[index] || [] }
|
|
234
|
+
}));
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (error instanceof MastraError) throw error;
|
|
237
|
+
throw new MastraError(
|
|
238
|
+
{
|
|
239
|
+
id: "CHROMA_VECTOR_QUERY_FAILED",
|
|
240
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
241
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
242
|
+
details: { indexName }
|
|
243
|
+
},
|
|
244
|
+
error
|
|
245
|
+
);
|
|
246
|
+
}
|
|
190
247
|
}
|
|
191
248
|
async listIndexes() {
|
|
192
|
-
|
|
193
|
-
|
|
249
|
+
try {
|
|
250
|
+
const collections = await this.client.listCollections();
|
|
251
|
+
return collections.map((collection) => collection);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new MastraError(
|
|
254
|
+
{
|
|
255
|
+
id: "CHROMA_VECTOR_LIST_INDEXES_FAILED",
|
|
256
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
257
|
+
category: ErrorCategory.THIRD_PARTY
|
|
258
|
+
},
|
|
259
|
+
error
|
|
260
|
+
);
|
|
261
|
+
}
|
|
194
262
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Retrieves statistics about a vector index.
|
|
265
|
+
*
|
|
266
|
+
* @param {string} indexName - The name of the index to describe
|
|
267
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
268
|
+
*/
|
|
269
|
+
async describeIndex({ indexName }) {
|
|
270
|
+
try {
|
|
271
|
+
const collection = await this.getCollection(indexName);
|
|
272
|
+
const count = await collection.count();
|
|
273
|
+
const metadata = collection.metadata;
|
|
274
|
+
const hnswSpace = metadata?.["hnsw:space"];
|
|
275
|
+
return {
|
|
276
|
+
dimension: metadata?.dimension || 0,
|
|
277
|
+
count,
|
|
278
|
+
metric: this.HnswSpaceMap[hnswSpace]
|
|
279
|
+
};
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (error instanceof MastraError) throw error;
|
|
282
|
+
throw new MastraError(
|
|
283
|
+
{
|
|
284
|
+
id: "CHROMA_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
285
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
286
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
287
|
+
details: { indexName }
|
|
288
|
+
},
|
|
289
|
+
error
|
|
290
|
+
);
|
|
291
|
+
}
|
|
205
292
|
}
|
|
206
|
-
async deleteIndex(indexName) {
|
|
207
|
-
|
|
208
|
-
|
|
293
|
+
async deleteIndex({ indexName }) {
|
|
294
|
+
try {
|
|
295
|
+
await this.client.deleteCollection({ name: indexName });
|
|
296
|
+
this.collections.delete(indexName);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
throw new MastraError(
|
|
299
|
+
{
|
|
300
|
+
id: "CHROMA_VECTOR_DELETE_INDEX_FAILED",
|
|
301
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
302
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
303
|
+
details: { indexName }
|
|
304
|
+
},
|
|
305
|
+
error
|
|
306
|
+
);
|
|
307
|
+
}
|
|
209
308
|
}
|
|
210
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
311
|
+
* @param indexName - The name of the index containing the vector.
|
|
312
|
+
* @param id - The ID of the vector to update.
|
|
313
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
314
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
315
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
316
|
+
* @returns A promise that resolves when the update is complete.
|
|
317
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
318
|
+
*/
|
|
319
|
+
async updateVector({ indexName, id, update }) {
|
|
211
320
|
if (!update.vector && !update.metadata) {
|
|
212
|
-
throw new
|
|
321
|
+
throw new MastraError({
|
|
322
|
+
id: "CHROMA_VECTOR_UPDATE_NO_PAYLOAD",
|
|
323
|
+
text: "No updates provided for vector",
|
|
324
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
325
|
+
category: ErrorCategory.USER,
|
|
326
|
+
details: { indexName, id }
|
|
327
|
+
});
|
|
213
328
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
329
|
+
try {
|
|
330
|
+
const collection = await this.getCollection(indexName, true);
|
|
331
|
+
const updateOptions = { ids: [id] };
|
|
332
|
+
if (update?.vector) {
|
|
333
|
+
const stats = await this.describeIndex({ indexName });
|
|
334
|
+
this.validateVectorDimensions([update.vector], stats.dimension);
|
|
335
|
+
updateOptions.embeddings = [update.vector];
|
|
336
|
+
}
|
|
337
|
+
if (update?.metadata) {
|
|
338
|
+
updateOptions.metadatas = [update.metadata];
|
|
339
|
+
}
|
|
340
|
+
return await collection.update(updateOptions);
|
|
341
|
+
} catch (error) {
|
|
342
|
+
if (error instanceof MastraError) throw error;
|
|
343
|
+
throw new MastraError(
|
|
344
|
+
{
|
|
345
|
+
id: "CHROMA_VECTOR_UPDATE_FAILED",
|
|
346
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
347
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
348
|
+
details: { indexName, id }
|
|
349
|
+
},
|
|
350
|
+
error
|
|
351
|
+
);
|
|
221
352
|
}
|
|
222
|
-
return await collection.update(updateOptions);
|
|
223
353
|
}
|
|
224
|
-
async
|
|
354
|
+
async deleteVector({ indexName, id }) {
|
|
225
355
|
try {
|
|
226
356
|
const collection = await this.getCollection(indexName, true);
|
|
227
357
|
await collection.delete({ ids: [id] });
|
|
228
358
|
} catch (error) {
|
|
229
|
-
|
|
359
|
+
if (error instanceof MastraError) throw error;
|
|
360
|
+
throw new MastraError(
|
|
361
|
+
{
|
|
362
|
+
id: "CHROMA_VECTOR_DELETE_FAILED",
|
|
363
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
364
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
365
|
+
details: { indexName, id }
|
|
366
|
+
},
|
|
367
|
+
error
|
|
368
|
+
);
|
|
230
369
|
}
|
|
231
370
|
}
|
|
232
371
|
};
|