@mastra/astra 0.10.2 → 0.10.3-alpha.1

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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/astra@0.10.2-alpha.0 build /home/runner/work/mastra/mastra/stores/astra
2
+ > @mastra/astra@0.10.3-alpha.1 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
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.5.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 6874ms
9
+ TSC ⚡️ Build success in 5956ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 9957ms
16
+ DTS ⚡️ Build success in 8264ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 11.63 KB
21
- CJS ⚡️ Build success in 733ms
22
- ESM dist/index.js 11.57 KB
23
- ESM ⚡️ Build success in 734ms
20
+ CJS dist/index.cjs 14.42 KB
21
+ CJS ⚡️ Build success in 539ms
22
+ ESM dist/index.js 14.16 KB
23
+ ESM ⚡️ Build success in 540ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @mastra/astra
2
2
 
3
+ ## 0.10.3-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 144eb0b: [MASTRA-3669] Metadata Filter Types
8
+ - Updated dependencies [15e9d26]
9
+ - Updated dependencies [07d6d88]
10
+ - Updated dependencies [5d74aab]
11
+ - Updated dependencies [144eb0b]
12
+ - @mastra/core@0.10.7-alpha.2
13
+
14
+ ## 0.10.3-alpha.0
15
+
16
+ ### Patch Changes
17
+
18
+ - 0e17048: Throw mastra errors in storage packages
19
+ - Updated dependencies [d1baedb]
20
+ - Updated dependencies [4d21bf2]
21
+ - Updated dependencies [2097952]
22
+ - Updated dependencies [4fb0cc2]
23
+ - Updated dependencies [d2a7a31]
24
+ - Updated dependencies [0e17048]
25
+ - @mastra/core@0.10.7-alpha.1
26
+
3
27
  ## 0.10.2
4
28
 
5
29
  ### Patch Changes
@@ -1,11 +1,14 @@
1
1
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { BlacklistedRootOperators } from '@mastra/core/vector/filter';
2
3
  import type { CreateIndexParams } from '@mastra/core/vector';
3
4
  import type { DeleteIndexParams } from '@mastra/core/vector';
4
5
  import type { DeleteVectorParams } from '@mastra/core/vector';
5
6
  import type { DescribeIndexParams } from '@mastra/core/vector';
6
7
  import type { IndexStats } from '@mastra/core/vector';
8
+ import type { LogicalOperatorValueMap } from '@mastra/core/vector/filter';
7
9
  import { MastraVector } from '@mastra/core/vector';
8
10
  import type { OperatorSupport } from '@mastra/core/vector/filter';
11
+ import type { OperatorValueMap } from '@mastra/core/vector/filter';
9
12
  import type { QueryResult } from '@mastra/core/vector';
10
13
  import type { QueryVectorParams } from '@mastra/core/vector';
11
14
  import type { UpdateVectorParams } from '@mastra/core/vector';
@@ -20,6 +23,8 @@ declare const ASTRA_PROMPT = "When querying Astra, you can ONLY use the operator
20
23
  export { ASTRA_PROMPT }
21
24
  export { ASTRA_PROMPT as ASTRA_PROMPT_alias_1 }
22
25
 
26
+ declare type AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';
27
+
23
28
  declare interface AstraDbOptions {
24
29
  token: string;
25
30
  endpoint: string;
@@ -33,14 +38,22 @@ export { AstraDbOptions as AstraDbOptions_alias_1 }
33
38
  * Maintains MongoDB-compatible syntax while ensuring proper validation
34
39
  * and normalization of values.
35
40
  */
36
- export declare class AstraFilterTranslator extends BaseFilterTranslator {
41
+ export declare class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {
37
42
  protected getSupportedOperators(): OperatorSupport;
38
- translate(filter?: VectorFilter): VectorFilter;
43
+ translate(filter?: AstraVectorFilter): AstraVectorFilter;
39
44
  private translateNode;
40
45
  private translateOperatorValue;
41
46
  }
42
47
 
43
- declare class AstraVector extends MastraVector {
48
+ declare type AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;
49
+
50
+ declare type AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {
51
+ $size: number;
52
+ };
53
+
54
+ declare type AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;
55
+
56
+ declare class AstraVector extends MastraVector<AstraVectorFilter> {
44
57
  #private;
45
58
  constructor({ token, endpoint, keyspace }: AstraDbOptions);
46
59
  /**
@@ -62,7 +75,7 @@ declare class AstraVector extends MastraVector {
62
75
  * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
63
76
  */
64
77
  upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
65
- transformFilter(filter?: VectorFilter): VectorFilter;
78
+ transformFilter(filter?: AstraVectorFilter): AstraVectorFilter;
66
79
  /**
67
80
  * Queries the specified collection using a vector and optional filter.
68
81
  *
@@ -73,7 +86,7 @@ declare class AstraVector extends MastraVector {
73
86
  * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
74
87
  * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
75
88
  */
76
- query({ indexName, queryVector, topK, filter, includeVector, }: QueryVectorParams): Promise<QueryResult[]>;
89
+ query({ indexName, queryVector, topK, filter, includeVector, }: AstraQueryVectorParams): Promise<QueryResult[]>;
77
90
  /**
78
91
  * Lists all collections in the database.
79
92
  *
@@ -117,4 +130,6 @@ declare class AstraVector extends MastraVector {
117
130
  export { AstraVector }
118
131
  export { AstraVector as AstraVector_alias_1 }
119
132
 
133
+ export declare type AstraVectorFilter = VectorFilter<keyof AstraOperatorValueMap, AstraOperatorValueMap, AstraLogicalOperatorValueMap, AstraBlacklisted>;
134
+
120
135
  export { }
@@ -1,11 +1,14 @@
1
1
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { BlacklistedRootOperators } from '@mastra/core/vector/filter';
2
3
  import type { CreateIndexParams } from '@mastra/core/vector';
3
4
  import type { DeleteIndexParams } from '@mastra/core/vector';
4
5
  import type { DeleteVectorParams } from '@mastra/core/vector';
5
6
  import type { DescribeIndexParams } from '@mastra/core/vector';
6
7
  import type { IndexStats } from '@mastra/core/vector';
8
+ import type { LogicalOperatorValueMap } from '@mastra/core/vector/filter';
7
9
  import { MastraVector } from '@mastra/core/vector';
8
10
  import type { OperatorSupport } from '@mastra/core/vector/filter';
11
+ import type { OperatorValueMap } from '@mastra/core/vector/filter';
9
12
  import type { QueryResult } from '@mastra/core/vector';
10
13
  import type { QueryVectorParams } from '@mastra/core/vector';
11
14
  import type { UpdateVectorParams } from '@mastra/core/vector';
@@ -20,6 +23,8 @@ declare const ASTRA_PROMPT = "When querying Astra, you can ONLY use the operator
20
23
  export { ASTRA_PROMPT }
21
24
  export { ASTRA_PROMPT as ASTRA_PROMPT_alias_1 }
22
25
 
26
+ declare type AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';
27
+
23
28
  declare interface AstraDbOptions {
24
29
  token: string;
25
30
  endpoint: string;
@@ -33,14 +38,22 @@ export { AstraDbOptions as AstraDbOptions_alias_1 }
33
38
  * Maintains MongoDB-compatible syntax while ensuring proper validation
34
39
  * and normalization of values.
35
40
  */
36
- export declare class AstraFilterTranslator extends BaseFilterTranslator {
41
+ export declare class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {
37
42
  protected getSupportedOperators(): OperatorSupport;
38
- translate(filter?: VectorFilter): VectorFilter;
43
+ translate(filter?: AstraVectorFilter): AstraVectorFilter;
39
44
  private translateNode;
40
45
  private translateOperatorValue;
41
46
  }
42
47
 
43
- declare class AstraVector extends MastraVector {
48
+ declare type AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;
49
+
50
+ declare type AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {
51
+ $size: number;
52
+ };
53
+
54
+ declare type AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;
55
+
56
+ declare class AstraVector extends MastraVector<AstraVectorFilter> {
44
57
  #private;
45
58
  constructor({ token, endpoint, keyspace }: AstraDbOptions);
46
59
  /**
@@ -62,7 +75,7 @@ declare class AstraVector extends MastraVector {
62
75
  * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
63
76
  */
64
77
  upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
65
- transformFilter(filter?: VectorFilter): VectorFilter;
78
+ transformFilter(filter?: AstraVectorFilter): AstraVectorFilter;
66
79
  /**
67
80
  * Queries the specified collection using a vector and optional filter.
68
81
  *
@@ -73,7 +86,7 @@ declare class AstraVector extends MastraVector {
73
86
  * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
74
87
  * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
75
88
  */
76
- query({ indexName, queryVector, topK, filter, includeVector, }: QueryVectorParams): Promise<QueryResult[]>;
89
+ query({ indexName, queryVector, topK, filter, includeVector, }: AstraQueryVectorParams): Promise<QueryResult[]>;
77
90
  /**
78
91
  * Lists all collections in the database.
79
92
  *
@@ -117,4 +130,6 @@ declare class AstraVector extends MastraVector {
117
130
  export { AstraVector }
118
131
  export { AstraVector as AstraVector_alias_1 }
119
132
 
133
+ export declare type AstraVectorFilter = VectorFilter<keyof AstraOperatorValueMap, AstraOperatorValueMap, AstraLogicalOperatorValueMap, AstraBlacklisted>;
134
+
120
135
  export { }
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 Error("Dimension must be a positive integer");
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
- await collection.insertMany(records);
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
- const cursor = collection.find(translatedFilter ?? {}, {
127
- sort: { $vector: queryVector },
128
- limit: topK,
129
- includeSimilarity: true,
130
- projection: {
131
- $vector: includeVector ? true : false
132
- }
133
- });
134
- const results = await cursor.toArray();
135
- return results.map((result) => ({
136
- id: result.id,
137
- score: result.$similarity,
138
- metadata: result.metadata,
139
- ...includeVector && { vector: result.$vector }
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
- return this.#db.listCollections({ nameOnly: true });
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
- const optionsPromise = collection.options();
159
- const countPromise = collection.countDocuments({}, 100);
160
- const [options, count] = await Promise.all([optionsPromise, countPromise]);
161
- const keys = Object.keys(metricMap);
162
- const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
163
- return {
164
- dimension: options.vector?.dimension,
165
- metric,
166
- count
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
- await collection.drop();
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
- throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
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
- throw new Error(`Failed to delete vector by id: ${id} for index name: ${indexName}: ${error.message}`);
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
  };