@mastra/astra 1.1.0 → 1.1.1-alpha.0

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/dist/index.js CHANGED
@@ -1,369 +1,325 @@
1
- import { DataAPIClient, UUID } from '@datastax/astra-db-ts';
2
- import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { createVectorErrorId } from '@mastra/core/storage';
4
- import { MastraVector } from '@mastra/core/vector';
5
- import { BaseFilterTranslator } from '@mastra/core/vector/filter';
6
-
7
- // src/vector/index.ts
1
+ import { DataAPIClient, UUID } from "@datastax/astra-db-ts";
2
+ import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
3
+ import { createVectorErrorId } from "@mastra/core/storage";
4
+ import { MastraVector } from "@mastra/core/vector";
5
+ import { BaseFilterTranslator } from "@mastra/core/vector/filter";
6
+ //#region src/vector/filter.ts
7
+ /**
8
+ * Translator for Astra DB filter queries.
9
+ * Maintains MongoDB-compatible syntax while ensuring proper validation
10
+ * and normalization of values.
11
+ */
8
12
  var AstraFilterTranslator = class extends BaseFilterTranslator {
9
- getSupportedOperators() {
10
- return {
11
- ...BaseFilterTranslator.DEFAULT_OPERATORS,
12
- array: ["$all", "$in", "$nin"],
13
- logical: ["$and", "$or", "$not"],
14
- regex: [],
15
- custom: ["$size"]
16
- };
17
- }
18
- translate(filter) {
19
- if (this.isEmpty(filter)) return filter;
20
- this.validateFilter(filter);
21
- return this.translateNode(filter);
22
- }
23
- translateNode(node) {
24
- if (this.isRegex(node)) {
25
- throw new Error("Regex is not supported in Astra DB");
26
- }
27
- if (this.isPrimitive(node) || Array.isArray(node)) {
28
- return node;
29
- }
30
- const entries = Object.entries(node);
31
- const translatedEntries = entries.map(([key, value]) => {
32
- if (this.isOperator(key)) {
33
- return [key, this.translateOperatorValue(key, value)];
34
- }
35
- return [key, this.translateNode(value)];
36
- });
37
- return Object.fromEntries(translatedEntries);
38
- }
39
- translateOperatorValue(operator, value) {
40
- if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) {
41
- return this.normalizeComparisonValue(value);
42
- }
43
- if (this.isArrayOperator(operator) && Array.isArray(value)) {
44
- return this.normalizeArrayValues(value);
45
- }
46
- return this.translateNode(value);
47
- }
13
+ getSupportedOperators() {
14
+ return {
15
+ ...BaseFilterTranslator.DEFAULT_OPERATORS,
16
+ array: [
17
+ "$all",
18
+ "$in",
19
+ "$nin"
20
+ ],
21
+ logical: [
22
+ "$and",
23
+ "$or",
24
+ "$not"
25
+ ],
26
+ regex: [],
27
+ custom: ["$size"]
28
+ };
29
+ }
30
+ translate(filter) {
31
+ if (this.isEmpty(filter)) return filter;
32
+ this.validateFilter(filter);
33
+ return this.translateNode(filter);
34
+ }
35
+ translateNode(node) {
36
+ if (this.isRegex(node)) throw new Error("Regex is not supported in Astra DB");
37
+ if (this.isPrimitive(node) || Array.isArray(node)) return node;
38
+ const translatedEntries = Object.entries(node).map(([key, value]) => {
39
+ if (this.isOperator(key)) return [key, this.translateOperatorValue(key, value)];
40
+ return [key, this.translateNode(value)];
41
+ });
42
+ return Object.fromEntries(translatedEntries);
43
+ }
44
+ translateOperatorValue(operator, value) {
45
+ if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) return this.normalizeComparisonValue(value);
46
+ if (this.isArrayOperator(operator) && Array.isArray(value)) return this.normalizeArrayValues(value);
47
+ return this.translateNode(value);
48
+ }
48
49
  };
49
-
50
- // src/vector/index.ts
51
- var metricMap = {
52
- cosine: "cosine",
53
- euclidean: "euclidean",
54
- dotproduct: "dot_product"
50
+ //#endregion
51
+ //#region src/vector/index.ts
52
+ const metricMap = {
53
+ cosine: "cosine",
54
+ euclidean: "euclidean",
55
+ dotproduct: "dot_product"
55
56
  };
56
57
  var AstraVector = class extends MastraVector {
57
- #db;
58
- constructor({ token, endpoint, keyspace, id }) {
59
- super({ id });
60
- const client = new DataAPIClient(token);
61
- this.#db = client.db(endpoint, { keyspace });
62
- }
63
- /**
64
- * Creates a new collection with the specified configuration.
65
- *
66
- * @param {string} indexName - The name of the collection to create.
67
- * @param {number} dimension - The dimension of the vectors to be stored in the collection.
68
- * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
69
- * @returns {Promise<void>} A promise that resolves when the collection is created.
70
- */
71
- async createIndex({ indexName, dimension, metric = "cosine" }) {
72
- if (!Number.isInteger(dimension) || dimension <= 0) {
73
- throw new MastraError({
74
- id: createVectorErrorId("ASTRA", "CREATE_INDEX", "INVALID_DIMENSION"),
75
- text: "Dimension must be a positive integer",
76
- domain: ErrorDomain.MASTRA_VECTOR,
77
- category: ErrorCategory.USER
78
- });
79
- }
80
- try {
81
- await this.#db.createCollection(indexName, {
82
- vector: {
83
- dimension,
84
- metric: metricMap[metric]
85
- },
86
- checkExists: false
87
- });
88
- } catch (error) {
89
- new MastraError(
90
- {
91
- id: createVectorErrorId("ASTRA", "CREATE_INDEX", "DB_ERROR"),
92
- domain: ErrorDomain.MASTRA_VECTOR,
93
- category: ErrorCategory.THIRD_PARTY,
94
- details: { indexName }
95
- },
96
- error
97
- );
98
- }
99
- }
100
- /**
101
- * Inserts or updates vectors in the specified collection.
102
- *
103
- * @param {string} indexName - The name of the collection to upsert into.
104
- * @param {number[][]} vectors - An array of vectors to upsert.
105
- * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
106
- * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
107
- * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
108
- */
109
- async upsert({ indexName, vectors, metadata, ids }) {
110
- const collection = this.#db.collection(indexName);
111
- const vectorIds = ids || vectors.map(() => UUID.v7().toString());
112
- const records = vectors.map((vector, i) => ({
113
- id: vectorIds[i],
114
- $vector: vector,
115
- metadata: metadata?.[i] || {}
116
- }));
117
- try {
118
- await collection.insertMany(records);
119
- } catch (error) {
120
- throw new MastraError(
121
- {
122
- id: createVectorErrorId("ASTRA", "UPSERT", "DB_ERROR"),
123
- domain: ErrorDomain.MASTRA_VECTOR,
124
- category: ErrorCategory.THIRD_PARTY,
125
- details: { indexName }
126
- },
127
- error
128
- );
129
- }
130
- return vectorIds;
131
- }
132
- transformFilter(filter) {
133
- const translator = new AstraFilterTranslator();
134
- return translator.translate(filter);
135
- }
136
- /**
137
- * Queries the specified collection using a vector and optional filter.
138
- *
139
- * @param {string} indexName - The name of the collection to query.
140
- * @param {number[]} queryVector - The vector to query with.
141
- * @param {number} [topK] - The maximum number of results to return.
142
- * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators
143
- * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
144
- * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
145
- */
146
- async query({
147
- indexName,
148
- queryVector,
149
- topK = 10,
150
- filter,
151
- includeVector = false
152
- }) {
153
- if (!queryVector) {
154
- throw new MastraError({
155
- id: createVectorErrorId("ASTRA", "QUERY", "MISSING_VECTOR"),
156
- text: "queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.",
157
- domain: ErrorDomain.MASTRA_VECTOR,
158
- category: ErrorCategory.USER,
159
- details: { indexName }
160
- });
161
- }
162
- const collection = this.#db.collection(indexName);
163
- const translatedFilter = this.transformFilter(filter);
164
- try {
165
- const cursor = collection.find(translatedFilter ?? {}, {
166
- sort: { $vector: queryVector },
167
- limit: topK,
168
- includeSimilarity: true,
169
- projection: {
170
- $vector: includeVector ? true : false
171
- }
172
- });
173
- const results = await cursor.toArray();
174
- return results.map((result) => ({
175
- id: result.id,
176
- score: result.$similarity,
177
- metadata: result.metadata,
178
- ...includeVector && { vector: result.$vector }
179
- }));
180
- } catch (error) {
181
- throw new MastraError(
182
- {
183
- id: createVectorErrorId("ASTRA", "QUERY", "DB_ERROR"),
184
- domain: ErrorDomain.MASTRA_VECTOR,
185
- category: ErrorCategory.THIRD_PARTY,
186
- details: { indexName }
187
- },
188
- error
189
- );
190
- }
191
- }
192
- /**
193
- * Lists all collections in the database.
194
- *
195
- * @returns {Promise<string[]>} A promise that resolves to an array of collection names.
196
- */
197
- async listIndexes() {
198
- try {
199
- return await this.#db.listCollections({ nameOnly: true });
200
- } catch (error) {
201
- throw new MastraError(
202
- {
203
- id: createVectorErrorId("ASTRA", "LIST_INDEXES", "DB_ERROR"),
204
- domain: ErrorDomain.MASTRA_VECTOR,
205
- category: ErrorCategory.THIRD_PARTY
206
- },
207
- error
208
- );
209
- }
210
- }
211
- /**
212
- * Retrieves statistics about a vector index.
213
- *
214
- * @param {string} indexName - The name of the index to describe
215
- * @returns A promise that resolves to the index statistics including dimension, count and metric
216
- */
217
- async describeIndex({ indexName }) {
218
- const collection = this.#db.collection(indexName);
219
- try {
220
- const optionsPromise = collection.options();
221
- const countPromise = collection.countDocuments({}, 100);
222
- const [options, count] = await Promise.all([optionsPromise, countPromise]);
223
- const keys = Object.keys(metricMap);
224
- const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
225
- return {
226
- dimension: options.vector?.dimension,
227
- metric,
228
- count
229
- };
230
- } catch (error) {
231
- if (error instanceof MastraError) throw error;
232
- throw new MastraError(
233
- {
234
- id: createVectorErrorId("ASTRA", "DESCRIBE_INDEX", "DB_ERROR"),
235
- domain: ErrorDomain.MASTRA_VECTOR,
236
- category: ErrorCategory.THIRD_PARTY,
237
- details: { indexName }
238
- },
239
- error
240
- );
241
- }
242
- }
243
- /**
244
- * Deletes the specified collection.
245
- *
246
- * @param {string} indexName - The name of the collection to delete.
247
- * @returns {Promise<void>} A promise that resolves when the collection is deleted.
248
- */
249
- async deleteIndex({ indexName }) {
250
- const collection = this.#db.collection(indexName);
251
- try {
252
- await collection.drop();
253
- } catch (error) {
254
- throw new MastraError(
255
- {
256
- id: createVectorErrorId("ASTRA", "DELETE_INDEX", "DB_ERROR"),
257
- domain: ErrorDomain.MASTRA_VECTOR,
258
- category: ErrorCategory.THIRD_PARTY,
259
- details: { indexName }
260
- },
261
- error
262
- );
263
- }
264
- }
265
- /**
266
- * Updates a vector by its ID with the provided vector and/or metadata.
267
- * @param indexName - The name of the index containing the vector.
268
- * @param id - The ID of the vector to update.
269
- * @param update - An object containing the vector and/or metadata to update.
270
- * @param update.vector - An optional array of numbers representing the new vector.
271
- * @param update.metadata - An optional record containing the new metadata.
272
- * @returns A promise that resolves when the update is complete.
273
- * @throws Will throw an error if no updates are provided or if the update operation fails.
274
- */
275
- async updateVector({ indexName, id, update }) {
276
- if (!id) {
277
- throw new MastraError({
278
- id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "NO_ID"),
279
- text: "id is required for Astra updateVector",
280
- domain: ErrorDomain.MASTRA_VECTOR,
281
- category: ErrorCategory.USER,
282
- details: { indexName }
283
- });
284
- }
285
- if (!update.vector && !update.metadata) {
286
- throw new MastraError({
287
- id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "NO_PAYLOAD"),
288
- text: "No updates provided for vector",
289
- domain: ErrorDomain.MASTRA_VECTOR,
290
- category: ErrorCategory.USER,
291
- details: {
292
- indexName,
293
- id
294
- }
295
- });
296
- }
297
- try {
298
- const collection = this.#db.collection(indexName);
299
- const updateDoc = {};
300
- if (update.vector) {
301
- updateDoc.$vector = update.vector;
302
- }
303
- if (update.metadata) {
304
- updateDoc.metadata = update.metadata;
305
- }
306
- await collection.findOneAndUpdate({ id }, { $set: updateDoc });
307
- } catch (error) {
308
- if (error instanceof MastraError) throw error;
309
- throw new MastraError(
310
- {
311
- id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "FAILED_UNHANDLED"),
312
- domain: ErrorDomain.MASTRA_VECTOR,
313
- category: ErrorCategory.THIRD_PARTY,
314
- details: {
315
- indexName,
316
- ...id && { id }
317
- }
318
- },
319
- error
320
- );
321
- }
322
- }
323
- /**
324
- * Deletes a vector by its ID.
325
- * @param indexName - The name of the index containing the vector.
326
- * @param id - The ID of the vector to delete.
327
- * @returns A promise that resolves when the deletion is complete.
328
- * @throws Will throw an error if the deletion operation fails.
329
- */
330
- async deleteVector({ indexName, id }) {
331
- try {
332
- const collection = this.#db.collection(indexName);
333
- await collection.deleteOne({ id });
334
- } catch (error) {
335
- if (error instanceof MastraError) throw error;
336
- throw new MastraError(
337
- {
338
- id: createVectorErrorId("ASTRA", "DELETE_VECTOR", "FAILED"),
339
- domain: ErrorDomain.MASTRA_VECTOR,
340
- category: ErrorCategory.THIRD_PARTY,
341
- details: {
342
- indexName,
343
- ...id && { id }
344
- }
345
- },
346
- error
347
- );
348
- }
349
- }
350
- async deleteVectors({ indexName, filter, ids }) {
351
- throw new MastraError({
352
- id: createVectorErrorId("ASTRA", "DELETE_VECTORS", "NOT_SUPPORTED"),
353
- text: "deleteVectors is not yet implemented for Astra vector store",
354
- domain: ErrorDomain.MASTRA_VECTOR,
355
- category: ErrorCategory.SYSTEM,
356
- details: {
357
- indexName,
358
- ...filter && { filter: JSON.stringify(filter) },
359
- ...ids && { idsCount: ids.length }
360
- }
361
- });
362
- }
58
+ #db;
59
+ constructor({ token, endpoint, keyspace, id }) {
60
+ super({ id });
61
+ const client = new DataAPIClient(token);
62
+ this.#db = client.db(endpoint, { keyspace });
63
+ }
64
+ /**
65
+ * Creates a new collection with the specified configuration.
66
+ *
67
+ * @param {string} indexName - The name of the collection to create.
68
+ * @param {number} dimension - The dimension of the vectors to be stored in the collection.
69
+ * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
70
+ * @returns {Promise<void>} A promise that resolves when the collection is created.
71
+ */
72
+ async createIndex({ indexName, dimension, metric = "cosine" }) {
73
+ if (!Number.isInteger(dimension) || dimension <= 0) throw new MastraError({
74
+ id: createVectorErrorId("ASTRA", "CREATE_INDEX", "INVALID_DIMENSION"),
75
+ text: "Dimension must be a positive integer",
76
+ domain: ErrorDomain.MASTRA_VECTOR,
77
+ category: ErrorCategory.USER
78
+ });
79
+ try {
80
+ await this.#db.createCollection(indexName, {
81
+ vector: {
82
+ dimension,
83
+ metric: metricMap[metric]
84
+ },
85
+ checkExists: false
86
+ });
87
+ } catch (error) {
88
+ new MastraError({
89
+ id: createVectorErrorId("ASTRA", "CREATE_INDEX", "DB_ERROR"),
90
+ domain: ErrorDomain.MASTRA_VECTOR,
91
+ category: ErrorCategory.THIRD_PARTY,
92
+ details: { indexName }
93
+ }, error);
94
+ }
95
+ }
96
+ /**
97
+ * Inserts or updates vectors in the specified collection.
98
+ *
99
+ * @param {string} indexName - The name of the collection to upsert into.
100
+ * @param {number[][]} vectors - An array of vectors to upsert.
101
+ * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
102
+ * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
103
+ * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
104
+ */
105
+ async upsert({ indexName, vectors, metadata, ids }) {
106
+ const collection = this.#db.collection(indexName);
107
+ const vectorIds = ids || vectors.map(() => UUID.v7().toString());
108
+ const records = vectors.map((vector, i) => ({
109
+ id: vectorIds[i],
110
+ $vector: vector,
111
+ metadata: metadata?.[i] || {}
112
+ }));
113
+ try {
114
+ await collection.insertMany(records);
115
+ } catch (error) {
116
+ throw new MastraError({
117
+ id: createVectorErrorId("ASTRA", "UPSERT", "DB_ERROR"),
118
+ domain: ErrorDomain.MASTRA_VECTOR,
119
+ category: ErrorCategory.THIRD_PARTY,
120
+ details: { indexName }
121
+ }, error);
122
+ }
123
+ return vectorIds;
124
+ }
125
+ transformFilter(filter) {
126
+ return new AstraFilterTranslator().translate(filter);
127
+ }
128
+ /**
129
+ * Queries the specified collection using a vector and optional filter.
130
+ *
131
+ * @param {string} indexName - The name of the collection to query.
132
+ * @param {number[]} queryVector - The vector to query with.
133
+ * @param {number} [topK] - The maximum number of results to return.
134
+ * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators
135
+ * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
136
+ * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
137
+ */
138
+ async query({ indexName, queryVector, topK = 10, filter, includeVector = false }) {
139
+ if (!queryVector) throw new MastraError({
140
+ id: createVectorErrorId("ASTRA", "QUERY", "MISSING_VECTOR"),
141
+ text: "queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.",
142
+ domain: ErrorDomain.MASTRA_VECTOR,
143
+ category: ErrorCategory.USER,
144
+ details: { indexName }
145
+ });
146
+ const collection = this.#db.collection(indexName);
147
+ const translatedFilter = this.transformFilter(filter);
148
+ try {
149
+ return (await collection.find(translatedFilter ?? {}, {
150
+ sort: { $vector: queryVector },
151
+ limit: topK,
152
+ includeSimilarity: true,
153
+ projection: { $vector: includeVector ? true : false }
154
+ }).toArray()).map((result) => ({
155
+ id: result.id,
156
+ score: result.$similarity,
157
+ metadata: result.metadata,
158
+ ...includeVector && { vector: result.$vector }
159
+ }));
160
+ } catch (error) {
161
+ throw new MastraError({
162
+ id: createVectorErrorId("ASTRA", "QUERY", "DB_ERROR"),
163
+ domain: ErrorDomain.MASTRA_VECTOR,
164
+ category: ErrorCategory.THIRD_PARTY,
165
+ details: { indexName }
166
+ }, error);
167
+ }
168
+ }
169
+ /**
170
+ * Lists all collections in the database.
171
+ *
172
+ * @returns {Promise<string[]>} A promise that resolves to an array of collection names.
173
+ */
174
+ async listIndexes() {
175
+ try {
176
+ return await this.#db.listCollections({ nameOnly: true });
177
+ } catch (error) {
178
+ throw new MastraError({
179
+ id: createVectorErrorId("ASTRA", "LIST_INDEXES", "DB_ERROR"),
180
+ domain: ErrorDomain.MASTRA_VECTOR,
181
+ category: ErrorCategory.THIRD_PARTY
182
+ }, error);
183
+ }
184
+ }
185
+ /**
186
+ * Retrieves statistics about a vector index.
187
+ *
188
+ * @param {string} indexName - The name of the index to describe
189
+ * @returns A promise that resolves to the index statistics including dimension, count and metric
190
+ */
191
+ async describeIndex({ indexName }) {
192
+ const collection = this.#db.collection(indexName);
193
+ try {
194
+ const optionsPromise = collection.options();
195
+ const countPromise = collection.countDocuments({}, 100);
196
+ const [options, count] = await Promise.all([optionsPromise, countPromise]);
197
+ const metric = Object.keys(metricMap).find((key) => metricMap[key] === options.vector?.metric);
198
+ return {
199
+ dimension: options.vector?.dimension,
200
+ metric,
201
+ count
202
+ };
203
+ } catch (error) {
204
+ if (error instanceof MastraError) throw error;
205
+ throw new MastraError({
206
+ id: createVectorErrorId("ASTRA", "DESCRIBE_INDEX", "DB_ERROR"),
207
+ domain: ErrorDomain.MASTRA_VECTOR,
208
+ category: ErrorCategory.THIRD_PARTY,
209
+ details: { indexName }
210
+ }, error);
211
+ }
212
+ }
213
+ /**
214
+ * Deletes the specified collection.
215
+ *
216
+ * @param {string} indexName - The name of the collection to delete.
217
+ * @returns {Promise<void>} A promise that resolves when the collection is deleted.
218
+ */
219
+ async deleteIndex({ indexName }) {
220
+ const collection = this.#db.collection(indexName);
221
+ try {
222
+ await collection.drop();
223
+ } catch (error) {
224
+ throw new MastraError({
225
+ id: createVectorErrorId("ASTRA", "DELETE_INDEX", "DB_ERROR"),
226
+ domain: ErrorDomain.MASTRA_VECTOR,
227
+ category: ErrorCategory.THIRD_PARTY,
228
+ details: { indexName }
229
+ }, error);
230
+ }
231
+ }
232
+ /**
233
+ * Updates a vector by its ID with the provided vector and/or metadata.
234
+ * @param indexName - The name of the index containing the vector.
235
+ * @param id - The ID of the vector to update.
236
+ * @param update - An object containing the vector and/or metadata to update.
237
+ * @param update.vector - An optional array of numbers representing the new vector.
238
+ * @param update.metadata - An optional record containing the new metadata.
239
+ * @returns A promise that resolves when the update is complete.
240
+ * @throws Will throw an error if no updates are provided or if the update operation fails.
241
+ */
242
+ async updateVector({ indexName, id, update }) {
243
+ if (!id) throw new MastraError({
244
+ id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "NO_ID"),
245
+ text: "id is required for Astra updateVector",
246
+ domain: ErrorDomain.MASTRA_VECTOR,
247
+ category: ErrorCategory.USER,
248
+ details: { indexName }
249
+ });
250
+ if (!update.vector && !update.metadata) throw new MastraError({
251
+ id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "NO_PAYLOAD"),
252
+ text: "No updates provided for vector",
253
+ domain: ErrorDomain.MASTRA_VECTOR,
254
+ category: ErrorCategory.USER,
255
+ details: {
256
+ indexName,
257
+ id
258
+ }
259
+ });
260
+ try {
261
+ const collection = this.#db.collection(indexName);
262
+ const updateDoc = {};
263
+ if (update.vector) updateDoc.$vector = update.vector;
264
+ if (update.metadata) updateDoc.metadata = update.metadata;
265
+ await collection.findOneAndUpdate({ id }, { $set: updateDoc });
266
+ } catch (error) {
267
+ if (error instanceof MastraError) throw error;
268
+ throw new MastraError({
269
+ id: createVectorErrorId("ASTRA", "UPDATE_VECTOR", "FAILED_UNHANDLED"),
270
+ domain: ErrorDomain.MASTRA_VECTOR,
271
+ category: ErrorCategory.THIRD_PARTY,
272
+ details: {
273
+ indexName,
274
+ ...id && { id }
275
+ }
276
+ }, error);
277
+ }
278
+ }
279
+ /**
280
+ * Deletes a vector by its ID.
281
+ * @param indexName - The name of the index containing the vector.
282
+ * @param id - The ID of the vector to delete.
283
+ * @returns A promise that resolves when the deletion is complete.
284
+ * @throws Will throw an error if the deletion operation fails.
285
+ */
286
+ async deleteVector({ indexName, id }) {
287
+ try {
288
+ await this.#db.collection(indexName).deleteOne({ id });
289
+ } catch (error) {
290
+ if (error instanceof MastraError) throw error;
291
+ throw new MastraError({
292
+ id: createVectorErrorId("ASTRA", "DELETE_VECTOR", "FAILED"),
293
+ domain: ErrorDomain.MASTRA_VECTOR,
294
+ category: ErrorCategory.THIRD_PARTY,
295
+ details: {
296
+ indexName,
297
+ ...id && { id }
298
+ }
299
+ }, error);
300
+ }
301
+ }
302
+ async deleteVectors({ indexName, filter, ids }) {
303
+ throw new MastraError({
304
+ id: createVectorErrorId("ASTRA", "DELETE_VECTORS", "NOT_SUPPORTED"),
305
+ text: "deleteVectors is not yet implemented for Astra vector store",
306
+ domain: ErrorDomain.MASTRA_VECTOR,
307
+ category: ErrorCategory.SYSTEM,
308
+ details: {
309
+ indexName,
310
+ ...filter && { filter: JSON.stringify(filter) },
311
+ ...ids && { idsCount: ids.length }
312
+ }
313
+ });
314
+ }
363
315
  };
364
-
365
- // src/vector/prompt.ts
366
- var ASTRA_PROMPT = `When querying Astra, you can ONLY use the operators listed below. Any other operators will be rejected.
316
+ //#endregion
317
+ //#region src/vector/prompt.ts
318
+ /**
319
+ * Vector store specific prompt that details supported operators and examples.
320
+ * This prompt helps users construct valid filters for Astra Vector.
321
+ */
322
+ const ASTRA_PROMPT = `When querying Astra, you can ONLY use the operators listed below. Any other operators will be rejected.
367
323
  Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
368
324
  If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
369
325
 
@@ -450,7 +406,7 @@ Example Complex Query:
450
406
  ]}
451
407
  ]
452
408
  }`;
453
-
409
+ //#endregion
454
410
  export { ASTRA_PROMPT, AstraVector };
455
- //# sourceMappingURL=index.js.map
411
+
456
412
  //# sourceMappingURL=index.js.map