2023-07-05 14:33:26 +03:00
|
|
|
import { AxiosInstance } from "axios";
|
|
|
|
|
2023-07-05 14:39:07 +03:00
|
|
|
import {
|
|
|
|
ChatEntity,
|
2023-09-07 18:23:31 +03:00
|
|
|
ChatItem,
|
2023-09-12 18:44:15 +03:00
|
|
|
ChatMessage,
|
2023-07-05 14:39:07 +03:00
|
|
|
ChatQuestion,
|
|
|
|
} from "@/app/chat/[chatId]/types";
|
2023-07-05 14:33:26 +03:00
|
|
|
|
|
|
|
export const createChat = async (
|
|
|
|
name: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<ChatEntity> => {
|
|
|
|
const createdChat = (
|
|
|
|
await axiosInstance.post<ChatEntity>("/chat", { name: name })
|
|
|
|
).data;
|
|
|
|
|
|
|
|
return createdChat;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getChats = async (
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<ChatEntity[]> => {
|
|
|
|
const response = await axiosInstance.get<{
|
|
|
|
chats: ChatEntity[];
|
|
|
|
}>(`/chat`);
|
|
|
|
|
|
|
|
return response.data.chats;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteChat = async (
|
|
|
|
chatId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
|
|
|
await axiosInstance.delete(`/chat/${chatId}`);
|
|
|
|
};
|
2023-07-05 14:39:07 +03:00
|
|
|
|
|
|
|
export type AddQuestionParams = {
|
|
|
|
chatId: string;
|
|
|
|
chatQuestion: ChatQuestion;
|
|
|
|
brainId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addQuestion = async (
|
|
|
|
{ chatId, chatQuestion, brainId }: AddQuestionParams,
|
|
|
|
axiosInstance: AxiosInstance
|
2023-09-12 18:44:15 +03:00
|
|
|
): Promise<ChatMessage> => {
|
|
|
|
const response = await axiosInstance.post<ChatMessage>(
|
2023-07-05 14:39:07 +03:00
|
|
|
`/chat/${chatId}/question?brain_id=${brainId}`,
|
|
|
|
chatQuestion
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
};
|
2023-07-05 19:33:18 +03:00
|
|
|
|
2023-09-12 18:44:15 +03:00
|
|
|
export const getChatItems = async (
|
2023-07-05 19:33:18 +03:00
|
|
|
chatId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
2023-09-07 18:23:31 +03:00
|
|
|
): Promise<ChatItem[]> =>
|
|
|
|
(await axiosInstance.get<ChatItem[]>(`/chat/${chatId}/history`)).data;
|
2023-07-05 19:33:18 +03:00
|
|
|
|
|
|
|
export type ChatUpdatableProperties = {
|
|
|
|
chat_name?: string;
|
|
|
|
};
|
|
|
|
export const updateChat = async (
|
|
|
|
chatId: string,
|
|
|
|
chat: ChatUpdatableProperties,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<ChatEntity> => {
|
|
|
|
return (await axiosInstance.put<ChatEntity>(`/chat/${chatId}/metadata`, chat))
|
|
|
|
.data;
|
|
|
|
};
|