2024-03-09 02:15:01 -05:00
|
|
|
function displayMessage(message, sender) {
|
|
|
|
const chatHistory = document.getElementById('chatHistory');
|
|
|
|
const messageDiv = document.createElement('div');
|
|
|
|
messageDiv.classList.add('message');
|
|
|
|
|
2024-03-09 17:23:16 -05:00
|
|
|
const parts = message.split('```');
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
const textPart = document.createElement('span');
|
|
|
|
textPart.textContent = parts[i];
|
|
|
|
messageDiv.appendChild(textPart);
|
|
|
|
} else {
|
|
|
|
const codeBlock = document.createElement('pre');
|
|
|
|
const code = document.createElement('code');
|
|
|
|
code.textContent = parts[i];
|
|
|
|
codeBlock.appendChild(code);
|
|
|
|
messageDiv.appendChild(codeBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 02:15:01 -05:00
|
|
|
if (sender === 'user') {
|
|
|
|
messageDiv.classList.add('userMessage');
|
2024-03-09 17:23:16 -05:00
|
|
|
} else {
|
2024-03-09 02:15:01 -05:00
|
|
|
messageDiv.classList.add('assistantMessage');
|
|
|
|
}
|
|
|
|
|
|
|
|
chatHistory.appendChild(messageDiv);
|
|
|
|
chatHistory.scrollTop = chatHistory.scrollHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
const chatForm = document.getElementById('chatForm');
|
|
|
|
const promptInput = document.getElementById('promptInput');
|
2024-03-09 05:43:40 -05:00
|
|
|
const modelSelect = document.getElementById('modelSelect');
|
2024-03-09 03:12:19 -05:00
|
|
|
const sendButton = document.getElementById('sendButton');
|
2024-03-09 15:22:44 -05:00
|
|
|
const stopButton = document.getElementById('stopButton');
|
2024-03-09 05:43:40 -05:00
|
|
|
const modelSelectionContainer = document.getElementById('modelSelectionContainer');
|
2024-03-09 02:15:01 -05:00
|
|
|
|
2024-03-09 05:43:40 -05:00
|
|
|
if (chatForm && promptInput && modelSelect) {
|
2024-03-09 02:15:01 -05:00
|
|
|
chatForm.addEventListener('submit', async (event) => {
|
|
|
|
event.preventDefault();
|
2024-03-09 05:43:40 -05:00
|
|
|
|
2024-03-09 02:15:01 -05:00
|
|
|
const userInput = promptInput.value;
|
2024-03-09 05:43:40 -05:00
|
|
|
const selectedModel = modelSelect.value;
|
2024-03-09 02:15:01 -05:00
|
|
|
promptInput.value = '';
|
2024-03-09 05:43:40 -05:00
|
|
|
modelSelectionContainer.style.display = 'none';
|
2024-03-09 02:15:01 -05:00
|
|
|
|
2024-03-09 03:12:19 -05:00
|
|
|
sendButton.disabled = true;
|
|
|
|
promptInput.disabled = true;
|
2024-03-09 15:22:44 -05:00
|
|
|
stopButton.disabled = false;
|
2024-03-09 03:12:19 -05:00
|
|
|
|
2024-03-09 02:15:01 -05:00
|
|
|
displayMessage(userInput, 'user');
|
|
|
|
|
2024-03-09 03:20:24 -05:00
|
|
|
const typingIndicator = displayTypingIndicator();
|
|
|
|
|
2024-03-09 02:15:01 -05:00
|
|
|
try {
|
2024-03-09 05:43:40 -05:00
|
|
|
const response = await window.electronAPI.sendPrompt(userInput, selectedModel);
|
2024-03-09 05:56:54 -05:00
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
2024-03-09 02:15:01 -05:00
|
|
|
const assistantMessage = response.choices[0].message.content.trim();
|
|
|
|
displayMessage(assistantMessage, 'assistant');
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error sending prompt: ${error.message}`);
|
|
|
|
displayMessage(`Error: ${error.message}`, 'assistant');
|
2024-03-09 03:12:19 -05:00
|
|
|
} finally {
|
2024-03-09 03:20:24 -05:00
|
|
|
typingIndicator.remove();
|
2024-03-09 03:12:19 -05:00
|
|
|
sendButton.disabled = false;
|
|
|
|
promptInput.disabled = false;
|
|
|
|
promptInput.focus();
|
2024-03-09 15:22:44 -05:00
|
|
|
stopButton.disabled = true;
|
2024-03-09 02:15:01 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error('chatForm or promptInput elements not found!');
|
|
|
|
}
|
2024-03-09 03:20:24 -05:00
|
|
|
});
|
|
|
|
|
2024-03-09 15:22:44 -05:00
|
|
|
document.getElementById('stopButton').addEventListener('click', () => {
|
|
|
|
window.electronAPI.abortPrompt();
|
|
|
|
document.getElementById('stopButton').disabled = true;
|
|
|
|
});
|
|
|
|
|
2024-03-09 03:20:24 -05:00
|
|
|
function displayTypingIndicator() {
|
|
|
|
const chatHistory = document.getElementById('chatHistory');
|
|
|
|
const typingIndicator = document.createElement('div');
|
|
|
|
typingIndicator.classList.add('message', 'assistantMessage', 'ellipsis');
|
|
|
|
typingIndicator.textContent = 'Processing';
|
|
|
|
chatHistory.appendChild(typingIndicator);
|
|
|
|
chatHistory.scrollTop = chatHistory.scrollHeight;
|
|
|
|
return typingIndicator;
|
2024-03-09 17:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('keydown', (e) => {
|
|
|
|
if (e.key === 'F11') {
|
|
|
|
e.preventDefault();
|
|
|
|
ipcRenderer.send('toggle-fullscreen');
|
|
|
|
}
|
|
|
|
});
|