Skip to content

Commit 313650a

Browse files
committed
WiP tweaks to fetch node-type (including optional output caching when input is the same)
1 parent 89705a7 commit 313650a

File tree

1 file changed

+51
-8
lines changed
  • libs/web-flow-executor/src/nodes

1 file changed

+51
-8
lines changed

libs/web-flow-executor/src/nodes/fetch.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
4242
const initializeCompute = () => {
4343
return;
4444
};
45+
46+
let inputCache: string | undefined = undefined;
47+
let currentOutput: any = undefined;
4548
const computeAsync = (
4649
input: string,
4750
loopIndex?: number,
@@ -51,8 +54,26 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
5154
runCounter?: RunCounter
5255
) => {
5356
return new Promise((resolve, reject) => {
54-
function sendFetchResult(result: any) {
57+
if (node.nodeInfo?.formValues?.cacheOutput) {
58+
if (inputCache === input && inputCache && currentOutput) {
59+
resolve({
60+
result: currentOutput,
61+
followPath: undefined,
62+
stop: true,
63+
dummyEndpoint: true,
64+
});
65+
return;
66+
}
67+
}
68+
inputCache = input;
69+
function sendFetchResult(result: any, isStreamChunk = false) {
5570
return new Promise<void>((resolve) => {
71+
// no cache when streaming
72+
if (isStreamChunk) {
73+
currentOutput = undefined;
74+
} else {
75+
currentOutput = result;
76+
}
5677
runNodeFromThumb(
5778
node.thumbConnectors![0],
5879
canvasAppInstance!,
@@ -199,6 +220,9 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
199220
.then(({ value, done }) => {
200221
if (done) {
201222
if (!isFullJson) {
223+
// no cache when streaming
224+
currentOutput = undefined;
225+
202226
sendEndStream().then(() => {
203227
resolve({
204228
result: false,
@@ -246,7 +270,7 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
246270
.map((line) => JSON.parse(line));
247271
if (lines.length > 0) {
248272
console.log('lines', lines);
249-
sendFetchResult(lines).then(() => {
273+
sendFetchResult(lines, true).then(() => {
250274
readChunk();
251275
});
252276
}
@@ -264,9 +288,9 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
264288
)?.classList.remove('hidden');
265289
(
266290
errorNode.domElement as unknown as HTMLElement
267-
).textContent = error?.toString() ?? 'Error';
291+
).textContent = error?.message?.toString() ?? 'Error';
268292
}
269-
sendError(error?.toString() ?? 'Error');
293+
sendError(error?.message?.toString() ?? 'Error');
270294
});
271295
};
272296

@@ -289,7 +313,7 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
289313
)?.classList.remove('hidden');
290314
(
291315
errorNode.domElement as unknown as HTMLElement
292-
).textContent = json?.error?.toString() ?? 'Error';
316+
).textContent = json?.error?.message?.toString() ?? 'Error';
293317
}
294318
sendError(json?.error?.message?.toString() ?? 'Error');
295319
return;
@@ -333,9 +357,9 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
333357
errorNode?.domElement as unknown as HTMLElement
334358
)?.classList.remove('hidden');
335359
(errorNode.domElement as unknown as HTMLElement).textContent =
336-
error?.toString() ?? 'Error';
360+
error?.message?.toString() ?? 'Error';
337361
}
338-
sendError(error?.toString() ?? 'Error');
362+
sendError(error?.message?.toString() ?? 'Error');
339363
});
340364

341365
//
@@ -490,7 +514,8 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
490514
errorNode = createElement(
491515
'div',
492516
{
493-
class: `bg-red-500 p-4 rounded absolute bottom-[calc(100%+8px)] h-[min-content] w-full hidden
517+
class: `bg-red-500 p-4 rounded absolute bottom-[calc(100%+8px)] h-[min-content] w-full hidden
518+
break-words
494519
after:content-['']
495520
after:w-0 after:h-0
496521
after:border-l-[10px] after:border-l-transparent
@@ -615,6 +640,24 @@ export const getFetch: NodeTaskFactory<NodeInfo> = (
615640
}
616641
},
617642
},
643+
{
644+
fieldType: FormFieldType.Checkbox,
645+
fieldName: 'cacheOutput',
646+
label: 'Cache Output for same input',
647+
value: initalValues?.['cacheOutput'] ?? false,
648+
onChange: (value: boolean) => {
649+
if (!node || !node.nodeInfo) {
650+
return;
651+
}
652+
node.nodeInfo.formValues = {
653+
...node.nodeInfo.formValues,
654+
['cacheOutput']: value,
655+
};
656+
if (updated) {
657+
updated();
658+
}
659+
},
660+
},
618661
];
619662
node.nodeInfo.computeAsync = computeAsync;
620663
node.nodeInfo.initializeCompute = initializeCompute;

0 commit comments

Comments
 (0)