Skip to content

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

Python
4.5 KB
Raw Blame
import {
  type UseMutationOptions,
  useMutation,
  useQueryClient,
} from "@tanstack/react-query";
import { apiClient } from "@/lib/api-client";
import { formatProviderErrorMessage } from "@/lib/chat-stream-errors";
import {
  refreshModelCatalog,
  useGetCurrentProviderModelsQuery,
} from "../queries/useGetModelsQuery";
import type { Settings } from "../queries/useGetSettingsQuery";

export interface UpdateSettingsRequest {
  // Agent settings
  llm_model?: string;
  llm_provider?: string;
  system_prompt?: string;

  // Knowledge settings
  chunk_size?: number;
  chunk_overlap?: number;
  table_structure?: boolean;
  ocr?: boolean;
  ocr_languages?: string[];
  picture_descriptions?: boolean;
  disable_ingest_with_langflow?: boolean;
  embedding_model?: string;
  embedding_provider?: string;

  // Docling VLM pipeline settings
  vlm_enabled?: boolean;
  vlm_provider?: string;
  vlm_model?: string;
  vlm_prompt?: string;
  vlm_response_format?: string;
  vlm_max_tokens?: number;
  vlm_concurrency?: number;
  vlm_timeout?: number;
  vlm_watsonx_api_version?: string;

  // Provider-specific settings (for dialogs)
  model_provider?: string; // Deprecated, kept for backward compatibility
  api_key?: string;
  endpoint?: string;
  project_id?: string;

  // Provider-specific API keys
  openai_api_key?: string;
  anthropic_api_key?: string;
  watsonx_api_key?: string;
  watsonx_endpoint?: string;
  watsonx_project_id?: string;
  ollama_endpoint?: string;
  remove_ollama_config?: boolean;
  remove_openai_config?: boolean;
  remove_anthropic_config?: boolean;
  remove_watsonx_config?: boolean;
  // Bypass the "this provider's embedding models are still in use" guard.
  force_remove?: boolean;
  provider_credentials?: Record<string, Record<string, string>>;
  provider_credential_removals?: Record<string, string[]>;
  provider_auth_methods?: Record<string, string>;
  remove_provider_config?: string;
}

export interface AffectedEmbeddingModel {
  model: string;
  doc_count: number;
}

// Typed error that preserves the structured 409 payload returned by
// POST /api/settings when removing a provider whose embedding models are
// still referenced by indexed documents.
class UpdateSettingsError extends Error {
  readonly status: number;
  readonly code?: string;
  readonly affectedProvider?: string;
  readonly affectedModels?: AffectedEmbeddingModel[];

  constructor(status: number, data: Record<string, unknown>) {
    const raw =
      typeof data.error === "string" ? data.error : "Failed to update settings";
    super(formatProviderErrorMessage(raw));
    this.name = "UpdateSettingsError";
    this.status = status;
    this.code = typeof data.code === "string" ? data.code : undefined;
    this.affectedProvider =
      typeof data.affected_provider === "string"
        ? data.affected_provider
        : undefined;
    this.affectedModels = Array.isArray(data.affected_models)
      ? (data.affected_models as AffectedEmbeddingModel[])
      : undefined;
  }
}

export const isEmbeddingProviderInUseError = (
  err: unknown,
): err is UpdateSettingsError =>
  err instanceof UpdateSettingsError &&
  err.code === "embedding_provider_in_use" &&
  Array.isArray(err.affectedModels) &&
  err.affectedModels.length > 0;

export interface UpdateSettingsResponse {
  message: string;
  settings: Settings;
}

async function updateSettings(
  variables: UpdateSettingsRequest,
): Promise<UpdateSettingsResponse> {
  const response = await apiClient.post<UpdateSettingsResponse>(
    "/settings",
    variables,
  );

  if (response.status < 200 || response.status >= 300) {
    const errorData = (response.data ?? {}) as unknown as Record<
      string,
      unknown
    >;
    throw new UpdateSettingsError(response.status, errorData);
  }

  return response.data;
}

export const useUpdateSettingsMutation = (
  options?: Omit<
    UseMutationOptions<UpdateSettingsResponse, Error, UpdateSettingsRequest>,
    "mutationFn"
  >,
) => {
  const queryClient = useQueryClient();
  const { refetch: refetchModels } = useGetCurrentProviderModelsQuery();

  return useMutation({
    mutationFn: updateSettings,
    onSuccess: (...args) => {
      queryClient.invalidateQueries({
        queryKey: ["settings"],
      });
      // A cluster-hosted provider's models are listed with its *saved*
      // credentials, so a save can change what the catalogue offers.
      refreshModelCatalog(queryClient);
      refetchModels(); // Refetch models for the settings page
      options?.onSuccess?.(...args);
    },
    onError: options?.onError,
    onSettled: options?.onSettled,
  });
};