quivr/frontend/lib/api/chat/chat.ts
Antoine Dewez da8e7513e6
feat(frontend & backend): thumbs for message feedback (#2360)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):

---------

Co-authored-by: Stan Girard <girard.stanislas@gmail.com>
2024-03-21 00:11:06 -07:00

92 lines
2.0 KiB
TypeScript

import { AxiosInstance } from "axios";
import {
ChatEntity,
ChatItem,
ChatMessage,
ChatQuestion,
} from "@/app/chat/[chatId]/types";
export type ChatUpdatableProperties = {
chat_name?: string;
};
export type ChatMessageUpdatableProperties = {
thumbs?: boolean | null;
};
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}`);
};
export type AddQuestionParams = {
chatId: string;
chatQuestion: ChatQuestion;
brainId: string;
};
export const addQuestion = async (
{ chatId, chatQuestion, brainId }: AddQuestionParams,
axiosInstance: AxiosInstance
): Promise<ChatMessage> => {
const response = await axiosInstance.post<ChatMessage>(
`/chat/${chatId}/question?brain_id=${brainId}`,
chatQuestion
);
return response.data;
};
export const getChatItems = async (
chatId: string,
axiosInstance: AxiosInstance
): Promise<ChatItem[]> =>
(await axiosInstance.get<ChatItem[]>(`/chat/${chatId}/history`)).data;
export const updateChat = async (
chatId: string,
chat: ChatUpdatableProperties,
axiosInstance: AxiosInstance
): Promise<ChatEntity> => {
return (await axiosInstance.put<ChatEntity>(`/chat/${chatId}/metadata`, chat))
.data;
};
export const updateChatMessage = async (
chatId: string,
messageId: string,
chatMessageUpdatableProperties: ChatMessageUpdatableProperties,
axiosInstance: AxiosInstance
): Promise<ChatItem> => {
return (
await axiosInstance.put<ChatItem>(
`/chat/${chatId}/${messageId}`,
chatMessageUpdatableProperties
)
).data;
};