Add stop button

This commit is contained in:
Wizzard 2024-03-09 15:22:44 -05:00
parent d2470b2dd6
commit bdf5715868
6 changed files with 54 additions and 11 deletions

View File

@ -38,6 +38,25 @@ body {
background-color: #6c757d; background-color: #6c757d;
cursor: not-allowed; cursor: not-allowed;
} }
#stopButton {
padding: 10px 20px;
font-size: 1rem;
color: white;
background-color: #c0392b;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.2s;
}
#stopButton:hover {
background-color: #a93226;
}
#stopButton:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.message { .message {
font-size: 0.9em; font-size: 0.9em;
padding: 10px 15px; padding: 10px 15px;

View File

@ -23,6 +23,7 @@
<form id="chatForm" class="chat-form"> <form id="chatForm" class="chat-form">
<input id="promptInput" type="text" placeholder="Enter your prompt" autofocus> <input id="promptInput" type="text" placeholder="Enter your prompt" autofocus>
<button id="sendButton" onclick="sendPrompt()">Send</button> <button id="sendButton" onclick="sendPrompt()">Send</button>
<button id="stopButton" disabled>Stop</button>
</form> </form>
</footer> </footer>
</div> </div>

View File

@ -75,4 +75,8 @@ const kuzcoCore = new KuzcoCore();
ipcMain.handle('send-prompt', async (event, { prompt, model }) => { ipcMain.handle('send-prompt', async (event, { prompt, model }) => {
console.log("Received model in main process:", model); console.log("Received model in main process:", model);
return await kuzcoCore.sendPrompt(prompt, model); return await kuzcoCore.sendPrompt(prompt, model);
});
ipcMain.on('abort-prompt', () => {
kuzcoCore.abortFetch();
}); });

View File

@ -14,6 +14,8 @@ class KuzcoCore {
constructor() { constructor() {
this.configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json'); this.configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json');
this.API_KEY = this.loadApiKey(); this.API_KEY = this.loadApiKey();
this.controller = new AbortController();
this.isAborted = false;
} }
loadApiKey() { loadApiKey() {
@ -36,12 +38,17 @@ class KuzcoCore {
return fs.existsSync(this.configPath) && this.API_KEY !== ''; return fs.existsSync(this.configPath) && this.API_KEY !== '';
} }
abortFetch() {
this.isAborted = true;
this.controller.abort();
}
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(); this.controller = new AbortController();
const signal = controller.signal; const signal = this.controller.signal;
const timeoutId = setTimeout(() => controller.abort(), 25000); const timeoutId = setTimeout(() => this.controller.abort(), 25000);
try { try {
const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', { const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', {
@ -65,15 +72,19 @@ class KuzcoCore {
} }
return await response.json(); return await response.json();
} catch (error) { } catch (error) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
if (error.name === 'AbortError') { const errorMessage = this.isAborted
console.error('Request was aborted due to timeout.'); ? 'Request aborted by the user. Please try again.'
return { error: 'Request timed out. Please try again.' }; : 'Request timed out. Please try again.';
} else {
console.error(`An error occurred: ${error.message}`); this.isAborted = false;
return { error: error.message }; this.controller = new AbortController();
}
return error.name === 'AbortError'
? { error: errorMessage }
: { error: error.message };
} }
} }
} }

View File

@ -3,4 +3,5 @@ const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', { contextBridge.exposeInMainWorld('electronAPI', {
sendPrompt: (prompt, model) => ipcRenderer.invoke('send-prompt', { prompt, model }), sendPrompt: (prompt, model) => ipcRenderer.invoke('send-prompt', { prompt, model }),
onApiKeySaved: (callback) => ipcRenderer.on('api-key-saved', callback), onApiKeySaved: (callback) => ipcRenderer.on('api-key-saved', callback),
abortPrompt: () => ipcRenderer.send('abort-prompt')
}); });

View File

@ -19,6 +19,7 @@ document.addEventListener('DOMContentLoaded', () => {
const promptInput = document.getElementById('promptInput'); const promptInput = document.getElementById('promptInput');
const modelSelect = document.getElementById('modelSelect'); const modelSelect = document.getElementById('modelSelect');
const sendButton = document.getElementById('sendButton'); const sendButton = document.getElementById('sendButton');
const stopButton = document.getElementById('stopButton');
const modelSelectionContainer = document.getElementById('modelSelectionContainer'); const modelSelectionContainer = document.getElementById('modelSelectionContainer');
if (chatForm && promptInput && modelSelect) { if (chatForm && promptInput && modelSelect) {
@ -32,6 +33,7 @@ document.addEventListener('DOMContentLoaded', () => {
sendButton.disabled = true; sendButton.disabled = true;
promptInput.disabled = true; promptInput.disabled = true;
stopButton.disabled = false;
displayMessage(userInput, 'user'); displayMessage(userInput, 'user');
@ -49,10 +51,10 @@ document.addEventListener('DOMContentLoaded', () => {
displayMessage(`Error: ${error.message}`, 'assistant'); displayMessage(`Error: ${error.message}`, 'assistant');
} finally { } finally {
typingIndicator.remove(); typingIndicator.remove();
sendButton.disabled = false; sendButton.disabled = false;
promptInput.disabled = false; promptInput.disabled = false;
promptInput.focus(); promptInput.focus();
stopButton.disabled = true;
} }
}); });
} else { } else {
@ -60,6 +62,11 @@ document.addEventListener('DOMContentLoaded', () => {
} }
}); });
document.getElementById('stopButton').addEventListener('click', () => {
window.electronAPI.abortPrompt();
document.getElementById('stopButton').disabled = true;
});
function displayTypingIndicator() { function displayTypingIndicator() {
const chatHistory = document.getElementById('chatHistory'); const chatHistory = document.getElementById('chatHistory');
const typingIndicator = document.createElement('div'); const typingIndicator = document.createElement('div');