@mastra/chroma 0.11.6 → 0.11.7-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/CHANGELOG.md +18 -0
- package/package.json +18 -5
- package/.turbo/turbo-build.log +0 -4
- package/docker-compose.yaml +0 -7
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -2
- package/src/vector/filter.test.ts +0 -421
- package/src/vector/filter.ts +0 -138
- package/src/vector/index.test.ts +0 -1672
- package/src/vector/index.ts +0 -418
- package/src/vector/prompt.ts +0 -72
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -17
- package/vitest.config.ts +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @mastra/chroma
|
|
2
2
|
|
|
3
|
+
## 0.11.7-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#7394](https://github.com/mastra-ai/mastra/pull/7394) [`f0dfcac`](https://github.com/mastra-ai/mastra/commit/f0dfcac4458bdf789b975e2d63e984f5d1e7c4d3) Thanks [@NikAiyer](https://github.com/NikAiyer)! - updated core peerdep
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`7149d8d`](https://github.com/mastra-ai/mastra/commit/7149d8d4bdc1edf0008e0ca9b7925eb0b8b60dbe)]:
|
|
10
|
+
- @mastra/core@0.15.3-alpha.7
|
|
11
|
+
|
|
12
|
+
## 0.11.7-alpha.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- [#7343](https://github.com/mastra-ai/mastra/pull/7343) [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e) Thanks [@LekoArts](https://github.com/LekoArts)! - Update the `package.json` file to include additional fields like `repository`, `homepage` or `files`.
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [[`85ef90b`](https://github.com/mastra-ai/mastra/commit/85ef90bb2cd4ae4df855c7ac175f7d392c55c1bf), [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e)]:
|
|
19
|
+
- @mastra/core@0.15.3-alpha.5
|
|
20
|
+
|
|
3
21
|
## 0.11.6
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/chroma",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.7-alpha.1",
|
|
4
4
|
"description": "Chroma vector store provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
|
-
"license": "
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"chromadb": "^3.0.11"
|
|
24
24
|
},
|
|
@@ -30,12 +30,25 @@
|
|
|
30
30
|
"typescript": "^5.8.3",
|
|
31
31
|
"vitest": "^3.2.4",
|
|
32
32
|
"@internal/lint": "0.0.34",
|
|
33
|
-
"@
|
|
33
|
+
"@mastra/core": "0.15.3-alpha.7",
|
|
34
34
|
"@internal/storage-test-utils": "0.0.30",
|
|
35
|
-
"@
|
|
35
|
+
"@internal/types-builder": "0.0.9"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@mastra/core": ">=0.
|
|
38
|
+
"@mastra/core": ">=0.15.3-0 <0.16.0-0"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"CHANGELOG.md"
|
|
43
|
+
],
|
|
44
|
+
"homepage": "https://mastra.ai",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/mastra-ai/mastra.git",
|
|
48
|
+
"directory": "stores/chroma"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/mastra-ai/mastra/issues"
|
|
39
52
|
},
|
|
40
53
|
"scripts": {
|
|
41
54
|
"build": "tsup --silent --config tsup.config.ts",
|
package/.turbo/turbo-build.log
DELETED
package/docker-compose.yaml
DELETED
package/eslint.config.js
DELETED
package/src/index.ts
DELETED
|
@@ -1,421 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import type { ChromaVectorFilter } from './filter';
|
|
4
|
-
import { ChromaFilterTranslator } from './filter';
|
|
5
|
-
|
|
6
|
-
describe('ChromaFilterTranslator', () => {
|
|
7
|
-
let translator: ChromaFilterTranslator;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
translator = new ChromaFilterTranslator();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
// Basic Filter Operations
|
|
14
|
-
describe('basic operations', () => {
|
|
15
|
-
it('handles empty filters', () => {
|
|
16
|
-
expect(translator.translate({})).toEqual({});
|
|
17
|
-
expect(translator.translate(null)).toEqual(null);
|
|
18
|
-
expect(translator.translate(undefined)).toEqual(undefined);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('retains implicit equality', () => {
|
|
22
|
-
const filter: ChromaVectorFilter = { field: 'value' };
|
|
23
|
-
expect(translator.translate(filter)).toEqual({ field: 'value' });
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('converts multiple top-level fields to $and', () => {
|
|
27
|
-
const filter: ChromaVectorFilter = {
|
|
28
|
-
field1: 'value1',
|
|
29
|
-
field2: 'value2',
|
|
30
|
-
};
|
|
31
|
-
expect(translator.translate(filter)).toEqual({
|
|
32
|
-
$and: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('handles multiple operators on same field', () => {
|
|
37
|
-
const filter: ChromaVectorFilter = {
|
|
38
|
-
price: { $gt: 100, $lt: 200 },
|
|
39
|
-
quantity: { $gte: 10, $lte: 20 },
|
|
40
|
-
};
|
|
41
|
-
expect(translator.translate(filter)).toEqual({
|
|
42
|
-
$and: [
|
|
43
|
-
{ price: { $gt: 100 } },
|
|
44
|
-
{ price: { $lt: 200 } },
|
|
45
|
-
{ quantity: { $gte: 10 } },
|
|
46
|
-
{ quantity: { $lte: 20 } },
|
|
47
|
-
],
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('normalizes date values', () => {
|
|
52
|
-
const date = new Date('2024-01-01');
|
|
53
|
-
const filter: ChromaVectorFilter = { timestamp: { $gt: date } };
|
|
54
|
-
expect(translator.translate(filter)).toEqual({ timestamp: { $gt: date.toISOString() } });
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Array Operations
|
|
59
|
-
describe('array operations', () => {
|
|
60
|
-
it('handles arrays as $in operator', () => {
|
|
61
|
-
const filter: ChromaVectorFilter = { tags: ['tag1', 'tag2'] };
|
|
62
|
-
expect(translator.translate(filter)).toEqual({ tags: { $in: ['tag1', 'tag2'] } });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('handles empty array values', () => {
|
|
66
|
-
// $in with empty array is valid in Pinecone
|
|
67
|
-
expect(translator.translate({ tags: { $in: [] } })).toEqual({ tags: { $in: [] } });
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('handles arrays as direct values', () => {
|
|
71
|
-
// Direct array value should be converted to $in
|
|
72
|
-
const filter: ChromaVectorFilter = { field: ['value1', 'value2'] };
|
|
73
|
-
expect(translator.translate(filter)).toEqual({
|
|
74
|
-
field: { $in: ['value1', 'value2'] },
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Empty direct array
|
|
78
|
-
const filter2 = { field: [] };
|
|
79
|
-
expect(translator.translate(filter2)).toEqual({ field: { $in: [] } });
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
describe('$in operator variations', () => {
|
|
83
|
-
it('handles $in with various values', () => {
|
|
84
|
-
// Empty array
|
|
85
|
-
expect(translator.translate({ field: { $in: [] } })).toEqual({ field: { $in: [] } });
|
|
86
|
-
|
|
87
|
-
// Single value
|
|
88
|
-
expect(translator.translate({ field: { $in: ['value'] } })).toEqual({ field: { $in: ['value'] } });
|
|
89
|
-
|
|
90
|
-
// Multiple values
|
|
91
|
-
expect(translator.translate({ field: { $in: [1, 'two', true] } })).toEqual({
|
|
92
|
-
field: { $in: [1, 'two', true] },
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// With dates
|
|
96
|
-
const date = new Date('2024-01-01');
|
|
97
|
-
expect(translator.translate({ field: { $in: [date.toISOString()] } })).toEqual({
|
|
98
|
-
field: { $in: [date.toISOString()] },
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// Logical Operators
|
|
105
|
-
describe('logical operators', () => {
|
|
106
|
-
it('handles logical operators', () => {
|
|
107
|
-
const filter: ChromaVectorFilter = {
|
|
108
|
-
$or: [{ status: { $eq: 'active' } }, { age: { $gt: 25 } }],
|
|
109
|
-
};
|
|
110
|
-
expect(translator.translate(filter)).toEqual({
|
|
111
|
-
$or: [{ status: { $eq: 'active' } }, { age: { $gt: 25 } }],
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('handles nested logical operators', () => {
|
|
116
|
-
const filter: ChromaVectorFilter = {
|
|
117
|
-
$and: [
|
|
118
|
-
{ status: { $eq: 'active' } },
|
|
119
|
-
{
|
|
120
|
-
$or: [{ category: { $in: ['A', 'B'] } }, { $and: [{ price: { $gt: 100 } }, { stock: { $lt: 50 } }] }],
|
|
121
|
-
},
|
|
122
|
-
],
|
|
123
|
-
};
|
|
124
|
-
expect(translator.translate(filter)).toEqual({
|
|
125
|
-
$and: [
|
|
126
|
-
{ status: { $eq: 'active' } },
|
|
127
|
-
{
|
|
128
|
-
$or: [{ category: { $in: ['A', 'B'] } }, { $and: [{ price: { $gt: 100 } }, { stock: { $lt: 50 } }] }],
|
|
129
|
-
},
|
|
130
|
-
],
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('handles nested arrays in logical operators', () => {
|
|
135
|
-
expect(
|
|
136
|
-
translator.translate({
|
|
137
|
-
$and: [{ field1: { $in: ['a', 'b'] } }, { field2: { $in: ['c', 'd'] } }],
|
|
138
|
-
}),
|
|
139
|
-
).toEqual({
|
|
140
|
-
$and: [
|
|
141
|
-
{ field1: { $in: ['a', 'b'] } },
|
|
142
|
-
{
|
|
143
|
-
field2: { $in: ['c', 'd'] },
|
|
144
|
-
},
|
|
145
|
-
],
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('handles complex nested conditions', () => {
|
|
150
|
-
const filter: ChromaVectorFilter = {
|
|
151
|
-
$or: [
|
|
152
|
-
{ age: { $gt: 25 } },
|
|
153
|
-
{
|
|
154
|
-
status: { $eq: 'active' },
|
|
155
|
-
theme: 'dark',
|
|
156
|
-
},
|
|
157
|
-
],
|
|
158
|
-
};
|
|
159
|
-
expect(translator.translate(filter)).toEqual({
|
|
160
|
-
$or: [
|
|
161
|
-
{ age: { $gt: 25 } },
|
|
162
|
-
{
|
|
163
|
-
$and: [{ status: { $eq: 'active' } }, { theme: 'dark' }],
|
|
164
|
-
},
|
|
165
|
-
],
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// Nested Objects and Fields
|
|
171
|
-
describe('nested objects and fields', () => {
|
|
172
|
-
it('flattens nested objects to dot notation', () => {
|
|
173
|
-
const filter = {
|
|
174
|
-
user: {
|
|
175
|
-
profile: {
|
|
176
|
-
age: { $gt: 25 },
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
expect(translator.translate(filter)).toEqual({ 'user.profile.age': { $gt: 25 } });
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('preserves empty objects as exact match conditions', () => {
|
|
184
|
-
const filter: ChromaVectorFilter = {
|
|
185
|
-
metadata: {},
|
|
186
|
-
'user.profile': {},
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
expect(translator.translate(filter)).toEqual({
|
|
190
|
-
$and: [{ metadata: {} }, { 'user.profile': {} }],
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('handles empty objects in logical operators', () => {
|
|
195
|
-
const filter: ChromaVectorFilter = {
|
|
196
|
-
$or: [{}, { status: 'active' }],
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
expect(translator.translate(filter)).toEqual({
|
|
200
|
-
$or: [{}, { status: 'active' }],
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('preserves empty objects in nested structures', () => {
|
|
205
|
-
const filter = {
|
|
206
|
-
user: {
|
|
207
|
-
profile: {
|
|
208
|
-
settings: {},
|
|
209
|
-
},
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
expect(translator.translate(filter)).toEqual({
|
|
214
|
-
'user.profile.settings': {},
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('handles empty objects in comparison operators', () => {
|
|
219
|
-
const filter: ChromaVectorFilter = {
|
|
220
|
-
metadata: { $eq: {} },
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
expect(translator.translate(filter)).toEqual({
|
|
224
|
-
metadata: { $eq: {} },
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
it('handles empty objects in array operators', () => {
|
|
229
|
-
const filter: ChromaVectorFilter = {
|
|
230
|
-
tags: { $in: [{}] },
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
expect(translator.translate(filter)).toEqual({
|
|
234
|
-
tags: { $in: [{}] },
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it('handles multiple empty nested fields', () => {
|
|
239
|
-
const filter = {
|
|
240
|
-
metadata: {},
|
|
241
|
-
settings: {},
|
|
242
|
-
config: { nested: {} },
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
expect(translator.translate(filter)).toEqual({
|
|
246
|
-
$and: [{ metadata: {} }, { settings: {} }, { 'config.nested': {} }],
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
// Operator Validation
|
|
252
|
-
describe('operator validation', () => {
|
|
253
|
-
describe('logical operator validation', () => {
|
|
254
|
-
it('allows $and and $or at root level', () => {
|
|
255
|
-
const validFilters = [
|
|
256
|
-
{
|
|
257
|
-
$and: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
258
|
-
},
|
|
259
|
-
{
|
|
260
|
-
$or: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
261
|
-
},
|
|
262
|
-
{
|
|
263
|
-
$and: [{ field1: 'value1' }],
|
|
264
|
-
$or: [{ field2: 'value2' }],
|
|
265
|
-
},
|
|
266
|
-
];
|
|
267
|
-
|
|
268
|
-
validFilters.forEach(filter => {
|
|
269
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('allows nested $and and $or within other logical operators', () => {
|
|
274
|
-
const validFilters = [
|
|
275
|
-
{
|
|
276
|
-
$and: [
|
|
277
|
-
{ field1: 'value1' },
|
|
278
|
-
{
|
|
279
|
-
$or: [{ field2: 'value2' }, { field3: 'value3' }],
|
|
280
|
-
},
|
|
281
|
-
],
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
$or: [
|
|
285
|
-
{ field1: 'value1' },
|
|
286
|
-
{
|
|
287
|
-
$and: [{ field2: 'value2' }, { field3: 'value3' }],
|
|
288
|
-
},
|
|
289
|
-
],
|
|
290
|
-
},
|
|
291
|
-
];
|
|
292
|
-
|
|
293
|
-
validFilters.forEach(filter => {
|
|
294
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
it('throws error for logical operators in field-level conditions', () => {
|
|
299
|
-
const invalidFilters = [
|
|
300
|
-
{
|
|
301
|
-
field: {
|
|
302
|
-
$and: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
303
|
-
},
|
|
304
|
-
},
|
|
305
|
-
{
|
|
306
|
-
field: {
|
|
307
|
-
$or: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
nested: {
|
|
312
|
-
field: {
|
|
313
|
-
$and: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
314
|
-
},
|
|
315
|
-
},
|
|
316
|
-
},
|
|
317
|
-
];
|
|
318
|
-
|
|
319
|
-
invalidFilters.forEach(filter => {
|
|
320
|
-
expect(() => translator.translate(filter)).toThrow(/cannot be used at field level/);
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
it('throws error for direct operators in logical operator arrays', () => {
|
|
325
|
-
const invalidFilters = [
|
|
326
|
-
{
|
|
327
|
-
$and: [{ $eq: 'value' }, { $gt: 100 }],
|
|
328
|
-
},
|
|
329
|
-
{
|
|
330
|
-
$or: [{ $in: ['value1', 'value2'] }],
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
$and: [{ field1: 'value1' }, { $or: [{ $eq: 'value2' }] }],
|
|
334
|
-
},
|
|
335
|
-
];
|
|
336
|
-
|
|
337
|
-
invalidFilters.forEach(filter => {
|
|
338
|
-
expect(() => translator.translate(filter)).toThrow(/must contain field conditions/);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('throws error for unsupported logical operators', () => {
|
|
343
|
-
const invalidFilters: any = [
|
|
344
|
-
{
|
|
345
|
-
$not: { field: 'value' },
|
|
346
|
-
},
|
|
347
|
-
{
|
|
348
|
-
$nor: [{ field: 'value' }],
|
|
349
|
-
},
|
|
350
|
-
{
|
|
351
|
-
$and: [{ field1: 'value1' }, { $nor: [{ field2: 'value2' }] }],
|
|
352
|
-
},
|
|
353
|
-
{
|
|
354
|
-
field: { $not: { $eq: 'value' } },
|
|
355
|
-
},
|
|
356
|
-
];
|
|
357
|
-
|
|
358
|
-
// @ts-expect-error
|
|
359
|
-
invalidFilters.forEach(filter => {
|
|
360
|
-
expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it('ensure all operator filters are supported', () => {
|
|
366
|
-
const supportedFilters = [
|
|
367
|
-
{ field: { $eq: 'value' } },
|
|
368
|
-
{ field: { $ne: 'value' } },
|
|
369
|
-
{ field: { $gt: 'value' } },
|
|
370
|
-
{ field: { $gte: 'value' } },
|
|
371
|
-
{ field: { $lt: 'value' } },
|
|
372
|
-
{ field: { $lte: 'value' } },
|
|
373
|
-
{ field: { $in: ['value'] } },
|
|
374
|
-
{ $and: [{ field: { $eq: 'value' } }] },
|
|
375
|
-
{ $or: [{ field: { $eq: 'value' } }] },
|
|
376
|
-
];
|
|
377
|
-
supportedFilters.forEach(filter => {
|
|
378
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
it('throws error for unsupported operators', () => {
|
|
383
|
-
const unsupportedFilters: any = [
|
|
384
|
-
{ field: { $regex: 'pattern' } },
|
|
385
|
-
{ field: { $contains: 'value' } },
|
|
386
|
-
{ field: { $exists: true } },
|
|
387
|
-
{ field: { $elemMatch: { $gt: 5 } } },
|
|
388
|
-
{ field: { $nor: [{ $eq: 'value' }] } },
|
|
389
|
-
{ field: { $not: [{ $eq: 'value' }] } },
|
|
390
|
-
{ field: { $regex: 'pattern', $options: 'i' } },
|
|
391
|
-
{ field: { $all: [{ $eq: 'value' }] } },
|
|
392
|
-
];
|
|
393
|
-
|
|
394
|
-
// @ts-expect-error
|
|
395
|
-
unsupportedFilters.forEach(filter => {
|
|
396
|
-
expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
it('throws error for regex operators', () => {
|
|
401
|
-
const filter = { field: /pattern/i };
|
|
402
|
-
expect(() => translator.translate(filter)).toThrow();
|
|
403
|
-
});
|
|
404
|
-
it('throws error for non-logical operators at top level', () => {
|
|
405
|
-
const invalidFilters: any = [{ $gt: 100 }, { $in: ['value1', 'value2'] }, { $eq: true }];
|
|
406
|
-
|
|
407
|
-
// @ts-expect-error
|
|
408
|
-
invalidFilters.forEach(filter => {
|
|
409
|
-
expect(() => translator.translate(filter)).toThrow(/Invalid top-level operator/);
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
it('allows logical operators at top level', () => {
|
|
414
|
-
const validFilters = [{ $and: [{ field: 'value' }] }, { $or: [{ field: 'value' }] }];
|
|
415
|
-
|
|
416
|
-
validFilters.forEach(filter => {
|
|
417
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
});
|
|
421
|
-
});
|
package/src/vector/filter.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
-
import type {
|
|
3
|
-
VectorFilter,
|
|
4
|
-
OperatorSupport,
|
|
5
|
-
QueryOperator,
|
|
6
|
-
OperatorValueMap,
|
|
7
|
-
LogicalOperatorValueMap,
|
|
8
|
-
BlacklistedRootOperators,
|
|
9
|
-
} from '@mastra/core/vector/filter';
|
|
10
|
-
|
|
11
|
-
type ChromaOperatorValueMap = Omit<OperatorValueMap, '$exists' | '$elemMatch' | '$regex' | '$options'>;
|
|
12
|
-
|
|
13
|
-
type ChromaLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor' | '$not'>;
|
|
14
|
-
|
|
15
|
-
type ChromaBlacklisted = BlacklistedRootOperators | '$nor' | '$not';
|
|
16
|
-
|
|
17
|
-
export type ChromaVectorFilter = VectorFilter<
|
|
18
|
-
keyof ChromaOperatorValueMap,
|
|
19
|
-
ChromaOperatorValueMap,
|
|
20
|
-
ChromaLogicalOperatorValueMap,
|
|
21
|
-
ChromaBlacklisted
|
|
22
|
-
>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Translator for Chroma filter queries.
|
|
26
|
-
* Maintains MongoDB-compatible syntax while ensuring proper validation
|
|
27
|
-
* and normalization of values.
|
|
28
|
-
*/
|
|
29
|
-
export class ChromaFilterTranslator extends BaseFilterTranslator<ChromaVectorFilter> {
|
|
30
|
-
protected override getSupportedOperators(): OperatorSupport {
|
|
31
|
-
return {
|
|
32
|
-
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
33
|
-
logical: ['$and', '$or'],
|
|
34
|
-
array: ['$in', '$nin'],
|
|
35
|
-
element: [],
|
|
36
|
-
regex: [],
|
|
37
|
-
custom: [],
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
translate(filter?: ChromaVectorFilter): ChromaVectorFilter {
|
|
42
|
-
if (this.isEmpty(filter)) return filter;
|
|
43
|
-
this.validateFilter(filter);
|
|
44
|
-
|
|
45
|
-
return this.translateNode(filter);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private translateNode(node: ChromaVectorFilter, currentPath: string = ''): any {
|
|
49
|
-
// Handle primitive values and arrays
|
|
50
|
-
if (this.isRegex(node)) {
|
|
51
|
-
throw new Error('Regex is supported in Chroma via the `documentFilter` argument');
|
|
52
|
-
}
|
|
53
|
-
if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
|
|
54
|
-
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
55
|
-
|
|
56
|
-
const entries = Object.entries(node as Record<string, any>);
|
|
57
|
-
const firstEntry = entries[0];
|
|
58
|
-
// Handle single operator case
|
|
59
|
-
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
60
|
-
const [operator, value] = firstEntry;
|
|
61
|
-
const translated = this.translateOperator(operator, value);
|
|
62
|
-
if (this.isLogicalOperator(operator) && Array.isArray(translated) && translated.length === 1) {
|
|
63
|
-
return translated[0];
|
|
64
|
-
}
|
|
65
|
-
return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Process each entry
|
|
69
|
-
const result: Record<string, any> = {};
|
|
70
|
-
const multiOperatorConditions: any[] = [];
|
|
71
|
-
|
|
72
|
-
for (const [key, value] of entries) {
|
|
73
|
-
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
74
|
-
|
|
75
|
-
if (this.isOperator(key)) {
|
|
76
|
-
result[key] = this.translateOperator(key, value);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
81
|
-
// Check for multiple operators on same field
|
|
82
|
-
const valueEntries = Object.entries(value);
|
|
83
|
-
if (valueEntries.every(([op]) => this.isOperator(op)) && valueEntries.length > 1) {
|
|
84
|
-
valueEntries.forEach(([op, opValue]) => {
|
|
85
|
-
multiOperatorConditions.push({
|
|
86
|
-
[newPath]: { [op]: this.normalizeComparisonValue(opValue) },
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Check if the nested object contains operators
|
|
93
|
-
if (Object.keys(value).length === 0) {
|
|
94
|
-
result[newPath] = this.translateNode(value);
|
|
95
|
-
} else {
|
|
96
|
-
const hasOperators = Object.keys(value).some(k => this.isOperator(k));
|
|
97
|
-
if (hasOperators) {
|
|
98
|
-
// For objects with operators, normalize each operator value
|
|
99
|
-
const normalizedValue: Record<string, any> = {};
|
|
100
|
-
for (const [op, opValue] of Object.entries(value)) {
|
|
101
|
-
normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
|
|
102
|
-
}
|
|
103
|
-
result[newPath] = normalizedValue;
|
|
104
|
-
} else {
|
|
105
|
-
// For objects without operators, flatten them
|
|
106
|
-
Object.assign(result, this.translateNode(value, newPath));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
} else {
|
|
110
|
-
result[newPath] = this.translateNode(value);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// If we have multiple operators, return them combined with $and
|
|
115
|
-
if (multiOperatorConditions.length > 0) {
|
|
116
|
-
return { $and: multiOperatorConditions };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Wrap in $and if there are multiple top-level fields
|
|
120
|
-
if (Object.keys(result).length > 1 && !currentPath) {
|
|
121
|
-
return {
|
|
122
|
-
$and: Object.entries(result).map(([key, value]) => ({ [key]: value })),
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return result;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private translateOperator(operator: QueryOperator, value: any): any {
|
|
130
|
-
// Handle logical operators
|
|
131
|
-
if (this.isLogicalOperator(operator)) {
|
|
132
|
-
return Array.isArray(value) ? value.map(item => this.translateNode(item)) : this.translateNode(value);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Handle comparison and element operators
|
|
136
|
-
return this.normalizeComparisonValue(value);
|
|
137
|
-
}
|
|
138
|
-
}
|