mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-17 11:21:35 +03:00
29da8c70b0
# Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useState } from "react";
|
|
|
|
import { ChatMessage, Notification } from "@/app/chat/[chatId]/types";
|
|
|
|
import { ChatContextProps } from "./types";
|
|
|
|
export const ChatContext = createContext<ChatContextProps | undefined>(
|
|
undefined
|
|
);
|
|
|
|
export const ChatProvider = ({
|
|
children,
|
|
}: {
|
|
children: JSX.Element | JSX.Element[];
|
|
}): JSX.Element => {
|
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
|
const [notifications, setNotifications] = useState<Notification[]>([]);
|
|
const [sourcesMessageIndex, setSourcesMessageIndex] = useState<
|
|
number | undefined
|
|
>(undefined);
|
|
|
|
const updateStreamingHistory = (streamedChat: ChatMessage): void => {
|
|
setMessages((prevHistory: ChatMessage[]) => {
|
|
const updatedHistory = prevHistory.find(
|
|
(item) => item.message_id === streamedChat.message_id
|
|
)
|
|
? prevHistory.map((item: ChatMessage) =>
|
|
item.message_id === streamedChat.message_id
|
|
? {
|
|
...item,
|
|
assistant: item.assistant + streamedChat.assistant,
|
|
metadata: streamedChat.metadata,
|
|
}
|
|
: item
|
|
)
|
|
: [...prevHistory, streamedChat];
|
|
|
|
return updatedHistory;
|
|
});
|
|
};
|
|
|
|
const removeMessage = (id: string): void => {
|
|
setMessages((prevHistory: ChatMessage[]) =>
|
|
prevHistory.filter((item) => item.message_id !== id)
|
|
);
|
|
};
|
|
|
|
return (
|
|
<ChatContext.Provider
|
|
value={{
|
|
messages,
|
|
setMessages,
|
|
updateStreamingHistory,
|
|
removeMessage,
|
|
notifications,
|
|
setNotifications,
|
|
sourcesMessageIndex,
|
|
setSourcesMessageIndex,
|
|
}}
|
|
>
|
|
{children}
|
|
</ChatContext.Provider>
|
|
);
|
|
};
|