@mastra/chroma 0.0.0-vector-query-tool-provider-options-20250828222356 → 0.0.0-vector-extension-schema-20250922130418

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,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
- }