@mastra/chroma 0.0.0-update-stores-peerDeps-20250723031338 → 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.
@@ -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
- }