@mastra/vectorize 0.0.0-storage-20250225005900
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 +19 -0
- package/CHANGELOG.md +664 -0
- package/LICENSE +44 -0
- package/README.md +39 -0
- package/dist/_tsup-dts-rollup.d.ts +39 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +165 -0
- package/package.json +33 -0
- package/src/index.ts +1 -0
- package/src/vector/filter.test.ts +198 -0
- package/src/vector/filter.ts +68 -0
- package/src/vector/index.test.ts +679 -0
- package/src/vector/index.ts +150 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
// configuration options
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
The Vectorize vector store requires the following configuration:
|
|
24
|
+
|
|
25
|
+
- `VECTORIZE_API_KEY`: Your Vectorize API key
|
|
26
|
+
- `VECTORIZE_INDEX_NAME`: Name of the index to use
|
|
27
|
+
- `VECTORIZE_PROJECT_ID`: Your Vectorize project ID
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
- Purpose-built for AI and ML workloads
|
|
32
|
+
- High-performance vector similarity search
|
|
33
|
+
- Automatic index optimization
|
|
34
|
+
- Scalable architecture
|
|
35
|
+
- Real-time updates and queries
|
|
36
|
+
|
|
37
|
+
## Related Links
|
|
38
|
+
|
|
39
|
+
- [Vectorize Documentation](https://www.vectorize.com/docs)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/filter';
|
|
2
|
+
import Cloudflare from 'cloudflare';
|
|
3
|
+
import { Filter } from '@mastra/core/filter';
|
|
4
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
5
|
+
import { OperatorSupport } from '@mastra/core/filter';
|
|
6
|
+
import { QueryResult } from '@mastra/core/vector';
|
|
7
|
+
|
|
8
|
+
declare class CloudflareVector extends MastraVector {
|
|
9
|
+
client: Cloudflare;
|
|
10
|
+
accountId: string;
|
|
11
|
+
constructor({ accountId, apiToken }: {
|
|
12
|
+
accountId: string;
|
|
13
|
+
apiToken: string;
|
|
14
|
+
});
|
|
15
|
+
upsert(indexName: string, vectors: number[][], metadata?: Record<string, any>[], ids?: string[]): Promise<string[]>;
|
|
16
|
+
transformFilter(filter?: Filter): Filter | undefined;
|
|
17
|
+
createIndex(indexName: string, dimension: number, metric?: 'cosine' | 'euclidean' | 'dotproduct'): Promise<void>;
|
|
18
|
+
query(indexName: string, queryVector: number[], topK?: number, filter?: Filter, includeVector?: boolean): Promise<QueryResult[]>;
|
|
19
|
+
listIndexes(): Promise<string[]>;
|
|
20
|
+
describeIndex(indexName: string): Promise<{
|
|
21
|
+
dimension: number;
|
|
22
|
+
count: number;
|
|
23
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
24
|
+
}>;
|
|
25
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
26
|
+
createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean'): Promise<void>;
|
|
27
|
+
deleteMetadataIndex(indexName: string, propertyName: string): Promise<void>;
|
|
28
|
+
listMetadataIndexes(indexName: string): Promise<Cloudflare.Vectorize.Indexes.MetadataIndex.MetadataIndexListResponse.MetadataIndex[]>;
|
|
29
|
+
}
|
|
30
|
+
export { CloudflareVector }
|
|
31
|
+
export { CloudflareVector as CloudflareVector_alias_1 }
|
|
32
|
+
|
|
33
|
+
export declare class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
34
|
+
protected getSupportedOperators(): OperatorSupport;
|
|
35
|
+
translate(filter?: Filter): Filter | undefined;
|
|
36
|
+
private translateNode;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { }
|
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,165 @@
|
|
|
1
|
+
import { BaseFilterTranslator } from '@mastra/core/filter';
|
|
2
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
3
|
+
import Cloudflare from 'cloudflare';
|
|
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
|
+
apiKey: apiToken
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async upsert(indexName, vectors, metadata, ids) {
|
|
72
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
73
|
+
const ndjson = vectors.map((vector, index) => ({
|
|
74
|
+
id: generatedIds[index],
|
|
75
|
+
values: vector,
|
|
76
|
+
metadata: metadata?.[index]
|
|
77
|
+
})).map((record) => JSON.stringify(record)).join("\n");
|
|
78
|
+
await this.client.vectorize.indexes.upsert(indexName, {
|
|
79
|
+
account_id: this.accountId,
|
|
80
|
+
body: ndjson
|
|
81
|
+
});
|
|
82
|
+
return generatedIds;
|
|
83
|
+
}
|
|
84
|
+
transformFilter(filter) {
|
|
85
|
+
const translator = new VectorizeFilterTranslator();
|
|
86
|
+
const translatedFilter = translator.translate(filter);
|
|
87
|
+
return translatedFilter;
|
|
88
|
+
}
|
|
89
|
+
async createIndex(indexName, dimension, metric = "cosine") {
|
|
90
|
+
await this.client.vectorize.indexes.create({
|
|
91
|
+
account_id: this.accountId,
|
|
92
|
+
config: {
|
|
93
|
+
dimensions: dimension,
|
|
94
|
+
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
95
|
+
},
|
|
96
|
+
name: indexName
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async query(indexName, queryVector, topK = 10, filter, includeVector = false) {
|
|
100
|
+
const translatedFilter = this.transformFilter(filter);
|
|
101
|
+
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
102
|
+
account_id: this.accountId,
|
|
103
|
+
vector: queryVector,
|
|
104
|
+
returnValues: includeVector,
|
|
105
|
+
returnMetadata: "all",
|
|
106
|
+
topK,
|
|
107
|
+
filter: translatedFilter
|
|
108
|
+
});
|
|
109
|
+
return response?.matches?.map((match) => {
|
|
110
|
+
return {
|
|
111
|
+
id: match.id,
|
|
112
|
+
metadata: match.metadata,
|
|
113
|
+
score: match.score,
|
|
114
|
+
vector: match.values
|
|
115
|
+
};
|
|
116
|
+
}) || [];
|
|
117
|
+
}
|
|
118
|
+
async listIndexes() {
|
|
119
|
+
const res = await this.client.vectorize.indexes.list({
|
|
120
|
+
account_id: this.accountId
|
|
121
|
+
});
|
|
122
|
+
return res?.result?.map((index) => index.name) || [];
|
|
123
|
+
}
|
|
124
|
+
async describeIndex(indexName) {
|
|
125
|
+
const index = await this.client.vectorize.indexes.get(indexName, {
|
|
126
|
+
account_id: this.accountId
|
|
127
|
+
});
|
|
128
|
+
const described = await this.client.vectorize.indexes.info(indexName, {
|
|
129
|
+
account_id: this.accountId
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
dimension: described?.dimensions,
|
|
133
|
+
// Since vector_count is not available in the response,
|
|
134
|
+
// we might need a separate API call to get the count if needed
|
|
135
|
+
count: described?.vectorCount || 0,
|
|
136
|
+
metric: index?.config?.metric
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async deleteIndex(indexName) {
|
|
140
|
+
await this.client.vectorize.indexes.delete(indexName, {
|
|
141
|
+
account_id: this.accountId
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
145
|
+
await this.client.vectorize.indexes.metadataIndex.create(indexName, {
|
|
146
|
+
account_id: this.accountId,
|
|
147
|
+
propertyName,
|
|
148
|
+
indexType
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
async deleteMetadataIndex(indexName, propertyName) {
|
|
152
|
+
await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
|
|
153
|
+
account_id: this.accountId,
|
|
154
|
+
propertyName
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
async listMetadataIndexes(indexName) {
|
|
158
|
+
const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
|
|
159
|
+
account_id: this.accountId
|
|
160
|
+
});
|
|
161
|
+
return res?.metadataIndexes ?? [];
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export { CloudflareVector };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/vectorize",
|
|
3
|
+
"version": "0.0.0-storage-20250225005900",
|
|
4
|
+
"description": "Cloudflare Vectorize store provider for Mastra",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"cloudflare": "^4.0.0",
|
|
19
|
+
"@mastra/core": "^0.0.0-storage-20250225005900"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@microsoft/api-extractor": "^7.49.2",
|
|
23
|
+
"@types/node": "^22.13.1",
|
|
24
|
+
"tsup": "^8.0.1",
|
|
25
|
+
"typescript": "^5.7.3",
|
|
26
|
+
"vitest": "^3.0.5"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
|
|
30
|
+
"build:watch": "pnpm build --watch",
|
|
31
|
+
"test": "vitest run"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vector';
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { VectorizeFilterTranslator } from './filter';
|
|
4
|
+
|
|
5
|
+
describe('VectorizeFilterTranslator', () => {
|
|
6
|
+
let translator: VectorizeFilterTranslator;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
translator = new VectorizeFilterTranslator();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('translate', () => {
|
|
13
|
+
it('handles empty filters', () => {
|
|
14
|
+
expect(translator.translate({})).toEqual({});
|
|
15
|
+
expect(translator.translate(null as any)).toEqual(null);
|
|
16
|
+
expect(translator.translate(undefined as any)).toEqual(undefined);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Basic cases
|
|
20
|
+
it('converts implicit equality to explicit $eq', () => {
|
|
21
|
+
const filter = { field: 'value' };
|
|
22
|
+
expect(translator.translate(filter)).toEqual({
|
|
23
|
+
field: { $eq: 'value' },
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('handles comparison operators', () => {
|
|
28
|
+
const filter = {
|
|
29
|
+
age: { $gt: 25 },
|
|
30
|
+
price: { $lte: 100 },
|
|
31
|
+
status: { $ne: 'inactive' },
|
|
32
|
+
quantity: { $gte: 10 },
|
|
33
|
+
rating: { $lt: 5 },
|
|
34
|
+
};
|
|
35
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('handles $in operator', () => {
|
|
39
|
+
const filter = {
|
|
40
|
+
tags: { $in: ['important', 'urgent'] },
|
|
41
|
+
};
|
|
42
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('handles $nin operator', () => {
|
|
46
|
+
const filter = {
|
|
47
|
+
status: { $nin: ['deleted', 'archived'] },
|
|
48
|
+
};
|
|
49
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('handles null values', () => {
|
|
53
|
+
const filter = {
|
|
54
|
+
field: null,
|
|
55
|
+
other: { $eq: null },
|
|
56
|
+
};
|
|
57
|
+
expect(translator.translate(filter)).toEqual({
|
|
58
|
+
field: { $eq: null },
|
|
59
|
+
other: { $eq: null },
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('handles empty objects', () => {
|
|
64
|
+
const filter = { field: {} };
|
|
65
|
+
expect(translator.translate(filter)).toEqual({ field: {} });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('flattens nested objects to dot notation', () => {
|
|
69
|
+
const filter = {
|
|
70
|
+
user: {
|
|
71
|
+
profile: {
|
|
72
|
+
age: { $gt: 25 },
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
expect(translator.translate(filter)).toEqual({
|
|
77
|
+
'user.profile.age': { $gt: 25 },
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('normalizes date values', () => {
|
|
82
|
+
const date = new Date('2024-01-01');
|
|
83
|
+
const filter = { timestamp: { $gt: date } };
|
|
84
|
+
expect(translator.translate(filter)).toEqual({
|
|
85
|
+
timestamp: { $gt: date.toISOString() },
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('handles multiple operators on same field', () => {
|
|
90
|
+
const filter = {
|
|
91
|
+
price: { $gt: 100, $lt: 200 },
|
|
92
|
+
quantity: { $gte: 10, $lte: 20 },
|
|
93
|
+
};
|
|
94
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('handles arrays of mixed types', () => {
|
|
98
|
+
const filter = {
|
|
99
|
+
field: { $in: [123, 'string', true] },
|
|
100
|
+
};
|
|
101
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('handles empty arrays in $in and $nin', () => {
|
|
105
|
+
const filter = {
|
|
106
|
+
field1: { $in: [] },
|
|
107
|
+
field2: { $nin: [] },
|
|
108
|
+
};
|
|
109
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('handles deeply nested null values', () => {
|
|
113
|
+
const filter = {
|
|
114
|
+
user: {
|
|
115
|
+
profile: {
|
|
116
|
+
lastLogin: null,
|
|
117
|
+
settings: { theme: { $eq: null } },
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
expect(translator.translate(filter)).toEqual({
|
|
122
|
+
'user.profile.lastLogin': { $eq: null },
|
|
123
|
+
'user.profile.settings.theme': { $eq: null },
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('preserves order of multiple operators', () => {
|
|
128
|
+
const filter = {
|
|
129
|
+
field: {
|
|
130
|
+
$gt: 0,
|
|
131
|
+
$lt: 10,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('preserves order of range operators', () => {
|
|
138
|
+
// Valid range operator combinations
|
|
139
|
+
const filters = [
|
|
140
|
+
{ field: { $gt: 0, $lt: 10 } },
|
|
141
|
+
{ field: { $gte: 0, $lte: 10 } },
|
|
142
|
+
{ field: { $gt: 0, $lte: 10 } },
|
|
143
|
+
{ field: { $gte: 0, $lt: 10 } },
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
filters.forEach(filter => {
|
|
147
|
+
expect(translator.translate(filter)).toEqual(filter);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('validate operators', () => {
|
|
153
|
+
it('ensure all operator filters are supported', () => {
|
|
154
|
+
const supportedFilters = [
|
|
155
|
+
{ field: { $eq: 'value' } },
|
|
156
|
+
{ field: { $ne: 'value' } },
|
|
157
|
+
{ field: { $gt: 'value' } },
|
|
158
|
+
{ field: { $gte: 'value' } },
|
|
159
|
+
{ field: { $lt: 'value' } },
|
|
160
|
+
{ field: { $lte: 'value' } },
|
|
161
|
+
{ field: { $in: ['value'] } },
|
|
162
|
+
{ field: { $nin: ['value'] } },
|
|
163
|
+
];
|
|
164
|
+
supportedFilters.forEach(filter => {
|
|
165
|
+
expect(() => translator.translate(filter)).not.toThrow();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
it('throws error for unsupported operators', () => {
|
|
169
|
+
const unsupportedFilters = [
|
|
170
|
+
{ field: { $regex: 'pattern' } },
|
|
171
|
+
{ field: { $exists: true } },
|
|
172
|
+
{ field: { $elemMatch: { $gt: 5 } } },
|
|
173
|
+
{ field: { $nor: [{ $eq: 'value' }] } },
|
|
174
|
+
{ field: { $not: [{ $eq: 'value' }] } },
|
|
175
|
+
{ field: { $regex: 'pattern', $options: 'i' } },
|
|
176
|
+
{ field: { $and: [{ $eq: 'value' }] } },
|
|
177
|
+
{ field: { $or: [{ $eq: 'value' }] } },
|
|
178
|
+
{ field: { $all: [{ $eq: 'value' }] } },
|
|
179
|
+
{ field: { $contains: 'value' } },
|
|
180
|
+
];
|
|
181
|
+
|
|
182
|
+
unsupportedFilters.forEach(filter => {
|
|
183
|
+
expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
it('throws error for regex operators', () => {
|
|
187
|
+
const filter = { field: /pattern/i };
|
|
188
|
+
expect(() => translator.translate(filter)).toThrow();
|
|
189
|
+
});
|
|
190
|
+
it('throws error for non-logical operators at top level', () => {
|
|
191
|
+
const invalidFilters = [{ $gt: 100 }, { $in: ['value1', 'value2'] }, { $eq: true }];
|
|
192
|
+
|
|
193
|
+
invalidFilters.forEach(filter => {
|
|
194
|
+
expect(() => translator.translate(filter)).toThrow(/Invalid top-level operator/);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { BaseFilterTranslator, type Filter, type FieldCondition, type OperatorSupport } from '@mastra/core/filter';
|
|
2
|
+
|
|
3
|
+
export class VectorizeFilterTranslator extends BaseFilterTranslator {
|
|
4
|
+
protected override getSupportedOperators(): OperatorSupport {
|
|
5
|
+
return {
|
|
6
|
+
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
7
|
+
logical: [],
|
|
8
|
+
array: ['$in', '$nin'],
|
|
9
|
+
element: [],
|
|
10
|
+
regex: [],
|
|
11
|
+
custom: [],
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
translate(filter?: Filter): Filter | undefined {
|
|
16
|
+
if (this.isEmpty(filter)) return filter;
|
|
17
|
+
this.validateFilter(filter as Filter);
|
|
18
|
+
return this.translateNode(filter);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private translateNode(node: Filter | FieldCondition, currentPath: string = ''): any {
|
|
22
|
+
if (this.isRegex(node)) {
|
|
23
|
+
throw new Error('Regex is not supported in Vectorize');
|
|
24
|
+
}
|
|
25
|
+
if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
|
|
26
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
27
|
+
|
|
28
|
+
const entries = Object.entries(node as Record<string, any>);
|
|
29
|
+
const firstEntry = entries[0];
|
|
30
|
+
|
|
31
|
+
// Handle single operator case
|
|
32
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
33
|
+
const [operator, value] = firstEntry;
|
|
34
|
+
return { [operator]: this.normalizeComparisonValue(value) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Process each entry
|
|
38
|
+
const result: Record<string, any> = {};
|
|
39
|
+
for (const [key, value] of entries) {
|
|
40
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
41
|
+
|
|
42
|
+
if (this.isOperator(key)) {
|
|
43
|
+
result[key] = this.normalizeComparisonValue(value);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
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
|
+
|
|
53
|
+
// Check if the nested object contains operators
|
|
54
|
+
const hasOperators = Object.keys(value).some(k => this.isOperator(k));
|
|
55
|
+
if (hasOperators) {
|
|
56
|
+
result[newPath] = this.translateNode(value);
|
|
57
|
+
} else {
|
|
58
|
+
// For objects without operators, flatten them
|
|
59
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
result[newPath] = this.translateNode(value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
}
|