2023-06-30 11:10:59 +03:00
|
|
|
/* eslint-disable max-lines */
|
|
|
|
|
2023-07-05 14:39:07 +03:00
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
2023-06-28 20:39:27 +03:00
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
2023-07-05 10:30:22 +03:00
|
|
|
import { useChatContext } from "@/lib/context/ChatProvider/hooks/useChatContext";
|
2023-07-05 19:33:18 +03:00
|
|
|
import { useFetch } from "@/lib/hooks";
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-07-05 14:33:26 +03:00
|
|
|
import { ChatHistory, ChatQuestion } from "../types";
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
interface UseChatService {
|
|
|
|
addQuestion: (chatId: string, chatQuestion: ChatQuestion) => Promise<void>;
|
|
|
|
addStreamQuestion: (
|
|
|
|
chatId: string,
|
|
|
|
chatQuestion: ChatQuestion
|
|
|
|
) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
export const useQuestion = (): UseChatService => {
|
2023-06-30 11:10:59 +03:00
|
|
|
const { fetchInstance } = useFetch();
|
|
|
|
const { updateHistory, updateStreamingHistory } = useChatContext();
|
2023-06-28 20:39:27 +03:00
|
|
|
const { currentBrain } = useBrainContext();
|
2023-07-05 14:39:07 +03:00
|
|
|
const { addQuestion } = useChatApi();
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-07-05 14:39:07 +03:00
|
|
|
const addQuestionHandler = async (
|
2023-06-30 11:10:59 +03:00
|
|
|
chatId: string,
|
|
|
|
chatQuestion: ChatQuestion
|
|
|
|
): Promise<void> => {
|
2023-07-05 14:39:07 +03:00
|
|
|
const response = await addQuestion({
|
|
|
|
chatId,
|
2023-07-14 22:02:26 +03:00
|
|
|
brainId: currentBrain?.id ?? "",
|
2023-07-05 14:39:07 +03:00
|
|
|
chatQuestion,
|
|
|
|
});
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-07-05 14:39:07 +03:00
|
|
|
updateHistory(response);
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
2023-06-30 11:10:59 +03:00
|
|
|
|
|
|
|
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) => {
|
|
|
|
try {
|
|
|
|
const parsedData = JSON.parse(data) as ChatHistory;
|
|
|
|
updateStreamingHistory(parsedData);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error parsing data:", error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await handleStreamRecursively();
|
|
|
|
};
|
|
|
|
|
|
|
|
await handleStreamRecursively();
|
|
|
|
};
|
|
|
|
|
|
|
|
const addStreamQuestion = async (
|
2023-06-22 18:50:06 +03:00
|
|
|
chatId: string,
|
|
|
|
chatQuestion: ChatQuestion
|
2023-06-30 11:10:59 +03:00
|
|
|
): Promise<void> => {
|
2023-06-28 20:39:27 +03:00
|
|
|
if (currentBrain?.id === undefined) {
|
|
|
|
throw new Error("No current brain");
|
|
|
|
}
|
2023-06-30 11:10:59 +03:00
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "text/event-stream",
|
|
|
|
};
|
|
|
|
const body = JSON.stringify(chatQuestion);
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
try {
|
|
|
|
const response = await fetchInstance.post(
|
|
|
|
`/chat/${chatId}/question/stream?brain_id=${currentBrain.id}`,
|
|
|
|
body,
|
|
|
|
headers
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.body === null) {
|
|
|
|
throw new Error("Response body is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Received response. Starting to handle stream...");
|
|
|
|
await handleStream(response.body.getReader());
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error calling the API:", error);
|
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2023-07-05 14:39:07 +03:00
|
|
|
addQuestion: addQuestionHandler,
|
2023-06-30 11:10:59 +03:00
|
|
|
addStreamQuestion,
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
|
|
|
};
|