@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/CHANGELOG.md +18 -0
- package/dist/docs/SKILL.md +3 -6
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/{docs-rag-retrieval.md → reference-rag-retrieval.md} +38 -19
- package/dist/docs/references/{docs-rag-vector-databases.md → reference-rag-vector-databases.md} +70 -5
- package/dist/docs/references/reference-vectors-s3vectors.md +10 -8
- package/dist/index.cjs +648 -699
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +646 -696
- package/dist/index.js.map +1 -1
- package/package.json +17 -16
package/dist/index.js
CHANGED
|
@@ -1,708 +1,658 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { v4 } from
|
|
3
|
-
import {
|
|
4
|
-
import { createVectorErrorId } from
|
|
5
|
-
import { MastraVector } from
|
|
6
|
-
import { BaseFilterTranslator } from
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
* @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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
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
|
-
|
|
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
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
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
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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
|
-
|
|
699
|
-
|
|
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
|
-
|
|
705
|
-
|
|
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
|
-
|
|
734
|
+
|
|
785
735
|
//# sourceMappingURL=index.js.map
|