@mastra/s3vectors 1.1.0 → 1.1.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.
package/dist/index.js CHANGED
@@ -1,708 +1,658 @@
1
- import { S3VectorsClient, CreateIndexCommand, PutVectorsCommand, QueryVectorsCommand, GetVectorsCommand, ListIndexesCommand, DeleteIndexCommand, DeleteVectorsCommand, GetIndexCommand, ListVectorsCommand } from '@aws-sdk/client-s3vectors';
2
- import { v4 } from '@lukeed/uuid';
3
- import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
4
- import { createVectorErrorId } from '@mastra/core/storage';
5
- import { MastraVector } from '@mastra/core/vector';
6
- import { BaseFilterTranslator } from '@mastra/core/vector/filter';
7
-
8
- // src/vector/index.ts
1
+ import { CreateIndexCommand, DeleteIndexCommand, DeleteVectorsCommand, GetIndexCommand, GetVectorsCommand, ListIndexesCommand, ListVectorsCommand, PutVectorsCommand, QueryVectorsCommand, S3VectorsClient } from "@aws-sdk/client-s3vectors";
2
+ import { v4 } from "@lukeed/uuid";
3
+ import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
4
+ import { createVectorErrorId } from "@mastra/core/storage";
5
+ import { MastraVector } from "@mastra/core/vector";
6
+ import { BaseFilterTranslator } from "@mastra/core/vector/filter";
7
+ //#region src/vector/filter.ts
8
+ /**
9
+ * Translates a high-level filter into the S3 Vectors filter shape.
10
+ *
11
+ * @remarks
12
+ * - Canonicalizes **implicit AND** (e.g. `{a:1,b:2}`) into explicit `{$and:[{a:1},{b:2}]}` in any
13
+ * non-field context that lacks `$and/$or`.
14
+ * - Normalizes `Date` values to epoch milliseconds where allowed (numeric comparisons and array elements).
15
+ * - Disallows `Date` at equality positions (including implicit equality).
16
+ * - Validates shapes using the base class after translation.
17
+ */
9
18
  var S3VectorsFilterTranslator = class extends BaseFilterTranslator {
10
- /** @inheritdoc */
11
- getSupportedOperators() {
12
- return {
13
- logical: ["$and", "$or"],
14
- basic: ["$eq", "$ne"],
15
- numeric: ["$gt", "$gte", "$lt", "$lte"],
16
- array: ["$in", "$nin"],
17
- element: ["$exists"]
18
- };
19
- }
20
- /**
21
- * Translates and validates a filter.
22
- * @param filter - Input filter; may be `undefined`, `null`, or `{}` (all treated as empty).
23
- * @returns The translated filter (or the original value if empty).
24
- */
25
- translate(filter) {
26
- if (this.isEmpty(filter)) return filter;
27
- const translated = this.translateNode(filter, false);
28
- this.validateFilter(translated);
29
- return translated;
30
- }
31
- /**
32
- * Recursively translates a node.
33
- * @param node - Current node to translate.
34
- * @param inFieldValue - When `true`, the node is the value of a field (i.e., equality context).
35
- * @remarks
36
- * - In a **field-value** context, only primitives or operator objects are allowed.
37
- * - In a **non-field** context (root / logical branches), operator keys are processed;
38
- * plain keys become field equalities and are validated.
39
- * - Implicit AND is canonicalized in non-field contexts when multiple non-logical keys exist.
40
- */
41
- translateNode(node, inFieldValue = false) {
42
- if (this.isPrimitive(node) || node instanceof Date) {
43
- return inFieldValue ? this.validateAndNormalizePrimitive(node) : node;
44
- }
45
- if (Array.isArray(node)) {
46
- if (inFieldValue) {
47
- throw new Error("Array equality is not supported in S3 Vectors. Use $in / $nin operators.");
48
- }
49
- return node;
50
- }
51
- const entries = Object.entries(node);
52
- if (inFieldValue) {
53
- if (entries.length === 0) {
54
- throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
55
- }
56
- const allOperatorKeys = entries.every(([k]) => this.isOperator(k));
57
- if (!allOperatorKeys) {
58
- throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
59
- }
60
- const opEntries = entries.map(([key, value]) => [key, this.translateOperatorValue(key, value)]);
61
- return Object.fromEntries(opEntries);
62
- }
63
- const translatedEntries = entries.map(([key, value]) => {
64
- if (this.isOperator(key)) {
65
- return [key, this.translateOperatorValue(key, value)];
66
- }
67
- return [key, this.translateNode(value, true)];
68
- });
69
- const obj = Object.fromEntries(translatedEntries);
70
- const keys = Object.keys(obj);
71
- const hasLogical = keys.some((k) => k === "$and" || k === "$or");
72
- if (!hasLogical) {
73
- const nonLogical = keys.filter((k) => k !== "$and" && k !== "$or");
74
- if (nonLogical.length > 1) {
75
- return { $and: nonLogical.map((k) => ({ [k]: obj[k] })) };
76
- }
77
- }
78
- return obj;
79
- }
80
- /**
81
- * Translates a single operator and validates its value.
82
- * @param operator - One of the supported query operators.
83
- * @param value - Operator value to normalize/validate.
84
- */
85
- translateOperatorValue(operator, value) {
86
- if (operator === "$and" || operator === "$or") {
87
- if (!Array.isArray(value) || value.length === 0) {
88
- throw new Error(`Value for logical operator ${operator} must be a non-empty array`);
89
- }
90
- return value.map((item) => this.translateNode(item));
91
- }
92
- if (operator === "$eq" || operator === "$ne") {
93
- if (value instanceof Date) {
94
- throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
95
- }
96
- return this.toPrimitiveForS3(value, operator);
97
- }
98
- if (operator === "$gt" || operator === "$gte" || operator === "$lt" || operator === "$lte") {
99
- const n = this.toNumberForRange(value, operator);
100
- return n;
101
- }
102
- if (operator === "$in" || operator === "$nin") {
103
- if (!Array.isArray(value) || value.length === 0) {
104
- throw new Error(`Value for array operator ${operator} must be a non-empty array`);
105
- }
106
- return value.map((v) => this.toPrimitiveForS3(v, operator));
107
- }
108
- if (operator === "$exists") {
109
- if (typeof value !== "boolean") {
110
- throw new Error(`Value for $exists operator must be a boolean`);
111
- }
112
- return value;
113
- }
114
- throw new Error(`Unsupported operator: ${operator}`);
115
- }
116
- /**
117
- * Normalizes a value to an S3-accepted primitive.
118
- * @param value - String | Number | Boolean | Date.
119
- * @param operatorForMessage - Operator name used in error messages.
120
- * @returns The normalized primitive; `Date` becomes epoch milliseconds.
121
- * @throws If the value is not a supported primitive or is null/undefined.
122
- */
123
- toPrimitiveForS3(value, operatorForMessage) {
124
- if (value === null || value === void 0) {
125
- if (operatorForMessage === "equality") {
126
- throw new Error("S3 Vectors does not support null/undefined for equality");
127
- }
128
- throw new Error(`Value for ${operatorForMessage} must be string, number, or boolean`);
129
- }
130
- if (value instanceof Date) {
131
- return value.getTime();
132
- }
133
- const t = typeof value;
134
- if (t === "string" || t === "boolean") return value;
135
- if (t === "number") return Object.is(value, -0) ? 0 : value;
136
- throw new Error(`Value for ${operatorForMessage} must be string, number, or boolean`);
137
- }
138
- /**
139
- * Ensures a numeric value for range operators; allows `Date` by converting to epoch ms.
140
- * @param value - Candidate value.
141
- * @param operatorForMessage - Operator name used in error messages.
142
- * @throws If the value is not a number (or a Date).
143
- */
144
- toNumberForRange(value, operatorForMessage) {
145
- if (value instanceof Date) return value.getTime();
146
- if (typeof value === "number" && !Number.isNaN(value)) return Object.is(value, -0) ? 0 : value;
147
- throw new Error(`Value for ${operatorForMessage} must be a number`);
148
- }
149
- /**
150
- * Validates and normalizes a primitive used in field equality (implicit `$eq`).
151
- * @param value - Candidate equality value.
152
- * @throws If the value is a `Date` or not a supported primitive.
153
- */
154
- validateAndNormalizePrimitive(value) {
155
- if (value instanceof Date) {
156
- throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
157
- }
158
- return this.toPrimitiveForS3(value, "equality");
159
- }
160
- /**
161
- * Determines whether a filter is considered empty.
162
- * @param filter - Input filter.
163
- */
164
- isEmpty(filter) {
165
- return filter === void 0 || filter === null || typeof filter === "object" && Object.keys(filter).length === 0;
166
- }
19
+ /** @inheritdoc */
20
+ getSupportedOperators() {
21
+ return {
22
+ logical: ["$and", "$or"],
23
+ basic: ["$eq", "$ne"],
24
+ numeric: [
25
+ "$gt",
26
+ "$gte",
27
+ "$lt",
28
+ "$lte"
29
+ ],
30
+ array: ["$in", "$nin"],
31
+ element: ["$exists"]
32
+ };
33
+ }
34
+ /**
35
+ * Translates and validates a filter.
36
+ * @param filter - Input filter; may be `undefined`, `null`, or `{}` (all treated as empty).
37
+ * @returns The translated filter (or the original value if empty).
38
+ */
39
+ translate(filter) {
40
+ if (this.isEmpty(filter)) return filter;
41
+ const translated = this.translateNode(filter, false);
42
+ this.validateFilter(translated);
43
+ return translated;
44
+ }
45
+ /**
46
+ * Recursively translates a node.
47
+ * @param node - Current node to translate.
48
+ * @param inFieldValue - When `true`, the node is the value of a field (i.e., equality context).
49
+ * @remarks
50
+ * - In a **field-value** context, only primitives or operator objects are allowed.
51
+ * - In a **non-field** context (root / logical branches), operator keys are processed;
52
+ * plain keys become field equalities and are validated.
53
+ * - Implicit AND is canonicalized in non-field contexts when multiple non-logical keys exist.
54
+ */
55
+ translateNode(node, inFieldValue = false) {
56
+ if (this.isPrimitive(node) || node instanceof Date) return inFieldValue ? this.validateAndNormalizePrimitive(node) : node;
57
+ if (Array.isArray(node)) {
58
+ if (inFieldValue) throw new Error("Array equality is not supported in S3 Vectors. Use $in / $nin operators.");
59
+ return node;
60
+ }
61
+ const entries = Object.entries(node);
62
+ if (inFieldValue) {
63
+ if (entries.length === 0) throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
64
+ if (!entries.every(([k]) => this.isOperator(k))) throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
65
+ const opEntries = entries.map(([key, value]) => [key, this.translateOperatorValue(key, value)]);
66
+ return Object.fromEntries(opEntries);
67
+ }
68
+ const translatedEntries = entries.map(([key, value]) => {
69
+ if (this.isOperator(key)) return [key, this.translateOperatorValue(key, value)];
70
+ return [key, this.translateNode(value, true)];
71
+ });
72
+ const obj = Object.fromEntries(translatedEntries);
73
+ const keys = Object.keys(obj);
74
+ if (!keys.some((k) => k === "$and" || k === "$or")) {
75
+ const nonLogical = keys.filter((k) => k !== "$and" && k !== "$or");
76
+ if (nonLogical.length > 1) return { $and: nonLogical.map((k) => ({ [k]: obj[k] })) };
77
+ }
78
+ return obj;
79
+ }
80
+ /**
81
+ * Translates a single operator and validates its value.
82
+ * @param operator - One of the supported query operators.
83
+ * @param value - Operator value to normalize/validate.
84
+ */
85
+ translateOperatorValue(operator, value) {
86
+ if (operator === "$and" || operator === "$or") {
87
+ if (!Array.isArray(value) || value.length === 0) throw new Error(`Value for logical operator ${operator} must be a non-empty array`);
88
+ return value.map((item) => this.translateNode(item));
89
+ }
90
+ if (operator === "$eq" || operator === "$ne") {
91
+ if (value instanceof Date) throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
92
+ return this.toPrimitiveForS3(value, operator);
93
+ }
94
+ if (operator === "$gt" || operator === "$gte" || operator === "$lt" || operator === "$lte") return this.toNumberForRange(value, operator);
95
+ if (operator === "$in" || operator === "$nin") {
96
+ if (!Array.isArray(value) || value.length === 0) throw new Error(`Value for array operator ${operator} must be a non-empty array`);
97
+ return value.map((v) => this.toPrimitiveForS3(v, operator));
98
+ }
99
+ if (operator === "$exists") {
100
+ if (typeof value !== "boolean") throw new Error(`Value for $exists operator must be a boolean`);
101
+ return value;
102
+ }
103
+ throw new Error(`Unsupported operator: ${operator}`);
104
+ }
105
+ /**
106
+ * Normalizes a value to an S3-accepted primitive.
107
+ * @param value - String | Number | Boolean | Date.
108
+ * @param operatorForMessage - Operator name used in error messages.
109
+ * @returns The normalized primitive; `Date` becomes epoch milliseconds.
110
+ * @throws If the value is not a supported primitive or is null/undefined.
111
+ */
112
+ toPrimitiveForS3(value, operatorForMessage) {
113
+ if (value === null || value === void 0) {
114
+ if (operatorForMessage === "equality") throw new Error("S3 Vectors does not support null/undefined for equality");
115
+ throw new Error(`Value for ${operatorForMessage} must be string, number, or boolean`);
116
+ }
117
+ if (value instanceof Date) return value.getTime();
118
+ const t = typeof value;
119
+ if (t === "string" || t === "boolean") return value;
120
+ if (t === "number") return Object.is(value, -0) ? 0 : value;
121
+ throw new Error(`Value for ${operatorForMessage} must be string, number, or boolean`);
122
+ }
123
+ /**
124
+ * Ensures a numeric value for range operators; allows `Date` by converting to epoch ms.
125
+ * @param value - Candidate value.
126
+ * @param operatorForMessage - Operator name used in error messages.
127
+ * @throws If the value is not a number (or a Date).
128
+ */
129
+ toNumberForRange(value, operatorForMessage) {
130
+ if (value instanceof Date) return value.getTime();
131
+ if (typeof value === "number" && !Number.isNaN(value)) return Object.is(value, -0) ? 0 : value;
132
+ throw new Error(`Value for ${operatorForMessage} must be a number`);
133
+ }
134
+ /**
135
+ * Validates and normalizes a primitive used in field equality (implicit `$eq`).
136
+ * @param value - Candidate equality value.
137
+ * @throws If the value is a `Date` or not a supported primitive.
138
+ */
139
+ validateAndNormalizePrimitive(value) {
140
+ if (value instanceof Date) throw new Error("Invalid equality value. Only string, number, or boolean are supported by S3 Vectors");
141
+ return this.toPrimitiveForS3(value, "equality");
142
+ }
143
+ /**
144
+ * Determines whether a filter is considered empty.
145
+ * @param filter - Input filter.
146
+ */
147
+ isEmpty(filter) {
148
+ return filter === void 0 || filter === null || typeof filter === "object" && Object.keys(filter).length === 0;
149
+ }
167
150
  };
168
-
169
- // src/vector/index.ts
170
- var S3Vectors = class _S3Vectors extends MastraVector {
171
- client;
172
- vectorBucketName;
173
- nonFilterableMetadataKeys;
174
- filterTranslator = new S3VectorsFilterTranslator();
175
- static METRIC_MAP = {
176
- cosine: "cosine",
177
- euclidean: "euclidean"
178
- };
179
- constructor(opts) {
180
- super({ id: opts.id });
181
- if (!opts?.vectorBucketName) {
182
- throw new MastraError(
183
- {
184
- id: createVectorErrorId("S3VECTORS", "INITIALIZATION", "MISSING_BUCKET_NAME"),
185
- domain: ErrorDomain.STORAGE,
186
- category: ErrorCategory.USER
187
- },
188
- new Error("vectorBucketName is required")
189
- );
190
- }
191
- this.vectorBucketName = opts.vectorBucketName;
192
- this.nonFilterableMetadataKeys = opts.nonFilterableMetadataKeys;
193
- this.client = new S3VectorsClient({ ...opts.clientConfig ?? {} });
194
- }
195
- /**
196
- * No-op to satisfy the base interface.
197
- *
198
- * @remarks The AWS SDK manages HTTP per request; no persistent connection is needed.
199
- */
200
- async connect() {
201
- }
202
- /**
203
- * Closes the underlying AWS SDK HTTP handler to free sockets.
204
- */
205
- async disconnect() {
206
- try {
207
- this.client.destroy();
208
- } catch (error) {
209
- throw new MastraError(
210
- {
211
- id: createVectorErrorId("S3VECTORS", "DISCONNECT", "FAILED"),
212
- domain: ErrorDomain.STORAGE,
213
- category: ErrorCategory.THIRD_PARTY
214
- },
215
- error
216
- );
217
- }
218
- }
219
- /**
220
- * Creates an index or validates an existing one.
221
- *
222
- * @param params.indexName - Logical index name; normalized internally.
223
- * @param params.dimension - Vector dimension (must be a positive integer).
224
- * @param params.metric - Distance metric (`cosine` | `euclidean`). Defaults to `cosine`.
225
- * @throws {MastraError} If arguments are invalid or AWS returns an error.
226
- * @remarks
227
- * On `ConflictException`, we verify the existing index schema via the parent implementation
228
- * and return if it matches.
229
- */
230
- async createIndex({ indexName, dimension, metric = "cosine" }) {
231
- indexName = normalizeIndexName(indexName);
232
- let s3Metric;
233
- try {
234
- assertPositiveInteger(dimension, "dimension");
235
- s3Metric = _S3Vectors.toS3Metric(metric);
236
- } catch (error) {
237
- throw new MastraError(
238
- {
239
- id: createVectorErrorId("S3VECTORS", "CREATE_INDEX", "INVALID_ARGS"),
240
- domain: ErrorDomain.STORAGE,
241
- category: ErrorCategory.USER,
242
- details: { indexName, dimension, metric }
243
- },
244
- error
245
- );
246
- }
247
- try {
248
- const input = {
249
- ...this.bucketParams(),
250
- indexName,
251
- dataType: "float32",
252
- dimension,
253
- distanceMetric: s3Metric
254
- };
255
- if (this.nonFilterableMetadataKeys?.length) {
256
- input.metadataConfiguration = { nonFilterableMetadataKeys: this.nonFilterableMetadataKeys };
257
- }
258
- await this.client.send(new CreateIndexCommand(input));
259
- } catch (error) {
260
- if (error?.name === "ConflictException") {
261
- await this.validateExistingIndex(indexName, dimension, metric);
262
- return;
263
- }
264
- throw new MastraError(
265
- {
266
- id: createVectorErrorId("S3VECTORS", "CREATE_INDEX", "FAILED"),
267
- domain: ErrorDomain.STORAGE,
268
- category: ErrorCategory.THIRD_PARTY,
269
- details: { indexName, dimension, metric }
270
- },
271
- error
272
- );
273
- }
274
- }
275
- /**
276
- * Upserts vectors in bulk.
277
- *
278
- * @param params.indexName - Index to write to.
279
- * @param params.vectors - Array of vectors; each must match the index dimension.
280
- * @param params.metadata - Optional metadata per vector; `Date` values are normalized to epoch ms.
281
- * @param params.ids - Optional explicit IDs; if omitted, UUIDs are generated.
282
- * @returns Array of IDs used for the upsert (explicit or generated).
283
- * @throws {MastraError} If validation fails or AWS returns an error.
284
- */
285
- async upsert({ indexName, vectors, metadata, ids }) {
286
- indexName = normalizeIndexName(indexName);
287
- try {
288
- const { dimension } = await this.getIndexInfo(indexName);
289
- validateVectorDimensions(vectors, dimension);
290
- const generatedIds = ids ?? vectors.map(() => v4());
291
- const putInput = {
292
- ...this.bucketParams(),
293
- indexName,
294
- vectors: vectors.map((vec, i) => ({
295
- key: generatedIds[i],
296
- data: { float32: vec },
297
- metadata: normalizeMetadata(metadata?.[i])
298
- }))
299
- };
300
- await this.client.send(new PutVectorsCommand(putInput));
301
- return generatedIds;
302
- } catch (error) {
303
- throw new MastraError(
304
- {
305
- id: createVectorErrorId("S3VECTORS", "UPSERT", "FAILED"),
306
- domain: ErrorDomain.STORAGE,
307
- category: ErrorCategory.THIRD_PARTY,
308
- details: { indexName }
309
- },
310
- error
311
- );
312
- }
313
- }
314
- /**
315
- * Queries nearest neighbors.
316
- *
317
- * @param params.indexName - Target index.
318
- * @param params.queryVector - Query vector (non-empty float32 array).
319
- * @param params.topK - Number of neighbors to return (positive integer). Defaults to 10.
320
- * @param params.filter - Metadata filter using explicit `$and`/`$or` (translator canonicalizes implicit AND).
321
- * @param params.includeVector - If `true`, fetches missing vector data in a second call.
322
- * @returns Results sorted by `score` descending.
323
- * @throws {MastraError} If validation fails or AWS returns an error.
324
- * @remarks
325
- * `score = 1/(1+distance)` (monotonic transform), so ranking matches the underlying distance.
326
- */
327
- async query({
328
- indexName,
329
- queryVector,
330
- topK = 10,
331
- filter,
332
- includeVector = false
333
- }) {
334
- indexName = normalizeIndexName(indexName);
335
- if (!queryVector) {
336
- throw new MastraError({
337
- id: createVectorErrorId("S3VECTORS", "QUERY", "MISSING_VECTOR"),
338
- text: "queryVector is required for S3 Vectors queries. Metadata-only queries are not supported by this vector store.",
339
- domain: ErrorDomain.STORAGE,
340
- category: ErrorCategory.USER,
341
- details: { indexName }
342
- });
343
- }
344
- try {
345
- if (!Array.isArray(queryVector) || queryVector.length === 0) {
346
- throw new Error("queryVector must be a non-empty float32 array");
347
- }
348
- assertPositiveInteger(topK, "topK");
349
- const translated = this.transformFilter(filter);
350
- const out = await this.client.send(
351
- new QueryVectorsCommand({
352
- ...this.bucketParams(),
353
- indexName,
354
- topK,
355
- queryVector: { float32: queryVector },
356
- filter: translated && Object.keys(translated).length > 0 ? translated : void 0,
357
- returnMetadata: true,
358
- returnDistance: true
359
- })
360
- );
361
- const vectors = (out.vectors ?? []).filter((v) => !!v?.key);
362
- let dataMap;
363
- if (includeVector) {
364
- const keys = vectors.filter((v) => v.key).map((v) => v.key);
365
- if (keys.length > 0) {
366
- const got = await this.client.send(
367
- new GetVectorsCommand({
368
- ...this.bucketParams(),
369
- indexName,
370
- keys,
371
- returnData: true,
372
- returnMetadata: false
373
- })
374
- );
375
- dataMap = {};
376
- for (const g of got.vectors ?? []) {
377
- if (g.key) dataMap[g.key] = g.data?.float32;
378
- }
379
- }
380
- }
381
- return vectors.map((v) => {
382
- const id = v.key;
383
- const score = _S3Vectors.distanceToScore(v.distance ?? 0);
384
- const result = { id, score };
385
- const md = v.metadata;
386
- if (md !== void 0) result.metadata = md;
387
- if (includeVector) {
388
- const vec = dataMap?.[id];
389
- if (vec !== void 0) result.vector = vec;
390
- }
391
- return result;
392
- });
393
- } catch (error) {
394
- throw new MastraError(
395
- {
396
- id: createVectorErrorId("S3VECTORS", "QUERY", "FAILED"),
397
- domain: ErrorDomain.STORAGE,
398
- category: ErrorCategory.THIRD_PARTY,
399
- details: { indexName }
400
- },
401
- error
402
- );
403
- }
404
- }
405
- /**
406
- * Lists indexes within the configured bucket.
407
- *
408
- * @returns Array of index names.
409
- * @throws {MastraError} On AWS errors.
410
- */
411
- async listIndexes() {
412
- try {
413
- const names = [];
414
- let nextToken;
415
- do {
416
- const out = await this.client.send(
417
- new ListIndexesCommand({
418
- ...this.bucketParams(),
419
- nextToken
420
- })
421
- );
422
- for (const idx of out.indexes ?? []) {
423
- if (idx.indexName) names.push(idx.indexName);
424
- }
425
- nextToken = out.nextToken;
426
- } while (nextToken);
427
- return names;
428
- } catch (error) {
429
- throw new MastraError(
430
- {
431
- id: createVectorErrorId("S3VECTORS", "LIST_INDEXES", "FAILED"),
432
- domain: ErrorDomain.STORAGE,
433
- category: ErrorCategory.THIRD_PARTY
434
- },
435
- error
436
- );
437
- }
438
- }
439
- /**
440
- * Returns index attributes.
441
- *
442
- * @param params.indexName - Index name.
443
- * @returns Object containing `dimension`, `metric`, and `count`.
444
- * @throws {MastraError} On AWS errors.
445
- * @remarks
446
- * `count` is computed via `ListVectors` pagination and may be costly (O(n)).
447
- */
448
- async describeIndex({ indexName }) {
449
- indexName = normalizeIndexName(indexName);
450
- try {
451
- const { dimension, metric } = await this.getIndexInfo(indexName);
452
- const count = await this.countVectors(indexName);
453
- return { dimension, metric, count };
454
- } catch (error) {
455
- throw new MastraError(
456
- {
457
- id: createVectorErrorId("S3VECTORS", "DESCRIBE_INDEX", "FAILED"),
458
- domain: ErrorDomain.STORAGE,
459
- category: ErrorCategory.THIRD_PARTY,
460
- details: { indexName }
461
- },
462
- error
463
- );
464
- }
465
- }
466
- /**
467
- * Deletes an index.
468
- *
469
- * @param params.indexName - Index name.
470
- * @throws {MastraError} On AWS errors.
471
- */
472
- async deleteIndex({ indexName }) {
473
- indexName = normalizeIndexName(indexName);
474
- try {
475
- await this.client.send(new DeleteIndexCommand({ ...this.bucketParams(), indexName }));
476
- } catch (error) {
477
- throw new MastraError(
478
- {
479
- id: createVectorErrorId("S3VECTORS", "DELETE_INDEX", "FAILED"),
480
- domain: ErrorDomain.STORAGE,
481
- category: ErrorCategory.THIRD_PARTY,
482
- details: { indexName }
483
- },
484
- error
485
- );
486
- }
487
- }
488
- /**
489
- * Updates (replaces) a vector and/or its metadata by ID.
490
- *
491
- * @param params.indexName - Target index.
492
- * @param params.id - Vector ID.
493
- * @param params.update.vector - New vector; if omitted, the existing vector is reused.
494
- * @param params.update.metadata - New metadata, merged with current metadata.
495
- * @throws {MastraError} If the vector does not exist and `update.vector` is omitted, or on AWS error.
496
- * @remarks
497
- * S3 Vectors `PutVectors` is replace-all; we `Get` the current item, merge, then `Put`.
498
- */
499
- async updateVector({ indexName, id, update }) {
500
- if (!id) {
501
- throw new MastraError({
502
- id: createVectorErrorId("S3VECTORS", "UPDATE_VECTOR", "INVALID_ARGS"),
503
- domain: ErrorDomain.STORAGE,
504
- category: ErrorCategory.USER,
505
- text: "id is required for S3Vectors updateVector",
506
- details: { indexName }
507
- });
508
- }
509
- indexName = normalizeIndexName(indexName);
510
- try {
511
- if (!update.vector && !update.metadata) {
512
- throw new Error("No updates provided");
513
- }
514
- const got = await this.client.send(
515
- new GetVectorsCommand({
516
- ...this.bucketParams(),
517
- indexName,
518
- keys: [id],
519
- returnData: true,
520
- returnMetadata: true
521
- })
522
- );
523
- const current = (got.vectors ?? [])[0];
524
- const newVector = update.vector ?? current?.data?.float32;
525
- if (!newVector) {
526
- throw new Error(`Vector "${id}" not found. Provide update.vector to create it.`);
527
- }
528
- const newMetadata = update.metadata !== void 0 ? normalizeMetadata(update.metadata) : current?.metadata ?? {};
529
- await this.client.send(
530
- new PutVectorsCommand({
531
- ...this.bucketParams(),
532
- indexName,
533
- vectors: [{ key: id, data: { float32: newVector }, metadata: newMetadata }]
534
- })
535
- );
536
- } catch (error) {
537
- throw new MastraError(
538
- {
539
- id: createVectorErrorId("S3VECTORS", "UPDATE_VECTOR", "FAILED"),
540
- domain: ErrorDomain.STORAGE,
541
- category: ErrorCategory.THIRD_PARTY,
542
- details: {
543
- indexName,
544
- ...id && { id }
545
- }
546
- },
547
- error
548
- );
549
- }
550
- }
551
- /**
552
- * Deletes a vector by ID.
553
- *
554
- * @param params.indexName - Target index.
555
- * @param params.id - Vector ID to delete.
556
- * @throws {MastraError} On AWS errors.
557
- */
558
- async deleteVector({ indexName, id }) {
559
- indexName = normalizeIndexName(indexName);
560
- try {
561
- await this.client.send(
562
- new DeleteVectorsCommand({
563
- ...this.bucketParams(),
564
- indexName,
565
- keys: [id]
566
- })
567
- );
568
- } catch (error) {
569
- throw new MastraError(
570
- {
571
- id: createVectorErrorId("S3VECTORS", "DELETE_VECTOR", "FAILED"),
572
- domain: ErrorDomain.STORAGE,
573
- category: ErrorCategory.THIRD_PARTY,
574
- details: {
575
- indexName,
576
- ...id && { id }
577
- }
578
- },
579
- error
580
- );
581
- }
582
- }
583
- async deleteVectors({ indexName, filter, ids }) {
584
- throw new MastraError({
585
- id: createVectorErrorId("S3VECTORS", "DELETE_VECTORS", "NOT_SUPPORTED"),
586
- text: "deleteVectors is not yet implemented for S3Vectors vector store",
587
- domain: ErrorDomain.STORAGE,
588
- category: ErrorCategory.SYSTEM,
589
- details: {
590
- indexName,
591
- ...filter && { filter: JSON.stringify(filter) },
592
- ...ids && { idsCount: ids.length }
593
- }
594
- });
595
- }
596
- // -------- internal helpers --------
597
- /**
598
- * Returns shared bucket parameters for AWS SDK calls.
599
- * @internal
600
- */
601
- bucketParams() {
602
- return { vectorBucketName: this.vectorBucketName };
603
- }
604
- /**
605
- * Retrieves index dimension/metric via `GetIndex`.
606
- * @internal
607
- * @throws {Error} If the index does not exist.
608
- * @returns `{ dimension, metric }`, where `metric` includes `'dotproduct'` to satisfy Mastra types (S3 never returns it).
609
- */
610
- async getIndexInfo(indexName) {
611
- const out = await this.client.send(new GetIndexCommand({ ...this.bucketParams(), indexName }));
612
- const idx = out.index;
613
- if (!idx) throw new Error(`Index "${indexName}" not found`);
614
- const metric = idx.distanceMetric ?? "cosine";
615
- return {
616
- dimension: idx.dimension ?? 0,
617
- metric
618
- };
619
- }
620
- /**
621
- * Pages through `ListVectors` and counts total items.
622
- * @internal
623
- * @remarks O(n). Avoid calling on hot paths.
624
- */
625
- async countVectors(indexName) {
626
- let total = 0;
627
- let nextToken;
628
- do {
629
- const out = await this.client.send(
630
- new ListVectorsCommand({
631
- ...this.bucketParams(),
632
- indexName,
633
- maxResults: 1e3,
634
- nextToken,
635
- returnData: false,
636
- returnMetadata: false
637
- })
638
- );
639
- total += (out.vectors ?? []).length;
640
- nextToken = out.nextToken;
641
- } while (nextToken);
642
- return total;
643
- }
644
- /**
645
- * Translates a high-level filter to the S3 Vectors filter shape.
646
- * @internal
647
- * @remarks Implicit AND is canonicalized by the translator where permitted by spec.
648
- */
649
- transformFilter(filter) {
650
- if (!filter) return void 0;
651
- return this.filterTranslator.translate(filter);
652
- }
653
- /**
654
- * Converts a Mastra metric to an S3 metric.
655
- * @internal
656
- * @throws {Error} If the metric is not supported by S3 Vectors.
657
- */
658
- static toS3Metric(metric) {
659
- const m = _S3Vectors.METRIC_MAP[metric];
660
- if (!m) {
661
- throw new Error(`Invalid metric: "${metric}". S3 Vectors supports only: cosine, euclidean`);
662
- }
663
- return m;
664
- }
665
- /**
666
- * Monotonic transform from distance (smaller is better) to score (larger is better).
667
- * @returns Number in (0, 1], preserving ranking.
668
- */
669
- static distanceToScore(distance) {
670
- return 1 / (1 + distance);
671
- }
151
+ //#endregion
152
+ //#region src/vector/index.ts
153
+ /**
154
+ * Vector store backed by Amazon S3 Vectors.
155
+ *
156
+ * @remarks
157
+ * - Supports `cosine` and `euclidean` distance metrics.
158
+ * - Filters must use explicit logical operators (`$and` / `$or`). The attached translator
159
+ * canonicalizes implicit AND (e.g., `{a:1,b:2}` → `{ $and: [{a:1},{b:2}] }`) where permitted by spec.
160
+ * - Methods wrap AWS errors in `MastraError` with domain/category metadata.
161
+ */
162
+ var S3Vectors = class S3Vectors extends MastraVector {
163
+ client;
164
+ vectorBucketName;
165
+ nonFilterableMetadataKeys;
166
+ filterTranslator = new S3VectorsFilterTranslator();
167
+ static METRIC_MAP = {
168
+ cosine: "cosine",
169
+ euclidean: "euclidean"
170
+ };
171
+ constructor(opts) {
172
+ super({ id: opts.id });
173
+ if (!opts?.vectorBucketName) throw new MastraError({
174
+ id: createVectorErrorId("S3VECTORS", "INITIALIZATION", "MISSING_BUCKET_NAME"),
175
+ domain: ErrorDomain.STORAGE,
176
+ category: ErrorCategory.USER
177
+ }, /* @__PURE__ */ new Error("vectorBucketName is required"));
178
+ this.vectorBucketName = opts.vectorBucketName;
179
+ this.nonFilterableMetadataKeys = opts.nonFilterableMetadataKeys;
180
+ this.client = new S3VectorsClient({ ...opts.clientConfig ?? {} });
181
+ }
182
+ /**
183
+ * No-op to satisfy the base interface.
184
+ *
185
+ * @remarks The AWS SDK manages HTTP per request; no persistent connection is needed.
186
+ */
187
+ async connect() {}
188
+ /**
189
+ * Closes the underlying AWS SDK HTTP handler to free sockets.
190
+ */
191
+ async disconnect() {
192
+ try {
193
+ this.client.destroy();
194
+ } catch (error) {
195
+ throw new MastraError({
196
+ id: createVectorErrorId("S3VECTORS", "DISCONNECT", "FAILED"),
197
+ domain: ErrorDomain.STORAGE,
198
+ category: ErrorCategory.THIRD_PARTY
199
+ }, error);
200
+ }
201
+ }
202
+ /**
203
+ * Creates an index or validates an existing one.
204
+ *
205
+ * @param params.indexName - Logical index name; normalized internally.
206
+ * @param params.dimension - Vector dimension (must be a positive integer).
207
+ * @param params.metric - Distance metric (`cosine` | `euclidean`). Defaults to `cosine`.
208
+ * @throws {MastraError} If arguments are invalid or AWS returns an error.
209
+ * @remarks
210
+ * On `ConflictException`, we verify the existing index schema via the parent implementation
211
+ * and return if it matches.
212
+ */
213
+ async createIndex({ indexName, dimension, metric = "cosine" }) {
214
+ indexName = normalizeIndexName(indexName);
215
+ let s3Metric;
216
+ try {
217
+ assertPositiveInteger(dimension, "dimension");
218
+ s3Metric = S3Vectors.toS3Metric(metric);
219
+ } catch (error) {
220
+ throw new MastraError({
221
+ id: createVectorErrorId("S3VECTORS", "CREATE_INDEX", "INVALID_ARGS"),
222
+ domain: ErrorDomain.STORAGE,
223
+ category: ErrorCategory.USER,
224
+ details: {
225
+ indexName,
226
+ dimension,
227
+ metric
228
+ }
229
+ }, error);
230
+ }
231
+ try {
232
+ const input = {
233
+ ...this.bucketParams(),
234
+ indexName,
235
+ dataType: "float32",
236
+ dimension,
237
+ distanceMetric: s3Metric
238
+ };
239
+ if (this.nonFilterableMetadataKeys?.length) input.metadataConfiguration = { nonFilterableMetadataKeys: this.nonFilterableMetadataKeys };
240
+ await this.client.send(new CreateIndexCommand(input));
241
+ } catch (error) {
242
+ if (error?.name === "ConflictException") {
243
+ await this.validateExistingIndex(indexName, dimension, metric);
244
+ return;
245
+ }
246
+ throw new MastraError({
247
+ id: createVectorErrorId("S3VECTORS", "CREATE_INDEX", "FAILED"),
248
+ domain: ErrorDomain.STORAGE,
249
+ category: ErrorCategory.THIRD_PARTY,
250
+ details: {
251
+ indexName,
252
+ dimension,
253
+ metric
254
+ }
255
+ }, error);
256
+ }
257
+ }
258
+ /**
259
+ * Upserts vectors in bulk.
260
+ *
261
+ * @param params.indexName - Index to write to.
262
+ * @param params.vectors - Array of vectors; each must match the index dimension.
263
+ * @param params.metadata - Optional metadata per vector; `Date` values are normalized to epoch ms.
264
+ * @param params.ids - Optional explicit IDs; if omitted, UUIDs are generated.
265
+ * @returns Array of IDs used for the upsert (explicit or generated).
266
+ * @throws {MastraError} If validation fails or AWS returns an error.
267
+ */
268
+ async upsert({ indexName, vectors, metadata, ids }) {
269
+ indexName = normalizeIndexName(indexName);
270
+ try {
271
+ const { dimension } = await this.getIndexInfo(indexName);
272
+ validateVectorDimensions(vectors, dimension);
273
+ const generatedIds = ids ?? vectors.map(() => v4());
274
+ const putInput = {
275
+ ...this.bucketParams(),
276
+ indexName,
277
+ vectors: vectors.map((vec, i) => ({
278
+ key: generatedIds[i],
279
+ data: { float32: vec },
280
+ metadata: normalizeMetadata(metadata?.[i])
281
+ }))
282
+ };
283
+ await this.client.send(new PutVectorsCommand(putInput));
284
+ return generatedIds;
285
+ } catch (error) {
286
+ throw new MastraError({
287
+ id: createVectorErrorId("S3VECTORS", "UPSERT", "FAILED"),
288
+ domain: ErrorDomain.STORAGE,
289
+ category: ErrorCategory.THIRD_PARTY,
290
+ details: { indexName }
291
+ }, error);
292
+ }
293
+ }
294
+ /**
295
+ * Queries nearest neighbors.
296
+ *
297
+ * @param params.indexName - Target index.
298
+ * @param params.queryVector - Query vector (non-empty float32 array).
299
+ * @param params.topK - Number of neighbors to return (positive integer). Defaults to 10.
300
+ * @param params.filter - Metadata filter using explicit `$and`/`$or` (translator canonicalizes implicit AND).
301
+ * @param params.includeVector - If `true`, fetches missing vector data in a second call.
302
+ * @returns Results sorted by `score` descending.
303
+ * @throws {MastraError} If validation fails or AWS returns an error.
304
+ * @remarks
305
+ * `score = 1/(1+distance)` (monotonic transform), so ranking matches the underlying distance.
306
+ */
307
+ async query({ indexName, queryVector, topK = 10, filter, includeVector = false }) {
308
+ indexName = normalizeIndexName(indexName);
309
+ if (!queryVector) throw new MastraError({
310
+ id: createVectorErrorId("S3VECTORS", "QUERY", "MISSING_VECTOR"),
311
+ text: "queryVector is required for S3 Vectors queries. Metadata-only queries are not supported by this vector store.",
312
+ domain: ErrorDomain.STORAGE,
313
+ category: ErrorCategory.USER,
314
+ details: { indexName }
315
+ });
316
+ try {
317
+ if (!Array.isArray(queryVector) || queryVector.length === 0) throw new Error("queryVector must be a non-empty float32 array");
318
+ assertPositiveInteger(topK, "topK");
319
+ const translated = this.transformFilter(filter);
320
+ const vectors = ((await this.client.send(new QueryVectorsCommand({
321
+ ...this.bucketParams(),
322
+ indexName,
323
+ topK,
324
+ queryVector: { float32: queryVector },
325
+ filter: translated && Object.keys(translated).length > 0 ? translated : void 0,
326
+ returnMetadata: true,
327
+ returnDistance: true
328
+ }))).vectors ?? []).filter((v) => !!v?.key);
329
+ let dataMap;
330
+ if (includeVector) {
331
+ const keys = vectors.filter((v) => v.key).map((v) => v.key);
332
+ if (keys.length > 0) {
333
+ const got = await this.client.send(new GetVectorsCommand({
334
+ ...this.bucketParams(),
335
+ indexName,
336
+ keys,
337
+ returnData: true,
338
+ returnMetadata: false
339
+ }));
340
+ dataMap = {};
341
+ for (const g of got.vectors ?? []) if (g.key) dataMap[g.key] = g.data?.float32;
342
+ }
343
+ }
344
+ return vectors.map((v) => {
345
+ const id = v.key;
346
+ const result = {
347
+ id,
348
+ score: S3Vectors.distanceToScore(v.distance ?? 0)
349
+ };
350
+ const md = v.metadata;
351
+ if (md !== void 0) result.metadata = md;
352
+ if (includeVector) {
353
+ const vec = dataMap?.[id];
354
+ if (vec !== void 0) result.vector = vec;
355
+ }
356
+ return result;
357
+ });
358
+ } catch (error) {
359
+ throw new MastraError({
360
+ id: createVectorErrorId("S3VECTORS", "QUERY", "FAILED"),
361
+ domain: ErrorDomain.STORAGE,
362
+ category: ErrorCategory.THIRD_PARTY,
363
+ details: { indexName }
364
+ }, error);
365
+ }
366
+ }
367
+ /**
368
+ * Lists indexes within the configured bucket.
369
+ *
370
+ * @returns Array of index names.
371
+ * @throws {MastraError} On AWS errors.
372
+ */
373
+ async listIndexes() {
374
+ try {
375
+ const names = [];
376
+ let nextToken;
377
+ do {
378
+ const out = await this.client.send(new ListIndexesCommand({
379
+ ...this.bucketParams(),
380
+ nextToken
381
+ }));
382
+ for (const idx of out.indexes ?? []) if (idx.indexName) names.push(idx.indexName);
383
+ nextToken = out.nextToken;
384
+ } while (nextToken);
385
+ return names;
386
+ } catch (error) {
387
+ throw new MastraError({
388
+ id: createVectorErrorId("S3VECTORS", "LIST_INDEXES", "FAILED"),
389
+ domain: ErrorDomain.STORAGE,
390
+ category: ErrorCategory.THIRD_PARTY
391
+ }, error);
392
+ }
393
+ }
394
+ /**
395
+ * Returns index attributes.
396
+ *
397
+ * @param params.indexName - Index name.
398
+ * @returns Object containing `dimension`, `metric`, and `count`.
399
+ * @throws {MastraError} On AWS errors.
400
+ * @remarks
401
+ * `count` is computed via `ListVectors` pagination and may be costly (O(n)).
402
+ */
403
+ async describeIndex({ indexName }) {
404
+ indexName = normalizeIndexName(indexName);
405
+ try {
406
+ const { dimension, metric } = await this.getIndexInfo(indexName);
407
+ return {
408
+ dimension,
409
+ metric,
410
+ count: await this.countVectors(indexName)
411
+ };
412
+ } catch (error) {
413
+ throw new MastraError({
414
+ id: createVectorErrorId("S3VECTORS", "DESCRIBE_INDEX", "FAILED"),
415
+ domain: ErrorDomain.STORAGE,
416
+ category: ErrorCategory.THIRD_PARTY,
417
+ details: { indexName }
418
+ }, error);
419
+ }
420
+ }
421
+ /**
422
+ * Deletes an index.
423
+ *
424
+ * @param params.indexName - Index name.
425
+ * @throws {MastraError} On AWS errors.
426
+ */
427
+ async deleteIndex({ indexName }) {
428
+ indexName = normalizeIndexName(indexName);
429
+ try {
430
+ await this.client.send(new DeleteIndexCommand({
431
+ ...this.bucketParams(),
432
+ indexName
433
+ }));
434
+ } catch (error) {
435
+ throw new MastraError({
436
+ id: createVectorErrorId("S3VECTORS", "DELETE_INDEX", "FAILED"),
437
+ domain: ErrorDomain.STORAGE,
438
+ category: ErrorCategory.THIRD_PARTY,
439
+ details: { indexName }
440
+ }, error);
441
+ }
442
+ }
443
+ /**
444
+ * Updates (replaces) a vector and/or its metadata by ID.
445
+ *
446
+ * @param params.indexName - Target index.
447
+ * @param params.id - Vector ID.
448
+ * @param params.update.vector - New vector; if omitted, the existing vector is reused.
449
+ * @param params.update.metadata - New metadata, merged with current metadata.
450
+ * @throws {MastraError} If the vector does not exist and `update.vector` is omitted, or on AWS error.
451
+ * @remarks
452
+ * S3 Vectors `PutVectors` is replace-all; we `Get` the current item, merge, then `Put`.
453
+ */
454
+ async updateVector({ indexName, id, update }) {
455
+ if (!id) throw new MastraError({
456
+ id: createVectorErrorId("S3VECTORS", "UPDATE_VECTOR", "INVALID_ARGS"),
457
+ domain: ErrorDomain.STORAGE,
458
+ category: ErrorCategory.USER,
459
+ text: "id is required for S3Vectors updateVector",
460
+ details: { indexName }
461
+ });
462
+ indexName = normalizeIndexName(indexName);
463
+ try {
464
+ if (!update.vector && !update.metadata) throw new Error("No updates provided");
465
+ const current = ((await this.client.send(new GetVectorsCommand({
466
+ ...this.bucketParams(),
467
+ indexName,
468
+ keys: [id],
469
+ returnData: true,
470
+ returnMetadata: true
471
+ }))).vectors ?? [])[0];
472
+ const newVector = update.vector ?? current?.data?.float32;
473
+ if (!newVector) throw new Error(`Vector "${id}" not found. Provide update.vector to create it.`);
474
+ const newMetadata = update.metadata !== void 0 ? normalizeMetadata(update.metadata) : current?.metadata ?? {};
475
+ await this.client.send(new PutVectorsCommand({
476
+ ...this.bucketParams(),
477
+ indexName,
478
+ vectors: [{
479
+ key: id,
480
+ data: { float32: newVector },
481
+ metadata: newMetadata
482
+ }]
483
+ }));
484
+ } catch (error) {
485
+ throw new MastraError({
486
+ id: createVectorErrorId("S3VECTORS", "UPDATE_VECTOR", "FAILED"),
487
+ domain: ErrorDomain.STORAGE,
488
+ category: ErrorCategory.THIRD_PARTY,
489
+ details: {
490
+ indexName,
491
+ ...id && { id }
492
+ }
493
+ }, error);
494
+ }
495
+ }
496
+ /**
497
+ * Deletes a vector by ID.
498
+ *
499
+ * @param params.indexName - Target index.
500
+ * @param params.id - Vector ID to delete.
501
+ * @throws {MastraError} On AWS errors.
502
+ */
503
+ async deleteVector({ indexName, id }) {
504
+ indexName = normalizeIndexName(indexName);
505
+ try {
506
+ await this.client.send(new DeleteVectorsCommand({
507
+ ...this.bucketParams(),
508
+ indexName,
509
+ keys: [id]
510
+ }));
511
+ } catch (error) {
512
+ throw new MastraError({
513
+ id: createVectorErrorId("S3VECTORS", "DELETE_VECTOR", "FAILED"),
514
+ domain: ErrorDomain.STORAGE,
515
+ category: ErrorCategory.THIRD_PARTY,
516
+ details: {
517
+ indexName,
518
+ ...id && { id }
519
+ }
520
+ }, error);
521
+ }
522
+ }
523
+ async deleteVectors({ indexName, filter, ids }) {
524
+ throw new MastraError({
525
+ id: createVectorErrorId("S3VECTORS", "DELETE_VECTORS", "NOT_SUPPORTED"),
526
+ text: "deleteVectors is not yet implemented for S3Vectors vector store",
527
+ domain: ErrorDomain.STORAGE,
528
+ category: ErrorCategory.SYSTEM,
529
+ details: {
530
+ indexName,
531
+ ...filter && { filter: JSON.stringify(filter) },
532
+ ...ids && { idsCount: ids.length }
533
+ }
534
+ });
535
+ }
536
+ /**
537
+ * Returns shared bucket parameters for AWS SDK calls.
538
+ * @internal
539
+ */
540
+ bucketParams() {
541
+ return { vectorBucketName: this.vectorBucketName };
542
+ }
543
+ /**
544
+ * Retrieves index dimension/metric via `GetIndex`.
545
+ * @internal
546
+ * @throws {Error} If the index does not exist.
547
+ * @returns `{ dimension, metric }`, where `metric` includes `'dotproduct'` to satisfy Mastra types (S3 never returns it).
548
+ */
549
+ async getIndexInfo(indexName) {
550
+ const idx = (await this.client.send(new GetIndexCommand({
551
+ ...this.bucketParams(),
552
+ indexName
553
+ }))).index;
554
+ if (!idx) throw new Error(`Index "${indexName}" not found`);
555
+ const metric = idx.distanceMetric ?? "cosine";
556
+ return {
557
+ dimension: idx.dimension ?? 0,
558
+ metric
559
+ };
560
+ }
561
+ /**
562
+ * Pages through `ListVectors` and counts total items.
563
+ * @internal
564
+ * @remarks O(n). Avoid calling on hot paths.
565
+ */
566
+ async countVectors(indexName) {
567
+ let total = 0;
568
+ let nextToken;
569
+ do {
570
+ const out = await this.client.send(new ListVectorsCommand({
571
+ ...this.bucketParams(),
572
+ indexName,
573
+ maxResults: 1e3,
574
+ nextToken,
575
+ returnData: false,
576
+ returnMetadata: false
577
+ }));
578
+ total += (out.vectors ?? []).length;
579
+ nextToken = out.nextToken;
580
+ } while (nextToken);
581
+ return total;
582
+ }
583
+ /**
584
+ * Translates a high-level filter to the S3 Vectors filter shape.
585
+ * @internal
586
+ * @remarks Implicit AND is canonicalized by the translator where permitted by spec.
587
+ */
588
+ transformFilter(filter) {
589
+ if (!filter) return void 0;
590
+ return this.filterTranslator.translate(filter);
591
+ }
592
+ /**
593
+ * Converts a Mastra metric to an S3 metric.
594
+ * @internal
595
+ * @throws {Error} If the metric is not supported by S3 Vectors.
596
+ */
597
+ static toS3Metric(metric) {
598
+ const m = S3Vectors.METRIC_MAP[metric];
599
+ if (!m) throw new Error(`Invalid metric: "${metric}". S3 Vectors supports only: cosine, euclidean`);
600
+ return m;
601
+ }
602
+ /**
603
+ * Monotonic transform from distance (smaller is better) to score (larger is better).
604
+ * @returns Number in (0, 1], preserving ranking.
605
+ */
606
+ static distanceToScore(distance) {
607
+ return 1 / (1 + distance);
608
+ }
672
609
  };
610
+ /**
611
+ * Ensures a value is a positive integer.
612
+ * @throws {Error} If the value is not a positive integer.
613
+ * @internal
614
+ */
673
615
  function assertPositiveInteger(value, name) {
674
- if (!Number.isInteger(value) || value <= 0) {
675
- throw new Error(`${name} must be a positive integer`);
676
- }
616
+ if (!Number.isInteger(value) || value <= 0) throw new Error(`${name} must be a positive integer`);
677
617
  }
618
+ /**
619
+ * Validates that all vectors match the required dimension.
620
+ * @throws {Error} If any vector length differs from `dimension`.
621
+ * @internal
622
+ */
678
623
  function validateVectorDimensions(vectors, dimension) {
679
- if (!Array.isArray(vectors) || vectors.length === 0) {
680
- throw new Error("No vectors provided for validation");
681
- }
682
- for (let i = 0; i < vectors.length; i++) {
683
- const len = vectors[i]?.length;
684
- if (len !== dimension) {
685
- throw new Error(`Vector at index ${i} has invalid dimension ${len}. Expected ${dimension} dimensions.`);
686
- }
687
- }
624
+ if (!Array.isArray(vectors) || vectors.length === 0) throw new Error("No vectors provided for validation");
625
+ for (let i = 0; i < vectors.length; i++) {
626
+ const len = vectors[i]?.length;
627
+ if (len !== dimension) throw new Error(`Vector at index ${i} has invalid dimension ${len}. Expected ${dimension} dimensions.`);
628
+ }
688
629
  }
630
+ /**
631
+ * Normalizes metadata values for S3 Vectors: `Date` → epoch ms.
632
+ * @internal
633
+ */
689
634
  function normalizeMetadata(meta) {
690
- if (!meta) return {};
691
- const out = {};
692
- for (const [k, v] of Object.entries(meta)) {
693
- out[k] = v instanceof Date ? v.getTime() : v;
694
- }
695
- return out;
635
+ if (!meta) return {};
636
+ const out = {};
637
+ for (const [k, v] of Object.entries(meta)) out[k] = v instanceof Date ? v.getTime() : v;
638
+ return out;
696
639
  }
640
+ /**
641
+ * Normalizes an index name to this store's canonical form (underscore → hyphen, lowercase).
642
+ * @internal
643
+ * @throws {TypeError} If the provided name is not a string.
644
+ */
697
645
  function normalizeIndexName(str) {
698
- if (typeof str !== "string") {
699
- throw new TypeError("Index name must be a string");
700
- }
701
- return str.replace(/_/g, "-").toLowerCase();
646
+ if (typeof str !== "string") throw new TypeError("Index name must be a string");
647
+ return str.replace(/_/g, "-").toLowerCase();
702
648
  }
703
-
704
- // src/vector/prompt.ts
705
- var S3VECTORS_PROMPT = `When querying Amazon S3 Vectors, you can ONLY use the operators listed below. Any other operators will be rejected.
649
+ //#endregion
650
+ //#region src/vector/prompt.ts
651
+ /**
652
+ * Vector store specific prompt that details supported operators and examples.
653
+ * This prompt helps users construct valid filters for Amazon S3 Vectors.
654
+ */
655
+ const S3VECTORS_PROMPT = `When querying Amazon S3 Vectors, you can ONLY use the operators listed below. Any other operators will be rejected.
706
656
  Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
707
657
  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.
708
658
 
@@ -779,7 +729,7 @@ Example Complex Query:
779
729
  ] }
780
730
  ]
781
731
  }`;
782
-
732
+ //#endregion
783
733
  export { S3VECTORS_PROMPT, S3Vectors };
784
- //# sourceMappingURL=index.js.map
734
+
785
735
  //# sourceMappingURL=index.js.map