@mastra/astra 0.2.15 → 0.3.0-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 +35 -0
- package/dist/_tsup-dts-rollup.d.cts +17 -37
- package/dist/_tsup-dts-rollup.d.ts +17 -37
- package/dist/index.cjs +19 -49
- package/dist/index.js +19 -49
- package/package.json +7 -4
- package/src/vector/index.test.ts +18 -115
- package/src/vector/index.ts +23 -67
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/astra@0.
|
|
2
|
+
> @mastra/astra@0.3.0-alpha.1 build /home/runner/work/mastra/mastra/stores/astra
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.4.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 6430ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/astra/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 8701ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
21
|
-
[32mCJS[39m ⚡️ Build success in
|
|
22
|
-
[32mESM[39m [1mdist/index.js [22m[
|
|
23
|
-
[32mESM[39m ⚡️ Build success in
|
|
20
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m11.63 KB[39m
|
|
21
|
+
[32mCJS[39m ⚡️ Build success in 657ms
|
|
22
|
+
[32mESM[39m [1mdist/index.js [22m[32m11.57 KB[39m
|
|
23
|
+
[32mESM[39m ⚡️ Build success in 669ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @mastra/astra
|
|
2
2
|
|
|
3
|
+
## 0.3.0-alpha.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 83da932: Move @mastra/core to peerdeps
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- a7292b0: BREAKING(@mastra/core, all vector stores): Vector store breaking changes (remove deprecated functions and positional arguments)
|
|
12
|
+
- Updated dependencies [b3a3d63]
|
|
13
|
+
- Updated dependencies [344f453]
|
|
14
|
+
- Updated dependencies [0a3ae6d]
|
|
15
|
+
- Updated dependencies [95911be]
|
|
16
|
+
- Updated dependencies [5eb5a99]
|
|
17
|
+
- Updated dependencies [7e632c5]
|
|
18
|
+
- Updated dependencies [1e9fbfa]
|
|
19
|
+
- Updated dependencies [b2ae5aa]
|
|
20
|
+
- Updated dependencies [a7292b0]
|
|
21
|
+
- Updated dependencies [0dcb9f0]
|
|
22
|
+
- @mastra/core@0.10.0-alpha.1
|
|
23
|
+
|
|
24
|
+
## 0.2.16-alpha.0
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- d0ee3c6: Change all public functions and constructors in vector stores to use named args and prepare to phase out positional args
|
|
29
|
+
- Updated dependencies [f53a6ac]
|
|
30
|
+
- Updated dependencies [eabdcd9]
|
|
31
|
+
- Updated dependencies [90be034]
|
|
32
|
+
- Updated dependencies [99f050a]
|
|
33
|
+
- Updated dependencies [d0ee3c6]
|
|
34
|
+
- Updated dependencies [23f258c]
|
|
35
|
+
- Updated dependencies [2672a05]
|
|
36
|
+
- @mastra/core@0.9.5-alpha.0
|
|
37
|
+
|
|
3
38
|
## 0.2.15
|
|
4
39
|
|
|
5
40
|
### Patch Changes
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
2
|
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
4
|
+
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
5
|
+
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
3
6
|
import type { IndexStats } from '@mastra/core/vector';
|
|
4
7
|
import { MastraVector } from '@mastra/core/vector';
|
|
5
8
|
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
6
|
-
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
7
9
|
import type { QueryResult } from '@mastra/core/vector';
|
|
8
10
|
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
+
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
9
12
|
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
10
13
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
11
14
|
|
|
@@ -48,7 +51,7 @@ declare class AstraVector extends MastraVector {
|
|
|
48
51
|
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
49
52
|
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
50
53
|
*/
|
|
51
|
-
createIndex(
|
|
54
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
52
55
|
/**
|
|
53
56
|
* Inserts or updates vectors in the specified collection.
|
|
54
57
|
*
|
|
@@ -58,7 +61,7 @@ declare class AstraVector extends MastraVector {
|
|
|
58
61
|
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
59
62
|
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
60
63
|
*/
|
|
61
|
-
upsert(
|
|
64
|
+
upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
|
|
62
65
|
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
63
66
|
/**
|
|
64
67
|
* Queries the specified collection using a vector and optional filter.
|
|
@@ -70,37 +73,27 @@ declare class AstraVector extends MastraVector {
|
|
|
70
73
|
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
71
74
|
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
72
75
|
*/
|
|
73
|
-
query(
|
|
76
|
+
query({ indexName, queryVector, topK, filter, includeVector, }: QueryVectorParams): Promise<QueryResult[]>;
|
|
74
77
|
/**
|
|
75
78
|
* Lists all collections in the database.
|
|
76
79
|
*
|
|
77
80
|
* @returns {Promise<string[]>} A promise that resolves to an array of collection names.
|
|
78
81
|
*/
|
|
79
82
|
listIndexes(): Promise<string[]>;
|
|
80
|
-
describeIndex(indexName: string): Promise<IndexStats>;
|
|
81
83
|
/**
|
|
82
|
-
*
|
|
84
|
+
* Retrieves statistics about a vector index.
|
|
83
85
|
*
|
|
84
|
-
* @param {string} indexName - The name of the
|
|
85
|
-
* @returns
|
|
86
|
+
* @param {string} indexName - The name of the index to describe
|
|
87
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
86
88
|
*/
|
|
87
|
-
|
|
89
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
88
90
|
/**
|
|
89
|
-
*
|
|
91
|
+
* Deletes the specified collection.
|
|
90
92
|
*
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
93
|
-
* @param id - The ID of the vector to update.
|
|
94
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
95
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
96
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
97
|
-
* @returns A promise that resolves when the update is complete.
|
|
98
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
93
|
+
* @param {string} indexName - The name of the collection to delete.
|
|
94
|
+
* @returns {Promise<void>} A promise that resolves when the collection is deleted.
|
|
99
95
|
*/
|
|
100
|
-
|
|
101
|
-
vector?: number[];
|
|
102
|
-
metadata?: Record<string, any>;
|
|
103
|
-
}): Promise<void>;
|
|
96
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
104
97
|
/**
|
|
105
98
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
106
99
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -111,20 +104,7 @@ declare class AstraVector extends MastraVector {
|
|
|
111
104
|
* @returns A promise that resolves when the update is complete.
|
|
112
105
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
113
106
|
*/
|
|
114
|
-
updateVector(indexName
|
|
115
|
-
vector?: number[];
|
|
116
|
-
metadata?: Record<string, any>;
|
|
117
|
-
}): Promise<void>;
|
|
118
|
-
/**
|
|
119
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
120
|
-
*
|
|
121
|
-
* Deletes a vector by its ID.
|
|
122
|
-
* @param indexName - The name of the index containing the vector.
|
|
123
|
-
* @param id - The ID of the vector to delete.
|
|
124
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
125
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
126
|
-
*/
|
|
127
|
-
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
107
|
+
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
128
108
|
/**
|
|
129
109
|
* Deletes a vector by its ID.
|
|
130
110
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -132,7 +112,7 @@ declare class AstraVector extends MastraVector {
|
|
|
132
112
|
* @returns A promise that resolves when the deletion is complete.
|
|
133
113
|
* @throws Will throw an error if the deletion operation fails.
|
|
134
114
|
*/
|
|
135
|
-
deleteVector(indexName
|
|
115
|
+
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
136
116
|
}
|
|
137
117
|
export { AstraVector }
|
|
138
118
|
export { AstraVector as AstraVector_alias_1 }
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
2
|
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
4
|
+
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
5
|
+
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
3
6
|
import type { IndexStats } from '@mastra/core/vector';
|
|
4
7
|
import { MastraVector } from '@mastra/core/vector';
|
|
5
8
|
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
6
|
-
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
7
9
|
import type { QueryResult } from '@mastra/core/vector';
|
|
8
10
|
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
+
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
9
12
|
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
10
13
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
11
14
|
|
|
@@ -48,7 +51,7 @@ declare class AstraVector extends MastraVector {
|
|
|
48
51
|
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
49
52
|
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
50
53
|
*/
|
|
51
|
-
createIndex(
|
|
54
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
52
55
|
/**
|
|
53
56
|
* Inserts or updates vectors in the specified collection.
|
|
54
57
|
*
|
|
@@ -58,7 +61,7 @@ declare class AstraVector extends MastraVector {
|
|
|
58
61
|
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
59
62
|
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
60
63
|
*/
|
|
61
|
-
upsert(
|
|
64
|
+
upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
|
|
62
65
|
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
63
66
|
/**
|
|
64
67
|
* Queries the specified collection using a vector and optional filter.
|
|
@@ -70,37 +73,27 @@ declare class AstraVector extends MastraVector {
|
|
|
70
73
|
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
71
74
|
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
72
75
|
*/
|
|
73
|
-
query(
|
|
76
|
+
query({ indexName, queryVector, topK, filter, includeVector, }: QueryVectorParams): Promise<QueryResult[]>;
|
|
74
77
|
/**
|
|
75
78
|
* Lists all collections in the database.
|
|
76
79
|
*
|
|
77
80
|
* @returns {Promise<string[]>} A promise that resolves to an array of collection names.
|
|
78
81
|
*/
|
|
79
82
|
listIndexes(): Promise<string[]>;
|
|
80
|
-
describeIndex(indexName: string): Promise<IndexStats>;
|
|
81
83
|
/**
|
|
82
|
-
*
|
|
84
|
+
* Retrieves statistics about a vector index.
|
|
83
85
|
*
|
|
84
|
-
* @param {string} indexName - The name of the
|
|
85
|
-
* @returns
|
|
86
|
+
* @param {string} indexName - The name of the index to describe
|
|
87
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
86
88
|
*/
|
|
87
|
-
|
|
89
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
88
90
|
/**
|
|
89
|
-
*
|
|
91
|
+
* Deletes the specified collection.
|
|
90
92
|
*
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
93
|
-
* @param id - The ID of the vector to update.
|
|
94
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
95
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
96
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
97
|
-
* @returns A promise that resolves when the update is complete.
|
|
98
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
93
|
+
* @param {string} indexName - The name of the collection to delete.
|
|
94
|
+
* @returns {Promise<void>} A promise that resolves when the collection is deleted.
|
|
99
95
|
*/
|
|
100
|
-
|
|
101
|
-
vector?: number[];
|
|
102
|
-
metadata?: Record<string, any>;
|
|
103
|
-
}): Promise<void>;
|
|
96
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
104
97
|
/**
|
|
105
98
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
106
99
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -111,20 +104,7 @@ declare class AstraVector extends MastraVector {
|
|
|
111
104
|
* @returns A promise that resolves when the update is complete.
|
|
112
105
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
113
106
|
*/
|
|
114
|
-
updateVector(indexName
|
|
115
|
-
vector?: number[];
|
|
116
|
-
metadata?: Record<string, any>;
|
|
117
|
-
}): Promise<void>;
|
|
118
|
-
/**
|
|
119
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
120
|
-
*
|
|
121
|
-
* Deletes a vector by its ID.
|
|
122
|
-
* @param indexName - The name of the index containing the vector.
|
|
123
|
-
* @param id - The ID of the vector to delete.
|
|
124
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
125
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
126
|
-
*/
|
|
127
|
-
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
107
|
+
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
128
108
|
/**
|
|
129
109
|
* Deletes a vector by its ID.
|
|
130
110
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -132,7 +112,7 @@ declare class AstraVector extends MastraVector {
|
|
|
132
112
|
* @returns A promise that resolves when the deletion is complete.
|
|
133
113
|
* @throws Will throw an error if the deletion operation fails.
|
|
134
114
|
*/
|
|
135
|
-
deleteVector(indexName
|
|
115
|
+
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
136
116
|
}
|
|
137
117
|
export { AstraVector }
|
|
138
118
|
export { AstraVector as AstraVector_alias_1 }
|
package/dist/index.cjs
CHANGED
|
@@ -68,9 +68,7 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
68
68
|
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
69
69
|
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
70
70
|
*/
|
|
71
|
-
async createIndex(
|
|
72
|
-
const params = this.normalizeArgs("createIndex", args);
|
|
73
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
71
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
74
72
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
75
73
|
throw new Error("Dimension must be a positive integer");
|
|
76
74
|
}
|
|
@@ -91,9 +89,7 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
91
89
|
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
92
90
|
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
93
91
|
*/
|
|
94
|
-
async upsert(
|
|
95
|
-
const params = this.normalizeArgs("upsert", args);
|
|
96
|
-
const { indexName, vectors, metadata, ids } = params;
|
|
92
|
+
async upsert({ indexName, vectors, metadata, ids }) {
|
|
97
93
|
const collection = this.#db.collection(indexName);
|
|
98
94
|
const vectorIds = ids || vectors.map(() => astraDbTs.UUID.v7().toString());
|
|
99
95
|
const records = vectors.map((vector, i) => ({
|
|
@@ -118,9 +114,13 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
118
114
|
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
119
115
|
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
120
116
|
*/
|
|
121
|
-
async query(
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
async query({
|
|
118
|
+
indexName,
|
|
119
|
+
queryVector,
|
|
120
|
+
topK = 10,
|
|
121
|
+
filter,
|
|
122
|
+
includeVector = false
|
|
123
|
+
}) {
|
|
124
124
|
const collection = this.#db.collection(indexName);
|
|
125
125
|
const translatedFilter = this.transformFilter(filter);
|
|
126
126
|
const cursor = collection.find(translatedFilter ?? {}, {
|
|
@@ -147,12 +147,17 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
147
147
|
listIndexes() {
|
|
148
148
|
return this.#db.listCollections({ nameOnly: true });
|
|
149
149
|
}
|
|
150
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Retrieves statistics about a vector index.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} indexName - The name of the index to describe
|
|
154
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
155
|
+
*/
|
|
156
|
+
async describeIndex({ indexName }) {
|
|
151
157
|
const collection = this.#db.collection(indexName);
|
|
152
158
|
const optionsPromise = collection.options();
|
|
153
159
|
const countPromise = collection.countDocuments({}, 100);
|
|
154
160
|
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
155
|
-
console.log(options, count);
|
|
156
161
|
const keys = Object.keys(metricMap);
|
|
157
162
|
const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
|
|
158
163
|
return {
|
|
@@ -167,28 +172,10 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
167
172
|
* @param {string} indexName - The name of the collection to delete.
|
|
168
173
|
* @returns {Promise<void>} A promise that resolves when the collection is deleted.
|
|
169
174
|
*/
|
|
170
|
-
async deleteIndex(indexName) {
|
|
175
|
+
async deleteIndex({ indexName }) {
|
|
171
176
|
const collection = this.#db.collection(indexName);
|
|
172
177
|
await collection.drop();
|
|
173
178
|
}
|
|
174
|
-
/**
|
|
175
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
176
|
-
*
|
|
177
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
178
|
-
* @param indexName - The name of the index containing the vector.
|
|
179
|
-
* @param id - The ID of the vector to update.
|
|
180
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
181
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
182
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
183
|
-
* @returns A promise that resolves when the update is complete.
|
|
184
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
185
|
-
*/
|
|
186
|
-
async updateIndexById(indexName, id, update) {
|
|
187
|
-
this.logger.warn(
|
|
188
|
-
`Deprecation Warning: updateIndexById() is deprecated. Please use updateVector() instead. updateIndexById() will be removed on May 20th, 2025.`
|
|
189
|
-
);
|
|
190
|
-
await this.updateVector(indexName, id, update);
|
|
191
|
-
}
|
|
192
179
|
/**
|
|
193
180
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
194
181
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -199,7 +186,7 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
199
186
|
* @returns A promise that resolves when the update is complete.
|
|
200
187
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
201
188
|
*/
|
|
202
|
-
async updateVector(indexName, id, update) {
|
|
189
|
+
async updateVector({ indexName, id, update }) {
|
|
203
190
|
try {
|
|
204
191
|
if (!update.vector && !update.metadata) {
|
|
205
192
|
throw new Error("No updates provided");
|
|
@@ -217,23 +204,6 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
217
204
|
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
218
205
|
}
|
|
219
206
|
}
|
|
220
|
-
/**
|
|
221
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
222
|
-
*
|
|
223
|
-
* Deletes a vector by its ID.
|
|
224
|
-
* @param indexName - The name of the index containing the vector.
|
|
225
|
-
* @param id - The ID of the vector to delete.
|
|
226
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
227
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
228
|
-
*/
|
|
229
|
-
async deleteIndexById(indexName, id) {
|
|
230
|
-
this.logger.warn(
|
|
231
|
-
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
232
|
-
Please use deleteVector() instead.
|
|
233
|
-
deleteIndexById() will be removed on May 20th, 2025.`
|
|
234
|
-
);
|
|
235
|
-
await this.deleteVector(indexName, id);
|
|
236
|
-
}
|
|
237
207
|
/**
|
|
238
208
|
* Deletes a vector by its ID.
|
|
239
209
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -241,7 +211,7 @@ var AstraVector = class extends vector.MastraVector {
|
|
|
241
211
|
* @returns A promise that resolves when the deletion is complete.
|
|
242
212
|
* @throws Will throw an error if the deletion operation fails.
|
|
243
213
|
*/
|
|
244
|
-
async deleteVector(indexName, id) {
|
|
214
|
+
async deleteVector({ indexName, id }) {
|
|
245
215
|
try {
|
|
246
216
|
const collection = this.#db.collection(indexName);
|
|
247
217
|
await collection.deleteOne({ id });
|
package/dist/index.js
CHANGED
|
@@ -66,9 +66,7 @@ var AstraVector = class extends MastraVector {
|
|
|
66
66
|
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
67
67
|
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
68
68
|
*/
|
|
69
|
-
async createIndex(
|
|
70
|
-
const params = this.normalizeArgs("createIndex", args);
|
|
71
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
69
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
72
70
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
73
71
|
throw new Error("Dimension must be a positive integer");
|
|
74
72
|
}
|
|
@@ -89,9 +87,7 @@ var AstraVector = class extends MastraVector {
|
|
|
89
87
|
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
90
88
|
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
91
89
|
*/
|
|
92
|
-
async upsert(
|
|
93
|
-
const params = this.normalizeArgs("upsert", args);
|
|
94
|
-
const { indexName, vectors, metadata, ids } = params;
|
|
90
|
+
async upsert({ indexName, vectors, metadata, ids }) {
|
|
95
91
|
const collection = this.#db.collection(indexName);
|
|
96
92
|
const vectorIds = ids || vectors.map(() => UUID.v7().toString());
|
|
97
93
|
const records = vectors.map((vector, i) => ({
|
|
@@ -116,9 +112,13 @@ var AstraVector = class extends MastraVector {
|
|
|
116
112
|
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
117
113
|
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
118
114
|
*/
|
|
119
|
-
async query(
|
|
120
|
-
|
|
121
|
-
|
|
115
|
+
async query({
|
|
116
|
+
indexName,
|
|
117
|
+
queryVector,
|
|
118
|
+
topK = 10,
|
|
119
|
+
filter,
|
|
120
|
+
includeVector = false
|
|
121
|
+
}) {
|
|
122
122
|
const collection = this.#db.collection(indexName);
|
|
123
123
|
const translatedFilter = this.transformFilter(filter);
|
|
124
124
|
const cursor = collection.find(translatedFilter ?? {}, {
|
|
@@ -145,12 +145,17 @@ var AstraVector = class extends MastraVector {
|
|
|
145
145
|
listIndexes() {
|
|
146
146
|
return this.#db.listCollections({ nameOnly: true });
|
|
147
147
|
}
|
|
148
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Retrieves statistics about a vector index.
|
|
150
|
+
*
|
|
151
|
+
* @param {string} indexName - The name of the index to describe
|
|
152
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
153
|
+
*/
|
|
154
|
+
async describeIndex({ indexName }) {
|
|
149
155
|
const collection = this.#db.collection(indexName);
|
|
150
156
|
const optionsPromise = collection.options();
|
|
151
157
|
const countPromise = collection.countDocuments({}, 100);
|
|
152
158
|
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
153
|
-
console.log(options, count);
|
|
154
159
|
const keys = Object.keys(metricMap);
|
|
155
160
|
const metric = keys.find((key) => metricMap[key] === options.vector?.metric);
|
|
156
161
|
return {
|
|
@@ -165,28 +170,10 @@ var AstraVector = class extends MastraVector {
|
|
|
165
170
|
* @param {string} indexName - The name of the collection to delete.
|
|
166
171
|
* @returns {Promise<void>} A promise that resolves when the collection is deleted.
|
|
167
172
|
*/
|
|
168
|
-
async deleteIndex(indexName) {
|
|
173
|
+
async deleteIndex({ indexName }) {
|
|
169
174
|
const collection = this.#db.collection(indexName);
|
|
170
175
|
await collection.drop();
|
|
171
176
|
}
|
|
172
|
-
/**
|
|
173
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
174
|
-
*
|
|
175
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
176
|
-
* @param indexName - The name of the index containing the vector.
|
|
177
|
-
* @param id - The ID of the vector to update.
|
|
178
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
179
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
180
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
181
|
-
* @returns A promise that resolves when the update is complete.
|
|
182
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
183
|
-
*/
|
|
184
|
-
async updateIndexById(indexName, id, update) {
|
|
185
|
-
this.logger.warn(
|
|
186
|
-
`Deprecation Warning: updateIndexById() is deprecated. Please use updateVector() instead. updateIndexById() will be removed on May 20th, 2025.`
|
|
187
|
-
);
|
|
188
|
-
await this.updateVector(indexName, id, update);
|
|
189
|
-
}
|
|
190
177
|
/**
|
|
191
178
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
192
179
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -197,7 +184,7 @@ var AstraVector = class extends MastraVector {
|
|
|
197
184
|
* @returns A promise that resolves when the update is complete.
|
|
198
185
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
199
186
|
*/
|
|
200
|
-
async updateVector(indexName, id, update) {
|
|
187
|
+
async updateVector({ indexName, id, update }) {
|
|
201
188
|
try {
|
|
202
189
|
if (!update.vector && !update.metadata) {
|
|
203
190
|
throw new Error("No updates provided");
|
|
@@ -215,23 +202,6 @@ var AstraVector = class extends MastraVector {
|
|
|
215
202
|
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
216
203
|
}
|
|
217
204
|
}
|
|
218
|
-
/**
|
|
219
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
220
|
-
*
|
|
221
|
-
* Deletes a vector by its ID.
|
|
222
|
-
* @param indexName - The name of the index containing the vector.
|
|
223
|
-
* @param id - The ID of the vector to delete.
|
|
224
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
225
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
226
|
-
*/
|
|
227
|
-
async deleteIndexById(indexName, id) {
|
|
228
|
-
this.logger.warn(
|
|
229
|
-
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
230
|
-
Please use deleteVector() instead.
|
|
231
|
-
deleteIndexById() will be removed on May 20th, 2025.`
|
|
232
|
-
);
|
|
233
|
-
await this.deleteVector(indexName, id);
|
|
234
|
-
}
|
|
235
205
|
/**
|
|
236
206
|
* Deletes a vector by its ID.
|
|
237
207
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -239,7 +209,7 @@ var AstraVector = class extends MastraVector {
|
|
|
239
209
|
* @returns A promise that resolves when the deletion is complete.
|
|
240
210
|
* @throws Will throw an error if the deletion operation fails.
|
|
241
211
|
*/
|
|
242
|
-
async deleteVector(indexName, id) {
|
|
212
|
+
async deleteVector({ indexName, id }) {
|
|
243
213
|
try {
|
|
244
214
|
const collection = this.#db.collection(indexName);
|
|
245
215
|
await collection.deleteOne({ id });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/astra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-alpha.1",
|
|
4
4
|
"description": "Astra DB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,7 @@
|
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@datastax/astra-db-ts": "^1.5.0"
|
|
24
|
-
"@mastra/core": "^0.9.4"
|
|
23
|
+
"@datastax/astra-db-ts": "^1.5.0"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
26
|
"@microsoft/api-extractor": "^7.52.5",
|
|
@@ -30,7 +29,11 @@
|
|
|
30
29
|
"tsup": "^8.4.0",
|
|
31
30
|
"typescript": "^5.8.2",
|
|
32
31
|
"vitest": "^3.1.2",
|
|
33
|
-
"@internal/lint": "0.0.5"
|
|
32
|
+
"@internal/lint": "0.0.5",
|
|
33
|
+
"@mastra/core": "0.10.0-alpha.1"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@mastra/core": "^0.9.4"
|
|
34
37
|
},
|
|
35
38
|
"scripts": {
|
|
36
39
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
package/src/vector/index.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { vi, describe, it, expect, beforeAll, afterAll, test
|
|
1
|
+
import { vi, describe, it, expect, beforeAll, afterAll, test } from 'vitest';
|
|
2
2
|
|
|
3
3
|
import { AstraVector } from './';
|
|
4
4
|
|
|
@@ -44,7 +44,7 @@ async function createIndexAndWait(
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
async function deleteIndexAndWait(vectorDB: AstraVector, indexName: string) {
|
|
47
|
-
await vectorDB.deleteIndex(indexName);
|
|
47
|
+
await vectorDB.deleteIndex({ indexName });
|
|
48
48
|
const deleted = await waitForCondition(
|
|
49
49
|
async () => {
|
|
50
50
|
const newCollections = await vectorDB.listIndexes();
|
|
@@ -80,7 +80,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
80
80
|
});
|
|
81
81
|
try {
|
|
82
82
|
const collections = await vectorDB.listIndexes();
|
|
83
|
-
await Promise.all(collections.map(c => vectorDB.deleteIndex(c)));
|
|
83
|
+
await Promise.all(collections.map(c => vectorDB.deleteIndex({ indexName: c })));
|
|
84
84
|
const deleted = await waitForCondition(
|
|
85
85
|
async () => {
|
|
86
86
|
const remainingCollections = await vectorDB.listIndexes();
|
|
@@ -104,12 +104,12 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
104
104
|
afterAll(async () => {
|
|
105
105
|
// Cleanup: delete test collection
|
|
106
106
|
try {
|
|
107
|
-
await vectorDB.deleteIndex(testIndexName);
|
|
107
|
+
await vectorDB.deleteIndex({ indexName: testIndexName });
|
|
108
108
|
} catch (error) {
|
|
109
109
|
console.error('Failed to delete test collection:', error);
|
|
110
110
|
}
|
|
111
111
|
try {
|
|
112
|
-
await vectorDB.deleteIndex(testIndexName2);
|
|
112
|
+
await vectorDB.deleteIndex({ indexName: testIndexName2 });
|
|
113
113
|
} catch (error) {
|
|
114
114
|
console.error('Failed to delete test collection:', error);
|
|
115
115
|
}
|
|
@@ -121,7 +121,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
121
121
|
expect(indexes).toContain(testIndexName);
|
|
122
122
|
|
|
123
123
|
// 2. Get collection stats
|
|
124
|
-
const initialStats = await vectorDB.describeIndex(testIndexName);
|
|
124
|
+
const initialStats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
125
125
|
expect(initialStats).toEqual({
|
|
126
126
|
dimension: 4,
|
|
127
127
|
metric: 'cosine',
|
|
@@ -144,7 +144,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
144
144
|
// Wait for document count to update (with timeout)
|
|
145
145
|
const countUpdated = await waitForCondition(
|
|
146
146
|
async () => {
|
|
147
|
-
const stats = await vectorDB.describeIndex(testIndexName);
|
|
147
|
+
const stats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
148
148
|
console.log('Current count:', stats.count);
|
|
149
149
|
return stats.count === 4;
|
|
150
150
|
},
|
|
@@ -176,7 +176,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
176
176
|
expect(filteredResults?.[0]?.metadata).toEqual({ label: 'vector2' });
|
|
177
177
|
|
|
178
178
|
// Get final stats
|
|
179
|
-
const finalStats = await vectorDB.describeIndex(testIndexName);
|
|
179
|
+
const finalStats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
180
180
|
console.log('Final stats:', finalStats);
|
|
181
181
|
|
|
182
182
|
// More lenient assertion for document count
|
|
@@ -1093,103 +1093,6 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1093
1093
|
});
|
|
1094
1094
|
});
|
|
1095
1095
|
});
|
|
1096
|
-
describe('Deprecation Warnings', () => {
|
|
1097
|
-
const indexName = 'testdeprecationwarnings';
|
|
1098
|
-
|
|
1099
|
-
const indexName2 = 'testdeprecationwarnings2';
|
|
1100
|
-
|
|
1101
|
-
let warnSpy;
|
|
1102
|
-
|
|
1103
|
-
beforeAll(async () => {
|
|
1104
|
-
await createIndexAndWait(vectorDB, indexName, 3, 'cosine');
|
|
1105
|
-
});
|
|
1106
|
-
|
|
1107
|
-
afterAll(async () => {
|
|
1108
|
-
await deleteIndexAndWait(vectorDB, indexName);
|
|
1109
|
-
await deleteIndexAndWait(vectorDB, indexName2);
|
|
1110
|
-
});
|
|
1111
|
-
|
|
1112
|
-
beforeEach(async () => {
|
|
1113
|
-
warnSpy = vi.spyOn(vectorDB['logger'], 'warn');
|
|
1114
|
-
});
|
|
1115
|
-
|
|
1116
|
-
afterEach(async () => {
|
|
1117
|
-
warnSpy.mockRestore();
|
|
1118
|
-
await deleteIndexAndWait(vectorDB, indexName2);
|
|
1119
|
-
});
|
|
1120
|
-
|
|
1121
|
-
it('should show deprecation warning when using individual args for createIndex', async () => {
|
|
1122
|
-
await vectorDB.createIndex(indexName2, 3, 'cosine');
|
|
1123
|
-
|
|
1124
|
-
expect(warnSpy).toHaveBeenCalledWith(
|
|
1125
|
-
expect.stringContaining('Deprecation Warning: Passing individual arguments to createIndex() is deprecated'),
|
|
1126
|
-
);
|
|
1127
|
-
});
|
|
1128
|
-
|
|
1129
|
-
it('should show deprecation warning when using individual args for upsert', async () => {
|
|
1130
|
-
await vectorDB.upsert(indexName, [[1, 2, 3]], [{ test: 'data' }]);
|
|
1131
|
-
|
|
1132
|
-
expect(warnSpy).toHaveBeenCalledWith(
|
|
1133
|
-
expect.stringContaining('Deprecation Warning: Passing individual arguments to upsert() is deprecated'),
|
|
1134
|
-
);
|
|
1135
|
-
});
|
|
1136
|
-
|
|
1137
|
-
it('should show deprecation warning when using individual args for query', async () => {
|
|
1138
|
-
await vectorDB.query(indexName, [1, 2, 3], 5);
|
|
1139
|
-
|
|
1140
|
-
expect(warnSpy).toHaveBeenCalledWith(
|
|
1141
|
-
expect.stringContaining('Deprecation Warning: Passing individual arguments to query() is deprecated'),
|
|
1142
|
-
);
|
|
1143
|
-
});
|
|
1144
|
-
|
|
1145
|
-
it('should not show deprecation warning when using object param for query', async () => {
|
|
1146
|
-
await vectorDB.query({
|
|
1147
|
-
indexName,
|
|
1148
|
-
queryVector: [1, 2, 3],
|
|
1149
|
-
topK: 5,
|
|
1150
|
-
});
|
|
1151
|
-
|
|
1152
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
1153
|
-
});
|
|
1154
|
-
|
|
1155
|
-
it('should not show deprecation warning when using object param for createIndex', async () => {
|
|
1156
|
-
await vectorDB.createIndex({
|
|
1157
|
-
indexName: indexName2,
|
|
1158
|
-
dimension: 3,
|
|
1159
|
-
metric: 'cosine',
|
|
1160
|
-
});
|
|
1161
|
-
|
|
1162
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
1163
|
-
});
|
|
1164
|
-
|
|
1165
|
-
it('should not show deprecation warning when using object param for upsert', async () => {
|
|
1166
|
-
await vectorDB.upsert({
|
|
1167
|
-
indexName,
|
|
1168
|
-
vectors: [[1, 2, 3]],
|
|
1169
|
-
metadata: [{ test: 'data' }],
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
1173
|
-
});
|
|
1174
|
-
|
|
1175
|
-
it('should maintain backward compatibility with individual args', async () => {
|
|
1176
|
-
// Query
|
|
1177
|
-
const queryResults = await vectorDB.query(indexName, [1, 2, 3], 5);
|
|
1178
|
-
expect(Array.isArray(queryResults)).toBe(true);
|
|
1179
|
-
|
|
1180
|
-
// CreateIndex
|
|
1181
|
-
await expect(vectorDB.createIndex(indexName2, 3, 'cosine')).resolves.not.toThrow();
|
|
1182
|
-
|
|
1183
|
-
// Upsert
|
|
1184
|
-
const upsertResults = await vectorDB.upsert({
|
|
1185
|
-
indexName,
|
|
1186
|
-
vectors: [[1, 2, 3]],
|
|
1187
|
-
metadata: [{ test: 'data' }],
|
|
1188
|
-
});
|
|
1189
|
-
expect(Array.isArray(upsertResults)).toBe(true);
|
|
1190
|
-
expect(upsertResults).toHaveLength(1);
|
|
1191
|
-
});
|
|
1192
|
-
});
|
|
1193
1096
|
|
|
1194
1097
|
describe('Basic vector operations', () => {
|
|
1195
1098
|
const indexName = 'testbasicvectoroperations';
|
|
@@ -1224,12 +1127,12 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1224
1127
|
metadata: newMetaData,
|
|
1225
1128
|
};
|
|
1226
1129
|
|
|
1227
|
-
await vectorDB.updateVector(indexName, idToBeUpdated, update);
|
|
1130
|
+
await vectorDB.updateVector({ indexName, id: idToBeUpdated, update });
|
|
1228
1131
|
|
|
1229
1132
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
1230
1133
|
|
|
1231
1134
|
const results = await vectorDB.query({
|
|
1232
|
-
indexName
|
|
1135
|
+
indexName,
|
|
1233
1136
|
queryVector: newVector,
|
|
1234
1137
|
topK: 2,
|
|
1235
1138
|
includeVector: true,
|
|
@@ -1256,11 +1159,11 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1256
1159
|
metadata: newMetaData,
|
|
1257
1160
|
};
|
|
1258
1161
|
|
|
1259
|
-
await vectorDB.updateVector(indexName, idToBeUpdated, update);
|
|
1162
|
+
await vectorDB.updateVector({ indexName, id: idToBeUpdated, update });
|
|
1260
1163
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
1261
1164
|
|
|
1262
1165
|
const results = await vectorDB.query({
|
|
1263
|
-
indexName
|
|
1166
|
+
indexName,
|
|
1264
1167
|
queryVector: testVectors[0],
|
|
1265
1168
|
topK: 2,
|
|
1266
1169
|
includeVector: true,
|
|
@@ -1285,11 +1188,11 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1285
1188
|
vector: newVector,
|
|
1286
1189
|
};
|
|
1287
1190
|
|
|
1288
|
-
await vectorDB.updateVector(indexName, idToBeUpdated, update);
|
|
1191
|
+
await vectorDB.updateVector({ indexName, id: idToBeUpdated, update });
|
|
1289
1192
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
1290
1193
|
|
|
1291
1194
|
const results = await vectorDB.query({
|
|
1292
|
-
indexName
|
|
1195
|
+
indexName,
|
|
1293
1196
|
queryVector: newVector,
|
|
1294
1197
|
topK: 2,
|
|
1295
1198
|
includeVector: true,
|
|
@@ -1303,7 +1206,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1303
1206
|
});
|
|
1304
1207
|
|
|
1305
1208
|
it('should throw exception when no updates are given', async () => {
|
|
1306
|
-
await expect(vectorDB.updateVector(indexName, 'id', {})).rejects.toThrow('No updates provided');
|
|
1209
|
+
await expect(vectorDB.updateVector({ indexName, id: 'id', update: {} })).rejects.toThrow('No updates provided');
|
|
1307
1210
|
});
|
|
1308
1211
|
|
|
1309
1212
|
it('should delete the vector by id', async () => {
|
|
@@ -1311,7 +1214,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1311
1214
|
expect(ids).toHaveLength(4);
|
|
1312
1215
|
|
|
1313
1216
|
const idToBeDeleted = ids[0];
|
|
1314
|
-
await vectorDB.deleteVector(indexName, idToBeDeleted);
|
|
1217
|
+
await vectorDB.deleteVector({ indexName, id: idToBeDeleted });
|
|
1315
1218
|
|
|
1316
1219
|
const results = await vectorDB.query({
|
|
1317
1220
|
indexName: indexName,
|
|
@@ -1331,7 +1234,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1331
1234
|
});
|
|
1332
1235
|
|
|
1333
1236
|
afterAll(async () => {
|
|
1334
|
-
await vectorDB.deleteIndex(testIndexName);
|
|
1237
|
+
await vectorDB.deleteIndex({ indexName: testIndexName });
|
|
1335
1238
|
});
|
|
1336
1239
|
|
|
1337
1240
|
it('should handle non-existent index queries', async () => {
|
|
@@ -1376,7 +1279,7 @@ describe.skip('AstraVector Integration Tests', () => {
|
|
|
1376
1279
|
);
|
|
1377
1280
|
} finally {
|
|
1378
1281
|
// Cleanup
|
|
1379
|
-
await vectorDB.deleteIndex(duplicateIndexName);
|
|
1282
|
+
await vectorDB.deleteIndex({ indexName: duplicateIndexName });
|
|
1380
1283
|
}
|
|
1381
1284
|
});
|
|
1382
1285
|
});
|
package/src/vector/index.ts
CHANGED
|
@@ -7,7 +7,10 @@ import type {
|
|
|
7
7
|
CreateIndexParams,
|
|
8
8
|
UpsertVectorParams,
|
|
9
9
|
QueryVectorParams,
|
|
10
|
-
|
|
10
|
+
DescribeIndexParams,
|
|
11
|
+
DeleteIndexParams,
|
|
12
|
+
DeleteVectorParams,
|
|
13
|
+
UpdateVectorParams,
|
|
11
14
|
} from '@mastra/core/vector';
|
|
12
15
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
13
16
|
|
|
@@ -43,11 +46,7 @@ export class AstraVector extends MastraVector {
|
|
|
43
46
|
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
44
47
|
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
45
48
|
*/
|
|
46
|
-
async createIndex(
|
|
47
|
-
const params = this.normalizeArgs<CreateIndexParams>('createIndex', args);
|
|
48
|
-
|
|
49
|
-
const { indexName, dimension, metric = 'cosine' } = params;
|
|
50
|
-
|
|
49
|
+
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
51
50
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
52
51
|
throw new Error('Dimension must be a positive integer');
|
|
53
52
|
}
|
|
@@ -69,11 +68,7 @@ export class AstraVector extends MastraVector {
|
|
|
69
68
|
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
70
69
|
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
71
70
|
*/
|
|
72
|
-
async upsert(
|
|
73
|
-
const params = this.normalizeArgs<UpsertVectorParams>('upsert', args);
|
|
74
|
-
|
|
75
|
-
const { indexName, vectors, metadata, ids } = params;
|
|
76
|
-
|
|
71
|
+
async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {
|
|
77
72
|
const collection = this.#db.collection(indexName);
|
|
78
73
|
|
|
79
74
|
// Generate IDs if not provided
|
|
@@ -104,11 +99,13 @@ export class AstraVector extends MastraVector {
|
|
|
104
99
|
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
105
100
|
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
106
101
|
*/
|
|
107
|
-
async query(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
async query({
|
|
103
|
+
indexName,
|
|
104
|
+
queryVector,
|
|
105
|
+
topK = 10,
|
|
106
|
+
filter,
|
|
107
|
+
includeVector = false,
|
|
108
|
+
}: QueryVectorParams): Promise<QueryResult[]> {
|
|
112
109
|
const collection = this.#db.collection(indexName);
|
|
113
110
|
|
|
114
111
|
const translatedFilter = this.transformFilter(filter);
|
|
@@ -141,14 +138,18 @@ export class AstraVector extends MastraVector {
|
|
|
141
138
|
return this.#db.listCollections({ nameOnly: true });
|
|
142
139
|
}
|
|
143
140
|
|
|
144
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Retrieves statistics about a vector index.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} indexName - The name of the index to describe
|
|
145
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
146
|
+
*/
|
|
147
|
+
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
145
148
|
const collection = this.#db.collection(indexName);
|
|
146
149
|
const optionsPromise = collection.options();
|
|
147
150
|
const countPromise = collection.countDocuments({}, 100);
|
|
148
151
|
const [options, count] = await Promise.all([optionsPromise, countPromise]);
|
|
149
152
|
|
|
150
|
-
console.log(options, count);
|
|
151
|
-
|
|
152
153
|
const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];
|
|
153
154
|
const metric = keys.find(key => metricMap[key] === options.vector?.metric);
|
|
154
155
|
return {
|
|
@@ -164,14 +165,12 @@ export class AstraVector extends MastraVector {
|
|
|
164
165
|
* @param {string} indexName - The name of the collection to delete.
|
|
165
166
|
* @returns {Promise<void>} A promise that resolves when the collection is deleted.
|
|
166
167
|
*/
|
|
167
|
-
async deleteIndex(indexName:
|
|
168
|
+
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
168
169
|
const collection = this.#db.collection(indexName);
|
|
169
170
|
await collection.drop();
|
|
170
171
|
}
|
|
171
172
|
|
|
172
173
|
/**
|
|
173
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
174
|
-
*
|
|
175
174
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
176
175
|
* @param indexName - The name of the index containing the vector.
|
|
177
176
|
* @param id - The ID of the vector to update.
|
|
@@ -181,32 +180,7 @@ export class AstraVector extends MastraVector {
|
|
|
181
180
|
* @returns A promise that resolves when the update is complete.
|
|
182
181
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
183
182
|
*/
|
|
184
|
-
async
|
|
185
|
-
indexName: string,
|
|
186
|
-
id: string,
|
|
187
|
-
update: { vector?: number[]; metadata?: Record<string, any> },
|
|
188
|
-
): Promise<void> {
|
|
189
|
-
this.logger.warn(
|
|
190
|
-
`Deprecation Warning: updateIndexById() is deprecated. Please use updateVector() instead. updateIndexById() will be removed on May 20th, 2025.`,
|
|
191
|
-
);
|
|
192
|
-
await this.updateVector(indexName, id, update);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
197
|
-
* @param indexName - The name of the index containing the vector.
|
|
198
|
-
* @param id - The ID of the vector to update.
|
|
199
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
200
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
201
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
202
|
-
* @returns A promise that resolves when the update is complete.
|
|
203
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
204
|
-
*/
|
|
205
|
-
async updateVector(
|
|
206
|
-
indexName: string,
|
|
207
|
-
id: string,
|
|
208
|
-
update: { vector?: number[]; metadata?: Record<string, any> },
|
|
209
|
-
): Promise<void> {
|
|
183
|
+
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
210
184
|
try {
|
|
211
185
|
if (!update.vector && !update.metadata) {
|
|
212
186
|
throw new Error('No updates provided');
|
|
@@ -229,24 +203,6 @@ export class AstraVector extends MastraVector {
|
|
|
229
203
|
}
|
|
230
204
|
}
|
|
231
205
|
|
|
232
|
-
/**
|
|
233
|
-
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
234
|
-
*
|
|
235
|
-
* Deletes a vector by its ID.
|
|
236
|
-
* @param indexName - The name of the index containing the vector.
|
|
237
|
-
* @param id - The ID of the vector to delete.
|
|
238
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
239
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
240
|
-
*/
|
|
241
|
-
async deleteIndexById(indexName: string, id: string): Promise<void> {
|
|
242
|
-
this.logger.warn(
|
|
243
|
-
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
244
|
-
Please use deleteVector() instead.
|
|
245
|
-
deleteIndexById() will be removed on May 20th, 2025.`,
|
|
246
|
-
);
|
|
247
|
-
await this.deleteVector(indexName, id);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
206
|
/**
|
|
251
207
|
* Deletes a vector by its ID.
|
|
252
208
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -254,7 +210,7 @@ export class AstraVector extends MastraVector {
|
|
|
254
210
|
* @returns A promise that resolves when the deletion is complete.
|
|
255
211
|
* @throws Will throw an error if the deletion operation fails.
|
|
256
212
|
*/
|
|
257
|
-
async deleteVector(indexName
|
|
213
|
+
async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
|
|
258
214
|
try {
|
|
259
215
|
const collection = this.#db.collection(indexName);
|
|
260
216
|
await collection.deleteOne({ id });
|