langchain 0.0.76 → 0.0.78

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.
Files changed (162) hide show
  1. package/chains/query_constructor/ir.cjs +1 -0
  2. package/chains/query_constructor/ir.d.ts +1 -0
  3. package/chains/query_constructor/ir.js +1 -0
  4. package/chains/query_constructor.cjs +1 -0
  5. package/chains/query_constructor.d.ts +1 -0
  6. package/chains/query_constructor.js +1 -0
  7. package/dist/agents/agent.d.ts +5 -4
  8. package/dist/agents/agent_toolkits/sql/sql.cjs +2 -2
  9. package/dist/agents/agent_toolkits/sql/sql.d.ts +1 -1
  10. package/dist/agents/agent_toolkits/sql/sql.js +2 -2
  11. package/dist/agents/chat/index.cjs +3 -3
  12. package/dist/agents/chat/index.d.ts +1 -0
  13. package/dist/agents/chat/index.js +3 -3
  14. package/dist/agents/chat_convo/index.cjs +30 -14
  15. package/dist/agents/chat_convo/index.d.ts +5 -1
  16. package/dist/agents/chat_convo/index.js +31 -15
  17. package/dist/agents/chat_convo/outputParser.cjs +79 -7
  18. package/dist/agents/chat_convo/outputParser.d.ts +25 -13
  19. package/dist/agents/chat_convo/outputParser.js +77 -6
  20. package/dist/agents/chat_convo/prompt.cjs +11 -8
  21. package/dist/agents/chat_convo/prompt.d.ts +2 -2
  22. package/dist/agents/chat_convo/prompt.js +11 -8
  23. package/dist/agents/executor.d.ts +2 -3
  24. package/dist/agents/index.cjs +6 -1
  25. package/dist/agents/index.d.ts +2 -0
  26. package/dist/agents/index.js +2 -0
  27. package/dist/agents/initialize.cjs +19 -13
  28. package/dist/agents/initialize.d.ts +12 -2
  29. package/dist/agents/initialize.js +16 -10
  30. package/dist/agents/mrkl/index.cjs +3 -3
  31. package/dist/agents/mrkl/index.d.ts +1 -0
  32. package/dist/agents/mrkl/index.js +3 -3
  33. package/dist/agents/mrkl/outputParser.cjs +2 -2
  34. package/dist/agents/mrkl/outputParser.js +2 -2
  35. package/dist/agents/structured_chat/index.cjs +106 -0
  36. package/dist/agents/structured_chat/index.d.ts +44 -0
  37. package/dist/agents/structured_chat/index.js +102 -0
  38. package/dist/agents/structured_chat/outputParser.cjs +92 -0
  39. package/dist/agents/structured_chat/outputParser.d.ts +24 -0
  40. package/dist/agents/structured_chat/outputParser.js +87 -0
  41. package/dist/agents/structured_chat/prompt.cjs +62 -0
  42. package/dist/agents/structured_chat/prompt.d.ts +4 -0
  43. package/dist/agents/structured_chat/prompt.js +59 -0
  44. package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
  45. package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
  46. package/dist/callbacks/handlers/tracer_langchain.js +12 -4
  47. package/dist/callbacks/manager.cjs +6 -2
  48. package/dist/callbacks/manager.js +6 -2
  49. package/dist/chains/query_constructor/index.cjs +105 -0
  50. package/dist/chains/query_constructor/index.d.ts +37 -0
  51. package/dist/chains/query_constructor/index.js +95 -0
  52. package/dist/chains/query_constructor/ir.cjs +116 -0
  53. package/dist/chains/query_constructor/ir.d.ts +60 -0
  54. package/dist/chains/query_constructor/ir.js +107 -0
  55. package/dist/chains/query_constructor/parser.cjs +103 -0
  56. package/dist/chains/query_constructor/parser.d.ts +12 -0
  57. package/dist/chains/query_constructor/parser.js +99 -0
  58. package/dist/chains/query_constructor/prompt.cjs +127 -0
  59. package/dist/chains/query_constructor/prompt.d.ts +15 -0
  60. package/dist/chains/query_constructor/prompt.js +124 -0
  61. package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
  62. package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
  63. package/dist/chains/sql_db/sql_db_chain.js +13 -0
  64. package/dist/client/langchainplus.cjs +21 -15
  65. package/dist/client/langchainplus.d.ts +4 -2
  66. package/dist/client/langchainplus.js +21 -15
  67. package/dist/llms/sagemaker_endpoint.cjs +123 -0
  68. package/dist/llms/sagemaker_endpoint.d.ts +82 -0
  69. package/dist/llms/sagemaker_endpoint.js +118 -0
  70. package/dist/memory/buffer_memory.cjs +1 -1
  71. package/dist/memory/buffer_memory.js +1 -1
  72. package/dist/memory/buffer_window_memory.cjs +1 -1
  73. package/dist/memory/buffer_window_memory.js +1 -1
  74. package/dist/output_parsers/expression.cjs +68 -0
  75. package/dist/output_parsers/expression.d.ts +25 -0
  76. package/dist/output_parsers/expression.js +49 -0
  77. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
  78. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
  79. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
  80. package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
  81. package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
  82. package/dist/output_parsers/expression_type_handlers/base.js +62 -0
  83. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
  84. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
  85. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
  86. package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
  87. package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
  88. package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
  89. package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
  90. package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
  91. package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
  92. package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
  93. package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
  94. package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
  95. package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
  96. package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
  97. package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
  98. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
  99. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
  100. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
  101. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
  102. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
  103. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
  104. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
  105. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
  106. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
  107. package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
  108. package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
  109. package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
  110. package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
  111. package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
  112. package/dist/output_parsers/expression_type_handlers/types.js +1 -0
  113. package/dist/output_parsers/index.cjs +2 -1
  114. package/dist/output_parsers/index.d.ts +1 -1
  115. package/dist/output_parsers/index.js +1 -1
  116. package/dist/output_parsers/structured.cjs +81 -23
  117. package/dist/output_parsers/structured.d.ts +18 -0
  118. package/dist/output_parsers/structured.js +79 -22
  119. package/dist/retrievers/self_query/index.cjs +79 -0
  120. package/dist/retrievers/self_query/index.d.ts +33 -0
  121. package/dist/retrievers/self_query/index.js +74 -0
  122. package/dist/retrievers/self_query/translator.cjs +72 -0
  123. package/dist/retrievers/self_query/translator.d.ts +14 -0
  124. package/dist/retrievers/self_query/translator.js +67 -0
  125. package/dist/schema/query_constructor.cjs +26 -0
  126. package/dist/schema/query_constructor.d.ts +6 -0
  127. package/dist/schema/query_constructor.js +22 -0
  128. package/dist/text_splitter.cjs +2 -1
  129. package/dist/text_splitter.js +2 -1
  130. package/dist/tools/dynamic.cjs +39 -1
  131. package/dist/tools/dynamic.d.ts +18 -3
  132. package/dist/tools/dynamic.js +38 -1
  133. package/dist/tools/index.cjs +2 -1
  134. package/dist/tools/index.d.ts +1 -1
  135. package/dist/tools/index.js +1 -1
  136. package/dist/tools/json.cjs +3 -1
  137. package/dist/tools/json.js +3 -1
  138. package/dist/tools/sql.cjs +17 -11
  139. package/dist/tools/sql.d.ts +7 -1
  140. package/dist/tools/sql.js +17 -11
  141. package/dist/util/event-source-parse.cjs +31 -5
  142. package/dist/util/event-source-parse.d.ts +3 -3
  143. package/dist/util/event-source-parse.js +31 -5
  144. package/dist/vectorstores/faiss.cjs +245 -0
  145. package/dist/vectorstores/faiss.d.ts +39 -0
  146. package/dist/vectorstores/faiss.js +218 -0
  147. package/llms/sagemaker_endpoint.cjs +1 -0
  148. package/llms/sagemaker_endpoint.d.ts +1 -0
  149. package/llms/sagemaker_endpoint.js +1 -0
  150. package/output_parsers/expression.cjs +1 -0
  151. package/output_parsers/expression.d.ts +1 -0
  152. package/output_parsers/expression.js +1 -0
  153. package/package.json +79 -3
  154. package/retrievers/self_query.cjs +1 -0
  155. package/retrievers/self_query.d.ts +1 -0
  156. package/retrievers/self_query.js +1 -0
  157. package/schema/query_constructor.cjs +1 -0
  158. package/schema/query_constructor.d.ts +1 -0
  159. package/schema/query_constructor.js +1 -0
  160. package/vectorstores/faiss.cjs +1 -0
  161. package/vectorstores/faiss.d.ts +1 -0
  162. package/vectorstores/faiss.js +1 -0
@@ -0,0 +1,95 @@
1
+ import { z } from "zod";
2
+ import { QueryTransformer } from "./parser.js";
3
+ import { Comparators, Operators, StructuredQuery, } from "./ir.js";
4
+ import { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, } from "./prompt.js";
5
+ import { interpolateFString } from "../../prompts/template.js";
6
+ import { LLMChain } from "../llm_chain.js";
7
+ import { FewShotPromptTemplate } from "../../prompts/few_shot.js";
8
+ import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
9
+ export { QueryTransformer };
10
+ export { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, };
11
+ const queryInputSchema = /* #__PURE__ */ z.object({
12
+ query: /* #__PURE__ */ z
13
+ .string()
14
+ .describe("text string to compare to document contents"),
15
+ filter: /* #__PURE__ */ z
16
+ .string()
17
+ .optional()
18
+ .describe("logical condition statement for filtering documents"),
19
+ });
20
+ export class StructuredQueryOutputParser extends AsymmetricStructuredOutputParser {
21
+ constructor(parserFunction) {
22
+ super(queryInputSchema);
23
+ Object.defineProperty(this, "parserFunction", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: parserFunction
28
+ });
29
+ }
30
+ async outputProcessor(input) {
31
+ return this.parserFunction(input.query, input.filter);
32
+ }
33
+ static fromComponents(allowedComparators = [], allowedOperators = []) {
34
+ const queryTransformer = new QueryTransformer(allowedComparators, allowedOperators);
35
+ return new StructuredQueryOutputParser(async (query, filter) => {
36
+ let myQuery = query;
37
+ if (myQuery.length === 0) {
38
+ myQuery = " ";
39
+ }
40
+ if (filter === "NO_FILTER" || filter === undefined) {
41
+ return new StructuredQuery(query);
42
+ }
43
+ else {
44
+ const parsedFilter = await queryTransformer.parse(filter);
45
+ return new StructuredQuery(query, parsedFilter);
46
+ }
47
+ });
48
+ }
49
+ }
50
+ export function formatAttributeInfo(info) {
51
+ const infoObj = info.reduce((acc, attr) => {
52
+ acc[attr.name] = {
53
+ type: attr.type,
54
+ description: attr.description,
55
+ };
56
+ return acc;
57
+ }, {});
58
+ return JSON.stringify(infoObj, null, 2)
59
+ .replaceAll("{", "{{")
60
+ .replaceAll("}", "}}");
61
+ }
62
+ const defaultExample = DEFAULT_EXAMPLES.map((EXAMPLE) => EXAMPLE);
63
+ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowedOperators, examples = defaultExample) {
64
+ const myAllowedComparators = allowedComparators ?? Object.values(Comparators);
65
+ const myAllowedOperators = allowedOperators ?? Object.values(Operators);
66
+ const attributeJSON = formatAttributeInfo(attributeInfo);
67
+ const schema = interpolateFString(DEFAULT_SCHEMA, {
68
+ allowed_comparators: myAllowedComparators.join(" | "),
69
+ allowed_operators: myAllowedOperators.join(" | "),
70
+ });
71
+ const prefix = interpolateFString(DEFAULT_PREFIX, {
72
+ schema,
73
+ });
74
+ const suffix = interpolateFString(DEFAULT_SUFFIX, {
75
+ i: examples.length + 1,
76
+ content: documentContents,
77
+ attributes: attributeJSON,
78
+ });
79
+ const outputParser = StructuredQueryOutputParser.fromComponents(allowedComparators, allowedOperators);
80
+ return new FewShotPromptTemplate({
81
+ examples,
82
+ examplePrompt: EXAMPLE_PROMPT,
83
+ inputVariables: ["query"],
84
+ suffix,
85
+ prefix,
86
+ outputParser,
87
+ });
88
+ }
89
+ export function loadQueryContstructorChain(opts) {
90
+ const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
91
+ return new LLMChain({
92
+ llm: opts.llm,
93
+ prompt,
94
+ });
95
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructuredQuery = exports.Operation = exports.Comparison = exports.FilterDirective = exports.Expression = exports.Visitor = exports.Comparators = exports.Operators = void 0;
4
+ exports.Operators = {
5
+ and: "and",
6
+ or: "or",
7
+ not: "not",
8
+ };
9
+ exports.Comparators = {
10
+ eq: "eq",
11
+ lt: "lt",
12
+ gt: "gt",
13
+ lte: "lte",
14
+ gte: "gte",
15
+ };
16
+ class Visitor {
17
+ }
18
+ exports.Visitor = Visitor;
19
+ class Expression {
20
+ accept(visitor) {
21
+ if (this.exprName === "Operation") {
22
+ return visitor.visitOperation(this);
23
+ }
24
+ else if (this.exprName === "Comparison") {
25
+ return visitor.visitComparison(this);
26
+ }
27
+ else if (this.exprName === "StructuredQuery") {
28
+ return visitor.visitStructuredQuery(this);
29
+ }
30
+ else {
31
+ throw new Error("Unknown Expression type");
32
+ }
33
+ }
34
+ }
35
+ exports.Expression = Expression;
36
+ class FilterDirective extends Expression {
37
+ }
38
+ exports.FilterDirective = FilterDirective;
39
+ class Comparison extends FilterDirective {
40
+ constructor(comparator, attribute, value) {
41
+ super();
42
+ Object.defineProperty(this, "comparator", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: comparator
47
+ });
48
+ Object.defineProperty(this, "attribute", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: attribute
53
+ });
54
+ Object.defineProperty(this, "value", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: value
59
+ });
60
+ Object.defineProperty(this, "exprName", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: "Comparison"
65
+ });
66
+ }
67
+ }
68
+ exports.Comparison = Comparison;
69
+ class Operation extends FilterDirective {
70
+ constructor(operator, args) {
71
+ super();
72
+ Object.defineProperty(this, "operator", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: operator
77
+ });
78
+ Object.defineProperty(this, "args", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: args
83
+ });
84
+ Object.defineProperty(this, "exprName", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: "Operation"
89
+ });
90
+ }
91
+ }
92
+ exports.Operation = Operation;
93
+ class StructuredQuery extends Expression {
94
+ constructor(query, filter) {
95
+ super();
96
+ Object.defineProperty(this, "query", {
97
+ enumerable: true,
98
+ configurable: true,
99
+ writable: true,
100
+ value: query
101
+ });
102
+ Object.defineProperty(this, "filter", {
103
+ enumerable: true,
104
+ configurable: true,
105
+ writable: true,
106
+ value: filter
107
+ });
108
+ Object.defineProperty(this, "exprName", {
109
+ enumerable: true,
110
+ configurable: true,
111
+ writable: true,
112
+ value: "StructuredQuery"
113
+ });
114
+ }
115
+ }
116
+ exports.StructuredQuery = StructuredQuery;
@@ -0,0 +1,60 @@
1
+ export type AND = "and";
2
+ export type OR = "or";
3
+ export type NOT = "not";
4
+ export type Operator = AND | OR | NOT;
5
+ export type EQ = "eq";
6
+ export type LT = "lt";
7
+ export type GT = "gt";
8
+ export type LTE = "lte";
9
+ export type GTE = "gte";
10
+ export type Comparator = EQ | LT | GT | LTE | GTE;
11
+ export declare const Operators: {
12
+ [key: string]: Operator;
13
+ };
14
+ export declare const Comparators: {
15
+ [key: string]: Comparator;
16
+ };
17
+ export type VisitorResult = VisitorOperationResult | VisitorComparisonResult | VisitorStructuredQueryResult;
18
+ export type VisitorOperationResult = {
19
+ [operator: string]: VisitorResult[];
20
+ };
21
+ export type VisitorComparisonResult = {
22
+ [attr: string]: {
23
+ [comparator: string]: string | number;
24
+ };
25
+ };
26
+ export type VisitorStructuredQueryResult = {
27
+ filter?: VisitorStructuredQueryResult | VisitorComparisonResult | VisitorOperationResult;
28
+ };
29
+ export declare abstract class Visitor {
30
+ abstract allowedOperators: Operator[];
31
+ abstract allowedComparators: Comparator[];
32
+ abstract visitOperation(operation: Operation): VisitorOperationResult;
33
+ abstract visitComparison(comparison: Comparison): VisitorComparisonResult;
34
+ abstract visitStructuredQuery(structuredQuery: StructuredQuery): VisitorStructuredQueryResult;
35
+ }
36
+ export declare abstract class Expression {
37
+ abstract exprName: "Operation" | "Comparison" | "StructuredQuery";
38
+ accept(visitor: Visitor): VisitorOperationResult | VisitorComparisonResult | VisitorStructuredQueryResult;
39
+ }
40
+ export declare abstract class FilterDirective extends Expression {
41
+ }
42
+ export declare class Comparison extends FilterDirective {
43
+ comparator: Comparator;
44
+ attribute: string;
45
+ value: string | number;
46
+ exprName: "Comparison";
47
+ constructor(comparator: Comparator, attribute: string, value: string | number);
48
+ }
49
+ export declare class Operation extends FilterDirective {
50
+ operator: Operator;
51
+ args?: FilterDirective[] | undefined;
52
+ exprName: "Operation";
53
+ constructor(operator: Operator, args?: FilterDirective[] | undefined);
54
+ }
55
+ export declare class StructuredQuery extends Expression {
56
+ query: string;
57
+ filter?: FilterDirective | undefined;
58
+ exprName: "StructuredQuery";
59
+ constructor(query: string, filter?: FilterDirective | undefined);
60
+ }
@@ -0,0 +1,107 @@
1
+ export const Operators = {
2
+ and: "and",
3
+ or: "or",
4
+ not: "not",
5
+ };
6
+ export const Comparators = {
7
+ eq: "eq",
8
+ lt: "lt",
9
+ gt: "gt",
10
+ lte: "lte",
11
+ gte: "gte",
12
+ };
13
+ export class Visitor {
14
+ }
15
+ export class Expression {
16
+ accept(visitor) {
17
+ if (this.exprName === "Operation") {
18
+ return visitor.visitOperation(this);
19
+ }
20
+ else if (this.exprName === "Comparison") {
21
+ return visitor.visitComparison(this);
22
+ }
23
+ else if (this.exprName === "StructuredQuery") {
24
+ return visitor.visitStructuredQuery(this);
25
+ }
26
+ else {
27
+ throw new Error("Unknown Expression type");
28
+ }
29
+ }
30
+ }
31
+ export class FilterDirective extends Expression {
32
+ }
33
+ export class Comparison extends FilterDirective {
34
+ constructor(comparator, attribute, value) {
35
+ super();
36
+ Object.defineProperty(this, "comparator", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: comparator
41
+ });
42
+ Object.defineProperty(this, "attribute", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: attribute
47
+ });
48
+ Object.defineProperty(this, "value", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: value
53
+ });
54
+ Object.defineProperty(this, "exprName", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: "Comparison"
59
+ });
60
+ }
61
+ }
62
+ export class Operation extends FilterDirective {
63
+ constructor(operator, args) {
64
+ super();
65
+ Object.defineProperty(this, "operator", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: operator
70
+ });
71
+ Object.defineProperty(this, "args", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: args
76
+ });
77
+ Object.defineProperty(this, "exprName", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: "Operation"
82
+ });
83
+ }
84
+ }
85
+ export class StructuredQuery extends Expression {
86
+ constructor(query, filter) {
87
+ super();
88
+ Object.defineProperty(this, "query", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: query
93
+ });
94
+ Object.defineProperty(this, "filter", {
95
+ enumerable: true,
96
+ configurable: true,
97
+ writable: true,
98
+ value: filter
99
+ });
100
+ Object.defineProperty(this, "exprName", {
101
+ enumerable: true,
102
+ configurable: true,
103
+ writable: true,
104
+ value: "StructuredQuery"
105
+ });
106
+ }
107
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryTransformer = void 0;
4
+ const ir_js_1 = require("./ir.cjs");
5
+ const expression_js_1 = require("../../output_parsers/expression.cjs");
6
+ class QueryTransformer {
7
+ constructor(allowedComparators = [], allowedOperators = []) {
8
+ Object.defineProperty(this, "allowedComparators", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: allowedComparators
13
+ });
14
+ Object.defineProperty(this, "allowedOperators", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: allowedOperators
19
+ });
20
+ }
21
+ matchFunctionName(funcName) {
22
+ if (funcName in ir_js_1.Comparators) {
23
+ if (this.allowedComparators.length > 0) {
24
+ if (this.allowedComparators.includes(funcName)) {
25
+ return funcName;
26
+ }
27
+ else {
28
+ throw new Error("Received comparator not allowed");
29
+ }
30
+ }
31
+ else {
32
+ return funcName;
33
+ }
34
+ }
35
+ if (funcName in ir_js_1.Operators) {
36
+ if (this.allowedOperators.length > 0) {
37
+ if (this.allowedOperators.includes(funcName)) {
38
+ return funcName;
39
+ }
40
+ else {
41
+ throw new Error("Received operator not allowed");
42
+ }
43
+ }
44
+ else {
45
+ return funcName;
46
+ }
47
+ }
48
+ throw new Error("Unknown function name");
49
+ }
50
+ transform(parsed) {
51
+ const traverse = (node) => {
52
+ switch (node.type) {
53
+ case "call_expression": {
54
+ if (typeof node.funcCall !== "string") {
55
+ throw new Error("Property access expression and element access expression not supported");
56
+ }
57
+ const funcName = this.matchFunctionName(node.funcCall);
58
+ if (funcName in ir_js_1.Operators) {
59
+ return new ir_js_1.Operation(funcName, node.args?.map((arg) => traverse(arg)));
60
+ }
61
+ if (funcName in ir_js_1.Comparators) {
62
+ if (node.args && node.args.length === 2) {
63
+ return new ir_js_1.Comparison(funcName, traverse(node.args[0]), traverse(node.args[1]));
64
+ }
65
+ throw new Error("Comparator must have exactly 2 arguments");
66
+ }
67
+ throw new Error("Function name neither operator nor comparator");
68
+ }
69
+ case "string_literal": {
70
+ return node.value;
71
+ }
72
+ case "numeric_literal": {
73
+ return node.value;
74
+ }
75
+ case "array_literal": {
76
+ return node.values.map((value) => traverse(value));
77
+ }
78
+ case "object_literal": {
79
+ return node.values.reduce((acc, value) => {
80
+ acc[value.identifier] = traverse(value.value);
81
+ return acc;
82
+ }, {});
83
+ }
84
+ case "boolean_literal": {
85
+ return node.value;
86
+ }
87
+ default: {
88
+ throw new Error("Unknown node type");
89
+ }
90
+ }
91
+ };
92
+ return traverse(parsed);
93
+ }
94
+ async parse(expression) {
95
+ const expressionParser = new expression_js_1.ExpressionParser();
96
+ const parsed = (await expressionParser.parse(expression));
97
+ if (!parsed) {
98
+ throw new Error("Could not parse expression");
99
+ }
100
+ return this.transform(parsed);
101
+ }
102
+ }
103
+ exports.QueryTransformer = QueryTransformer;
@@ -0,0 +1,12 @@
1
+ import { Comparator, Comparison, Operation, Operator } from "./ir.js";
2
+ export type TraverseType = boolean | Operation | Comparison | string | number | {
3
+ [key: string]: TraverseType;
4
+ } | TraverseType[];
5
+ export declare class QueryTransformer {
6
+ allowedComparators: Comparator[];
7
+ allowedOperators: Operator[];
8
+ constructor(allowedComparators?: Comparator[], allowedOperators?: Operator[]);
9
+ private matchFunctionName;
10
+ private transform;
11
+ parse(expression: string): Promise<Operation | Comparison>;
12
+ }
@@ -0,0 +1,99 @@
1
+ import { Comparators, Comparison, Operation, Operators, } from "./ir.js";
2
+ import { ExpressionParser, } from "../../output_parsers/expression.js";
3
+ export class QueryTransformer {
4
+ constructor(allowedComparators = [], allowedOperators = []) {
5
+ Object.defineProperty(this, "allowedComparators", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: allowedComparators
10
+ });
11
+ Object.defineProperty(this, "allowedOperators", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: allowedOperators
16
+ });
17
+ }
18
+ matchFunctionName(funcName) {
19
+ if (funcName in Comparators) {
20
+ if (this.allowedComparators.length > 0) {
21
+ if (this.allowedComparators.includes(funcName)) {
22
+ return funcName;
23
+ }
24
+ else {
25
+ throw new Error("Received comparator not allowed");
26
+ }
27
+ }
28
+ else {
29
+ return funcName;
30
+ }
31
+ }
32
+ if (funcName in Operators) {
33
+ if (this.allowedOperators.length > 0) {
34
+ if (this.allowedOperators.includes(funcName)) {
35
+ return funcName;
36
+ }
37
+ else {
38
+ throw new Error("Received operator not allowed");
39
+ }
40
+ }
41
+ else {
42
+ return funcName;
43
+ }
44
+ }
45
+ throw new Error("Unknown function name");
46
+ }
47
+ transform(parsed) {
48
+ const traverse = (node) => {
49
+ switch (node.type) {
50
+ case "call_expression": {
51
+ if (typeof node.funcCall !== "string") {
52
+ throw new Error("Property access expression and element access expression not supported");
53
+ }
54
+ const funcName = this.matchFunctionName(node.funcCall);
55
+ if (funcName in Operators) {
56
+ return new Operation(funcName, node.args?.map((arg) => traverse(arg)));
57
+ }
58
+ if (funcName in Comparators) {
59
+ if (node.args && node.args.length === 2) {
60
+ return new Comparison(funcName, traverse(node.args[0]), traverse(node.args[1]));
61
+ }
62
+ throw new Error("Comparator must have exactly 2 arguments");
63
+ }
64
+ throw new Error("Function name neither operator nor comparator");
65
+ }
66
+ case "string_literal": {
67
+ return node.value;
68
+ }
69
+ case "numeric_literal": {
70
+ return node.value;
71
+ }
72
+ case "array_literal": {
73
+ return node.values.map((value) => traverse(value));
74
+ }
75
+ case "object_literal": {
76
+ return node.values.reduce((acc, value) => {
77
+ acc[value.identifier] = traverse(value.value);
78
+ return acc;
79
+ }, {});
80
+ }
81
+ case "boolean_literal": {
82
+ return node.value;
83
+ }
84
+ default: {
85
+ throw new Error("Unknown node type");
86
+ }
87
+ }
88
+ };
89
+ return traverse(parsed);
90
+ }
91
+ async parse(expression) {
92
+ const expressionParser = new ExpressionParser();
93
+ const parsed = (await expressionParser.parse(expression));
94
+ if (!parsed) {
95
+ throw new Error("Could not parse expression");
96
+ }
97
+ return this.transform(parsed);
98
+ }
99
+ }