Skip to content

Commit db62eba

Browse files
committed
Add timeout to openai call
1 parent ba220f5 commit db62eba

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

src/components/molecules/flow/flowtestai.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ export const generateFlowData = async (instruction, modelName, collectionId) =>
6262
return Promise.reject(new Error('Collection not found'));
6363
}
6464
} catch (error) {
65-
return Promise.reject(`Error generating flowData: ${error}`);
65+
return Promise.reject(error);
6666
}
6767
};

src/components/molecules/flow/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ export const init = (flowData) => {
3333
// Initialization
3434
if (flowData && flowData.nodes && flowData.edges) {
3535
return {
36-
nodes: flowData.nodes,
37-
edges: flowData.edges,
36+
nodes: cloneDeep(flowData.nodes),
37+
edges: cloneDeep(flowData.edges),
3838
};
3939
} else if (flowData && flowData.nodes && !flowData.edges) {
4040
// AI prompt generated
41-
const nodes = initFlowData.nodes;
42-
const edges = initFlowData.edges;
41+
const nodes = cloneDeep(initFlowData.nodes);
42+
const edges = cloneDeep(initFlowData.edges);
4343
for (let i = 0; i < flowData.nodes.length; i++) {
4444
nodes.push({
4545
id: `${i + 2}`,
@@ -61,7 +61,7 @@ export const init = (flowData) => {
6161
edges,
6262
};
6363
} else {
64-
return initFlowData;
64+
return cloneDeep(initFlowData);
6565
}
6666
};
6767

src/components/molecules/modals/GenerateFlowTestModal.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import useCanvasStore from 'stores/CanvasStore';
1111
import { toast } from 'react-toastify';
1212
import { isEqual } from 'lodash';
1313
import useCommonStore from 'stores/CommonStore';
14+
import useCollectionStore from 'stores/CollectionStore';
15+
import { promiseWithTimeout } from 'utils/common';
1416

1517
const GenerateFlowTestModal = ({ closeFn = () => null, open = false, collectionId }) => {
1618
const setShowLoader = useCommonStore((state) => state.setShowLoader);
@@ -20,6 +22,8 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false, collectionI
2022
const [selectedModel, setSelectedModel] = useState(null);
2123
const [textareaValue, setTextareaValue] = useState('');
2224

25+
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
26+
2327
return (
2428
<Transition appear show={open} as={Fragment}>
2529
<Dialog as='div' className='relative z-10' onClose={closeFn}>
@@ -109,7 +113,9 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false, collectionI
109113
className='nodrag nowheel block w-full p-2.5'
110114
name='keyName'
111115
placeholder='Enter your Open AI key'
112-
onChange={(e) => console.log(e.target.value)}
116+
value={collection ? collection.dotEnvVariables['OPENAI_APIKEY'] : ''}
117+
readOnly='readonly'
118+
//onChange={(e) => setOpenAIKey(e.target.value)}
113119
/>
114120
</div>
115121
) : (
@@ -139,7 +145,7 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false, collectionI
139145
toast.info('Please select a model');
140146
} else {
141147
setShowLoader(true);
142-
generateFlowData(textareaValue, selectedModel, collectionId)
148+
promiseWithTimeout(generateFlowData(textareaValue, selectedModel, collectionId), 30000)
143149
.then((flowData) => {
144150
setShowLoader(false);
145151
if (isEqual(flowData.nodes, [])) {
@@ -153,8 +159,7 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false, collectionI
153159
})
154160
.catch((error) => {
155161
setShowLoader(false);
156-
console.log(error);
157-
toast.error(`Error while generating flow data`);
162+
toast.error(`Error while generating flow data ${error}`);
158163
closeFn();
159164
});
160165
}

src/service/collection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toast } from 'react-toastify';
77
import { useTabStore } from 'stores/TabStore';
88
import useCanvasStore from 'stores/CanvasStore';
99
import { initFlowData } from 'components/molecules/flow/utils';
10+
import { cloneDeep } from 'lodash';
1011

1112
export const createCollection = (openAPISpecFilePath, collectionFolderPath) => {
1213
const { ipcRenderer } = window;
@@ -244,7 +245,10 @@ export const createFlowTest = (name, folderPath, collectionId) => {
244245
return Promise.reject(new Error('A flowtest with the same name already exists'));
245246
} else {
246247
return new Promise((resolve, reject) => {
247-
ipcRenderer.invoke('renderer:create-flowtest', name, folderPath, initFlowData).then(resolve).catch(reject);
248+
ipcRenderer
249+
.invoke('renderer:create-flowtest', name, folderPath, cloneDeep(initFlowData))
250+
.then(resolve)
251+
.catch(reject);
248252
useEventStore.getState().addEvent({
249253
id: uuidv4(),
250254
type: 'OPEN_NEW_FLOWTEST',

src/utils/common.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ export const getDefaultValue = (type) => {
1515
return '';
1616
}
1717
};
18+
19+
export const promiseWithTimeout = (promise, ms) => {
20+
// Create a new promise that rejects in <ms> milliseconds
21+
let timeout = new Promise((resolve, reject) => {
22+
let id = setTimeout(() => {
23+
clearTimeout(id);
24+
reject(new Error('Timed out in ' + ms + 'ms.'));
25+
}, ms);
26+
});
27+
28+
// Returns a race between our timeout and the passed in promise
29+
return Promise.race([promise, timeout]);
30+
};

0 commit comments

Comments
 (0)