mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-24 03:41:56 +03:00
a4a2d769b3
* 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
42 lines
967 B
TypeScript
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,
|
|
};
|
|
};
|