@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.js
CHANGED
|
@@ -1,369 +1,325 @@
|
|
|
1
|
-
import { DataAPIClient, UUID } from
|
|
2
|
-
import {
|
|
3
|
-
import { createVectorErrorId } from
|
|
4
|
-
import { MastraVector } from
|
|
5
|
-
import { BaseFilterTranslator } from
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
58
|
-
|
|
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
|
-
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
|
-
|
|
366
|
-
|
|
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
|
-
|
|
411
|
+
|
|
456
412
|
//# sourceMappingURL=index.js.map
|