File size: 3,056 Bytes
e454383 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | import vault from "vault-storage";
import type { Chat, Message } from "@/lib/types";
const initializeChats = async () => {
const newChatHistory = new Array<Chat>();
await vault.setItem("chats", newChatHistory);
return newChatHistory;
};
const getAllChats: () => Promise<Chat[]> = async () => {
let chats = await vault.getItem("chats");
if (!chats) {
chats = await initializeChats();
}
return chats;
};
const readChat = async (id: string): Promise<Chat | undefined> => {
const chats = await getAllChats();
return chats.find((chat) => chat.id === id);
};
const createChat = async (
title: string,
messages: Array<Message>
): Promise<Chat> => {
const chats = await getAllChats();
const newChat: Chat = {
id: Date.now().toString(),
title,
messages,
date: new Date().toDateString(),
};
chats.push(newChat);
await vault.setItem("chats", chats);
return newChat;
};
const updateChat = async (
id: string,
updates: Partial<Chat>
): Promise<Chat | undefined> => {
const chats = await getAllChats();
const index = chats.findIndex((chat) => chat.id === id);
if (index === -1) return undefined;
chats[index] = { ...chats[index], ...updates };
await vault.setItem("chats", chats);
return chats[index];
};
const deleteChat = async (id: string): Promise<boolean> => {
const chats = await getAllChats();
const filteredChats = chats.filter((chat) => chat.id !== id);
if (filteredChats.length === chats.length) return false;
await vault.setItem("chats", filteredChats);
return true;
};
const deleteAllChats = async (): Promise<void> => {
await initializeChats();
};
const addMessage = async (
chatId: string,
message: Omit<Message, "id">
): Promise<Chat | undefined> => {
const chats = await getAllChats();
const chatIndex = chats.findIndex((chat) => chat.id === chatId);
if (chatIndex === -1) return undefined;
const id = Date.now().toString();
const newMessage: Message = {
...message,
id: id,
};
chats[chatIndex].messages.push(newMessage);
await vault.setItem("chats", chats);
return chats[chatIndex];
};
const removeMessage = async (
chatId: string,
messageId: string
): Promise<Chat | undefined> => {
const chats = await getAllChats();
const chatIndex = chats.findIndex((chat) => chat.id === chatId);
if (chatIndex === -1) return undefined;
const messageIndex = chats[chatIndex].messages.findIndex(
(message) => message.id === messageId
);
if (messageIndex === -1) return undefined;
chats[chatIndex].messages.splice(messageIndex, 1);
await vault.setItem("chats", chats);
return chats[chatIndex];
};
const updateChatName = async (
chatId: string,
name: string
): Promise<boolean> => {
const chat = await readChat(chatId);
if (!chat) return false;
const updatedChat = { ...chat, title: name };
await updateChat(chatId, updatedChat);
return true;
};
export {
getAllChats,
createChat,
readChat,
updateChat,
deleteChat,
deleteAllChats,
addMessage,
updateChatName,
removeMessage,
};
|