File size: 989 Bytes
c59d808 |
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 |
import { deleteChat, getStoredChats, saveChat } from "@/lib/chat";
import { UIMessage, UIDataTypes, UITools } from "ai";
import { create } from "zustand";
interface ChatState {
messages: Record<string, UIMessage<unknown, UIDataTypes, UITools>[]>;
addMessages: (
chatId: string,
msgs: UIMessage<unknown, UIDataTypes, UITools>[]
) => void;
deleteChat: (chatId: string) => void;
}
const useChatStore = create<ChatState>((set) => ({
messages: getStoredChats(),
addMessages: (
chatId: string,
msgs: UIMessage<unknown, UIDataTypes, UITools>[] = []
) => {
if (!chatId) return;
saveChat(chatId, msgs);
set((state) => ({
messages: {
...state.messages,
[chatId]: msgs,
},
}));
},
deleteChat: (chatId: string) => {
if (!chatId) return;
deleteChat(chatId);
set((state) => {
const { [chatId]: _, ...rest } = state.messages;
return { messages: rest };
});
},
}));
export default useChatStore;
|