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