@langchain/core 0.3.26 → 0.3.27

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.
@@ -51,7 +51,7 @@ class BasicTranslator extends BaseTranslator {
51
51
  if (func in ir_js_1.Comparators) {
52
52
  if (this.allowedComparators.length > 0 &&
53
53
  this.allowedComparators.indexOf(func) === -1) {
54
- throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
54
+ throw new Error(`Comparator ${func} not allowed. Allowed comparators: ${this.allowedComparators.join(", ")}`);
55
55
  }
56
56
  }
57
57
  else if (func in ir_js_1.Operators) {
@@ -47,7 +47,7 @@ export class BasicTranslator extends BaseTranslator {
47
47
  if (func in Comparators) {
48
48
  if (this.allowedComparators.length > 0 &&
49
49
  this.allowedComparators.indexOf(func) === -1) {
50
- throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
50
+ throw new Error(`Comparator ${func} not allowed. Allowed comparators: ${this.allowedComparators.join(", ")}`);
51
51
  }
52
52
  }
53
53
  else if (func in Operators) {
@@ -41,6 +41,41 @@ class FunctionalTranslator extends base_js_1.BaseTranslator {
41
41
  formatFunction() {
42
42
  throw new Error("Not implemented");
43
43
  }
44
+ /**
45
+ * Returns the allowed comparators for a given data type.
46
+ * @param input The input value to get the allowed comparators for.
47
+ * @returns An array of allowed comparators for the input data type.
48
+ */
49
+ getAllowedComparatorsForType(inputType) {
50
+ switch (inputType) {
51
+ case "string": {
52
+ return [
53
+ ir_js_1.Comparators.eq,
54
+ ir_js_1.Comparators.ne,
55
+ ir_js_1.Comparators.gt,
56
+ ir_js_1.Comparators.gte,
57
+ ir_js_1.Comparators.lt,
58
+ ir_js_1.Comparators.lte,
59
+ ];
60
+ }
61
+ case "number": {
62
+ return [
63
+ ir_js_1.Comparators.eq,
64
+ ir_js_1.Comparators.ne,
65
+ ir_js_1.Comparators.gt,
66
+ ir_js_1.Comparators.gte,
67
+ ir_js_1.Comparators.lt,
68
+ ir_js_1.Comparators.lte,
69
+ ];
70
+ }
71
+ case "boolean": {
72
+ return [ir_js_1.Comparators.eq, ir_js_1.Comparators.ne];
73
+ }
74
+ default: {
75
+ throw new Error(`Unsupported data type: ${inputType}`);
76
+ }
77
+ }
78
+ }
44
79
  /**
45
80
  * Returns a function that performs a comparison based on the provided
46
81
  * comparator.
@@ -130,6 +165,9 @@ class FunctionalTranslator extends base_js_1.BaseTranslator {
130
165
  const { comparator, attribute, value } = comparison;
131
166
  const undefinedTrue = [ir_js_1.Comparators.ne];
132
167
  if (this.allowedComparators.includes(comparator)) {
168
+ if (!this.getAllowedComparatorsForType(typeof value).includes(comparator)) {
169
+ throw new Error(`'${comparator}' comparator not allowed to be used with ${typeof value}`);
170
+ }
133
171
  const comparatorFunction = this.getComparatorFunction(comparator);
134
172
  return (document) => {
135
173
  const documentValue = document.metadata[attribute];
@@ -7,8 +7,8 @@ import { BaseTranslator } from "./base.js";
7
7
  * the result of a comparison operation.
8
8
  */
9
9
  type ValueType = {
10
- eq: string | number;
11
- ne: string | number;
10
+ eq: string | number | boolean;
11
+ ne: string | number | boolean;
12
12
  lt: string | number;
13
13
  lte: string | number;
14
14
  gt: string | number;
@@ -41,6 +41,12 @@ export declare class FunctionalTranslator extends BaseTranslator {
41
41
  allowedOperators: Operator[];
42
42
  allowedComparators: Comparator[];
43
43
  formatFunction(): string;
44
+ /**
45
+ * Returns the allowed comparators for a given data type.
46
+ * @param input The input value to get the allowed comparators for.
47
+ * @returns An array of allowed comparators for the input data type.
48
+ */
49
+ getAllowedComparatorsForType(inputType: string): Comparator[];
44
50
  /**
45
51
  * Returns a function that performs a comparison based on the provided
46
52
  * comparator.
@@ -68,7 +74,7 @@ export declare class FunctionalTranslator extends BaseTranslator {
68
74
  * @param comparison The comparison part of a structured query.
69
75
  * @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison.
70
76
  */
71
- visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
77
+ visitComparison(comparison: Comparison<string | number | boolean>): this["VisitComparisonOutput"];
72
78
  /**
73
79
  * Visits a structured query and translates it into a functional filter.
74
80
  * @param query The structured query to translate.
@@ -38,6 +38,41 @@ export class FunctionalTranslator extends BaseTranslator {
38
38
  formatFunction() {
39
39
  throw new Error("Not implemented");
40
40
  }
41
+ /**
42
+ * Returns the allowed comparators for a given data type.
43
+ * @param input The input value to get the allowed comparators for.
44
+ * @returns An array of allowed comparators for the input data type.
45
+ */
46
+ getAllowedComparatorsForType(inputType) {
47
+ switch (inputType) {
48
+ case "string": {
49
+ return [
50
+ Comparators.eq,
51
+ Comparators.ne,
52
+ Comparators.gt,
53
+ Comparators.gte,
54
+ Comparators.lt,
55
+ Comparators.lte,
56
+ ];
57
+ }
58
+ case "number": {
59
+ return [
60
+ Comparators.eq,
61
+ Comparators.ne,
62
+ Comparators.gt,
63
+ Comparators.gte,
64
+ Comparators.lt,
65
+ Comparators.lte,
66
+ ];
67
+ }
68
+ case "boolean": {
69
+ return [Comparators.eq, Comparators.ne];
70
+ }
71
+ default: {
72
+ throw new Error(`Unsupported data type: ${inputType}`);
73
+ }
74
+ }
75
+ }
41
76
  /**
42
77
  * Returns a function that performs a comparison based on the provided
43
78
  * comparator.
@@ -127,6 +162,9 @@ export class FunctionalTranslator extends BaseTranslator {
127
162
  const { comparator, attribute, value } = comparison;
128
163
  const undefinedTrue = [Comparators.ne];
129
164
  if (this.allowedComparators.includes(comparator)) {
165
+ if (!this.getAllowedComparatorsForType(typeof value).includes(comparator)) {
166
+ throw new Error(`'${comparator}' comparator not allowed to be used with ${typeof value}`);
167
+ }
130
168
  const comparatorFunction = this.getComparatorFunction(comparator);
131
169
  return (document) => {
132
170
  const documentValue = document.metadata[attribute];
@@ -66,7 +66,7 @@ export type VisitorOperationResult = {
66
66
  */
67
67
  export type VisitorComparisonResult = {
68
68
  [attr: string]: {
69
- [comparator: string]: string | number;
69
+ [comparator: string]: string | number | boolean;
70
70
  };
71
71
  };
72
72
  /**
@@ -109,12 +109,12 @@ export declare abstract class FilterDirective extends Expression {
109
109
  * Class representing a comparison filter directive. It extends the
110
110
  * FilterDirective class.
111
111
  */
112
- export declare class Comparison extends FilterDirective {
112
+ export declare class Comparison<ValueTypes = string | number> extends FilterDirective {
113
113
  comparator: Comparator;
114
114
  attribute: string;
115
- value: string | number;
115
+ value: ValueTypes;
116
116
  exprName: "Comparison";
117
- constructor(comparator: Comparator, attribute: string, value: string | number);
117
+ constructor(comparator: Comparator, attribute: string, value: ValueTypes);
118
118
  }
119
119
  /**
120
120
  * Class representing an operation filter directive. It extends the
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.castValue = exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
3
+ exports.castValue = exports.isBoolean = exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
4
4
  /**
5
5
  * Checks if the provided argument is an object and not an array.
6
6
  */
@@ -69,6 +69,13 @@ function isString(value) {
69
69
  (Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
70
70
  }
71
71
  exports.isString = isString;
72
+ /**
73
+ * Checks if the provided value is a boolean.
74
+ */
75
+ function isBoolean(value) {
76
+ return typeof value === "boolean";
77
+ }
78
+ exports.isBoolean = isBoolean;
72
79
  /**
73
80
  * Casts a value that might be string or number to actual string or number.
74
81
  * Since LLM might return back an integer/float as a string, we need to cast
@@ -86,6 +93,9 @@ function castValue(input) {
86
93
  else if (isFloat(input)) {
87
94
  value = parseFloat(input);
88
95
  }
96
+ else if (isBoolean(input)) {
97
+ value = Boolean(input);
98
+ }
89
99
  else {
90
100
  throw new Error("Unsupported value type");
91
101
  }
@@ -20,10 +20,14 @@ export declare function isFloat(value: unknown): boolean;
20
20
  * number.
21
21
  */
22
22
  export declare function isString(value: unknown): boolean;
23
+ /**
24
+ * Checks if the provided value is a boolean.
25
+ */
26
+ export declare function isBoolean(value: unknown): boolean;
23
27
  /**
24
28
  * Casts a value that might be string or number to actual string or number.
25
29
  * Since LLM might return back an integer/float as a string, we need to cast
26
30
  * it back to a number, as many vector databases can't handle number as string
27
31
  * values as a comparator.
28
32
  */
29
- export declare function castValue(input: unknown): string | number;
33
+ export declare function castValue(input: unknown): string | number | boolean;
@@ -61,6 +61,12 @@ export function isString(value) {
61
61
  return (typeof value === "string" &&
62
62
  (Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
63
63
  }
64
+ /**
65
+ * Checks if the provided value is a boolean.
66
+ */
67
+ export function isBoolean(value) {
68
+ return typeof value === "boolean";
69
+ }
64
70
  /**
65
71
  * Casts a value that might be string or number to actual string or number.
66
72
  * Since LLM might return back an integer/float as a string, we need to cast
@@ -78,6 +84,9 @@ export function castValue(input) {
78
84
  else if (isFloat(input)) {
79
85
  value = parseFloat(input);
80
86
  }
87
+ else if (isBoolean(input)) {
88
+ value = Boolean(input);
89
+ }
81
90
  else {
82
91
  throw new Error("Unsupported value type");
83
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.26",
3
+ "version": "0.3.27",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {