@takeshape/schema 11.63.7 → 11.64.0
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/dist/__tests__/validate.test.js +26 -0
- package/dist/agents.d.ts +2 -1
- package/dist/agents.js +8 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/project-schema/latest.d.ts +4 -61
- package/dist/project-schema/v3.48.0.d.ts +4 -61
- package/dist/project-schema/v3.49.0.d.ts +4 -61
- package/dist/project-schema/v3.50.0.d.ts +4 -61
- package/dist/project-schema/v3.51.0.d.ts +4 -61
- package/dist/project-schema/v3.52.0.d.ts +4 -61
- package/dist/project-schema/v3.53.0.d.ts +4 -61
- package/dist/schemas/project-schema/experimental.json +4 -245
- package/dist/util/__tests__/expressions.test.d.ts +1 -0
- package/dist/util/__tests__/expressions.test.js +172 -0
- package/dist/util/expressions.d.ts +25 -0
- package/dist/util/expressions.js +323 -0
- package/dist/validate/validate.js +40 -0
- package/examples/latest/agent-schema.json +17 -40
- package/examples/source/agent-schema.json +36 -31
- package/package.json +6 -5
|
@@ -285,8 +285,6 @@ export type CacheTriggerConfigV3_53_0 = CacheScheduleTriggerConfigV3_53_0 | Cach
|
|
|
285
285
|
export type ShapeSchemaV3_53_0 = ShapeSchemaAllOfV3_53_0 | ShapeSchemaExtendsV3_53_0 | ShapeSchemaOneOfV3_53_0 | ShapeSchemaEnumV3_53_0 | ObjectSchemaV3_53_0 | ShapeSchemaAnyV3_53_0;
|
|
286
286
|
export type AgentAPIV3_53_0 = AgentAPIChatV3_53_0 | AgentAPIGenerateV3_53_0;
|
|
287
287
|
export type MemoryLocationsV3_53_0 = AgentSessionMemoryLocationV3_53_0[];
|
|
288
|
-
export type AgentVariableStepV3_53_0 = AgentVariableStepSessionMemoryV3_53_0 | AgentVariableStepGraphQLArgV3_53_0 | AgentVariableStepStateOutputV3_53_0 | AgentVariableStepPreviousStateOutputV3_53_0 | AgentVariableStepStaticValueV3_53_0;
|
|
289
|
-
export type AgentVariableStepConditionV3_53_0 = AgentVariableStepConditionNoneV3_53_0 | AgentVariableStepConditionSourceStateV3_53_0;
|
|
290
288
|
export type VariablesV3_53_0 = AgentVariableV3_53_0[];
|
|
291
289
|
export type AgentExecutionV3_53_0 = AgentExecutionGraphQLV3_53_0 | AgentExecutionGenerateV3_53_0 | AgentExecutionChatV3_53_0;
|
|
292
290
|
export type AgentAIStateInputV3_53_0 = AgentAIStateInputArgumentV3_53_0 | AgentAIStateInputTemplateV3_53_0;
|
|
@@ -1762,34 +1760,9 @@ export interface AgentStartV3_53_0 {
|
|
|
1762
1760
|
export interface AgentTransitionStepV3_53_0 {
|
|
1763
1761
|
destination: string;
|
|
1764
1762
|
suspend?: boolean;
|
|
1765
|
-
condition
|
|
1763
|
+
condition?: string;
|
|
1766
1764
|
limit?: number;
|
|
1767
1765
|
}
|
|
1768
|
-
export interface AgentTransitionStepConditionNoneV3_53_0 {
|
|
1769
|
-
type: 'none';
|
|
1770
|
-
}
|
|
1771
|
-
export interface AgentTransitionStepConditionArtifactV3_53_0 {
|
|
1772
|
-
type: 'artifact';
|
|
1773
|
-
path?: string;
|
|
1774
|
-
negated?: boolean;
|
|
1775
|
-
}
|
|
1776
|
-
export interface AgentTransitionStepConditionStateOutputV3_53_0 {
|
|
1777
|
-
type: 'stateOutput';
|
|
1778
|
-
stateId: string;
|
|
1779
|
-
path?: string;
|
|
1780
|
-
negated?: boolean;
|
|
1781
|
-
}
|
|
1782
|
-
export interface AgentTransitionStepConditionStringContainsV3_53_0 {
|
|
1783
|
-
type: 'stringContains';
|
|
1784
|
-
path?: string;
|
|
1785
|
-
string: string;
|
|
1786
|
-
negated?: boolean;
|
|
1787
|
-
}
|
|
1788
|
-
export interface AgentTransitionStepConditionArgumentV3_53_0 {
|
|
1789
|
-
type: 'argument';
|
|
1790
|
-
argument: string;
|
|
1791
|
-
negated?: boolean;
|
|
1792
|
-
}
|
|
1793
1766
|
/**
|
|
1794
1767
|
* States that are traversed during the execution of an agent.
|
|
1795
1768
|
*/
|
|
@@ -1816,39 +1789,9 @@ export interface AgentVariableV3_53_0 {
|
|
|
1816
1789
|
*/
|
|
1817
1790
|
steps: AgentVariableStepV3_53_0[];
|
|
1818
1791
|
}
|
|
1819
|
-
export interface
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
path?: string;
|
|
1823
|
-
}
|
|
1824
|
-
export interface AgentVariableStepGraphQLArgV3_53_0 {
|
|
1825
|
-
type: 'graphqlArg';
|
|
1826
|
-
argName: string;
|
|
1827
|
-
}
|
|
1828
|
-
export interface AgentVariableStepStateOutputV3_53_0 {
|
|
1829
|
-
type: 'stateOutput';
|
|
1830
|
-
stateId: string;
|
|
1831
|
-
path?: string;
|
|
1832
|
-
}
|
|
1833
|
-
export interface AgentVariableStepPreviousStateOutputV3_53_0 {
|
|
1834
|
-
type: 'previousStateOutput';
|
|
1835
|
-
path?: string;
|
|
1836
|
-
}
|
|
1837
|
-
/**
|
|
1838
|
-
* An Agent Variable that has a static value.
|
|
1839
|
-
*/
|
|
1840
|
-
export interface AgentVariableStepStaticValueV3_53_0 {
|
|
1841
|
-
type: 'staticValue';
|
|
1842
|
-
condition?: AgentVariableStepConditionV3_53_0;
|
|
1843
|
-
value: string;
|
|
1844
|
-
}
|
|
1845
|
-
export interface AgentVariableStepConditionNoneV3_53_0 {
|
|
1846
|
-
type: 'none';
|
|
1847
|
-
}
|
|
1848
|
-
export interface AgentVariableStepConditionSourceStateV3_53_0 {
|
|
1849
|
-
type: 'sourceState';
|
|
1850
|
-
stateId: string;
|
|
1851
|
-
negated?: boolean;
|
|
1792
|
+
export interface AgentVariableStepV3_53_0 {
|
|
1793
|
+
condition?: string;
|
|
1794
|
+
expression: string;
|
|
1852
1795
|
}
|
|
1853
1796
|
export interface AgentExecutionGraphQLV3_53_0 {
|
|
1854
1797
|
type: 'graphql';
|
|
@@ -143,148 +143,16 @@
|
|
|
143
143
|
},
|
|
144
144
|
"agentVariableStep": {
|
|
145
145
|
"title": "Agent Variable Step",
|
|
146
|
-
"discriminator": {
|
|
147
|
-
"propertyName": "type"
|
|
148
|
-
},
|
|
149
|
-
"oneOf": [
|
|
150
|
-
{
|
|
151
|
-
"$ref": "#/definitions/agentVariableStepSessionMemory"
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
"$ref": "#/definitions/agentVariableStepGraphqlArg"
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
"$ref": "#/definitions/agentVariableStepStateOutput"
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
"$ref": "#/definitions/agentVariableStepPreviousStateOutput"
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
"$ref": "#/definitions/agentVariableStepStaticValue"
|
|
164
|
-
}
|
|
165
|
-
]
|
|
166
|
-
},
|
|
167
|
-
"agentVariableStepSessionMemory": {
|
|
168
|
-
"title": "Agent Variable Step Session Memory",
|
|
169
|
-
"type": "object",
|
|
170
|
-
"properties": {
|
|
171
|
-
"type": {
|
|
172
|
-
"enum": ["sessionMemory"]
|
|
173
|
-
},
|
|
174
|
-
"memoryKey": {
|
|
175
|
-
"type": "string"
|
|
176
|
-
},
|
|
177
|
-
"path": {
|
|
178
|
-
"type": "string"
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
"required": ["type", "memoryKey"],
|
|
182
|
-
"additionalProperties": false
|
|
183
|
-
},
|
|
184
|
-
"agentVariableStepGraphqlArg": {
|
|
185
|
-
"title": "Agent Variable Step GraphQL Arg",
|
|
186
|
-
"type": "object",
|
|
187
|
-
"properties": {
|
|
188
|
-
"type": {
|
|
189
|
-
"enum": ["graphqlArg"]
|
|
190
|
-
},
|
|
191
|
-
"argName": {
|
|
192
|
-
"type": "string"
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
"required": ["type", "argName"],
|
|
196
|
-
"additionalProperties": false
|
|
197
|
-
},
|
|
198
|
-
"agentVariableStepPreviousStateOutput": {
|
|
199
|
-
"title": "Agent Variable Step Previous State Output",
|
|
200
|
-
"type": "object",
|
|
201
|
-
"properties": {
|
|
202
|
-
"type": {
|
|
203
|
-
"enum": ["previousStateOutput"]
|
|
204
|
-
},
|
|
205
|
-
"path": {
|
|
206
|
-
"type": "string"
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
"required": ["type"],
|
|
210
|
-
"additionalProperties": false
|
|
211
|
-
},
|
|
212
|
-
"agentVariableStepStateOutput": {
|
|
213
|
-
"title": "Agent Variable Step State Output",
|
|
214
146
|
"type": "object",
|
|
215
147
|
"properties": {
|
|
216
|
-
"type": {
|
|
217
|
-
"enum": ["stateOutput"]
|
|
218
|
-
},
|
|
219
|
-
"stateId": {
|
|
220
|
-
"type": "string",
|
|
221
|
-
"pattern": "^[0-9A-Za-z_]+$"
|
|
222
|
-
},
|
|
223
|
-
"path": {
|
|
224
|
-
"type": "string"
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
"required": ["type", "stateId"],
|
|
228
|
-
"additionalProperties": false
|
|
229
|
-
},
|
|
230
|
-
"agentVariableStepCondition": {
|
|
231
|
-
"title": "Agent Variable Step Condition",
|
|
232
|
-
"discriminator": {
|
|
233
|
-
"propertyName": "type"
|
|
234
|
-
},
|
|
235
|
-
"oneOf": [
|
|
236
|
-
{
|
|
237
|
-
"$ref": "#/definitions/agentVariableStepConditionNone"
|
|
238
|
-
},
|
|
239
|
-
{
|
|
240
|
-
"$ref": "#/definitions/agentVariableStepConditionSourceState"
|
|
241
|
-
}
|
|
242
|
-
]
|
|
243
|
-
},
|
|
244
|
-
"agentVariableStepStaticValue": {
|
|
245
|
-
"title": "Agent Variable Step Static Value",
|
|
246
|
-
"description": "An Agent Variable that has a static value.",
|
|
247
|
-
"type": "object",
|
|
248
|
-
"properties": {
|
|
249
|
-
"type": {
|
|
250
|
-
"enum": ["staticValue"]
|
|
251
|
-
},
|
|
252
148
|
"condition": {
|
|
253
|
-
"$ref": "#/definitions/agentVariableStepCondition"
|
|
254
|
-
},
|
|
255
|
-
"value": {
|
|
256
149
|
"type": "string"
|
|
257
|
-
}
|
|
258
|
-
},
|
|
259
|
-
"required": ["type", "value"],
|
|
260
|
-
"additionalProperties": false
|
|
261
|
-
},
|
|
262
|
-
"agentVariableStepConditionNone": {
|
|
263
|
-
"title": "Agent Variable Step Condition None",
|
|
264
|
-
"type": "object",
|
|
265
|
-
"properties": {
|
|
266
|
-
"type": {
|
|
267
|
-
"enum": ["none"]
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
"required": ["type"],
|
|
271
|
-
"additionalProperties": false
|
|
272
|
-
},
|
|
273
|
-
"agentVariableStepConditionSourceState": {
|
|
274
|
-
"title": "Agent Variable Step Condition Source State",
|
|
275
|
-
"type": "object",
|
|
276
|
-
"properties": {
|
|
277
|
-
"type": {
|
|
278
|
-
"enum": ["sourceState"]
|
|
279
150
|
},
|
|
280
|
-
"
|
|
151
|
+
"expression": {
|
|
281
152
|
"type": "string"
|
|
282
|
-
},
|
|
283
|
-
"negated": {
|
|
284
|
-
"type": "boolean"
|
|
285
153
|
}
|
|
286
154
|
},
|
|
287
|
-
"required": ["
|
|
155
|
+
"required": ["expression"],
|
|
288
156
|
"additionalProperties": false
|
|
289
157
|
},
|
|
290
158
|
"agentStart": {
|
|
@@ -695,91 +563,6 @@
|
|
|
695
563
|
"required": ["type", "service", "model", "input"],
|
|
696
564
|
"additionalProperties": false
|
|
697
565
|
},
|
|
698
|
-
"agentTransitionStepConditionNone": {
|
|
699
|
-
"title": "Agent Transition Step Condition None",
|
|
700
|
-
"type": "object",
|
|
701
|
-
"properties": {
|
|
702
|
-
"type": {
|
|
703
|
-
"enum": ["none"]
|
|
704
|
-
}
|
|
705
|
-
},
|
|
706
|
-
"required": ["type"],
|
|
707
|
-
"additionalProperties": false
|
|
708
|
-
},
|
|
709
|
-
"agentTransitionStepConditionStateOutput": {
|
|
710
|
-
"title": "Agent Transition Step Condition State Output",
|
|
711
|
-
"type": "object",
|
|
712
|
-
"properties": {
|
|
713
|
-
"type": {
|
|
714
|
-
"enum": ["stateOutput"]
|
|
715
|
-
},
|
|
716
|
-
"stateId": {
|
|
717
|
-
"type": "string"
|
|
718
|
-
},
|
|
719
|
-
"path": {
|
|
720
|
-
"type": "string"
|
|
721
|
-
},
|
|
722
|
-
"negated": {
|
|
723
|
-
"type": "boolean"
|
|
724
|
-
}
|
|
725
|
-
},
|
|
726
|
-
"required": ["type", "stateId"],
|
|
727
|
-
"additionalProperties": false
|
|
728
|
-
},
|
|
729
|
-
"agentTransitionStepConditionArtifact": {
|
|
730
|
-
"title": "Agent Transition Step Condition Artifact",
|
|
731
|
-
"type": "object",
|
|
732
|
-
"properties": {
|
|
733
|
-
"type": {
|
|
734
|
-
"enum": ["artifact"]
|
|
735
|
-
},
|
|
736
|
-
"path": {
|
|
737
|
-
"type": "string"
|
|
738
|
-
},
|
|
739
|
-
"negated": {
|
|
740
|
-
"type": "boolean"
|
|
741
|
-
}
|
|
742
|
-
},
|
|
743
|
-
"required": ["type"],
|
|
744
|
-
"additionalProperties": false
|
|
745
|
-
},
|
|
746
|
-
"agentTransitionStepConditionStringContains": {
|
|
747
|
-
"title": "Agent Transition Step Condition String Contains",
|
|
748
|
-
"type": "object",
|
|
749
|
-
"properties": {
|
|
750
|
-
"type": {
|
|
751
|
-
"enum": ["stringContains"]
|
|
752
|
-
},
|
|
753
|
-
"path": {
|
|
754
|
-
"type": "string"
|
|
755
|
-
},
|
|
756
|
-
"string": {
|
|
757
|
-
"type": "string"
|
|
758
|
-
},
|
|
759
|
-
"negated": {
|
|
760
|
-
"type": "boolean"
|
|
761
|
-
}
|
|
762
|
-
},
|
|
763
|
-
"required": ["type", "string"],
|
|
764
|
-
"additionalProperties": false
|
|
765
|
-
},
|
|
766
|
-
"agentTransitionStepConditionArgument": {
|
|
767
|
-
"title": "Agent Transition Step Condition Argument",
|
|
768
|
-
"type": "object",
|
|
769
|
-
"properties": {
|
|
770
|
-
"type": {
|
|
771
|
-
"enum": ["argument"]
|
|
772
|
-
},
|
|
773
|
-
"argument": {
|
|
774
|
-
"type": "string"
|
|
775
|
-
},
|
|
776
|
-
"negated": {
|
|
777
|
-
"type": "boolean"
|
|
778
|
-
}
|
|
779
|
-
},
|
|
780
|
-
"required": ["type", "argument"],
|
|
781
|
-
"additionalProperties": false
|
|
782
|
-
},
|
|
783
566
|
"agentTransitionStep": {
|
|
784
567
|
"title": "Agent Transition Step",
|
|
785
568
|
"description": "These steps are evaluated in order until a destination is found.",
|
|
@@ -793,38 +576,14 @@
|
|
|
793
576
|
"type": "boolean"
|
|
794
577
|
},
|
|
795
578
|
"condition": {
|
|
796
|
-
"
|
|
797
|
-
"propertyName": "type"
|
|
798
|
-
},
|
|
799
|
-
"oneOf": [
|
|
800
|
-
{
|
|
801
|
-
"$ref": "#/definitions/agentTransitionStepConditionNone"
|
|
802
|
-
},
|
|
803
|
-
{
|
|
804
|
-
"$ref": "#/definitions/agentTransitionStepConditionArtifact"
|
|
805
|
-
},
|
|
806
|
-
{
|
|
807
|
-
"$ref": "#/definitions/agentTransitionStepConditionStateOutput"
|
|
808
|
-
},
|
|
809
|
-
{
|
|
810
|
-
"$ref": "#/definitions/agentTransitionStepConditionStringContains"
|
|
811
|
-
},
|
|
812
|
-
{
|
|
813
|
-
"$ref": "#/definitions/agentTransitionStepConditionArgument"
|
|
814
|
-
}
|
|
815
|
-
]
|
|
579
|
+
"type": "string"
|
|
816
580
|
},
|
|
817
581
|
"limit": {
|
|
818
582
|
"type": "integer"
|
|
819
583
|
}
|
|
820
584
|
},
|
|
821
585
|
"additionalProperties": false,
|
|
822
|
-
"required": ["destination"
|
|
823
|
-
"default": {
|
|
824
|
-
"condition": {
|
|
825
|
-
"type": "none"
|
|
826
|
-
}
|
|
827
|
-
}
|
|
586
|
+
"required": ["destination"]
|
|
828
587
|
},
|
|
829
588
|
"agentMap": {
|
|
830
589
|
"title": "Agent Map",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import jsep from 'jsep';
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
import { compileExpression, expressionToString, METHODS_NOT_SUPPORTED, THIS_NOT_SUPPORTED, validateExpression } from '../expressions.js';
|
|
4
|
+
const disallowedMemberError = /Access to member "\w+" disallowed/;
|
|
5
|
+
const fixtures = [
|
|
6
|
+
// array expression
|
|
7
|
+
{ expr: '([1,2,3])[0]', expected: 1, normalized: '[1,2,3][0]' },
|
|
8
|
+
{ expr: '(["one","two","three"])[1]', expected: 'two', normalized: '["one","two","three"][1]' },
|
|
9
|
+
{ expr: '([true,false,true])[1]', expected: false, normalized: '[true,false,true][1]' },
|
|
10
|
+
{ expr: '([true,false,true])[2]', expected: true, normalized: '[true,false,true][2]' },
|
|
11
|
+
{ expr: '([1,2,3]).length', expected: 3, normalized: '[1,2,3].length' },
|
|
12
|
+
{ expr: '([1,true,"three"]).length', expected: 3, normalized: '[1,true,"three"].length' },
|
|
13
|
+
{ expr: 'isArray([1,2,3])', expected: true },
|
|
14
|
+
{ expr: 'list[3]', expected: 4 },
|
|
15
|
+
{ expr: '(1 + 5) * 10', expected: 60 },
|
|
16
|
+
{ expr: '1 + 5 * 10', expected: 51 },
|
|
17
|
+
{ expr: 'numMap[1 + two]', expected: 'three' },
|
|
18
|
+
// binary expression
|
|
19
|
+
{ expr: '1+2', expected: 3, normalized: '1 + 2' },
|
|
20
|
+
{ expr: '2-1', expected: 1, normalized: '2 - 1' },
|
|
21
|
+
{ expr: '2*2', expected: 4, normalized: '2 * 2' },
|
|
22
|
+
{ expr: '6/3', expected: 2, normalized: '6 / 3' },
|
|
23
|
+
{ expr: '(1 + 2) + (3 + 5)', expected: 11, normalized: '1 + 2 + 3 + 5' },
|
|
24
|
+
// {expr: '5|3', expected: 7},
|
|
25
|
+
// {expr: '5&3', expected: 1},
|
|
26
|
+
// {expr: '5^3', expected: 6},
|
|
27
|
+
// {expr: '4<<2', expected: 16},
|
|
28
|
+
// {expr: '256>>4', expected: 16},
|
|
29
|
+
// {expr: '-14>>>2', expected: 1073741820},
|
|
30
|
+
{ expr: '10%6', expected: 4, normalized: '10 % 6' },
|
|
31
|
+
{ expr: '"a"+"b"', expected: 'ab', normalized: '"a" + "b"' },
|
|
32
|
+
{ expr: 'one + three', expected: 4 },
|
|
33
|
+
// call expression
|
|
34
|
+
{ expr: 'func(5)', expected: 6 },
|
|
35
|
+
{ expr: 'func(1+2)', expected: 4, normalized: 'func(1 + 2)' },
|
|
36
|
+
// conditional expression
|
|
37
|
+
{ expr: '(true ? "true" : "false")', expected: 'true', normalized: 'true ? "true" : "false"' },
|
|
38
|
+
{ expr: '( ( bool || false ) ? "true" : "false")', expected: 'true', normalized: 'bool || false ? "true" : "false"' },
|
|
39
|
+
{ expr: 'bool || false ? "true" : "false"', expected: 'true' },
|
|
40
|
+
{ expr: '( true ? ( 123*456 ) : "false")', expected: 123 * 456, normalized: 'true ? 123 * 456 : "false"' },
|
|
41
|
+
{ expr: '( false ? "true" : one + two )', expected: 3, normalized: 'false ? "true" : one + two' },
|
|
42
|
+
// identifier
|
|
43
|
+
{ expr: 'string', expected: 'string' },
|
|
44
|
+
{ expr: 'number', expected: 123 },
|
|
45
|
+
{ expr: 'bool', expected: true },
|
|
46
|
+
// literal
|
|
47
|
+
{ expr: '"foo"', expected: 'foo' }, // string literal
|
|
48
|
+
{ expr: "'foo'", expected: 'foo' }, // string literal
|
|
49
|
+
{ expr: '123', expected: 123 }, // numeric literal
|
|
50
|
+
{ expr: 'true', expected: true }, // boolean literal
|
|
51
|
+
// logical expression
|
|
52
|
+
{ expr: 'true || false', expected: true },
|
|
53
|
+
{ expr: 'true && false', expected: false },
|
|
54
|
+
{ expr: '1 == "1"', expected: true },
|
|
55
|
+
{ expr: '2 != "2"', expected: false },
|
|
56
|
+
{ expr: '1.234 === 1.234', expected: true },
|
|
57
|
+
{ expr: '123 !== "123"', expected: true },
|
|
58
|
+
{ expr: '1 < 2', expected: true },
|
|
59
|
+
{ expr: '1 > 2', expected: false },
|
|
60
|
+
{ expr: '2 <= 2', expected: true },
|
|
61
|
+
{ expr: '1 >= 2', expected: false },
|
|
62
|
+
// logical expression lazy evaluation
|
|
63
|
+
{ expr: 'true || throw()', expected: true },
|
|
64
|
+
{ expr: 'false || true', expected: true },
|
|
65
|
+
{ expr: 'false && throw()', expected: false },
|
|
66
|
+
{ expr: 'true && false', expected: false },
|
|
67
|
+
// member expression
|
|
68
|
+
{ expr: 'foo.bar', expected: 'baz' },
|
|
69
|
+
{ expr: 'foo["bar"]', expected: 'baz' },
|
|
70
|
+
{ expr: 'foo[foo.bar]', expected: 'wow' },
|
|
71
|
+
// call expression with member
|
|
72
|
+
{ expr: 'foo.func("bar")', throws: METHODS_NOT_SUPPORTED },
|
|
73
|
+
// unary expression
|
|
74
|
+
{ expr: '-one', expected: -1 },
|
|
75
|
+
{ expr: '+two', expected: 2 },
|
|
76
|
+
{ expr: '!false', expected: true },
|
|
77
|
+
{ expr: '!!true', expected: true },
|
|
78
|
+
// {expr: '~15', expected: -16},
|
|
79
|
+
{ expr: '+[]', expected: 0 },
|
|
80
|
+
// 'this' context
|
|
81
|
+
{ expr: 'this.three', expected: 3 },
|
|
82
|
+
// custom operators
|
|
83
|
+
// {expr: '@2', expected: 'two'},
|
|
84
|
+
// {expr: '3#4', expected: 3.4},
|
|
85
|
+
// {expr: '(1 # 2 # 3)', expected: 1.5}, // Fails with undefined precedence, see issue #45
|
|
86
|
+
// {expr: '1 + 2 ~ 3', expected: 9} // ~ is * but with low precedence
|
|
87
|
+
// Disallowed members
|
|
88
|
+
{ expr: 'o.__proto__', throws: disallowedMemberError },
|
|
89
|
+
{ expr: 'o.prototype', throws: disallowedMemberError },
|
|
90
|
+
{ expr: 'o.constructor', throws: disallowedMemberError },
|
|
91
|
+
{ expr: 'o["__proto__"]', throws: disallowedMemberError },
|
|
92
|
+
{ expr: 'o["prototype"]', throws: disallowedMemberError },
|
|
93
|
+
{ expr: 'o["constructor"]', throws: disallowedMemberError },
|
|
94
|
+
{ expr: 'o[p1]', throws: disallowedMemberError },
|
|
95
|
+
{ expr: 'o[p2]', throws: disallowedMemberError },
|
|
96
|
+
{ expr: 'o[c]', throws: disallowedMemberError }
|
|
97
|
+
];
|
|
98
|
+
const context = {
|
|
99
|
+
string: 'string',
|
|
100
|
+
number: 123,
|
|
101
|
+
bool: true,
|
|
102
|
+
one: 1,
|
|
103
|
+
two: 2,
|
|
104
|
+
three: 3,
|
|
105
|
+
foo: {
|
|
106
|
+
bar: 'baz',
|
|
107
|
+
baz: 'wow',
|
|
108
|
+
func() {
|
|
109
|
+
throw new Error('Should not be called.');
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
numMap: { 10: 'ten', 3: 'three' },
|
|
113
|
+
list: [1, 2, 3, 4, 5],
|
|
114
|
+
func(x) {
|
|
115
|
+
return x + 1;
|
|
116
|
+
},
|
|
117
|
+
isArray: Array.isArray,
|
|
118
|
+
throw() {
|
|
119
|
+
throw new Error('Should not be called.');
|
|
120
|
+
},
|
|
121
|
+
o: {},
|
|
122
|
+
p1: '__proto__',
|
|
123
|
+
p2: 'prototype',
|
|
124
|
+
c: 'constructor'
|
|
125
|
+
};
|
|
126
|
+
test.each(fixtures)('eval $expr', ({ expr, expected, throws }) => {
|
|
127
|
+
const compiled = compileExpression(expr);
|
|
128
|
+
if (throws) {
|
|
129
|
+
expect(() => compiled(context)).toThrow(throws);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
expect(compiled(context)).toEqual(expected);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
test.each(fixtures)('astToString $expr', ({ expr, normalized }) => {
|
|
136
|
+
const ast = jsep(expr);
|
|
137
|
+
expect(expressionToString(ast)).toEqual(normalized ?? expr);
|
|
138
|
+
});
|
|
139
|
+
describe('validateExpression', () => {
|
|
140
|
+
test('valid', () => {
|
|
141
|
+
expect(validateExpression('1 + 2')).toEqual({ valid: true });
|
|
142
|
+
});
|
|
143
|
+
test('invalid variable', () => {
|
|
144
|
+
expect(validateExpression('bogus[currentStateOutput].value')).toEqual({
|
|
145
|
+
errors: ['Unknown variable "bogus"'],
|
|
146
|
+
valid: false
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
test('invalid variable in member expression', () => {
|
|
150
|
+
expect(validateExpression('currentStateOutput[bogus].value')).toEqual({
|
|
151
|
+
errors: ['Unknown variable "bogus"'],
|
|
152
|
+
valid: false
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
test('valid member expression', () => {
|
|
156
|
+
expect(validateExpression('currentStateOutput.bogus.value')).toEqual({
|
|
157
|
+
valid: true
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
test('invalid function', () => {
|
|
161
|
+
expect(validateExpression('foo(currentStateOutput)')).toEqual({ errors: ['Unknown function "foo"'], valid: false });
|
|
162
|
+
});
|
|
163
|
+
test('invalid this', () => {
|
|
164
|
+
expect(validateExpression('this')).toEqual({ errors: [THIS_NOT_SUPPORTED], valid: false });
|
|
165
|
+
});
|
|
166
|
+
test('invalid member expression', () => {
|
|
167
|
+
expect(validateExpression('currentStateOutput.foo()')).toEqual({
|
|
168
|
+
valid: false,
|
|
169
|
+
errors: [METHODS_NOT_SUPPORTED]
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import jsep from 'jsep';
|
|
2
|
+
type AnyExpression = jsep.ArrayExpression | jsep.BinaryExpression | jsep.MemberExpression | jsep.CallExpression | jsep.ConditionalExpression | jsep.Identifier | jsep.Literal | jsep.ThisExpression | jsep.UnaryExpression;
|
|
3
|
+
export declare const METHODS_NOT_SUPPORTED = "Methods are not supported in expressions";
|
|
4
|
+
export declare const THIS_NOT_SUPPORTED = "This is not supported in expressions";
|
|
5
|
+
export declare function parseExpression(expression: string): AnyExpression;
|
|
6
|
+
export type ExpressionContext = {
|
|
7
|
+
previousStateOutput?: unknown;
|
|
8
|
+
currentStateId: string;
|
|
9
|
+
previousStateId?: string;
|
|
10
|
+
stateOutputs: Record<string, unknown>;
|
|
11
|
+
currentStateOutput?: unknown;
|
|
12
|
+
sessionMemory: Record<string, unknown>;
|
|
13
|
+
claims: Record<string, unknown>;
|
|
14
|
+
args: Record<string, unknown>;
|
|
15
|
+
vars: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
export declare function validateExpression(expression: string): {
|
|
18
|
+
valid: true;
|
|
19
|
+
} | {
|
|
20
|
+
valid: false;
|
|
21
|
+
errors: string[];
|
|
22
|
+
};
|
|
23
|
+
export declare function compileExpression(expression: string): (context: Record<string, unknown>) => unknown;
|
|
24
|
+
export declare function expressionToString(_node: jsep.Expression | null, parent?: AnyExpression): string;
|
|
25
|
+
export {};
|