mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-17 11:21:35 +03:00
6f047f4a39
* feat: streaming for standard brain picking * fix(bug): private llm * wip: test Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com> * wip: almost good Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com> * feat: useFetch * chore: remove 💀 * chore: fix linting * fix: forward the request if not streaming * feat: streaming for standard brain picking * fix(bug): private llm * wip: test Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com> * wip: almost good Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com> * feat: useFetch * chore: remove 💀 * chore: fix linting * fix: forward the request if not streaming * fix: 💀 code * fix: check_user_limit * feat: brain_id to new chat stream * fix: missing imports * feat: message_id created on backend Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com> * chore: remove dead * remove: cpython * remove: dead --------- Co-authored-by: Mamadou DICKO <mamadoudicko@users.noreply.github.com>
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useContext, useState } from "react";
|
|
|
|
import { ChatHistory } from "../types";
|
|
|
|
type ChatContextProps = {
|
|
history: ChatHistory[];
|
|
setHistory: (history: ChatHistory[]) => void;
|
|
addToHistory: (message: ChatHistory) => void;
|
|
updateHistory: (chat: ChatHistory) => void;
|
|
updateStreamingHistory: (streamedChat: ChatHistory) => void;
|
|
};
|
|
|
|
export const ChatContext = createContext<ChatContextProps | undefined>(
|
|
undefined
|
|
);
|
|
|
|
export const ChatProvider = ({
|
|
children,
|
|
}: {
|
|
children: JSX.Element | JSX.Element[];
|
|
}): JSX.Element => {
|
|
const [history, setHistory] = useState<ChatHistory[]>([]);
|
|
|
|
const addToHistory = (message: ChatHistory) => {
|
|
setHistory((prevHistory) => [...prevHistory, message]);
|
|
};
|
|
|
|
const updateStreamingHistory = (streamedChat: ChatHistory): void => {
|
|
setHistory((prevHistory: ChatHistory[]) => {
|
|
console.log("new chat", streamedChat);
|
|
const updatedHistory = prevHistory.find(
|
|
(item) => item.message_id === streamedChat.message_id
|
|
)
|
|
? prevHistory.map((item: ChatHistory) =>
|
|
item.message_id === streamedChat.message_id
|
|
? { ...item, assistant: item.assistant + streamedChat.assistant }
|
|
: item
|
|
)
|
|
: [...prevHistory, streamedChat];
|
|
|
|
console.log("updated history", updatedHistory);
|
|
|
|
return updatedHistory;
|
|
});
|
|
};
|
|
|
|
const updateHistory = (chat: ChatHistory): void => {
|
|
setHistory((prevHistory: ChatHistory[]) => {
|
|
const updatedHistory = prevHistory.find(
|
|
(item) => item.message_id === chat.message_id
|
|
)
|
|
? prevHistory.map((item: ChatHistory) =>
|
|
item.message_id === chat.message_id
|
|
? { ...item, assistant: chat.assistant }
|
|
: item
|
|
)
|
|
: [...prevHistory, chat];
|
|
|
|
return updatedHistory;
|
|
});
|
|
};
|
|
|
|
return (
|
|
<ChatContext.Provider
|
|
value={{
|
|
history,
|
|
setHistory,
|
|
addToHistory,
|
|
updateHistory,
|
|
updateStreamingHistory,
|
|
}}
|
|
>
|
|
{children}
|
|
</ChatContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useChatContext = (): ChatContextProps => {
|
|
const context = useContext(ChatContext);
|
|
|
|
if (context === undefined) {
|
|
throw new Error("useChatContext must be used inside ChatProvider");
|
|
}
|
|
|
|
return context;
|
|
};
|