feat: chat history db code refac for backend support

This commit is contained in:
Timothy J. Baek 2023-11-21 09:39:11 -08:00
parent 14fb5a9b5c
commit c7b1fd1cd1
5 changed files with 111 additions and 178 deletions

View file

@ -3,7 +3,7 @@
import { onMount, tick } from 'svelte';
import { goto } from '$app/navigation';
import { config, user, showSettings, settings, models, db } from '$lib/stores';
import { config, user, showSettings, settings, models, db, chats } from '$lib/stores';
import SettingsModal from '$lib/components/chat/SettingsModal.svelte';
import Sidebar from '$lib/components/layout/Sidebar.svelte';
@ -77,7 +77,7 @@
};
const getDB = async () => {
return await openDB('Chats', 1, {
const _db = await openDB('Chats', 1, {
upgrade(db) {
const store = db.createObjectStore('chats', {
keyPath: 'id',
@ -86,6 +86,63 @@
store.createIndex('timestamp', 'timestamp');
}
});
return {
db: _db,
getChatById: async function (id) {
return await this.db.get('chats', id);
},
getChats: async function () {
let chats = await this.db.getAllFromIndex('chats', 'timestamp');
chats = chats.map((item, idx) => ({
title: chats[chats.length - 1 - idx].title,
id: chats[chats.length - 1 - idx].id
}));
return chats;
},
exportChats: async function () {
let chats = await this.db.getAllFromIndex('chats', 'timestamp');
chats = chats.map((item, idx) => chats[chats.length - 1 - idx]);
return chats;
},
addChats: async function (_chats) {
for (const chat of _chats) {
console.log(chat);
await this.addChat(chat);
}
await chats.set(await this.db.getChats());
},
addChat: async function (chat) {
await this.db.put('chats', {
...chat
});
},
createNewChat: async function (chat) {
await this.addChat({ ...chat, timestamp: Date.now() });
await chats.set(await this.db.getChats());
},
updateChatById: async function (id, updated) {
const chat = await this.getChatById(id);
await this.db.put('chats', {
...chat,
...updated,
timestamp: Date.now()
});
await chats.set(await this.db.getChats());
},
deleteChatById: async function (id) {
await this.db.delete('chats', id);
await chats.set(await this.db.getChats());
},
deleteAllChat: async function () {
const tx = this.db.transaction('chats', 'readwrite');
await Promise.all([tx.store.clear(), tx.done]);
await chats.set(await this.db.getChats());
}
};
};
onMount(async () => {

View file

@ -71,8 +71,6 @@
//////////////////////////
const sendPrompt = async (userPrompt, parentId) => {
await chats.set(await $db.getAllFromIndex('chats', 'timestamp'));
await Promise.all(
selectedModels.map(async (model) => {
if (model.includes('gpt-')) {
@ -83,9 +81,7 @@
})
);
await chats.set(await $db.getAllFromIndex('chats', 'timestamp'));
console.log(history);
await chats.set(await $db.getChats());
};
const sendPromptOllama = async (model, userPrompt, parentId) => {
@ -189,8 +185,7 @@
window.scrollTo({ top: document.body.scrollHeight });
}
await $db.put('chats', {
id: $chatId,
await $db.updateChatById($chatId, {
title: title === '' ? 'New Chat' : title,
models: selectedModels,
system: $settings.system ?? undefined,
@ -202,8 +197,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -316,11 +310,9 @@
window.scrollTo({ top: document.body.scrollHeight });
}
await $db.put('chats', {
id: $chatId,
await $db.updateChatById($chatId, {
title: title === '' ? 'New Chat' : title,
models: selectedModels,
system: $settings.system ?? undefined,
options: {
seed: $settings.seed ?? undefined,
@ -330,8 +322,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -344,7 +335,6 @@
if (messages.length == 2) {
window.history.replaceState(history.state, '', `/c/${$chatId}`);
await setChatTitle($chatId, userPrompt);
}
}
@ -362,7 +352,6 @@
document.getElementById('chat-textarea').style.height = '';
let userMessageId = uuidv4();
let userMessage = {
id: userMessageId,
parentId: messages.length !== 0 ? messages.at(-1).id : null,
@ -381,7 +370,7 @@
prompt = '';
if (messages.length == 0) {
await $db.put('chats', {
await $db.createNewChat({
id: $chatId,
title: 'New Chat',
models: selectedModels,
@ -394,8 +383,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -459,9 +447,8 @@
};
const setChatTitle = async (_chatId, _title) => {
const chat = await $db.get('chats', _chatId);
await $db.put('chats', { ...chat, title: _title });
if (chat.id === $chatId) {
await $db.updateChatById(_chatId, { title: _title });
if (_chatId === $chatId) {
title = _title;
}
};
@ -469,7 +456,6 @@
<svelte:window
on:scroll={(e) => {
console.log(e);
autoScroll = window.innerHeight + window.scrollY >= document.body.offsetHeight - 40;
}}
/>

View file

@ -42,21 +42,27 @@
messages = _messages;
}
onMount(async () => {
let chat = await loadChat();
// onMount(async () => {
// let chat = await loadChat();
await tick();
if (chat) {
loaded = true;
} else {
await goto('/');
}
});
// await tick();
// if (chat) {
// loaded = true;
// } else {
// await goto('/');
// }
// });
$: if ($page.params.id) {
console.log($page.params.id);
(async () => {
await loadChat();
let chat = await loadChat();
await tick();
if (chat) {
loaded = true;
} else {
await goto('/');
}
})();
}
@ -66,7 +72,7 @@
const loadChat = async () => {
await chatId.set($page.params.id);
const chat = await $db.get('chats', $chatId);
const chat = await $db.getChatById($chatId);
if (chat) {
console.log(chat);
@ -76,10 +82,8 @@
(chat?.history ?? undefined) !== undefined
? chat.history
: convertMessagesToHistory(chat.messages);
console.log(history);
title = chat.title;
await settings.set({
...$settings,
system: chat.system ?? $settings.system,
@ -104,8 +108,6 @@
//////////////////////////
const sendPrompt = async (userPrompt, parentId) => {
await chats.set(await $db.getAllFromIndex('chats', 'timestamp'));
await Promise.all(
selectedModels.map(async (model) => {
if (model.includes('gpt-')) {
@ -116,9 +118,7 @@
})
);
await chats.set(await $db.getAllFromIndex('chats', 'timestamp'));
console.log(history);
await chats.set(await $db.getChats());
};
const sendPromptOllama = async (model, userPrompt, parentId) => {
@ -222,8 +222,7 @@
window.scrollTo({ top: document.body.scrollHeight });
}
await $db.put('chats', {
id: $chatId,
await $db.updateChatById($chatId, {
title: title === '' ? 'New Chat' : title,
models: selectedModels,
system: $settings.system ?? undefined,
@ -235,8 +234,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -247,6 +245,7 @@
}
if (messages.length == 2 && messages.at(1).content !== '') {
window.history.replaceState(history.state, '', `/c/${$chatId}`);
await generateChatTitle($chatId, userPrompt);
}
};
@ -348,11 +347,9 @@
window.scrollTo({ top: document.body.scrollHeight });
}
await $db.put('chats', {
id: $chatId,
await $db.updateChatById($chatId, {
title: title === '' ? 'New Chat' : title,
models: selectedModels,
system: $settings.system ?? undefined,
options: {
seed: $settings.seed ?? undefined,
@ -362,8 +359,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -375,6 +371,7 @@
}
if (messages.length == 2) {
window.history.replaceState(history.state, '', `/c/${$chatId}`);
await setChatTitle($chatId, userPrompt);
}
}
@ -392,7 +389,6 @@
document.getElementById('chat-textarea').style.height = '';
let userMessageId = uuidv4();
let userMessage = {
id: userMessageId,
parentId: messages.length !== 0 ? messages.at(-1).id : null,
@ -411,7 +407,7 @@
prompt = '';
if (messages.length == 0) {
await $db.put('chats', {
await $db.createNewChat({
id: $chatId,
title: 'New Chat',
models: selectedModels,
@ -424,8 +420,7 @@
top_p: $settings.top_p ?? undefined
},
messages: messages,
history: history,
timestamp: Date.now()
history: history
});
}
@ -489,9 +484,8 @@
};
const setChatTitle = async (_chatId, _title) => {
const chat = await $db.get('chats', _chatId);
await $db.put('chats', { ...chat, title: _title });
if (chat.id === $chatId) {
await $db.updateChatById(_chatId, { title: _title });
if (_chatId === $chatId) {
title = _title;
}
};
@ -499,8 +493,6 @@
<svelte:window
on:scroll={(e) => {
console.log(e);
autoScroll = window.innerHeight + window.scrollY >= document.body.offsetHeight - 40;
}}
/>