OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
feat: Implement model provider discovery for Red Hat OpenShift API (#2482)
Mike Pawlowskide05e36552d9
diff --git a/.env.example b/.env.exampleindex d383ecfc3..a0e4e8b98 100644--- a/.env.example+++ b/.env.example@@ -337,10 +337,13 @@ WATSONX_PROJECT_ID=## Maximum embedding requests OpenRAG sends to the embeddings endpoint at once# (default 16, sized for a GPU-served model; 0 = no limit). Extra requests wait-# inside OpenRAG. Set 4 for a CPU-served model: it embeds well under one chunk-# per second, and the endpoint's kube-rbac-proxy answers 502 once a request-# waits upstream past its 30s timeout -- which fails folder uploads (several-# files x 8 embedding threads). Also editable in Settings. One of these+# inside OpenRAG. A CPU-served model copes with 16 when its InferenceService+# sets CPU requests = limits and VLLM_CPU_OMP_THREADS_BIND to that many cores+# (e.g. 6 and 0-5); otherwise vLLM binds a thread per node core and the pod is+# throttled to well under one chunk per second. Set 4 for such a slow endpoint:+# the endpoint's kube-rbac-proxy answers 502 once a request waits upstream past+# its 30s timeout -- which fails folder uploads (several files x 8 embedding+# threads). Also editable in Settings. One of these# slots is reserved for search and chat queries, so they are not queued behind# ingestion (with a limit of 1, both share the single slot).# RHOAI_EMBEDDING_MAX_CONCURRENCY=diff --git a/docs/docs/reference/configuration.mdx b/docs/docs/reference/configuration.mdxindex f294e2cc4..5b12adb69 100644--- a/docs/docs/reference/configuration.mdx+++ b/docs/docs/reference/configuration.mdx@@ -74,7 +74,7 @@ These variables are initially set during the [application onboarding process](/i| `RHOAI_EMBEDDINGS_ENDPOINT` | Not set | Embeddings endpoint for the Red Hat OpenShift AI model provider, a second `InferenceService`. Leave unset only if one endpoint serves both chat and embeddings. || `RHOAI_API_KEY` | Not set | Kubernetes bearer token for the Red Hat OpenShift AI model provider, sent to both endpoints. Its subject needs `get` on `inferenceservices` in the model namespace. || `RHOAI_TLS_VERIFY` | Not set | TLS trust for the Red Hat OpenShift AI endpoints. Set to a CA bundle path, such as the service-serving CA OpenShift projects into every pod (`/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt`), or `false` to skip verification for a local `oc port-forward` only. Scoped to this provider, so it does not affect other providers. |-| `RHOAI_EMBEDDING_MAX_CONCURRENCY` | `16` | Maximum number of embedding requests OpenRAG sends to the Red Hat OpenShift AI embeddings endpoint at once; further requests wait inside OpenRAG. The default suits a GPU-served model. For a CPU-served model set `4`: it answers slowly enough that more queued requests outlast the endpoint proxy's upstream timeout, which returns `502` when several files are ingested together. One slot is reserved for search and chat queries so they are not queued behind ingestion; with a limit of `1` both share it. Set `0` for no limit. Also editable in **Settings**. |+| `RHOAI_EMBEDDING_MAX_CONCURRENCY` | `16` | Maximum number of embedding requests OpenRAG sends to the Red Hat OpenShift AI embeddings endpoint at once; further requests wait inside OpenRAG. The default suits a GPU-served model, and also a CPU-served model whose InferenceService sets CPU requests equal to limits and `VLLM_CPU_OMP_THREADS_BIND` to the same number of cores (for example `6` and `0-5`). Without that, vLLM binds one thread per node core, the pod is CPU-throttled, and embedding takes over a second per chunk. Set `4` for such a slow endpoint: otherwise queued requests outlast the endpoint proxy's upstream timeout, which returns `502` when several files are ingested together. One slot is reserved for search and chat queries so they are not queued behind ingestion; with a limit of `1` both share it. Set `0` for no limit. Also editable in **Settings**. |Newly indexed chunks keep the model name in `embedding_model` for compatibility and also store`embedding_provider` plus the unambiguous `embedding_space_id` (`provider:model`). Retrieval queriesdiff --git a/enhancements/providers/redhat/openshift_ai.py b/enhancements/providers/redhat/openshift_ai.pyindex e08889c2e..11552e0d4 100644--- a/enhancements/providers/redhat/openshift_ai.py+++ b/enhancements/providers/redhat/openshift_ai.py@@ -72,7 +72,7 @@ from __future__ import annotationsimport asyncioimport time-from collections.abc import Mapping+from collections.abc import Collection, Mappingfrom typing import Any, Literal, NamedTuplefrom urllib.parse import urlsplit, urlunsplit@@ -152,9 +152,11 @@ CREDENTIAL_FIELDS: list[dict[str, Any]] = ["tooltip": "How many embedding requests OpenRAG sends to the embedding endpoint at ""once; the rest wait in OpenRAG. One slot is kept for search and chat queries so ""they never wait behind ingestion. Leave blank for the default of 16, sized for a "- "GPU-served model. Lower it to 4 for a CPU-served model: it answers slowly, and "- "the endpoint's kube-rbac-proxy returns 502 once a queued request waits past its "- "upstream timeout (30s by default), which fails ingestion. 0 means no limit.",+ "GPU-served model. A CPU-served model whose vLLM threads match its CPU limit "+ "copes with 16; lower it to 4 if the endpoint takes more than about a second per "+ "chunk: the endpoint's kube-rbac-proxy returns 502 once a queued request waits "+ "past its upstream timeout (30s by default), which fails ingestion. 0 means no "+ "limit.","required": False,"field_type": "text","options": None,@@ -175,10 +177,13 @@ _LOCAL_ONLY_FIELDS = frozenset({"embedding_api_base", "embedding_max_concurrency#: long before the GPU does: granite-embedding-english-r2 on an H100 served#: about 200 calls/s at 16 in flight (p95 under 170ms) and topped out near 430#: calls/s at 64-128, with the GPU about 20% busy. 16 leaves that headroom to-#: other OpenRAG instances sharing the endpoint. A CPU-served model is ~100x-#: slower (about 2.7s per ~1000-character chunk on 5 cores), so 16 in flight-#: queues calls past the 30s `kube-rbac-proxy` upstream timeout; CPU-#: deployments set 4.+#: other OpenRAG instances sharing the endpoint. On CPU, vLLM binds one OpenMP+#: thread per *node* core whatever the pod's CPU limit, so a small limit spends+#: most of its time throttled: 2 cores gave 1.65s per ~1000-character chunk and+#: ~0.85 calls/s. With requests = limits = 6 and `VLLM_CPU_OMP_THREADS_BIND=0-5`+#: the same model served 0.13s per chunk and ~10 calls/s, p95 2s at 16 in+#: flight. Only an endpoint still slower than about a second per chunk needs 4,+#: or 16 in flight queues calls past the 30s `kube-rbac-proxy` upstream timeout.DEFAULT_EMBEDDING_MAX_CONCURRENCY = 16#: LiteLLM kwargs that must not become request-body fields. `ssl_verify` is@@ -437,6 +442,11 @@ HEALTH_TIMEOUT_SECONDS = 10.0#: rides out a pod mid-rollout, and a human is waiting on the answer.HEALTH_MAX_RETRIES = 1+#: Retries for onboarding's draft discovery. Fewer than the catalogue path for+#: the same reason as the health check: one retry still rides out a pod+#: mid-rollout, and a human is watching the picker wait.+DISCOVERY_MAX_RETRIES = 1+#: Upper bound on the whole pre-save check, both endpoints, retries and backoff#: included. Without it two unreachable endpoints × attempts × timeout adds up#: to a minute during which the settings save just hangs.@@ -626,7 +636,9 @@ def is_model_listing(body: Any) -> bool:return isinstance(body, Mapping) and isinstance(body.get("data"), list)-async def _list_models(client: Any, api_base: str, api_key: str) -> tuple[str, ...] | None:+async def _list_models(+ client: Any, api_base: str, api_key: str, *, max_retries: int = 2+) -> tuple[str, ...] | None:"""The models one endpoint serves, or None if it could not be asked."""url = models_url(api_base)logger.debug("Listing models on the OpenShift AI endpoint", url=url)@@ -635,6 +647,7 @@ async def _list_models(client: Any, api_base: str, api_key: str) -> tuple[str, ."GET",url,client=client,+ max_retries=max_retries,headers=_auth_headers(api_key),timeout=MODELS_TIMEOUT_SECONDS,)@@ -666,6 +679,46 @@ async def _list_models(client: Any, api_base: str, api_key: str) -> tuple[str, .return models+#: The two pickers, each filled from its own endpoint.+_HALVES: tuple[CallKind, ...] = ("chat", "embedding")+++async def _list_halves(+ values: Mapping[str, str],+ *,+ halves: Collection[CallKind] = _HALVES,+ max_retries: int = 2,+) -> dict[str, tuple[str, ...] | None]:+ """What each endpoint in `halves` serves, asked at once.++ The one place the endpoints are asked; `fetch_models()` adds the+ catalogue's cache on top and `list_cluster_models()` the draft form's+ fallbacks. When one endpoint serves both, it is asked once and its answer+ fills both halves, whichever were asked for. A half is None when its+ endpoint could not be asked. Errors building the client — a CA path that+ does not exist — propagate, so each caller keeps its own fallback.+ """+ import httpx++ chat_base, embedding_base = endpoints(values)+ api_key = values.get("api_key", "")+ async with httpx.AsyncClient(+ verify=ssl_verify_for(values), timeout=MODELS_TIMEOUT_SECONDS+ ) as client:+ if chat_base == embedding_base:+ shared = await _list_models(client, chat_base, api_key, max_retries=max_retries)+ return {"chat": shared, "embedding": shared}+ bases = {"chat": chat_base, "embedding": embedding_base}+ wanted = [half for half in _HALVES if half in halves]+ listed = await asyncio.gather(+ *(+ _list_models(client, bases[half], api_key, max_retries=max_retries)+ for half in wanted+ )+ )+ return dict(zip(wanted, listed, strict=True))++async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:"""Ask each configured endpoint which models it actually serves.@@ -687,8 +740,6 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:Returns None when nothing is known at all, so a catalogue request neverfails because the cluster is unreachable; the caller keeps whatever it had."""- import httpx-values = _values(credentials)chat_base, embedding_base = endpoints(values)api_key = values.get("api_key", "")@@ -716,19 +767,9 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:embedding_endpoint=embedding_base,shared_endpoint=chat_base == embedding_base,)- listed: dict[str, tuple[str, ...] | None] = {}+ needed = [half for half in _HALVES if getattr(known, half) is None]try:- async with httpx.AsyncClient(- verify=ssl_verify_for(values), timeout=MODELS_TIMEOUT_SECONDS- ) as client:- if chat_base == embedding_base:- shared = await _list_models(client, chat_base, api_key)- listed = {"chat": shared, "embedding": shared}- else:- if known.chat is None:- listed["chat"] = await _list_models(client, chat_base, api_key)- if known.embedding is None:- listed["embedding"] = await _list_models(client, embedding_base, api_key)+ listed = await _list_halves(values, halves=needed)except Exception as error:logger.warning("Could not list models on OpenShift AI; keeping the configured list",@@ -766,6 +807,72 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:return result+async def list_cluster_models(+ credentials: Mapping[str, Any], *, kind: CallKind | None = None+) -> ClusterModels:+ """Ask each endpoint what it serves, without touching the catalogue's cache.++ For credentials that have not been saved yet — the onboarding form asking+ what a cluster serves while the operator is still typing. `fetch_models()`+ cannot be used for that: its cache is keyed on one set of credentials, and+ `cached_models()` hands whatever it holds to the catalogue, so a draft+ listing would replace the saved cluster's models in every picker.++ `kind` asks only the endpoint behind that picker; None asks both. Each+ onboarding step shows one picker, so an unreachable embedding endpoint must+ not hold up the language step for its whole retry window. A half that was+ not asked is None, like one that failed.++ A half is None when its endpoint could not be asked or listed nothing+ usable, so the caller falls back to the configured list for that half.++ Someone is watching the picker wait, so it retries less than the catalogue+ does (`DISCOVERY_MAX_RETRIES`): a mistyped host gives up after about 20s+ rather than a minute.+ """+ values = _values(credentials)+ chat_base, embedding_base = endpoints(values)+ api_key = values.get("api_key", "")+ if not chat_base or not api_key:+ return ClusterModels(chat=None, embedding=None)++ halves: tuple[CallKind, ...] = _HALVES if kind is None else (kind,)+ logger.info(+ "Listing draft models on OpenShift AI",+ kind=kind or "both",+ shared_endpoint=chat_base == embedding_base,+ )+ try:+ listed = await _list_halves(values, halves=halves, max_retries=DISCOVERY_MAX_RETRIES)+ except Exception as error:+ logger.warning(+ "Could not list models on OpenShift AI", kind=kind or "both", error=str(error)+ )+ return ClusterModels(chat=None, embedding=None)++ # `_list_halves()` returns only the halves it asked (both, for a shared+ # endpoint), so read with `.get()`. An empty listing is far more likely one+ # we could not interpret than an endpoint serving nothing, so it falls back+ # like a failure does.+ result = ClusterModels(+ chat=listed.get("chat") or None, embedding=listed.get("embedding") or None+ )++ def _summary(half: CallKind) -> int | str:+ if half not in listed:+ return "not asked"+ models = getattr(result, half)+ return len(models) if models is not None else "fallback"++ logger.info(+ "Listed draft models on OpenShift AI",+ kind=kind or "both",+ chat=_summary("chat"),+ embedding=_summary("embedding"),+ )+ return result++# --------------------------------------------------------------------------# Health# --------------------------------------------------------------------------diff --git a/enhancements/providers/registry.py b/enhancements/providers/registry.pyindex e0bb26468..8f042e631 100644--- a/enhancements/providers/registry.py+++ b/enhancements/providers/registry.py@@ -84,6 +84,39 @@ def enhancements() -> tuple[ModuleType, ...]:return tuple(_ENHANCEMENTS.values())+def model_discovery_for(provider: str) -> ModuleType | None:+ """The enhancement that can list `provider`'s models from unsaved credentials.++ The optional `list_cluster_models(credentials)` member of the contract: the+ same listing `fetch_models()` caches, without the cache, for a form that is+ asking what a cluster serves before anything is saved. None when the+ provider has no enhancement or the enhancement cannot list its own models.+ """+ enhancement = get(provider)+ if enhancement is None or not callable(getattr(enhancement, "list_cluster_models", None)):+ return None+ return enhancement+++async def list_cluster_models_for(+ enhancement: ModuleType,+ credentials: Mapping[str, Any],+ *,+ kind: CallKind | None = None,+) -> Any:+ """Call the module's `list_cluster_models`, narrowed to one picker if it can be.++ `kind` follows the same rule as in `credentials_for`: it is passed only to a+ module whose hook declares it. Red Hat OpenShift AI lists chat and embedding+ models from two endpoints and uses it to ask only the one a picker needs; a+ single-endpoint provider (watsonx.ai on-prem) lists everything at once and+ is called without it. None asks for both pickers.+ """+ if kind is not None and _hook_accepts_kind(enhancement, "list_cluster_models"):+ return await enhancement.list_cluster_models(credentials, kind=kind)+ return await enhancement.list_cluster_models(credentials)++def live_models_for(provider: str, kind: CallKind) -> tuple[str, ...] | None:"""What `provider` last said it serves for `kind`, if it can say at all.@@ -187,6 +220,25 @@ def embedding_concurrency_for(return limit+def default_auth_method_for(provider: str, stored: Mapping[str, Any] | None) -> str | None:+ """The authentication method to assume for `provider` when none was chosen.++ The optional `default_auth_method(stored)` member of the enhancement+ contract, for a provider whose form offers several methods. None when the+ provider has no such hook, the hook fails (logged and ignored), or it+ returns anything but a non-empty string.+ """+ hook = getattr(get(provider), "default_auth_method", None)+ if not callable(hook):+ return None+ try:+ method = hook(stored or {})+ except Exception as exc:+ _log_hook_failure(provider, "default_auth_method", exc)+ return None+ return method if isinstance(method, str) and method else None++def _log_hook_failure(provider: str, hook: str, exc: Exception) -> None:from utils.logging_config import get_logger@@ -217,20 +269,25 @@ def _accepts_kind(enhancement: ModuleType) -> bool:Cached per module: the answer is fixed for the life of the process, andthis sits on the path of every gateway request."""+ return _hook_accepts_kind(enhancement, "litellm_credentials")+++def _hook_accepts_kind(enhancement: ModuleType, hook: str) -> bool:+ """Whether the module's `hook` takes a `kind` keyword."""try:- return "kind" in inspect.signature(enhancement.litellm_credentials).parameters+ return "kind" in inspect.signature(getattr(enhancement, hook)).parametersexcept (TypeError, ValueError):# A C-implemented or otherwise unintrospectable callable. Assume the# base contract rather than risk a TypeError on a real call.- _log_signature_fallback(getattr(enhancement, "__name__", str(enhancement)))+ _log_signature_fallback(getattr(enhancement, "__name__", str(enhancement)), hook)return False-def _log_signature_fallback(module_name: str) -> None:+def _log_signature_fallback(module_name: str, hook: str) -> None:from utils.logging_config import get_loggerget_logger(__name__).debug(- "Could not inspect a provider enhancement's credential translation; "- "calling it without a call kind",+ "Could not inspect a provider enhancement hook; calling it without a call kind",module=module_name,+ hook=hook,)diff --git a/enhancements/providers/watsonx/onprem.py b/enhancements/providers/watsonx/onprem.pyindex 8c3dd3385..8b6bb5dd2 100644--- a/enhancements/providers/watsonx/onprem.py+++ b/enhancements/providers/watsonx/onprem.py@@ -190,6 +190,15 @@ def credential_fields_for_auth_method(auth_method: str) -> frozenset[str]:return SHARED_CREDENTIAL_FIELDS | active+def default_auth_method(stored: Mapping[str, Any] | None) -> str:+ """The authentication method stored credentials imply when none was recorded.++ A Zen API key means `zen_api_key`; anything else is the username and API+ key pair, which is also the method the form offers first.+ """+ return "zen_api_key" if _values(stored).get("zen_api_key") else "username_api_key"++def _values(stored: Mapping[str, Any] | None) -> dict[str, str]:"""Stored form values as trimmed strings, excluding runtime objects."""values: dict[str, str] = {}@@ -714,8 +723,6 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:catalogue request never fails because the cluster is unreachable; the callerkeeps whatever it had."""- import httpx-values = litellm_credentials(credentials)api_base = (values.get("api_base") or "").strip()header = auth_header(credentials)@@ -734,6 +741,38 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:if fresh is not None and _models_cache["key"] == key:return fresh+ models = await list_cluster_models(credentials)+ if models is None:+ return None+ _models_cache.update(key=key, at=time.monotonic(), value=models)+ return models+++async def list_cluster_models(credentials: Mapping[str, Any]) -> ClusterModels | None:+ """List the cluster's foundation models without touching the cache.++ `fetch_models()` is this plus the TTL cache the catalogue reads. Called+ directly for credentials that have not been saved yet — the onboarding form+ asking what a cluster serves — because caching a draft listing would hand+ an unsaved cluster's models to every picker through `cached_models()`.++ None on any failure, or when the credentials are incomplete.+ """+ import httpx++ values = litellm_credentials(credentials)+ api_base = (values.get("api_base") or "").strip()+ header = auth_header(credentials)+ if not api_base or not header:+ # Debug, not warning: onboarding reaches this on purpose while the+ # form is still being filled. Presence flags only — never values.+ logger.debug(+ "Not listing models on the watsonx.ai cluster: draft credentials are incomplete",+ has_cluster_url=bool(api_base),+ has_credentials=bool(header),+ )+ return None+headers = {"Authorization": header, "Accept": "application/json"}url = model_specs_url(api_base)# Only the first request needs parameters built here; a `next` link already@@ -787,7 +826,6 @@ async def fetch_models(credentials: Mapping[str, Any]) -> ClusterModels | None:)return None- _models_cache.update(key=key, at=time.monotonic(), value=models)logger.info("Listed models on the watsonx.ai cluster",chat=len(models.chat),diff --git a/frontend/app/api/mutations/useOnboardingMutation.test.tsx b/frontend/app/api/mutations/useOnboardingMutation.test.tsxindex 265e83db1..764074941 100644--- a/frontend/app/api/mutations/useOnboardingMutation.test.tsx+++ b/frontend/app/api/mutations/useOnboardingMutation.test.tsx@@ -8,6 +8,7 @@ import { HttpResponse, http } from "msw";import { describe, expect, it, vi } from "vitest";import { server } from "@/test-utils/msw/server";import { createQueryWrapper, createTestQueryClient } from "@/test-utils/render";+import { useGetModelCatalogQuery } from "../queries/useGetModelsQuery";import { useOnboardingMutation } from "./useOnboardingMutation";const toast = vi.hoisted(() => ({@@ -71,6 +72,133 @@ describe("useOnboardingMutation", () => {);});+ it("refreshes the model catalogue once onboarding has saved credentials", async () => {+ let catalogFetchCount = 0;+ server.use(+ http.post("/api/onboarding", () =>+ HttpResponse.json({ message: "done", edited: true }),+ ),+ http.get("/api/models/catalog", () => {+ catalogFetchCount += 1;+ return HttpResponse.json({ providers: [] });+ }),+ );++ const { result } = renderHook(+ () => ({+ catalog: useGetModelCatalogQuery(),+ mutation: useOnboardingMutation(),+ }),+ { wrapper: createQueryWrapper() },+ );++ await waitFor(() => expect(catalogFetchCount).toBe(1));++ act(() => result.current.mutation.mutate({ llm_provider: "rhoai" }));++ await waitFor(() => expect(result.current.mutation.isSuccess).toBe(true));+ await waitFor(() => expect(catalogFetchCount).toBe(2));+ });++ it("refetches the catalogue when its first load was still in flight during onboarding", async () => {+ let catalogFetchCount = 0;+ let releaseFirstLoad: () => void = () => {};+ const firstLoadHeld = new Promise<void>((resolve) => {+ releaseFirstLoad = resolve;+ });+ server.use(+ http.post("/api/onboarding", () =>+ HttpResponse.json({ message: "done", edited: true }),+ ),+ http.get("/api/models/catalog", async () => {+ catalogFetchCount += 1;+ // The first load started before onboarding saved the credentials.+ if (catalogFetchCount === 1) {+ await firstLoadHeld;+ return HttpResponse.json({ providers: [] });+ }+ return HttpResponse.json({+ providers: [+ {+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ credential_fields: [],+ model_placeholder: null,+ models: [{ model: "gpt-oss-120b", mode: "chat" }],+ embedding_models: [],+ },+ ],+ });+ }),+ );++ const { result } = renderHook(+ () => ({+ catalog: useGetModelCatalogQuery(),+ mutation: useOnboardingMutation(),+ }),+ { wrapper: createQueryWrapper() },+ );++ try {+ await waitFor(() => expect(catalogFetchCount).toBe(1));++ act(() => result.current.mutation.mutate({ llm_provider: "rhoai" }));++ await waitFor(() => expect(result.current.mutation.isSuccess).toBe(true));+ await waitFor(() => expect(catalogFetchCount).toBe(2));+ } finally {+ releaseFirstLoad();+ }++ await waitFor(() =>+ expect(result.current.catalog.data?.providers[0]?.key).toBe("rhoai"),+ );+ });++ it("succeeds without waiting for the catalogue refresh to finish", async () => {+ let catalogFetchCount = 0;+ let releaseCatalog: () => void = () => {};+ const catalogHeld = new Promise<void>((resolve) => {+ releaseCatalog = resolve;+ });+ server.use(+ http.post("/api/onboarding", () =>+ HttpResponse.json({ message: "done", edited: true }),+ ),+ http.get("/api/models/catalog", async () => {+ catalogFetchCount += 1;+ // The refresh after onboarding stands in for a slow cluster listing.+ if (catalogFetchCount > 1) {+ await catalogHeld;+ }+ return HttpResponse.json({ providers: [] });+ }),+ );++ const { result } = renderHook(+ () => ({+ catalog: useGetModelCatalogQuery(),+ mutation: useOnboardingMutation(),+ }),+ { wrapper: createQueryWrapper() },+ );++ try {+ await waitFor(() => expect(result.current.catalog.isSuccess).toBe(true));++ act(() => result.current.mutation.mutate({ llm_provider: "rhoai" }));++ await waitFor(() => expect(result.current.mutation.isSuccess).toBe(true));+ await waitFor(() => expect(catalogFetchCount).toBe(2));+ expect(result.current.catalog.isFetching).toBe(true);+ } finally {+ releaseCatalog();+ }++ await waitFor(() => expect(result.current.catalog.isFetching).toBe(false));+ });+it("explains when watsonx.ai on-prem reduces the ingestion chunk size", async () => {server.use(http.post("/api/onboarding", () =>diff --git a/frontend/app/api/mutations/useOnboardingMutation.ts b/frontend/app/api/mutations/useOnboardingMutation.tsindex 646db9402..b4fda6176 100644--- a/frontend/app/api/mutations/useOnboardingMutation.ts+++ b/frontend/app/api/mutations/useOnboardingMutation.ts@@ -6,6 +6,7 @@ import {import { toast } from "sonner";import { apiClient } from "@/lib/api-client";import { formatProviderErrorMessage } from "@/lib/chat-stream-errors";+import { refreshModelCatalog } from "../queries/useGetModelsQuery";import { useUpdateOnboardingStateMutation } from "./useUpdateOnboardingStateMutation";export interface OnboardingVariables {@@ -88,6 +89,12 @@ export const useOnboardingMutation = (onSettled: async (data, error, variables, onMutateResult, context) => {// Invalidate settings query to refetch updated onboarding stateawait queryClient.invalidateQueries({ queryKey: ["settings"] });+ // The catalogue lists a cluster-hosted provider with its saved+ // credentials, which onboarding has just written. Not awaited: the+ // catalogue route lists every cluster provider live, and the mutation+ // stays pending until onSettled resolves, which would stall the+ // embedding step's progress behind a slow cluster.+ refreshModelCatalog(queryClient);await options?.onSettled?.(data,error,diff --git a/frontend/app/api/mutations/useUpdateSettingsMutation.test.tsx b/frontend/app/api/mutations/useUpdateSettingsMutation.test.tsxindex a2340b19e..f991d572e 100644--- a/frontend/app/api/mutations/useUpdateSettingsMutation.test.tsx+++ b/frontend/app/api/mutations/useUpdateSettingsMutation.test.tsx@@ -10,6 +10,7 @@ import { HttpResponse, http } from "msw";import { describe, expect, it } from "vitest";import { server } from "@/test-utils/msw/server";import { createQueryWrapper } from "@/test-utils/render";+import { useGetModelCatalogQuery } from "../queries/useGetModelsQuery";import {isEmbeddingProviderInUseError,useUpdateSettingsMutation,@@ -50,6 +51,132 @@ describe("useUpdateSettingsMutation", () => {await waitFor(() => expect(settingsFetchCount).toBe(2));});+ it("refreshes the model catalogue so pickers see a re-configured cluster's models", async () => {+ // Settings → Providers saves credentials only; the Agent and Ingestion+ // pickers read the catalogue, which lists a cluster with saved credentials.+ let catalogFetchCount = 0;+ server.use(+ http.get("/api/settings", () => HttpResponse.json({})),+ http.post("/api/settings", () =>+ HttpResponse.json({ message: "saved", settings: {} }),+ ),+ http.post("/api/models/openai", () =>+ HttpResponse.json({ language_models: [], embedding_models: [] }),+ ),+ http.get("/api/models/catalog", () => {+ catalogFetchCount += 1;+ const model =+ catalogFetchCount === 1 ? "granite-3.3-2b-instruct" : "gpt-oss-120b";+ return HttpResponse.json({+ providers: [+ {+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ credential_fields: [],+ model_placeholder: null,+ models: [{ model, mode: "chat" }],+ embedding_models: [],+ },+ ],+ });+ }),+ );++ const { result } = renderHook(+ () => ({+ catalog: useGetModelCatalogQuery(),+ mutation: useUpdateSettingsMutation(),+ }),+ { wrapper: createQueryWrapper() },+ );++ await waitFor(() =>+ expect(result.current.catalog.data?.providers[0].models[0].model).toBe(+ "granite-3.3-2b-instruct",+ ),+ );++ act(() =>+ result.current.mutation.mutate({+ provider_credentials: { rhoai: { api_key: "token" } },+ }),+ );++ await waitFor(() =>+ expect(result.current.catalog.data?.providers[0].models[0].model).toBe(+ "gpt-oss-120b",+ ),+ );+ });++ it("refetches the catalogue when its first load was still in flight during the save", async () => {+ let catalogFetchCount = 0;+ let releaseFirstLoad: () => void = () => {};+ const firstLoadHeld = new Promise<void>((resolve) => {+ releaseFirstLoad = resolve;+ });+ const catalogWith = (model: string) => ({+ providers: [+ {+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ credential_fields: [],+ model_placeholder: null,+ models: [{ model, mode: "chat" }],+ embedding_models: [],+ },+ ],+ });+ server.use(+ http.get("/api/settings", () => HttpResponse.json({})),+ http.post("/api/settings", () =>+ HttpResponse.json({ message: "saved", settings: {} }),+ ),+ http.post("/api/models/openai", () =>+ HttpResponse.json({ language_models: [], embedding_models: [] }),+ ),+ http.get("/api/models/catalog", async () => {+ catalogFetchCount += 1;+ // The first load started before the save, so it lists the old models.+ if (catalogFetchCount === 1) {+ await firstLoadHeld;+ return HttpResponse.json(catalogWith("granite-3.3-2b-instruct"));+ }+ return HttpResponse.json(catalogWith("gpt-oss-120b"));+ }),+ );++ const { result } = renderHook(+ () => ({+ catalog: useGetModelCatalogQuery(),+ mutation: useUpdateSettingsMutation(),+ }),+ { wrapper: createQueryWrapper() },+ );++ try {+ await waitFor(() => expect(catalogFetchCount).toBe(1));++ act(() =>+ result.current.mutation.mutate({+ provider_credentials: { rhoai: { api_key: "token" } },+ }),+ );++ await waitFor(() => expect(result.current.mutation.isSuccess).toBe(true));+ await waitFor(() => expect(catalogFetchCount).toBe(2));+ } finally {+ releaseFirstLoad();+ }++ await waitFor(() =>+ expect(result.current.catalog.data?.providers[0].models[0].model).toBe(+ "gpt-oss-120b",+ ),+ );+ expect(result.current.catalog.isFetching).toBe(false);+ });+it("calls the caller's onSuccess in addition to the hook's own side effects", async () => {server.use(http.get("/api/settings", () => HttpResponse.json({})),diff --git a/frontend/app/api/mutations/useUpdateSettingsMutation.ts b/frontend/app/api/mutations/useUpdateSettingsMutation.tsindex f2e49d5da..2ac59f875 100644--- a/frontend/app/api/mutations/useUpdateSettingsMutation.ts+++ b/frontend/app/api/mutations/useUpdateSettingsMutation.ts@@ -5,7 +5,10 @@ import {} from "@tanstack/react-query";import { apiClient } from "@/lib/api-client";import { formatProviderErrorMessage } from "@/lib/chat-stream-errors";-import { useGetCurrentProviderModelsQuery } from "../queries/useGetModelsQuery";+import {+ refreshModelCatalog,+ useGetCurrentProviderModelsQuery,+} from "../queries/useGetModelsQuery";import type { Settings } from "../queries/useGetSettingsQuery";export interface UpdateSettingsRequest {@@ -139,6 +142,9 @@ export const useUpdateSettingsMutation = (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 pageoptions?.onSuccess?.(...args);},diff --git a/frontend/app/api/queries/useDiscoverProviderModelsQuery.ts b/frontend/app/api/queries/useDiscoverProviderModelsQuery.tsnew file mode 100644index 000000000..7bc114dc0--- /dev/null+++ b/frontend/app/api/queries/useDiscoverProviderModelsQuery.ts@@ -0,0 +1,89 @@+import {+ type UseQueryOptions,+ useQuery,+ useQueryClient,+} from "@tanstack/react-query";+import type { DiscoveredCatalogModels } from "@/components/models/catalog-models";++export type DiscoveredModels = DiscoveredCatalogModels;++export interface DiscoverProviderModelsParams {+ provider: string;+ credentials: Record<string, string>;+ authMethod?: string;+ /**+ * The one picker the caller shows. A provider with an endpoint per kind+ * then asks only that one, so an unreachable embedding endpoint cannot+ * stall the language step. Omitted, both are asked.+ */+ kind?: "chat" | "embedding";+}++/**+ * Lists the models a cluster-hosted provider serves, using credentials typed+ * into a form that has not been saved yet.+ *+ * The catalogue only lists a cluster with *saved* credentials, so during+ * onboarding it can offer nothing but the configured fallback. A vLLM+ * `--served-model-name` can be anything, so the cluster is the only source+ * that knows what to offer.+ *+ * Deliberately not under the `["models"]` key: onboarding disables Submit+ * while anything there is fetching, and a slow cluster must not block it.+ */+export const useDiscoverProviderModelsQuery = (+ params: DiscoverProviderModelsParams,+ options?: Omit<UseQueryOptions<DiscoveredModels>, "queryKey" | "queryFn">,+) => {+ const queryClient = useQueryClient();+ const { provider, credentials, authMethod, kind } = params;++ return useQuery(+ {+ queryKey: [+ "provider-model-discovery",+ provider,+ credentials,+ authMethod ?? null,+ kind ?? null,+ ] as const,+ queryFn: async ({ signal }): Promise<DiscoveredModels> => {+ const response = await fetch(+ `/api/models/${encodeURIComponent(provider)}/discover`,+ {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ credentials,+ ...(authMethod ? { auth_method: authMethod } : {}),+ ...(kind ? { kind } : {}),+ }),+ signal,+ },+ );+ const data = await response.json().catch(() => ({}));+ if (!response.ok) {+ throw new Error(+ typeof data?.error === "string"+ ? data.error+ : "Unable to list models from the provider.",+ );+ }+ return {+ models: Array.isArray(data?.models) ? data.models : null,+ embedding_models: Array.isArray(data?.embedding_models)+ ? data.embedding_models+ : null,+ };+ },+ retry: false,+ refetchOnWindowFocus: false,+ staleTime: 5 * 60 * 1000,+ // The credentials are part of the key; don't keep them around longer+ // than the form that typed them.+ gcTime: 60 * 1000,+ ...options,+ },+ queryClient,+ );+};diff --git a/frontend/app/api/queries/useGetModelsQuery.test.tsx b/frontend/app/api/queries/useGetModelsQuery.test.tsxindex b58cbe14a..800fc5df9 100644--- a/frontend/app/api/queries/useGetModelsQuery.test.tsx+++ b/frontend/app/api/queries/useGetModelsQuery.test.tsx@@ -1,12 +1,19 @@/*** Tests for useGetModelsQuery.ts: the four per-provider model queries, the- * provider-switching dispatcher, and the static model catalogue query.+ * provider-switching dispatcher, the static model catalogue query, and the+ * post-save catalogue refresh.*/import { HttpResponse, http } from "msw";-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import { server } from "@/test-utils/msw/server";-import { createQueryWrapper, renderHook, waitFor } from "@/test-utils/render";import {+ createQueryWrapper,+ createTestQueryClient,+ renderHook,+ waitFor,+} from "@/test-utils/render";+import {+ refreshModelCatalog,useGetAnthropicModelsQuery,useGetCurrentProviderModelsQuery,useGetIBMModelsQuery,@@ -313,3 +320,80 @@ describe("useGetModelCatalogQuery", () => {);});});++describe("refreshModelCatalog", () => {+ const catalogWith = (model: string) => ({+ providers: [+ {+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ credential_fields: [],+ model_placeholder: null,+ models: [{ model, mode: "chat" }],+ embedding_models: [],+ },+ ],+ });++ it("refetches even while the catalogue's first load is still in flight", async () => {+ // Without the cancel, invalidation would join the pre-save request and+ // staleTime: Infinity would keep its (stale) result indefinitely.+ let catalogFetchCount = 0;+ let releaseFirstLoad: () => void = () => {};+ const firstLoadHeld = new Promise<void>((resolve) => {+ releaseFirstLoad = resolve;+ });+ server.use(+ http.get("/api/models/catalog", async () => {+ catalogFetchCount += 1;+ if (catalogFetchCount === 1) {+ await firstLoadHeld;+ return HttpResponse.json(catalogWith("granite-3.3-2b-instruct"));+ }+ return HttpResponse.json(catalogWith("gpt-oss-120b"));+ }),+ );++ const queryClient = createTestQueryClient();+ const { result } = renderHook(() => useGetModelCatalogQuery(), {+ wrapper: createQueryWrapper({ queryClient }),+ });++ try {+ await waitFor(() => expect(catalogFetchCount).toBe(1));+ expect(result.current.data).toBeUndefined();++ refreshModelCatalog(queryClient);++ await waitFor(() => expect(catalogFetchCount).toBe(2));+ } finally {+ releaseFirstLoad();+ }++ await waitFor(() =>+ expect(result.current.data?.providers[0].models[0].model).toBe(+ "gpt-oss-120b",+ ),+ );+ expect(result.current.isFetching).toBe(false);+ });++ it("warns instead of rejecting when the refresh itself fails", async () => {+ const queryClient = createTestQueryClient();+ const failure = new Error("cache unavailable");+ vi.spyOn(queryClient, "cancelQueries").mockRejectedValueOnce(failure);+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});++ try {+ refreshModelCatalog(queryClient);++ await waitFor(() =>+ expect(warn).toHaveBeenCalledWith(+ "Failed to refresh the model catalogue after saving. Error: Error: cache unavailable.",+ ),+ );+ } finally {+ warn.mockRestore();+ }+ });+});diff --git a/frontend/app/api/queries/useGetModelsQuery.ts b/frontend/app/api/queries/useGetModelsQuery.tsindex b7a7b45f0..a63c1252f 100644--- a/frontend/app/api/queries/useGetModelsQuery.ts+++ b/frontend/app/api/queries/useGetModelsQuery.ts@@ -1,4 +1,5 @@import {+ type QueryClient,type UseQueryOptions,useQuery,useQueryClient,@@ -300,15 +301,49 @@ export interface CatalogProvider {model_placeholder: string | null;models: CatalogModel[];embedding_models: CatalogModel[];+ /**+ * Whether `POST /api/models/{key}/discover` can list what this provider+ * serves from credentials that are not saved yet (cluster-hosted providers).+ */+ discovers_models?: boolean;}export interface ModelCatalogResponse {providers: CatalogProvider[];}+const MODEL_CATALOG_QUERY_FILTERS = {+ queryKey: ["models", "catalog"],+ exact: true,+} as const;++/**+ * Refetch the catalogue after a save that can change it — a cluster-hosted+ * provider is listed with its *saved* credentials. Fire-and-forget, so the+ * caller's mutation never waits on a slow cluster listing.+ *+ * Cancelling first matters: while the catalogue's first load is in flight+ * (no data yet), invalidateQueries joins that pre-save request instead of+ * starting a new one, and staleTime: Infinity would then keep its result for+ * the tab's lifetime. Cancelling reverts the query to its pre-fetch state, so+ * the invalidation is guaranteed a post-save fetch.+ */+export const refreshModelCatalog = (queryClient: QueryClient): void => {+ void queryClient+ .cancelQueries(MODEL_CATALOG_QUERY_FILTERS)+ .then(() => queryClient.invalidateQueries(MODEL_CATALOG_QUERY_FILTERS))+ .catch((error: unknown) => {+ // Both calls swallow fetch errors themselves; this guards the chain.+ console.warn(+ `Failed to refresh the model catalogue after saving. Error: ${error}.`,+ );+ });+};+/*** LiteLLM's bundled model list, grouped by provider. Static for the tab's- * lifetime — same as openrag-next's `/agent/model-catalog` fetch.+ * lifetime — same as openrag-next's `/agent/model-catalog` fetch — except+ * where a save refreshes it through `refreshModelCatalog`.*/export const useGetModelCatalogQuery = (options?: Omit<UseQueryOptions<ModelCatalogResponse>, "queryKey" | "queryFn">,@@ -317,7 +352,7 @@ export const useGetModelCatalogQuery = (return useQuery({- queryKey: ["models", "catalog"] as const,+ queryKey: MODEL_CATALOG_QUERY_FILTERS.queryKey,queryFn: async (): Promise<ModelCatalogResponse> => {const response =await apiClient.get<ModelCatalogResponse>("/models/catalog");diff --git a/frontend/app/onboarding/_components/generic-onboarding.test.tsx b/frontend/app/onboarding/_components/generic-onboarding.test.tsxindex b56b62673..6e3d9f3df 100644--- a/frontend/app/onboarding/_components/generic-onboarding.test.tsx+++ b/frontend/app/onboarding/_components/generic-onboarding.test.tsx@@ -1,9 +1,14 @@-import { HttpResponse, http } from "msw";+import { HttpResponse, http, type RequestHandler } from "msw";import type { Dispatch, SetStateAction } from "react";import { describe, expect, it } from "vitest";import type { OnboardingVariables } from "@/app/api/mutations/useOnboardingMutation";import type { SavedProvidersSnapshot } from "@/components/models/catalog-models";-import { renderWithProviders, screen, userEvent } from "@/test-utils/render";+import {+ renderWithProviders,+ screen,+ userEvent,+ waitFor,+} from "@/test-utils/render";import { GenericOnboarding } from "./generic-onboarding";const catalog = {@@ -75,6 +80,35 @@ const catalog = {],embedding_models: [],},+ {+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ discovers_models: true,+ credential_fields: [+ {+ key: "api_base",+ label: "Chat endpoint",+ required: true,+ field_type: "text",+ },+ {+ key: "embedding_api_base",+ label: "Embedding endpoint",+ required: false,+ field_type: "text",+ },+ {+ key: "api_key",+ label: "Token",+ required: true,+ field_type: "password",+ },+ ],+ models: [{ model: "granite-3.3-2b-instruct", mode: "chat" }],+ embedding_models: [+ { model: "granite-embedding-english-r2", mode: "embedding" },+ ],+ },],};@@ -82,23 +116,36 @@ function renderOnboarding(provider: string,isEmbedding = false,savedProviders?: SavedProvidersSnapshot,+ extraHandlers: RequestHandler[] = [],+ catalogResponse: { providers: object[] } = catalog,) {let settings: OnboardingVariables = {};+ // Every model the parent was handed, in order.+ const modelHistory: Array<string | undefined> = [];const setSettings: Dispatch<SetStateAction<OnboardingVariables>> = (next) => {settings = typeof next === "function" ? next(settings) : next;+ modelHistory.push(+ isEmbedding ? settings.embedding_model : settings.llm_model,+ );};+ let blocker: string | null = null;const user = userEvent.setup();- renderWithProviders(+ const view = renderWithProviders(<GenericOnboardingprovider={provider}isEmbedding={isEmbedding}setSettings={setSettings}providers={savedProviders}+ onCompletionBlockerChange={(next) => {+ blocker = next;+ }}/>,{providers: ["tooltip"],handlers: [- http.get("/api/models/catalog", () => HttpResponse.json(catalog)),+ http.get("/api/models/catalog", () =>+ HttpResponse.json(catalogResponse),+ ),http.post("/api/models/watsonx_onprem/spaces", () =>HttpResponse.json({spaces: [@@ -107,10 +154,17 @@ function renderOnboarding(],}),),+ ...extraHandlers,],},);- return { user, getSettings: () => settings };+ return {+ user,+ getSettings: () => settings,+ getBlocker: () => blocker,+ unmount: view.unmount,+ modelHistory,+ };}describe("GenericOnboarding model selection", () => {@@ -288,3 +342,870 @@ describe("GenericOnboarding model selection", () => {expect(getSettings().embedding_model).toBe("text-embedding-3-large");});});++describe("GenericOnboarding cluster model discovery", () => {+ // Debounce (500 ms) plus the request.+ const DISCOVERY_TIMEOUT = { timeout: 3000 };++ type DiscoverRequest = {+ credentials: Record<string, string>;+ kind?: string;+ };++ function discoverHandler(+ response: () => Response,+ requests: DiscoverRequest[] = [],+ ) {+ return http.post("/api/models/rhoai/discover", async ({ request }) => {+ requests.push((await request.json()) as DiscoverRequest);+ return response();+ });+ }++ const served = () =>+ HttpResponse.json({+ models: [{ model: "gpt-oss-120b", mode: "chat" }],+ embedding_models: [{ model: "nomic-embed-text", mode: "embedding" }],+ });++ async function typeCredentials(user: ReturnType<typeof userEvent.setup>) {+ await user.type(+ await screen.findByLabelText(/Chat endpoint/),+ "https://chat.example.com/v1",+ );+ await user.type(screen.getByLabelText(/Token/), "sha256~token");+ }++ it("offers the models the cluster serves instead of the configured fallback", async () => {+ const requests: DiscoverRequest[] = [];+ const { user, getSettings } = renderOnboarding("rhoai", false, undefined, [+ discoverHandler(served, requests),+ ]);+ await waitFor(() =>+ expect(getSettings().llm_model).toBe("granite-3.3-2b-instruct"),+ );++ await typeCredentials(user);++ expect(+ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ // The automatic default follows the list; the cluster does not serve granite.+ expect(getSettings().llm_model).toBe("gpt-oss-120b");+ expect(requests.at(-1)?.credentials).toEqual({+ api_base: "https://chat.example.com/v1",+ api_key: "sha256~token",+ });+ // Only the chat endpoint is asked, so a dead embedding endpoint can't+ // stall this step.+ expect(requests.at(-1)?.kind).toBe("chat");++ await user.click(screen.getByRole("combobox", { name: "Language model" }));+ expect(+ await screen.findByRole("option", { name: "gpt-oss-120b" }),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole("option", { name: "granite-3.3-2b-instruct" }),+ ).not.toBeInTheDocument();+ });++ it("uses the embedding endpoint's listing for the embedding picker", async () => {+ const requests: DiscoverRequest[] = [];+ const { user, getSettings } = renderOnboarding("rhoai", true, undefined, [+ discoverHandler(served, requests),+ ]);++ await typeCredentials(user);++ await screen.findByText(+ "Showing 1 embedding model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ expect(getSettings().embedding_model).toBe("nomic-embed-text");+ expect(requests.at(-1)?.kind).toBe("embedding");+ });++ it("keeps a model the user chose even when the cluster does not list it", async () => {+ const { user, getSettings } = renderOnboarding("rhoai", false, undefined, [+ discoverHandler(served),+ ]);+ await user.click(+ await screen.findByRole("combobox", { name: "Language model" }),+ );+ await user.click(+ await screen.findByRole("option", { name: "granite-3.3-2b-instruct" }),+ );++ await typeCredentials(user);++ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ expect(getSettings().llm_model).toBe("granite-3.3-2b-instruct");+ });++ it("falls back to the configured models when the cluster cannot be listed", async () => {+ const { user, getSettings } = renderOnboarding("rhoai", false, undefined, [+ discoverHandler(() =>+ HttpResponse.json({ models: null, embedding_models: null }),+ ),+ ]);++ await typeCredentials(user);++ expect(+ await screen.findByText(+ /Couldn't list language models from the cluster/,+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ expect(getSettings().llm_model).toBe("granite-3.3-2b-instruct");+ });++ it("reports a failed discovery request the same way", async () => {+ const { user } = renderOnboarding("rhoai", false, undefined, [+ discoverHandler(() =>+ HttpResponse.json(+ { error: "Unable to list models from the provider." },+ { status: 500 },+ ),+ ),+ ]);++ await typeCredentials(user);++ expect(+ await screen.findByText(+ /Couldn't list language models from the cluster/,+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ });++ it("does not ask the cluster until the required credentials are filled", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user } = renderOnboarding("rhoai", false, undefined, [+ discoverHandler(served, requests),+ ]);++ await user.type(+ await screen.findByLabelText(/Chat endpoint/),+ "https://chat.example.com/v1",+ );+ await new Promise((resolve) => setTimeout(resolve, 800));++ expect(requests).toHaveLength(0);+ });++ it("reuses a saved token without retyping it", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ renderOnboarding(+ "rhoai",+ false,+ {+ custom: {+ rhoai: {+ credential_values: { api_base: "https://chat.example.com/v1" },+ secret_fields: ["api_key"],+ },+ },+ },+ [discoverHandler(served, requests)],+ );++ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ // The backend fills the saved token in; it never reaches the browser.+ expect(requests.at(-1)?.credentials).toEqual({+ api_base: "https://chat.example.com/v1",+ });+ });++ it("asks for the token again once the endpoint changes", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user } = renderOnboarding(+ "rhoai",+ false,+ {+ custom: {+ rhoai: {+ credential_values: { api_base: "https://chat.example.com/v1" },+ secret_fields: ["api_key"],+ },+ },+ },+ [discoverHandler(served, requests)],+ );+ const endpoint = await screen.findByLabelText(/Chat endpoint/);+ expect(+ screen.getByText(+ "A value is already saved. Leave this blank to keep it.",+ ),+ ).toBeInTheDocument();++ await user.clear(endpoint);+ await user.type(endpoint, "https://other.example.com/v1");++ // The backend drops the saved token for a new host, so it no longer counts.+ expect(+ screen.queryByText(+ "A value is already saved. Leave this blank to keep it.",+ ),+ ).not.toBeInTheDocument();+ const asked = requests.length;+ await new Promise((resolve) => setTimeout(resolve, 800));+ expect(+ requests+ .slice(asked)+ .some((r) => r.credentials.api_base === "https://other.example.com/v1"),+ ).toBe(false);++ await user.type(screen.getByLabelText(/Token/), "sha256~new-token");+ await waitFor(+ () =>+ expect(requests.at(-1)?.credentials).toEqual({+ api_base: "https://other.example.com/v1",+ api_key: "sha256~new-token",+ }),+ DISCOVERY_TIMEOUT,+ );+ });++ describe("Submit gate for a changed target", () => {+ const savedToken: SavedProvidersSnapshot = {+ custom: {+ rhoai: {+ credential_values: { api_base: "https://chat.example.com/v1" },+ secret_fields: ["api_key"],+ },+ },+ };+ const blockerText =+ "Enter Token again: the endpoint or TLS setting changed.";+ const hintText =+ "The endpoint or TLS setting changed, so the saved value is not sent to it. Enter it again.";++ it("does not block while the saved endpoint is kept", async () => {+ const { getBlocker } = renderOnboarding("rhoai", false, savedToken);+ await screen.findByLabelText(/Chat endpoint/);++ expect(getBlocker()).toBeNull();+ expect(screen.queryByText(hintText)).not.toBeInTheDocument();+ });++ it("blocks until the saved token is typed again", async () => {+ const { user, getBlocker } = renderOnboarding("rhoai", false, savedToken);+ const endpoint = await screen.findByLabelText(/Chat endpoint/);++ await user.clear(endpoint);+ await user.type(endpoint, "https://other.example.com/v1");++ expect(getBlocker()).toBe(blockerText);+ expect(screen.getByText(hintText)).toBeInTheDocument();++ await user.type(screen.getByLabelText(/Token/), "sha256~new-token");++ expect(getBlocker()).toBeNull();+ expect(screen.queryByText(hintText)).not.toBeInTheDocument();+ });++ it("unblocks when the saved endpoint is restored", async () => {+ const { user, getBlocker } = renderOnboarding("rhoai", false, savedToken);+ const endpoint = await screen.findByLabelText(/Chat endpoint/);++ await user.type(endpoint, "x");+ expect(getBlocker()).toBe(blockerText);++ await user.type(endpoint, "{Backspace}");+ expect(getBlocker()).toBeNull();+ });++ it("stops blocking once the tab is left", async () => {+ const { user, getBlocker, unmount } = renderOnboarding(+ "rhoai",+ false,+ savedToken,+ );+ await user.type(await screen.findByLabelText(/Chat endpoint/), "x");+ expect(getBlocker()).toBe(blockerText);++ unmount();++ expect(getBlocker()).toBeNull();+ });++ it("ignores a saved secret the chosen auth method does not use", async () => {+ const { user, getBlocker } = renderOnboarding("watsonx_onprem", false, {+ custom: {+ watsonx_onprem: {+ auth_method: "username_api_key",+ credential_values: {+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ },+ secret_fields: ["api_key", "zen_api_key"],+ },+ },+ });+ const endpoint = await screen.findByLabelText(/Cluster URL/);++ await user.clear(endpoint);+ await user.type(endpoint, "https://other-cpd.example.com");++ // Only the username + API key method is active, so only its key counts.+ expect(getBlocker()).toBe(+ "Enter API key again: the endpoint or TLS setting changed.",+ );+ });+ });++ it("treats a cleared saved endpoint as a new target", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user, getSettings } = renderOnboarding(+ "rhoai",+ true,+ {+ custom: {+ rhoai: {+ credential_values: {+ api_base: "https://chat.example.com/v1",+ embedding_api_base: "https://embed.example.com/v1",+ },+ secret_fields: ["api_key"],+ },+ },+ },+ [discoverHandler(served, requests)],+ );+ await screen.findByText(+ "Showing 1 embedding model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );++ await user.clear(screen.getByLabelText(/Embedding endpoint/));++ // Submit removes the endpoint, so embeddings go to the chat endpoint: the+ // saved token no longer counts, and nothing is asked until it is retyped.+ expect(getSettings().provider_credential_removals?.rhoai).toEqual([+ "embedding_api_base",+ ]);+ expect(+ screen.queryByText(+ "A value is already saved. Leave this blank to keep it.",+ ),+ ).not.toBeInTheDocument();+ const asked = requests.length;+ await new Promise((resolve) => setTimeout(resolve, 800));+ expect(requests.length).toBe(asked);++ await user.type(screen.getByLabelText(/Token/), "sha256~new-token");+ // Sent blank, so the backend does not fill the saved endpoint back in.+ await waitFor(+ () =>+ expect(requests.at(-1)?.credentials).toEqual({+ api_base: "https://chat.example.com/v1",+ embedding_api_base: "",+ api_key: "sha256~new-token",+ }),+ DISCOVERY_TIMEOUT,+ );+ });++ it("keeps the discovered model while the cluster is asked again", async () => {+ let calls = 0;+ let release: () => void = () => {};+ const { user, getSettings, modelHistory } = renderOnboarding(+ "rhoai",+ false,+ undefined,+ [+ http.post("/api/models/rhoai/discover", async () => {+ calls += 1;+ if (calls === 1) return served();+ await new Promise<void>((resolve) => {+ release = resolve;+ });+ return HttpResponse.json({+ models: [{ model: "gpt-oss-20b", mode: "chat" }],+ embedding_models: null,+ });+ }),+ ],+ );++ await typeCredentials(user);+ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ expect(getSettings().llm_model).toBe("gpt-oss-120b");+ const discoveredAt = modelHistory.lastIndexOf("gpt-oss-120b");++ await user.type(screen.getByLabelText(/Token/), "2");+ await screen.findByText(+ "Checking which models the cluster serves…",+ {},+ DISCOVERY_TIMEOUT,+ );+ // The configured fallback must not be handed over while the cluster is+ // being asked again.+ expect(getSettings().llm_model).toBe("gpt-oss-120b");++ // The status reflects the query, not the handler; release only once the+ // handler has installed its resolver.+ await waitFor(() => expect(calls).toBe(2), DISCOVERY_TIMEOUT);+ release();+ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ await waitFor(() => expect(getSettings().llm_model).toBe("gpt-oss-20b"));+ expect(modelHistory.slice(discoveredAt)).not.toContain(+ "granite-3.3-2b-instruct",+ );+ });++ it("selects the configured fallback while saved credentials are first checked", async () => {+ let asked = false;+ let release: () => void = () => {};+ const { getSettings } = renderOnboarding(+ "rhoai",+ false,+ {+ custom: {+ rhoai: {+ credential_values: { api_base: "https://chat.example.com/v1" },+ secret_fields: ["api_key"],+ },+ },+ },+ [+ http.post("/api/models/rhoai/discover", async () => {+ await new Promise<void>((resolve) => {+ release = resolve;+ asked = true;+ });+ return served();+ }),+ ],+ );++ await screen.findByText(+ "Checking which models the cluster serves…",+ {},+ DISCOVERY_TIMEOUT,+ );+ // Saved credentials make the first render already in flight; a slow+ // cluster must not leave Submit without a model meanwhile.+ await waitFor(() =>+ expect(getSettings().llm_model).toBe("granite-3.3-2b-instruct"),+ );++ // Release only once the handler has installed its resolver.+ await waitFor(() => expect(asked).toBe(true), DISCOVERY_TIMEOUT);+ release();+ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ );+ await waitFor(() => expect(getSettings().llm_model).toBe("gpt-oss-120b"));+ });++ describe("with no configured fallback", () => {+ const noFallbackCatalog = {+ ...catalog,+ providers: catalog.providers.map((entry) =>+ entry.key === "rhoai"+ ? { ...entry, models: [], embedding_models: [] }+ : entry,+ ),+ };++ it("asks for credentials instead of sending the operator elsewhere", async () => {+ renderOnboarding("rhoai", false, undefined, [], noFallbackCatalog);++ expect(+ await screen.findByText(+ "Enter the credentials above to list the language models the cluster serves.",+ ),+ ).toBeInTheDocument();+ expect(+ screen.queryByText(/Pick a different provider/),+ ).not.toBeInTheDocument();+ });++ it("asks for a model ID when the cluster cannot be listed", async () => {+ const { user } = renderOnboarding(+ "rhoai",+ false,+ undefined,+ [+ discoverHandler(() =>+ HttpResponse.json({ models: null, embedding_models: null }),+ ),+ ],+ noFallbackCatalog,+ );++ await typeCredentials(user);++ expect(+ await screen.findByText(+ "Couldn't list language models from the cluster. Type a model ID to continue.",+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ expect(screen.queryByText(/configured defaults/)).not.toBeInTheDocument();+ expect(+ screen.queryByText(/Pick a different provider/),+ ).not.toBeInTheDocument();+ });++ it("says so when the cluster lists nothing for this picker", async () => {+ const { user } = renderOnboarding(+ "rhoai",+ true,+ undefined,+ [+ discoverHandler(() =>+ HttpResponse.json({+ models: [{ model: "gpt-oss-120b", mode: "chat" }],+ embedding_models: [],+ }),+ ),+ ],+ noFallbackCatalog,+ );++ await typeCredentials(user);++ expect(+ await screen.findByText(+ "The cluster lists no embedding models. Type a model ID to continue.",+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ });+ });++ it("clears the automatic fallback when the cluster lists nothing", async () => {+ const { user, getSettings } = renderOnboarding("rhoai", true, undefined, [+ discoverHandler(() =>+ HttpResponse.json({+ models: [{ model: "gpt-oss-120b", mode: "chat" }],+ embedding_models: [],+ }),+ ),+ ]);+ await waitFor(() =>+ expect(getSettings().embedding_model).toBe(+ "granite-embedding-english-r2",+ ),+ );++ await typeCredentials(user);++ await screen.findByText(+ "The cluster lists no embedding models. Type a model ID to continue.",+ {},+ DISCOVERY_TIMEOUT,+ );+ // The cluster does not serve the fallback, so "Complete" must not send it.+ expect(getSettings().embedding_model).toBe("");+ });++ it("still sends the operator elsewhere for a provider that cannot discover", async () => {+ renderOnboarding("watsonx_onprem", true);++ expect(+ await screen.findByText(/publishes no embedding models in the catalogue/),+ ).toBeInTheDocument();+ });++ describe("for watsonx.ai on-prem's username + API key method", () => {+ // As the backend declares it: `username` is optional, because the Zen API+ // key method does not use it.+ const discoveringOnPrem = {+ ...catalog,+ providers: catalog.providers.map((entry) =>+ entry.key === "watsonx_onprem"+ ? {+ ...entry,+ discovers_models: true,+ credential_fields: entry.credential_fields.map((field) =>+ field.key === "username"+ ? { ...field, required: false }+ : field,+ ),+ }+ : entry,+ ),+ };++ function onPremDiscoverHandler(+ requests: Array<{ credentials: Record<string, string> }>,+ ) {+ return http.post(+ "/api/models/watsonx_onprem/discover",+ async ({ request }) => {+ requests.push(+ (await request.json()) as { credentials: Record<string, string> },+ );+ return HttpResponse.json({+ models: [{ model: "ibm/granite-4-h-small", mode: "chat" }],+ embedding_models: [],+ });+ },+ );+ }++ it("does not ask the cluster without a username", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user } = renderOnboarding(+ "watsonx_onprem",+ false,+ undefined,+ [onPremDiscoverHandler(requests)],+ discoveringOnPrem,+ );++ await user.type(+ await screen.findByLabelText(/Cluster URL/),+ "https://cpd.example.com",+ );+ await user.type(screen.getByLabelText(/^API key/), "cpd-api-key");+ await new Promise((resolve) => setTimeout(resolve, 800));++ expect(requests).toHaveLength(0);+ expect(+ screen.queryByText(/Couldn't list language models from the cluster/),+ ).not.toBeInTheDocument();+ });++ it("asks the cluster once the username is filled", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user } = renderOnboarding(+ "watsonx_onprem",+ false,+ undefined,+ [onPremDiscoverHandler(requests)],+ discoveringOnPrem,+ );++ await user.type(+ await screen.findByLabelText(/Cluster URL/),+ "https://cpd.example.com",+ );+ await user.type(screen.getByLabelText(/^API key/), "cpd-api-key");+ await user.type(screen.getByLabelText("Username"), "cpd-user");++ expect(+ await screen.findByText(+ "Showing 1 language model served by the cluster.",+ {},+ DISCOVERY_TIMEOUT,+ ),+ ).toBeInTheDocument();+ expect(requests.at(-1)?.credentials).toEqual({+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ api_key: "cpd-api-key",+ });+ });++ describe("when the auth method is switched", () => {+ // The Zen API key method's own field, which the shared catalogue above+ // leaves out.+ const withZenKeyField = {+ ...discoveringOnPrem,+ providers: discoveringOnPrem.providers.map((entry) =>+ entry.key === "watsonx_onprem"+ ? {+ ...entry,+ credential_fields: [+ ...entry.credential_fields,+ {+ key: "zen_api_key",+ label: "Zen API key",+ required: false,+ field_type: "password",+ },+ ],+ }+ : entry,+ ),+ };+ const savedZenKey: SavedProvidersSnapshot = {+ custom: {+ watsonx_onprem: {+ auth_method: "zen_api_key",+ credential_values: { api_base: "https://cpd.example.com" },+ secret_fields: ["zen_api_key"],+ },+ },+ };+ const servedStatus = "Showing 1 language model served by the cluster.";+ // Longer than the typing debounce, so a late second request would land.+ const settle = () => new Promise((resolve) => setTimeout(resolve, 800));+ type OnPremRequest = {+ credentials: Record<string, string>;+ auth_method?: string;+ kind?: string;+ };++ it("asks once, with only the new method's fields", async () => {+ const requests: OnPremRequest[] = [];+ const { user } = renderOnboarding(+ "watsonx_onprem",+ false,+ {+ custom: {+ watsonx_onprem: {+ auth_method: "username_api_key",+ credential_values: {+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ },+ secret_fields: ["api_key", "zen_api_key"],+ },+ },+ },+ [onPremDiscoverHandler(requests)],+ withZenKeyField,+ );+ await screen.findByText(servedStatus, {}, DISCOVERY_TIMEOUT);+ expect(requests.at(-1)).toMatchObject({+ credentials: {+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ },+ auth_method: "username_api_key",+ });+ const asked = requests.length;++ await user.click(screen.getByRole("button", { name: /Zen API key/ }));+ await settle();++ // The saved Zen key makes the method ready at once; the username+ // typed for the other method must not ride along under its name.+ expect(requests.slice(asked)).toEqual([+ {+ credentials: { api_base: "https://cpd.example.com" },+ auth_method: "zen_api_key",+ kind: "chat",+ },+ ]);+ expect(screen.getByText(servedStatus)).toBeInTheDocument();+ });++ it("does not re-ask with the other method's fields when switching back", async () => {+ const requests: OnPremRequest[] = [];+ const { user } = renderOnboarding(+ "watsonx_onprem",+ false,+ savedZenKey,+ [onPremDiscoverHandler(requests)],+ withZenKeyField,+ );+ await screen.findByText(servedStatus, {}, DISCOVERY_TIMEOUT);++ await user.click(+ screen.getByRole("button", { name: /Username \+ API key/ }),+ );+ await user.type(screen.getByLabelText("Username"), "cpd-user");+ await user.type(screen.getByLabelText(/^API key/), "cpd-api-key");+ await waitFor(+ () => expect(requests.at(-1)?.auth_method).toBe("username_api_key"),+ DISCOVERY_TIMEOUT,+ );+ await screen.findByText(servedStatus, {}, DISCOVERY_TIMEOUT);+ await settle();+ const asked = requests.length;++ await user.click(screen.getByRole("button", { name: /Zen API key/ }));+ await settle();++ // Asked with the Zen key on mount, so the answer is cached: nothing+ // goes out, rather than the typed username + API key under its name.+ expect(requests.slice(asked)).toEqual([]);+ expect(screen.getByText(servedStatus)).toBeInTheDocument();+ });++ it("keeps the discovered model when switching to a method already typed", async () => {+ const requests: OnPremRequest[] = [];+ const { user, getSettings, modelHistory } = renderOnboarding(+ "watsonx_onprem",+ false,+ savedZenKey,+ [onPremDiscoverHandler(requests)],+ withZenKeyField,+ );+ await screen.findByText(servedStatus, {}, DISCOVERY_TIMEOUT);++ await user.click(+ screen.getByRole("button", { name: /Username \+ API key/ }),+ );+ await user.type(screen.getByLabelText("Username"), "cpd-user");+ await user.type(screen.getByLabelText(/^API key/), "cpd-api-key");+ await waitFor(+ () => expect(requests.at(-1)?.auth_method).toBe("username_api_key"),+ DISCOVERY_TIMEOUT,+ );+ await user.click(screen.getByRole("button", { name: /Zen API key/ }));+ await settle();+ expect(getSettings().llm_model).toBe("ibm/granite-4-h-small");+ const switchedAt = modelHistory.length;++ await user.click(+ screen.getByRole("button", { name: /Username \+ API key/ }),+ );+ await settle();++ // The typed username already counts for the method switched to, so+ // the picker must not fall back to the configured model meanwhile.+ expect(modelHistory.slice(switchedAt)).not.toContain(+ "ibm/granite-3-8b-instruct",+ );+ expect(getSettings().llm_model).toBe("ibm/granite-4-h-small");+ });+ });+ });++ it("never asks a provider that cannot list its models", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ const { user } = renderOnboarding("openai_like", false, undefined, [+ http.post("/api/models/openai_like/discover", async ({ request }) => {+ requests.push(+ (await request.json()) as { credentials: Record<string, string> },+ );+ return HttpResponse.json({ models: [], embedding_models: [] });+ }),+ ]);++ await user.type(+ await screen.findByLabelText("API base"),+ "https://other.example",+ );+ await new Promise((resolve) => setTimeout(resolve, 800));++ expect(requests).toHaveLength(0);+ });+});diff --git a/frontend/app/onboarding/_components/generic-onboarding.tsx b/frontend/app/onboarding/_components/generic-onboarding.tsxindex 87dd677fd..352778ca5 100644--- a/frontend/app/onboarding/_components/generic-onboarding.tsx+++ b/frontend/app/onboarding/_components/generic-onboarding.tsx@@ -1,16 +1,13 @@import {type Dispatch,type SetStateAction,- useEffect,useMemo,useRef,useState,} from "react";import { useGetModelCatalogQuery } from "@/app/api/queries/useGetModelsQuery";-import { LabelInput } from "@/components/label-input";import {onboardingCredentialFields,- providerCatalogOptions,type SavedProvidersSnapshot,savedCredentialValuesForProvider,savedSecretFieldsForProvider,@@ -19,12 +16,23 @@ import {getProviderChrome,requiresExplicitModelSelection,} from "@/components/models/model-helpers";+import {+ activeCredentialKeys,+ authMethodCredentialKeys,+ clearedSavedCredentialKeys,+ credentialTargetChanged,+} from "@/components/models/provider-credentials";import { WatsonxSpaceSelect } from "@/components/models/watsonx-space-select";import { WatsonxTlsSettings } from "@/components/models/watsonx-tls-settings";import type { OnboardingVariables } from "../../api/mutations/useOnboardingMutation";import { AdvancedOnboarding } from "./advanced";-import { GenericProviderCredentialFields } from "./generic-provider-credential-fields";-import { AZURE_AUTH_GROUPS } from "./generic-provider-credential-fields.helpers";+import {+ GenericCredentialInput,+ GenericProviderCredentialFields,+} from "./generic-provider-credential-fields";+import { useOnboardingCompletionBlocker } from "./use-onboarding-completion-blocker";+import { useOnboardingModelDiscovery } from "./use-onboarding-model-discovery";+import { useOnboardingModelSelection } from "./use-onboarding-model-selection";/*** Onboarding step for a provider with no hand-built component.@@ -34,6 +42,9 @@ import { AZURE_AUTH_GROUPS } from "./generic-provider-credential-fields.helpers"* `config/model_providers.yaml` gets a working onboarding tab with no frontend* change. Credentials are submitted through the generic `provider_credentials`* payload the onboarding endpoint already accepts.+ *+ * `onCompletionBlockerChange` reports why Submit must wait (a saved secret to+ * re-enter for a changed endpoint), or `null` once nothing blocks it.*/export function GenericOnboarding({provider,@@ -41,12 +52,14 @@ export function GenericOnboarding({setSettings,isEmbedding = false,providers,+ onCompletionBlockerChange,}: {provider: string;displayName?: string;setSettings: Dispatch<SetStateAction<OnboardingVariables>>;isEmbedding?: boolean;providers?: SavedProvidersSnapshot;+ onCompletionBlockerChange?: (blocker: string | null) => void;}) {const { data: catalog } = useGetModelCatalogQuery();const chrome = getProviderChrome(provider, displayName);@@ -67,7 +80,15 @@ export function GenericOnboarding({const [credentials, setCredentials] =useState<Record<string, string>>(savedCredentials);- const [model, setModel] = useState("");+ // A saved secret belongs to the host it was saved for: once the endpoint or+ // TLS setting is edited — or a saved one cleared, which Submit sends as a+ // removal — it is neither offered for reuse nor sent anywhere, and the+ // backend refuses a Submit that leaves it blank.+ const targetChanged = credentialTargetChanged(credentials, savedCredentials);+ const reusableSecrets = useMemo(+ () => (targetChanged ? new Set<string>() : savedSecrets),+ [targetChanged, savedSecrets],+ );const [azureAuthMethod, setAzureAuthMethod] = useState(providers?.custom?.[provider]?.auth_method ?? "api_key",);@@ -86,33 +107,22 @@ export function GenericOnboarding({nextModel: string,) => {const submitted: Record<string, string> = {};- const removals: string[] = [];- const activeAzureFields = new Set([- "api_base",- "api_version",- ...(AZURE_AUTH_GROUPS.find((group) => group.key === azureAuthMethod)- ?.fields ?? []),- ]);- const activeOnPremFields = new Set([- "api_base",- "space_id",- "project_id",- "ssl_verify",- ...(onPremAuthMethod === "zen_api_key"- ? ["zen_api_key"]- : ["username", "api_key"]),- ]);+ const active = activeCredentialKeys(+ provider,+ azureAuthMethod,+ onPremAuthMethod,+ );for (const [key, value] of Object.entries(nextCredentials)) {- if (provider === "azure" && !activeAzureFields.has(key)) continue;- if (provider === "watsonx_onprem" && !activeOnPremFields.has(key))- continue;+ if (active && !active.has(key)) continue;const trimmed = (value ?? "").trim();- if (trimmed !== "") {- submitted[key] = trimmed;- } else if (!savedSecrets.has(key) && savedCredentials[key]) {- removals.push(key);- }+ if (trimmed !== "") submitted[key] = trimmed;}+ const removals = clearedSavedCredentialKeys(+ nextCredentials,+ savedCredentials,+ savedSecrets,+ active,+ );setSettingsRef.current((prev) => {const providerCredentialRemovals = {...prev.provider_credential_removals,@@ -155,35 +165,57 @@ export function GenericOnboarding({});};- const models = useMemo(- () =>- providerCatalogOptions(- catalog,- provider,- isEmbedding ? "embedding" : "language",- ),- [catalog, provider, isEmbedding],+ // Memoised: model discovery filters by this set, and a fresh one each render+ // would redo that work every render.+ const activeKeys = useMemo(+ () => activeCredentialKeys(provider, azureAuthMethod, onPremAuthMethod),+ [provider, azureAuthMethod, onPremAuthMethod],);- // Azure's catalogue lists model families, not this customer's deployments,- // so require an explicit choice. Other providers still default to the- // highest-ranked model when the catalogue loads or provider changes.- const defaultedModelRef = useRef<string | undefined>(undefined);- useEffect(() => {- if (- requiresExplicitModelSelection(provider) ||- model ||- models.length === 0- )- return;- const defaultModel = models[0].value;- // Only set once per provider so switching back doesn't re-default.- if (defaultedModelRef.current === `${provider}:${defaultModel}`) return;- defaultedModelRef.current = `${provider}:${defaultModel}`;- setModel(defaultModel);- syncParentSettings(credentials, defaultModel);- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [models, model, provider]);+ const reenterKeys = useOnboardingCompletionBlocker({+ credentials,+ savedSecrets,+ activeKeys,+ targetChanged,+ fields,+ onCompletionBlockerChange,+ });++ const {+ discovers,+ models,+ status: discoveryStatus,+ inFlight: discoveryInFlight,+ } = useOnboardingModelDiscovery({+ catalog,+ provider,+ isEmbedding,+ fields,+ credentials,+ activeKeys,+ requiredKeys: authMethodCredentialKeys(+ provider,+ azureAuthMethod,+ onPremAuthMethod,+ ),+ // Every method's, unfiltered: discovery applies the chosen method's keys+ // after debouncing what was typed, so a method switch is not debounced.+ clearedKeys: clearedSavedCredentialKeys(+ credentials,+ savedCredentials,+ savedSecrets,+ null,+ ),+ savedSecrets: reusableSecrets,+ authMethod: provider === "watsonx_onprem" ? onPremAuthMethod : undefined,+ });++ const { model, pickModel } = useOnboardingModelSelection({+ provider,+ models,+ inFlight: discoveryInFlight,+ onModelChange: (nextModel) => syncParentSettings(credentials, nextModel),+ });const handleCredentialChange = (fieldKey: string, newValue: string) => {const nextCredentials = { ...credentials, [fieldKey]: newValue };@@ -191,52 +223,31 @@ export function GenericOnboarding({syncParentSettings(nextCredentials, model);};- const handleModelChange = (newModel: string) => {- setModel(newModel);- syncParentSettings(credentials, newModel);- };-- const handleAzureAuthMethodChange = (method: string) => {- setAzureAuthMethod(method);+ const handleAuthMethodChange = (+ target: "azure" | "watsonx_onprem",+ method: string,+ ) => {+ const isAzure = target === "azure";+ (isAzure ? setAzureAuthMethod : setOnPremAuthMethod)(method);// The closure still holds the previous method during this event; submit// just the new method's fields explicitly so inactive values stay local.- const active = new Set([- "api_base",- "api_version",- ...(AZURE_AUTH_GROUPS.find((group) => group.key === method)?.fields ??- []),- ]);- const selected = Object.fromEntries(- Object.entries(credentials).filter(([key]) => active.has(key)),+ const active = activeCredentialKeys(+ target,+ isAzure ? method : azureAuthMethod,+ isAzure ? onPremAuthMethod : method,);- setSettings((prev) => ({- ...prev,- provider_credentials: { ...prev.provider_credentials, azure: selected },- provider_auth_methods: { ...prev.provider_auth_methods, azure: method },- }));- };-- const handleOnPremAuthMethodChange = (method: string) => {- setOnPremAuthMethod(method);- const active = new Set([- "api_base",- "space_id",- "project_id",- "ssl_verify",- ...(method === "zen_api_key" ? ["zen_api_key"] : ["username", "api_key"]),- ]);const selected = Object.fromEntries(- Object.entries(credentials).filter(([key]) => active.has(key)),+ Object.entries(credentials).filter(([key]) => active?.has(key)),);setSettings((prev) => ({...prev,provider_credentials: {...prev.provider_credentials,- watsonx_onprem: selected,+ [target]: selected,},provider_auth_methods: {...prev.provider_auth_methods,- watsonx_onprem: method,+ [target]: method,},}));};@@ -249,8 +260,8 @@ export function GenericOnboarding({idPrefix={`onboarding-${provider}`}credentials={credentials}authMethod={onPremAuthMethod}- hasSavedApiKey={savedSecrets.has("api_key")}- hasSavedZenApiKey={savedSecrets.has("zen_api_key")}+ hasSavedApiKey={reusableSecrets.has("api_key")}+ hasSavedZenApiKey={reusableSecrets.has("zen_api_key")}value={credentials.space_id}onValueChange={(value) => handleCredentialChange("space_id", value)}helperText={field.tooltip ?? undefined}@@ -269,29 +280,16 @@ export function GenericOnboarding({);}- const isSecret =- field.field_type === "password" || field.field_type === "textarea";- const hasSaved = isSecret && savedSecrets.has(field.key);return (- <div key={field.key} className="space-y-1">- <LabelInput- label={field.label}- helperText={field.tooltip ?? ""}- id={`onboarding-${provider}-${field.key}`}- type={field.field_type === "password" ? "password" : "text"}- required={field.required && !hasSaved}- placeholder={- hasSaved ? "•••••••••" : (field.placeholder ?? undefined)- }- value={credentials[field.key] ?? ""}- onChange={(e) => handleCredentialChange(field.key, e.target.value)}- />- {hasSaved && (- <p className="text-mmd text-muted-foreground">- A value is already saved. Leave this blank to keep it.- </p>- )}- </div>+ <GenericCredentialInput+ key={field.key}+ field={field}+ id={`onboarding-${provider}-${field.key}`}+ value={credentials[field.key] ?? ""}+ hasSavedSecret={reusableSecrets.has(field.key)}+ needsReentry={reenterKeys.includes(field.key)}+ onChange={(value) => handleCredentialChange(field.key, value)}+ />);};@@ -303,11 +301,20 @@ export function GenericOnboarding({fields={fields}azureAuthMethod={azureAuthMethod}onPremAuthMethod={onPremAuthMethod}- onAzureAuthMethodChange={handleAzureAuthMethodChange}- onOnPremAuthMethodChange={handleOnPremAuthMethodChange}+ onAzureAuthMethodChange={(method) =>+ handleAuthMethodChange("azure", method)+ }+ onOnPremAuthMethodChange={(method) =>+ handleAuthMethodChange("watsonx_onprem", method)+ }renderField={renderField}/>- {models.length === 0 && (+ {discoveryStatus && (+ <p className="text-mmd text-muted-foreground" role="status">+ {discoveryStatus}+ </p>+ )}+ {models.length === 0 && !discovers && (<p className="text-mmd text-muted-foreground">{chrome.name} publishes no {isEmbedding ? "embedding" : "language"}{" "}models in the catalogue. Pick a different provider for this step.@@ -325,8 +332,8 @@ export function GenericOnboarding({embeddingModels={isEmbedding ? models : undefined}languageModel={isEmbedding ? undefined : model}embeddingModel={isEmbedding ? model : undefined}- setLanguageModel={isEmbedding ? undefined : handleModelChange}- setEmbeddingModel={isEmbedding ? handleModelChange : undefined}+ setLanguageModel={isEmbedding ? undefined : pickModel}+ setEmbeddingModel={isEmbedding ? pickModel : undefined}/></>);diff --git a/frontend/app/onboarding/_components/generic-provider-credential-fields.helpers.ts b/frontend/app/onboarding/_components/generic-provider-credential-fields.helpers.tsdeleted file mode 100644index 8665ca25a..000000000--- a/frontend/app/onboarding/_components/generic-provider-credential-fields.helpers.ts+++ /dev/null@@ -1,35 +0,0 @@-import type { CatalogCredentialField } from "@/components/models/catalog-models";--export const AZURE_AUTH_GROUPS = [- { key: "api_key", label: "API key", fields: ["api_key"] },- {- key: "entra_token",- label: "Microsoft Entra access token",- fields: ["azure_ad_token"],- },- {- key: "service_principal",- label: "Microsoft Entra service principal",- fields: ["tenant_id", "client_id", "client_secret"],- },-] as const;--export const ON_PREM_AUTH_GROUPS = [- {- key: "username_api_key",- label: "Username + API key",- fields: ["username", "api_key"],- },- { key: "zen_api_key", label: "Zen API key", fields: ["zen_api_key"] },-] as const;--export function fieldsForKeys(- fields: CatalogCredentialField[],- keys: readonly string[],-) {- const byKey = new Map(fields.map((field) => [field.key, field]));- return keys.flatMap((key) => {- const field = byKey.get(key);- return field ? [field] : [];- });-}diff --git a/frontend/app/onboarding/_components/generic-provider-credential-fields.tsx b/frontend/app/onboarding/_components/generic-provider-credential-fields.tsxindex 8e69a2da0..125c889a1 100644--- a/frontend/app/onboarding/_components/generic-provider-credential-fields.tsx+++ b/frontend/app/onboarding/_components/generic-provider-credential-fields.tsx@@ -1,16 +1,18 @@import type { ReactNode } from "react";+import { LabelInput } from "@/components/label-input";import type { CatalogCredentialField } from "@/components/models/catalog-models";+import {+ AZURE_AUTH_GROUPS,+ fieldsForKeys,+ ON_PREM_AUTH_GROUPS,+ SECRET_REENTRY_HINT,+} from "@/components/models/provider-credentials";import {Accordion,AccordionContent,AccordionItem,AccordionTrigger,} from "@/components/ui/accordion";-import {- AZURE_AUTH_GROUPS,- fieldsForKeys,- ON_PREM_AUTH_GROUPS,-} from "./generic-provider-credential-fields.helpers";export function GenericProviderCredentialFields({provider,@@ -109,3 +111,50 @@ export function GenericProviderCredentialFields({}return <>{fields.map(renderField)}</>;}++/**+ * One catalogue-described credential input, with the hints for a saved secret+ * that may be kept or must be entered again.+ */+export function GenericCredentialInput({+ field,+ id,+ value,+ hasSavedSecret,+ needsReentry,+ onChange,+}: {+ field: CatalogCredentialField;+ id: string;+ value: string;+ /** A saved value for this key may be reused (the target is unchanged). */+ hasSavedSecret: boolean;+ needsReentry: boolean;+ onChange: (value: string) => void;+}) {+ const isSecret =+ field.field_type === "password" || field.field_type === "textarea";+ const hasSaved = isSecret && hasSavedSecret;+ return (+ <div className="space-y-1">+ <LabelInput+ label={field.label}+ helperText={field.tooltip ?? ""}+ id={id}+ type={field.field_type === "password" ? "password" : "text"}+ required={field.required && !hasSaved}+ placeholder={hasSaved ? "•••••••••" : (field.placeholder ?? undefined)}+ value={value}+ onChange={(e) => onChange(e.target.value)}+ />+ {hasSaved && (+ <p className="text-mmd text-muted-foreground">+ A value is already saved. Leave this blank to keep it.+ </p>+ )}+ {needsReentry && (+ <p className="text-mmd text-muted-foreground">{SECRET_REENTRY_HINT}</p>+ )}+ </div>+ );+}diff --git a/frontend/app/onboarding/_components/onboarding-card.test.tsx b/frontend/app/onboarding/_components/onboarding-card.test.tsxnew file mode 100644index 000000000..e74d03058--- /dev/null+++ b/frontend/app/onboarding/_components/onboarding-card.test.tsx@@ -0,0 +1,122 @@+import { HttpResponse, http } from "msw";+import { describe, expect, it } from "vitest";+import type { ModelProvidersResponse } from "@/app/api/queries/useGetModelProvidersQuery";+import {+ renderWithProviders,+ screen,+ userEvent,+ waitFor,+} from "@/test-utils/render";+import OnboardingCard from "./onboarding-card";++const providers: ModelProvidersResponse = {+ run_mode: "oss",+ providers: [+ { name: "openai", display_name: "OpenAI" },+ { name: "anthropic", display_name: "Anthropic" },+ { name: "rhoai", display_name: "Red Hat OpenShift AI" },+ ],+};++const catalog = {+ providers: [+ {+ key: "openai",+ name: "OpenAI",+ credential_fields: [],+ models: [{ model: "gpt-5.5", capabilities: ["function_calling"] }],+ embedding_models: [{ model: "text-embedding-3-small" }],+ },+ {+ // Serves no embedding models, and cannot discover any.+ key: "anthropic",+ name: "Anthropic",+ credential_fields: [],+ models: [{ model: "claude-sonnet-5-5" }],+ embedding_models: [],+ },+ {+ // No shipped fallback rows: the lists stay empty until the cluster is+ // asked with the credentials typed into this step.+ key: "rhoai",+ name: "Red Hat OpenShift AI",+ discovers_models: true,+ credential_fields: [+ {+ key: "api_base",+ label: "Chat endpoint",+ required: true,+ field_type: "text",+ },+ {+ key: "embedding_api_base",+ label: "Embedding endpoint",+ required: false,+ field_type: "text",+ },+ {+ key: "api_key",+ label: "Token",+ required: true,+ field_type: "password",+ },+ ],+ models: [],+ embedding_models: [],+ },+ ],+};++function renderEmbeddingStep() {+ const user = userEvent.setup();+ renderWithProviders(<OnboardingCard onComplete={() => {}} isEmbedding />, {+ providers: ["tooltip"],+ handlers: [+ http.get("/api/models/providers", () => HttpResponse.json(providers)),+ http.get("/api/models/catalog", () => HttpResponse.json(catalog)),+ ],+ });+ return { user };+}++// Until the catalogue loads every tab is shown, so the filter has only run+// once a provider without embedding models has dropped out.+async function waitForCatalogueFilter() {+ await screen.findByRole("tab", { name: /OpenAI/ });+ await waitFor(() =>+ expect(+ screen.queryByRole("tab", { name: /Anthropic/ }),+ ).not.toBeInTheDocument(),+ );+}++describe("OnboardingCard embedding step", () => {+ it("drops a provider that serves no embedding models", async () => {+ renderEmbeddingStep();++ await waitForCatalogueFilter();++ expect(screen.getByRole("tab", { name: /OpenAI/ })).toBeInTheDocument();+ });++ it("keeps a provider that discovers its models before any are listed", async () => {+ renderEmbeddingStep();++ await waitForCatalogueFilter();++ expect(+ screen.getByRole("tab", { name: /Red Hat OpenShift AI/ }),+ ).toBeInTheDocument();+ });++ it("lets the operator reach the credentials that discovery needs", async () => {+ const { user } = renderEmbeddingStep();++ await waitForCatalogueFilter();+ await user.click(screen.getByRole("tab", { name: /Red Hat OpenShift AI/ }));++ expect(+ await screen.findByRole("textbox", { name: /Chat endpoint/ }),+ ).toBeInTheDocument();+ });+});diff --git a/frontend/app/onboarding/_components/onboarding-card.tsx b/frontend/app/onboarding/_components/onboarding-card.tsxindex 956c86459..c27e4084a 100644--- a/frontend/app/onboarding/_components/onboarding-card.tsx+++ b/frontend/app/onboarding/_components/onboarding-card.tsx@@ -20,6 +20,7 @@ import {import { useGetTasksQuery } from "@/app/api/queries/useGetTasksQuery";import type { ProviderHealthResponse } from "@/app/api/queries/useProviderHealthQuery";import { useDoclingHealth } from "@/components/docling-health-banner";+import { providerDiscoversModels } from "@/components/models/catalog-models";import {EMBEDDING_PROVIDER_ORDER,getProviderChrome,@@ -66,7 +67,9 @@ const STEP_LIST = [];// Providers whose model inventory is read live from the running service, so an-// empty catalogue list says nothing about what they can serve.+// empty catalogue list says nothing about what they can serve. Provider+// enhancements that list their models from typed credentials+// (`discovers_models`) are in the same position until onboarding asks them.const LIVE_MODEL_PROVIDERS = new Set(["ollama", "watsonx"]);const EMBEDDING_STEP_LIST = [@@ -120,13 +123,18 @@ const OnboardingCard = ({// The embedding step must not offer a provider that serves no embedding// models — Anthropic being the standing example. The catalogue answers that// for every provider except the two whose inventory comes from the running- // server rather than LiteLLM's bundled list.+ // server rather than LiteLLM's bundled list, and those that discover their+ // models from the credentials typed here — for them an empty list means "not+ // asked yet", and hiding the tab would leave nowhere to type them.const tabProviders = useMemo(() => {if (!isEmbedding) {return providerKeys;}return providerKeys.filter((providerKey) => {- if (LIVE_MODEL_PROVIDERS.has(providerKey)) {+ if (+ LIVE_MODEL_PROVIDERS.has(providerKey) ||+ providerDiscoversModels(catalog, providerKey)+ ) {return true;}const entry = catalog?.providers?.find(@@ -201,6 +209,29 @@ const OnboardingCard = ({}}+ // Why Submit must wait, per provider tab — a generic provider whose endpoint+ // changed needs its saved secret typed again. Keyed by provider so a tab+ // reporting late can never block another one.+ const [credentialBlockers, setCredentialBlockers] = useState<+ Record<string, string>+ >({});+ const handleCompletionBlockerChange = (+ provider: string,+ blocker: string | null,+ ) => {+ setCredentialBlockers((prev) => {+ if ((prev[provider] ?? null) === blocker) return prev;+ const next = { ...prev };+ if (blocker) {+ next[provider] = blocker;+ } else {+ delete next[provider];+ }+ return next;+ });+ };+ const credentialBlocker = credentialBlockers[modelProvider] ?? null;+const handleSetModelProvider = (provider: string) => {setModelProvider(provider);setSettings({@@ -520,6 +551,12 @@ const OnboardingCard = ({toast.error("Please complete all required fields");return;}+ // The button is disabled too; this keeps a stale secret from ever being+ // submitted for a changed endpoint, which the backend would refuse.+ if (credentialBlocker) {+ toast.error(credentialBlocker);+ return;+ }// Clear any previous errorsetError(null);@@ -595,6 +632,7 @@ const OnboardingCard = ({isEmbedding,llmModel: settings.llm_model ?? "",embeddingModel: settings.embedding_model ?? "",+ credentialBlocker,});return (@@ -765,6 +803,9 @@ const OnboardingCard = ({setSettings={setSettings}isEmbedding={isEmbedding}providers={currentSettings?.providers}+ onCompletionBlockerChange={(blocker) =>+ handleCompletionBlockerChange(providerKey, blocker)+ }/>)}</TabsContent>@@ -789,7 +830,8 @@ const OnboardingCard = ({<TooltipContent>{isLoadingModels? "Loading models..."- : "Please fill in all required fields"}+ : (credentialBlocker ??+ "Please fill in all required fields")}</TooltipContent>)}</Tooltip>diff --git a/frontend/app/onboarding/_components/onboarding-completion.test.ts b/frontend/app/onboarding/_components/onboarding-completion.test.tsindex 6ae693693..ef4bc039b 100644--- a/frontend/app/onboarding/_components/onboarding-completion.test.ts+++ b/frontend/app/onboarding/_components/onboarding-completion.test.ts@@ -41,6 +41,33 @@ describe("canCompleteOnboarding", () => {});});+describe("canCompleteOnboarding with a credential blocker", () => {+ it("blocks Submit while the provider form reports a reason", () => {+ assert.equal(+ canCompleteOnboarding({+ isEmbedding: false,+ llmModel: "granite-chat",+ embeddingModel: "",+ credentialBlocker:+ "Enter Token again: the endpoint or TLS setting changed.",+ }),+ false,+ );+ });++ it("allows Submit once the reason is cleared", () => {+ assert.equal(+ canCompleteOnboarding({+ isEmbedding: true,+ llmModel: "",+ embeddingModel: "granite-embedding",+ credentialBlocker: null,+ }),+ true,+ );+ });+});+describe("shouldRollbackFailedOnboarding", () => {it("does not roll back an initial validation failure with no saved config", () => {assert.equal(shouldRollbackFailedOnboarding(false), false);diff --git a/frontend/app/onboarding/_components/onboarding-completion.ts b/frontend/app/onboarding/_components/onboarding-completion.tsindex d9913d21c..0d22e8bcd 100644--- a/frontend/app/onboarding/_components/onboarding-completion.ts+++ b/frontend/app/onboarding/_components/onboarding-completion.ts@@ -1,15 +1,23 @@import type { QueryClient } from "@tanstack/react-query";import { getSettings } from "@/app/api/queries/useGetSettingsQuery";+/**+ * Whether the step can be submitted. `credentialBlocker` is a reason the+ * provider form reported (a saved secret to re-enter for a changed endpoint);+ * the backend would refuse that Submit, so it is blocked here instead.+ */export function canCompleteOnboarding({isEmbedding,llmModel,embeddingModel,+ credentialBlocker = null,}: {isEmbedding: boolean;llmModel: string;embeddingModel: string;+ credentialBlocker?: string | null;}): boolean {+ if (credentialBlocker) return false;return isEmbedding ? !!embeddingModel : !!llmModel;}diff --git a/frontend/app/onboarding/_components/use-onboarding-completion-blocker.ts b/frontend/app/onboarding/_components/use-onboarding-completion-blocker.tsnew file mode 100644index 000000000..0e7cbae01--- /dev/null+++ b/frontend/app/onboarding/_components/use-onboarding-completion-blocker.ts@@ -0,0 +1,52 @@+import { useEffect, useRef } from "react";+import type { CatalogCredentialField } from "@/components/models/catalog-models";+import { secretsToReenter } from "@/components/models/provider-credentials";++/**+ * The saved secrets that must be typed again before Submit, reported to the+ * parent as one readable reason (or `null` once nothing blocks it).+ *+ * A saved secret is not sent to an endpoint or TLS setting other than the one+ * it was saved for, so changing the target leaves those fields to re-enter.+ * Returns their keys so each field can say why.+ */+export function useOnboardingCompletionBlocker({+ credentials,+ savedSecrets,+ activeKeys,+ targetChanged,+ fields,+ onCompletionBlockerChange,+}: {+ credentials: Record<string, string>;+ savedSecrets: Set<string>;+ /** The keys the chosen auth method uses, or null when every field applies. */+ activeKeys: Set<string> | null;+ targetChanged: boolean;+ fields: CatalogCredentialField[];+ onCompletionBlockerChange?: (blocker: string | null) => void;+}): string[] {+ const reenterKeys = secretsToReenter(+ credentials,+ savedSecrets,+ activeKeys,+ targetChanged,+ );+ const reenterLabels = reenterKeys.map(+ (key) => fields.find((field) => field.key === key)?.label ?? key,+ );+ // A string, so the effect below re-runs only when the reason changes.+ const completionBlocker =+ reenterLabels.length > 0+ ? `Enter ${reenterLabels.join(", ")} again: the endpoint or TLS setting changed.`+ : null;+ const onCompletionBlockerChangeRef = useRef(onCompletionBlockerChange);+ onCompletionBlockerChangeRef.current = onCompletionBlockerChange;+ useEffect(() => {+ onCompletionBlockerChangeRef.current?.(completionBlocker);+ }, [completionBlocker]);+ // A tab that is switched away from must not keep blocking the next one.+ useEffect(() => () => onCompletionBlockerChangeRef.current?.(null), []);++ return reenterKeys;+}diff --git a/frontend/app/onboarding/_components/use-onboarding-model-discovery.ts b/frontend/app/onboarding/_components/use-onboarding-model-discovery.tsnew file mode 100644index 000000000..a5936596a--- /dev/null+++ b/frontend/app/onboarding/_components/use-onboarding-model-discovery.ts@@ -0,0 +1,155 @@+import { useMemo } from "react";+import { useDiscoverProviderModelsQuery } from "@/app/api/queries/useDiscoverProviderModelsQuery";+import {+ type CatalogCredentialField,+ type CatalogSelectOption,+ providerCatalogOptions,+ providerDiscoversModels,+ withDiscoveredModels,+} from "@/components/models/catalog-models";+import { useDebouncedValue } from "@/lib/debounce";++type Catalog = Parameters<typeof providerCatalogOptions>[0];++export interface OnboardingModelDiscovery {+ /** Whether the provider can list its own models from unsaved credentials. */+ discovers: boolean;+ /** Picker options: what the cluster serves, or the catalogue's rows. */+ models: CatalogSelectOption[];+ /** One line on where `models` came from, or null when there is nothing to say. */+ status: string | null;+ /**+ * No answer yet for the credentials as typed. A changed credential starts a+ * new query with no data, so `models` briefly falls back to the catalogue.+ */+ inFlight: boolean;+}++/**+ * The model picker's options for an onboarding provider, asking the cluster+ * what it serves when the provider can say.+ *+ * A cluster-hosted provider serves whatever its operator deployed, and the+ * catalogue can only list it once credentials are saved. This asks the cluster+ * with what has been typed so far, so the picker offers what it serves rather+ * than the configured fallback. Any other provider gets the catalogue's rows.+ */+export function useOnboardingModelDiscovery({+ catalog,+ provider,+ isEmbedding,+ fields,+ credentials,+ activeKeys,+ requiredKeys = [],+ clearedKeys = [],+ savedSecrets,+ authMethod,+}: {+ catalog: Catalog;+ provider: string;+ isEmbedding: boolean;+ fields: CatalogCredentialField[];+ credentials: Record<string, string>;+ /** The keys the chosen auth method uses, or null when every field applies. */+ activeKeys: Set<string> | null;+ /**+ * Keys the chosen auth method needs filled even though the catalogue marks+ * them optional. Without one the backend has nothing to authenticate with+ * and answers without asking the cluster, which would read as a failure.+ */+ requiredKeys?: readonly string[];+ /**+ * Saved non-secret fields the operator emptied, for every auth method. Sent+ * as `""` so the backend does not fill the saved value back in — Submit+ * removes them. Only the chosen method's keys are sent.+ */+ clearedKeys?: string[];+ savedSecrets: Set<string>;+ authMethod?: string;+}): OnboardingModelDiscovery {+ const discovers = providerDiscoversModels(catalog, provider);+ const typedCredentials: Record<string, string> = {};+ for (const [key, value] of Object.entries(credentials)) {+ const trimmed = (value ?? "").trim();+ if (trimmed) typedCredentials[key] = trimmed;+ }+ for (const key of clearedKeys) typedCredentials[key] = "";+ // Only typing is debounced. The auth method is a discrete choice, so its+ // keys are applied after the debounce: a switch asks once, at once, with+ // the new method's fields — never the old method's fields under the new+ // method's name, which would be a second, wasted request.+ // Debounced as a string: a fresh object each render would reset the timer.+ const typedKey = useDebouncedValue(JSON.stringify(typedCredentials), 500);+ const discoveryCredentials = useMemo(() => {+ const typed = JSON.parse(typedKey) as Record<string, string>;+ if (!activeKeys) return typed;+ return Object.fromEntries(+ Object.entries(typed).filter(([key]) => activeKeys.has(key)),+ );+ }, [typedKey, activeKeys]);+ const hasValue = (key: string) =>+ Boolean(discoveryCredentials[key]) || savedSecrets.has(key);+ const activeFields = fields.filter(+ (field) => !activeKeys || activeKeys.has(field.key),+ );+ const ready =+ discovers &&+ activeFields.every((field) => !field.required || hasValue(field.key)) &&+ activeFields+ .filter((field) => field.field_type === "password")+ .every((field) => hasValue(field.key)) &&+ requiredKeys.every(hasValue);+ const discovery = useDiscoverProviderModelsQuery(+ {+ provider,+ credentials: discoveryCredentials,+ authMethod,+ kind: isEmbedding ? "embedding" : "chat",+ },+ { enabled: ready },+ );+ const discovered = ready ? discovery.data : undefined;+ const discoveredHalf = isEmbedding+ ? discovered?.embedding_models+ : discovered?.models;+ const inFlight = ready && (discovery.isPending || discovery.isFetching);++ const models = useMemo(+ () =>+ providerCatalogOptions(+ withDiscoveredModels(catalog, provider, discovered),+ provider,+ isEmbedding ? "embedding" : "language",+ ),+ [catalog, provider, isEmbedding, discovered],+ );++ const kindLabel = isEmbedding ? "embedding" : "language";+ // A provider that discovers its models may configure no fallback at all, so+ // an empty picker means "not asked yet" or "could not ask", never "this+ // provider has nothing to offer".+ let status: string | null = null;+ if (discovers && !ready) {+ if (models.length === 0) {+ status = `Enter the credentials above to list the ${kindLabel} models the cluster serves.`;+ }+ } else if (ready) {+ if (inFlight) {+ status = "Checking which models the cluster serves…";+ } else if (discovery.isError || !discoveredHalf) {+ status =+ models.length > 0+ ? `Couldn't list ${kindLabel} models from the cluster — showing configured defaults. Type a model ID to use another.`+ : `Couldn't list ${kindLabel} models from the cluster. Type a model ID to continue.`;+ } else if (discoveredHalf.length > 0) {+ status = `Showing ${discoveredHalf.length} ${kindLabel} ${+ discoveredHalf.length === 1 ? "model" : "models"+ } served by the cluster.`;+ } else {+ status = `The cluster lists no ${kindLabel} models. Type a model ID to continue.`;+ }+ }++ return { discovers, models, status, inFlight };+}diff --git a/frontend/app/onboarding/_components/use-onboarding-model-selection.ts b/frontend/app/onboarding/_components/use-onboarding-model-selection.tsnew file mode 100644index 000000000..196944bd4--- /dev/null+++ b/frontend/app/onboarding/_components/use-onboarding-model-selection.ts@@ -0,0 +1,62 @@+import { useEffect, useEffectEvent, useRef, useState } from "react";+import type { CatalogSelectOption } from "@/components/models/catalog-models";+import { requiresExplicitModelSelection } from "@/components/models/model-helpers";++export interface OnboardingModelSelection {+ /** The model Submit uses: the user's pick, or the list's default. */+ model: string;+ /** Records the user's own choice, which then wins over the default. */+ pickModel: (model: string) => void;+}++/**+ * The model an onboarding step submits, given the picker's current options.+ *+ * Only the user's own pick is state; otherwise the model follows the list.+ * Azure's catalogue lists model families, not this customer's deployments,+ * so it requires an explicit choice. Other providers default to the+ * highest-ranked model, which tracks the list as discovery replaces the+ * configured fallback — a model the cluster does not serve never stays+ * selected — while a pick is kept even when the cluster does not list it.+ */+export function useOnboardingModelSelection({+ provider,+ models,+ inFlight,+ onModelChange,+}: {+ provider: string;+ models: CatalogSelectOption[];+ /** Discovery has no answer yet for the credentials as typed. */+ inFlight: boolean;+ /** Called on every change of the effective model, never on mount with "". */+ onModelChange: (model: string) => void;+}): OnboardingModelSelection {+ const [pickedModel, setPickedModel] = useState<string | null>(null);+ const defaultModel = requiresExplicitModelSelection(provider)+ ? ""+ : (models[0]?.value ?? "");+ // While the cluster is asked again the list briefly falls back to the+ // configured rows: keep the last settled default rather than flick to one of+ // those and back. With nothing to keep, follow the list: saved credentials+ // start discovery as soon as the catalogue loads, and a slow cluster must+ // not leave Submit without a model meanwhile.+ const [settledDefault, setSettledDefault] = useState("");+ if (!inFlight && settledDefault !== defaultModel) {+ setSettledDefault(defaultModel);+ }+ const model =+ pickedModel ?? (inFlight && settledDefault ? settledDefault : defaultModel);++ const notifyModelChange = useEffectEvent(onModelChange);+ // Mirror every change, including an automatic choice the list no longer+ // offers; mount stays a no-op so an empty model isn't written up front.+ const syncedModelRef = useRef("");+ useEffect(() => {+ if (model === syncedModelRef.current) return;+ syncedModelRef.current = model;+ notifyModelChange(model);+ }, [model]);++ return { model, pickModel: setPickedModel };+}diff --git a/frontend/app/settings/_components/provider-settings-dialog.test.tsx b/frontend/app/settings/_components/provider-settings-dialog.test.tsxindex fc24de4ff..adf4047df 100644--- a/frontend/app/settings/_components/provider-settings-dialog.test.tsx+++ b/frontend/app/settings/_components/provider-settings-dialog.test.tsx@@ -106,6 +106,9 @@ describe("ProviderSettingsDialog watsonx.ai on-prem TLS", () => {await user.click(screen.getByRole("switch", { name: "Verify TLS certificates" }),);+ // A changed TLS setting is a new credential target: the saved key is not+ // reused, so it has to be typed again.+ await user.type(screen.getByLabelText(/^API key/), "new-key");await user.click(screen.getByRole("button", { name: "Save" }));await waitFor(() => {@@ -114,6 +117,7 @@ describe("ProviderSettingsDialog watsonx.ai on-prem TLS", () => {watsonx_onprem: {api_base: "https://cpd.example.com",username: "cpd-user",+ api_key: "new-key",ssl_verify: "false",},},@@ -273,3 +277,254 @@ describe("ProviderSettingsDialog watsonx.ai on-prem TLS", () => {});});});++const SAVED_HINT = "A value is already saved. Leave this blank to keep it.";+const REENTRY_HINT =+ "The endpoint or TLS setting changed, so the saved value is not sent to it. Enter it again.";++const azureCatalog = {+ providers: [+ {+ key: "azure",+ name: "Azure OpenAI",+ credential_fields: [+ {+ key: "api_base",+ label: "Endpoint",+ required: true,+ field_type: "text",+ },+ {+ key: "api_key",+ label: "API key",+ required: false,+ field_type: "password",+ },+ {+ key: "azure_ad_token",+ label: "Access token",+ required: false,+ field_type: "password",+ },+ {+ key: "api_version",+ label: "API version",+ required: false,+ field_type: "text",+ },+ ],+ models: [],+ embedding_models: [],+ },+ ],+};++function renderDialog({+ provider,+ providerCatalog,+ credentialValues,+ secretFields,+ authMethod,+}: {+ provider: string;+ providerCatalog: typeof catalog;+ credentialValues: Record<string, string>;+ secretFields: string[];+ authMethod: string;+}) {+ const result: {+ submitted?: UpdateSettingsRequest;+ discoveryRequests: number;+ } = { discoveryRequests: 0 };+ const settings = makeSettings({+ providers: {+ custom: {+ [provider]: {+ configured: true,+ auth_method: authMethod,+ credential_values: credentialValues,+ secret_fields: secretFields,+ },+ },+ },+ });++ renderWithProviders(+ <ProviderSettingsDialog+ provider={provider}+ displayName={provider}+ open+ setOpen={() => {}}+ />,+ {+ providers: ["auth", "tooltip"],+ handlers: [+ http.get("/api/settings", () => HttpResponse.json(settings)),+ http.get("/api/models/catalog", () =>+ HttpResponse.json(providerCatalog),+ ),+ http.post("/api/models/watsonx_onprem/spaces", () => {+ result.discoveryRequests += 1;+ return HttpResponse.json({ spaces: [] });+ }),+ http.post("/api/settings", async ({ request }) => {+ result.submitted = (await request.json()) as UpdateSettingsRequest;+ return HttpResponse.json({ message: "saved", settings });+ }),+ http.post("/api/models/openai", () =>+ HttpResponse.json({ models: [], embedding_models: [] }),+ ),+ ],+ },+ );+ return result;+}++const onPremSaved = {+ provider: "watsonx_onprem",+ providerCatalog: catalog,+ credentialValues: {+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ ssl_verify: "true",+ },+ secretFields: ["api_key"],+ authMethod: "username_api_key",+};++describe("ProviderSettingsDialog credential target changes", () => {+ it("offers to keep a saved secret while the target is unchanged", async () => {+ renderDialog(onPremSaved);++ expect(await screen.findByText(SAVED_HINT)).toBeInTheDocument();+ expect(screen.queryByText(REENTRY_HINT)).not.toBeInTheDocument();+ });++ it("requires the saved secret again once the endpoint changes", async () => {+ const result = renderDialog(onPremSaved);+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Cluster URL/);+ await screen.findByText(SAVED_HINT);+ await user.clear(endpoint);+ await user.type(endpoint, "https://other.example.com");++ expect(screen.queryByText(SAVED_HINT)).not.toBeInTheDocument();+ expect(screen.getByText(REENTRY_HINT)).toBeInTheDocument();++ await user.click(screen.getByRole("button", { name: "Save" }));++ expect(await screen.findByText("API key is required")).toBeInTheDocument();+ expect(result.submitted).toBeUndefined();+ });++ it("saves the new endpoint once the secret is typed again", async () => {+ const result = renderDialog(onPremSaved);+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Cluster URL/);+ await screen.findByText(SAVED_HINT);+ await user.clear(endpoint);+ await user.type(endpoint, "https://other.example.com");+ await user.type(screen.getByLabelText(/^API key/), "new-key");+ await user.click(screen.getByRole("button", { name: "Save" }));++ await waitFor(() => {+ expect(result.submitted?.provider_credentials?.watsonx_onprem).toEqual({+ api_base: "https://other.example.com",+ username: "cpd-user",+ api_key: "new-key",+ ssl_verify: "true",+ });+ });+ });++ it("treats a cleared endpoint as a target change", async () => {+ const result = renderDialog(onPremSaved);+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Cluster URL/);+ await screen.findByText(SAVED_HINT);+ await user.clear(endpoint);++ expect(screen.getByText(REENTRY_HINT)).toBeInTheDocument();+ await user.click(screen.getByRole("button", { name: "Save" }));++ expect(await screen.findByText("API key is required")).toBeInTheDocument();+ expect(result.submitted).toBeUndefined();+ });++ it("does not list deployment spaces with a key saved for another endpoint", async () => {+ const result = renderDialog(onPremSaved);+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Cluster URL/);+ await user.click(screen.getByRole("button", { name: "Advanced settings" }));+ const spaceSelect = screen.getByRole("combobox", {+ name: "Deployment space ID",+ });+ await waitFor(() => expect(spaceSelect).not.toBeDisabled());+ const requestsBeforeEdit = result.discoveryRequests;++ await user.clear(endpoint);+ await user.type(endpoint, "https://other.example.com");++ await waitFor(() => expect(spaceSelect).toBeDisabled());+ expect(result.discoveryRequests).toBe(requestsBeforeEdit);+ });++ it("does not demand a saved secret of an auth method that is not chosen", async () => {+ const result = renderDialog({+ provider: "azure",+ providerCatalog: azureCatalog,+ credentialValues: { api_base: "https://old.openai.azure.com" },+ secretFields: ["api_key"],+ authMethod: "api_key",+ });+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Endpoint/);+ await screen.findByText(SAVED_HINT);+ await user.click(+ screen.getByRole("button", { name: "Microsoft Entra access token" }),+ );+ await user.clear(endpoint);+ await user.type(endpoint, "https://new.openai.azure.com");+ await user.type(screen.getByLabelText(/^Access token/), "entra-token");+ await user.click(screen.getByRole("button", { name: "Save" }));++ await waitFor(() => {+ expect(result.submitted).toEqual({+ provider_credentials: {+ azure: {+ api_base: "https://new.openai.azure.com",+ azure_ad_token: "entra-token",+ },+ },+ provider_auth_methods: { azure: "entra_token" },+ });+ });+ });++ it("requires the chosen auth method's saved secret after the endpoint changes", async () => {+ const result = renderDialog({+ provider: "azure",+ providerCatalog: azureCatalog,+ credentialValues: { api_base: "https://old.openai.azure.com" },+ secretFields: ["api_key"],+ authMethod: "api_key",+ });+ const user = userEvent.setup();++ const endpoint = await screen.findByLabelText(/^Endpoint/);+ await screen.findByText(SAVED_HINT);+ await user.clear(endpoint);+ await user.type(endpoint, "https://new.openai.azure.com");++ expect(screen.getByText(REENTRY_HINT)).toBeInTheDocument();+ await user.click(screen.getByRole("button", { name: "Save" }));++ expect(await screen.findByText("API key is required")).toBeInTheDocument();+ expect(result.submitted).toBeUndefined();+ });+});diff --git a/frontend/app/settings/_components/provider-settings-dialog.tsx b/frontend/app/settings/_components/provider-settings-dialog.tsxindex 25f9d3795..9eda1f4be 100644--- a/frontend/app/settings/_components/provider-settings-dialog.tsx+++ b/frontend/app/settings/_components/provider-settings-dialog.tsx@@ -18,6 +18,11 @@ import {getProviderChrome,type ModelProvider,} from "@/components/models/model-helpers";+import {+ activeCredentialKeys,+ credentialTargetChanged,+ secretsToReenter,+} from "@/components/models/provider-credentials";import {Dialog,DialogContent,@@ -43,6 +48,10 @@ const EMPTY_FIELDS: CatalogCredentialField[] = [];* Most generic providers have no credential-only check on save; they are* validated when a model is selected in Agent or Ingestion settings. Azure* OpenAI is an exception: the backend probes its key before persisting it.+ *+ * A saved secret belongs to the endpoint and TLS setting it was saved for:+ * once either is edited (or a saved one cleared), the backend refuses to reuse+ * it, so the form stops offering "leave blank to keep" and requires it again.*/const ProviderSettingsDialog = ({provider,@@ -118,6 +127,27 @@ const ProviderSettingsDialog = ({const { handleSubmit } = methods;+ // Merged over the saved values so a target field the catalogue form does not+ // render reads as unchanged rather than cleared.+ const watchedCredentials = methods.watch("credentials") ?? {};+ const savedCredentialValues = saved?.credential_values ?? {};+ const targetChanged = credentialTargetChanged(+ { ...savedCredentialValues, ...watchedCredentials },+ savedCredentialValues,+ );+ const activeKeys = activeCredentialKeys(+ provider,+ azureAuthMethod,+ onPremAuthMethod,+ );+ const reenterSecretFields = secretsToReenter(+ watchedCredentials,+ new Set(savedSecretFields),+ activeKeys,+ targetChanged,+ );+ const reusableSecretFields = targetChanged ? [] : savedSecretFields;+const settingsMutation = useUpdateSettingsMutation({onSuccess: () => {toast.message(`${chrome.name} successfully configured`, {@@ -154,32 +184,10 @@ const ProviderSettingsDialog = ({// "leave unchanged". Empty non-secret fields explicitly remove saved data.const credentials: Record<string, string> = {};const removals: string[] = [];- const azureAuthFields: Record<string, Set<string>> = {- api_key: new Set(["api_key"]),- entra_token: new Set(["azure_ad_token"]),- service_principal: new Set(["tenant_id", "client_id", "client_secret"]),- };- const allowedAzureFields = new Set([- "api_base",- "api_version",- ...(azureAuthFields[azureAuthMethod] ?? []),- ]);- const allowedOnPremFields = new Set([- "api_base",- "space_id",- "project_id",- "ssl_verify",- ...(onPremAuthMethod === "zen_api_key"- ? ["zen_api_key"]- : ["username", "api_key"]),- ]);+ // Values typed for another auth method stay in the form but are not sent.const fieldsByKey = new Map(fields.map((field) => [field.key, field]));for (const [key, value] of Object.entries(data.credentials ?? {})) {- if (provider === "azure" && !allowedAzureFields.has(key)) {- continue;- }- if (provider === "watsonx_onprem" && !allowedOnPremFields.has(key))- continue;+ if (activeKeys && !activeKeys.has(key)) continue;const trimmed = (value ?? "").trim();if (trimmed !== "") {credentials[key] = trimmed;@@ -245,7 +253,8 @@ const ProviderSettingsDialog = ({provider={provider}providerName={chrome.name}fields={fields}- savedSecretFields={savedSecretFields}+ savedSecretFields={reusableSecretFields}+ reenterSecretFields={reenterSecretFields}saveError={methods.formState.errors.root?.message}azureAuthMethod={azureAuthMethod}onAzureAuthMethodChange={setAzureAuthMethod}diff --git a/frontend/app/settings/_components/provider-settings-form.tsx b/frontend/app/settings/_components/provider-settings-form.tsxindex 4e2fb2a09..19d8449b4 100644--- a/frontend/app/settings/_components/provider-settings-form.tsx+++ b/frontend/app/settings/_components/provider-settings-form.tsx@@ -1,6 +1,12 @@import { useFormContext } from "react-hook-form";import { LabelWrapper } from "@/components/label-wrapper";import type { CatalogCredentialField } from "@/components/models/catalog-models";+import {+ AZURE_AUTH_GROUPS,+ fieldsForKeys,+ ON_PREM_AUTH_GROUPS,+ SECRET_REENTRY_HINT,+} from "@/components/models/provider-credentials";import { WatsonxSpaceSelect } from "@/components/models/watsonx-space-select";import { WatsonxTlsSettings } from "@/components/models/watsonx-tls-settings";import {@@ -16,29 +22,6 @@ export interface ProviderSettingsFormData {credentials: Record<string, string>;}-const AZURE_AUTH_GROUPS = [- { key: "api_key", label: "API key", fields: ["api_key"] },- {- key: "entra_token",- label: "Microsoft Entra access token",- fields: ["azure_ad_token"],- },- {- key: "service_principal",- label: "Microsoft Entra service principal",- fields: ["tenant_id", "client_id", "client_secret"],- },-] as const;--const ON_PREM_AUTH_GROUPS = [- {- key: "username_api_key",- label: "Username + API key",- fields: ["username", "api_key"],- },- { key: "zen_api_key", label: "Zen API key", fields: ["zen_api_key"] },-] as const;-/*** Credential form for a provider with no bespoke dialog, rendered from the* field spec `/api/models/catalog` publishes for it. A provider added to@@ -50,6 +33,7 @@ export function ProviderSettingsForm({providerName,fields,savedSecretFields,+ reenterSecretFields = [],saveError,azureAuthMethod,onAzureAuthMethodChange,@@ -59,7 +43,13 @@ export function ProviderSettingsForm({provider: string;providerName: string;fields: CatalogCredentialField[];+ /**+ * Saved secrets that may be reused. Empty once the endpoint or TLS setting+ * changed: the backend no longer sends them to the new target.+ */savedSecretFields: string[];+ /** Saved secrets the chosen auth method needs typed again before Save. */+ reenterSecretFields?: string[];saveError?: string | null;azureAuthMethod?: string;onAzureAuthMethodChange?: (method: string) => void;@@ -74,6 +64,7 @@ export function ProviderSettingsForm({} = useFormContext<ProviderSettingsFormData>();const saved = new Set(savedSecretFields);+ const reenter = new Set(reenterSecretFields);const credentials = watch("credentials");const renderField = (field: CatalogCredentialField) => {@@ -112,10 +103,12 @@ export function ProviderSettingsForm({const isSecret =field.field_type === "password" || field.field_type === "textarea";const hasSaved = isSecret && saved.has(field.key);+ const needsReentry = reenter.has(field.key);// A stored secret is never sent back to the browser, so an empty box// means "keep what is saved" rather than "no value" — required only- // bites when nothing is stored yet.- const required = field.required && !hasSaved;+ // bites when nothing is stored yet, or when the target changed and the+ // backend would refuse to reuse the stored value.+ const required = needsReentry || (field.required && !hasSaved);return (<div key={field.key} className="min-w-0 space-y-2">@@ -158,6 +151,9 @@ export function ProviderSettingsForm({A value is already saved. Leave this blank to keep it.</p>)}+ {needsReentry && (+ <p className="text-sm text-muted-foreground">{SECRET_REENTRY_HINT}</p>+ )}{error && (<pdata-testid="provider-connection-error"@@ -207,12 +203,7 @@ export function ProviderSettingsForm({)}</AccordionTrigger><AccordionContent className="space-y-4">- {group.fields- .map((key) => fields.find((field) => field.key === key))- .filter((field): field is CatalogCredentialField =>- Boolean(field),- )- .map(renderField)}+ {fieldsForKeys(fields, group.fields).map(renderField)}</AccordionContent></AccordionItem>))}@@ -248,12 +239,7 @@ export function ProviderSettingsForm({)}</AccordionTrigger><AccordionContent className="space-y-4">- {group.fields- .map((key) => fields.find((field) => field.key === key))- .filter((field): field is CatalogCredentialField =>- Boolean(field),- )- .map(renderField)}+ {fieldsForKeys(fields, group.fields).map(renderField)}</AccordionContent></AccordionItem>))}diff --git a/frontend/components/models/catalog-models.test.ts b/frontend/components/models/catalog-models.test.tsindex 388785a9e..6121d8d23 100644--- a/frontend/components/models/catalog-models.test.ts+++ b/frontend/components/models/catalog-models.test.ts@@ -9,7 +9,9 @@ import {onboardingCredentialFields,providerCatalogOptions,providerCredentialsSatisfied,+ providerDiscoversModels,savedSecretFieldsForProvider,+ withDiscoveredModels,} from "./catalog-models";const catalog = {@@ -418,3 +420,72 @@ describe("mergeLiveCatalogOptions creates a missing group", () => {);});});++describe("withDiscoveredModels", () => {+ const clusterCatalog = {+ providers: [+ {+ key: "rhoai",+ discovers_models: true,+ models: [{ model: "granite-3.3-2b-instruct" }],+ embedding_models: [{ model: "granite-embedding-english-r2" }],+ },+ { key: "openai", models: [{ model: "gpt-4.1" }] },+ ],+ };++ it("replaces the provider's configured models with what the cluster serves", () => {+ const result = withDiscoveredModels(clusterCatalog, "rhoai", {+ models: [{ model: "gpt-oss-120b", mode: "chat" }],+ embedding_models: null,+ });++ assert.deepEqual(+ providerCatalogOptions(result, "rhoai", "language").map(+ (option) => option.value,+ ),+ ["gpt-oss-120b"],+ );+ // A half the cluster could not list keeps the configured fallback.+ assert.deepEqual(+ providerCatalogOptions(result, "rhoai", "embedding").map(+ (option) => option.value,+ ),+ ["granite-embedding-english-r2"],+ );+ // Other providers are untouched.+ assert.deepEqual(+ providerCatalogOptions(result, "openai", "language").map(+ (option) => option.value,+ ),+ ["gpt-4.1"],+ );+ });++ it("returns the catalogue unchanged when nothing was discovered", () => {+ assert.equal(+ withDiscoveredModels(clusterCatalog, "rhoai", undefined),+ clusterCatalog,+ );+ assert.equal(+ withDiscoveredModels(clusterCatalog, "rhoai", {+ models: null,+ embedding_models: null,+ }),+ clusterCatalog,+ );+ assert.equal(+ withDiscoveredModels(undefined, "rhoai", {+ models: [],+ embedding_models: [],+ }),+ undefined,+ );+ });++ it("knows which providers can list their own models", () => {+ assert.equal(providerDiscoversModels(clusterCatalog, "rhoai"), true);+ assert.equal(providerDiscoversModels(clusterCatalog, "openai"), false);+ assert.equal(providerDiscoversModels(undefined, "rhoai"), false);+ });+});diff --git a/frontend/components/models/catalog-models.ts b/frontend/components/models/catalog-models.tsindex 7cb0498f4..c17fe215a 100644--- a/frontend/components/models/catalog-models.ts+++ b/frontend/components/models/catalog-models.ts@@ -39,6 +39,7 @@ interface CatalogProvider {models?: CatalogModel[];embedding_models?: CatalogModel[];credential_fields?: CatalogCredentialField[];+ discovers_models?: boolean;}interface ModelCatalogResponse {@@ -282,6 +283,60 @@ export function providerCatalogOptions(return entry ? sortedProviderOptions(entry, kind) : [];}+/**+ * What a cluster said it serves, per picker. A half is `null` when that+ * endpoint could not be listed, and the catalogue's rows stay for it.+ */+export interface DiscoveredCatalogModels {+ models: CatalogModel[] | null;+ embedding_models: CatalogModel[] | null;+}++/**+ * The catalogue with `provider`'s models replaced by what its cluster serves.+ *+ * Replaced rather than merged: the catalogue's rows for a cluster-hosted+ * provider are only the configured fallback, and a model the cluster does not+ * serve must not sit in the picker waiting to be chosen — the same rule the+ * backend applies once credentials are saved.+ */+export function withDiscoveredModels(+ catalog: ModelCatalogResponse | undefined,+ provider: string,+ discovered: DiscoveredCatalogModels | undefined,+): ModelCatalogResponse | undefined {+ if (!catalog || !discovered) {+ return catalog;+ }+ const { models, embedding_models } = discovered;+ if (models === null && embedding_models === null) {+ return catalog;+ }+ return {+ ...catalog,+ providers: catalog.providers.map((entry) =>+ entry.key === provider+ ? {+ ...entry,+ models: models ?? entry.models,+ embedding_models: embedding_models ?? entry.embedding_models,+ }+ : entry,+ ),+ };+}++/** Whether the backend can list `provider`'s models from unsaved credentials. */+export function providerDiscoversModels(+ catalog: ModelCatalogResponse | undefined,+ provider: string,+): boolean {+ return (+ catalog?.providers?.find((entry) => entry.key === provider)+ ?.discovers_models === true+ );+}+/*** Onboarding lists every OpenRAG-credentialed provider, even before the* operator has saved a key — they are here to configure one.diff --git a/frontend/components/models/provider-credentials.test.ts b/frontend/components/models/provider-credentials.test.tsnew file mode 100644index 000000000..9f12783a7--- /dev/null+++ b/frontend/components/models/provider-credentials.test.ts@@ -0,0 +1,185 @@+import { describe, expect, it } from "vitest";+import {+ activeCredentialKeys,+ authMethodCredentialKeys,+ clearedSavedCredentialKeys,+ credentialTargetChanged,+ secretsToReenter,+} from "./provider-credentials";++describe("authMethodCredentialKeys", () => {+ it.each([+ ["watsonx_onprem", "username_api_key", ["username", "api_key"]],+ ["watsonx_onprem", "zen_api_key", ["zen_api_key"]],+ // Anything else is the method the form offers first.+ ["watsonx_onprem", "unknown", ["username", "api_key"]],+ ])("lists every key of %s's %s method", (provider, method, expected) => {+ expect(authMethodCredentialKeys(provider, "api_key", method)).toEqual(+ expected,+ );+ });++ it.each([+ ["api_key", ["api_key"]],+ ["service_principal", ["tenant_id", "client_id", "client_secret"]],+ ["unknown", []],+ ])("lists every key of Azure's %s method", (method, expected) => {+ expect(+ authMethodCredentialKeys("azure", method, "username_api_key"),+ ).toEqual(expected);+ });++ it("is empty for a provider with no auth-method choice", () => {+ expect(+ authMethodCredentialKeys("rhoai", "api_key", "username_api_key"),+ ).toEqual([]);+ });+});++describe("activeCredentialKeys", () => {+ it("adds the chosen on-prem method's keys to the shared fields", () => {+ expect(+ activeCredentialKeys("watsonx_onprem", "api_key", "zen_api_key"),+ ).toEqual(+ new Set([+ "api_base",+ "space_id",+ "project_id",+ "ssl_verify",+ "zen_api_key",+ ]),+ );+ });++ it("adds the chosen Azure method's keys to the endpoint fields", () => {+ expect(+ activeCredentialKeys("azure", "entra_token", "username_api_key"),+ ).toEqual(new Set(["api_base", "api_version", "azure_ad_token"]));+ });++ it("is null for a provider with no auth-method choice", () => {+ expect(+ activeCredentialKeys("rhoai", "api_key", "username_api_key"),+ ).toBeNull();+ });+});++describe("clearedSavedCredentialKeys", () => {+ const saved = {+ api_base: "https://chat.example.com/v1",+ embedding_api_base: "https://embed.example.com/v1",+ ssl_verify: "/etc/pki/ca.crt",+ };++ it("lists saved non-secret fields the operator emptied", () => {+ expect(+ clearedSavedCredentialKeys(+ {+ api_base: "https://chat.example.com/v1",+ embedding_api_base: " ",+ ssl_verify: "",+ },+ saved,+ new Set(),+ null,+ ),+ ).toEqual(["embedding_api_base", "ssl_verify"]);+ });++ it("leaves out a blank secret, which keeps the saved one", () => {+ expect(+ clearedSavedCredentialKeys(+ { api_key: "" },+ { ...saved, api_key: "redacted" },+ new Set(["api_key"]),+ null,+ ),+ ).toEqual([]);+ });++ it("leaves out a blank field that was never saved", () => {+ expect(+ clearedSavedCredentialKeys({ project_id: "" }, saved, new Set(), null),+ ).toEqual([]);+ });++ it("leaves out fields the chosen auth method does not use", () => {+ expect(+ clearedSavedCredentialKeys(+ { username: "", api_base: "" },+ { username: "cpd-user", api_base: "https://cpd.example.com" },+ new Set(),+ new Set(["api_base", "zen_api_key"]),+ ),+ ).toEqual(["api_base"]);+ });+});++describe("credentialTargetChanged", () => {+ const saved = {+ api_base: "https://chat.example.com/v1",+ ssl_verify: "true",+ username: "cpd-user",+ };++ it("is false while every target field matches what was saved", () => {+ expect(+ credentialTargetChanged(+ { api_base: " https://chat.example.com/v1 ", ssl_verify: "true" },+ saved,+ ),+ ).toBe(false);+ });++ it("ignores fields that are not targets", () => {+ expect(+ credentialTargetChanged(+ { ...saved, username: "someone-else", api_key: "typed" },+ saved,+ ),+ ).toBe(false);+ });++ it.each([+ ["a new endpoint", { ...saved, api_base: "https://other.example.com/v1" }],+ ["a new TLS setting", { ...saved, ssl_verify: "false" }],+ ["a cleared endpoint", { ...saved, api_base: "" }],+ [+ "a newly set embedding endpoint",+ { ...saved, embedding_api_base: "https://embed.example.com/v1" },+ ],+ ])("is true for %s", (_name, credentials) => {+ expect(credentialTargetChanged(credentials, saved)).toBe(true);+ });+});++describe("secretsToReenter", () => {+ const savedSecrets = new Set(["api_key", "zen_api_key"]);++ it("lists nothing while the target is unchanged", () => {+ expect(secretsToReenter({}, savedSecrets, null, false)).toEqual([]);+ });++ it("lists every blank saved secret once the target changed", () => {+ expect(+ secretsToReenter({ api_key: " " }, savedSecrets, null, true),+ ).toEqual(["api_key", "zen_api_key"]);+ });++ it("leaves out a secret that was typed again", () => {+ expect(+ secretsToReenter({ api_key: "new" }, savedSecrets, null, true),+ ).toEqual(["zen_api_key"]);+ });++ it("leaves out secrets the chosen auth method does not use", () => {+ expect(+ secretsToReenter(+ {},+ savedSecrets,+ new Set(["api_base", "username", "api_key"]),+ true,+ ),+ ).toEqual(["api_key"]);+ });+});diff --git a/frontend/components/models/provider-credentials.ts b/frontend/components/models/provider-credentials.tsnew file mode 100644index 000000000..6610e87c9--- /dev/null+++ b/frontend/components/models/provider-credentials.ts@@ -0,0 +1,174 @@+import type { CatalogCredentialField } from "@/components/models/catalog-models";++export const AZURE_AUTH_GROUPS = [+ { key: "api_key", label: "API key", fields: ["api_key"] },+ {+ key: "entra_token",+ label: "Microsoft Entra access token",+ fields: ["azure_ad_token"],+ },+ {+ key: "service_principal",+ label: "Microsoft Entra service principal",+ fields: ["tenant_id", "client_id", "client_secret"],+ },+] as const;++export const ON_PREM_AUTH_GROUPS = [+ {+ key: "username_api_key",+ label: "Username + API key",+ fields: ["username", "api_key"],+ },+ { key: "zen_api_key", label: "Zen API key", fields: ["zen_api_key"] },+] as const;++export function fieldsForKeys(+ fields: CatalogCredentialField[],+ keys: readonly string[],+) {+ const byKey = new Map(fields.map((field) => [field.key, field]));+ return keys.flatMap((key) => {+ const field = byKey.get(key);+ return field ? [field] : [];+ });+}++const ON_PREM_SHARED_FIELDS = [+ "api_base",+ "space_id",+ "project_id",+ "ssl_verify",+];++/**+ * The keys of the chosen auth method itself — every one of them, since a+ * method authenticates only once all of its fields are filled. A catalogue+ * field's `required` flag cannot say this: `username` is needed for the+ * username + API key method and meaningless for a Zen API key. Shared fields+ * (endpoint, TLS) are not included; they keep their own `required` flag.+ *+ * Empty when the provider has no auth-method choice.+ */+export function authMethodCredentialKeys(+ provider: string,+ azureAuthMethod: string,+ onPremAuthMethod: string,+): readonly string[] {+ if (provider === "azure") {+ return (+ AZURE_AUTH_GROUPS.find((group) => group.key === azureAuthMethod)+ ?.fields ?? []+ );+ }+ if (provider === "watsonx_onprem") {+ // Anything but a Zen API key is the username + API key pair, the method+ // the form offers first.+ return (+ ON_PREM_AUTH_GROUPS.find((group) => group.key === onPremAuthMethod) ??+ ON_PREM_AUTH_GROUPS[0]+ ).fields;+ }+ return [];+}++/**+ * The credential keys the chosen auth method actually uses, or `null` when the+ * provider has no auth-method choice and every field applies.+ *+ * Values typed for the *other* method stay in the form so switching back+ * keeps them, but they must not be submitted — nor sent to a cluster when+ * onboarding asks it which models it serves.+ */+export function activeCredentialKeys(+ provider: string,+ azureAuthMethod: string,+ onPremAuthMethod: string,+): Set<string> | null {+ const methodKeys = authMethodCredentialKeys(+ provider,+ azureAuthMethod,+ onPremAuthMethod,+ );+ if (provider === "azure") {+ return new Set(["api_base", "api_version", ...methodKeys]);+ }+ if (provider === "watsonx_onprem") {+ return new Set([...ON_PREM_SHARED_FIELDS, ...methodKeys]);+ }+ return null;+}++/**+ * Fields that say *where* credentials are sent. Mirrors+ * `CREDENTIAL_TARGET_FIELDS` in `src/config/config_manager.py`: once a form+ * changes one — or clears one that was saved — the backend refuses to reuse a+ * stored secret, for model discovery and for Submit alike. Onboarding and the+ * Settings provider dialog both apply this rule.+ */+export const CREDENTIAL_TARGET_FIELDS = [+ "api_base",+ "embedding_api_base",+ "ssl_verify",+] as const;++/** Shown under a saved secret that must be typed again after a target change. */+export const SECRET_REENTRY_HINT =+ "The endpoint or TLS setting changed, so the saved value is not sent to it. Enter it again.";++/** Whether the form points the credentials somewhere other than the saved target. */+export function credentialTargetChanged(+ credentials: Record<string, string>,+ savedCredentials: Record<string, string>,+): boolean {+ return CREDENTIAL_TARGET_FIELDS.some(+ (key) =>+ (credentials[key] ?? "").trim() !== (savedCredentials[key] ?? "").trim(),+ );+}++/**+ * Saved secrets the operator has to type again before Submit.+ *+ * A blank secret normally means "keep the saved one", but the backend refuses+ * that once the target changed (it would send the token to a host it was not+ * saved for). Only secrets the chosen auth method uses count: the backend+ * drops the other method's values on save anyway.+ */+export function secretsToReenter(+ credentials: Record<string, string>,+ savedSecrets: Set<string>,+ active: Set<string> | null,+ targetChanged: boolean,+): string[] {+ if (!targetChanged) return [];+ return [...savedSecrets].filter(+ (key) =>+ (!active || active.has(key)) && (credentials[key] ?? "").trim() === "",+ );+}++/**+ * The keys the operator emptied that still hold a saved non-secret value.+ *+ * A secret is never shown, so a blank one means "keep the saved value". Any+ * other field is shown pre-filled, so blanking it means "clear it": Submit+ * sends these as removals, and model discovery sends them as `""` so the+ * cluster is not asked at the endpoint the operator just removed.+ */+export function clearedSavedCredentialKeys(+ credentials: Record<string, string>,+ savedCredentials: Record<string, string>,+ savedSecrets: Set<string>,+ active: Set<string> | null,+): string[] {+ return Object.entries(credentials)+ .filter(+ ([key, value]) =>+ (!active || active.has(key)) &&+ (value ?? "").trim() === "" &&+ !savedSecrets.has(key) &&+ Boolean(savedCredentials[key]),+ )+ .map(([key]) => key);+}diff --git a/frontend/components/models/watsonx-space-select.test.tsx b/frontend/components/models/watsonx-space-select.test.tsxindex d0f582e3e..a265063d2 100644--- a/frontend/components/models/watsonx-space-select.test.tsx+++ b/frontend/components/models/watsonx-space-select.test.tsx@@ -1,6 +1,11 @@import { HttpResponse, http } from "msw";import { describe, expect, it, vi } from "vitest";-import { renderWithProviders, screen, userEvent } from "@/test-utils/render";+import {+ renderWithProviders,+ screen,+ userEvent,+ waitFor,+} from "@/test-utils/render";import { WatsonxSpaceSelect } from "./watsonx-space-select";describe("WatsonxSpaceSelect", () => {@@ -65,6 +70,48 @@ describe("WatsonxSpaceSelect", () => {expect(onValueChange).toHaveBeenCalledWith("space-prod");});+ it("sends a blank TLS setting so a saved one is not reused", async () => {+ const requests: Array<{ credentials: Record<string, string> }> = [];+ renderWithProviders(+ <WatsonxSpaceSelect+ credentials={{+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ api_key: "",+ ssl_verify: "",+ }}+ hasSavedApiKey+ value=""+ onValueChange={vi.fn()}+ idPrefix="test"+ />,+ {+ handlers: [+ http.post(+ "/api/models/watsonx_onprem/spaces",+ async ({ request }) => {+ requests.push(+ (await request.json()) as {+ credentials: Record<string, string>;+ },+ );+ return HttpResponse.json({ spaces: [] });+ },+ ),+ ],+ },+ );++ await waitFor(() => expect(requests).toHaveLength(1));+ // The blank secret is left out (the saved key is kept); the blank TLS+ // setting is sent so the backend does not fill the saved one back in.+ expect(requests[0].credentials).toEqual({+ api_base: "https://cpd.example.com",+ username: "cpd-user",+ ssl_verify: "",+ });+ });+it("does not suggest disabling TLS when verification is already off", async () => {renderWithProviders(<WatsonxSpaceSelectdiff --git a/frontend/components/models/watsonx-space-select.tsx b/frontend/components/models/watsonx-space-select.tsxindex dc6202523..b8f82b9d7 100644--- a/frontend/components/models/watsonx-space-select.tsx+++ b/frontend/components/models/watsonx-space-select.tsx@@ -118,15 +118,21 @@ export function WatsonxSpaceSelect({const timeout = window.setTimeout(async () => {try {- const submittedCredentials = Object.fromEntries(- Object.entries({- api_base: apiBase,- username,- api_key: apiKey,- zen_api_key: zenApiKey,- ssl_verify: sslVerify,- }).filter(([, credentialValue]) => credentialValue !== ""),- );+ // A blank ssl_verify still goes out: if one was saved, the operator+ // cleared it (Submit removes it), and the backend must not list spaces+ // with the saved CA — or the saved key. Blank secrets mean "keep the+ // saved one" and are left out as before.+ const submittedCredentials = {+ ...Object.fromEntries(+ Object.entries({+ api_base: apiBase,+ username,+ api_key: apiKey,+ zen_api_key: zenApiKey,+ }).filter(([, credentialValue]) => credentialValue !== ""),+ ),+ ssl_verify: sslVerify,+ };const response = await apiClient.post("/models/watsonx_onprem/spaces",{diff --git a/src/api/models.py b/src/api/models.pyindex 86cdbfb11..04c412a8b 100644--- a/src/api/models.py+++ b/src/api/models.py@@ -1,4 +1,7 @@import re+from collections.abc import Mapping+from types import ModuleType+from typing import Any, Literalimport httpxfrom fastapi import Depends@@ -9,9 +12,10 @@ from api.provider_validation import (is_provider_credential_error,sanitize_provider_error_content,)+from config.config_manager import retargeted_credential_fieldsfrom config.settings import get_openrag_configfrom dependencies import get_models_service, require_permission-from services.model_catalog import hide_excluded_live_models+from services.model_catalog import hide_excluded_live_models, secret_field_keysfrom session_manager import Userfrom utils.logging_config import get_logger@@ -52,6 +56,87 @@ class WatsonxOnPremSpacesBody(BaseModel):auth_method: str | None = None+class ModelDiscoveryBody(BaseModel):+ credentials: dict[str, str] = Field(default_factory=dict)+ auth_method: str | None = None+ #: The one picker the caller shows; None lists both.+ kind: Literal["chat", "embedding"] | None = None+++class InvalidAuthMethodError(ValueError):+ """The form named an auth method its provider does not offer — a client error."""+++#: Client-safe text for a missing or unknown auth method. The enhancement+#: hook's own message is logged, never returned.+_INVALID_AUTH_METHOD_ERROR = "Choose a valid authentication method."+++def _draft_credentials(+ enhancement: ModuleType, submitted: Mapping[str, str], auth_method: str | None+) -> dict[str, Any]:+ """Credentials a form has typed, completed from the stored ones where safe.++ A secret the operator saved earlier is shown blank in the form, so the+ stored value fills in whatever was not typed — but only while the form still+ points at the same host. A changed endpoint or TLS setting drops every+ stored value, so a saved token is never sent somewhere it was not saved for.+ Only the fields the provider's form defines (for the chosen auth method,+ where it has several) are kept.++ An empty value means what it means on Submit: for a secret, "keep the saved+ one"; for any other field, "cleared" — so the stored value is not filled+ back in, and clearing a saved endpoint or TLS setting counts as a change.++ Raises `InvalidAuthMethodError` when the provider rejects `auth_method`.+ """+ from enhancements.providers.registry import default_auth_method_for++ config = get_openrag_config()+ stored = config.providers.stored_credentials(enhancement.PROVIDER_KEY)+ fields_for_auth_method = getattr(enhancement, "credential_fields_for_auth_method", None)+ if callable(fields_for_auth_method):+ stored_config = config.providers.get_provider_config(enhancement.PROVIDER_KEY)+ method = (+ auth_method+ or getattr(stored_config, "auth_method", None)+ or default_auth_method_for(enhancement.PROVIDER_KEY, stored)+ )+ if not method:+ raise InvalidAuthMethodError("Choose an authentication method.")+ try:+ allowed = set(fields_for_auth_method(method))+ except ValueError as exc:+ raise InvalidAuthMethodError(str(exc)) from exc+ else:+ allowed = {str(field["key"]) for field in enhancement.CREDENTIAL_FIELDS}+ secrets = secret_field_keys(enhancement.PROVIDER_KEY)+ typed: dict[str, str] = {}+ cleared: set[str] = set()+ for name, value in submitted.items():+ if name not in allowed:+ continue+ text = str(value).strip()+ if text:+ typed[name] = text+ elif name not in secrets:+ cleared.add(name)+ # The same rule a save enforces (`set_credentials`), so discovery never+ # reuses a secret that Submit would then refuse to.+ retargeted = retargeted_credential_fields(stored, typed, cleared)+ if retargeted:+ # Names only: the values are endpoints, CA paths and possibly secrets.+ logger.info(+ "Draft credentials point at a new target; stored credentials are not reused",+ provider=enhancement.PROVIDER_KEY,+ fields=retargeted,+ )+ base: Mapping[str, str] = {}+ else:+ base = {name: value for name, value in stored.items() if name not in cleared}+ return {name: value for name, value in {**base, **typed}.items() if name in allowed}++def _models_error_response(exc: Exception) -> JSONResponse:"""Map model-route failures to client (400) vs upstream (502) vs server (500).@@ -232,28 +317,7 @@ async def get_watsonx_onprem_spaces(request = body or WatsonxOnPremSpacesBody()try:- config = get_openrag_config()- stored_config = config.providers.get_provider_config(onprem.PROVIDER_KEY)- stored_credentials = config.providers.stored_credentials(onprem.PROVIDER_KEY)- auth_method = request.auth_method or getattr(stored_config, "auth_method", None)- if auth_method is None:- auth_method = (- "zen_api_key" if stored_credentials.get("zen_api_key") else "username_api_key"- )- allowed = onprem.credential_fields_for_auth_method(auth_method)- submitted = {- name: str(value).strip()- for name, value in request.credentials.items()- if name in allowed and str(value).strip()- }- target_changed = any(- name in submitted and submitted[name] != stored_credentials.get(name)- for name in ("api_base", "ssl_verify")- )- base = {} if target_changed else stored_credentials- credentials = {- name: value for name, value in {**base, **submitted}.items() if name in allowed- }+ credentials = _draft_credentials(onprem, request.credentials, request.auth_method)spaces = await onprem.list_spaces(credentials)return JSONResponse({"spaces": spaces},@@ -274,6 +338,12 @@ async def get_watsonx_onprem_spaces(else:error = "Unable to list deployment spaces from the cluster."return JSONResponse({"error": error}, status_code=status_code)+ except InvalidAuthMethodError as exc:+ logger.warning(+ "watsonx.ai on-prem space discovery named an unknown auth method",+ detail=str(exc),+ )+ return JSONResponse({"error": _INVALID_AUTH_METHOD_ERROR}, status_code=400)except Exception as exc:logger.error("Failed to list watsonx.ai on-prem spaces", exc_info=exc)return JSONResponse(@@ -282,6 +352,64 @@ async def get_watsonx_onprem_spaces()+async def discover_provider_models(+ provider: str,+ body: ModelDiscoveryBody | None = None,+ user: User = Depends(require_permission("providers:write")),+):+ """Models a cluster serves, asked with credentials that may not be saved yet.++ POST /models/{provider}/discover. Onboarding calls this while the operator+ is still filling the form: the catalogue only lists a cluster with *saved*+ credentials, and a vLLM `--served-model-name` can be anything, so without it+ the picker offers the configured fallback rather than what is deployed.++ `kind` names the one picker the caller shows (each onboarding step shows+ one), so a provider with a separate endpoint per kind asks only that one: an+ unreachable embedding endpoint cannot stall the language step. Omitted, both+ are asked.++ Each half is null when that endpoint could not be listed or was not asked,+ and the caller keeps the catalogue's rows for it.+ """+ from enhancements.providers.registry import list_cluster_models_for, model_discovery_for+ from services.model_catalog import discovered_model_payload, supported_provider_keys++ key = (provider or "").strip().lower()+ enhancement = model_discovery_for(key)+ if enhancement is None or key not in supported_provider_keys():+ return JSONResponse(+ {"error": "This provider cannot list its models."},+ status_code=404,+ )+ request = body or ModelDiscoveryBody()+ kind_label = request.kind or "both"+ logger.info("Discovering provider models", provider=key, kind=kind_label)+ try:+ credentials = _draft_credentials(enhancement, request.credentials, request.auth_method)+ listed = await list_cluster_models_for(enhancement, credentials, kind=request.kind)+ return JSONResponse(+ discovered_model_payload(key, listed),+ headers={"Cache-Control": "no-store"},+ )+ except InvalidAuthMethodError as exc:+ logger.warning(+ "Model discovery named an unknown auth method",+ provider=key,+ kind=kind_label,+ detail=str(exc),+ )+ return JSONResponse({"error": _INVALID_AUTH_METHOD_ERROR}, status_code=400)+ except Exception as exc:+ logger.error(+ "Failed to discover provider models", provider=key, kind=kind_label, exc_info=exc+ )+ return JSONResponse(+ {"error": "Unable to list models from the provider."},+ status_code=500,+ )++async def get_model_providers(user: User = Depends(require_permission("providers:read")),):diff --git a/src/api/settings/endpoints.py b/src/api/settings/endpoints.pyindex a14e19a69..e67983e8d 100644--- a/src/api/settings/endpoints.py+++ b/src/api/settings/endpoints.py@@ -65,6 +65,7 @@ from api.settings.models import (from config.config_manager import (ALLOWED_INDEX_NAME_PATTERNS,DEFAULT_SYSTEM_PROMPT,+ CredentialTargetChangedError,is_permitted_index_name,)from config.settings import (@@ -437,6 +438,10 @@ async def update_settings(for name, fields in (body.provider_credential_removals or {}).items()}provider_update_keys = set(submitted_credentials) | set(removals_by_provider)+ auth_methods_by_provider = {+ _provider_key(name): method+ for name, method in (body.provider_auth_methods or {}).items()+ }should_validate = any(getattr(body, field) is not None for field in provider_fields)# Docling VLM settings reuse provider credentials, so they are gated@@ -484,11 +489,11 @@ async def update_settings(for provider_key in provider_update_keys:submitted = submitted_credentials.get(provider_key, {})removals = removals_by_provider.get(provider_key, set())+ auth_method = auth_methods_by_provider.get(provider_key)credentials = current_config.providers.pending_credentials(- provider_key, submitted, remove=removals+ provider_key, submitted, remove=removals, auth_method=auth_method)if provider_key == "azure":- auth_method = (body.provider_auth_methods or {}).get(provider_key)required_by_method = {"api_key": {"api_key"},"entra_token": {"azure_ad_token"},@@ -519,7 +524,7 @@ async def update_settings(provider=provider_key,credentials=credentials,stored_credentials=current_config.providers.pending_stored_credentials(- provider_key, submitted, remove=removals+ provider_key, submitted, remove=removals, auth_method=auth_method),)@@ -548,6 +553,7 @@ async def update_settings(llm_provider_key,submitted_credentials.get(llm_provider_key, {}),remove=removals_by_provider.get(llm_provider_key, set()),+ auth_method=auth_methods_by_provider.get(llm_provider_key),)api_key = credentials.get("api_key", api_key)endpoint = credentials.get("api_base", endpoint)@@ -574,6 +580,7 @@ async def update_settings(llm_provider_key,submitted_credentials.get(llm_provider_key, {}),remove=removals_by_provider.get(llm_provider_key, set()),+ auth_method=auth_methods_by_provider.get(llm_provider_key),),# A model the cluster does not serve must fail the# save, not the first chat.@@ -609,6 +616,7 @@ async def update_settings(submitted_credentials.get(embedding_provider_key, {}),kind="embedding",remove=removals_by_provider.get(embedding_provider_key, set()),+ auth_method=auth_methods_by_provider.get(embedding_provider_key),)api_key = credentials.get("api_key", api_key)endpoint = credentials.get("api_base", endpoint)@@ -635,6 +643,7 @@ async def update_settings(embedding_provider_key,submitted_credentials.get(embedding_provider_key, {}),remove=removals_by_provider.get(embedding_provider_key, set()),+ auth_method=auth_methods_by_provider.get(embedding_provider_key),),verify_model=True,)@@ -642,6 +651,16 @@ async def update_settings(f"Embedding provider validation successful for {embedding_provider}")+ except CredentialTargetChangedError as e:+ # Raised before any probe is sent; the message names form+ # labels only, so it is returned as-is rather than run through+ # the provider-error humaniser.+ logger.warning(+ "Settings update refused: a credential target changed without its secrets",+ provider=e.provider,+ fields=list(e.fields),+ )+ return JSONResponse({"error": str(e)}, status_code=400)except Exception as e:logger.error(f"Provider validation failed: {str(e)}")return JSONResponse({"error": sanitize_provider_error_content(e)}, status_code=400)@@ -965,11 +984,19 @@ async def update_settings(# Update provider-specific settingsprovider_updated = Falsefor provider in provider_update_keys:+ removals = removals_by_provider.get(provider, set())+ auth_method = auth_methods_by_provider.get(provider)+ logger.info(+ "Saving provider credentials",+ provider=provider,+ auth_method=auth_method,+ removed=sorted(removals),+ )working_config.providers.set_credentials(provider,submitted_credentials.get(provider, {}),- auth_method=(body.provider_auth_methods or {}).get(provider),- remove=removals_by_provider.get(provider, set()),+ auth_method=auth_method,+ remove=removals,)config_updated = Trueprovider_updated = True@@ -1197,6 +1224,15 @@ async def update_settings(# Preserve intentional client-error status codes (e.g. 422 validation# failures) instead of letting the catch-all below flatten them to 500.raise+ except CredentialTargetChangedError as e:+ # Normally caught during validation; this covers a save that skipped it.+ # The changes were staged on `working_config`, so nothing was applied.+ logger.warning(+ "Settings update refused: a credential target changed without its secrets",+ provider=e.provider,+ fields=list(e.fields),+ )+ return JSONResponse({"error": str(e)}, status_code=400)except Exception:logger.exception("Failed to update settings")await TelemetryClient.send_event(@@ -1343,13 +1379,36 @@ async def onboarding(_provider_key(name): set(fields)for name, fields in (body.provider_credential_removals or {}).items()}+ auth_methods_by_provider = {+ _provider_key(name): method+ for name, method in (body.provider_auth_methods or {}).items()+ }for provider in set(submitted_credentials) | set(removals_by_provider):- current_config.providers.set_credentials(- provider,- submitted_credentials.get(provider, {}),- auth_method=(body.provider_auth_methods or {}).get(provider),- remove=removals_by_provider.get(provider, set()),+ removals = removals_by_provider.get(provider, set())+ auth_method = auth_methods_by_provider.get(provider)+ logger.info(+ "Saving provider credentials during onboarding",+ provider=provider,+ auth_method=auth_method,+ removed=sorted(removals),)+ try:+ current_config.providers.set_credentials(+ provider,+ submitted_credentials.get(provider, {}),+ auth_method=auth_method,+ remove=removals,+ )+ except CredentialTargetChangedError as e:+ # Refused before the validation probe below could send the+ # stored secret to the new target; `current_config` is a copy,+ # so nothing was applied.+ logger.warning(+ "Onboarding refused: a credential target changed without its secrets",+ provider=e.provider,+ fields=list(e.fields),+ )+ return JSONResponse({"error": str(e)}, status_code=400)config_updated = True# Mark providers as configured if they were chosen during onboardingdiff --git a/src/app/routes/internal.py b/src/app/routes/internal.pyindex a2ca7f93b..efe5167d1 100644--- a/src/app/routes/internal.py+++ b/src/app/routes/internal.py@@ -465,6 +465,12 @@ def register_internal_routes(app: FastAPI):methods=["POST"],tags=["internal"],)+ app.add_api_route(+ "/models/{provider}/discover",+ models.discover_provider_models,+ methods=["POST"],+ tags=["internal"],+ )app.add_api_route("/models/catalog", models.get_model_catalog, methods=["GET"], tags=["internal"])diff --git a/src/config/config_manager.py b/src/config/config_manager.pyindex 61a2ab923..34f44905c 100644--- a/src/config/config_manager.py+++ b/src/config/config_manager.py@@ -141,6 +141,172 @@ def _sanitize_for_log(value: object) -> str:return re.sub(r"[\r\n\t]", "_", str(value))+#: Fields that say *where* credentials are sent. When a save changes one —+#: including clearing one that was stored — the stored secrets belong to a+#: different host and must not travel with it. Mirrored by+#: `CREDENTIAL_TARGET_FIELDS` in `frontend/components/models/provider-credentials.ts`+#: (shared by onboarding and the Settings provider dialog).+CREDENTIAL_TARGET_FIELDS = ("api_base", "embedding_api_base", "ssl_verify")++#: Providers with a bespoke settings and onboarding form, outside the+#: credential-target rule. Their forms never promise that a saved key is+#: withheld from a new endpoint, and none takes a free-text target: watsonx.ai+#: offers only IBM's region hosts. A credential that is valid tenant- or+#: account-wide (an IBM Cloud API key, an Azure service principal) is not on+#: its own a reason to exempt a provider: what matters is whether the derived+#: token can be sent to an arbitrary host.+_FIRST_CLASS_PROVIDERS = frozenset({"openai", "anthropic", "watsonx", "ollama"})++_AZURE_AUTH_METHOD_FIELDS: dict[str, frozenset[str]] = {+ "api_key": frozenset({"api_key"}),+ "entra_token": frozenset({"azure_ad_token"}),+ "service_principal": frozenset({"tenant_id", "client_id", "client_secret"}),+}+_AZURE_SHARED_FIELDS = frozenset({"api_base", "api_version", "base_model"})+++class CredentialTargetChangedError(ValueError):+ """A save points a provider at a new target without re-entering its secrets.++ The message is client-safe: it names form labels, never values.+ """++ def __init__(self, provider: str, fields: Sequence[str], labels: Sequence[str]) -> None:+ self.provider = provider+ self.fields = tuple(fields)+ named = ", ".join(labels)+ super().__init__(+ f"The endpoint or TLS setting changed, so the saved {named} is not sent to it. "+ f"Enter {named} again."+ )+++def retargeted_credential_fields(+ stored: Mapping[str, Any], submitted: Mapping[str, str], cleared: set[str] | frozenset[str]+) -> list[str]:+ """The target fields a pending update changes, sorted.++ `submitted` holds the non-blank values the request carries; `cleared` the+ fields it removes. A field counts as changed when a submitted value differs+ from the stored one, or when a stored value is cleared.+ """+ return sorted(+ name+ for name in CREDENTIAL_TARGET_FIELDS+ if (name in submitted and submitted[name] != str(stored.get(name) or "").strip())+ or (name in cleared and str(stored.get(name) or "").strip())+ )+++def _credentials_for_auth_method(+ provider: str, credentials: Mapping[str, str], auth_method: str | None+) -> dict[str, str]:+ """`credentials` narrowed to the fields `auth_method` uses.++ Credentials for another authentication method must not leak into LiteLLM's+ call kwargs after the user switches methods. Only Azure and watsonx.ai+ on-prem offer several methods; every other provider is returned unchanged,+ as is any provider when no method is given.++ Raises `ValueError` for a method the provider does not offer.+ """+ if not auth_method:+ return dict(credentials)+ if provider == "azure":+ active = _AZURE_AUTH_METHOD_FIELDS.get(auth_method)+ if active is None:+ raise ValueError(f"Unknown Azure authentication method: {auth_method}")+ return {+ name: value+ for name, value in credentials.items()+ if name in _AZURE_SHARED_FIELDS or name in active+ }+ if provider == "watsonx_onprem":+ from enhancements.providers.watsonx.onprem import (+ credential_fields_for_auth_method,+ )++ allowed = credential_fields_for_auth_method(auth_method)+ return {name: value for name, value in credentials.items() if name in allowed}+ return dict(credentials)+++def _ensure_secrets_follow_target(+ provider: str,+ stored: Mapping[str, Any],+ submitted: Mapping[str, str],+ cleared: set[str],+) -> None:+ """Refuse an update that would send a stored secret to a new target.++ A secret is never echoed back to a form, so a blank secret means "keep the+ saved one" — which is only safe while the credentials still go where they+ went when it was saved. Once a target field changes, every stored secret+ must be entered again (or explicitly removed). Raising rather than quietly+ dropping it keeps a pre-save probe from being sent without the secret and+ the operator from saving a provider that silently stops working.++ Called before anything is mutated, so a refused update leaves the config+ untouched. Providers in `_FIRST_CLASS_PROVIDERS` are exempt.++ Raises `CredentialTargetChangedError`.+ """+ if provider in _FIRST_CLASS_PROVIDERS:+ return+ retargeted = retargeted_credential_fields(stored, submitted, cleared)+ if not retargeted:+ return+ logger.info(+ "Checking stored secrets against a changed credential target...",+ provider=provider,+ fields=retargeted,+ )+ try:+ from services.model_catalog import credential_fields++ spec = credential_fields(provider)+ secrets = {+ str(field["key"])+ for field in spec+ if field.get("field_type") in {"password", "textarea", "upload"}+ }+ labels = {str(field["key"]): str(field.get("label") or field["key"]) for field in spec}+ except Exception as error:+ # Fail closed: without the form spec any stored field may be a secret.+ logger.warning(+ "Could not read the credential form spec; treating every stored "+ "non-target field as a secret",+ provider=provider,+ error=str(error),+ )+ secrets = {name for name in stored if name not in CREDENTIAL_TARGET_FIELDS}+ labels = {}+ stale = sorted(+ name+ for name in stored+ if name in secrets+ and str(stored.get(name) or "").strip()+ and name not in submitted+ and name not in cleared+ )+ if not stale:+ logger.info(+ "Successfully checked stored secrets against the changed credential "+ "target. Every secret was re-entered or removed.",+ provider=provider,+ fields=retargeted,+ )+ return+ # Names only: the values are endpoints, CA paths and secrets.+ logger.warning(+ "Refusing to reuse stored secrets for a changed credential target",+ provider=provider,+ fields=retargeted,+ secrets=stale,+ )+ raise CredentialTargetChangedError(provider, stale, [labels.get(name, name) for name in stale])++class ProviderConfig(Protocol):"""Structural type shared by every provider config dataclass.@@ -239,7 +405,15 @@ class ProvidersConfig:auth_method: str | None = None,remove: set[str] | None = None,) -> None:- """Upsert credentials and apply explicitly requested field removals."""+ """Upsert credentials and apply explicitly requested field removals.++ A blank or omitted field keeps its stored value — except a secret+ when the update changes a credential target (`CREDENTIAL_TARGET_FIELDS`):+ then every stored secret must be re-entered or removed, and the update+ is refused with `CredentialTargetChangedError` before anything changes.++ Raises `ValueError` (including `CredentialTargetChangedError`).+ """key = provider.strip().lower()clean = {str(name): str(value).strip()@@ -253,33 +427,12 @@ class ProvidersConfig:# separate operation so an empty password cannot erase a secret.returnprevious = self.custom.get(key, GenericProviderConfig())- if key == "azure" and auth_method:- methods = {- "api_key": {"api_key"},- "entra_token": {"azure_ad_token"},- "service_principal": {"tenant_id", "client_id", "client_secret"},- }- active = methods.get(auth_method)- if active is None:- raise ValueError(f"Unknown Azure authentication method: {auth_method}")- shared = {"api_base", "api_version", "base_model"}- # Credentials for another authentication method must not leak into- # LiteLLM's call kwargs after the user switches methods.- previous.credentials = {- name: value- for name, value in previous.credentials.items()- if name in shared or name in active- }- previous.auth_method = auth_method- if key == "watsonx_onprem" and auth_method:- from enhancements.providers.watsonx.onprem import (- credential_fields_for_auth_method,- )-- allowed = credential_fields_for_auth_method(auth_method)- previous.credentials = {- name: value for name, value in previous.credentials.items() if name in allowed- }+ # Both checks run before `previous` (the live entry) is touched, so a+ # refused update leaves the config exactly as it was.+ kept = _credentials_for_auth_method(key, previous.credentials, auth_method)+ _ensure_secrets_follow_target(key, kept, clean, removals)+ previous.credentials = kept+ if auth_method and key in {"azure", "watsonx_onprem"}:previous.auth_method = auth_methodfor name in removals:previous.credentials.pop(name, None)@@ -329,12 +482,24 @@ class ProvidersConfig:submitted: dict[str, str] | None = None,*,remove: set[str] | None = None,+ auth_method: str | None = None,) -> dict[str, str]:- """`stored_credentials()` as it would read after one pending update."""- pending = self.stored_credentials(provider)- for name in remove or set():+ """`stored_credentials()` as it would read after one pending update.++ Applies the same rules `set_credentials()` will — the auth-method+ narrowing and the credential-target check — so a pre-save probe never+ sends a stored secret to a target the save would refuse it for.++ Raises `ValueError` (including `CredentialTargetChangedError`).+ """+ key = provider.strip().lower()+ clean = _clean_submitted(submitted)+ removals = set(remove or set())+ pending = _credentials_for_auth_method(key, self.stored_credentials(key), auth_method)+ _ensure_secrets_follow_target(key, pending, clean, removals)+ for name in removals:pending.pop(name, None)- pending.update(_clean_submitted(submitted))+ pending.update(clean)return pendingdef pending_credentials(@@ -344,6 +509,7 @@ class ProvidersConfig:*,kind: str = "chat",remove: set[str] | None = None,+ auth_method: str | None = None,) -> dict[str, Any]:"""LiteLLM kwargs for `provider` as it would be once `submitted` is saved.@@ -357,6 +523,9 @@ class ProvidersConfig:`kind` selects which endpoint a multi-endpoint provider is validatedagainst, so the pre-save probe hits the same one the real call will.++ Raises `ValueError` (including `CredentialTargetChangedError`) on the+ same terms as `set_credentials()`."""from enhancements.providers.registry import credentials_forfrom enhancements.providers.registry import get as get_provider_enhancement@@ -365,9 +534,16 @@ class ProvidersConfig:clean = _clean_submitted(submitted)enhancement = get_provider_enhancement(key)if enhancement:- stored = self.pending_stored_credentials(key, clean, remove=remove)+ stored = self.pending_stored_credentials(+ key, clean, remove=remove, auth_method=auth_method+ )return credentials_for(enhancement, stored, kind)values = self.credential_values(key, kind=kind)+ if key not in _FIRST_CLASS_PROVIDERS:+ # A generic provider's call kwargs are its stored form, so the save's+ # narrowing and target check apply to them directly.+ values = _credentials_for_auth_method(key, values, auth_method)+ _ensure_secrets_follow_target(key, values, clean, set(remove or set()))for name in remove or set():values.pop(name, None)values.update(clean)@@ -847,10 +1023,12 @@ class ConfigManager:onprem_credentials,required=("api_base",),)+ from enhancements.providers.watsonx.onprem import default_auth_method+entry = config_data["providers"]["custom"]["watsonx_onprem"]stored = entry["credentials"]has_zen = bool(stored.get("zen_api_key"))- entry["auth_method"] = "zen_api_key" if has_zen else "username_api_key"+ entry["auth_method"] = default_auth_method(stored)entry["configured"] = bool(stored.get("api_base")and (has_zen or (stored.get("username") and stored.get("api_key")))diff --git a/src/config/model_providers.yaml b/src/config/model_providers.yamlindex fae86a86f..42c40f0b8 100644--- a/src/config/model_providers.yaml+++ b/src/config/model_providers.yaml@@ -140,28 +140,27 @@ providers:# is not `openai_like` (that key silently drops the per-provider TLS setting a# cluster CA needs).#- # Off in every mode as shipped. It needs two endpoint URLs and a cluster token- # that only the deployment knows, so it is enabled per deployment with- # OPENRAG_MODEL_PROVIDERS_CONFIG rather than offered to everyone.+ # On in on_prem only as shipped: the cluster is the customer's own, and its two+ # endpoint URLs and token are entered in Settings. Elsewhere it is enabled per+ # deployment with OPENRAG_MODEL_PROVIDERS_CONFIG rather than offered to everyone.#- # The two lists below are a *fallback*, not the catalogue. OpenRAG asks each- # InferenceService what it serves (`GET /v1/models`) and shows that in the- # pickers, so a model the cluster does not have is never offered. These rows- # are what it falls back to while the endpoints are unreachable or before any- # credentials are entered.+ # No `models:` / `embedding_models:` rows are shipped. Model ids are the+ # InferenceServices' `--served-model-name` values, which only the deployment+ # knows, so OpenRAG asks each endpoint what it serves (`GET /v1/models`):+ # with the saved credentials for the catalogue, and with the typed ones+ # (`POST /models/rhoai/discover`) during onboarding. A model the cluster does+ # not serve is never offered; while it cannot be reached, the pickers accept+ # a typed model id.+ #+ # A deployment's own config may still add rows as a fallback for while the+ # endpoints are unreachable, e.g. `models: [gpt-oss-120b]`. Use the+ # served-model-name, not the image tag.- name: rhoaidisplay_name: Red Hat OpenShift AImodes:oss: false- on_prem: false+ on_prem: truesaas: false- # Verified on an RHOAI 3.5.0 cluster running the x86 CPU vLLM runtime.- # These are `--served-model-name` values, not image tags: whatever the- # InferenceService was started with is the id that has to be sent.- models:- - gpt-oss-120b- embedding_models:- - granite-embedding-english-r2- name: anthropicdisplay_name: Anthropicdiff --git a/src/services/model_catalog.py b/src/services/model_catalog.pyindex fa3b2e298..46fb85f33 100644--- a/src/services/model_catalog.py+++ b/src/services/model_catalog.py@@ -35,6 +35,7 @@ from config.model_providers import ProviderEntry, visible_provider_entriesfrom enhancements.providers.registry import (credential_field_overrides,litellm_route_for,+ model_discovery_for,route_aliases,)from enhancements.providers.registry import (@@ -284,6 +285,27 @@ def _with_vision(entry: dict[str, Any]) -> dict[str, Any]:return {**entry, "capabilities": [*capabilities, "vision"]}+def _picker_rows(+ rows: list[dict[str, Any]],+ *,+ excluded: tuple[str, ...],+ vision: tuple[str, ...],+ chat: bool,+) -> list[dict[str, Any]]:+ """Apply a provider's config to its rows: `exclude_models`, then `vision_models`.++ The one place both rules live, so the catalogue and draft discovery cannot+ disagree about an id. Exclusion runs first, so it wins over a `models:`+ row, a live listing and a `vision_models` match alike — a deployment that+ suppresses an id means it, wherever the id came from. Only chat rows are+ tagged: an embedding model takes no images.+ """+ kept = [entry for entry in rows if not _matches(entry["model"], excluded)]+ if not chat or not vision:+ return kept+ return [_with_vision(entry) if _matches(entry["model"], vision) else entry for entry in kept]++@lru_cache(maxsize=8)def _catalog(providers: tuple[ProviderEntry, ...]) -> dict[str, Any]:"""Payload for `providers`, given as `config.model_providers` entries.@@ -353,13 +375,9 @@ def _catalog(providers: tuple[ProviderEntry, ...]) -> dict[str, Any]:tuple(name for name in declared_embed if name not in known_embed), EMBEDDING_MODE)# Applied last, so `exclude_models` also wins over a `models:` row —- # a deployment that suppresses an id means it, wherever the id came- # from, and the alternative is two lines that quietly contradict.- chat = [entry for entry in chat if not _matches(entry["model"], excluded)]- embed = [entry for entry in embed if not _matches(entry["model"], excluded)]- chat = [- _with_vision(entry) if _matches(entry["model"], vision) else entry for entry in chat- ]+ # the alternative is two lines that quietly contradict.+ chat = _picker_rows(chat, excluded=excluded, vision=vision, chat=True)+ embed = _picker_rows(embed, excluded=excluded, vision=vision, chat=False)entries.append({"key": key,@@ -368,6 +386,9 @@ def _catalog(providers: tuple[ProviderEntry, ...]) -> dict[str, Any]:"name": display_name or (specs.get(key) or {}).get("provider_display_name") or key,"credential_fields": credential_fields(key),"model_placeholder": (specs.get(key) or {}).get("default_model_placeholder"),+ # Whether `POST /models/{provider}/discover` can list what this+ # provider serves from credentials that are not saved yet.+ "discovers_models": model_discovery_for(key) is not None,"models": sorted(chat, key=lambda entry: entry["model"]),"embedding_models": sorted(embed, key=lambda entry: entry["model"]),}@@ -397,13 +418,46 @@ def _catalog_for(today: datetime.date, providers: tuple[ProviderEntry, ...]) ->return {"providers": entries}+def _provider_entry(provider: str) -> ProviderEntry | None:+ """The visible `model_providers.yaml` row for `provider`, or None."""+ key = (provider or "").strip().lower()+ return next((entry for entry in visible_provider_entries() if entry.name == key), None)++def exclusions_for(provider: str) -> tuple[str, ...]:"""The `exclude_models` patterns configured for `provider`."""- key = (provider or "").strip().lower()- for entry in visible_provider_entries():- if entry.name == key:- return entry.exclude_models- return ()+ entry = _provider_entry(provider)+ return entry.exclude_models if entry is not None else ()+++def discovered_model_payload(provider: str, listed: Any) -> dict[str, Any]:+ """Catalogue rows for what a cluster said it serves, per picker.++ `listed` is an enhancement's `ClusterModels` (or None). A half is None when+ that endpoint could not be listed — the caller keeps the catalogue's rows+ for it — and a list otherwise. The provider's `exclude_models` and+ `vision_models` apply exactly as they do to the catalogue (`_picker_rows`),+ so an id reads the same before and after its credentials are saved.+ """+ entry = _provider_entry(provider)+ excluded = entry.exclude_models if entry is not None else ()+ vision = entry.vision_models if entry is not None else ()++ def _rows(names: Any, mode: str) -> list[dict[str, Any]] | None:+ if names is None:+ return None+ declared = _declared_entries(tuple(str(name) for name in names), mode)+ return _picker_rows(declared, excluded=excluded, vision=vision, chat=mode != EMBEDDING_MODE)++ chat = _rows(getattr(listed, "chat", None), "chat")+ embedding = _rows(getattr(listed, "embedding", None), EMBEDDING_MODE)+ logger.debug(+ "Built discovered model rows",+ provider=provider,+ chat=None if chat is None else len(chat),+ embedding=None if embedding is None else len(embedding),+ )+ return {"models": chat, "embedding_models": embedding}#: Keys in a live `/models/{provider}` payload that hold model lists.diff --git a/tests/unit/api/test_model_discovery_endpoint.py b/tests/unit/api/test_model_discovery_endpoint.pynew file mode 100644index 000000000..fcf696b5a--- /dev/null+++ b/tests/unit/api/test_model_discovery_endpoint.py@@ -0,0 +1,455 @@+"""`POST /models/{provider}/discover`: what a cluster serves, before it is saved.++Onboarding asks this while the operator is still typing, because the catalogue+lists a cluster only with *saved* credentials and a vLLM `--served-model-name`+can be anything.+"""++import inspect+import json+from types import SimpleNamespace+from typing import Any++import pydantic+import pytest+from fastapi import params as fastapi_params++import config.model_providers as model_providers+from api import models as models_api+from config.config_manager import (+ AnthropicConfig,+ GenericProviderConfig,+ OllamaConfig,+ OpenAIConfig,+ ProvidersConfig,+ WatsonXConfig,+)+from enhancements.providers.redhat import openshift_ai as rhoai+from services import model_catalog++CHAT_BASE = "https://chat.example.com/v1"+EMBED_BASE = "https://embed.example.com/v1"+SAVED_TOKEN = "sha256~saved"++[email protected](autouse=True)+def _fresh_provider_config():+ model_providers.reload()+ yield+ model_providers.reload()+++def _offer_rhoai(+ tmp_path, monkeypatch, *, visible: bool = True, exclude: str = "", vision: str = ""+) -> None:+ config = tmp_path / "model_providers.yaml"+ config.write_text(+ "providers:\n"+ " - name: rhoai\n"+ " display_name: Red Hat OpenShift AI\n"+ " modes:\n"+ f" oss: {'true' if visible else 'false'}\n"+ " models:\n - granite-3.3-2b-instruct\n"+ + (f" exclude_models:\n - '{exclude}'\n" if exclude else "")+ + (f" vision_models:\n - '{vision}'\n" if vision else "")+ + " - name: openai\n"+ " display_name: OpenAI\n"+ " modes:\n"+ " oss: true\n",+ encoding="utf-8",+ )+ monkeypatch.setenv("OPENRAG_RUN_MODE", "oss")+ monkeypatch.setenv("OPENRAG_MODEL_PROVIDERS_CONFIG", str(config))+ model_providers.reload()+++def _saved(monkeypatch, **credentials: Any) -> None:+ providers = ProvidersConfig(+ openai=OpenAIConfig(),+ anthropic=AnthropicConfig(),+ watsonx=WatsonXConfig(),+ ollama=OllamaConfig(),+ custom={"rhoai": GenericProviderConfig(credentials=credentials, configured=True)}+ if credentials+ else {},+ )+ monkeypatch.setattr(+ models_api, "get_openrag_config", lambda: SimpleNamespace(providers=providers)+ )+++def _listing(monkeypatch, result: Any) -> dict[str, Any]:+ seen: dict[str, Any] = {}++ async def _list(credentials):+ seen.update(credentials)+ return result++ monkeypatch.setattr(rhoai, "list_cluster_models", _list)+ return seen+++async def _discover(provider: str = "rhoai", **credentials: str):+ return await models_api.discover_provider_models(+ provider,+ body=models_api.ModelDiscoveryBody(credentials=credentials),+ user=SimpleNamespace(),+ )+++def test_it_requires_providers_write():+ """It sends credentials to an operator-typed host, like the spaces route."""+ default = inspect.signature(models_api.discover_provider_models).parameters["user"].default+ assert isinstance(default, fastapi_params.Depends)+ perms = [cell.cell_contents for cell in default.dependency.__closure__]+ assert "providers:write" in perms+++async def test_the_cluster_listing_reaches_both_pickers(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ _listing(+ monkeypatch,+ rhoai.ClusterModels(chat=("gpt-oss-120b",), embedding=("granite-embedding-english-r2",)),+ )++ response = await _discover(api_base=CHAT_BASE, api_key="typed-token")++ assert response.status_code == 200+ assert response.headers["cache-control"] == "no-store"+ assert json.loads(response.body) == {+ "models": [{"model": "gpt-oss-120b", "mode": "chat"}],+ "embedding_models": [{"model": "granite-embedding-english-r2", "mode": "embedding"}],+ }+++def _kind_aware_listing(monkeypatch, result: Any) -> list[Any]:+ """A lister that declares `kind`, recording what each call passed (or `"omitted"`)."""+ kinds: list[Any] = []++ async def _list(credentials, *, kind="omitted"):+ kinds.append(kind)+ return result++ monkeypatch.setattr(rhoai, "list_cluster_models", _list)+ return kinds++[email protected]("kind", ["chat", "embedding"])+async def test_the_picker_kind_reaches_a_lister_that_takes_it(tmp_path, monkeypatch, kind):+ """Each onboarding step asks for its own picker, so a dead embedding+ endpoint cannot stall the language step."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ kinds = _kind_aware_listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ response = await models_api.discover_provider_models(+ "rhoai",+ body=models_api.ModelDiscoveryBody(+ credentials={"api_base": CHAT_BASE, "api_key": "t"}, kind=kind+ ),+ user=SimpleNamespace(),+ )++ assert response.status_code == 200+ assert kinds == [kind]+++async def test_without_a_kind_the_lister_is_asked_for_both(tmp_path, monkeypatch):+ """An older client that sends no kind keeps the both-pickers behaviour."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ kinds = _kind_aware_listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(api_base=CHAT_BASE, api_key="t")++ assert kinds == ["omitted"]+++async def test_a_lister_without_a_kind_is_called_without_one(tmp_path, monkeypatch):+ """A single-endpoint provider lists everything at once and takes no kind."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=("gpt-oss-120b",), embedding=None))++ response = await models_api.discover_provider_models(+ "rhoai",+ body=models_api.ModelDiscoveryBody(+ credentials={"api_base": CHAT_BASE, "api_key": "t"}, kind="embedding"+ ),+ user=SimpleNamespace(),+ )++ assert response.status_code == 200+ assert seen == {"api_base": CHAT_BASE, "api_key": "t"}+++def test_an_unknown_kind_is_rejected():+ with pytest.raises(pydantic.ValidationError):+ models_api.ModelDiscoveryBody(kind="rerank")+++async def test_a_half_that_could_not_be_listed_is_null(tmp_path, monkeypatch):+ """Null tells the picker to keep the catalogue's rows; [] would empty it."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ _listing(monkeypatch, rhoai.ClusterModels(chat=("gpt-oss-120b",), embedding=None))++ body = json.loads((await _discover(api_base=CHAT_BASE, api_key="t")).body)++ assert body["embedding_models"] is None+++async def test_excluded_models_stay_excluded(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch, exclude="internal-*")+ _saved(monkeypatch)+ _listing(+ monkeypatch,+ rhoai.ClusterModels(chat=("gpt-oss-120b", "internal-draft"), embedding=None),+ )++ body = json.loads((await _discover(api_base=CHAT_BASE, api_key="t")).body)++ assert [row["model"] for row in body["models"]] == ["gpt-oss-120b"]+++async def test_vision_models_tag_discovered_chat_rows(tmp_path, monkeypatch):+ """`vision_models` reaches discovery too, and never an embedding row."""+ _offer_rhoai(tmp_path, monkeypatch, vision="*-vision-*")+ _saved(monkeypatch)+ _listing(+ monkeypatch,+ rhoai.ClusterModels(+ chat=("granite-vision-3.3-2b", "gpt-oss-120b"),+ embedding=("granite-vision-embedding",),+ ),+ )++ body = json.loads((await _discover(api_base=CHAT_BASE, api_key="t")).body)++ capabilities = {row["model"]: row.get("capabilities") for row in body["models"]}+ assert capabilities == {"granite-vision-3.3-2b": ["vision"], "gpt-oss-120b": None}+ assert "capabilities" not in body["embedding_models"][0]+++async def test_discovered_rows_match_the_catalogue_for_the_same_listing(tmp_path, monkeypatch):+ """One id reads the same before its credentials are saved as after.++ Discovery answers for the unsaved form and the catalogue for the saved+ one; if a config rule reached only one of them, a model would change+ capabilities — or reappear — the moment onboarding was submitted.+ """+ _offer_rhoai(tmp_path, monkeypatch, exclude="internal-*", vision="*-vision-*")+ _saved(monkeypatch)+ listed = rhoai.ClusterModels(+ chat=("gpt-oss-120b", "granite-vision-3.3-2b", "internal-draft"),+ embedding=("granite-embedding-english-r2", "internal-embed"),+ )+ _listing(monkeypatch, listed)+ monkeypatch.setattr(rhoai, "cached_models", lambda: listed)++ body = json.loads((await _discover(api_base=CHAT_BASE, api_key="t")).body)+ catalogued = next(+ entry for entry in model_catalog.catalog()["providers"] if entry["key"] == "rhoai"+ )++ def _by_model(rows):+ return sorted(rows, key=lambda row: row["model"])++ assert _by_model(body["models"]) == _by_model(catalogued["models"])+ assert _by_model(body["embedding_models"]) == _by_model(catalogued["embedding_models"])+++async def test_a_saved_token_fills_a_blank_secret_on_the_same_host(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch, api_base=CHAT_BASE, embedding_api_base=EMBED_BASE, api_key=SAVED_TOKEN)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(api_base=CHAT_BASE, api_key="")++ assert seen == {+ "api_base": CHAT_BASE,+ "embedding_api_base": EMBED_BASE,+ "api_key": SAVED_TOKEN,+ }+++ "changed",+ [+ {"api_base": "https://elsewhere.example.com/v1"},+ {"embedding_api_base": "https://elsewhere.example.com/v1"},+ {"ssl_verify": "false"},+ ],+)+async def test_a_saved_token_never_goes_to_a_new_target(tmp_path, monkeypatch, changed):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch, api_base=CHAT_BASE, embedding_api_base=EMBED_BASE, api_key=SAVED_TOKEN)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(**changed)++ assert seen == changed+++ "cleared, saved_extra",+ [+ ("embedding_api_base", {}),+ ("ssl_verify", {"ssl_verify": "/etc/pki/cluster-ca.crt"}),+ ],+)+async def test_clearing_a_saved_target_is_a_new_target(tmp_path, monkeypatch, cleared, saved_extra):+ # Submit sends a cleared field as a removal, so discovery must not ask the+ # cluster with the stored value — nor send the saved token along with it.+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(+ monkeypatch,+ api_base=CHAT_BASE,+ embedding_api_base=EMBED_BASE,+ api_key=SAVED_TOKEN,+ **saved_extra,+ )+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(api_base=CHAT_BASE, api_key="", **{cleared: ""})++ assert seen == {"api_base": CHAT_BASE}+++async def test_a_blank_target_that_was_never_saved_keeps_the_saved_token(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch, api_base=CHAT_BASE, api_key=SAVED_TOKEN)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(api_base=CHAT_BASE, embedding_api_base="", ssl_verify=" ", api_key="")++ assert seen == {"api_base": CHAT_BASE, "api_key": SAVED_TOKEN}+++async def test_fields_the_form_does_not_define_are_dropped(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))++ await _discover(api_base=CHAT_BASE, api_key="t", extra_headers="x")++ assert "extra_headers" not in seen+++async def test_a_provider_that_cannot_list_its_models_is_404(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)++ assert (await _discover("openai")).status_code == 404+ assert (await _discover("no-such-provider")).status_code == 404+++async def test_a_hidden_provider_is_404(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch, visible=False)+ _listing(monkeypatch, rhoai.ClusterModels(chat=("x",), embedding=None))++ assert (await _discover(api_base=CHAT_BASE, api_key="t")).status_code == 404+++async def test_an_unexpected_failure_is_not_echoed(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)++ async def _boom(credentials):+ raise RuntimeError("Traceback: Bearer exposed-secret")++ monkeypatch.setattr(rhoai, "list_cluster_models", _boom)++ response = await _discover(api_base=CHAT_BASE, api_key="t")++ assert response.status_code == 500+ assert b"exposed-secret" not in response.body+++async def test_an_unknown_auth_method_is_a_client_error(tmp_path, monkeypatch):+ """A bad request, not a server fault: 400 with a fixed message, not the hook's."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ _listing(monkeypatch, rhoai.ClusterModels(chat=("x",), embedding=None))++ def _fields(method):+ raise ValueError("hook detail: exposed-secret")++ monkeypatch.setattr(rhoai, "credential_fields_for_auth_method", _fields, raising=False)++ response = await models_api.discover_provider_models(+ "rhoai",+ body=models_api.ModelDiscoveryBody(+ credentials={"api_base": CHAT_BASE, "api_key": "t"}, auth_method="bogus"+ ),+ user=SimpleNamespace(),+ )++ assert response.status_code == 400+ assert json.loads(response.body) == {"error": models_api._INVALID_AUTH_METHOD_ERROR}+ assert b"exposed-secret" not in response.body+++async def test_the_provider_picks_the_auth_method_when_none_is_given(tmp_path, monkeypatch):+ """The fallback is the enhancement's own rule, not one written into the route."""+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ seen = _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))+ methods: list[str] = []++ def _fields(method):+ methods.append(method)+ return frozenset({"api_base", "api_key"})++ monkeypatch.setattr(rhoai, "credential_fields_for_auth_method", _fields, raising=False)+ monkeypatch.setattr(rhoai, "default_auth_method", lambda stored: "token", raising=False)++ await _discover(api_base=CHAT_BASE, api_key="t", ssl_verify="false")++ assert methods == ["token"]+ assert seen == {"api_base": CHAT_BASE, "api_key": "t"}+++async def test_no_auth_method_and_no_default_is_a_client_error(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)+ _saved(monkeypatch)+ _listing(monkeypatch, rhoai.ClusterModels(chat=None, embedding=None))+ monkeypatch.setattr(+ rhoai,+ "credential_fields_for_auth_method",+ lambda method: frozenset({"api_base", "api_key"}),+ raising=False,+ )++ response = await _discover(api_base=CHAT_BASE, api_key="t")++ assert response.status_code == 400+ assert json.loads(response.body) == {"error": models_api._INVALID_AUTH_METHOD_ERROR}+++def test_the_catalogue_says_which_providers_can_discover(tmp_path, monkeypatch):+ _offer_rhoai(tmp_path, monkeypatch)++ entries = {entry["key"]: entry for entry in model_catalog.catalog()["providers"]}++ assert entries["rhoai"]["discovers_models"] is True+ assert entries["openai"]["discovers_models"] is Falsediff --git a/tests/unit/api/test_settings_endpoints.py b/tests/unit/api/test_settings_endpoints.pyindex 5b80bdc2d..0bee3ba21 100644--- a/tests/unit/api/test_settings_endpoints.py+++ b/tests/unit/api/test_settings_endpoints.py@@ -409,6 +409,56 @@ async def test_update_settings_rejects_invalid_azure_foundry_key_before_saving()save.assert_not_called()+async def test_update_settings_saves_the_auth_method_it_validated_for_a_mixed_case_key():+ """The save must narrow by the same normalized key the probe used.++ Regression: the save read `provider_auth_methods` by the lowercased+ provider key, so `{"Azure": ...}` validated the new method but saved with+ none — keeping the old method's secret and the old `auth_method`.+ """+ from api.settings.endpoints import update_settings+ from config.config_manager import OpenRAGConfig++ config = OpenRAGConfig.from_dict({})+ config.edited = True+ config.providers.set_credentials(+ "azure",+ {"api_key": "old-key", "api_base": "https://example.openai.azure.com"},+ auth_method="api_key",+ )+ body = SettingsUpdateBody(+ provider_credentials={"Azure": {"azure_ad_token": "new-token"}},+ provider_auth_methods={"Azure": "entra_token"},+ )+ rbac = MagicMock()+ rbac.has_permission = AsyncMock(return_value=True)++ with (+ patch("api.settings.endpoints.get_openrag_config", return_value=config),+ patch("api.settings.endpoints.validate_provider_setup", new_callable=AsyncMock) as validate,+ patch("api.settings.endpoints.config_manager.save_config_file", return_value=True) as save,+ patch("api.settings.endpoints.clients.refresh_patched_client", new_callable=AsyncMock),+ ):+ response = await update_settings(+ body=body,+ session_manager=AsyncMock(),+ user=MagicMock(spec=User),+ models_service=MagicMock(),+ rbac=rbac,+ )++ assert getattr(response, "status_code", 200) == 200+ assert "api_key" not in validate.await_args.kwargs["credentials"]+ save.assert_called_once()+ saved = save.call_args[0][0].providers+ assert saved.custom["azure"].auth_method == "entra_token"+ assert saved.stored_credentials("azure") == {+ "api_base": "https://example.openai.azure.com",+ "azure_ad_token": "new-token",+ }++@pytest.mark.asyncioasync def test_update_settings_validates_enhancement_credentials_before_saving():"""A credential-only save runs a provider enhancement's model-free check.@@ -817,6 +867,61 @@ async def test_onboarding_embedding_validation_receives_onprem_tls_policy():assert validate.await_args.kwargs["stored_credentials"]["ssl_verify"] == "false"+async def test_onboarding_narrows_credentials_by_auth_method_for_a_mixed_case_key():+ """Onboarding must find `provider_auth_methods` under the normalized key.++ Regression: a raw-key lookup gave `set_credentials` no method for+ `{"WatsonX_OnPrem": ...}`, so the previous method's secrets were kept and+ reached the validation probe.+ """+ from api.settings.endpoints import onboarding+ from config.config_manager import OpenRAGConfig++ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(+ "watsonx_onprem",+ {"api_base": "https://cpd.example.com", "username": "cpd-user", "api_key": "old"},+ auth_method="username_api_key",+ )+ body = OnboardingBody(+ llm_provider="watsonx_onprem",+ llm_model="ibm/granite-3-3-8b-instruct",+ provider_credentials={"WatsonX_OnPrem": {"zen_api_key": "zen-key"}},+ provider_auth_methods={"WatsonX_OnPrem": "zen_api_key"},+ )++ with (+ patch("api.settings.endpoints.get_openrag_config", return_value=config),+ patch(+ "api.settings.endpoints.TelemetryClient.send_event",+ new_callable=AsyncMock,+ ),+ patch(+ "api.settings.endpoints.validate_provider_setup",+ new_callable=AsyncMock,+ side_effect=Exception("stop after validation arguments are captured"),+ ) as validate,+ ):+ response = await onboarding(+ body=body,+ flows_service=MagicMock(),+ session_manager=AsyncMock(),+ document_service=MagicMock(),+ models_service=MagicMock(),+ task_service=MagicMock(),+ langflow_file_service=MagicMock(),+ knowledge_filter_service=MagicMock(),+ user=MagicMock(spec=User),+ )++ assert response.status_code == 400+ assert validate.await_args.kwargs["stored_credentials"] == {+ "api_base": "https://cpd.example.com",+ "zen_api_key": "zen-key",+ }++@pytest.mark.asyncioasync def test_onboarding_caps_chunk_size_for_watsonx_onprem_embeddings():from api.settings.endpoints import onboarding@@ -966,3 +1071,92 @@ async def test_non_provider_settings_save_keeps_recorded_failures():assert provider_error_log.latest_failure("rhoai", "chat") == "still failing"finally:provider_error_log.clear()+++def _config_with_saved_rhoai_token():+ from config.config_manager import OpenRAGConfig++ config = OpenRAGConfig.from_dict({})+ config.edited = True+ config.providers.set_credentials(+ "rhoai",+ {+ "api_base": "https://chat.host-a:8443/v1",+ "api_key": "sha256~host-a-token",+ },+ )+ return config+++async def test_update_settings_refuses_a_new_endpoint_without_the_token_before_probing():+ """A blank secret means "keep the saved one" — but not for a new host. The+ pre-save probe would otherwise send host A's token to host B."""+ from api.settings.endpoints import update_settings++ config = _config_with_saved_rhoai_token()+ body = SettingsUpdateBody(+ provider_credentials={"rhoai": {"api_base": "https://chat.host-b:8443/v1"}}+ )+ rbac = MagicMock()+ rbac.has_permission = AsyncMock(return_value=True)++ with (+ patch("api.settings.endpoints.get_openrag_config", return_value=config),+ patch("api.settings.endpoints.validate_provider_setup", new_callable=AsyncMock) as validate,+ patch("api.settings.endpoints.config_manager.save_config_file") as save,+ ):+ response = await update_settings(+ body=body,+ session_manager=AsyncMock(),+ user=MagicMock(spec=User),+ models_service=MagicMock(),+ rbac=rbac,+ )++ assert response.status_code == 400+ assert "Enter ServiceAccount token again" in _error_text(response)+ validate.assert_not_awaited()+ save.assert_not_called()+ assert config.providers.stored_credentials("rhoai")["api_base"] == (+ "https://chat.host-a:8443/v1"+ )+++async def test_onboarding_refuses_a_new_endpoint_without_the_token_before_probing():+ from api.settings.endpoints import onboarding++ config = _config_with_saved_rhoai_token()+ body = OnboardingBody(+ llm_provider="rhoai",+ llm_model="granite-chat",+ provider_credentials={"rhoai": {"api_base": "https://chat.host-b:8443/v1"}},+ )++ with (+ patch("api.settings.endpoints.get_openrag_config", return_value=config),+ patch(+ "api.settings.endpoints.TelemetryClient.send_event",+ new_callable=AsyncMock,+ ),+ patch("api.settings.endpoints.validate_provider_setup", new_callable=AsyncMock) as validate,+ ):+ response = await onboarding(+ body=body,+ flows_service=MagicMock(),+ session_manager=AsyncMock(),+ document_service=MagicMock(),+ models_service=MagicMock(),+ task_service=MagicMock(),+ langflow_file_service=MagicMock(),+ knowledge_filter_service=MagicMock(),+ user=MagicMock(spec=User),+ )++ assert response.status_code == 400+ assert "Enter ServiceAccount token again" in _error_text(response)+ validate.assert_not_awaited()+ assert config.providers.stored_credentials("rhoai")["api_base"] == (+ "https://chat.host-a:8443/v1"+ )diff --git a/tests/unit/config/test_generic_provider_config.py b/tests/unit/config/test_generic_provider_config.pyindex 981bac317..7b697ffb7 100644--- a/tests/unit/config/test_generic_provider_config.py+++ b/tests/unit/config/test_generic_provider_config.py@@ -1,3 +1,5 @@+import pytest+from config.config_manager import OpenRAGConfig@@ -390,13 +392,20 @@ def test_pending_stored_credentials_overlays_the_submission_on_the_stored_form(config = ConfigManager(config_file=tmp_path / "config.yaml").load_config()pending = config.providers.pending_stored_credentials(- "rhoai", {"embedding_api_base": " https://new-embed.svc:8443/v1 ", "ssl_verify": " "}+ "rhoai",+ {+ "embedding_api_base": " https://new-embed.svc:8443/v1 ",+ # A changed endpoint needs the token re-entered (see the+ # credential-target tests below).+ "api_key": "sha256~new-token",+ "ssl_verify": " ",+ },)assert pending == {"api_base": "https://chat.svc:8443/v1","embedding_api_base": "https://new-embed.svc:8443/v1",- "api_key": "sha256~token",+ "api_key": "sha256~new-token",}# Pending, not saved.assert config.providers.stored_credentials("rhoai")["embedding_api_base"] == (@@ -456,3 +465,265 @@ def test_the_settings_save_and_the_env_seed_agree_on_configured(monkeypatch, tmpassert seeded.providers.custom["rhoai"].configured is Falseassert saved.providers.custom["rhoai"].configured is False+++# --- Credential targets ------------------------------------------------------+#+# A secret is never echoed to a form, so a blank secret means "keep the saved+# one". That is only safe while the credentials still go where they went when it+# was saved: once `api_base`, `embedding_api_base` or `ssl_verify` changes, a+# kept token would be sent to the new host. The save, and the pre-save probe,+# refuse instead.++_RHOAI_SAVED = {+ "api_base": "https://chat.host-a:8443/v1",+ "embedding_api_base": "https://embed.host-a:8443/v1",+ "api_key": "sha256~host-a-token",+ "ssl_verify": "true",+}+++def _rhoai_config() -> OpenRAGConfig:+ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials("rhoai", dict(_RHOAI_SAVED))+ return config+++ ("submitted", "remove"),+ [+ ({"api_base": "https://chat.host-b:8443/v1"}, set()),+ ({"embedding_api_base": "https://embed.host-b:8443/v1"}, set()),+ ({"ssl_verify": "false"}, set()),+ ({}, {"ssl_verify"}),+ ],+ ids=["api_base", "embedding_api_base", "ssl_verify", "ssl_verify-cleared"],+)+def test_a_changed_target_without_the_secret_is_refused_and_nothing_changes(submitted, remove):+ from config.config_manager import CredentialTargetChangedError++ config = _rhoai_config()++ with pytest.raises(CredentialTargetChangedError) as exc_info:+ config.providers.set_credentials("rhoai", submitted, remove=remove)++ assert exc_info.value.fields == ("api_key",)+ # Client-safe: names the form label, never a value.+ assert "ServiceAccount token" in str(exc_info.value)+ assert "host-a-token" not in str(exc_info.value)+ assert config.providers.stored_credentials("rhoai") == _RHOAI_SAVED+ assert config.providers.custom["rhoai"].configured is True+++def test_the_same_target_keeps_the_saved_secret():+ config = _rhoai_config()++ config.providers.set_credentials(+ "rhoai",+ {+ # Resubmitted unchanged (the form pre-fills non-secrets) — not a change.+ "api_base": " https://chat.host-a:8443/v1 ",+ "embedding_max_concurrency": "4",+ },+ )++ assert config.providers.stored_credentials("rhoai")["api_key"] == "sha256~host-a-token"+++def test_a_changed_target_with_the_secret_re_entered_is_saved():+ config = _rhoai_config()++ config.providers.set_credentials(+ "rhoai",+ {"api_base": "https://chat.host-b:8443/v1", "api_key": "sha256~host-b-token"},+ )++ stored = config.providers.stored_credentials("rhoai")+ assert stored["api_base"] == "https://chat.host-b:8443/v1"+ assert stored["api_key"] == "sha256~host-b-token"+++def test_a_changed_target_with_the_secret_removed_is_saved_unconfigured():+ """Removing the secret is the other explicit answer: nothing is reused."""+ config = _rhoai_config()++ config.providers.set_credentials(+ "rhoai", {"api_base": "https://chat.host-b:8443/v1"}, remove={"api_key"}+ )++ assert "api_key" not in config.providers.stored_credentials("rhoai")+ assert config.providers.custom["rhoai"].configured is False++[email protected]("kind", ["chat", "embedding"])+def test_the_pre_save_probe_never_gets_the_stale_secret(kind):+ """Validation runs before the write, so it must refuse on the same terms —+ otherwise the probe itself sends host A's token to host B."""+ from config.config_manager import CredentialTargetChangedError++ config = _rhoai_config()+ submitted = {"api_base": "https://chat.host-b:8443/v1"}++ with pytest.raises(CredentialTargetChangedError):+ config.providers.pending_stored_credentials("rhoai", submitted)+ with pytest.raises(CredentialTargetChangedError):+ config.providers.pending_credentials("rhoai", submitted, kind=kind)+++def test_a_generic_provider_probe_is_refused_too():+ """Azure is not an enhancement: its call kwargs are its stored form."""+ from config.config_manager import CredentialTargetChangedError++ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(+ "azure",+ {"api_base": "https://a.openai.azure.com", "api_key": "azure-key"},+ auth_method="api_key",+ )++ with pytest.raises(CredentialTargetChangedError):+ config.providers.pending_credentials(+ "azure", {"api_base": "https://b.openai.azure.com"}, auth_method="api_key"+ )+ with pytest.raises(CredentialTargetChangedError):+ config.providers.set_credentials(+ "azure", {"api_base": "https://b.openai.azure.com"}, auth_method="api_key"+ )+ assert config.providers.stored_credentials("azure")["api_base"] == (+ "https://a.openai.azure.com"+ )+++_AZURE_TARGET_CASES = [+ pytest.param(+ "azure",+ "service_principal",+ {+ "api_base": "https://a.openai.azure.com",+ "api_version": "2024-10-21",+ "tenant_id": "tenant-a",+ "client_id": "client-a",+ "client_secret": "sp-secret-a",+ },+ "client_secret",+ "Client Secret",+ id="azure-service_principal",+ ),+ pytest.param(+ "azure_ai",+ None,+ {+ "api_base": "https://a.services.ai.azure.com",+ "api_key": "foundry-key-a",+ "api_version": "2024-05-01-preview",+ },+ "api_key",+ "API key",+ id="azure_ai",+ ),+]+++ ("provider", "auth_method", "saved", "secret", "label"), _AZURE_TARGET_CASES+)+def test_an_azure_retarget_asks_only_for_the_secret(provider, auth_method, saved, secret, label):+ """A service principal's secret goes only to Entra ID, but the token LiteLLM+ gets for it is tenant-wide and would be sent to the new `api_base` — so it is+ in scope like any other secret. Only the secret is asked for again, never the+ tenant or client id."""+ from config.config_manager import CredentialTargetChangedError++ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(provider, dict(saved), auth_method=auth_method)+ submitted = {"api_base": saved["api_base"].replace("https://a.", "https://b.")}++ with pytest.raises(CredentialTargetChangedError) as exc_info:+ config.providers.set_credentials(provider, submitted, auth_method=auth_method)++ assert exc_info.value.fields == (secret,)+ assert label in str(exc_info.value)+ assert saved[secret] not in str(exc_info.value)+ assert config.providers.stored_credentials(provider) == saved+++ ("provider", "auth_method", "saved", "secret", "label"), _AZURE_TARGET_CASES+)+def test_an_azure_retarget_with_the_secret_re_entered_is_saved(+ provider, auth_method, saved, secret, label+):+ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(provider, dict(saved), auth_method=auth_method)+ submitted = {+ "api_base": saved["api_base"].replace("https://a.", "https://b."),+ secret: "secret-b",+ }++ config.providers.set_credentials(provider, submitted, auth_method=auth_method)++ assert config.providers.stored_credentials(provider) == {**saved, **submitted}+++def test_switching_auth_method_with_a_new_target_drops_the_inactive_secret():+ """The old method's secret is not reused (it is out of scope for the new+ method) and must not reach the probe either."""+ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(+ "azure",+ {"api_base": "https://a.openai.azure.com", "api_key": "azure-key"},+ auth_method="api_key",+ )+ submitted = {"api_base": "https://b.openai.azure.com", "azure_ad_token": "entra-token"}++ probe = config.providers.pending_credentials("azure", submitted, auth_method="entra_token")+ config.providers.set_credentials("azure", submitted, auth_method="entra_token")++ assert "api_key" not in probe+ assert config.providers.stored_credentials("azure") == submitted+++def test_first_class_providers_are_exempt():+ """The bespoke forms take no free-text target — watsonx.ai offers only IBM's+ region hosts — and never promise that a saved key is withheld, so switching+ region keeps the saved key."""+ config = OpenRAGConfig.from_dict({})+ config.providers.set_credentials(+ "watsonx",+ {"api_base": "https://us-south.ml.cloud.ibm.com", "api_key": "ibm-key"},+ )++ config.providers.set_credentials("watsonx", {"api_base": "https://eu-de.ml.cloud.ibm.com"})++ assert config.providers.credential_values("watsonx")["api_key"] == "ibm-key"+++def test_an_unreadable_form_spec_fails_closed(monkeypatch):+ """Without the spec every stored non-target field may be a secret."""+ import services.model_catalog as model_catalog+ from config.config_manager import CredentialTargetChangedError++ config = _rhoai_config()++ def _broken(_provider):+ raise RuntimeError("catalogue unavailable")++ monkeypatch.setattr(model_catalog, "credential_fields", _broken)++ with pytest.raises(CredentialTargetChangedError) as exc_info:+ config.providers.set_credentials("rhoai", {"api_base": "https://chat.host-b:8443/v1"})+ assert exc_info.value.fields == ("api_key",)+++def test_discovery_and_save_share_one_target_rule():+ """`_draft_credentials` (model discovery) reuses the save's rule, so the+ UI's discovery never offers a secret the save then refuses."""+ from config.config_manager import retargeted_credential_fields++ stored = {"api_base": "https://a", "ssl_verify": "true"}++ assert retargeted_credential_fields(stored, {"api_base": "https://a"}, set()) == []+ assert retargeted_credential_fields(stored, {"api_base": "https://b"}, set()) == ["api_base"]+ assert retargeted_credential_fields(stored, {}, {"ssl_verify"}) == ["ssl_verify"]+ # Clearing something that was never stored is not a change.+ assert retargeted_credential_fields(stored, {}, {"embedding_api_base"}) == []diff --git a/tests/unit/enhancements/providers/redhat/test_openshift_ai.py b/tests/unit/enhancements/providers/redhat/test_openshift_ai.pyindex ac084eedb..1c336fe40 100644--- a/tests/unit/enhancements/providers/redhat/test_openshift_ai.py+++ b/tests/unit/enhancements/providers/redhat/test_openshift_ai.py@@ -878,6 +878,214 @@ async def test_incomplete_credentials_are_not_taken_to_the_network(monkeypatch)assert await rhoai.fetch_models({"api_base": CHAT_BASE}) is None+async def test_draft_discovery_lists_each_endpoint(monkeypatch) -> None:+ """Onboarding asks before anything is saved; each endpoint fills its own picker."""+ seen: dict[str, Any] = {}+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning(+ {+ f"{CHAT_BASE}/models": _Response(200, _models_body("gpt-oss-120b")),+ f"{EMBED_BASE}/models": _Response(200, _models_body(EMBED_MODEL)),+ },+ seen,+ ),+ )++ models = await rhoai.list_cluster_models(_stored())++ assert models == rhoai.ClusterModels(chat=("gpt-oss-120b",), embedding=(EMBED_MODEL,))+ assert seen["headers"]["Authorization"] == f"Bearer {TOKEN}"+++async def test_draft_discovery_on_one_endpoint_fills_both_pickers(monkeypatch) -> None:+ seen: dict[str, Any] = {}+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning({f"{CHAT_BASE}/models": _Response(200, _models_body("solo"))}, seen),+ )++ models = await rhoai.list_cluster_models(_stored(embedding_api_base=None))++ assert models == rhoai.ClusterModels(chat=("solo",), embedding=("solo",))+ assert seen["urls"] == [f"{CHAT_BASE}/models"]+++async def test_draft_discovery_leaves_a_failed_half_unknown(monkeypatch) -> None:+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning(+ {+ f"{CHAT_BASE}/models": _Response(200, _models_body(CHAT_MODEL)),+ f"{EMBED_BASE}/models": _Response(401, {"error": {"message": "nope"}}),+ }+ ),+ )+ monkeypatch.setattr(rhoai, "_http_request_with_retry", _no_retry)++ models = await rhoai.list_cluster_models(_stored())++ assert models == rhoai.ClusterModels(chat=(CHAT_MODEL,), embedding=None)+++ "kind, asked, expected",+ [+ ("chat", CHAT_BASE, rhoai.ClusterModels(chat=(CHAT_MODEL,), embedding=None)),+ ("embedding", EMBED_BASE, rhoai.ClusterModels(chat=None, embedding=(EMBED_MODEL,))),+ ],+)+async def test_draft_discovery_for_one_picker_asks_only_its_endpoint(+ monkeypatch, kind, asked, expected+) -> None:+ """Each onboarding step shows one picker; the other endpoint is not its concern."""+ seen: dict[str, Any] = {}+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning(+ {+ f"{CHAT_BASE}/models": _Response(200, _models_body(CHAT_MODEL)),+ f"{EMBED_BASE}/models": _Response(200, _models_body(EMBED_MODEL)),+ },+ seen,+ ),+ )++ models = await rhoai.list_cluster_models(_stored(), kind=kind)++ assert models == expected+ assert seen["urls"] == [f"{asked}/models"]+++async def test_an_unreachable_embedding_endpoint_does_not_hold_up_the_chat_picker(+ monkeypatch,+) -> None:+ """The review case: chat answers at once, so its step must not wait out the+ embedding endpoint's retries."""++ async def _hang_on_embedding(method, url, *, client, **kwargs):+ if url.startswith(EMBED_BASE):+ raise AssertionError("the embedding endpoint should not be asked")+ return await client.request(method, url, **kwargs)++ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning({f"{CHAT_BASE}/models": _Response(200, _models_body(CHAT_MODEL))}),+ )+ monkeypatch.setattr(rhoai, "_http_request_with_retry", _hang_on_embedding)++ models = await rhoai.list_cluster_models(_stored(), kind="chat")++ assert models == rhoai.ClusterModels(chat=(CHAT_MODEL,), embedding=None)+++async def test_draft_discovery_for_one_picker_on_a_shared_endpoint_fills_both(+ monkeypatch,+) -> None:+ """One endpoint serves both, so asking it for one picker answers the other too."""+ seen: dict[str, Any] = {}+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning({f"{CHAT_BASE}/models": _Response(200, _models_body("solo"))}, seen),+ )++ models = await rhoai.list_cluster_models(_stored(embedding_api_base=None), kind="embedding")++ assert models == rhoai.ClusterModels(chat=("solo",), embedding=("solo",))+ assert seen["urls"] == [f"{CHAT_BASE}/models"]+++async def test_draft_discovery_never_touches_the_catalogue_cache(monkeypatch) -> None:+ """`cached_models()` feeds every picker, so a draft cluster's listing must not+ replace the saved cluster's — nor be dropped by it."""+ other = "https://draft-chat.svc:8443/v1"+ monkeypatch.setattr(+ "httpx.AsyncClient",+ _client_returning(+ {+ f"{CHAT_BASE}/models": _Response(200, _models_body(CHAT_MODEL)),+ f"{EMBED_BASE}/models": _Response(200, _models_body(EMBED_MODEL)),+ f"{other}/models": _Response(200, _models_body("draft-model")),+ }+ ),+ )+ await rhoai.fetch_models(_stored())+ saved = rhoai.cached_models()++ draft = await rhoai.list_cluster_models(_stored(api_base=other, embedding_api_base=None))++ assert draft.chat == ("draft-model",)+ assert rhoai.cached_models() == saved+++async def test_draft_discovery_retries_less_than_the_catalogue(monkeypatch) -> None:+ """Onboarding has someone watching the picker; the catalogue can wait out a rollout."""+ calls: list[int] = []++ async def _count(method, url, *, client, max_retries=2, **kwargs):+ calls.append(max_retries)+ return _Response(200, _models_body(CHAT_MODEL))++ monkeypatch.setattr("httpx.AsyncClient", _client_returning({}))+ monkeypatch.setattr(rhoai, "_http_request_with_retry", _count)++ await rhoai.list_cluster_models(_stored())+ assert calls == [rhoai.DISCOVERY_MAX_RETRIES] * 2+ assert rhoai.DISCOVERY_MAX_RETRIES < 2++ calls.clear()+ rhoai.forget_models()+ await rhoai.fetch_models(_stored())+ assert calls == [2, 2]++[email protected]("lister", ["list_cluster_models", "fetch_models"])+async def test_both_endpoints_are_asked_at_once(monkeypatch, lister) -> None:+ """One after the other, a mistyped host's timeouts would add up — for the+ onboarding form and the catalogue alike, since both list through one helper."""+ import asyncio++ arrived: list[str] = []+ both_asked = asyncio.Event()++ async def _barrier(method, url, *, client, **kwargs):+ arrived.append(url)+ if len(arrived) == 2:+ both_asked.set()+ # Asked one at a time, the first call never sees the second arrive.+ await asyncio.wait_for(both_asked.wait(), timeout=1)+ model = CHAT_MODEL if url.startswith(CHAT_BASE) else EMBED_MODEL+ return _Response(200, _models_body(model))++ monkeypatch.setattr("httpx.AsyncClient", _client_returning({}))+ monkeypatch.setattr(rhoai, "_http_request_with_retry", _barrier)++ models = await getattr(rhoai, lister)(_stored())++ assert models == rhoai.ClusterModels(chat=(CHAT_MODEL,), embedding=(EMBED_MODEL,))+++async def test_draft_discovery_with_incomplete_credentials_makes_no_request(monkeypatch) -> None:+ def _explode(**kwargs):+ raise AssertionError("no HTTP call should be made")++ monkeypatch.setattr("httpx.AsyncClient", _explode)++ unknown = rhoai.ClusterModels(chat=None, embedding=None)+ assert await rhoai.list_cluster_models({"api_key": TOKEN}) == unknown+ assert await rhoai.list_cluster_models({"api_base": CHAT_BASE}) == unknown++@pytest.mark.parametrize("body, expected",[@@ -1333,15 +1541,18 @@ async def test_missing_credentials_say_what_is_missing(monkeypatch) -> None:# ---------------------------------------------------------------------------[email protected]("run_mode", ["oss", "on_prem", "saas"])-def test_it_is_hidden_everywhere_as_shipped(monkeypatch, run_mode) -> None:- """Its endpoints and token are deployment-specific, so it is turned on with+ ("run_mode", "visible"), [("oss", False), ("on_prem", True), ("saas", False)]+)+def test_it_is_offered_only_on_prem_as_shipped(monkeypatch, run_mode, visible) -> None:+ """It runs inside the customer's own OpenShift cluster, so it is offered in+ on-prem deployments only; elsewhere it is turned on withOPENRAG_MODEL_PROVIDERS_CONFIG rather than offered to everyone."""monkeypatch.delenv("OPENRAG_MODEL_PROVIDERS_CONFIG", raising=False)monkeypatch.setenv("OPENRAG_RUN_MODE", run_mode)model_providers.reload()- assert PROVIDER not in model_providers.visible_provider_keys()+ assert (PROVIDER in model_providers.visible_provider_keys()) is visibledef test_a_deployment_can_offer_it_with_a_config_override(tmp_path, monkeypatch) -> None:diff --git a/tests/unit/enhancements/providers/test_registry_model_discovery.py b/tests/unit/enhancements/providers/test_registry_model_discovery.pynew file mode 100644index 000000000..fd349d70d--- /dev/null+++ b/tests/unit/enhancements/providers/test_registry_model_discovery.py@@ -0,0 +1,90 @@+"""`list_cluster_models_for`: draft discovery narrowed to one picker when it can be.++`kind` is the optional half of the `list_cluster_models` contract, passed only+to a module that declares it — the same rule `credentials_for` applies to+`litellm_credentials` — so a single-endpoint provider keeps its simpler hook.+"""++from __future__ import annotations++from types import SimpleNamespace+from typing import Any++import pytest++from enhancements.providers import registry+++def _lister_taking_kind(calls: list[dict[str, Any]]):+ async def list_cluster_models(credentials, *, kind=None):+ calls.append({"credentials": credentials, "kind": kind})+ return "listed"++ return SimpleNamespace(__name__="with_kind", list_cluster_models=list_cluster_models)+++def _lister_without_kind(calls: list[dict[str, Any]]):+ async def list_cluster_models(credentials):+ calls.append({"credentials": credentials})+ return "listed"++ return SimpleNamespace(__name__="without_kind", list_cluster_models=list_cluster_models)++[email protected]("kind", ["chat", "embedding"])+async def test_a_lister_that_declares_kind_is_given_it(kind):+ calls: list[dict[str, Any]] = []++ result = await registry.list_cluster_models_for(+ _lister_taking_kind(calls), {"api_key": "t"}, kind=kind+ )++ assert result == "listed"+ assert calls == [{"credentials": {"api_key": "t"}, "kind": kind}]+++async def test_no_kind_is_not_passed_even_to_a_lister_that_declares_it():+ """None means both pickers; the hook's own default says how to list both."""+ calls: list[dict[str, Any]] = []++ await registry.list_cluster_models_for(_lister_taking_kind(calls), {})++ assert calls == [{"credentials": {}, "kind": None}]+++async def test_a_lister_without_kind_is_called_without_it():+ calls: list[dict[str, Any]] = []++ result = await registry.list_cluster_models_for(+ _lister_without_kind(calls), {"api_key": "t"}, kind="embedding"+ )++ assert result == "listed"+ assert calls == [{"credentials": {"api_key": "t"}}]+++async def test_an_unintrospectable_lister_is_called_without_kind(monkeypatch):+ """Assume the base contract rather than risk a TypeError on a real call."""+ calls: list[dict[str, Any]] = []++ def _no_signature(_callable):+ raise ValueError("no signature found")++ monkeypatch.setattr(registry.inspect, "signature", _no_signature)++ await registry.list_cluster_models_for(_lister_without_kind(calls), {}, kind="chat")++ assert calls == [{"credentials": {}}]+++def test_the_shipped_listers_follow_the_contract():+ """RHOAI has an endpoint per picker and takes kind; watsonx.ai on-prem has one."""+ from enhancements.providers.redhat import openshift_ai+ from enhancements.providers.watsonx import onprem++ assert registry._hook_accepts_kind(openshift_ai, "list_cluster_models") is True+ assert registry._hook_accepts_kind(onprem, "list_cluster_models") is Falsediff --git a/tests/unit/enhancements/providers/watsonx/test_onprem.py b/tests/unit/enhancements/providers/watsonx/test_onprem.pyindex 930397531..ed1fff743 100644--- a/tests/unit/enhancements/providers/watsonx/test_onprem.py+++ b/tests/unit/enhancements/providers/watsonx/test_onprem.py@@ -671,6 +671,38 @@ def test_auth_method_changes_preserve_the_tls_policy() -> None:}+ "stored, expected",+ [+ ({"zen_api_key": "cHJlOmVuY29kZWQ="}, "zen_api_key"),+ ({"username": "cpduser", "api_key": "APIKEY"}, "username_api_key"),+ # A blank Zen key is not a Zen key.+ ({"zen_api_key": " ", "username": "cpduser"}, "username_api_key"),+ ({}, "username_api_key"),+ (None, "username_api_key"),+ ],+)+def test_the_default_auth_method_follows_the_saved_secret(stored, expected) -> None:+ assert watsonx_onprem.default_auth_method(stored) == expected+++def test_the_registry_exposes_the_default_auth_method(monkeypatch) -> None:+ from enhancements.providers import registry++ assert (+ registry.default_auth_method_for(PROVIDER, {"zen_api_key": "cHJlOmVuY29kZWQ="})+ == "zen_api_key"+ )+ # No hook: nothing to assume.+ assert registry.default_auth_method_for("openai", {}) is None++ def broken(stored):+ raise RuntimeError("boom")++ monkeypatch.setattr(watsonx_onprem, "default_auth_method", broken)+ assert registry.default_auth_method_for(PROVIDER, {}) is None++def test_cleared_optional_credential_is_removed() -> None:providers = _providers(api_base="https://cpd.example.com",@@ -982,6 +1014,49 @@ async def test_an_empty_listing_is_treated_as_a_bad_filter_not_an_empty_cluster(assert watsonx_onprem.cached_models() is None+async def test_draft_discovery_lists_without_caching(monkeypatch, _forget_cluster_models) -> None:+ """Onboarding lists a cluster before its credentials are saved; that answer+ must not reach the catalogue through `cached_models()`."""++ class _Client:+ def __init__(self, **kwargs):+ pass++ async def __aenter__(self):+ return self++ async def __aexit__(self, *exc):+ return False++ async def get(self, url, headers=None, params=None):+ return _specs_response(CLUSTER_RESOURCES)++ monkeypatch.setattr("httpx.AsyncClient", _Client)+ credentials = {"api_base": "https://cpd.example.com", "username": "u", "api_key": "k"}++ models = await watsonx_onprem.list_cluster_models(credentials)++ assert models.chat == ("openai/gpt-oss-120b",)+ assert models.embedding == ("ibm/slate-30m-english-rtrvr",)+ assert watsonx_onprem.cached_models() is None++ await watsonx_onprem.fetch_models(credentials)+ assert watsonx_onprem.cached_models() == models+++async def test_draft_discovery_with_incomplete_credentials_makes_no_request(+ monkeypatch,+) -> None:+ def _explode(**kwargs):+ raise AssertionError("no HTTP call should be made")++ monkeypatch.setattr("httpx.AsyncClient", _explode)++ assert await watsonx_onprem.list_cluster_models({"api_base": "https://cpd.example.com"}) is None++@pytest.mark.asyncioasync def test_validating_an_embedding_model_sends_a_list_not_a_string(monkeypatch) -> None:"""watsonx.ai takes `inputs` only as an array.diff --git a/tests/unit/test_models_api_errors.py b/tests/unit/test_models_api_errors.pyindex 4d52ca864..5fd228747 100644--- a/tests/unit/test_models_api_errors.py+++ b/tests/unit/test_models_api_errors.py@@ -307,6 +307,94 @@ async def test_watsonx_space_listing_hides_upstream_error_details(monkeypatch):assert b"exposed-secret" not in response.body+async def test_watsonx_space_listing_infers_the_auth_method_from_a_saved_zen_key(monkeypatch):+ from config.config_manager import (+ AnthropicConfig,+ GenericProviderConfig,+ OllamaConfig,+ OpenAIConfig,+ ProvidersConfig,+ WatsonXConfig,+ )+ from enhancements.providers.watsonx import onprem++ providers = ProvidersConfig(+ openai=OpenAIConfig(),+ anthropic=AnthropicConfig(),+ watsonx=WatsonXConfig(),+ ollama=OllamaConfig(),+ custom={+ onprem.PROVIDER_KEY: GenericProviderConfig(+ credentials={+ "api_base": "https://cpd.example.com",+ "zen_api_key": "saved-zen",+ },+ configured=True,+ )+ },+ )+ monkeypatch.setattr(+ models_api,+ "get_openrag_config",+ lambda: SimpleNamespace(providers=providers),+ )+ seen = {}++ async def _list_spaces(credentials):+ seen.update(credentials)+ return []++ monkeypatch.setattr(onprem, "list_spaces", _list_spaces)++ response = await models_api.get_watsonx_onprem_spaces(+ body=models_api.WatsonxOnPremSpacesBody(credentials={"username": "typed-user"}),+ user=SimpleNamespace(),+ )++ assert response.status_code == 200+ # Zen fields apply, so the username typed for the other method is dropped.+ assert seen == {"api_base": "https://cpd.example.com", "zen_api_key": "saved-zen"}+++async def test_watsonx_space_listing_rejects_an_unknown_auth_method(monkeypatch):+ from config.config_manager import (+ AnthropicConfig,+ OllamaConfig,+ OpenAIConfig,+ ProvidersConfig,+ WatsonXConfig,+ )+ from enhancements.providers.watsonx import onprem++ providers = ProvidersConfig(+ openai=OpenAIConfig(),+ anthropic=AnthropicConfig(),+ watsonx=WatsonXConfig(),+ ollama=OllamaConfig(),+ )+ monkeypatch.setattr(+ models_api,+ "get_openrag_config",+ lambda: SimpleNamespace(providers=providers),+ )+ list_spaces = AsyncMock(return_value=[])+ monkeypatch.setattr(onprem, "list_spaces", list_spaces)++ response = await models_api.get_watsonx_onprem_spaces(+ body=models_api.WatsonxOnPremSpacesBody(+ credentials={"api_base": "https://cpd.example.com"},+ auth_method="bogus",+ ),+ user=SimpleNamespace(),+ )++ assert response.status_code == 400+ assert json.loads(response.body) == {"error": models_api._INVALID_AUTH_METHOD_ERROR}+ list_spaces.assert_not_awaited()++@pytest.mark.asyncioasync def test_get_model_catalog_returns_only_supported_providers():"""The catalogue publishes exactly what this run mode's config exposes.