diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index fc29651a..67e70c50 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -20,7 +20,6 @@ export let messages = []; $: if (messages && messages.length > 0 && (messages.at(-1).done ?? false)) { - console.log('message done: rendering'); (async () => { await tick(); renderLatex(); @@ -32,7 +31,6 @@ const createCopyCodeBlockButton = () => { // use a class selector if available let blocks = document.querySelectorAll('pre'); - console.log(blocks); blocks.forEach((block) => { // only add button if browser supports Clipboard API @@ -195,8 +193,6 @@ }; const rateMessage = async (messageIdx, rating) => { - const chat = await $db.get('chats', chatId); - messages = messages.map((message, idx) => { if (messageIdx === idx) { message.rating = rating; @@ -204,14 +200,10 @@ return message; }); - await $db.put('chats', { - ...chat, - timestamp: Date.now(), + $db.updateChatById(chatId, { messages: messages, history: history }); - - console.log(messages); }; const showPreviousMessage = async (message) => { diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 97943742..a14855dc 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -21,134 +21,40 @@ let showDropdown = false; - let _chats = $chats.map((item, idx) => $chats[$chats.length - 1 - idx]); - $: if ($chats) { - // Reverse Order - _chats = $chats.map((item, idx) => $chats[$chats.length - 1 - idx]); - } - onMount(async () => { if (window.innerWidth > 1280) { show = true; } - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); + await chats.set(await $db.getChats()); }); const loadChat = async (id) => { goto(`/c/${id}`); - - // const chat = await db.get('chats', id); - // console.log(chat); - // if (chatId !== chat.id) { - // if ('history' in chat && chat.history !== undefined) { - // history = chat.history; - // } else { - // let _history = { - // messages: {}, - // currentId: null - // }; - - // let parentMessageId = null; - // let messageId = null; - - // for (const message of chat.messages) { - // messageId = uuidv4(); - - // if (parentMessageId !== null) { - // _history.messages[parentMessageId].childrenIds = [ - // ..._history.messages[parentMessageId].childrenIds, - // messageId - // ]; - // } - - // _history.messages[messageId] = { - // ...message, - // id: messageId, - // parentId: parentMessageId, - // childrenIds: [] - // }; - - // parentMessageId = messageId; - // } - // _history.currentId = messageId; - - // history = _history; - // } - - // if ('models' in chat && chat.models !== undefined) { - // selectedModels = chat.models ?? selectedModels; - // } else { - // selectedModels = [chat.model ?? '']; - // } - - // console.log(history); - - // title = chat.title; - // chatId = chat.id; - // settings.system = chat.system ?? settings.system; - // settings.temperature = chat.temperature ?? settings.temperature; - // autoScroll = true; - - // await tick(); - - // if (messages.length > 0) { - // history.messages[messages.at(-1).id].done = true; - // } - // } }; const editChatTitle = async (id, _title) => { - const chat = await $db.get('chats', id); - console.log(chat); - - await $db.put('chats', { - ...chat, + await $db.updateChatById(id, { title: _title }); - title = _title; - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); }; const deleteChat = async (id) => { goto('/'); - - const chat = await $db.delete('chats', id); - console.log(chat); - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); + $db.deleteChatById(id); }; const deleteChatHistory = async () => { - const tx = $db.transaction('chats', 'readwrite'); - await Promise.all([tx.store.clear(), tx.done]); - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); + await $db.deleteAllChat(); }; - const importChatHistory = async (chatHistory) => { - for (const chat of chatHistory) { - console.log(chat); - - await $db.put('chats', { - id: chat.id, - model: chat.model, - models: chat.models, - system: chat.system, - options: chat.options, - title: chat.title, - timestamp: chat.timestamp, - messages: chat.messages, - history: chat.history - }); - } - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); - - console.log(chats); + const importChats = async (chatHistory) => { + await $db.addChats(chatHistory); }; - const exportChatHistory = async () => { - await chats.set(await $db.getAllFromIndex('chats', 'timestamp')); - let blob = new Blob([JSON.stringify($chats)], { type: 'application/json' }); + const exportChats = async () => { + let blob = new Blob([JSON.stringify(await $db.exportChats())], { type: 'application/json' }); saveAs(blob, `chat-export-${Date.now()}.json`); }; @@ -159,7 +65,7 @@ reader.onload = (event) => { let chats = JSON.parse(event.target.result); console.log(chats); - importChatHistory(chats); + importChats(chats); }; reader.readAsText(importFiles[0]); @@ -238,7 +144,7 @@
- {#each _chats as chat, i} + {#each $chats as chat, i}