@mastra/astra 0.10.2 → 0.10.3-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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +13 -0
- package/dist/index.cjs +146 -44
- package/dist/index.js +144 -42
- package/package.json +6 -6
- package/src/vector/index.ts +148 -46
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/astra@0.10.
|
|
2
|
+
> @mastra/astra@0.10.3-alpha.0 build /home/runner/work/mastra/mastra/stores/astra
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.5.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 5947ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 8679ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
21
|
-
[32mCJS[39m ⚡️ Build success in
|
|
22
|
-
[32mESM[39m [1mdist/index.js [22m[
|
|
23
|
-
[32mESM[39m ⚡️ Build success in
|
|
20
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m14.42 KB[39m
|
|
21
|
+
[32mCJS[39m ⚡️ Build success in 486ms
|
|
22
|
+
[32mESM[39m [1mdist/index.js [22m[32m14.16 KB[39m
|
|
23
|
+
[32mESM[39m ⚡️ Build success in 486ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @mastra/astra
|
|
2
2
|
|
|
3
|
+
## 0.10.3-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0e17048: Throw mastra errors in storage packages
|
|
8
|
+
- Updated dependencies [d1baedb]
|
|
9
|
+
- Updated dependencies [4d21bf2]
|
|
10
|
+
- Updated dependencies [2097952]
|
|
11
|
+
- Updated dependencies [4fb0cc2]
|
|
12
|
+
- Updated dependencies [d2a7a31]
|
|
13
|
+
- Updated dependencies [0e17048]
|
|
14
|
+
- @mastra/core@0.10.7-alpha.1
|
|
15
|
+
|
|
3
16
|
## 0.10.2
|
|
4
17
|
|
|
5
18
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var astraDbTs = require('@datastax/astra-db-ts');
|
|
4
|
+
var error = require('@mastra/core/error');
|
|
4
5
|
var vector = require('@mastra/core/vector');
|
|
5
6
|
var filter = require('@mastra/core/vector/filter');
|
|
6
7
|
|
|
@@ -70,15 +71,32 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
70
71
|
*/
|
|
71
72
|
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
72
73
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
73
|
-
throw new
|
|
74
|
+
throw new error.MastraError({
|
|
75
|
+
id: "ASTRA_VECTOR_CREATE_INDEX_INVALID_DIMENSION",
|
|
76
|
+
text: "Dimension must be a positive integer",
|
|
77
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
78
|
+
category: error.ErrorCategory.USER
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
await this.#db.createCollection(indexName, {
|
|
83
|
+
vector: {
|
|
84
|
+
dimension,
|
|
85
|
+
metric: metricMap[metric]
|
|
86
|
+
},
|
|
87
|
+
checkExists: false
|
|
88
|
+
});
|
|
89
|
+
} catch (error$1) {
|
|
90
|
+
new error.MastraError(
|
|
91
|
+
{
|
|
92
|
+
id: "ASTRA_VECTOR_CREATE_INDEX_DB_ERROR",
|
|
93
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
94
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
95
|
+
details: { indexName }
|
|
96
|
+
},
|
|
97
|
+
error$1
|
|
98
|
+
);
|
|
74
99
|
}
|
|
75
|
-
await this.#db.createCollection(indexName, {
|
|
76
|
-
vector: {
|
|
77
|
-
dimension,
|
|
78
|
-
metric: metricMap[metric]
|
|
79
|
-
},
|
|
80
|
-
checkExists: false
|
|
81
|
-
});
|
|
82
100
|
}
|
|
83
101
|
/**
|
|
84
102
|
* Inserts or updates vectors in the specified collection.
|
|
@@ -97,7 +115,19 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
97
115
|
$vector: vector,
|
|
98
116
|
metadata: metadata?.[i] || {}
|
|
99
117
|
}));
|
|
100
|
-
|
|
118
|
+
try {
|
|
119
|
+
await collection.insertMany(records);
|
|
120
|
+
} catch (error$1) {
|
|
121
|
+
throw new error.MastraError(
|
|
122
|
+
{
|
|
123
|
+
id: "ASTRA_VECTOR_UPSERT_DB_ERROR",
|
|
124
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
125
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
126
|
+
details: { indexName }
|
|
127
|
+
},
|
|
128
|
+
error$1
|
|
129
|
+
);
|
|
130
|
+
}
|
|
101
131
|
return vectorIds;
|
|
102
132
|
}
|
|
103
133
|
transformFilter(filter) {
|
|
@@ -123,29 +153,52 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
123
153
|
}) {
|
|
124
154
|
const collection = this.#db.collection(indexName);
|
|
125
155
|
const translatedFilter = this.transformFilter(filter);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
156
|
+
try {
|
|
157
|
+
const cursor = collection.find(translatedFilter ?? {}, {
|
|
158
|
+
sort: { $vector: queryVector },
|
|
159
|
+
limit: topK,
|
|
160
|
+
includeSimilarity: true,
|
|
161
|
+
projection: {
|
|
162
|
+
$vector: includeVector ? true : false
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const results = await cursor.toArray();
|
|
166
|
+
return results.map((result) => ({
|
|
167
|
+
id: result.id,
|
|
168
|
+
score: result.$similarity,
|
|
169
|
+
metadata: result.metadata,
|
|
170
|
+
...includeVector && { vector: result.$vector }
|
|
171
|
+
}));
|
|
172
|
+
} catch (error$1) {
|
|
173
|
+
throw new error.MastraError(
|
|
174
|
+
{
|
|
175
|
+
id: "ASTRA_VECTOR_QUERY_DB_ERROR",
|
|
176
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
177
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
178
|
+
details: { indexName }
|
|
179
|
+
},
|
|
180
|
+
error$1
|
|
181
|
+
);
|
|
182
|
+
}
|
|
141
183
|
}
|
|
142
184
|
/**
|
|
143
185
|
* Lists all collections in the database.
|
|
144
186
|
*
|
|
145
187
|
* @returns {Promise<string[]>} A promise that resolves to an array of collection names.
|
|
146
188
|
*/
|
|
147
|
-
listIndexes() {
|
|
148
|
-
|
|
189
|
+
async listIndexes() {
|
|
190
|
+
try {
|
|
191
|
+
return await this.#db.listCollections({ nameOnly: true });
|
|
192
|
+
} catch (error$1) {
|
|
193
|
+
throw new error.MastraError(
|
|
194
|
+
{
|
|
195
|
+
id: "ASTRA_VECTOR_LIST_INDEXES_DB_ERROR",
|
|
196
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
197
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
198
|
+
},
|
|
199
|
+
error$1
|
|
200
|
+
);
|
|
201
|
+
}
|
|
149
202
|
}
|
|
150
203
|
/**
|
|
151
204
|
* Retrieves statistics about a vector index.
|
|
@@ -155,16 +208,29 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
155
208
|
*/
|
|
156
209
|
async describeIndex({ indexName }) {
|
|
157
210
|
const collection = this.#db.collection(indexName);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
211
|
+
try {
|
|
212
|
+
const optionsPromise = collection.options();
|
|
213
|
+
const countPromise = collection.countDocuments({}, 100);
|
|
214
|
+
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
215
|
+
const keys = Object.keys(metricMap);
|
|
216
|
+
const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
|
|
217
|
+
return {
|
|
218
|
+
dimension: options.vector?.dimension,
|
|
219
|
+
metric,
|
|
220
|
+
count
|
|
221
|
+
};
|
|
222
|
+
} catch (error$1) {
|
|
223
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
224
|
+
throw new error.MastraError(
|
|
225
|
+
{
|
|
226
|
+
id: "ASTRA_VECTOR_DESCRIBE_INDEX_DB_ERROR",
|
|
227
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
228
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
229
|
+
details: { indexName }
|
|
230
|
+
},
|
|
231
|
+
error$1
|
|
232
|
+
);
|
|
233
|
+
}
|
|
168
234
|
}
|
|
169
235
|
/**
|
|
170
236
|
* Deletes the specified collection.
|
|
@@ -174,7 +240,19 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
174
240
|
*/
|
|
175
241
|
async deleteIndex({ indexName }) {
|
|
176
242
|
const collection = this.#db.collection(indexName);
|
|
177
|
-
|
|
243
|
+
try {
|
|
244
|
+
await collection.drop();
|
|
245
|
+
} catch (error$1) {
|
|
246
|
+
throw new error.MastraError(
|
|
247
|
+
{
|
|
248
|
+
id: "ASTRA_VECTOR_DELETE_INDEX_DB_ERROR",
|
|
249
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
250
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
251
|
+
details: { indexName }
|
|
252
|
+
},
|
|
253
|
+
error$1
|
|
254
|
+
);
|
|
255
|
+
}
|
|
178
256
|
}
|
|
179
257
|
/**
|
|
180
258
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
@@ -187,10 +265,16 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
187
265
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
188
266
|
*/
|
|
189
267
|
async updateVector({ indexName, id, update }) {
|
|
268
|
+
if (!update.vector && !update.metadata) {
|
|
269
|
+
throw new error.MastraError({
|
|
270
|
+
id: "ASTRA_VECTOR_UPDATE_NO_PAYLOAD",
|
|
271
|
+
text: "No updates provided for vector",
|
|
272
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
273
|
+
category: error.ErrorCategory.USER,
|
|
274
|
+
details: { indexName, id }
|
|
275
|
+
});
|
|
276
|
+
}
|
|
190
277
|
try {
|
|
191
|
-
if (!update.vector && !update.metadata) {
|
|
192
|
-
throw new Error("No updates provided");
|
|
193
|
-
}
|
|
194
278
|
const collection = this.#db.collection(indexName);
|
|
195
279
|
const updateDoc = {};
|
|
196
280
|
if (update.vector) {
|
|
@@ -200,8 +284,17 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
200
284
|
updateDoc.metadata = update.metadata;
|
|
201
285
|
}
|
|
202
286
|
await collection.findOneAndUpdate({ id }, { $set: updateDoc });
|
|
203
|
-
} catch (error) {
|
|
204
|
-
|
|
287
|
+
} catch (error$1) {
|
|
288
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
289
|
+
throw new error.MastraError(
|
|
290
|
+
{
|
|
291
|
+
id: "ASTRA_VECTOR_UPDATE_FAILED_UNHANDLED",
|
|
292
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
293
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
294
|
+
details: { indexName, id }
|
|
295
|
+
},
|
|
296
|
+
error$1
|
|
297
|
+
);
|
|
205
298
|
}
|
|
206
299
|
}
|
|
207
300
|
/**
|
|
@@ -215,8 +308,17 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
215
308
|
try {
|
|
216
309
|
const collection = this.#db.collection(indexName);
|
|
217
310
|
await collection.deleteOne({ id });
|
|
218
|
-
} catch (error) {
|
|
219
|
-
|
|
311
|
+
} catch (error$1) {
|
|
312
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
313
|
+
throw new error.MastraError(
|
|
314
|
+
{
|
|
315
|
+
id: "ASTRA_VECTOR_DELETE_FAILED",
|
|
316
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
317
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
318
|
+
details: { indexName, id }
|
|
319
|
+
},
|
|
320
|
+
error$1
|
|
321
|
+
);
|
|
220
322
|
}
|
|
221
323
|
}
|
|
222
324
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DataAPIClient, UUID } from '@datastax/astra-db-ts';
|
|
2
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
3
|
import { MastraVector } from '@mastra/core/vector';
|
|
3
4
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
4
5
|
|
|
@@ -68,15 +69,32 @@ var AstraVector = class extends MastraVector {
|
|
|
68
69
|
*/
|
|
69
70
|
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
70
71
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
71
|
-
throw new
|
|
72
|
+
throw new MastraError({
|
|
73
|
+
id: "ASTRA_VECTOR_CREATE_INDEX_INVALID_DIMENSION",
|
|
74
|
+
text: "Dimension must be a positive integer",
|
|
75
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
76
|
+
category: ErrorCategory.USER
|
|
77
|
+
});
|
|
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
|
+
{
|
|
90
|
+
id: "ASTRA_VECTOR_CREATE_INDEX_DB_ERROR",
|
|
91
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
92
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
93
|
+
details: { indexName }
|
|
94
|
+
},
|
|
95
|
+
error
|
|
96
|
+
);
|
|
72
97
|
}
|
|
73
|
-
await this.#db.createCollection(indexName, {
|
|
74
|
-
vector: {
|
|
75
|
-
dimension,
|
|
76
|
-
metric: metricMap[metric]
|
|
77
|
-
},
|
|
78
|
-
checkExists: false
|
|
79
|
-
});
|
|
80
98
|
}
|
|
81
99
|
/**
|
|
82
100
|
* Inserts or updates vectors in the specified collection.
|
|
@@ -95,7 +113,19 @@ var AstraVector = class extends MastraVector {
|
|
|
95
113
|
$vector: vector,
|
|
96
114
|
metadata: metadata?.[i] || {}
|
|
97
115
|
}));
|
|
98
|
-
|
|
116
|
+
try {
|
|
117
|
+
await collection.insertMany(records);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw new MastraError(
|
|
120
|
+
{
|
|
121
|
+
id: "ASTRA_VECTOR_UPSERT_DB_ERROR",
|
|
122
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
123
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
124
|
+
details: { indexName }
|
|
125
|
+
},
|
|
126
|
+
error
|
|
127
|
+
);
|
|
128
|
+
}
|
|
99
129
|
return vectorIds;
|
|
100
130
|
}
|
|
101
131
|
transformFilter(filter) {
|
|
@@ -121,29 +151,52 @@ var AstraVector = class extends MastraVector {
|
|
|
121
151
|
}) {
|
|
122
152
|
const collection = this.#db.collection(indexName);
|
|
123
153
|
const translatedFilter = this.transformFilter(filter);
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
154
|
+
try {
|
|
155
|
+
const cursor = collection.find(translatedFilter ?? {}, {
|
|
156
|
+
sort: { $vector: queryVector },
|
|
157
|
+
limit: topK,
|
|
158
|
+
includeSimilarity: true,
|
|
159
|
+
projection: {
|
|
160
|
+
$vector: includeVector ? true : false
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
const results = await cursor.toArray();
|
|
164
|
+
return results.map((result) => ({
|
|
165
|
+
id: result.id,
|
|
166
|
+
score: result.$similarity,
|
|
167
|
+
metadata: result.metadata,
|
|
168
|
+
...includeVector && { vector: result.$vector }
|
|
169
|
+
}));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
throw new MastraError(
|
|
172
|
+
{
|
|
173
|
+
id: "ASTRA_VECTOR_QUERY_DB_ERROR",
|
|
174
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
175
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
176
|
+
details: { indexName }
|
|
177
|
+
},
|
|
178
|
+
error
|
|
179
|
+
);
|
|
180
|
+
}
|
|
139
181
|
}
|
|
140
182
|
/**
|
|
141
183
|
* Lists all collections in the database.
|
|
142
184
|
*
|
|
143
185
|
* @returns {Promise<string[]>} A promise that resolves to an array of collection names.
|
|
144
186
|
*/
|
|
145
|
-
listIndexes() {
|
|
146
|
-
|
|
187
|
+
async listIndexes() {
|
|
188
|
+
try {
|
|
189
|
+
return await this.#db.listCollections({ nameOnly: true });
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw new MastraError(
|
|
192
|
+
{
|
|
193
|
+
id: "ASTRA_VECTOR_LIST_INDEXES_DB_ERROR",
|
|
194
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
195
|
+
category: ErrorCategory.THIRD_PARTY
|
|
196
|
+
},
|
|
197
|
+
error
|
|
198
|
+
);
|
|
199
|
+
}
|
|
147
200
|
}
|
|
148
201
|
/**
|
|
149
202
|
* Retrieves statistics about a vector index.
|
|
@@ -153,16 +206,29 @@ var AstraVector = class extends MastraVector {
|
|
|
153
206
|
*/
|
|
154
207
|
async describeIndex({ indexName }) {
|
|
155
208
|
const collection = this.#db.collection(indexName);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
209
|
+
try {
|
|
210
|
+
const optionsPromise = collection.options();
|
|
211
|
+
const countPromise = collection.countDocuments({}, 100);
|
|
212
|
+
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
213
|
+
const keys = Object.keys(metricMap);
|
|
214
|
+
const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
|
|
215
|
+
return {
|
|
216
|
+
dimension: options.vector?.dimension,
|
|
217
|
+
metric,
|
|
218
|
+
count
|
|
219
|
+
};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error instanceof MastraError) throw error;
|
|
222
|
+
throw new MastraError(
|
|
223
|
+
{
|
|
224
|
+
id: "ASTRA_VECTOR_DESCRIBE_INDEX_DB_ERROR",
|
|
225
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
226
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
227
|
+
details: { indexName }
|
|
228
|
+
},
|
|
229
|
+
error
|
|
230
|
+
);
|
|
231
|
+
}
|
|
166
232
|
}
|
|
167
233
|
/**
|
|
168
234
|
* Deletes the specified collection.
|
|
@@ -172,7 +238,19 @@ var AstraVector = class extends MastraVector {
|
|
|
172
238
|
*/
|
|
173
239
|
async deleteIndex({ indexName }) {
|
|
174
240
|
const collection = this.#db.collection(indexName);
|
|
175
|
-
|
|
241
|
+
try {
|
|
242
|
+
await collection.drop();
|
|
243
|
+
} catch (error) {
|
|
244
|
+
throw new MastraError(
|
|
245
|
+
{
|
|
246
|
+
id: "ASTRA_VECTOR_DELETE_INDEX_DB_ERROR",
|
|
247
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
248
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
249
|
+
details: { indexName }
|
|
250
|
+
},
|
|
251
|
+
error
|
|
252
|
+
);
|
|
253
|
+
}
|
|
176
254
|
}
|
|
177
255
|
/**
|
|
178
256
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
@@ -185,10 +263,16 @@ var AstraVector = class extends MastraVector {
|
|
|
185
263
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
186
264
|
*/
|
|
187
265
|
async updateVector({ indexName, id, update }) {
|
|
266
|
+
if (!update.vector && !update.metadata) {
|
|
267
|
+
throw new MastraError({
|
|
268
|
+
id: "ASTRA_VECTOR_UPDATE_NO_PAYLOAD",
|
|
269
|
+
text: "No updates provided for vector",
|
|
270
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
271
|
+
category: ErrorCategory.USER,
|
|
272
|
+
details: { indexName, id }
|
|
273
|
+
});
|
|
274
|
+
}
|
|
188
275
|
try {
|
|
189
|
-
if (!update.vector && !update.metadata) {
|
|
190
|
-
throw new Error("No updates provided");
|
|
191
|
-
}
|
|
192
276
|
const collection = this.#db.collection(indexName);
|
|
193
277
|
const updateDoc = {};
|
|
194
278
|
if (update.vector) {
|
|
@@ -199,7 +283,16 @@ var AstraVector = class extends MastraVector {
|
|
|
199
283
|
}
|
|
200
284
|
await collection.findOneAndUpdate({ id }, { $set: updateDoc });
|
|
201
285
|
} catch (error) {
|
|
202
|
-
|
|
286
|
+
if (error instanceof MastraError) throw error;
|
|
287
|
+
throw new MastraError(
|
|
288
|
+
{
|
|
289
|
+
id: "ASTRA_VECTOR_UPDATE_FAILED_UNHANDLED",
|
|
290
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
291
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
292
|
+
details: { indexName, id }
|
|
293
|
+
},
|
|
294
|
+
error
|
|
295
|
+
);
|
|
203
296
|
}
|
|
204
297
|
}
|
|
205
298
|
/**
|
|
@@ -214,7 +307,16 @@ var AstraVector = class extends MastraVector {
|
|
|
214
307
|
const collection = this.#db.collection(indexName);
|
|
215
308
|
await collection.deleteOne({ id });
|
|
216
309
|
} catch (error) {
|
|
217
|
-
|
|
310
|
+
if (error instanceof MastraError) throw error;
|
|
311
|
+
throw new MastraError(
|
|
312
|
+
{
|
|
313
|
+
id: "ASTRA_VECTOR_DELETE_FAILED",
|
|
314
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
315
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
316
|
+
details: { indexName, id }
|
|
317
|
+
},
|
|
318
|
+
error
|
|
319
|
+
);
|
|
218
320
|
}
|
|
219
321
|
}
|
|
220
322
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/astra",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3-alpha.0",
|
|
4
4
|
"description": "Astra DB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@microsoft/api-extractor": "^7.52.8",
|
|
27
|
-
"@types/node": "^20.
|
|
27
|
+
"@types/node": "^20.19.0",
|
|
28
28
|
"eslint": "^9.28.0",
|
|
29
29
|
"tsup": "^8.5.0",
|
|
30
|
-
"typescript": "^5.8.
|
|
31
|
-
"vitest": "^3.2.
|
|
32
|
-
"@internal/lint": "0.0.
|
|
33
|
-
"@mastra/core": "0.10.
|
|
30
|
+
"typescript": "^5.8.3",
|
|
31
|
+
"vitest": "^3.2.3",
|
|
32
|
+
"@internal/lint": "0.0.13",
|
|
33
|
+
"@mastra/core": "0.10.7-alpha.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@mastra/core": ">=0.10.4-0 <0.11.0"
|
package/src/vector/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Db } from '@datastax/astra-db-ts';
|
|
2
2
|
import { DataAPIClient, UUID } from '@datastax/astra-db-ts';
|
|
3
|
+
import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
3
4
|
import { MastraVector } from '@mastra/core/vector';
|
|
4
5
|
import type {
|
|
5
6
|
QueryResult,
|
|
@@ -48,15 +49,32 @@ export class AstraVector extends MastraVector {
|
|
|
48
49
|
*/
|
|
49
50
|
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
50
51
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
51
|
-
throw new
|
|
52
|
+
throw new MastraError({
|
|
53
|
+
id: 'ASTRA_VECTOR_CREATE_INDEX_INVALID_DIMENSION',
|
|
54
|
+
text: 'Dimension must be a positive integer',
|
|
55
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
56
|
+
category: ErrorCategory.USER,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await this.#db.createCollection(indexName, {
|
|
61
|
+
vector: {
|
|
62
|
+
dimension,
|
|
63
|
+
metric: metricMap[metric],
|
|
64
|
+
},
|
|
65
|
+
checkExists: false,
|
|
66
|
+
});
|
|
67
|
+
} catch (error: any) {
|
|
68
|
+
new MastraError(
|
|
69
|
+
{
|
|
70
|
+
id: 'ASTRA_VECTOR_CREATE_INDEX_DB_ERROR',
|
|
71
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
72
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
73
|
+
details: { indexName },
|
|
74
|
+
},
|
|
75
|
+
error,
|
|
76
|
+
);
|
|
52
77
|
}
|
|
53
|
-
await this.#db.createCollection(indexName, {
|
|
54
|
-
vector: {
|
|
55
|
-
dimension,
|
|
56
|
-
metric: metricMap[metric],
|
|
57
|
-
},
|
|
58
|
-
checkExists: false,
|
|
59
|
-
});
|
|
60
78
|
}
|
|
61
79
|
|
|
62
80
|
/**
|
|
@@ -80,7 +98,19 @@ export class AstraVector extends MastraVector {
|
|
|
80
98
|
metadata: metadata?.[i] || {},
|
|
81
99
|
}));
|
|
82
100
|
|
|
83
|
-
|
|
101
|
+
try {
|
|
102
|
+
await collection.insertMany(records);
|
|
103
|
+
} catch (error: any) {
|
|
104
|
+
throw new MastraError(
|
|
105
|
+
{
|
|
106
|
+
id: 'ASTRA_VECTOR_UPSERT_DB_ERROR',
|
|
107
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
108
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
109
|
+
details: { indexName },
|
|
110
|
+
},
|
|
111
|
+
error,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
84
114
|
return vectorIds;
|
|
85
115
|
}
|
|
86
116
|
|
|
@@ -110,23 +140,35 @@ export class AstraVector extends MastraVector {
|
|
|
110
140
|
|
|
111
141
|
const translatedFilter = this.transformFilter(filter);
|
|
112
142
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
143
|
+
try {
|
|
144
|
+
const cursor = collection.find(translatedFilter ?? {}, {
|
|
145
|
+
sort: { $vector: queryVector },
|
|
146
|
+
limit: topK,
|
|
147
|
+
includeSimilarity: true,
|
|
148
|
+
projection: {
|
|
149
|
+
$vector: includeVector ? true : false,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const results = await cursor.toArray();
|
|
154
|
+
|
|
155
|
+
return results.map(result => ({
|
|
156
|
+
id: result.id,
|
|
157
|
+
score: result.$similarity,
|
|
158
|
+
metadata: result.metadata,
|
|
159
|
+
...(includeVector && { vector: result.$vector }),
|
|
160
|
+
}));
|
|
161
|
+
} catch (error: any) {
|
|
162
|
+
throw new MastraError(
|
|
163
|
+
{
|
|
164
|
+
id: 'ASTRA_VECTOR_QUERY_DB_ERROR',
|
|
165
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
166
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
167
|
+
details: { indexName },
|
|
168
|
+
},
|
|
169
|
+
error,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
130
172
|
}
|
|
131
173
|
|
|
132
174
|
/**
|
|
@@ -134,8 +176,19 @@ export class AstraVector extends MastraVector {
|
|
|
134
176
|
*
|
|
135
177
|
* @returns {Promise<string[]>} A promise that resolves to an array of collection names.
|
|
136
178
|
*/
|
|
137
|
-
listIndexes(): Promise<string[]> {
|
|
138
|
-
|
|
179
|
+
async listIndexes(): Promise<string[]> {
|
|
180
|
+
try {
|
|
181
|
+
return await this.#db.listCollections({ nameOnly: true });
|
|
182
|
+
} catch (error: any) {
|
|
183
|
+
throw new MastraError(
|
|
184
|
+
{
|
|
185
|
+
id: 'ASTRA_VECTOR_LIST_INDEXES_DB_ERROR',
|
|
186
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
187
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
188
|
+
},
|
|
189
|
+
error,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
139
192
|
}
|
|
140
193
|
|
|
141
194
|
/**
|
|
@@ -146,17 +199,30 @@ export class AstraVector extends MastraVector {
|
|
|
146
199
|
*/
|
|
147
200
|
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
148
201
|
const collection = this.#db.collection(indexName);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
202
|
+
try {
|
|
203
|
+
const optionsPromise = collection.options();
|
|
204
|
+
const countPromise = collection.countDocuments({}, 100);
|
|
205
|
+
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
206
|
+
|
|
207
|
+
const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];
|
|
208
|
+
const metric = keys.find(key => metricMap[key] === options.vector?.metric);
|
|
209
|
+
return {
|
|
210
|
+
dimension: options.vector?.dimension!,
|
|
211
|
+
metric,
|
|
212
|
+
count: count,
|
|
213
|
+
};
|
|
214
|
+
} catch (error: any) {
|
|
215
|
+
if (error instanceof MastraError) throw error;
|
|
216
|
+
throw new MastraError(
|
|
217
|
+
{
|
|
218
|
+
id: 'ASTRA_VECTOR_DESCRIBE_INDEX_DB_ERROR',
|
|
219
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
220
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
221
|
+
details: { indexName },
|
|
222
|
+
},
|
|
223
|
+
error,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
160
226
|
}
|
|
161
227
|
|
|
162
228
|
/**
|
|
@@ -167,7 +233,19 @@ export class AstraVector extends MastraVector {
|
|
|
167
233
|
*/
|
|
168
234
|
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
169
235
|
const collection = this.#db.collection(indexName);
|
|
170
|
-
|
|
236
|
+
try {
|
|
237
|
+
await collection.drop();
|
|
238
|
+
} catch (error: any) {
|
|
239
|
+
throw new MastraError(
|
|
240
|
+
{
|
|
241
|
+
id: 'ASTRA_VECTOR_DELETE_INDEX_DB_ERROR',
|
|
242
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
243
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
244
|
+
details: { indexName },
|
|
245
|
+
},
|
|
246
|
+
error,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
171
249
|
}
|
|
172
250
|
|
|
173
251
|
/**
|
|
@@ -181,11 +259,17 @@ export class AstraVector extends MastraVector {
|
|
|
181
259
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
182
260
|
*/
|
|
183
261
|
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
262
|
+
if (!update.vector && !update.metadata) {
|
|
263
|
+
throw new MastraError({
|
|
264
|
+
id: 'ASTRA_VECTOR_UPDATE_NO_PAYLOAD',
|
|
265
|
+
text: 'No updates provided for vector',
|
|
266
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
267
|
+
category: ErrorCategory.USER,
|
|
268
|
+
details: { indexName, id },
|
|
269
|
+
});
|
|
270
|
+
}
|
|
188
271
|
|
|
272
|
+
try {
|
|
189
273
|
const collection = this.#db.collection(indexName);
|
|
190
274
|
const updateDoc: Record<string, any> = {};
|
|
191
275
|
|
|
@@ -199,7 +283,16 @@ export class AstraVector extends MastraVector {
|
|
|
199
283
|
|
|
200
284
|
await collection.findOneAndUpdate({ id }, { $set: updateDoc });
|
|
201
285
|
} catch (error: any) {
|
|
202
|
-
|
|
286
|
+
if (error instanceof MastraError) throw error;
|
|
287
|
+
throw new MastraError(
|
|
288
|
+
{
|
|
289
|
+
id: 'ASTRA_VECTOR_UPDATE_FAILED_UNHANDLED',
|
|
290
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
291
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
292
|
+
details: { indexName, id },
|
|
293
|
+
},
|
|
294
|
+
error,
|
|
295
|
+
);
|
|
203
296
|
}
|
|
204
297
|
}
|
|
205
298
|
|
|
@@ -215,7 +308,16 @@ export class AstraVector extends MastraVector {
|
|
|
215
308
|
const collection = this.#db.collection(indexName);
|
|
216
309
|
await collection.deleteOne({ id });
|
|
217
310
|
} catch (error: any) {
|
|
218
|
-
|
|
311
|
+
if (error instanceof MastraError) throw error;
|
|
312
|
+
throw new MastraError(
|
|
313
|
+
{
|
|
314
|
+
id: 'ASTRA_VECTOR_DELETE_FAILED',
|
|
315
|
+
domain: ErrorDomain.MASTRA_VECTOR,
|
|
316
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
317
|
+
details: { indexName, id },
|
|
318
|
+
},
|
|
319
|
+
error,
|
|
320
|
+
);
|
|
219
321
|
}
|
|
220
322
|
}
|
|
221
323
|
}
|