Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
4.2 KB
Raw Blame
import {
  type UseQueryOptions,
  useQuery,
  useQueryClient,
} from "@tanstack/react-query";
import { apiClient } from "@/lib/api-client";
import type { File } from "./useGetSearchQuery";

export interface ListFilesParams {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortOrder?: "asc" | "desc";
  connectorType?: string[];
  mimetype?: string[];
  owner?: string[];
  dataSources?: string[];
  search?: string;
  afterKey?: Record<string, unknown> | null; //composite pagination cursor, could be undefined in the beginning
  createdAfter?: string;
  createdBefore?: string;
}

export interface ListFilesResponse {
  files: File[];
  total: number;
  is_approximate: boolean;
  page: number;
  page_size: number;
  after_key: Record<string, unknown> | null;
}

export const useListFiles = (
  params: ListFilesParams = {},
  options?: Omit<UseQueryOptions<ListFilesResponse>, "queryKey" | "queryFn">,
) => {
  const queryClient = useQueryClient();

  async function fetchFiles(): Promise<ListFilesResponse> {
    const searchParams = new URLSearchParams();

    if (params.page) searchParams.set("page", String(params.page));
    if (params.pageSize) searchParams.set("page_size", String(params.pageSize));
    if (params.sortBy) searchParams.set("sort_by", params.sortBy);
    if (params.sortOrder) searchParams.set("sort_order", params.sortOrder);
    for (const v of params.connectorType ?? [])
      searchParams.append("connector_type", v);
    for (const v of params.mimetype ?? []) searchParams.append("mimetype", v);
    for (const v of params.owner ?? []) searchParams.append("owner", v);
    for (const v of params.dataSources ?? [])
      searchParams.append("data_sources", v);
    if (params.search) searchParams.set("search", params.search);
    if (params.afterKey)
      searchParams.set("after_key", JSON.stringify(params.afterKey));
    if (params.createdAfter)
      searchParams.set("created_after", params.createdAfter);
    if (params.createdBefore)
      searchParams.set("created_before", params.createdBefore);

    const url = `/files?${searchParams.toString()}`; //internal (cookie auth)

    const response = await apiClient.get(url);

    if (response.status < 200 || response.status >= 300) {
      const errorData = response.data ?? { error: "Unknown error" };
      throw new Error(
        errorData.error || `Failed to list files: ${response.status}`,
      );
    }

    const data = response.data;

    const files: File[] = (data.files || []).map(
      (f: Record<string, unknown>) => ({
        filename: (f.filename as string) || "",
        mimetype: (f.mimetype as string) || "",
        chunkCount: (f.chunk_count as number) || 0,
        source_url: (f.source_url as string) || "",
        owner: (f.owner as string) || "",
        owner_name: (f.owner_name as string) || "",
        owner_email: (f.owner_email as string) || "",
        size: (f.file_size as number) || 0,
        connector_type: (f.connector_type as string) || "local",
        embedding_model: f.embedding_model as string | undefined,
        embedding_dimensions: f.embedding_dimensions as number | undefined,
        allowed_users: (f.allowed_users as string[]) || [],
        allowed_groups: (f.allowed_groups as string[]) || [],
        indexed_time: (f.indexed_time as string) || undefined,
        status: (f.status as File["status"]) || "active",
        error: f.error as string | undefined,
        document_id: f.document_id as string | undefined,
        web_source_id: f.web_source_id as string | undefined,
        web_page_id: f.web_page_id as string | undefined,
        web_page_depth:
          typeof f.web_page_depth === "number" ? f.web_page_depth : 0,
        web_child_count: f.web_child_count as number | undefined,
      }),
    );

    const result: ListFilesResponse = {
      files,
      total: data.total || 0,
      is_approximate: data.is_approximate ?? true,
      page: data.page || 1,
      page_size: data.page_size || 25,
      after_key: data.after_key ?? null,
    };

    return result;
  }

  return useQuery(
    {
      queryKey: ["listFiles", params],
      placeholderData: (prev: ListFilesResponse | undefined) => prev,
      queryFn: fetchFiles,
      retry: false,
      ...options,
    },
    queryClient,
  );
};