-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_area.js
157 lines (136 loc) · 6.1 KB
/
text_area.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
let bot_default_message = `To ensure that messages in the chat interface wrap and display as multiline when the text is too long to fit in one line, you need to update the CSS to allow for word wrapping and handling overflow appropriately.
Here’s how you can adjust the CSS to ensure that messages are displayed in multiple lines within the chat interface: `
// const apiUrl = 'https://192.168.1.44:3000/api/openai/completions'; // API endpoint on your server
const apiUrl = 'http://192.168.1.44:4000/api/openai/completions'; // API endpoint on your server
document.addEventListener('DOMContentLoaded', (globalEvent) => {
const messagesContainer = document.getElementById('messages');
// set message ids
// here we are overwriting the ids in html
let messageElement = document.getElementById('first-message')
let idCounter = 0
for (let messageElement of messagesContainer.children){
messageElement.role = messageElement.classList['2']
messageElement.counter = idCounter
messageElement.name = `${messageElement.role}-${idCounter}`
// autoResizeTextarea()
console.log(messageElement.counter)
console.log(messageElement.role)
console.log(messageElement.name)
idCounter++
}
console.log(idCounter);
console.log(messageElement.name)
console.log(messageElement.contentEditable)
// let messageElement = addMessage('user', 'Type your question'); // add user text area
// messageElement.textContent = 'ass'.repeat(100)
// add evenListener
messageElement.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent the default behavior of adding a new line
logEvent(event.target.textContent.trim());
console.log(`div: ${messageElement.textContent}`)
event.target.blur();
// autoResizeTextarea();
}
});
// console.log(idCounter)
async function logEvent(text){
const target = event.target
// console.log(`**${target.role}-${target.counter} clicked!**`);
if ((target.counter+1)<idCounter && target.role==='user'){ //old and user
// console.log(`old convo ${target.counter}-${idCounter}`);
// bot already exists at target.counter + 1; put text there
const responseGPT = await makeApiCall(target.textContent.trim());
const final_text = responseGPT.message.content.trim();
// const final_text = bot_default_message.trim();
console.log(final_text)
messagesContainer.children[target.counter+1].textContent = final_text // bot textarea; 0 indexing
// autoResizeTextarea(); // readjust the boxes
// messagesContainer.children[target.counter+1].scrollIntoView({ behavior: 'smooth', block: 'end' });
scrollToLatestMessage(messagesContainer.children[target.counter+1]);
// scrollToTopOfLastMessage();
} else if ( target.role==='user') { // latest and user
// console.log(`latest convo ${target.counter}-${idCounter}`);
const responseGPT = await makeApiCall(target.textContent.trim());
const final_text = responseGPT.message.content.trim();
// const final_text = bot_default_message.trim();
// console.log(final_text)
let messageElement = addMessage('bot', final_text); // add bot textarea
// messageElement.scrollIntoView({ behavior: 'smooth', block: 'end' }); // scroll bot
scrollToLatestMessage(messageElement);
messageElement = addMessage('user', ''); // add user text area
// autoResizeTextarea(); // readjust the boxes
messageElement.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent the default behavior of adding a new line
logEvent(event.target.textContent.trim());
event.target.blur();
// autoResizeTextarea(); // readjust the boxes
}
});// listen to user
// console.log(messageElement.name);
// console.log(idCounter);
};
}
function addMessage(sender, text) {
const messageElement = document.createElement('div');
messagesContainer.appendChild(messageElement);
// autoResizeTextarea();
messageElement.classList.add('message', sender);
messageElement.textContent = text;
messageElement.style.width = "100%"
messageElement.style.height = "auto"
messageElement.contentEditable = true
messageElement.role = messageElement.classList['1'];
messageElement.name = `${messageElement.role}-${idCounter}`
messageElement.counter = idCounter;
idCounter++
return messageElement
}
async function makeApiCall(userMessage) {
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: userMessage},
],
model: "gpt-4o-mini",
max_tokens: 100
})
});
const data = await response.json();
console.log(data);
return data
} catch (error) {
console.error('Error:', error);
console.error(error);
}
}
// script.js
function scrollToLatestMessage(latestMessage) {
const chatContainer = document.getElementById('messages');
// Adjust scroll position to show some of the previous messages
const offset = 200; // Adjust this value as needed
console.log(`before scrolltop${chatContainer.scrollTop}`)
chatContainer.scrollTop = latestMessage.offsetTop - offset;
console.log(`after scrolltop${chatContainer.scrollTop}`)
}
function scrollToTopOfLastMessage() {
const lastMessage = messagesContainer.lastElementChild;
// lastMessage.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function showChildren(){
// loop through messagesContainer
console.log('*'.repeat(20))
for (child of messagesContainer.children){
console.log(child.counter)
}
console.log('*'.repeat(20))
//
}
})