Skip to content

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

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

export interface ModelProviderEntry {
  name: string;
  display_name: string;
  badge?: string;
}

export interface ModelProvidersResponse {
  run_mode: string;
  providers: ModelProviderEntry[];
}

/**
 * The providers this deployment offers, filtered by OPENRAG_RUN_MODE.
 *
 * `config/model_providers.yaml` on the backend is the only source of truth for
 * this list: Settings cards, the Onboarding tabs and the model pickers all
 * render what it returns, so the frontend keeps no denylist of its own and
 * brand/theme never decides whether a provider is available.
 */
export const useGetModelProvidersQuery = (
  options?: Omit<
    UseQueryOptions<ModelProvidersResponse>,
    "queryKey" | "queryFn"
  >,
) => {
  const queryClient = useQueryClient();

  return useQuery(
    {
      // The response now carries display metadata (for example provider
      // badges). Version the cache key so an HMR session that cached the old
      // two-field shape fetches the enriched response immediately.
      queryKey: ["models", "providers", "v2"] as const,
      queryFn: async (): Promise<ModelProvidersResponse> => {
        const response =
          await apiClient.get<ModelProvidersResponse>("/models/providers");
        if (response.status < 200 || response.status >= 300) {
          throw new Error("Failed to fetch the model providers");
        }
        return response.data;
      },
      // Run mode and the config file are fixed for the life of the backend
      // process, so this never goes stale within a session. Editing
      // `config/model_providers.yaml` therefore takes a backend restart *and* a
      // page reload; the endpoint sends `no-store` so the reload actually asks.
      staleTime: Number.POSITIVE_INFINITY,
      gcTime: Number.POSITIVE_INFINITY,
      refetchOnWindowFocus: false,
      ...options,
    },
    queryClient,
  );
};