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

@ -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;
}}
/>