Download src/lib/storage.ts from IOE-IFAD/test: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/lib/storage.ts
- Command line
-
hf download hf://spaces/IOE-IFAD/test/src/lib/storage.ts
-
curl -L -o storage.ts https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/lib/storage.ts
3.06 kB
| 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, | |
| }; | |