@mastra/vectorize 0.1.6-alpha.0 → 0.1.6-alpha.3
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 +10 -5
- package/CHANGELOG.md +37 -0
- package/README.md +36 -1
- package/dist/_tsup-dts-rollup.d.cts +43 -0
- package/dist/_tsup-dts-rollup.d.ts +13 -9
- package/dist/index.cjs +184 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.js +12 -7
- package/eslint.config.js +6 -0
- package/package.json +12 -5
- package/src/vector/filter.ts +5 -4
- package/src/vector/index.test.ts +345 -93
- package/src/vector/index.ts +27 -24
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/vectorize@0.1.6-alpha.
|
|
3
|
-
> tsup src/index.ts --format esm --experimental-dts --clean --treeshake
|
|
2
|
+
> @mastra/vectorize@0.1.6-alpha.3 build /home/runner/work/mastra/mastra/stores/vectorize
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake
|
|
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.3.6
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 12974ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.7.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/vectorize/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
|
-
|
|
14
|
+
Analysis will use the bundled TypeScript version 5.7.3
|
|
15
|
+
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/vectorize/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 17317ms
|
|
15
17
|
[34mCLI[39m Cleaning output folder
|
|
16
18
|
[34mESM[39m Build start
|
|
17
|
-
[
|
|
19
|
+
[34mCJS[39m Build start
|
|
20
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m5.68 KB[39m
|
|
21
|
+
[32mCJS[39m ⚡️ Build success in 465ms
|
|
22
|
+
[32mESM[39m [1mdist/index.js [22m[32m5.48 KB[39m
|
|
18
23
|
[32mESM[39m ⚡️ Build success in 465ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# @mastra/vectorize
|
|
2
2
|
|
|
3
|
+
## 0.1.6-alpha.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0fd78ac: Update vector store functions to use object params
|
|
8
|
+
- fd14a3f: Updating filter location from @mastra/core/filter to @mastra/core/vector/filter
|
|
9
|
+
- c4fdac3: Updated tests for upstash and astra
|
|
10
|
+
- 4d4e1e1: Updated vector tests and pinecone
|
|
11
|
+
- bb4f447: Add support for commonjs
|
|
12
|
+
- Updated dependencies [0fd78ac]
|
|
13
|
+
- Updated dependencies [0d25b75]
|
|
14
|
+
- Updated dependencies [fd14a3f]
|
|
15
|
+
- Updated dependencies [3f369a2]
|
|
16
|
+
- Updated dependencies [4d4e1e1]
|
|
17
|
+
- Updated dependencies [bb4f447]
|
|
18
|
+
- @mastra/core@0.4.3-alpha.3
|
|
19
|
+
|
|
20
|
+
## 0.1.6-alpha.2
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [2512a93]
|
|
25
|
+
- Updated dependencies [e62de74]
|
|
26
|
+
- @mastra/core@0.4.3-alpha.2
|
|
27
|
+
|
|
28
|
+
## 0.1.6-alpha.1
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- Updated dependencies [0d185b1]
|
|
33
|
+
- Updated dependencies [ed55f1d]
|
|
34
|
+
- Updated dependencies [8d13b14]
|
|
35
|
+
- Updated dependencies [3ee4831]
|
|
36
|
+
- Updated dependencies [108793c]
|
|
37
|
+
- Updated dependencies [5f28f44]
|
|
38
|
+
- @mastra/core@0.4.3-alpha.1
|
|
39
|
+
|
|
3
40
|
## 0.1.6-alpha.0
|
|
4
41
|
|
|
5
42
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -14,7 +14,33 @@ npm install @mastra/vectorize
|
|
|
14
14
|
import { VectorizeStore } from '@mastra/vectorize';
|
|
15
15
|
|
|
16
16
|
const vectorStore = new VectorizeStore({
|
|
17
|
-
|
|
17
|
+
apiKey: process.env.VECTORIZE_API_KEY,
|
|
18
|
+
projectId: process.env.VECTORIZE_PROJECT_ID
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Create a new index
|
|
22
|
+
await vectorStore.createIndex({
|
|
23
|
+
indexName: 'my-index',
|
|
24
|
+
dimension: 1536,
|
|
25
|
+
metric: 'cosine'
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Add vectors
|
|
29
|
+
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
|
|
30
|
+
const metadata = [{ text: 'doc1' }, { text: 'doc2' }];
|
|
31
|
+
const ids = await vectorStore.upsert({
|
|
32
|
+
indexName: 'my-index',
|
|
33
|
+
vectors,
|
|
34
|
+
metadata
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Query vectors
|
|
38
|
+
const results = await vectorStore.query({
|
|
39
|
+
indexName: 'my-index',
|
|
40
|
+
queryVector: [0.1, 0.2, ...],
|
|
41
|
+
topK: 10,
|
|
42
|
+
filter: { text: { $eq: 'doc1' } },
|
|
43
|
+
includeVector: false
|
|
18
44
|
});
|
|
19
45
|
```
|
|
20
46
|
|
|
@@ -34,6 +60,15 @@ The Vectorize vector store requires the following configuration:
|
|
|
34
60
|
- Scalable architecture
|
|
35
61
|
- Real-time updates and queries
|
|
36
62
|
|
|
63
|
+
## Methods
|
|
64
|
+
|
|
65
|
+
- `createIndex({ indexName, dimension, metric? })`: Create a new index
|
|
66
|
+
- `upsert({ indexName, vectors, metadata?, ids? })`: Add or update vectors
|
|
67
|
+
- `query({ indexName, queryVector, topK?, filter?, includeVector? })`: Search for similar vectors
|
|
68
|
+
- `listIndexes()`: List all indexes
|
|
69
|
+
- `describeIndex(indexName)`: Get index statistics
|
|
70
|
+
- `deleteIndex(indexName)`: Delete an index
|
|
71
|
+
|
|
37
72
|
## Related Links
|
|
38
73
|
|
|
39
74
|
- [Vectorize Documentation](https://www.vectorize.com/docs)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
+
import Cloudflare from 'cloudflare';
|
|
3
|
+
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
4
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
5
|
+
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
6
|
+
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
7
|
+
import type { QueryResult } from '@mastra/core/vector';
|
|
8
|
+
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
9
|
+
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
10
|
+
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
11
|
+
|
|
12
|
+
declare class CloudflareVector extends MastraVector {
|
|
13
|
+
client: Cloudflare;
|
|
14
|
+
accountId: string;
|
|
15
|
+
constructor({ accountId, apiToken }: {
|
|
16
|
+
accountId: string;
|
|
17
|
+
apiToken: string;
|
|
18
|
+
});
|
|
19
|
+
upsert(...args: ParamsToArgs<UpsertVectorParams>): Promise<string[]>;
|
|
20
|
+
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
21
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
22
|
+
query(...args: ParamsToArgs<QueryVectorParams>): Promise<QueryResult[]>;
|
|
23
|
+
listIndexes(): Promise<string[]>;
|
|
24
|
+
describeIndex(indexName: string): Promise<{
|
|
25
|
+
dimension: number;
|
|
26
|
+
count: number;
|
|
27
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
28
|
+
}>;
|
|
29
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
30
|
+
createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean'): Promise<void>;
|
|
31
|
+
deleteMetadataIndex(indexName: string, propertyName: string): Promise<void>;
|
|
32
|
+
listMetadataIndexes(indexName: string): Promise<Cloudflare.Vectorize.Indexes.MetadataIndex.MetadataIndexListResponse.MetadataIndex[]>;
|
|
33
|
+
}
|
|
34
|
+
export { CloudflareVector }
|
|
35
|
+
export { CloudflareVector as CloudflareVector_alias_1 }
|
|
36
|
+
|
|
37
|
+
export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
38
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
39
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
40
|
+
private translateNode;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { }
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/filter';
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
2
|
import Cloudflare from 'cloudflare';
|
|
3
|
-
import {
|
|
3
|
+
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
4
4
|
import { MastraVector } from '@mastra/core/vector';
|
|
5
|
-
import { OperatorSupport } from '@mastra/core/filter';
|
|
6
|
-
import {
|
|
5
|
+
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
6
|
+
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
7
|
+
import type { QueryResult } from '@mastra/core/vector';
|
|
8
|
+
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
9
|
+
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
10
|
+
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
7
11
|
|
|
8
12
|
declare class CloudflareVector extends MastraVector {
|
|
9
13
|
client: Cloudflare;
|
|
@@ -12,10 +16,10 @@ declare class CloudflareVector extends MastraVector {
|
|
|
12
16
|
accountId: string;
|
|
13
17
|
apiToken: string;
|
|
14
18
|
});
|
|
15
|
-
upsert(
|
|
16
|
-
transformFilter(filter?:
|
|
17
|
-
createIndex(
|
|
18
|
-
query(
|
|
19
|
+
upsert(...args: ParamsToArgs<UpsertVectorParams>): Promise<string[]>;
|
|
20
|
+
transformFilter(filter?: VectorFilter): VectorFilter;
|
|
21
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
22
|
+
query(...args: ParamsToArgs<QueryVectorParams>): Promise<QueryResult[]>;
|
|
19
23
|
listIndexes(): Promise<string[]>;
|
|
20
24
|
describeIndex(indexName: string): Promise<{
|
|
21
25
|
dimension: number;
|
|
@@ -32,7 +36,7 @@ export { CloudflareVector as CloudflareVector_alias_1 }
|
|
|
32
36
|
|
|
33
37
|
export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
34
38
|
protected getSupportedOperators(): OperatorSupport;
|
|
35
|
-
translate(filter?:
|
|
39
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
36
40
|
private translateNode;
|
|
37
41
|
}
|
|
38
42
|
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vector = require('@mastra/core/vector');
|
|
4
|
+
var Cloudflare = require('cloudflare');
|
|
5
|
+
var filter = require('@mastra/core/vector/filter');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
|
|
10
|
+
|
|
11
|
+
// src/vector/index.ts
|
|
12
|
+
var VectorizeFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
13
|
+
getSupportedOperators() {
|
|
14
|
+
return {
|
|
15
|
+
...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
16
|
+
logical: [],
|
|
17
|
+
array: ["$in", "$nin"],
|
|
18
|
+
element: [],
|
|
19
|
+
regex: [],
|
|
20
|
+
custom: []
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
translate(filter) {
|
|
24
|
+
if (this.isEmpty(filter)) return filter;
|
|
25
|
+
this.validateFilter(filter);
|
|
26
|
+
return this.translateNode(filter);
|
|
27
|
+
}
|
|
28
|
+
translateNode(node, currentPath = "") {
|
|
29
|
+
if (this.isRegex(node)) {
|
|
30
|
+
throw new Error("Regex is not supported in Vectorize");
|
|
31
|
+
}
|
|
32
|
+
if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
|
|
33
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
34
|
+
const entries = Object.entries(node);
|
|
35
|
+
const firstEntry = entries[0];
|
|
36
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
37
|
+
const [operator, value] = firstEntry;
|
|
38
|
+
return { [operator]: this.normalizeComparisonValue(value) };
|
|
39
|
+
}
|
|
40
|
+
const result = {};
|
|
41
|
+
for (const [key, value] of entries) {
|
|
42
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
43
|
+
if (this.isOperator(key)) {
|
|
44
|
+
result[key] = this.normalizeComparisonValue(value);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
48
|
+
if (Object.keys(value).length === 0) {
|
|
49
|
+
result[newPath] = this.translateNode(value);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
53
|
+
if (hasOperators) {
|
|
54
|
+
result[newPath] = this.translateNode(value);
|
|
55
|
+
} else {
|
|
56
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
result[newPath] = this.translateNode(value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/vector/index.ts
|
|
67
|
+
var CloudflareVector = class extends vector.MastraVector {
|
|
68
|
+
client;
|
|
69
|
+
accountId;
|
|
70
|
+
constructor({ accountId, apiToken }) {
|
|
71
|
+
super();
|
|
72
|
+
this.accountId = accountId;
|
|
73
|
+
this.client = new Cloudflare__default.default({
|
|
74
|
+
apiKey: apiToken
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async upsert(...args) {
|
|
78
|
+
const params = this.normalizeArgs("upsert", args);
|
|
79
|
+
const { indexName, vectors, metadata, ids } = params;
|
|
80
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
81
|
+
const ndjson = vectors.map(
|
|
82
|
+
(vector, index) => JSON.stringify({
|
|
83
|
+
id: generatedIds[index],
|
|
84
|
+
values: vector,
|
|
85
|
+
metadata: metadata?.[index]
|
|
86
|
+
})
|
|
87
|
+
).join("\n");
|
|
88
|
+
await this.client.vectorize.indexes.upsert(
|
|
89
|
+
indexName,
|
|
90
|
+
{
|
|
91
|
+
account_id: this.accountId,
|
|
92
|
+
body: ndjson
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
__binaryRequest: true
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
return generatedIds;
|
|
99
|
+
}
|
|
100
|
+
transformFilter(filter) {
|
|
101
|
+
const translator = new VectorizeFilterTranslator();
|
|
102
|
+
return translator.translate(filter);
|
|
103
|
+
}
|
|
104
|
+
async createIndex(...args) {
|
|
105
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
106
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
107
|
+
await this.client.vectorize.indexes.create({
|
|
108
|
+
account_id: this.accountId,
|
|
109
|
+
config: {
|
|
110
|
+
dimensions: dimension,
|
|
111
|
+
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
112
|
+
},
|
|
113
|
+
name: indexName
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async query(...args) {
|
|
117
|
+
const params = this.normalizeArgs("query", args);
|
|
118
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false } = params;
|
|
119
|
+
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
120
|
+
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
121
|
+
account_id: this.accountId,
|
|
122
|
+
vector: queryVector,
|
|
123
|
+
returnValues: includeVector,
|
|
124
|
+
returnMetadata: "all",
|
|
125
|
+
topK,
|
|
126
|
+
filter: translatedFilter
|
|
127
|
+
});
|
|
128
|
+
return response?.matches?.map((match) => {
|
|
129
|
+
return {
|
|
130
|
+
id: match.id,
|
|
131
|
+
metadata: match.metadata,
|
|
132
|
+
score: match.score,
|
|
133
|
+
vector: match.values
|
|
134
|
+
};
|
|
135
|
+
}) || [];
|
|
136
|
+
}
|
|
137
|
+
async listIndexes() {
|
|
138
|
+
const res = await this.client.vectorize.indexes.list({
|
|
139
|
+
account_id: this.accountId
|
|
140
|
+
});
|
|
141
|
+
return res?.result?.map((index) => index.name) || [];
|
|
142
|
+
}
|
|
143
|
+
async describeIndex(indexName) {
|
|
144
|
+
const index = await this.client.vectorize.indexes.get(indexName, {
|
|
145
|
+
account_id: this.accountId
|
|
146
|
+
});
|
|
147
|
+
const described = await this.client.vectorize.indexes.info(indexName, {
|
|
148
|
+
account_id: this.accountId
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
dimension: described?.dimensions,
|
|
152
|
+
// Since vector_count is not available in the response,
|
|
153
|
+
// we might need a separate API call to get the count if needed
|
|
154
|
+
count: described?.vectorCount || 0,
|
|
155
|
+
metric: index?.config?.metric
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async deleteIndex(indexName) {
|
|
159
|
+
await this.client.vectorize.indexes.delete(indexName, {
|
|
160
|
+
account_id: this.accountId
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
164
|
+
await this.client.vectorize.indexes.metadataIndex.create(indexName, {
|
|
165
|
+
account_id: this.accountId,
|
|
166
|
+
propertyName,
|
|
167
|
+
indexType
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async deleteMetadataIndex(indexName, propertyName) {
|
|
171
|
+
await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
|
|
172
|
+
account_id: this.accountId,
|
|
173
|
+
propertyName
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
async listMetadataIndexes(indexName) {
|
|
177
|
+
const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
|
|
178
|
+
account_id: this.accountId
|
|
179
|
+
});
|
|
180
|
+
return res?.metadataIndexes ?? [];
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
exports.CloudflareVector = CloudflareVector;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareVector } from './_tsup-dts-rollup.cjs';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/filter';
|
|
2
1
|
import { MastraVector } from '@mastra/core/vector';
|
|
3
2
|
import Cloudflare from 'cloudflare';
|
|
3
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
4
4
|
|
|
5
5
|
// src/vector/index.ts
|
|
6
6
|
var VectorizeFilterTranslator = class extends BaseFilterTranslator {
|
|
@@ -68,7 +68,9 @@ var CloudflareVector = class extends MastraVector {
|
|
|
68
68
|
apiKey: apiToken
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
|
-
async upsert(
|
|
71
|
+
async upsert(...args) {
|
|
72
|
+
const params = this.normalizeArgs("upsert", args);
|
|
73
|
+
const { indexName, vectors, metadata, ids } = params;
|
|
72
74
|
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
73
75
|
const ndjson = vectors.map(
|
|
74
76
|
(vector, index) => JSON.stringify({
|
|
@@ -91,10 +93,11 @@ var CloudflareVector = class extends MastraVector {
|
|
|
91
93
|
}
|
|
92
94
|
transformFilter(filter) {
|
|
93
95
|
const translator = new VectorizeFilterTranslator();
|
|
94
|
-
|
|
95
|
-
return translatedFilter;
|
|
96
|
+
return translator.translate(filter);
|
|
96
97
|
}
|
|
97
|
-
async createIndex(
|
|
98
|
+
async createIndex(...args) {
|
|
99
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
100
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
98
101
|
await this.client.vectorize.indexes.create({
|
|
99
102
|
account_id: this.accountId,
|
|
100
103
|
config: {
|
|
@@ -104,8 +107,10 @@ var CloudflareVector = class extends MastraVector {
|
|
|
104
107
|
name: indexName
|
|
105
108
|
});
|
|
106
109
|
}
|
|
107
|
-
async query(
|
|
108
|
-
const
|
|
110
|
+
async query(...args) {
|
|
111
|
+
const params = this.normalizeArgs("query", args);
|
|
112
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false } = params;
|
|
113
|
+
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
109
114
|
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
110
115
|
account_id: this.accountId,
|
|
111
116
|
vector: queryVector,
|
package/eslint.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/vectorize",
|
|
3
|
-
"version": "0.1.6-alpha.
|
|
3
|
+
"version": "0.1.6-alpha.3",
|
|
4
4
|
"description": "Cloudflare Vectorize store provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -10,24 +10,31 @@
|
|
|
10
10
|
"import": {
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.cts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
13
17
|
}
|
|
14
18
|
},
|
|
15
19
|
"./package.json": "./package.json"
|
|
16
20
|
},
|
|
17
21
|
"dependencies": {
|
|
18
22
|
"cloudflare": "^4.0.0",
|
|
19
|
-
"@mastra/core": "^0.4.3-alpha.
|
|
23
|
+
"@mastra/core": "^0.4.3-alpha.3"
|
|
20
24
|
},
|
|
21
25
|
"devDependencies": {
|
|
22
26
|
"@microsoft/api-extractor": "^7.49.2",
|
|
23
27
|
"@types/node": "^22.13.1",
|
|
24
28
|
"tsup": "^8.0.1",
|
|
25
29
|
"typescript": "^5.7.3",
|
|
26
|
-
"vitest": "^3.0.5"
|
|
30
|
+
"vitest": "^3.0.5",
|
|
31
|
+
"eslint": "^9.20.1",
|
|
32
|
+
"@internal/lint": "0.0.0"
|
|
27
33
|
},
|
|
28
34
|
"scripts": {
|
|
29
|
-
"build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
|
|
35
|
+
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake",
|
|
30
36
|
"build:watch": "pnpm build --watch",
|
|
31
|
-
"test": "vitest run"
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"lint": "eslint ."
|
|
32
39
|
}
|
|
33
40
|
}
|
package/src/vector/filter.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { BaseFilterTranslator
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
+
import type { VectorFilter, FieldCondition, OperatorSupport } from '@mastra/core/vector/filter';
|
|
2
3
|
|
|
3
4
|
export class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
4
5
|
protected override getSupportedOperators(): OperatorSupport {
|
|
@@ -12,13 +13,13 @@ export class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
translate(filter?:
|
|
16
|
+
translate(filter?: VectorFilter): VectorFilter {
|
|
16
17
|
if (this.isEmpty(filter)) return filter;
|
|
17
|
-
this.validateFilter(filter
|
|
18
|
+
this.validateFilter(filter);
|
|
18
19
|
return this.translateNode(filter);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
private translateNode(node:
|
|
22
|
+
private translateNode(node: VectorFilter | FieldCondition, currentPath: string = ''): any {
|
|
22
23
|
if (this.isRegex(node)) {
|
|
23
24
|
throw new Error('Regex is not supported in Vectorize');
|
|
24
25
|
}
|