-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
312 lines (258 loc) · 11.4 KB
/
home.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const chatBox = document.getElementById('chat-box');
const inputField = document.getElementById('input-field');
const questionForm = document.getElementById('question_form');
const $intro_bot = document.getElementById('intro_bot')
const $intro_user = document.getElementById('intro_user')
const characters = [{
name: 'Jane',
gender: 'female',
relationship: 'colleague',
interests: ['hiking', 'cats'],
hates: 'politics',
talking_context: 'at coffee break',
spark_for: 'casual chat starter',
sell_my: 'joke about job',
next_step: 'spend relaxed lunchtime',
other_info: 'has a daughter in primary school',
my_goal: 'get more information on how other people are performing',
tone: 'casual'
}, {
name: 'John',
gender: 'male',
relationship: 'people with the same passion as me',
interests: ['footabll', 'dogs'],
hates: 'social',
talking_context: 'in washroom',
spark_for: 'personal greeting',
sell_my: 'my passion and knowledge about sports',
next_step: 'join my project team',
other_info: 'have a lot of knowledge on debugging',
my_goal: 'work with him and learn from his expereience',
tone: 'relaxed and considerate'
},
{
name: 'Jose',
gender: 'neutral',
relationship: 'mentor',
interests: ['investment', 'cars'],
hates: 'travel',
talking_context: 'meeting',
spark_for: 'opening remark for team presentation',
sell_my: 'the quality and high aim of our team work',
next_step: 'give positive comments',
other_info: 'forgets things easily unless reminded repeatedly',
my_goal: 'get higher marks in performance evaluation',
tone: 'confident and ambitious'
}, {
name: 'Jess',
gender: 'female',
relationship: 'LinkedIn contact',
interests: ['coding', 'start-ups'],
hates: 'gossip',
talking_context: 'online',
spark_for: 'LinkedIn message',
sell_my: 'my euthusiasm on work',
next_step: 'have a coffee chat',
other_info: 'likes to keep distance until she finds common interest with others',
my_goal: 'gather some information about her department and business, and know if they may have head count for hiring next quarter',
tone: 'respectful and passionate'
}
]
//add new char
// Load characters from localStorage and append to characters array
var new_characters = [];
// Retrieve new_characters array from localStorage
var newCharacters = JSON.parse(localStorage.getItem('new_characters'));
// Check if newCharacters is not null or undefined
if (newCharacters !== null && newCharacters !== undefined) {
// Append newCharacters array to characters array
new_characters = new_characters.concat(newCharacters);
// Loop through newCharacters array and append grid cards to grid container
for (var i = 0; i < newCharacters.length; i++) {
var new_character = newCharacters[i];
var gridCard = document.createElement('div');
gridCard.className = 'grid_card';
var avatarSrc = new_character.gender === 'Male' ? 'image/avatar9.png' : 'image/avatar8.png';
gridCard.innerHTML = `
<img src="${avatarSrc}" alt="${new_character.name}" class="avatar">
<div class="infowrapper">
<p class="char_name">${new_character.name}</p>
<button class="delete_button" data-index="${i}">X</button>
</div>
`;
var gridContainer = document.querySelector('.grid_container');
var lastGridCard = gridContainer.lastElementChild;
gridContainer.insertBefore(gridCard, lastGridCard);
// Add click event listener to delete button
var deleteButton = gridCard.querySelector('.delete_button');
deleteButton.addEventListener('click', function (event) {
var index = event.target.dataset.index;
// Remove grid card from grid container
event.target.parentElement.parentElement.remove();
// Remove corresponding character from characters array
new_characters.splice(index, 1);
// Update characters array in local storage
localStorage.setItem('new_characters', JSON.stringify(new_characters));
// Log the updated characters array for testing
console.log(new_characters);
});
}
}
// Log the characters array for testing
console.log(characters);
// Function to generate a message using character information
function generateMessage(character) {
if (character.talking_context) {
const personal_data = `${character.name} is ${character.gender} and is my ${character.relationship}. ${character.name} is interested in ${character.interests.join(', ')}. Normally, ${character.name} hates ${character.hates}. In addition, ${character.name} ${character.other_info}. Today I will meet and greet this person at ${character.talking_context}. Based on the given information, if you were me, what would you say in this circumstance? `
return personal_data
} else {
// const personal_data = character.name + 'is a ' + character.gender + ' gender ' + ' and is my ' + character.relationship + '.' + character.name + ' is interested in ' + character.interests.join(', ') + '.' + ' However, ' + character.name + ' hates ' + character.hates + '. ' + "In addition, " + character.name + ' does ' + character.other_info + '.' + 'One day I meet this guy at ' + '' + ' based on the given information, '
const personal_data = `${character.name} is ${character.gender} and is my ${character.relationship}. ${character.name} is interested in ${character.interests.join(', ')}. Normally, ${character.name} hates ${character.hates}. In addition, ${character.name} ${character.other_info}. Today I will meet and greet this person. Based on the given information, if you were me, what would you say in this circumstance? `
return personal_data
}
}
document.querySelector('.grid_container').addEventListener('click', function (event) {
var target = event.target;
// Check if the clicked element has the class "grid_card"
if (target.classList.contains('grid_card')) {
// Remove bold class from all grid cards
var gridCards = document.querySelectorAll('.grid_card');
gridCards.forEach(function (card) {
card.classList.remove('bold');
});
// Add bold class to the clicked grid card
target.classList.add('bold');
// Get the name of the clicked character from either the "characters" or "new_characters" array
var name;
if (characters.some(function (character) {
return character.name === target.querySelector('.char_name').textContent;
})) {
name = target.querySelector('.char_name').textContent;
} else if (new_characters.some(function (character) {
return character.name === target.querySelector('.char_name').textContent;
})) {
name = target.querySelector('.char_name').textContent;
}
// Generate a message using the retrieved character's information
if (name) {
var character = characters.find(function (character) {
return character.name === name;
}) || new_characters.find(function (character) {
return character.name === name;
});
personal_data = generateMessage(character); // Assign the returned message to the global variable
console.log(personal_data);
// Remove the existing record from local storage
localStorage.removeItem('personaldata');
// Get the existing "personaldata" record from local storage and parse it as an array
var existingPersonalData = localStorage.getItem('personaldata');
var personalDataArray = existingPersonalData ? JSON.parse(existingPersonalData) : [];
// Push the new record into the personal data array
personalDataArray = personalDataArray.concat(personal_data);
// Convert the personal data array back to a string and store it in local storage
localStorage.setItem('personaldata', JSON.stringify(personalDataArray));
}
}
});
//function to assemble quetion to send GPT and retrieve response
function delete_intro() {
if ($intro_bot && $intro_user) {
}
$intro_bot.remove()
$intro_user.remove()
}
questionForm.addEventListener('submit', async (event) => {
event.preventDefault();
delete_intro()
const existingPersonalData = localStorage.getItem('personaldata') || '';
const message = existingPersonalData + ' ' + inputField.value;
const input = inputField.value
inputField.value = '';
console.log(message);
displayMessage('user', input);
const messageElem = document.createElement('div');
messageElem.classList.add('message', 'bot');
messageElem.textContent = 'Reply to be displayed...'
chatBox.appendChild(messageElem);
const response = await sendMessage(message)
displayMessage('bot', response.message);
localStorage.removeItem('personaldata');
});
async function sendMessage(message) {
const response = await fetch('http://localhost:3000/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message })
});
const data = await response.json();
return data;
}
function displayMessage(role, content) {
const messageElem_block = document.createElement('div')
// bot msg
const bot_logo_image = document.createElement('img')
bot_logo_image.setAttribute('class', 'logo_image')
bot_logo_image.setAttribute('src', 'image/Introvert copilot Logo smaller.png')
bot_logo_image.setAttribute('alt', 'Introvert Copilot')
const $save_button = document.createElement('button')
$save_button.classList.add('save_spark')
$save_button.type = 'button'
$save_button.textContent = 'Save'
//user msg
const user_logo_image = document.createElement('img')
user_logo_image.setAttribute('class', 'logo_image')
user_logo_image.setAttribute('src', 'image/user.png')
user_logo_image.setAttribute('alt', "'user's avatar'")
const messageElem = document.createElement('div');
messageElem.classList.add('message', role);
chatBox.appendChild(messageElem_block);
// If role is "bot", update message with actual content when response is received
if (role === 'bot') {
sendMessage(content)
.then(response => {
const botMessages = document.querySelectorAll(".message.bot");
const secondToLastBotMessage = botMessages[botMessages.length - 2];
secondToLastBotMessage.remove()
messageElem.textContent = response.message;
// console.log(response.message);
})
.catch(error => {
console.log('Error fetching response:', error);
});
messageElem_block.append(bot_logo_image)
messageElem_block.append(messageElem)
messageElem_block.append($save_button)
messageElem_block.classList.add('aspark')
}
//role = 'user'
else {
messageElem.textContent = content;
messageElem_block.append(user_logo_image)
messageElem_block.append(messageElem)
messageElem_block.classList.add('afollowup')
}
}
//dark mode
const darkModeButton = document.getElementById('mymode');
const htmlRoot = document.querySelector('html');
darkModeButton.addEventListener('click', () => {
if (htmlRoot.getAttribute('data-bs-theme') === 'dark') {
htmlRoot.removeAttribute('data-bs-theme');
darkModeButton.textContent = 'Dark mode'
} else {
htmlRoot.setAttribute('data-bs-theme', 'dark');
darkModeButton.textContent = 'Light mode'
}
});
//store saved msg into localstorage
document.getElementById('chat-box').addEventListener('click', function (event) {
if (event.target.classList.contains('save_spark')) {
const botDiv = event.target.previousElementSibling;
const botText = botDiv.textContent.trim();
let savedMsgs = JSON.parse(localStorage.getItem('savedMsgs')) || [];
savedMsgs.push(botText);
localStorage.setItem('savedMsgs', JSON.stringify(savedMsgs));
}
});