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