forked from StanGirard/ChatGPT-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
70 lines (56 loc) · 2.11 KB
/
ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const {
addUserMessageToChat,
addAssistantMessageToChat,
createNewChatSession,
} = require('./chat');
const { setAPIKey } = require('./api');
function saveAPIKeyToLocalStorage(apiKey) {
localStorage.setItem('apiKey', apiKey);
}
function initUI() {
const modelSelect = document.getElementById('model-select');
const apiKeyInput = document.getElementById('api-key-input');
const apiKeySubmit = document.getElementById('api-key-submit');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
const chatContainer = document.getElementById('chat-container');
const newChatSessionButton = document.getElementById('new-chat-session-button');
apiKeySubmit.addEventListener('click', () => {
const apiKey = apiKeyInput.value.trim();
if (!apiKey) return;
setAPIKey(apiKey);
saveAPIKeyToLocalStorage(apiKey);
// Show a success message or any other indication that the key is set
alert('API key set successfully');
});
sendButton.addEventListener('click', async () => {
const message = userInput.value;
if (!message) return;
// Clear the input field
userInput.value = '';
// Add user message to chat container and message history
addUserMessageToChat(message);
// Get assistant message
const assistantMessage = await addAssistantMessageToChat(message);
if (!assistantMessage) {
alert('An error occurred while calling the OpenAI API. Check the console for details.');
}
});
newChatSessionButton.addEventListener('click', () => {
createNewChatSession();
});
userInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
if (!event.shiftKey) {
event.preventDefault(); // Prevent newline from being added to the input
sendButton.click();
} else {
userInput.value += '\n';
event.preventDefault();
}
}
});
}
module.exports = {
initUI,
};