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