Include timeout function in GUI

This commit is contained in:
Wizzard 2024-03-09 05:56:54 -05:00
parent 437831aea5
commit 445eda821d
3 changed files with 20 additions and 3 deletions

View File

@ -38,6 +38,11 @@ class KuzcoCore {
async sendPrompt(prompt, model) { async sendPrompt(prompt, model) {
console.log("Model received in sendPrompt:", model) console.log("Model received in sendPrompt:", model)
const controller = new AbortController();
const signal = controller.signal;
const timeoutId = setTimeout(() => controller.abort(), 15000);
try { try {
const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', { const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', {
method: 'POST', method: 'POST',
@ -50,16 +55,25 @@ class KuzcoCore {
model: model, model: model,
stream: false, stream: false,
}), }),
signal: signal,
}); });
clearTimeout(timeoutId);
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return await response.json(); return await response.json();
} catch (error) { } catch (error) {
console.error(`An error occurred: ${error.message}`); clearTimeout(timeoutId);
return { error: error.message }; if (error.name === 'AbortError') {
console.error('Request was aborted due to timeout.');
return { error: 'Request timed out. Please try again.' };
} else {
console.error(`An error occurred: ${error.message}`);
return { error: error.message };
}
} }
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "kuzco-gui", "name": "kuzco-gui",
"version": "1.0.0", "version": "0.0.1",
"description": "Simple gui for kuzco api", "description": "Simple gui for kuzco api",
"author": "Wizzard <Wizzard@deadzone.lol>", "author": "Wizzard <Wizzard@deadzone.lol>",
"main": "kuzco-gui.js", "main": "kuzco-gui.js",

View File

@ -39,6 +39,9 @@ document.addEventListener('DOMContentLoaded', () => {
try { try {
const response = await window.electronAPI.sendPrompt(userInput, selectedModel); const response = await window.electronAPI.sendPrompt(userInput, selectedModel);
if (response.error) {
throw new Error(response.error);
}
const assistantMessage = response.choices[0].message.content.trim(); const assistantMessage = response.choices[0].message.content.trim();
displayMessage(assistantMessage, 'assistant'); displayMessage(assistantMessage, 'assistant');
} catch (error) { } catch (error) {