+
+ ${item.role == "assistant" ? gpt_image : user_image}
+ ${
+ item.role == "assistant"
+ ? ``
+ : ``
+ }
+
+
+ ${item.role == "assistant" ? markdown.render(item.content) : item.content}
+
+
+ `;
+ }
+
+ document.querySelectorAll(`code`).forEach((el) => {
+ hljs.highlightElement(el);
+ });
+
+ message_box.scrollTo({ top: message_box.scrollHeight, behavior: "smooth" });
+
+ setTimeout(() => {
+ message_box.scrollTop = message_box.scrollHeight;
+ }, 500);
+};
+
+const get_conversation = async (conversation_id) => {
+ let conversation = await JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
+ return conversation.items;
+};
+
+const add_conversation = async (conversation_id, title) => {
+ if (localStorage.getItem(`conversation:${conversation_id}`) == null) {
+ localStorage.setItem(
+ `conversation:${conversation_id}`,
+ JSON.stringify({
+ id: conversation_id,
+ title: title,
+ items: [],
+ })
+ );
+ }
+};
+
+const add_message = async (conversation_id, role, content) => {
+ before_adding = JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
+
+ before_adding.items.push({
+ role: role,
+ content: content,
+ });
+
+ localStorage.setItem(`conversation:${conversation_id}`, JSON.stringify(before_adding)); // update conversation
+};
+
+const load_conversations = async (limit, offset, loader) => {
+ //console.log(loader);
+ //if (loader === undefined) box_conversations.appendChild(spinner);
+
+ let conversations = [];
+ for (let i = 0; i < localStorage.length; i++) {
+ if (localStorage.key(i).startsWith("conversation:")) {
+ let conversation = localStorage.getItem(localStorage.key(i));
+ conversations.push(JSON.parse(conversation));
+ }
+ }
+
+ //if (loader === undefined) spinner.parentNode.removeChild(spinner)
+ await clear_conversations();
+
+ for (conversation of conversations) {
+ box_conversations.innerHTML += `
+