2025-11-15 02:07:20 +02:00
|
|
|
import type { Message } from "@ai-sdk/react";
|
2025-11-14 00:42:19 +02:00
|
|
|
import type { Chat, ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client";
|
2025-11-15 02:07:20 +02:00
|
|
|
import type { ResearchMode } from "@/components/chat/types";
|
2025-11-11 04:02:04 +02:00
|
|
|
|
|
|
|
|
export const fetchChatDetails = async (
|
2025-11-14 00:42:19 +02:00
|
|
|
chatId: string,
|
|
|
|
|
authToken: string
|
2025-11-11 04:02:04 +02:00
|
|
|
): Promise<ChatDetails | null> => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch chat details: ${response.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
2025-11-12 12:03:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchChatsBySearchSpace = async (
|
2025-11-14 00:42:19 +02:00
|
|
|
searchSpaceId: string,
|
|
|
|
|
authToken: string
|
2025-11-12 12:03:06 +02:00
|
|
|
): Promise<ChatDetails[] | null> => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?search_space_id=${searchSpaceId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch chats: ${response.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
2025-11-12 16:59:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const deleteChat = async (chatId: number, authToken: string) => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${chatId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to delete chat: ${response.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
2025-11-11 04:02:04 +02:00
|
|
|
};
|
2025-11-12 13:27:15 +02:00
|
|
|
|
2025-11-12 16:59:56 +02:00
|
|
|
export const createChat = async (
|
2025-11-14 00:42:19 +02:00
|
|
|
initialMessage: string,
|
|
|
|
|
researchMode: ResearchMode,
|
|
|
|
|
selectedConnectors: string[],
|
|
|
|
|
authToken: string,
|
|
|
|
|
searchSpaceId: number
|
2025-11-12 16:59:56 +02:00
|
|
|
): Promise<Chat | null> => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
type: researchMode,
|
|
|
|
|
title: "Untitled Chat",
|
|
|
|
|
initial_connectors: selectedConnectors,
|
|
|
|
|
messages: [
|
|
|
|
|
{
|
|
|
|
|
role: "user",
|
|
|
|
|
content: initialMessage,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
search_space_id: searchSpaceId,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to create chat: ${response.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
2025-11-12 16:59:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const updateChat = async (
|
2025-11-14 00:42:19 +02:00
|
|
|
chatId: string,
|
|
|
|
|
messages: Message[],
|
|
|
|
|
researchMode: ResearchMode,
|
|
|
|
|
selectedConnectors: string[],
|
|
|
|
|
authToken: string,
|
|
|
|
|
searchSpaceId: number
|
2025-11-12 16:59:56 +02:00
|
|
|
) => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const userMessages = messages.filter((msg) => msg.role === "user");
|
|
|
|
|
if (userMessages.length === 0) return;
|
|
|
|
|
|
|
|
|
|
const title = userMessages[0].content;
|
|
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`,
|
|
|
|
|
{
|
|
|
|
|
method: "PUT",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
type: researchMode,
|
|
|
|
|
title: title,
|
|
|
|
|
initial_connectors: selectedConnectors,
|
|
|
|
|
messages: messages,
|
|
|
|
|
search_space_id: searchSpaceId,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to update chat: ${response.statusText}`);
|
|
|
|
|
}
|
2025-11-12 16:59:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchChats = async (
|
2025-11-14 00:42:19 +02:00
|
|
|
searchSpaceId: string,
|
|
|
|
|
limit: number,
|
|
|
|
|
skip: number,
|
|
|
|
|
authToken: string
|
2025-11-12 16:59:56 +02:00
|
|
|
) => {
|
2025-11-14 00:42:19 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?limit=${limit}&skip=${skip}&search_space_id=${searchSpaceId}`,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${authToken}`,
|
|
|
|
|
},
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch chats: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.json();
|
2025-11-12 13:27:15 +02:00
|
|
|
};
|