diff --git a/surfsense_web/lib/apis/documents.api.ts b/surfsense_web/lib/apis/documents.api.ts deleted file mode 100644 index 02975d2fd..000000000 --- a/surfsense_web/lib/apis/documents.api.ts +++ /dev/null @@ -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(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(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; - } catch (err: any) { - console.error("Error fetching document type counts:", err); - return {}; - } -}; diff --git a/surfsense_web/lib/apis/llm-configs.api.ts b/surfsense_web/lib/apis/llm-configs.api.ts deleted file mode 100644 index 53860b4dd..000000000 --- a/surfsense_web/lib/apis/llm-configs.api.ts +++ /dev/null @@ -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 => { - 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 => { - 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 => { - 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; -}; diff --git a/surfsense_web/lib/apis/podcasts.api.ts b/surfsense_web/lib/apis/podcasts.api.ts deleted file mode 100644 index beaa475ca..000000000 --- a/surfsense_web/lib/apis/podcasts.api.ts +++ /dev/null @@ -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); - } -}; diff --git a/surfsense_web/lib/apis/search-source-connectors.api.ts b/surfsense_web/lib/apis/search-source-connectors.api.ts deleted file mode 100644 index 98ac31284..000000000 --- a/surfsense_web/lib/apis/search-source-connectors.api.ts +++ /dev/null @@ -1,107 +0,0 @@ -import type { Connector, CreateConnectorRequest } from "@/hooks/use-connectors"; - -export const createConnector = async ( - data: CreateConnectorRequest, - authToken: string -): Promise => { - 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 => { - 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 => { - 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 => { - 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 => { - 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"); - } -}; diff --git a/surfsense_web/lib/apis/search-spaces.api.ts b/surfsense_web/lib/apis/search-spaces.api.ts deleted file mode 100644 index d63273f9d..000000000 --- a/surfsense_web/lib/apis/search-spaces.api.ts +++ /dev/null @@ -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(); -};