@mastra/vectorize 0.0.0-a2a-20250421213654
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +74 -0
- package/dist/_tsup-dts-rollup.d.cts +49 -0
- package/dist/_tsup-dts-rollup.d.ts +49 -0
- package/dist/index.cjs +233 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +227 -0
- package/package.json +45 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Mastra AI, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @mastra/vectorize
|
|
2
|
+
|
|
3
|
+
Vector store implementation for Vectorize, a managed vector database service optimized for AI applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/vectorize
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { VectorizeStore } from '@mastra/vectorize';
|
|
15
|
+
|
|
16
|
+
const vectorStore = new VectorizeStore({
|
|
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
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Configuration
|
|
48
|
+
|
|
49
|
+
The Vectorize vector store requires the following configuration:
|
|
50
|
+
|
|
51
|
+
- `VECTORIZE_API_KEY`: Your Vectorize API key
|
|
52
|
+
- `VECTORIZE_INDEX_NAME`: Name of the index to use
|
|
53
|
+
- `VECTORIZE_PROJECT_ID`: Your Vectorize project ID
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
|
|
57
|
+
- Purpose-built for AI and ML workloads
|
|
58
|
+
- High-performance vector similarity search
|
|
59
|
+
- Automatic index optimization
|
|
60
|
+
- Scalable architecture
|
|
61
|
+
- Real-time updates and queries
|
|
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
|
+
|
|
72
|
+
## Related Links
|
|
73
|
+
|
|
74
|
+
- [Vectorize Documentation](https://www.vectorize.com/docs)
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
private verifyIndexExists;
|
|
22
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
23
|
+
query(...args: ParamsToArgs<QueryVectorParams>): Promise<QueryResult[]>;
|
|
24
|
+
listIndexes(): Promise<string[]>;
|
|
25
|
+
describeIndex(indexName: string): Promise<{
|
|
26
|
+
dimension: number;
|
|
27
|
+
count: number;
|
|
28
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
29
|
+
}>;
|
|
30
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
31
|
+
createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean'): Promise<void>;
|
|
32
|
+
deleteMetadataIndex(indexName: string, propertyName: string): Promise<void>;
|
|
33
|
+
listMetadataIndexes(indexName: string): Promise<Cloudflare.Vectorize.Indexes.MetadataIndex.MetadataIndexListResponse.MetadataIndex[]>;
|
|
34
|
+
updateIndexById(indexName: string, id: string, update: {
|
|
35
|
+
vector?: number[];
|
|
36
|
+
metadata?: Record<string, any>;
|
|
37
|
+
}): Promise<void>;
|
|
38
|
+
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export { CloudflareVector }
|
|
41
|
+
export { CloudflareVector as CloudflareVector_alias_1 }
|
|
42
|
+
|
|
43
|
+
export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
44
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
45
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
46
|
+
private translateNode;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { }
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
private verifyIndexExists;
|
|
22
|
+
createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
|
|
23
|
+
query(...args: ParamsToArgs<QueryVectorParams>): Promise<QueryResult[]>;
|
|
24
|
+
listIndexes(): Promise<string[]>;
|
|
25
|
+
describeIndex(indexName: string): Promise<{
|
|
26
|
+
dimension: number;
|
|
27
|
+
count: number;
|
|
28
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
29
|
+
}>;
|
|
30
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
31
|
+
createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean'): Promise<void>;
|
|
32
|
+
deleteMetadataIndex(indexName: string, propertyName: string): Promise<void>;
|
|
33
|
+
listMetadataIndexes(indexName: string): Promise<Cloudflare.Vectorize.Indexes.MetadataIndex.MetadataIndexListResponse.MetadataIndex[]>;
|
|
34
|
+
updateIndexById(indexName: string, id: string, update: {
|
|
35
|
+
vector?: number[];
|
|
36
|
+
metadata?: Record<string, any>;
|
|
37
|
+
}): Promise<void>;
|
|
38
|
+
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export { CloudflareVector }
|
|
41
|
+
export { CloudflareVector as CloudflareVector_alias_1 }
|
|
42
|
+
|
|
43
|
+
export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
44
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
45
|
+
translate(filter?: VectorFilter): VectorFilter;
|
|
46
|
+
private translateNode;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { }
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
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
|
+
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 verifyIndexExists(indexName, dimension) {
|
|
105
|
+
try {
|
|
106
|
+
const info = await this.client.vectorize.indexes.info(indexName, {
|
|
107
|
+
account_id: this.accountId
|
|
108
|
+
});
|
|
109
|
+
if (!info) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (info.dimensions !== dimension) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
`Index "${indexName}" already exists with ${info.dimensions} dimensions, but ${dimension} dimensions were requested`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const message = error?.errors?.[0]?.message || error?.message;
|
|
120
|
+
if (error.status === 404 || error.status === 410 || message?.toLowerCase().includes("not found") || message?.toLowerCase().includes("deleted")) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async createIndex(...args) {
|
|
127
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
128
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
129
|
+
const exists = await this.verifyIndexExists(indexName, dimension);
|
|
130
|
+
if (exists) {
|
|
131
|
+
this.logger.info(
|
|
132
|
+
`Index "${indexName}" already exists with ${dimension} dimensions and metric ${metric}, skipping creation.`
|
|
133
|
+
);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
await this.client.vectorize.indexes.create({
|
|
137
|
+
account_id: this.accountId,
|
|
138
|
+
config: {
|
|
139
|
+
dimensions: dimension,
|
|
140
|
+
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
141
|
+
},
|
|
142
|
+
name: indexName
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async query(...args) {
|
|
146
|
+
const params = this.normalizeArgs("query", args);
|
|
147
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false } = params;
|
|
148
|
+
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
149
|
+
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
150
|
+
account_id: this.accountId,
|
|
151
|
+
vector: queryVector,
|
|
152
|
+
returnValues: includeVector,
|
|
153
|
+
returnMetadata: "all",
|
|
154
|
+
topK,
|
|
155
|
+
filter: translatedFilter
|
|
156
|
+
});
|
|
157
|
+
return response?.matches?.map((match) => {
|
|
158
|
+
return {
|
|
159
|
+
id: match.id,
|
|
160
|
+
metadata: match.metadata,
|
|
161
|
+
score: match.score,
|
|
162
|
+
vector: match.values
|
|
163
|
+
};
|
|
164
|
+
}) || [];
|
|
165
|
+
}
|
|
166
|
+
async listIndexes() {
|
|
167
|
+
const res = await this.client.vectorize.indexes.list({
|
|
168
|
+
account_id: this.accountId
|
|
169
|
+
});
|
|
170
|
+
return res?.result?.map((index) => index.name) || [];
|
|
171
|
+
}
|
|
172
|
+
async describeIndex(indexName) {
|
|
173
|
+
const index = await this.client.vectorize.indexes.get(indexName, {
|
|
174
|
+
account_id: this.accountId
|
|
175
|
+
});
|
|
176
|
+
const described = await this.client.vectorize.indexes.info(indexName, {
|
|
177
|
+
account_id: this.accountId
|
|
178
|
+
});
|
|
179
|
+
return {
|
|
180
|
+
dimension: described?.dimensions,
|
|
181
|
+
// Since vector_count is not available in the response,
|
|
182
|
+
// we might need a separate API call to get the count if needed
|
|
183
|
+
count: described?.vectorCount || 0,
|
|
184
|
+
metric: index?.config?.metric
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
async deleteIndex(indexName) {
|
|
188
|
+
await this.client.vectorize.indexes.delete(indexName, {
|
|
189
|
+
account_id: this.accountId
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
193
|
+
await this.client.vectorize.indexes.metadataIndex.create(indexName, {
|
|
194
|
+
account_id: this.accountId,
|
|
195
|
+
propertyName,
|
|
196
|
+
indexType
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async deleteMetadataIndex(indexName, propertyName) {
|
|
200
|
+
await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
|
|
201
|
+
account_id: this.accountId,
|
|
202
|
+
propertyName
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async listMetadataIndexes(indexName) {
|
|
206
|
+
const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
|
|
207
|
+
account_id: this.accountId
|
|
208
|
+
});
|
|
209
|
+
return res?.metadataIndexes ?? [];
|
|
210
|
+
}
|
|
211
|
+
async updateIndexById(indexName, id, update) {
|
|
212
|
+
if (!update.vector && !update.metadata) {
|
|
213
|
+
throw new Error("No update data provided");
|
|
214
|
+
}
|
|
215
|
+
const updatePayload = {
|
|
216
|
+
};
|
|
217
|
+
if (update.vector) {
|
|
218
|
+
updatePayload.vectors = [update.vector];
|
|
219
|
+
}
|
|
220
|
+
if (update.metadata) {
|
|
221
|
+
updatePayload.metadata = [update.metadata];
|
|
222
|
+
}
|
|
223
|
+
await this.upsert({ indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });
|
|
224
|
+
}
|
|
225
|
+
async deleteIndexById(indexName, id) {
|
|
226
|
+
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
227
|
+
ids: [id],
|
|
228
|
+
account_id: this.accountId
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
exports.CloudflareVector = CloudflareVector;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareVector } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareVector } from './_tsup-dts-rollup.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
+
import Cloudflare from 'cloudflare';
|
|
3
|
+
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
4
|
+
|
|
5
|
+
// src/vector/index.ts
|
|
6
|
+
var VectorizeFilterTranslator = class extends BaseFilterTranslator {
|
|
7
|
+
getSupportedOperators() {
|
|
8
|
+
return {
|
|
9
|
+
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
10
|
+
logical: [],
|
|
11
|
+
array: ["$in", "$nin"],
|
|
12
|
+
element: [],
|
|
13
|
+
regex: [],
|
|
14
|
+
custom: []
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
translate(filter) {
|
|
18
|
+
if (this.isEmpty(filter)) return filter;
|
|
19
|
+
this.validateFilter(filter);
|
|
20
|
+
return this.translateNode(filter);
|
|
21
|
+
}
|
|
22
|
+
translateNode(node, currentPath = "") {
|
|
23
|
+
if (this.isRegex(node)) {
|
|
24
|
+
throw new Error("Regex is not supported in Vectorize");
|
|
25
|
+
}
|
|
26
|
+
if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
|
|
27
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
28
|
+
const entries = Object.entries(node);
|
|
29
|
+
const firstEntry = entries[0];
|
|
30
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
31
|
+
const [operator, value] = firstEntry;
|
|
32
|
+
return { [operator]: this.normalizeComparisonValue(value) };
|
|
33
|
+
}
|
|
34
|
+
const result = {};
|
|
35
|
+
for (const [key, value] of entries) {
|
|
36
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
37
|
+
if (this.isOperator(key)) {
|
|
38
|
+
result[key] = this.normalizeComparisonValue(value);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
42
|
+
if (Object.keys(value).length === 0) {
|
|
43
|
+
result[newPath] = this.translateNode(value);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
47
|
+
if (hasOperators) {
|
|
48
|
+
result[newPath] = this.translateNode(value);
|
|
49
|
+
} else {
|
|
50
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
result[newPath] = this.translateNode(value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/vector/index.ts
|
|
61
|
+
var CloudflareVector = class extends MastraVector {
|
|
62
|
+
client;
|
|
63
|
+
accountId;
|
|
64
|
+
constructor({ accountId, apiToken }) {
|
|
65
|
+
super();
|
|
66
|
+
this.accountId = accountId;
|
|
67
|
+
this.client = new Cloudflare({
|
|
68
|
+
apiToken
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async upsert(...args) {
|
|
72
|
+
const params = this.normalizeArgs("upsert", args);
|
|
73
|
+
const { indexName, vectors, metadata, ids } = params;
|
|
74
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
75
|
+
const ndjson = vectors.map(
|
|
76
|
+
(vector, index) => JSON.stringify({
|
|
77
|
+
id: generatedIds[index],
|
|
78
|
+
values: vector,
|
|
79
|
+
metadata: metadata?.[index]
|
|
80
|
+
})
|
|
81
|
+
).join("\n");
|
|
82
|
+
await this.client.vectorize.indexes.upsert(
|
|
83
|
+
indexName,
|
|
84
|
+
{
|
|
85
|
+
account_id: this.accountId,
|
|
86
|
+
body: ndjson
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
__binaryRequest: true
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
return generatedIds;
|
|
93
|
+
}
|
|
94
|
+
transformFilter(filter) {
|
|
95
|
+
const translator = new VectorizeFilterTranslator();
|
|
96
|
+
return translator.translate(filter);
|
|
97
|
+
}
|
|
98
|
+
async verifyIndexExists(indexName, dimension) {
|
|
99
|
+
try {
|
|
100
|
+
const info = await this.client.vectorize.indexes.info(indexName, {
|
|
101
|
+
account_id: this.accountId
|
|
102
|
+
});
|
|
103
|
+
if (!info) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (info.dimensions !== dimension) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Index "${indexName}" already exists with ${info.dimensions} dimensions, but ${dimension} dimensions were requested`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const message = error?.errors?.[0]?.message || error?.message;
|
|
114
|
+
if (error.status === 404 || error.status === 410 || message?.toLowerCase().includes("not found") || message?.toLowerCase().includes("deleted")) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async createIndex(...args) {
|
|
121
|
+
const params = this.normalizeArgs("createIndex", args);
|
|
122
|
+
const { indexName, dimension, metric = "cosine" } = params;
|
|
123
|
+
const exists = await this.verifyIndexExists(indexName, dimension);
|
|
124
|
+
if (exists) {
|
|
125
|
+
this.logger.info(
|
|
126
|
+
`Index "${indexName}" already exists with ${dimension} dimensions and metric ${metric}, skipping creation.`
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
await this.client.vectorize.indexes.create({
|
|
131
|
+
account_id: this.accountId,
|
|
132
|
+
config: {
|
|
133
|
+
dimensions: dimension,
|
|
134
|
+
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
135
|
+
},
|
|
136
|
+
name: indexName
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async query(...args) {
|
|
140
|
+
const params = this.normalizeArgs("query", args);
|
|
141
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false } = params;
|
|
142
|
+
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
143
|
+
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
144
|
+
account_id: this.accountId,
|
|
145
|
+
vector: queryVector,
|
|
146
|
+
returnValues: includeVector,
|
|
147
|
+
returnMetadata: "all",
|
|
148
|
+
topK,
|
|
149
|
+
filter: translatedFilter
|
|
150
|
+
});
|
|
151
|
+
return response?.matches?.map((match) => {
|
|
152
|
+
return {
|
|
153
|
+
id: match.id,
|
|
154
|
+
metadata: match.metadata,
|
|
155
|
+
score: match.score,
|
|
156
|
+
vector: match.values
|
|
157
|
+
};
|
|
158
|
+
}) || [];
|
|
159
|
+
}
|
|
160
|
+
async listIndexes() {
|
|
161
|
+
const res = await this.client.vectorize.indexes.list({
|
|
162
|
+
account_id: this.accountId
|
|
163
|
+
});
|
|
164
|
+
return res?.result?.map((index) => index.name) || [];
|
|
165
|
+
}
|
|
166
|
+
async describeIndex(indexName) {
|
|
167
|
+
const index = await this.client.vectorize.indexes.get(indexName, {
|
|
168
|
+
account_id: this.accountId
|
|
169
|
+
});
|
|
170
|
+
const described = await this.client.vectorize.indexes.info(indexName, {
|
|
171
|
+
account_id: this.accountId
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
dimension: described?.dimensions,
|
|
175
|
+
// Since vector_count is not available in the response,
|
|
176
|
+
// we might need a separate API call to get the count if needed
|
|
177
|
+
count: described?.vectorCount || 0,
|
|
178
|
+
metric: index?.config?.metric
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
async deleteIndex(indexName) {
|
|
182
|
+
await this.client.vectorize.indexes.delete(indexName, {
|
|
183
|
+
account_id: this.accountId
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
187
|
+
await this.client.vectorize.indexes.metadataIndex.create(indexName, {
|
|
188
|
+
account_id: this.accountId,
|
|
189
|
+
propertyName,
|
|
190
|
+
indexType
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
async deleteMetadataIndex(indexName, propertyName) {
|
|
194
|
+
await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
|
|
195
|
+
account_id: this.accountId,
|
|
196
|
+
propertyName
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async listMetadataIndexes(indexName) {
|
|
200
|
+
const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
|
|
201
|
+
account_id: this.accountId
|
|
202
|
+
});
|
|
203
|
+
return res?.metadataIndexes ?? [];
|
|
204
|
+
}
|
|
205
|
+
async updateIndexById(indexName, id, update) {
|
|
206
|
+
if (!update.vector && !update.metadata) {
|
|
207
|
+
throw new Error("No update data provided");
|
|
208
|
+
}
|
|
209
|
+
const updatePayload = {
|
|
210
|
+
};
|
|
211
|
+
if (update.vector) {
|
|
212
|
+
updatePayload.vectors = [update.vector];
|
|
213
|
+
}
|
|
214
|
+
if (update.metadata) {
|
|
215
|
+
updatePayload.metadata = [update.metadata];
|
|
216
|
+
}
|
|
217
|
+
await this.upsert({ indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });
|
|
218
|
+
}
|
|
219
|
+
async deleteIndexById(indexName, id) {
|
|
220
|
+
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
221
|
+
ids: [id],
|
|
222
|
+
account_id: this.accountId
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export { CloudflareVector };
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/vectorize",
|
|
3
|
+
"version": "0.0.0-a2a-20250421213654",
|
|
4
|
+
"description": "Cloudflare Vectorize store provider for Mastra",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"cloudflare": "^4.1.0",
|
|
27
|
+
"@mastra/core": "0.0.0-a2a-20250421213654"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@microsoft/api-extractor": "^7.52.1",
|
|
31
|
+
"@types/node": "^20.17.27",
|
|
32
|
+
"dotenv": "^16.4.7",
|
|
33
|
+
"eslint": "^9.23.0",
|
|
34
|
+
"tsup": "^8.4.0",
|
|
35
|
+
"typescript": "^5.8.2",
|
|
36
|
+
"vitest": "^3.0.9",
|
|
37
|
+
"@internal/lint": "0.0.2"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
|
41
|
+
"build:watch": "pnpm build --watch",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"lint": "eslint ."
|
|
44
|
+
}
|
|
45
|
+
}
|