Skip to content

Commit dc2d241

Browse files
committed
additional @pushto operation in json transformer
1 parent f8b261a commit dc2d241

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

archive/json-map-language.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
}
1212
}
1313
},
14+
"@pushTo:messages": {
15+
"@comment": "this pushes a new message to messages",
16+
"role": "system",
17+
"@set:content": "input"
18+
},
1419
"@comment": "this sets the name property to the value of payload['input-property-name']",
1520
"@set:name": "input-property-name",
1621
"@operation:object": {

libs/web-flow-executor/src/nodes/json-utils/transform-json.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,23 @@ describe('transformJSON', () => {
8282
},
8383
},
8484
},
85+
'@pushTo:messages': {
86+
'@comment': 'this pushes a new message to messages',
87+
role: 'system',
88+
'@set:content': 'input',
89+
},
8590
};
8691
const result = transformJSON(input, undefined, 'root', {
8792
['payload-messages']: [
8893
{ role: 'admin', message: 'hello', a: 5, b: 8 },
8994
{ role: 'user', message: 'world', a: 303, b: 606 },
9095
],
96+
['input']: 'hello test',
9197
});
9298
expect((result as any)['messages']).toEqual([
9399
{ role: 'admin', content: 'hello', calc: 13 },
94100
{ role: 'user', content: 'world', calc: 909 },
101+
{ role: 'system', content: 'hello test' },
95102
]);
96103
});
97104

libs/web-flow-executor/src/nodes/json-utils/transform-json.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,37 @@ const evaluateAtProperty = (
1111
newJson: any,
1212
key: string,
1313
value: any,
14-
payload: any
14+
payload: any,
15+
initialPayload?: any
1516
) => {
16-
if (key.startsWith('@expression:')) {
17+
if (key.startsWith('@pushTo:')) {
18+
const setProperty = key.replace('@pushTo:', '');
19+
if (!newJson[setProperty]) {
20+
newJson[setProperty] = [];
21+
}
22+
const newItem: any = {};
23+
Object.keys(value).forEach((mapKey) => {
24+
if (mapKey.startsWith('@') && !mapKey.startsWith('@@')) {
25+
const mapValue = value[mapKey];
26+
evaluateAtProperty(
27+
newItem,
28+
mapKey,
29+
mapValue,
30+
initialPayload,
31+
initialPayload
32+
);
33+
} else {
34+
const mapValue = value[mapKey];
35+
if (isArrayOrObject(mapValue)) {
36+
transformJSON(mapValue, mapKey, '', initialPayload);
37+
} else {
38+
newItem[mapKey] = mapValue;
39+
}
40+
}
41+
});
42+
43+
newJson[setProperty].push(newItem);
44+
} else if (key.startsWith('@expression:')) {
1745
const setProperty = key.replace('@expression:', '');
1846
const compiledExpression = compileExpressionAsInfo(value);
1947
const expressionFunction = (
@@ -55,7 +83,7 @@ const evaluateAtProperty = (
5583
const newItem: any = {};
5684
Object.keys(value.map.properties).forEach((mapKey) => {
5785
const mapValue = value.map.properties[mapKey];
58-
evaluateAtProperty(newItem, mapKey, mapValue, item);
86+
evaluateAtProperty(newItem, mapKey, mapValue, item, initialPayload);
5987
});
6088
return newItem;
6189
});
@@ -98,7 +126,7 @@ export const transformJSON = (
98126
console.log(`${path ? path + '.' : ''}${key}`);
99127

100128
if (key.startsWith('@') && !key.startsWith('@@')) {
101-
evaluateAtProperty(newJson, key, value, payload);
129+
evaluateAtProperty(newJson, key, value, payload, payload);
102130
} else if (isArrayOrObject(value)) {
103131
const result = transformJSON(
104132
value,
@@ -115,7 +143,7 @@ export const transformJSON = (
115143
});
116144
return newJson;
117145
} else {
118-
console.log(path, json);
119-
return json;
146+
console.error(path, json);
147+
throw new Error('Unexpected JSON');
120148
}
121149
};

libs/web-flow-executor/src/nodes/raw-json.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ export const getRawJsonNode: NodeTaskFactory<NodeInfo> = (
172172
];
173173
},
174174
(nodeInstance) => {
175-
if (nodeInstance.node.nodeInfo) {
176-
nodeInstance.node.nodeInfo.isRunOnStart = true;
177-
}
175+
// if (nodeInstance.node.nodeInfo) {
176+
// nodeInstance.node.nodeInfo.isRunOnStart = true;
177+
// }
178178
canvasAppInstance = nodeInstance.contextInstance;
179179
rect = nodeInstance.rect;
180180
node = nodeInstance.node as IRectNodeComponent<NodeInfo>;

libs/web-flow-executor/src/nodes/user-text-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const getUserTextInput =
5252
followPath: undefined,
5353
};
5454
} else {
55-
if (typeof input !== 'string') {
55+
if (typeof input === 'string') {
5656
currentValue = input;
5757
if (inputElement) {
5858
inputElement.value = currentValue;

0 commit comments

Comments
 (0)