monra commited on
Commit
4c0b081
1 Parent(s): eebc4a2

Fix: codes sent in the question are no longer injected into the page

Browse files
Files changed (1) hide show
  1. client/js/chat.js +60 -24
client/js/chat.js CHANGED
@@ -16,10 +16,6 @@ window.conversation_id = `{{chat_id}}`;
16
 
17
  hljs.addPlugin(new CopyButtonPlugin());
18
 
19
- const format = (text) => {
20
- return text.replace(/(?:\r\n|\r|\n)/g, "<br>");
21
- };
22
-
23
  message_input.addEventListener("blur", () => {
24
  window.scrollTo(0, 0);
25
  });
@@ -72,16 +68,7 @@ const ask_gpt = async (message) => {
72
 
73
  stop_generating.classList.remove(`stop-generating-hidden`);
74
 
75
- message_box.innerHTML += `
76
- <div class="message">
77
- <div class="avatar-container">
78
- ${user_image}
79
- </div>
80
- <div class="content" id="user_${token}">
81
- ${format(message)}
82
- </div>
83
- </div>
84
- `;
85
 
86
  message_box.scrollTop = message_box.scrollHeight;
87
  window.scrollTo(0, 0);
@@ -199,6 +186,25 @@ const ask_gpt = async (message) => {
199
  }
200
  };
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  const decodeUnicode = (str) => {
203
  return str.replace(/\\u([a-fA-F0-9]{4})/g, function (match, grp) {
204
  return String.fromCharCode(parseInt(grp, 16));
@@ -259,16 +265,11 @@ const load_conversation = async (conversation_id) => {
259
  console.log(conversation, conversation_id);
260
 
261
  for (item of conversation.items) {
262
- message_box.innerHTML += `
263
- <div class="message">
264
- <div class="avatar-container">
265
- ${item.role == "assistant" ? gpt_image : user_image}
266
- </div>
267
- <div class="content">
268
- ${item.role == "assistant" ? markdown.render(item.content) : item.content}
269
- </div>
270
- </div>
271
- `;
272
  }
273
 
274
  document.querySelectorAll(`code`).forEach((el) => {
@@ -282,6 +283,41 @@ const load_conversation = async (conversation_id) => {
282
  }, 500);
283
  };
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  const get_conversation = async (conversation_id) => {
286
  let conversation = await JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
287
  return conversation.items;
 
16
 
17
  hljs.addPlugin(new CopyButtonPlugin());
18
 
 
 
 
 
19
  message_input.addEventListener("blur", () => {
20
  window.scrollTo(0, 0);
21
  });
 
68
 
69
  stop_generating.classList.remove(`stop-generating-hidden`);
70
 
71
+ add_user_message_box(message);
 
 
 
 
 
 
 
 
 
72
 
73
  message_box.scrollTop = message_box.scrollHeight;
74
  window.scrollTo(0, 0);
 
186
  }
187
  };
188
 
189
+ const add_user_message_box = (message) => {
190
+ const messageDiv = document.createElement("div");
191
+ messageDiv.classList.add("message");
192
+
193
+ const avatarContainer = document.createElement("div");
194
+ avatarContainer.classList.add("avatar-container");
195
+ avatarContainer.innerHTML = user_image;
196
+
197
+ const contentDiv = document.createElement("div");
198
+ contentDiv.classList.add("content");
199
+ contentDiv.id = `user_${token}`;
200
+ contentDiv.textContent = message;
201
+
202
+ messageDiv.appendChild(avatarContainer);
203
+ messageDiv.appendChild(contentDiv);
204
+
205
+ message_box.appendChild(messageDiv);
206
+ };
207
+
208
  const decodeUnicode = (str) => {
209
  return str.replace(/\\u([a-fA-F0-9]{4})/g, function (match, grp) {
210
  return String.fromCharCode(parseInt(grp, 16));
 
265
  console.log(conversation, conversation_id);
266
 
267
  for (item of conversation.items) {
268
+ if (is_assistant(item.role)) {
269
+ message_box.innerHTML += load_gpt_message_box(item.content);
270
+ } else {
271
+ message_box.innerHTML += load_user_message_box(item.content);
272
+ }
 
 
 
 
 
273
  }
274
 
275
  document.querySelectorAll(`code`).forEach((el) => {
 
283
  }, 500);
284
  };
285
 
286
+ const load_user_message_box = (content) => {
287
+ const messageDiv = document.createElement("div");
288
+ messageDiv.classList.add("message");
289
+
290
+ const avatarContainer = document.createElement("div");
291
+ avatarContainer.classList.add("avatar-container");
292
+ avatarContainer.innerHTML = user_image;
293
+
294
+ const contentDiv = document.createElement("div");
295
+ contentDiv.classList.add("content");
296
+ contentDiv.textContent = content;
297
+
298
+ messageDiv.appendChild(avatarContainer);
299
+ messageDiv.appendChild(contentDiv);
300
+
301
+ return messageDiv.outerHTML;
302
+ };
303
+
304
+ const load_gpt_message_box = (content) => {
305
+ return `
306
+ <div class="message">
307
+ <div class="avatar-container">
308
+ ${gpt_image}
309
+ </div>
310
+ <div class="content">
311
+ ${markdown.render(content)}
312
+ </div>
313
+ </div>
314
+ `;
315
+ };
316
+
317
+ const is_assistant = (role) => {
318
+ return role == "assistant";
319
+ };
320
+
321
  const get_conversation = async (conversation_id) => {
322
  let conversation = await JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
323
  return conversation.items;