This commit is contained in:
thierryverse 2025-11-15 03:46:22 +02:00
parent 62df67b93b
commit 981e3a74e7
5 changed files with 0 additions and 629 deletions

View file

@ -1,260 +0,0 @@
import type { DocumentWithChunks } from "@/hooks/use-document-by-chunk";
import type { DocumentTypeCount } from "@/hooks/use-document-types";
import { normalizeListResponse } from "../pagination";
export const uploadDocument = async (formData: FormData, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/fileupload`,
{
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
},
body: formData,
}
);
if (!response.ok) {
throw new Error("Failed to upload document");
}
return await response.json();
};
export const createDocument = async (request: {
documentType: string;
content: any;
searchSpaceId: number;
authToken: string;
}) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${request.authToken}`,
},
body: JSON.stringify(request),
});
if (!response.ok) {
throw new Error("Failed to process document");
}
return await response.json();
};
export const fetchDocumentByChunk = async (chunkId: number, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/by-chunk/${chunkId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
method: "GET",
}
);
if (!response.ok) {
const errorText = await response.text();
let errorMessage = "Failed to fetch document";
try {
const errorData = JSON.parse(errorText);
errorMessage = errorData.detail || errorMessage;
} catch {
// If parsing fails, use default message
}
if (response.status === 404) {
errorMessage = "Chunk not found or you don't have access to it";
}
throw new Error(errorMessage);
}
const data: DocumentWithChunks = await response.json();
return data;
};
export const fetchDocumentTypes = async (authToken: string) => {
if (!authToken) {
throw new Error("No authentication token found");
}
// Build URL with optional search_space_id query parameter
const url = new URL(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/type-counts`
);
const response = await fetch(url.toString(), {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch document types: ${response.statusText}`);
}
const data = await response.json();
// Convert the object to an array of DocumentTypeCount
const typeCounts: DocumentTypeCount[] = Object.entries(data).map(([type, count]) => ({
type,
count: count as number,
}));
return typeCounts;
};
export const fetchDocuments = async (
searchSpaceId: number,
authToken: string,
fetchPage?: number,
fetchPageSize?: number,
fetchDocumentTypes?: string[]
) => {
// Build query params
const params = new URLSearchParams({
search_space_id: searchSpaceId.toString(),
});
// // Use passed parameters or fall back to state/options
// const effectivePage = fetchPage !== undefined ? fetchPage : page;
// const effectivePageSize =
// fetchPageSize !== undefined ? fetchPageSize : pageSize;
// const effectiveDocumentTypes =
// fetchDocumentTypes !== undefined ? fetchDocumentTypes : documentTypes;
// if (effectivePage !== undefined) {
// params.append("page", effectivePage.toString());
// }
// if (effectivePageSize !== undefined) {
// params.append("page_size", effectivePageSize.toString());
// }
// if (effectiveDocumentTypes && effectiveDocumentTypes.length > 0) {
// params.append("document_types", effectiveDocumentTypes.join(","));
// }
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents?${params.toString()}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Failed to fetch documents");
}
const data = await response.json();
return normalizeListResponse<Document>(data);
};
export const searchDocuments = async (
searchSpaceId: number,
authToken: string,
searchQuery: string,
fetchPage?: number,
fetchPageSize?: number,
fetchDocumentTypes?: string[]
) => {
// if (!searchQuery.trim()) {
// // If search is empty, fetch all documents
// // return fetchDocuments(fetchPage, fetchPageSize, fetchDocumentTypes);
// }
// Build query params
const params = new URLSearchParams({
search_space_id: searchSpaceId.toString(),
title: searchQuery,
});
// // Use passed parameters or fall back to state/options
// const effectivePage = fetchPage !== undefined ? fetchPage : page;
// const effectivePageSize = fetchPageSize !== undefined ? fetchPageSize : pageSize;
// const effectiveDocumentTypes =
// fetchDocumentTypes !== undefined ? fetchDocumentTypes : documentTypes;
// if (effectivePage !== undefined) {
// params.append("page", effectivePage.toString());
// }
// if (effectivePageSize !== undefined) {
// params.append("page_size", effectivePageSize.toString());
// }
// if (effectiveDocumentTypes && effectiveDocumentTypes.length > 0) {
// params.append("document_types", effectiveDocumentTypes.join(","));
// }
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/search?${params.toString()}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Failed to search documents");
}
const data = await response.json();
const normalized = normalizeListResponse<Document>(data);
return normalized;
};
export const deleteDocument = async (documentId: number, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/${documentId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "DELETE",
}
);
if (!response.ok) {
throw new Error("Failed to delete document");
}
return await response.json();
};
export const getDocumentTypeCounts = async (searchSpaceId: number, authToken: string) => {
try {
const params = new URLSearchParams({
search_space_id: searchSpaceId.toString(),
});
const response = await fetch(
`${
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL
}/api/v1/documents/type-counts?${params.toString()}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Failed to fetch document type counts");
}
const counts = await response.json();
return counts as Record<string, number>;
} catch (err: any) {
console.error("Error fetching document type counts:", err);
return {};
}
};

View file

@ -1,90 +0,0 @@
import type { CreateLLMConfig, LLMConfig, UpdateLLMConfig } from "@/hooks/use-llm-configs";
export const fetchLLMConfigs = async (searchSpaceId: number, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs?search_space_id=${searchSpaceId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Failed to fetch LLM configurations");
}
return await response.json();
};
export const createLLMConfig = async (
config: CreateLLMConfig,
authToken: string
): Promise<LLMConfig | null> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(config),
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to create LLM configuration");
}
const newConfig = await response.json();
return newConfig;
};
export const deleteLLMConfig = async (id: number, authToken: string): Promise<boolean> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs/${id}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
if (!response.ok) {
throw new Error("Failed to delete LLM configuration");
}
return await response.json();
};
export const updateLLMConfig = async (
id: number,
config: UpdateLLMConfig,
authToken: string
): Promise<LLMConfig | null> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs/${id}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(config),
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to update LLM configuration");
}
const updatedConfig = await response.json();
return updatedConfig;
};

View file

@ -1,74 +0,0 @@
import type { PodcastItem } from "@/app/dashboard/[search_space_id]/podcasts/podcasts-client";
import type { GeneratePodcastRequest } from "@/components/chat/ChatPanel/ChatPanelContainer";
export const getPodcastByChatId = async (chatId: string, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/by-chat/${Number(chatId)}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || "Failed to fetch podcast");
}
return (await response.json()) as PodcastItem | null;
};
export const generatePodcast = async (request: GeneratePodcastRequest, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/generate/`,
{
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(request),
}
);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || "Failed to generate podcast");
}
return await response.json();
};
export const loadPodcast = async (podcast: PodcastItem, authToken: string) => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000);
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/${podcast.id}/stream`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
signal: controller.signal,
}
);
if (!response.ok) {
throw new Error(`Failed to fetch audio stream: ${response.statusText}`);
}
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} catch (error) {
if (error instanceof DOMException && error.name === "AbortError") {
throw new Error("Request timed out. Please try again.");
}
throw error;
} finally {
clearTimeout(timeoutId);
}
};

View file

@ -1,107 +0,0 @@
import type { Connector, CreateConnectorRequest } from "@/hooks/use-connectors";
export const createConnector = async (
data: CreateConnectorRequest,
authToken: string
): Promise<Connector> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(data),
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to create connector");
}
return response.json();
};
export const getConnectors = async (
skip = 0,
limit = 100,
authToken: string
): Promise<Connector[]> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors?skip=${skip}&limit=${limit}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connectors");
}
return response.json();
};
export const getConnector = async (connectorId: number, authToken: string): Promise<Connector> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connector");
}
return response.json();
};
export const updateConnector = async (
connectorId: number,
data: CreateConnectorRequest,
authToken: string
): Promise<Connector> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(data),
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to update connector");
}
return response.json();
};
export const deleteConnector = async (connectorId: number, authToken: string): Promise<void> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to delete connector");
}
};

View file

@ -1,98 +0,0 @@
export const fetchSearchSpaces = async () => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Not authenticated");
}
return await response.json();
};
export const deleteSearchSpace = async (id: number) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${id}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
}
);
if (!response.ok) {
throw new Error("Failed to delete search space");
}
return await response.json();
};
export const createSearchSpace = async (data: { name: string; description: string }) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
body: JSON.stringify(data),
}
);
if (!response.ok) {
throw new Error("Failed to create search space");
}
return await response.json();
};
export const fetchSearchSpace = async (searchSpaceId: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${searchSpaceId}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
method: "GET",
}
);
if (response.status === 401) {
// Clear token and redirect to home
localStorage.removeItem("surfsense_bearer_token");
window.location.href = "/";
throw new Error("Unauthorized: Redirecting to login page");
}
if (!response.ok) {
throw new Error(`Failed to fetch search space: ${response.status}`);
}
return await response.json();
};
export const fetchSearchSpacePreferences = async (searchSpaceId: number, authToken: string) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-spaces/${searchSpaceId}/llm-preferences`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "GET",
}
);
if (!response.ok) {
throw new Error("Failed to fetch LLM preferences");
}
return await response.json();
};