quivr/frontend/app/chat/[chatId]/hooks/useChatService.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-06-28 20:39:27 +03:00
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { useAxios } from "@/lib/hooks";
import { ChatEntity, ChatHistory, ChatQuestion } from "../types";
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useChatService = () => {
const { axiosInstance } = useAxios();
2023-06-28 20:39:27 +03:00
const { currentBrain } = useBrainContext();
const createChat = async ({ name }: { name: string }) => {
return axiosInstance.post<ChatEntity>(`/chat`, { name });
};
const getChatHistory = async (chatId: string | undefined) => {
if (chatId === undefined) {
return [];
}
const rep = (
await axiosInstance.get<ChatHistory[]>(`/chat/${chatId}/history`)
).data;
return rep;
};
const addQuestion = async (
chatId: string,
chatQuestion: ChatQuestion
): Promise<ChatHistory> => {
2023-06-28 20:39:27 +03:00
if (currentBrain?.id === undefined) {
throw new Error("No current brain");
}
return (
await axiosInstance.post<ChatHistory>(
`/chat/${chatId}/question?brain_id=${currentBrain.id}`,
chatQuestion
)
).data;
};
return {
createChat,
getChatHistory,
addQuestion,
};
};