@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/LICENSE.md +6 -4
- package/dist/docs/SKILL.md +4 -7
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/{docs-rag-retrieval.md → reference-rag-retrieval.md} +168 -31
- package/dist/docs/references/{docs-rag-vector-databases.md → reference-rag-vector-databases.md} +104 -37
- package/dist/docs/references/reference-vectors-astra.md +5 -1
- package/dist/index.cjs +322 -367
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +319 -363
- package/dist/index.js.map +1 -1
- package/dist/vector/filter.d.ts.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +17 -17
- package/CHANGELOG.md +0 -2397
package/dist/index.cjs
CHANGED
|
@@ -1,371 +1,326 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
-
|
|
368
|
-
|
|
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
|
-
|
|
413
|
+
|
|
459
414
|
//# sourceMappingURL=index.cjs.map
|