quivr/frontend/app/chat/[chatId]/hooks/useHandleStream.ts
Mamadou DICKO a4a2d769b3
feat: allow setting public brain status to private (#1258)
* feat: refetch brains list on when new brain is added

* feat: update BrainConfig type

* feat: update useSettingsTab add usebrainFormState and useSettings tab

* feat: add <PrivateAccessConfirmationModal/> modal

* feat: update translations

* feat: handle brain status change to private

* feat: validate chat access

* test: fix failaing tests and remove deprecated
2023-09-26 10:35:52 +02:00

42 lines
967 B
TypeScript

import { useChatContext } from "@/lib/context";
import { ChatMessage } from "../types";
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useHandleStream = () => {
const { updateStreamingHistory } = useChatContext();
const handleStream = async (
reader: ReadableStreamDefaultReader<Uint8Array>
): Promise<void> => {
const decoder = new TextDecoder("utf-8");
const handleStreamRecursively = async () => {
const { done, value } = await reader.read();
if (done) {
return;
}
const dataStrings = decoder
.decode(value)
.trim()
.split("data: ")
.filter(Boolean);
dataStrings.forEach((data) => {
const parsedData = JSON.parse(data) as ChatMessage;
updateStreamingHistory(parsedData);
});
await handleStreamRecursively();
};
await handleStreamRecursively();
};
return {
handleStream,
};
};