@mastra/chroma 0.0.0-update-scorers-api-20250801170445 → 0.0.0-usechat-duplicate-20251016110554

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/eslint.config.js DELETED
@@ -1,6 +0,0 @@
1
- import { createConfig } from '@internal/lint/eslint';
2
-
3
- const config = await createConfig();
4
-
5
- /** @type {import("eslint").Linter.Config[]} */
6
- export default [...config];
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './vector/index';
2
- export { CHROMA_PROMPT } from './vector/prompt';
@@ -1,418 +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
- invalidFilters.forEach(filter => {
359
- expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
360
- });
361
- });
362
- });
363
-
364
- it('ensure all operator filters are supported', () => {
365
- const supportedFilters = [
366
- { field: { $eq: 'value' } },
367
- { field: { $ne: 'value' } },
368
- { field: { $gt: 'value' } },
369
- { field: { $gte: 'value' } },
370
- { field: { $lt: 'value' } },
371
- { field: { $lte: 'value' } },
372
- { field: { $in: ['value'] } },
373
- { $and: [{ field: { $eq: 'value' } }] },
374
- { $or: [{ field: { $eq: 'value' } }] },
375
- ];
376
- supportedFilters.forEach(filter => {
377
- expect(() => translator.translate(filter)).not.toThrow();
378
- });
379
- });
380
-
381
- it('throws error for unsupported operators', () => {
382
- const unsupportedFilters: any = [
383
- { field: { $regex: 'pattern' } },
384
- { field: { $contains: 'value' } },
385
- { field: { $exists: true } },
386
- { field: { $elemMatch: { $gt: 5 } } },
387
- { field: { $nor: [{ $eq: 'value' }] } },
388
- { field: { $not: [{ $eq: 'value' }] } },
389
- { field: { $regex: 'pattern', $options: 'i' } },
390
- { field: { $all: [{ $eq: 'value' }] } },
391
- ];
392
-
393
- unsupportedFilters.forEach(filter => {
394
- expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
395
- });
396
- });
397
-
398
- it('throws error for regex operators', () => {
399
- const filter = { field: /pattern/i };
400
- expect(() => translator.translate(filter)).toThrow();
401
- });
402
- it('throws error for non-logical operators at top level', () => {
403
- const invalidFilters: any = [{ $gt: 100 }, { $in: ['value1', 'value2'] }, { $eq: true }];
404
-
405
- invalidFilters.forEach(filter => {
406
- expect(() => translator.translate(filter)).toThrow(/Invalid top-level operator/);
407
- });
408
- });
409
-
410
- it('allows logical operators at top level', () => {
411
- const validFilters = [{ $and: [{ field: 'value' }] }, { $or: [{ field: 'value' }] }];
412
-
413
- validFilters.forEach(filter => {
414
- expect(() => translator.translate(filter)).not.toThrow();
415
- });
416
- });
417
- });
418
- });
@@ -1,146 +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
- type ChromaDocumentOperatorValueMap = ChromaOperatorValueMap;
25
-
26
- type ChromaDocumentBlacklisted = Exclude<ChromaBlacklisted, '$contains'>;
27
-
28
- export type ChromaVectorDocumentFilter = VectorFilter<
29
- keyof ChromaDocumentOperatorValueMap,
30
- ChromaDocumentOperatorValueMap,
31
- ChromaLogicalOperatorValueMap,
32
- ChromaDocumentBlacklisted
33
- >;
34
-
35
- /**
36
- * Translator for Chroma filter queries.
37
- * Maintains MongoDB-compatible syntax while ensuring proper validation
38
- * and normalization of values.
39
- */
40
- export class ChromaFilterTranslator extends BaseFilterTranslator<ChromaVectorFilter> {
41
- protected override getSupportedOperators(): OperatorSupport {
42
- return {
43
- ...BaseFilterTranslator.DEFAULT_OPERATORS,
44
- logical: ['$and', '$or'],
45
- array: ['$in', '$nin'],
46
- element: [],
47
- regex: [],
48
- custom: [],
49
- };
50
- }
51
-
52
- translate(filter?: ChromaVectorFilter): ChromaVectorFilter {
53
- if (this.isEmpty(filter)) return filter;
54
- this.validateFilter(filter);
55
-
56
- return this.translateNode(filter);
57
- }
58
-
59
- private translateNode(node: ChromaVectorFilter, currentPath: string = ''): any {
60
- // Handle primitive values and arrays
61
- if (this.isRegex(node)) {
62
- throw new Error('Regex is not supported in Chroma');
63
- }
64
- if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
65
- if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
66
-
67
- const entries = Object.entries(node as Record<string, any>);
68
- const firstEntry = entries[0];
69
- // Handle single operator case
70
- if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
71
- const [operator, value] = firstEntry;
72
- const translated = this.translateOperator(operator, value);
73
- return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
74
- }
75
-
76
- // Process each entry
77
- const result: Record<string, any> = {};
78
- const multiOperatorConditions: any[] = [];
79
-
80
- for (const [key, value] of entries) {
81
- const newPath = currentPath ? `${currentPath}.${key}` : key;
82
-
83
- if (this.isOperator(key)) {
84
- result[key] = this.translateOperator(key, value);
85
- continue;
86
- }
87
-
88
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
89
- // Check for multiple operators on same field
90
- const valueEntries = Object.entries(value);
91
- if (valueEntries.every(([op]) => this.isOperator(op)) && valueEntries.length > 1) {
92
- valueEntries.forEach(([op, opValue]) => {
93
- multiOperatorConditions.push({
94
- [newPath]: { [op]: this.normalizeComparisonValue(opValue) },
95
- });
96
- });
97
- continue;
98
- }
99
-
100
- // Check if the nested object contains operators
101
- if (Object.keys(value).length === 0) {
102
- result[newPath] = this.translateNode(value);
103
- } else {
104
- const hasOperators = Object.keys(value).some(k => this.isOperator(k));
105
- if (hasOperators) {
106
- // For objects with operators, normalize each operator value
107
- const normalizedValue: Record<string, any> = {};
108
- for (const [op, opValue] of Object.entries(value)) {
109
- normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
110
- }
111
- result[newPath] = normalizedValue;
112
- } else {
113
- // For objects without operators, flatten them
114
- Object.assign(result, this.translateNode(value, newPath));
115
- }
116
- }
117
- } else {
118
- result[newPath] = this.translateNode(value);
119
- }
120
- }
121
-
122
- // If we have multiple operators, return them combined with $and
123
- if (multiOperatorConditions.length > 0) {
124
- return { $and: multiOperatorConditions };
125
- }
126
-
127
- // Wrap in $and if there are multiple top-level fields
128
- if (Object.keys(result).length > 1 && !currentPath) {
129
- return {
130
- $and: Object.entries(result).map(([key, value]) => ({ [key]: value })),
131
- };
132
- }
133
-
134
- return result;
135
- }
136
-
137
- private translateOperator(operator: QueryOperator, value: any): any {
138
- // Handle logical operators
139
- if (this.isLogicalOperator(operator)) {
140
- return Array.isArray(value) ? value.map(item => this.translateNode(item)) : this.translateNode(value);
141
- }
142
-
143
- // Handle comparison and element operators
144
- return this.normalizeComparisonValue(value);
145
- }
146
- }