@mastra/opensearch 0.10.3 → 0.10.4-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +24 -0
- package/dist/_tsup-dts-rollup.d.cts +18 -5
- package/dist/_tsup-dts-rollup.d.ts +18 -5
- package/dist/index.cjs +128 -53
- package/dist/index.js +120 -45
- package/package.json +4 -4
- package/src/vector/filter.test.ts +55 -54
- package/src/vector/filter.ts +23 -5
- package/src/vector/index.test.ts +4 -5
- package/src/vector/index.ts +134 -55
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
1
2
|
import { MastraVector } from '@mastra/core/vector';
|
|
2
3
|
import { Client } from '@opensearch-project/opensearch';
|
|
3
4
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
@@ -9,7 +10,6 @@ var OpenSearchFilterTranslator = class extends BaseFilterTranslator {
|
|
|
9
10
|
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
10
11
|
logical: ["$and", "$or", "$not"],
|
|
11
12
|
array: ["$in", "$nin", "$all"],
|
|
12
|
-
element: ["$exists"],
|
|
13
13
|
regex: ["$regex"],
|
|
14
14
|
custom: []
|
|
15
15
|
};
|
|
@@ -381,7 +381,13 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
381
381
|
*/
|
|
382
382
|
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
383
383
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
384
|
-
throw new
|
|
384
|
+
throw new MastraError({
|
|
385
|
+
id: "STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_INVALID_ARGS",
|
|
386
|
+
domain: ErrorDomain.STORAGE,
|
|
387
|
+
category: ErrorCategory.USER,
|
|
388
|
+
text: "Dimension must be a positive integer",
|
|
389
|
+
details: { indexName, dimension }
|
|
390
|
+
});
|
|
385
391
|
}
|
|
386
392
|
try {
|
|
387
393
|
await this.client.indices.create({
|
|
@@ -412,8 +418,15 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
412
418
|
await this.validateExistingIndex(indexName, dimension, metric);
|
|
413
419
|
return;
|
|
414
420
|
}
|
|
415
|
-
|
|
416
|
-
|
|
421
|
+
throw new MastraError(
|
|
422
|
+
{
|
|
423
|
+
id: "STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_FAILED",
|
|
424
|
+
domain: ErrorDomain.STORAGE,
|
|
425
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
426
|
+
details: { indexName, dimension, metric }
|
|
427
|
+
},
|
|
428
|
+
error
|
|
429
|
+
);
|
|
417
430
|
}
|
|
418
431
|
}
|
|
419
432
|
/**
|
|
@@ -427,8 +440,14 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
427
440
|
const indexes = response.body.map((record) => record.index).filter((index) => index !== void 0);
|
|
428
441
|
return indexes;
|
|
429
442
|
} catch (error) {
|
|
430
|
-
|
|
431
|
-
|
|
443
|
+
throw new MastraError(
|
|
444
|
+
{
|
|
445
|
+
id: "STORAGE_OPENSEARCH_VECTOR_LIST_INDEXES_FAILED",
|
|
446
|
+
domain: ErrorDomain.STORAGE,
|
|
447
|
+
category: ErrorCategory.THIRD_PARTY
|
|
448
|
+
},
|
|
449
|
+
error
|
|
450
|
+
);
|
|
432
451
|
}
|
|
433
452
|
}
|
|
434
453
|
/**
|
|
@@ -459,7 +478,17 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
459
478
|
try {
|
|
460
479
|
await this.client.indices.delete({ index: indexName });
|
|
461
480
|
} catch (error) {
|
|
462
|
-
|
|
481
|
+
const mastraError = new MastraError(
|
|
482
|
+
{
|
|
483
|
+
id: "STORAGE_OPENSEARCH_VECTOR_DELETE_INDEX_FAILED",
|
|
484
|
+
domain: ErrorDomain.STORAGE,
|
|
485
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
486
|
+
details: { indexName }
|
|
487
|
+
},
|
|
488
|
+
error
|
|
489
|
+
);
|
|
490
|
+
this.logger?.error(mastraError.toString());
|
|
491
|
+
this.logger?.trackException(mastraError);
|
|
463
492
|
}
|
|
464
493
|
}
|
|
465
494
|
/**
|
|
@@ -474,31 +503,38 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
474
503
|
async upsert({ indexName, vectors, metadata = [], ids }) {
|
|
475
504
|
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
476
505
|
const operations = [];
|
|
477
|
-
const indexInfo = await this.describeIndex({ indexName });
|
|
478
|
-
this.validateVectorDimensions(vectors, indexInfo.dimension);
|
|
479
|
-
for (let i = 0; i < vectors.length; i++) {
|
|
480
|
-
const operation = {
|
|
481
|
-
index: {
|
|
482
|
-
_index: indexName,
|
|
483
|
-
_id: vectorIds[i]
|
|
484
|
-
}
|
|
485
|
-
};
|
|
486
|
-
const document = {
|
|
487
|
-
id: vectorIds[i],
|
|
488
|
-
embedding: vectors[i],
|
|
489
|
-
metadata: metadata[i] || {}
|
|
490
|
-
};
|
|
491
|
-
operations.push(operation);
|
|
492
|
-
operations.push(document);
|
|
493
|
-
}
|
|
494
506
|
try {
|
|
507
|
+
const indexInfo = await this.describeIndex({ indexName });
|
|
508
|
+
this.validateVectorDimensions(vectors, indexInfo.dimension);
|
|
509
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
510
|
+
const operation = {
|
|
511
|
+
index: {
|
|
512
|
+
_index: indexName,
|
|
513
|
+
_id: vectorIds[i]
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
const document = {
|
|
517
|
+
id: vectorIds[i],
|
|
518
|
+
embedding: vectors[i],
|
|
519
|
+
metadata: metadata[i] || {}
|
|
520
|
+
};
|
|
521
|
+
operations.push(operation);
|
|
522
|
+
operations.push(document);
|
|
523
|
+
}
|
|
495
524
|
if (operations.length > 0) {
|
|
496
525
|
await this.client.bulk({ body: operations, refresh: true });
|
|
497
526
|
}
|
|
498
527
|
return vectorIds;
|
|
499
528
|
} catch (error) {
|
|
500
|
-
|
|
501
|
-
|
|
529
|
+
throw new MastraError(
|
|
530
|
+
{
|
|
531
|
+
id: "STORAGE_OPENSEARCH_VECTOR_UPSERT_FAILED",
|
|
532
|
+
domain: ErrorDomain.STORAGE,
|
|
533
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
534
|
+
details: { indexName, vectorCount: vectors?.length || 0 }
|
|
535
|
+
},
|
|
536
|
+
error
|
|
537
|
+
);
|
|
502
538
|
}
|
|
503
539
|
}
|
|
504
540
|
/**
|
|
@@ -543,8 +579,15 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
543
579
|
});
|
|
544
580
|
return results;
|
|
545
581
|
} catch (error) {
|
|
546
|
-
|
|
547
|
-
|
|
582
|
+
throw new MastraError(
|
|
583
|
+
{
|
|
584
|
+
id: "STORAGE_OPENSEARCH_VECTOR_QUERY_FAILED",
|
|
585
|
+
domain: ErrorDomain.STORAGE,
|
|
586
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
587
|
+
details: { indexName, topK }
|
|
588
|
+
},
|
|
589
|
+
error
|
|
590
|
+
);
|
|
548
591
|
}
|
|
549
592
|
}
|
|
550
593
|
/**
|
|
@@ -562,7 +605,7 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
562
605
|
/**
|
|
563
606
|
* Transforms the filter to the OpenSearch DSL.
|
|
564
607
|
*
|
|
565
|
-
* @param {
|
|
608
|
+
* @param {OpenSearchVectorFilter} filter - The filter to transform.
|
|
566
609
|
* @returns {Record<string, any>} The transformed filter.
|
|
567
610
|
*/
|
|
568
611
|
transformFilter(filter) {
|
|
@@ -580,35 +623,52 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
580
623
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
581
624
|
*/
|
|
582
625
|
async updateVector({ indexName, id, update }) {
|
|
583
|
-
|
|
584
|
-
throw new Error("No updates provided");
|
|
585
|
-
}
|
|
626
|
+
let existingDoc;
|
|
586
627
|
try {
|
|
587
|
-
|
|
628
|
+
if (!update.vector && !update.metadata) {
|
|
629
|
+
throw new Error("No updates provided");
|
|
630
|
+
}
|
|
631
|
+
const { body } = await this.client.get({
|
|
588
632
|
index: indexName,
|
|
589
633
|
id
|
|
590
634
|
}).catch(() => {
|
|
591
635
|
throw new Error(`Document with ID ${id} not found in index ${indexName}`);
|
|
592
636
|
});
|
|
593
|
-
if (!
|
|
637
|
+
if (!body || !body._source) {
|
|
594
638
|
throw new Error(`Document with ID ${id} has no source data in index ${indexName}`);
|
|
595
639
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
640
|
+
existingDoc = body;
|
|
641
|
+
} catch (error) {
|
|
642
|
+
throw new MastraError(
|
|
643
|
+
{
|
|
644
|
+
id: "STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED",
|
|
645
|
+
domain: ErrorDomain.STORAGE,
|
|
646
|
+
category: ErrorCategory.USER,
|
|
647
|
+
details: { indexName, id }
|
|
648
|
+
},
|
|
649
|
+
error
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const source = existingDoc._source;
|
|
653
|
+
const updatedDoc = {
|
|
654
|
+
id: source?.id || id
|
|
655
|
+
};
|
|
656
|
+
try {
|
|
600
657
|
if (update.vector) {
|
|
658
|
+
console.log(`1`);
|
|
601
659
|
const indexInfo = await this.describeIndex({ indexName });
|
|
660
|
+
console.log(`2`);
|
|
602
661
|
this.validateVectorDimensions([update.vector], indexInfo.dimension);
|
|
603
662
|
updatedDoc.embedding = update.vector;
|
|
604
|
-
} else if (source
|
|
663
|
+
} else if (source?.embedding) {
|
|
605
664
|
updatedDoc.embedding = source.embedding;
|
|
606
665
|
}
|
|
607
666
|
if (update.metadata) {
|
|
608
667
|
updatedDoc.metadata = update.metadata;
|
|
609
668
|
} else {
|
|
610
|
-
updatedDoc.metadata = source
|
|
669
|
+
updatedDoc.metadata = source?.metadata || {};
|
|
611
670
|
}
|
|
671
|
+
console.log(`3`);
|
|
612
672
|
await this.client.index({
|
|
613
673
|
index: indexName,
|
|
614
674
|
id,
|
|
@@ -616,8 +676,15 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
616
676
|
refresh: true
|
|
617
677
|
});
|
|
618
678
|
} catch (error) {
|
|
619
|
-
|
|
620
|
-
|
|
679
|
+
throw new MastraError(
|
|
680
|
+
{
|
|
681
|
+
id: "STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED",
|
|
682
|
+
domain: ErrorDomain.STORAGE,
|
|
683
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
684
|
+
details: { indexName, id }
|
|
685
|
+
},
|
|
686
|
+
error
|
|
687
|
+
);
|
|
621
688
|
}
|
|
622
689
|
}
|
|
623
690
|
/**
|
|
@@ -635,10 +702,18 @@ var OpenSearchVector = class extends MastraVector {
|
|
|
635
702
|
refresh: true
|
|
636
703
|
});
|
|
637
704
|
} catch (error) {
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
throw error;
|
|
705
|
+
if (error && typeof error === "object" && "statusCode" in error && error.statusCode === 404) {
|
|
706
|
+
return;
|
|
641
707
|
}
|
|
708
|
+
throw new MastraError(
|
|
709
|
+
{
|
|
710
|
+
id: "STORAGE_OPENSEARCH_VECTOR_DELETE_VECTOR_FAILED",
|
|
711
|
+
domain: ErrorDomain.STORAGE,
|
|
712
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
713
|
+
details: { indexName, id }
|
|
714
|
+
},
|
|
715
|
+
error
|
|
716
|
+
);
|
|
642
717
|
}
|
|
643
718
|
}
|
|
644
719
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/opensearch",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4-alpha.1",
|
|
4
4
|
"description": "OpenSearch vector store provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@microsoft/api-extractor": "^7.52.8",
|
|
26
26
|
"@types/node": "^20.19.0",
|
|
27
|
-
"eslint": "^9.
|
|
27
|
+
"eslint": "^9.29.0",
|
|
28
28
|
"tsup": "^8.5.0",
|
|
29
29
|
"typescript": "^5.8.3",
|
|
30
30
|
"vitest": "^3.2.3",
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
31
|
+
"@internal/lint": "0.0.13",
|
|
32
|
+
"@mastra/core": "0.10.7-alpha.2"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@mastra/core": ">=0.10.4-0 <0.11.0"
|