The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[BOT-2138] Centralize shared logic of metabot LLM provider adapters (#82165)
Voytek Pitułacd6bcc23e0ba
diff --git a/src/metabase/llm/api/provider.clj b/src/metabase/llm/api/provider.cljindex f6012416ca4..fb6d35914da 100644--- a/src/metabase/llm/api/provider.clj+++ b/src/metabase/llm/api/provider.clj@@ -175,7 +175,7 @@`probe?` asks a type that can check more than its credentials to do so — vLLM exercises the tool calling andstructured output the agent loop depends on against the model it will run on. Only [[verify-credentials!]] setsit: a probe generates, so it is far too slow for a plain listing. A probe reports whatever it determined about the- connection as `:learned-config`, passed through here for [[verify-credentials!]]'s callers to store on it."+ connection as `:connection-info`, passed through here for [[verify-credentials!]]'s callers to store on it."[{conn-key :key :keys [type config]} config-override model probe?](let [fixed (llm.provider/fixed-models type)](if (llm.provider/managed-type? type)@@ -194,7 +194,7 @@model (assoc :model model)proposed-model (assoc :proposed-model proposed-model)probe? (assoc :probe? true)))]- (merge (select-keys listed [:learned-config])+ (merge (select-keys listed [:connection-info]){:models (or config-models (vec (:models listed)))}))(catch clojure.lang.ExceptionInfo e(if (provider-client-error? e)@@ -274,7 +274,7 @@for a type that probes more than its credentials, by exercising the model it will run on. Throws a 400 carryingthe provider's own message when the credentials are rejected.- Returns the model listing and `:learned-config`: whatever the probe determined about the connection, for the+ Returns the model listing and `:connection-info`: whatever the probe determined about the connection, for thecaller to store on it. A probe records the model it exercised as `:probed-model`."[conn config model](when-not (llm.provider/managed-type? (:type conn))@@ -438,14 +438,14 @@:name (or (not-empty name) (str (:label provider-type))):config config}](llm.provider/validate-config! type config)- (let [{:keys [learned-config] :as listed} (verify-credentials! conn config model)- conn (update conn :config merge learned-config)+ (let [{:keys [connection-info] :as listed} (verify-credentials! conn config model)+ conn (update conn :config merge connection-info)had-usable-model? (metabot-has-a-usable-model?)](llm.provider/set-connections! (conj (llm.provider/stored-connections) conn))(when-not had-usable-model?;; a type with no default model — vLLM, which serves whatever the operator loaded — starts on the model;; the probe exercised, so connecting one leaves the instance working rather than model-less- (select-model-for-new-connection! conn (or model (:probed-model learned-config))))+ (select-model-for-new-connection! conn (or model (:probed-model connection-info))))(seed-models-cache! conn listed)(connection-response (assoc conn :source :db))))))@@ -486,10 +486,10 @@(llm.provider/assert-base-url-change-authorized! (:type merged) (:config live) effective config(:env-fields live))(llm.provider/validate-config! (:type merged) effective)- (let [{:keys [learned-config] :as listed}+ (let [{:keys [connection-info] :as listed}(verify-credentials! merged effective (or model (selected-model conn-key)))- merged (update merged :config merge learned-config)- effective (merge effective learned-config)]+ merged (update merged :config merge connection-info)+ effective (merge effective connection-info)](llm.provider/set-connections! (assoc stored idx merged))(follow-edited-connection-model! (assoc merged :config effective) model)(seed-models-cache! (assoc merged :config effective) listed)diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex f49ace975d1..c2b12ce4c0d 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -15,18 +15,8 @@[metabase.api.common :as api][metabase.llm.provider :as llm.provider][metabase.metabot.scope :as scope]- [metabase.metabot.self.azure :as azure]- [metabase.metabot.self.bedrock :as bedrock]- [metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]- [metabase.metabot.self.deepseek :as deepseek]- [metabase.metabot.self.google :as google]- [metabase.metabot.self.mistral :as mistral]- [metabase.metabot.self.moonshot :as moonshot]- [metabase.metabot.self.openai :as openai]- [metabase.metabot.self.openrouter :as openrouter]- [metabase.metabot.self.vllm :as vllm]- [metabase.metabot.self.zai :as zai]+ [metabase.metabot.self.registry :as registry][metabase.metabot.settings :as metabot.settings][metabase.metabot.usage :as usage][metabase.util :as u]@@ -36,50 +26,16 @@(set! *warn-on-reflection* true)-(defn- resolve-adapter [provider]- ;; a `case` inside of function instead of a map so that with-redefs work well- (case provider- "anthropic" claude/claude- "azure" azure/azure- "bedrock" bedrock/bedrock- "deepseek" deepseek/deepseek- "google" google/google- "mistral" mistral/mistral- "moonshot" moonshot/moonshot- "openai" openai/openai- "openrouter" openrouter/openrouter- "vllm" vllm/vllm- "zai" zai/zai- (throw (ex-info (str "Unknown LLM provider: " provider)- {:provider provider}))))--(defn- resolve-model-lister [provider]- ;; a `case` inside of function instead of a map so that with-redefs work well- (case provider- "anthropic" claude/list-models- "azure" azure/list-models- "bedrock" bedrock/list-models- "deepseek" deepseek/list-models- "google" google/list-models- "mistral" mistral/list-models- "moonshot" moonshot/list-models- "openai" openai/list-models- "openrouter" openrouter/list-models- "vllm" vllm/list-models- "zai" zai/list-models- (throw (ex-info (str "Unknown LLM provider: " provider)- {:provider provider}))))-(defn- normalize-known-model- "Coerce one adapter's `supported-models` value into `{:display-name ... :context-window ...}`. Most adapters store a- map already; DeepSeek stores the display name on its own. Anything else throws, so an adapter that invents a third- shape fails loudly instead of quietly documenting a model with no name."+ "Check one adapter's `supported-models` value carries a `:display-name` (and optionally a+ `:context-window`). Anything else throws, so an adapter that invents a different shape fails loudly+ instead of quietly documenting a model with no name — [[metabase.cmd.ai-provider-dox]] falls back to the+ model id in the Model column, which reads as a name rather than as a gap."[provider model-id value]- (cond- (map? value) value- (string? value) {:display-name value}- :else (throw (ex-info (str "Unrecognized supported-models entry for " provider)- {:provider provider :model model-id :value value}))))+ (if (:display-name value)+ value+ (throw (ex-info (str "Unrecognized supported-models entry for " provider)+ {:provider provider :model model-id :value value}))))(defn known-models"The models `provider`'s adapter is willing to offer, as `{model-id {:display-name ... :context-window ...}}`.@@ -89,20 +45,7 @@no allow-list: `azure`, whose model is the deployment name the admin gives it, `vllm`, which serves whatever theoperator loaded, and `google` and `metabase`, whose catalogs are fixed in [[metabase.llm.provider]] instead."[provider]- ;; a `case` like [[resolve-adapter]], so a new adapter that forgets to register here throws rather than reading as- ;; a provider that simply has no models- (when-let [models (case provider- "anthropic" claude/supported-models- "bedrock" bedrock/supported-models- "deepseek" deepseek/supported-models- "mistral" mistral/supported-models- "moonshot" moonshot/supported-models- "openai" openai/supported-models- "openrouter" openrouter/supported-models- "zai" zai/supported-models- ("azure" "google" "metabase" "vllm") nil- (throw (ex-info (str "Unknown LLM provider: " provider)- {:provider provider})))]+ (when-let [models (registry/optional provider :supported-models)](into {}(map (fn [[model-id value]] [model-id (normalize-known-model provider model-id value)]))models)))@@ -121,25 +64,11 @@:error-code :llm-not-configured:model-ref s})))]{:provider type- :stream-fn (resolve-adapter type)+ :stream-fn (registry/required type :stream):model model:credentials credentials:ai-proxy? ai-proxy?}))-(defn- resolve-context-window-fn [provider]- ;; a `case` inside of function instead of a map so that with-redefs work well- (case provider- "anthropic" claude/context-window-tokens- "azure" azure/context-window-tokens- "bedrock" bedrock/context-window-tokens- "google" google/context-window-tokens- "mistral" mistral/context-window-tokens- "moonshot" moonshot/context-window-tokens- "openai" openai/context-window-tokens- "openrouter" openrouter/context-window-tokens- "zai" zai/context-window-tokens- nil))-(defn context-window-tokens"Input context window (tokens) for a `connection-key/model` string, or nil when theconnection, provider, or model isn't one we know.@@ -150,19 +79,16 @@128,000 output), and the shared context window for providers whose output countsagainst the window itself (Anthropic et al.)."[model-ref]- (let [{:keys [type model]} (llm.provider/resolve-model-ref model-ref)- window-fn (resolve-context-window-fn type)]- (when (and window-fn model)- (window-fn model))))+ (registry/context-window-tokens model-ref))(defn list-models"List available models for a provider using its configured credentials, or `:credentials` in `opts`.The shape of the credentials map varies by provider: API-key providers take `{:api-key ...}`, while Bedrock takes- optional AWS key material and region (see [[bedrock/list-models]])."+ optional AWS key material and region (see [[metabase.metabot.self.bedrock/list-models]])."([provider]- ((resolve-model-lister provider)))+ ((registry/required provider :list-models)))([provider opts]- ((resolve-model-lister provider) opts)))+ ((registry/required provider :list-models) opts)));;; General LLM calling;; Matches the Python ai-service retry behavior:diff --git a/src/metabase/metabot/self/adapter.clj b/src/metabase/metabot/self/adapter.cljnew file mode 100644index 00000000000..9f92d1e0b68--- /dev/null+++ b/src/metabase/metabot/self/adapter.clj@@ -0,0 +1,378 @@+(ns metabase.metabot.self.adapter+ "Shared scaffolding for the LLM provider adapters in `metabase.metabot.self.*`.++ Every adapter does the same handful of things around whatever is genuinely provider-specific+ (request bodies, stream translation, capability quirks): it names itself in errors, refuses the+ Metabase Cloud AI proxy unless it can serve one, fetches a model catalog and intersects it with an+ allow-list, and opens a streaming request wrapped in a span, a debug capture, and provider-friendly+ error translation. That scaffolding lives here so an adapter is only its differences.++ Adapters start from a [[provider]] descriptor and pass it to the helpers below."+ (:require+ [metabase.metabot.self.core :as core]+ [metabase.metabot.self.debug :as debug]+ [metabase.metabot.self.openai.chat-completions :as chat-completions]+ [metabase.util.i18n :refer [tru]]+ [metabase.util.json :as json]+ [metabase.util.log :as log]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [metabase.util.o11y :refer [with-span]]))++(set! *warn-on-reflection* true)++;;; -------------------------------------------------- Schemas ---------------------------------------------------++(def Auth+ "What [[core/request]] sends a request with. A server that takes no key at all (vLLM started without+ `--api-key`) carries no headers."+ [:map {:closed true}+ [:url {:optional true} [:maybe :string]]+ [:headers {:optional true} [:maybe [:map-of :string :string]]]+ [:network-policy-floor {:optional true} [:maybe core/NetworkPolicyFloor]]])++(def Request+ "One HTTP request to a provider, as [[request!]] performs it and a descriptor's `:auth` sees it.+ `:credentials` and `:ai-proxy?` come from the caller — the latter is the caller asking to be proxied,+ which [[reject-ai-proxy!]] grants only for a provider whose descriptor says `:supports-ai-proxy?`. The+ rest describe the wire. `:body` is already encoded, so a provider that signs over it can."+ [:map {:closed true}+ [:method :keyword]+ [:path :string]+ [:credentials {:optional true} [:maybe core/LLMCredentials]]+ [:ai-proxy? {:optional true} [:maybe :boolean]]+ [:as {:optional true} [:maybe :keyword]]+ [:headers {:optional true} [:maybe [:map-of :string :string]]]+ [:body {:optional true} [:maybe :string]]])++(def ProviderSpec+ "What an adapter hands [[provider]]; see that fn for what each key means."+ [:map {:closed true}+ [:slug :string]+ [:display-name :string]+ [:errors {:optional true} [:maybe [:map-of :int fn?]]]+ [:error-fallback {:optional true} [:maybe fn?]]+ [:headers {:optional true} [:maybe [:map-of :string :string]]]+ [:auth {:optional true} [:maybe fn?]]+ [:supports-ai-proxy? {:optional true} [:maybe :boolean]]])++(def Provider+ "A built descriptor: a [[ProviderSpec]] with `:auth` defaulted and `:span` and `:error-msg` derived.+ Every helper here takes one as its first argument."+ [:merge ProviderSpec+ [:map {:closed true}+ [:auth fn?]+ [:span :keyword]+ [:error-msg fn?]]])++(def SupportedModels+ "An adapter's allow-list of the models it offers in the picker, keyed by model id. A provider that+ publishes no context window for a model (DeepSeek) records only the display name."+ [:map-of :string [:map {:closed false, ::mr/deliberately-open true+ :description "an allow-list entry; providers add their own flags"}+ [:display-name :string]+ [:context-window {:optional true} [:maybe :int]]]])++(def ResolvedRef+ "What [[metabase.llm.provider/resolve-model-ref]] turns a `connection-key/model` string into.++ Lives here rather than in the registry: the registry requires the adapters, not the other way round, so+ this is where both ends can name it."+ [:map {:closed true}+ [:connection-key {:optional true} [:maybe :string]]+ [:type {:optional true} [:maybe :string]]+ [:model {:optional true} [:maybe :string]]+ [:credentials {:optional true} [:maybe core/LLMCredentials]]+ [:ai-proxy? {:optional true} [:maybe :boolean]]])++(def ListOpts+ "What a `list-models` call carries. Not [[core/LLMRequestOpts]]: a listing is not a generation, and the+ connect path adds `:proposed-model` and `:probe?` — the model it believes the connection serves, and+ permission to spend a probe verifying it (see [[metabase.llm.api.provider]])."+ [:map {:closed true}+ [:credentials {:optional true} [:maybe core/LLMCredentials]]+ [:ai-proxy? {:optional true} [:maybe :boolean]]+ [:model {:optional true} [:maybe :string]]+ [:proposed-model {:optional true} [:maybe :string]]+ [:probe? {:optional true} [:maybe :boolean]]])++(def CatalogEntry+ "One row of a provider's model catalog, as the provider sends it. Open: every provider adds its own+ fields, and an adapter reads only `:id` and whichever name key its catalog uses."+ [:map {::mr/deliberately-open true+ :description "a provider catalog entry"}+ [:id {:optional true} [:maybe :string]]])++(def ModelListing+ "The model-listing response the admin picker consumes, plus what a connect-time probe learned about the+ connection for the connect path to store on it."+ [:map {:closed true}+ [:models [:sequential [:map {:closed true}+ [:id :string]+ [:display_name [:maybe :string]]]]]+ [:connection-info {:optional true} [:map {:closed true}+ [:model-reasoning {:optional true} :string]+ [:probed-model {:optional true} :string]]]])++(def StreamOpts+ "How an adapter puts one request on the wire; see [[stream!]]."+ [:map {:closed true}+ [:path [:or :string fn?]]+ [:body [:map {::mr/deliberately-open true+ :description "a provider's composed request body"}]]+ [:headers {:optional true} [:maybe [:map-of :string :string]]]+ [:request-options {:optional true} [:maybe [:map {::mr/deliberately-open true+ :description "extra clj-http request options"}]]]+ [:span-attrs {:optional true} [:maybe [:map {::mr/deliberately-open true+ :description "extra span attributes"}]]]+ [:error-msg {:optional true} [:maybe fn?]]+ [:wrap-stream {:optional true} [:maybe fn?]]+ [:on-request-error {:optional true} [:maybe fn?]]])++;;; ------------------------------------------------- Descriptor -------------------------------------------------++(defn- status-error-msg-fn+ "Build the `res->message` callback [[core/rethrow-api-error!]] and [[core/reducible-with-api-errors]] take.++ `errors` maps an HTTP status to a thunk returning that status's user-facing message; a status with no+ entry falls back to `fallback`, a fn of the status. The messages stay thunks so each one is rendered in+ the caller's locale at throw time, the way an inline `tru` would be.++ `:error-fallback` is here to keep old translations working until the new one is properly translated"+ [display-name errors fallback]+ (fn [res]+ (let [status (long (:status res 0))]+ (if-let [msg (get errors status)]+ (msg)+ (if fallback+ (fallback status)+ (tru "{0} API error (HTTP {1})" display-name status))))))++;;; --------------------------------------------------- Auth -----------------------------------------------------++(mu/defn reject-ai-proxy! :- :nil+ "Throw when a caller asks for a proxied request to a provider the proxy cannot serve.++ [[request!]] applies this, so the check lands on every request to a provider — an adapter's own+ listing and probe paths included, since those go through [[request!]] too. An adapter has to call it+ itself only on a path that can answer without reaching the provider, which would otherwise accept a+ proxied call by making no request to refuse."+ [{:keys [display-name supports-ai-proxy?]} :- Provider+ requested-proxy? :- [:maybe :boolean]]+ (when (and requested-proxy? (not supports-ai-proxy?))+ (throw (ex-info (tru "AI proxy is not supported for {0}" display-name)+ {:api-error true+ :error-code :proxy-unsupported}))))++(mu/defn bearer-auth :- Auth+ "The default `:auth`: carry the connection's API key as `Authorization: Bearer`. Nothing about the+ request itself matters, which is true of every provider that authenticates per connection.++ A blank key passes no auth map at all, so [[core/resolve-auth]] raises the provider's own missing-key+ error rather than sending an unauthenticated request."+ [{:keys [slug display-name]} :- Provider+ {:keys [credentials ai-proxy?]} :- Request]+ (core/resolve-auth slug display-name+ (when-let [k (not-empty (:api-key credentials))]+ {:url (:base-url credentials)+ :headers {"Authorization" (str "Bearer " k)}})+ ai-proxy?))++(mu/defn request!+ "Perform one HTTP request to `p` and return the response.++ The single door every provider request goes through, so what is true of all of them lives here rather+ than in each adapter: a proxied request `p` cannot serve is refused, and the descriptor's+ `:auth` authenticates whatever is left. That the `:auth` fn runs per request is deliberate: it keeps+ the door single, and an adapter that finds resolution expensive caches it rather than hoisting it out+ — the one that does, parsing a Google service-account key, memoizes in the adapter.++ A request with a `:body` is sent as JSON unless something overrides the header.++ `req` carries the caller's `:credentials` and `:ai-proxy?` alongside the wire details (`:method`,+ `:path`, `:as`, `:headers`, and an already-encoded `:body`); `extra` is merged into the+ [[core/request]] opts, for per-provider timeouts and the like."+ ([p :- Provider+ req :- Request]+ (request! p req nil))+ ([{:keys [auth] :as p} :- Provider+ {:keys [method path body as headers ai-proxy?] :as req} :- Request+ extra :- [:maybe [:map {::mr/deliberately-open true+ :description "extra clj-http request options"}]]]+ (reject-ai-proxy! p ai-proxy?)+ (core/request (auth p req)+ (merge (cond-> {:method method+ :url path+ :headers (merge (when body {"Content-Type" "application/json"})+ (:headers p)+ headers)}+ as (assoc :as as)+ body (assoc :body body))+ extra))))++(mu/defn provider :- Provider+ "Build the descriptor the helpers in this namespace take as their first argument.++ :slug - the `llm-providers` type string. Tags errors and debug logs, and names the+ request span `:metabot.{slug}/request`.+ :display-name - the human name spliced into user-facing messages.+ :errors - HTTP status -> thunk returning that status's message (see+ [[status-error-msg-fn]]).+ :error-fallback - fn of the status, for a status `:errors` does not name. Each adapter keeps its+ own for now for the sake of i18n backward compatibility.+ :headers - headers every request to this provider carries (e.g. an API version).+ :auth - how this provider authenticates one request: a fn of the descriptor and the+ request (`:credentials`, `:ai-proxy?`, `:method`, `:path`, and the encoded+ `:body`), returning the `{:url ... :headers ...}` [[core/request]] takes.+ Defaults to [[bearer-auth]]. Everything provider-specific about+ authenticating lives here — the header the key travels in, the validation its+ credentials need, or a signature over the request itself.+ :supports-ai-proxy? - whether the Metabase Cloud AI proxy can serve this provider. Defaults to+ false, which makes [[request!]] reject a request that asked for the proxy.+ Distinct from a request's own `:ai-proxy?`, which is a caller asking for it."+ [{:keys [slug display-name errors error-fallback auth] :as descriptor} :- ProviderSpec]+ (assoc descriptor+ :error-msg (status-error-msg-fn display-name errors error-fallback)+ :auth (or auth bearer-auth)+ :span (keyword (str "metabot." slug) "request")))++(mu/defn rethrow!+ "Rethrow a provider HTTP exception with `p`'s own user-facing message. See [[core/rethrow-api-error!]]."+ [{:keys [slug error-msg]} :- Provider+ e :- [:fn #(instance? Throwable %)]]+ (core/rethrow-api-error! slug error-msg e))++;;; ------------------------------------------------ Model catalog -----------------------------------------------++(mu/defn fetch-catalog :- [:maybe [:sequential :map]]+ "Fetch a provider's OpenAI-style model catalog and return its entries.++ This doubles as the credential round trip behind the admin Connect button for every provider whose+ catalog endpoint answers one, so failures are translated with the provider's own messages.++ Entries come from [[chat-completions/models-catalog]], which fails closed: a 2xx whose body is not a+ recognizable catalog throws rather than yielding no entries, which would leave the admin an empty model+ picker and a Connect button that succeeded against a provider we never actually reached. A well-formed+ but empty catalog is a legitimate answer and passes.++ `opts` is the caller's request; `path` is the catalog endpoint relative to the base URL, for a provider+ that does not serve one at `/models`. The descriptor's own `:headers` ride along either way."+ ([p :- Provider+ opts :- ListOpts]+ (fetch-catalog p opts "/models"))+ ([{:keys [display-name] :as p} :- Provider+ {:keys [credentials ai-proxy?]} :- ListOpts+ path :- :string]+ (try+ (let [res (request! p {:credentials credentials+ :ai-proxy? ai-proxy?+ :method :get+ :path path+ :as :json})]+ (chat-completions/models-catalog display-name res))+ (catch Exception e+ (rethrow! p e)))))++(mu/defn model-listing :- ModelListing+ "Shape a provider's catalog `entries` into the `{:models [{:id ... :display_name ...}]}` listing response.++ Keeps only the entries `supported-models` allows and sorts by id, so the admin picker is stable across+ catalog reorderings.++ `catalog-name-key` is the field this provider's catalog carries a model's own name in — `:name` for+ OpenRouter and Z.AI, `:display_name` for Anthropic. Each provider names its own, rather than the+ shared code guessing between them: a provider whose catalog grew a second name field would otherwise+ start rendering a different one. Omit it for a catalog that carries no name and the allow-list's name+ is used, which is also what happens when an entry is missing the field."+ ([supported-models :- SupportedModels+ entries :- [:maybe [:sequential CatalogEntry]]]+ (model-listing supported-models entries nil))+ ([supported-models :- SupportedModels+ entries :- [:maybe [:sequential CatalogEntry]]+ catalog-name-key :- [:maybe :keyword]]+ {:models (->> entries+ (filter (comp supported-models :id))+ (sort-by :id)+ (mapv (fn [{:keys [id] :as entry}]+ {:id id+ :display_name (or (get entry catalog-name-key)+ (get-in supported-models [id :display-name]))})))}))++;;; ------------------------------------------------- Streaming --------------------------------------------------++(mu/defn stream!+ "Open a provider's streaming request and return a reducible over its raw SSE events.++ Wraps the exchange in what every adapter needs around it: the request span, the opt-in debug capture of+ the composed request and its response, and translation of failures into the provider's own messages —+ both at request time and mid-stream, since the SSE body is consumed lazily, long after this returns.++ `opts` is the caller's request — its `:model` names the span and the debug log, its `:credentials`+ and `:ai-proxy?` are what the descriptor's `:auth` authenticates from, and its `:input` and `:tools`+ are what the span counts.++ The third argument says how this adapter puts that request on the wire:++ :path - the streaming endpoint, relative to the base URL, or a thunk returning it. A+ thunk is called inside the span and behind the proxy refusal, for the one+ provider whose path is derived from credentials that can fail to resolve+ (Google) — so that failure lands on the trace, and loses to the refusal.+ :body - the composed request body. Encoded here.+ :headers - extra request headers, beyond the descriptor's own and the `Content-Type`+ [[request!]] adds for a request with a body.+ :request-options - extra [[core/request]] opts, e.g. per-provider timeouts.+ :span-attrs - extra keys merged into the `with-span` map. They reach its log line; clj-otel+ drops them from the trace itself, along with the model and the counts, until+ BOT-2168 fixes the wrapper.+ :error-msg - replaces the descriptor's own `res->message`, for a provider whose message+ depends on the connection rather than only on the response. Applies to both+ phases below.+ :wrap-stream - applied to the reducible before error translation, for an adapter with its own+ translation to do first.+ :on-request-error - replaces the default [[rethrow!]] catch, for an adapter that retries. Only+ request-time failures reach it: the body is consumed long after this returns,+ so a mid-stream failure is translated by [[core/reducible-with-api-errors]]+ instead, which no adapter overrides."+ [{:keys [slug display-name span] :as p} :- Provider+ {:keys [model input tools credentials ai-proxy?]} :- core/LLMRequestOpts+ {:keys [path body headers request-options span-attrs wrap-stream on-request-error error-msg]+ :or {wrap-stream identity}} :- StreamOpts]+ (let [msg-count (count input)+ tool-count (count tools)+ res->msg (or error-msg (:error-msg p))]+ (log/debug (str display-name " request") {:model model :msg-count msg-count :tools tool-count})+ ;; flat keys, which is what `u.o11y/with-span` renders into its log line. clj-otel reads span+ ;; attributes only from `:attributes` and drops every other key, so these reach the log and not the+ ;; trace.+ (with-span :info (merge {:name span+ :model model+ :msg-count msg-count+ :tool-count tool-count}+ span-attrs)+ (try+ ;; ahead of `path`, which a provider may derive from credentials that can fail to resolve: a proxied+ ;; request should say the proxy is unsupported, not report whatever is missing from a connection it+ ;; will never use. [[request!]] checks again, for callers that do not come through here.+ (reject-ai-proxy! p ai-proxy?)+ (let [path (if (fn? path) (path) path)]+ (-> (request! p {:credentials credentials+ :ai-proxy? ai-proxy?+ :method :post+ :path path+ :as :stream+ :headers headers+ ;; encoded up front, since a provider may sign over the body+ :body (json/encode body)}+ request-options)+ :body+ core/sse-reducible+ (debug/capture-stream {:provider slug+ :model model+ :url path+ :request body})+ wrap-stream+ (core/reducible-with-api-errors slug res->msg)))+ (catch Exception e+ (if on-request-error+ (on-request-error e)+ (core/rethrow-api-error! slug res->msg e)))))))diff --git a/src/metabase/metabot/self/azure.clj b/src/metabase/metabot/self/azure.cljindex 1f1bba32981..284af0c926f 100644--- a/src/metabase/metabot/self/azure.clj+++ b/src/metabase/metabot/self/azure.clj@@ -17,15 +17,13 @@(:require[clojure.string :as str][metabase.llm.provider :as llm.provider]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai :as openai][metabase.util :as u][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)@@ -69,6 +67,11 @@:openai (openai/reasoning-model? (model->deployment model))nil false))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Azure answers from the deployment name, delegating to the family's adapter."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(def ^:private model-context-windows"Input context windows for the models Azure sells, keyed by model id.GPT values are max input tokens (Microsoft's listed windows are input + output totals):@@ -96,12 +99,12 @@"gpt-5.4-nano" 272000"gpt-5.4" 922000})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for a `{family}/{deployment}` model string. Deployment namesdefault to the model id at deploy time, so the longest model id prefixing the deploymentname decides (tolerating date/custom suffixes like `gpt-5.4-2026-03-05`); nil when thedeployment name matches no known model."- [model]+ [model :- [:maybe :string]](let [deployment (u/lower-case-en (str (model->deployment model)))](some->> (keys model-context-windows)(filter #(str/starts-with? deployment %))@@ -117,11 +120,6 @@:error-code :api-key-missing:status-code 403}))-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for Azure")- {:api-error true- :error-code :proxy-unsupported}))-(defn- ensure-credentials"Validate the credentials of the connection serving this request.Throws when the API key or base URL is missing."@@ -130,40 +128,25 @@(throw (missing-credentials-ex)))credentials)-(defn- azure-error-msg- "Canonical, status-specific Azure error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "Azure rejected the API key for this resource")- 403 (tru "Azure API key lacks permission for this resource or deployment")- 404 (tru "Azure API endpoint or deployment was not found — check the base URL and deployment name")- 429 (tru "Azure has rate limited us")- 500 (tru "Azure is not working but not saying why")- (tru "Azure API error (HTTP {0})" status))))--(defn- azure-request- "Perform an HTTP request against the Azure resource's compatible surface.- `credentials` is the `{:api-key ... :base-url ...}` map of the connection serving this request.- `headers` are extra headers (e.g. `anthropic-version`).- `ai-proxy?` is accepted for parity with the other provider adapters but is not supported:- throws when true. Auth is resolved through [[core/resolve-auth]]/[[core/request]] so proxy- redirection is already wired up should Azure proxying ever be supported."- [{:keys [method path body as headers credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [{:keys [api-key base-url]} (ensure-credentials credentials)- auth (core/resolve-auth "azure" "Azure"- {:url base-url- :headers {"Authorization" (str "Bearer " api-key)}}- ai-proxy?)]- (core/request auth- (cond-> {:method method- :url path- :headers headers}- as (assoc :as as)- body (-> (assoc :body body)- (assoc-in [:headers "Content-Type"] "application/json"))))))+(defn- azure-auth+ "Azure's `:auth`. The scheme is the default [[adapter/bearer-auth]]; the only difference is that Azure+ validates the whole credential pair up front, so a half-configured connection fails with its own+ message instead of a 401 from Azure."+ [p {:keys [credentials] :as req}]+ (ensure-credentials credentials)+ (adapter/bearer-auth p req))++(def ^:private provider+ (adapter/provider+ {:slug "azure"+ :display-name "Azure"+ :auth azure-auth+ :error-fallback #(tru "Azure API error (HTTP {0})" %)+ :errors {401 #(tru "Azure rejected the API key for this resource")+ 403 #(tru "Azure API key lacks permission for this resource or deployment")+ 404 #(tru "Azure API endpoint or deployment was not found — check the base URL and deployment name")+ 429 #(tru "Azure has rate limited us")+ 500 #(tru "Azure is not working but not saying why")}}));;; ---------------------------------------------- Connect validation -------------------------------------------@@ -171,7 +154,11 @@"Round-trip the `/openai` surface: `GET /v1/models` succeeds (with the regional catalog,which we discard) iff the key and base URL reach an authenticated OpenAI-compatible surface."[credentials ai-proxy?]- (azure-request {:method :get :path "/v1/models" :as :json :credentials credentials :ai-proxy? ai-proxy?}))+ (adapter/request! provider {:method :get+ :path "/v1/models"+ :as :json+ :credentials credentials+ :ai-proxy? ai-proxy?}))(defn- validate-anthropic-surface!"Round-trip the `/anthropic` surface, which exposes no GET routes (they 404 with@@ -181,17 +168,17 @@invoking a model."[credentials ai-proxy?](try- (azure-request {:method :post- :path "/v1/messages"- :body "{}"- :headers {"anthropic-version" anthropic-version}- :credentials credentials- :ai-proxy? ai-proxy?})+ (adapter/request! provider {:method :post+ :path "/v1/messages"+ :body "{}"+ :headers {"anthropic-version" anthropic-version}+ :credentials credentials+ :ai-proxy? ai-proxy?})(catch Exception e(when-not (= 400 (:status (ex-data e)))(throw e)))))-(defn list-models+(mu/defn list-models :- adapter/ModelListing"Validate Azure credentials with a model-free round trip and return an empty model list.There is never a dropdown to populate (Azure's listing returns the regional catalog, not the@@ -201,17 +188,22 @@chat time with `DeploymentNotFound`.Opts: `:credentials` (`{:api-key ... :base-url ...}`), `:model` (the `{family}/{deployment}`- string selecting which surface family to validate; without it validation is skipped), and- `:ai-proxy?`, which is not supported for Azure and throws when true."+ string selecting which surface family to validate) and `:ai-proxy?`, which is not supported for+ Azure and throws when true.++ With no model there is no family to pick a surface for, so the round trip is skipped. The caller+ supplies one: `metabase.llm.api.provider` resolves it from the connection's own `:model-fields`, and+ falls back to what `llm-metabot-provider` names for *that* connection."([] (list-models {}))- ([{:keys [credentials model ai-proxy?]}]+ ([{:keys [credentials model ai-proxy?]} :- adapter/ListOpts]+ (adapter/reject-ai-proxy! provider ai-proxy?)(when-let [model (not-empty model)](try(case (model->family model):anthropic (validate-anthropic-surface! credentials ai-proxy?):openai (validate-openai-surface! credentials ai-proxy?))(catch Exception e- (core/rethrow-api-error! "azure" azure-error-msg e))))+ (adapter/rethrow! provider e)))){:models []}));;; --------------------------------------------------- Streaming -----------------------------------------------@@ -220,40 +212,22 @@"Perform a streaming request to an Azure-hosted model deployment.Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing. `:ai-proxy?` is not supported for Azure and throws when true."- [{:keys [model input tools credentials ai-proxy?] :as opts} :- core/LLMRequestOpts]+ [{:keys [model] :as opts} :- core/LLMRequestOpts](let [family (model->family model)- opts (assoc opts :model (model->deployment model) :fast? false)+ ;; the body names the Azure deployment; the span keeps the `{family}/{deployment}` model it was called with+ deployed (assoc opts :model (model->deployment model) :fast? false){:keys [path headers req]}(case family:anthropic {:path "/v1/messages":headers {"anthropic-version" anthropic-version}- :req (claude/claude-request-body opts)}+ :req (claude/claude-request-body deployed)}:openai {:path "/v1/responses"- :req (openai/openai-request-body opts)})]- (with-span :info {:name :metabot.azure/request- :model model- :family family- :msg-count (count input)- :tool-count (count tools)}- (try- (let [response (azure-request {:method :post- :path path- :as :stream- :headers headers- :body (json/encode req)- :credentials credentials- :ai-proxy? ai-proxy?})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "azure"- :model model- :url path- :request req})- (core/reducible-with-api-errors "azure" azure-error-msg)))- (catch Exception e- (core/rethrow-api-error! "azure" azure-error-msg e))))))+ :req (openai/openai-request-body deployed)})]+ (adapter/stream! provider opts+ {:path path+ :body req+ :headers headers+ :span-attrs {:family (name family)}})))(defn- model->aisdk-chunks-xf"The SSE->AISDK translating transducer for an Azure model string.diff --git a/src/metabase/metabot/self/bedrock.clj b/src/metabase/metabot/self/bedrock.cljindex 9dd4656a1d1..73f23046c46 100644--- a/src/metabase/metabot/self/bedrock.clj+++ b/src/metabase/metabot/self/bedrock.clj@@ -21,16 +21,14 @@[clojure.string :as str][metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai :as openai][metabase.util :as u][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json][metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]])+ [metabase.util.malli :as mu])(:import(java.net URI)(java.util.function Consumer)@@ -166,11 +164,6 @@:error-code :api-key-missing:status-code 403}))-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for AWS Bedrock")- {:api-error true- :error-code :proxy-unsupported}))-(defn- ensure-credentials"Validate the credentials of the connection serving this request.Self-hosted, no access key pair at all is fine, signing falls back to the AWS default credentials chain; on a@@ -193,58 +186,39 @@(u/assoc-dissoc :session-token token)(update :region #(validate-region (or (not-empty %) "us-east-1"))))))-(defn- bedrock-error-msg- "Canonical, status-specific Bedrock error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "AWS Bedrock rejected our credentials or request signature")- 403 (tru "AWS Bedrock credentials lack permission for this model or action")- 404 (tru "AWS Bedrock model or endpoint is unavailable in the configured region")- 429 (tru "AWS Bedrock has rate limited us")- 500 (tru "AWS Bedrock is not working but not saying why")- (tru "AWS Bedrock API error (HTTP {0})" status))))--(defn- bedrock-request- "Perform a SigV4-signed HTTP request against the Bedrock mantle endpoint.- `headers` are extra *unsigned* headers (e.g. `anthropic-version`). `credentials` is the AWS credentials map of- the connection serving this request. `ai-proxy?` is accepted for parity with the other provider adapters but is- not supported: throws when true."- [{:keys [method path body as headers credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))+(defn- bedrock-auth+ "Bedrock's `:auth`. Alone among the providers it authenticates per request rather than per connection:+ SigV4 signs over the method, the full URL and the body, so those arrive with the request rather than+ being fixed by the descriptor. The signed headers travel as the request's auth headers; extra unsigned+ headers (e.g. `anthropic-version`) can still be added freely alongside them.++ `ai-proxy?` is accepted for parity with the other adapters but is not supported."+ [{:keys [slug display-name]} {:keys [credentials ai-proxy? method path body]}](let [{:keys [region] :as creds} (ensure-credentials credentials)base-url (str "https://bedrock-mantle." region ".api.aws")content-type (when body "application/json")sig-headers (signed-headers (merge creds {:method method:url (str base-url path):body body- :content-type content-type}))- auth (core/resolve-auth "bedrock" "AWS Bedrock"- {:url base-url :headers sig-headers}- ai-proxy?)]- (core/request auth- (cond-> {:method method- :url path- :headers headers}- as (assoc :as as)- body (assoc :body body)))))+ :content-type content-type}))]+ (core/resolve-auth slug display-name+ {:url base-url :headers sig-headers}+ ai-proxy?)))++(def ^:private provider+ (adapter/provider+ {:slug "bedrock"+ :auth bedrock-auth+ :display-name "AWS Bedrock"+ :error-fallback #(tru "AWS Bedrock API error (HTTP {0})" %)+ :errors {401 #(tru "AWS Bedrock rejected our credentials or request signature")+ 403 #(tru "AWS Bedrock credentials lack permission for this model or action")+ 404 #(tru "AWS Bedrock model or endpoint is unavailable in the configured region")+ 429 #(tru "AWS Bedrock has rate limited us")+ 500 #(tru "AWS Bedrock is not working but not saying why")}}));;; ------------------------------------------------ Model listing -----------------------------------------------(defn- list-all-models- "Fetch the full mantle model catalog (`GET /v1/models`), every vendor included."- [{:keys [credentials ai-proxy?]}]- (try- (let [res (bedrock-request {:method :get- :path "/v1/models"- :as :json- :credentials credentials- :ai-proxy? ai-proxy?})]- (get-in res [:body :data]))- (catch Exception e- (core/rethrow-api-error! "bedrock" bedrock-error-msg e))))-(def supported-models"Bedrock models offered in the Metabot model picker, keyed by model id.`list-models` returns the intersection of this map with the mantle `/v1/models` catalog.@@ -263,16 +237,11 @@"openai.gpt-5.5" {:display-name "GPT-5.5" :context-window 272000}"openai.gpt-5.5-2026-04-23" {:display-name "GPT-5.5 (2026-04-23)" :context-window 272000}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))-(defn- supported-model?- "Whether a `/v1/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))-(defn- available-model?"Whether a `/v1/models` catalog entry is available.The AWS catalog lists models the account cannot invoke with `:status \"unavailable\"`, e.g. claude-fable-5 when the@@ -280,19 +249,16 @@[{:keys [status]}](= status "available"))-(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the Bedrock models supported by this adapter (see [[supported-models]]).The opts map supports `:credentials`, a map of `:access-key-id`, `:secret-access-key`, `:region`, and (fortemporary credentials) `:session-token`, plus `:ai-proxy?`, which is not supported for Bedrock and throws whentrue. On a self-hosted Metabase with no access key pair, requests are signed with whatever the AWS defaultcredentials chain resolves; Metabase Cloud requires the pair."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter (every-pred supported-model? available-model?))- (sort-by :id)- (mapv (fn [{:keys [id]}]- {:id id :display_name (get-in supported-models [id :display-name])})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models+ (filter available-model? (adapter/fetch-catalog provider opts "/v1/models")))));;; --------------------------------------------- API family dispatch -------------------------------------------@@ -334,6 +300,11 @@:openai falsenil false))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Bedrock answers from the model id, delegating to the family's adapter."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(defn ->mantle-anthropic-body"Adapt a canonical Anthropic Messages request body for the mantle endpoint.@@ -348,7 +319,7 @@`:region`, and (for temporary credentials) `:session-token`. On a self-hosted Metabase with no access key pair,requests are signed with whatever the AWS default credentials chain resolves; Metabase Cloud requires the pair.`:ai-proxy?` is not supported for Bedrock and throws when true."- [{:keys [model input tools credentials ai-proxy?] :as opts+ [{:keys [model] :as opts:or {model default-model}} :- core/LLMRequestOpts](let [opts (assoc opts :model model :fast? false)family (model->family model)@@ -359,30 +330,11 @@:req (->mantle-anthropic-body (claude/claude-request-body opts))}:openai {:path "/openai/v1/responses":req (openai/openai-request-body opts)})]- (with-span :info {:name :metabot.bedrock/request- :model model- :family family- :msg-count (count input)- :tool-count (count tools)}- (try- (let [response (bedrock-request {:method :post- :path path- :as :stream- :headers headers- :body (json/encode req)- :credentials credentials- :ai-proxy? ai-proxy?})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "bedrock"- :model model- :url path- :request req})- (core/reducible-with-api-errors "bedrock" bedrock-error-msg)))- (catch Exception e- (core/rethrow-api-error! "bedrock" bedrock-error-msg e))))))+ (adapter/stream! provider opts+ {:path path+ :body req+ :headers headers+ :span-attrs {:family (name family)}})))(defn- model->aisdk-chunks-xf"The SSE->AISDK translating transducer for a Bedrock model id.diff --git a/src/metabase/metabot/self/catalog.clj b/src/metabase/metabot/self/catalog.cljdeleted file mode 100644index a49e140f5a9..00000000000--- a/src/metabase/metabot/self/catalog.clj+++ /dev/null@@ -1,49 +0,0 @@-(ns metabase.metabot.self.catalog- "Provider capability dispatch: given a model reference, which optional capabilities the provider- and model behind it serve."- (:require- [metabase.llm.provider :as llm.provider]- [metabase.metabot.self.azure :as azure]- [metabase.metabot.self.bedrock :as bedrock]- [metabase.metabot.self.claude :as claude]- [metabase.metabot.self.deepseek :as deepseek]- [metabase.metabot.self.google :as google]- [metabase.metabot.self.mistral :as mistral]- [metabase.metabot.self.moonshot :as moonshot]- [metabase.metabot.self.openai :as openai]- [metabase.metabot.self.openrouter :as openrouter]- [metabase.metabot.self.vllm :as vllm]- [metabase.metabot.self.zai :as zai]))--(set! *warn-on-reflection* true)--(defn streams-reasoning?- "Whether a model reference names a model that streams its reasoning back to us.-- Anthropic, OpenAI, DeepSeek, Z.AI, OpenRouter, Google, Mistral, Moonshot, and — delegating per API family —- Bedrock and Azure answer from the model name, because thinking is requested in the request body. vLLM answers- from what its connect-time probe observed and recorded on the connection — the flag depends on the operator's- `--reasoning-parser` as well as on the model, so the name cannot settle it. Unknown provider types answer false."- [model-ref]- (let [{:keys [type model credentials]} (llm.provider/resolve-model-ref model-ref)]- (case type- "anthropic" (claude/reasoning-model? model)- "azure" (azure/reasoning-model? model)- "bedrock" (bedrock/reasoning-model? model)- "deepseek" (deepseek/reasoning-model? model)- "google" (google/reasoning-model? model)- "mistral" (mistral/reasoning-model? model)- "moonshot" (moonshot/reasoning-model? model)- "openai" (openai/reasoning-model? model)- "openrouter" (openrouter/reasoning-model? model)- "vllm" (vllm/reasoning-connection? credentials)- "zai" (zai/reasoning-model? model)- false)))--(defn supports-fast-mode?- "Whether a model reference names a model we can serve in Anthropic fast mode."- [model-ref]- (let [{:keys [type model ai-proxy?]} (llm.provider/resolve-model-ref model-ref)]- (case type- "anthropic" (claude/fast-mode-model? model ai-proxy?)- false)))diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.cljindex 53ec830e01f..7509b746545 100644--- a/src/metabase/metabot/self/claude.clj+++ b/src/metabase/metabot/self/claude.clj@@ -1,18 +1,24 @@(ns metabase.metabot.self.claude(:require[clojure.string :as str]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.schema :as schema][metabase.util :as u][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json][metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)+(def ^:private default-model "claude-haiku-4-5")++(def ^:private anthropic-version "2023-06-01")++(def ^:private fast-mode-beta+ "The beta header opting a request into Anthropic fast mode."+ "fast-mode-2026-02-01")+(defn- claude-usage->aisdk-usage"Convert an Anthropic `usage` block into the AISDK `:usage` shape.@@ -310,19 +316,33 @@{:type "text":text suffix}])))-(defn- anthropic-error-msg- "Canonical, status-specific Anthropic error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "Anthropic API key expired or invalid")- 403 (tru "Anthropic API key has insufficient permissions")- 404 (tru "Anthropic API endpoint is unavailable or the model was not found")- 413 (tru "Anthropic API rejected our request because it was too large")- 429 (tru "Anthropic API has rate limited us")- 500 (tru "Anthropic API is not working but not saying why")- 529 (tru "Anthropic API is overloaded and is asking us to wait")- (tru "Anthropic API error (HTTP {0})" status))))+(defn- anthropic-auth+ "Anthropic's `:auth`. Identical to [[adapter/bearer-auth]] except that the key travels bare in+ `x-api-key` rather than as a bearer token."+ [{:keys [slug display-name]} {:keys [credentials ai-proxy?]}]+ (core/resolve-auth slug display-name+ (when-let [k (not-empty (:api-key credentials))]+ {:url (:base-url credentials)+ :headers {"x-api-key" k}})+ ai-proxy?))++(def ^:private provider+ "Anthropic is the one provider the Metabase Cloud AI proxy can serve, so `:supports-ai-proxy?` is true+ here and a proxied request goes through rather than being rejected."+ (adapter/provider+ {:slug "anthropic"+ :display-name "Anthropic"+ :supports-ai-proxy? true+ :auth anthropic-auth+ :headers {"anthropic-version" anthropic-version}+ :error-fallback #(tru "Anthropic API error (HTTP {0})" %)+ :errors {401 #(tru "Anthropic API key expired or invalid")+ 403 #(tru "Anthropic API key has insufficient permissions")+ 404 #(tru "Anthropic API endpoint is unavailable or the model was not found")+ 413 #(tru "Anthropic API rejected our request because it was too large")+ 429 #(tru "Anthropic API has rate limited us")+ 500 #(tru "Anthropic API is not working but not saying why")+ 529 #(tru "Anthropic API is overloaded and is asking us to wait")}}))(def supported-models"Anthropic chat models offered in the Metabot model picker, keyed by model id.@@ -343,40 +363,16 @@"`max_tokens` for an unresolved model — low enough to be safe on any of them."64000)-(defn- supported-model?- "Whether a `/v1/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))--(defn- list-all-models- "Fetch the full Anthropic model catalog (`GET /v1/models`).- Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,- and throws when they are missing. Also supports `:ai-proxy?`."- [{:keys [credentials ai-proxy?]}]- (try- (let [auth (core/resolve-auth "anthropic" "Anthropic"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"x-api-key" k}})- ai-proxy?)- res (core/request auth {:method :get- :url "/v1/models"- :headers {"anthropic-version" "2023-06-01"}})]- (:data (json/decode+kw (:body res))))- (catch Exception e- (core/rethrow-api-error! "anthropic" anthropic-error-msg e))))--(defn list-models- "List the Anthropic chat models supported by this adapter (see [[supported-models]]).+(mu/defn list-models :- adapter/ModelListing+ "List the Anthropic chat models supported by this adapter, by intersecting [[supported-models]] with the+ account's `/v1/models` catalog.Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,and throws when they are missing. Also supports `:ai-proxy?`."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id display_name]}]- {:id id :display_name (or display_name (get-in supported-models [id :display-name]))})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models+ (adapter/fetch-catalog provider opts "/v1/models")+ :display_name)))(defn- strip-vendor-prefix"`model` lowercased and without an optional vendor prefix (e.g. Bedrock's `anthropic.`).@@ -390,9 +386,9 @@[model](get-in supported-models [(strip-vendor-prefix model) :max-tokens]))-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [(strip-vendor-prefix model) :context-window]))(defn- claude-model-version@@ -435,6 +431,11 @@[model](some? (model-thinking-config model)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Anthropic answers from the model name: thinking is requested in the request body."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(def ^:private fast-mode-models"The models Anthropic documents fast mode for: https://code.claude.com/docs/en/fast-mode"#{"claude-opus-4-8" "claude-opus-5"})@@ -446,6 +447,11 @@(and (not ai-proxy?)(contains? fast-mode-models (strip-vendor-prefix model))))+(mu/defn supports-fast-mode? :- :boolean+ "Registry capability. Fast mode depends on the model and on whether the call is proxied."+ [{:keys [model ai-proxy?]} :- adapter/ResolvedRef]+ (fast-mode-model? model ai-proxy?))+(mu/defn claude-request-body"Build the Anthropic Messages API request body for an LLM request.@@ -453,7 +459,7 @@adapter re-hosting a non-Claude model here knows its own provider's thinking shape andrestrictions, which the model-id-derived config and the suppression rules below cannot describe."[{:keys [model system input tools schema tool_choice temperature max-tokens reasoning? reasoning-config fast? ai-proxy?]- :or {model "claude-haiku-4-5" reasoning? true}} :- core/LLMRequestOpts]+ :or {model default-model reasoning? true}} :- core/LLMRequestOpts](let [;; forced tool choice (structured output, or "required") is incompatible;; with thinking — suppress it there.thinking (or reasoning-config@@ -516,62 +522,44 @@[](< (System/currentTimeMillis) @fast-mode-cooldown-until))+(defn- fast-mode-retry-or-throw!+ "The `:on-request-error` handler for [[claude-raw]]. Retries the request at standard speed when Anthropic+ rejected fast mode itself, and otherwise rethrows through the usual translation.++ Decoding the error body also closes the streamed response, so the connection is not leaked when the+ exception is swallowed by the retry. Fast mode has its own rate-limit pool, so a 429 here doesn't+ imply standard speed is limited; a 400 needs its message checked to keep unrelated malformed requests+ failing fast. A 529 means the API itself is overloaded, so it gets no immediate retry: surface it and+ let the caller's retry loop pace the next attempt, which the armed cooldown keeps at standard speed."+ [req retry! ^Throwable e]+ (let [status (:status (ex-data e))+ res (when (and (:speed req) (contains? #{400 429 529} status))+ (core/decode-error-body e))+ rejected? (and res (or (not= 400 status) (fast-mode-rejection? res)))]+ (when rejected?+ (reset! fast-mode-cooldown-until (+ (System/currentTimeMillis) fast-mode-cooldown-ms))+ (log/warn "Anthropic rejected the fast-mode request; falling back to standard speed" {:status status}))+ (if (and rejected? (not= 529 status))+ (retry!)+ (adapter/rethrow! provider (if res (ex-info (str (ex-message e)) res e) e)))))+(mu/defn claude-raw"Perform a streaming request to Claude API.Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing."- [{:keys [model input tools credentials ai-proxy?] :as opts- :or {model "claude-haiku-4-5"}} :- core/LLMRequestOpts]- (let [opts (cond-> opts (fast-mode-cooling-down?) (assoc :fast? false))+ [{:keys [model] :as opts+ :or {model default-model}} :- core/LLMRequestOpts]+ (let [opts (cond-> (assoc opts :model model)+ (fast-mode-cooling-down?) (assoc :fast? false))req (claude-request-body opts)]- (with-span :info {:name :metabot.claude/request- :model model- :msg-count (count input)- :tool-count (count tools)}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "anthropic" "Anthropic"- (when api-key- {:url (:base-url credentials)- :headers {"x-api-key" api-key}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/v1/messages"- :as :stream- :headers (cond-> {"anthropic-version" "2023-06-01"- "content-type" "application/json"}- (:speed req) (assoc "anthropic-beta" "fast-mode-2026-02-01"))- :body (json/encode req)})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "anthropic"- :model model- :url "/v1/messages"- :request req})- (core/reducible-with-api-errors "anthropic" anthropic-error-msg)))- (catch Exception e- ;; decoding the error body also closes the streamed response, so the connection is- ;; not leaked when the exception is swallowed by the retry below. Fast mode has its- ;; own rate-limit pool, so a 429 here doesn't imply standard speed is limited;- ;; a 400 needs its message checked to keep unrelated malformed requests failing fast.- (let [status (:status (ex-data e))- res (when (and (:speed req) (contains? #{400 429 529} status))- (core/decode-error-body e))- rejected? (and res (or (not= 400 status) (fast-mode-rejection? res)))]- (when rejected?- (reset! fast-mode-cooldown-until (+ (System/currentTimeMillis) fast-mode-cooldown-ms))- (log/warn "Anthropic rejected the fast-mode request; falling back to standard speed"- {:status status}))- ;; 529 means the API itself is overloaded, so no immediate retry: surface it and- ;; let the caller's retry loop pace the next attempt, which the armed cooldown- ;; keeps at standard speed.- (if (and rejected? (not= 529 status))- (claude-raw (assoc opts :fast? false))- (core/rethrow-api-error! "anthropic" anthropic-error-msg- (if res (ex-info (str (ex-message e)) res e) e)))))))))+ (adapter/stream! provider opts+ {:path "/v1/messages"+ :body req+ :headers (when (:speed req) {"anthropic-beta" fast-mode-beta})+ :on-request-error #(fast-mode-retry-or-throw!+ req+ (fn [] (claude-raw (assoc opts :fast? false)))+ %)})))(defn claude"Call Claude API, return AISDK stream"diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.cljindex 6a41bc53fe9..3d6b54254dd 100644--- a/src/metabase/metabot/self/core.clj+++ b/src/metabase/metabot/self/core.clj@@ -14,6 +14,7 @@[metabase.request.schema :as request.schema][metabase.settings.core :as setting][metabase.util :as u]+ [metabase.util.http :as u.http][metabase.util.i18n :refer [tru]][metabase.util.json :as json][metabase.util.log :as log]@@ -141,11 +142,13 @@[:provider-metadata {:optional true} [:maybe ProviderMetadata]]])(def ^:private ApiKeyCredentials- "The `{:api-key ... :base-url ...}` connection shape shared by most providers."+ "The `{:api-key ... :base-url ...}` connection shape shared by most providers. `:model-reasoning` and+ `:probed-model` are not admin-entered: a connect-time probe records them on the connection (vLLM)."[:map {:closed true}[:api-key {:optional true} [:maybe :string]][:base-url {:optional true} [:maybe :string]]- [:model-reasoning {:optional true} [:maybe [:or :boolean :string]]]])+ [:model-reasoning {:optional true} [:maybe [:or :boolean :string]]]+ [:probed-model {:optional true} [:maybe :string]]])(def ^:private AzureCredentials"An Azure connection's config: the API-key pair plus the model family and deployment name its model is composed from."@@ -169,9 +172,13 @@[:project-id {:optional true} [:maybe :string]][:location {:optional true} [:maybe :string]][:auth-method {:optional true} [:maybe :string]]- [:base-url {:optional true} [:maybe :string]]])+ [:base-url {:optional true} [:maybe :string]]+ ;; recorded by the connect-time probe, not entered by the admin+ [:probed-model {:optional true} [:maybe :string]]])-(def ^:private LLMCredentials+(def LLMCredentials+ "A connection's credentials, in whichever provider shape it carries. Public so the adapter layer can say+ `:credentials` once rather than restating an open map at each schema that carries one."[:or ApiKeyCredentials AzureCredentials BedrockCredentials GoogleCredentials])(def ^:private ReasoningConfig@@ -1226,6 +1233,12 @@{:api-error true:error-code :api-key-missing}))+(def NetworkPolicyFloor+ "The `:network-policy-floor` [[resolve-auth]] may put on an auth map, as the set of policies+ [[metabase.llm.settings/network-policy]] ranks. Derived from that list rather than spelled out again, so+ a policy added there cannot leave this behind."+ (into [:enum] u.http/configurable-network-policies))+(defn resolve-auth"Pick the right auth map for an LLM request.diff --git a/src/metabase/metabot/self/deepseek.clj b/src/metabase/metabot/self/deepseek.cljindex 5b7400afd2f..86cc2e8ef90 100644--- a/src/metabase/metabot/self/deepseek.clj+++ b/src/metabase/metabot/self/deepseek.clj@@ -15,15 +15,12 @@https://api-docs.deepseek.com/guides/anthropic_api"(:require+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug]- [metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json][metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)@@ -33,11 +30,30 @@(def ^:private messages-path "/anthropic/v1/messages")+(def ^:private provider+ "`metabase.llm.api.provider`'s `provider-client-error?` surfaces any 4xx `:api-error` verbatim on the+ connection form, so the request-rejected statuses (400, 422) need messages that do not send admins hunting+ a key problem that does not exist. 402 is an exhausted account balance, which DeepSeek reports separately+ from rate limiting."+ (adapter/provider+ {:slug "deepseek"+ :display-name "DeepSeek"+ :error-fallback #(tru "DeepSeek API error (HTTP {0})" %)+ :errors {400 #(tru "DeepSeek rejected the request — check the model and request parameters")+ 401 #(tru "DeepSeek API key expired or invalid")+ 402 #(tru "DeepSeek account balance is exhausted")+ 403 #(tru "DeepSeek denied access — check the API key permissions")+ 404 #(tru "DeepSeek API endpoint or model was not found — check the base URL and model")+ 422 #(tru "DeepSeek rejected the request parameters")+ 429 #(tru "DeepSeek has rate limited us")+ 500 #(tru "DeepSeek returned an internal server error")+ 503 #(tru "DeepSeek is overloaded and is asking us to wait")}}))+(def supported-models- "DeepSeek models offered in the Metabot model picker, as a map of model id -> display name.+ "DeepSeek models offered in the Metabot model picker, keyed by model id.`list-models` returns the intersection of this map with the `/models` catalog."- {"deepseek-v4-flash" "DeepSeek V4 Flash"- "deepseek-v4-pro" "DeepSeek V4 Pro"})+ {"deepseek-v4-flash" {:display-name "DeepSeek V4 Flash"}+ "deepseek-v4-pro" {:display-name "DeepSeek V4 Pro"}})(def ^:private thinking-enabled-payload"Sent whenever thinking is allowed. Explicit rather than omitted: DeepSeek ignores an@@ -54,73 +70,22 @@[model](contains? supported-models model))-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for DeepSeek")- {:api-error true- :error-code :proxy-unsupported}))--(defn- deepseek-error-msg- "Canonical, status-specific DeepSeek error message.-- `metabase.metabot.api`'s `provider-client-error?` renders any 4xx `:api-error` under the admin- API-key field, so the request-rejected statuses (400, 422) need messages that do not send admins- hunting a key problem that does not exist. 402 is an exhausted account balance, which DeepSeek- reports separately from rate limiting."- [res]- (let [status (long (:status res 0))]- (case status- 400 (tru "DeepSeek rejected the request — check the model and request parameters")- 401 (tru "DeepSeek API key expired or invalid")- 402 (tru "DeepSeek account balance is exhausted")- 403 (tru "DeepSeek denied access — check the API key permissions")- 404 (tru "DeepSeek API endpoint or model was not found — check the base URL and model")- 422 (tru "DeepSeek rejected the request parameters")- 429 (tru "DeepSeek has rate limited us")- 500 (tru "DeepSeek returned an internal server error")- 503 (tru "DeepSeek is overloaded and is asking us to wait")- (tru "DeepSeek API error (HTTP {0})" status))))--(defn- supported-model?- "Whether a `/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. DeepSeek answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))-(defn- list-all-models- "Fetch the full DeepSeek model catalog (`GET /models`).-- The catalog is OpenAI-style even though the chat surface is not, so the fail-closed extraction in- [[chat-completions/models-catalog]] applies. It doubles as the credential round-trip behind the- admin Connect button — it 401s on a bad key.- `:ai-proxy?` is not supported for DeepSeek and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "deepseek" "DeepSeek"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/models"- :as :json- :headers {"Content-Type" "application/json"}})]- (chat-completions/models-catalog "DeepSeek" res))- (catch Exception e- (core/rethrow-api-error! "deepseek" deepseek-error-msg e))))--(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the DeepSeek models supported by this adapter (see [[supported-models]]).- Display names come from [[supported-models]]: DeepSeek catalog entries carry no `:name`.+ The `/models` catalog it intersects is OpenAI-style even though the chat surface is not, so the fail-closed+ extraction in [[adapter/fetch-catalog]] applies. It doubles as the credential round-trip behind the admin+ Connect button — it 401s on a bad key. Display names come from [[supported-models]]: DeepSeek catalog+ entries carry no `:name`.`:ai-proxy?` is not supported for DeepSeek and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id]}]- {:id id :display_name (supported-models id)})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models (adapter/fetch-catalog provider opts))));;; --------------------------------------------- The thinking contract ------------------------------------------;;;@@ -213,41 +178,13 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for DeepSeek and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts+ [{:keys [model] :as opts:or {model default-model}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (deepseek-request-body (assoc opts :model model))]- (log/debug "DeepSeek request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.deepseek/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "deepseek" "DeepSeek"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url messages-path- :as :stream- :headers {"anthropic-version" anthropic-version- "Content-Type" "application/json"}- :body (json/encode req)})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "deepseek"- :model model- :url messages-path- :request req})- (core/reducible-with-api-errors "deepseek" deepseek-error-msg)))- (catch Exception e- (core/rethrow-api-error! "deepseek" deepseek-error-msg e))))))+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path messages-path+ :body (deepseek-request-body opts)+ :headers {"anthropic-version" anthropic-version}})))(defn deepseek->aisdk-chunks-xf"Translates DeepSeek Anthropic Messages streaming events into AI SDK v5 protocol chunks."diff --git a/src/metabase/metabot/self/google.clj b/src/metabase/metabot/self/google.cljindex 89d8abb59a8..1cd20865fdd 100644--- a/src/metabase/metabot/self/google.clj+++ b/src/metabase/metabot/self/google.clj@@ -29,8 +29,8 @@[clojure.string :as str][metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.google.models :as models][metabase.metabot.self.google.raw-predict :as raw-predict][metabase.metabot.self.google.stream-generate-content :as stream-generate-content]@@ -38,8 +38,7 @@[metabase.util.i18n :refer [tru]][metabase.util.json :as json][metabase.util.malli :as mu]- [metabase.util.memoize :as u.memoize]- [metabase.util.o11y :refer [with-span]])+ [metabase.util.memoize :as u.memoize])(:import(com.google.auth.oauth2 GoogleCredentials ServiceAccountCredentials)(java.io ByteArrayInputStream IOException)@@ -187,43 +186,64 @@(location-host location)configured)))-(defn- resolve-google-auth- "Resolves the `{:auth {:url ... :headers ...} :credentials ...}` pair for a Google request.-- The credentials in the result are the resolved ones, so that callers can build resource paths from the project and- the location. A service account key has precedence over an OAuth access token. Throws if no project ID resolves, or- if `ai-proxy?` is true."- [credentials ai-proxy?]- (when ai-proxy?- (throw (ex-info (tru "AI proxy is not supported for the Google provider")- {:api-error true- :error-code :proxy-unsupported})))+(defn- auth-method+ "Which credential a connection authenticates with, or nil when it carries neither.+ A service account key has precedence over an OAuth access token."+ [{:keys [service-account-key oauth-access-token]}]+ (cond+ (not-empty service-account-key) :service-account+ (not-empty oauth-access-token) :oauth-token))++(defn- resolve-credentials+ "Resolve a connection's credentials into the form the rest of this namespace reads: blanks removed, and the+ project ID derived from the service account key when the admin did not set one.++ Google is the one provider whose *request path* is built from its credentials — the project and the location are+ URL segments (see [[model-resource-path]]) — so resolution happens before a request is composed rather than inside+ [[google-auth]]. Idempotent, so re-resolving an already-resolved map is harmless."+ [credentials];; blank credentials count as absent — the environment can hand a setting an empty string, and one credential;; left blank must not shadow the other or the project ID a service account key carries- (let [{:keys [service-account-key oauth-access-token project-id]- :as creds} (merge credentials- {:service-account-key (u/trimmed-string (:service-account-key credentials))- :oauth-access-token (u/trimmed-string (:oauth-access-token credentials))- :project-id (u/trimmed-string (:project-id credentials))})- sa-creds (when service-account-key- (cached-service-account-credentials service-account-key))- project-id (or project-id (some-> sa-creds service-account-project-id))- auth-method (cond- sa-creds :service-account- oauth-access-token :oauth-token)]- (when (and auth-method (not project-id))+ (let [creds (merge credentials+ {:service-account-key (u/trimmed-string (:service-account-key credentials))+ :oauth-access-token (u/trimmed-string (:oauth-access-token credentials))+ :project-id (u/trimmed-string (:project-id credentials))})+ project-id (or (:project-id creds)+ (some-> (:service-account-key creds)+ cached-service-account-credentials+ service-account-project-id))]+ ;; a connection with no credential at all fails here rather than several steps later: the project and the+ ;; location are URL segments, so without this the missing project ID would be reported before the missing+ ;; credential that was the actual problem+ (when-not (auth-method creds)+ (throw (core/missing-api-key-ex "Google")))+ (when-not project-id(throw (ex-info (tru "A Google Cloud project ID is required for the Google provider"){:api-error true:status-code 400:error-code :project-id-required})))- {:auth (core/resolve-auth "google" "Google"- (when auth-method- {:url (api-base-url creds)- :headers (case auth-method- :service-account (fresh-bearer-headers sa-creds)- :oauth-token (oauth-bearer-headers oauth-access-token))})- ai-proxy?)- :credentials (assoc creds :project-id project-id)}))+ (assoc creds :project-id project-id)))++(defn- google-auth+ "Google's `:auth`. Reads the host from the credentials: the API host has to agree with the location, so it is not a+ fixed base URL the way it is for every other provider.++ Resolves them itself rather than trusting the caller to have done it. [[resolve-credentials]] is idempotent and the+ service-account parse is cached, so the [[adapter/request!]] callers that resolve first pay nothing, and the chat+ path can hand over the raw map — which is what puts a missing credential, a bad project ID or a malformed key+ inside the request span. Because resolution raises the missing-credential error itself, [[core/resolve-auth]] never+ reaches its own `missing-api-key-ex` branch here."+ [{:keys [slug display-name]} {:keys [credentials ai-proxy?]}]+ (let [credentials (resolve-credentials credentials)]+ (core/resolve-auth slug display-name+ (when-let [method (auth-method credentials)]+ {:url (api-base-url credentials)+ :headers (case method+ :service-account (fresh-bearer-headers+ (cached-service-account-credentials+ (:service-account-key credentials)))+ :oauth-token (oauth-bearer-headers (:oauth-access-token credentials)))})+ ai-proxy?)))(defn- effective-project-id"Returns the project ID for a credential's requests.@@ -326,13 +346,18 @@:google (stream-generate-content/reasoning-model? model)false))-(defn context-window-tokens+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Google answers from the model name, per wire family."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))++(mu/defn context-window-tokens :- [:maybe :int]"The input context window for a publisher-qualified `model`, or nil when it isn't one we know.Gemini windows come from the [[models/catalog]], the same rows that drive the reasoning gate.Answers nil for a model this adapter cannot serve rather than throwing the way [[model->family]] does, for thesame reason [[reasoning-model?]] does."- [model]+ [model :- [:maybe :string]](case (model-families (model-publisher model)):anthropic (raw-predict/context-window-tokens (model-id model)):google (get-in models/catalog [model :context-window])@@ -359,20 +384,23 @@:error-code :invalid-model})))(format "%s/publishers/%s/models/%s" (location-path credentials) publisher model-id)))-(defn- google-error-msg- "Returns the Google API error message for the status of `res`."- [res]- (let [status (long (:status res 0))]- (case status- 400 (tru "Google API rejected the request as invalid")- 401 (tru "Google API credentials expired or invalid")- 403 (tru "Google API credentials have insufficient permissions or the API is not enabled for this project")- 404 (tru "Google API endpoint is unavailable or the model was not found")- 429 (tru "Google API has rate limited us")- 500 (tru "Google API returned an internal server error")- 501 (tru "Google API is not available in this location")- 503 (tru "Google API is temporarily unavailable")- (tru "Google API error (HTTP {0})" status))))+(def ^:private provider+ "Google is the only adapter whose request path is derived from its credentials rather than fixed, so the path it+ hands [[adapter/stream!]] is a thunk and [[google-auth]] resolves the credentials itself (see+ [[resolve-credentials]]) — both inside the request span."+ (adapter/provider+ {:slug "google"+ :display-name "Google"+ :auth google-auth+ :error-fallback #(tru "Google API error (HTTP {0})" %)+ :errors {400 #(tru "Google API rejected the request as invalid")+ 401 #(tru "Google API credentials expired or invalid")+ 403 #(tru "Google API credentials have insufficient permissions or the API is not enabled for this project")+ 404 #(tru "Google API endpoint is unavailable or the model was not found")+ 429 #(tru "Google API has rate limited us")+ 500 #(tru "Google API returned an internal server error")+ 501 #(tru "Google API is not available in this location")+ 503 #(tru "Google API is temporarily unavailable")}}))(defn- json-content?"Returns true if an HTTP response has a JSON content type."@@ -388,11 +416,15 @@(contains? #{404 501} status))(defn- google-res->msg- "The `res->message` callback for [[core/rethrow-api-error!]] and [[core/reducible-with-api-errors]]."+ "The `res->message` callback for [[core/rethrow-api-error!]] and [[adapter/stream!]]'s `:error-msg`.++ Wraps the descriptor's own message rather than restating it: a status whose message should name the host it was+ sent to (see [[include-endpoint-in-msg?]]) gets the endpoint appended, and the endpoint is known only per+ connection, which is why this cannot live on the descriptor."[credentials](let [endpoint (delay (try (api-base-url credentials) (catch Exception _ nil)))](fn [res]- (cond-> (google-error-msg res)+ (cond-> ((:error-msg provider) res)(and (include-endpoint-in-msg? (:status res)) @endpoint)(str " " (tru "(endpoint: {0})" @endpoint))))))@@ -406,7 +438,7 @@location (:location credentials)known? (conj multi-region-locations global-location)](core/rethrow-api-error!- "google"+ (:slug provider)(google-res->msg credentials)(if (and location(= 404 (:status data))@@ -432,12 +464,11 @@credential, the project, the location, and the model all resolve.https://docs.cloud.google.com/gemini-enterprise-agent-platform/reference/rest/v1/projects.locations.publishers.models/countTokens"- [auth credentials model]- (core/request auth- {:method :post- :url (str (model-resource-path credentials model) ":countTokens")- :headers {"Content-Type" "application/json"}- :body (json/encode count-tokens-probe-body)}))+ [credentials model]+ (adapter/request! provider {:method :post+ :path (str (model-resource-path credentials model) ":countTokens")+ :body (json/encode count-tokens-probe-body)+ :credentials credentials}))(defn- anthropic-error-body?"Whether an error `body` shape matches an Anthropic error rather than Google's.@@ -466,13 +497,12 @@Unlike [[metabase.metabot.self.azure/validate-anthropic-surface!]], the status code alone cannot settle it. Googlerejects a model that the location does not serve with a `400 FAILED_PRECONDITION` of its own — the same statusAnthropic uses for the validation error that means the model *is* servable."- [auth credentials model]+ [credentials model](try- (core/request auth- {:method :post- :url (str (model-resource-path credentials model) raw-predict-method)- :headers {"Content-Type" "application/json"}- :body "{}"})+ (adapter/request! provider {:method :post+ :path (str (model-resource-path credentials model) raw-predict-method)+ :body "{}"+ :credentials credentials})(catch Exception e(let [{:keys [status body]} (ex-data e)](when-not (and (= 400 status) (anthropic-error-body? body))@@ -480,13 +510,13 @@(defn- validate-model!"Validates `model` against the surface that serves it, and discards the response."- [auth credentials model]+ [credentials model](case (model->family model)- :anthropic (validate-anthropic-surface! auth credentials model)- :google (validate-google-surface! auth credentials model))+ :anthropic (validate-anthropic-surface! credentials model)+ :google (validate-google-surface! credentials model))nil)-(defn list-models+(mu/defn list-models :- adapter/ModelListing"Validates the Google credentials and the candidate model with a probe, and returns an empty model list.Similar to the Azure provider, there is no list-models call that we can use to whitelist models for the Gemini@@ -494,57 +524,47 @@sometimes omits models that are available, hence we can't rely on it. Seehttps://github.com/googleapis/python-genai/issues/679- `:probe?` reports the model the probe verified as `:learned-config` `:probed-model`, for the connect and edit paths+ `:probe?` reports the model the probe verified as `:connection-info` `:probed-model`, for the connect and edit pathsto record on the connection and re-verify against later passing it in as the `proposed-model` on future attempts."([] (list-models {}))- ([{:keys [credentials model proposed-model ai-proxy? probe?]}]+ ([{:keys [credentials model proposed-model ai-proxy? probe?]} :- adapter/ListOpts]+ (adapter/reject-ai-proxy! provider ai-proxy?)(if-let [model (or (not-empty model) (not-empty proposed-model))](do(try- (let [{:keys [auth credentials]} (resolve-google-auth credentials ai-proxy?)]- (validate-model! auth credentials model))+ (validate-model! (resolve-credentials credentials) model)(catch Exception e(rethrow-google-api-error! credentials e)))(cond-> {:models []}- probe? (assoc :learned-config {:probed-model model})))+ probe? (assoc :connection-info {:probed-model model}))){:models []})))(mu/defn google-raw"Makes a streaming request to the Gemini Enterprise Agent Platform.Gemini models stream through `streamGenerateContent`; Anthropic partner models through `streamRawPredict`.`:ai-proxy?` is not supported and throws when it is true."- [{:keys [model input tools credentials ai-proxy?] :as opts+ [{:keys [model credentials] :as opts:or {model default-model}} :- core/LLMRequestOpts]- (let [family (model->family model)- req (case family- :anthropic (raw-predict/request-body (model-id model) opts)- ;; pass the defaulted model down: the thinking directive keys off it- :google (stream-generate-content/request-body (assoc opts :model model)))- method (case family- :anthropic raw-predict-method- :google generate-content-method)- res->msg (google-res->msg credentials)]- (with-span :info {:name :metabot.google/request- :model model- :msg-count (count input)- :tool-count (count tools)}- (try- (let [{:keys [auth credentials]} (resolve-google-auth credentials ai-proxy?)- url (str (model-resource-path credentials model) method)- response (core/request auth- {:method :post- :url url- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)})]- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "google"- :model model- :url url- :request req})- (core/reducible-with-api-errors "google" res->msg)))- (catch Exception e- (rethrow-google-api-error! credentials e))))))+ (let [family (model->family model)+ opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {;; a thunk, not a string: the project and the location are URL segments, so the path cannot be+ ;; built until the credentials resolve, and that resolution is the only one in the fleet that+ ;; can fail. [[adapter/stream!]] calls it inside the span and behind the proxy refusal, so a+ ;; bad project ID lands on the trace and a proxied request still hears about the proxy first.+ :path #(str (model-resource-path (resolve-credentials credentials) model)+ (case family+ :anthropic raw-predict-method+ :google generate-content-method))+ :body (case family+ :anthropic (raw-predict/request-body (model-id model) opts)+ ;; `opts` carries the defaulted model: the thinking directive keys off it+ :google (stream-generate-content/request-body opts))+ :span-attrs {:family (name family)}+ ;; both read only `:location`, which resolution does not touch, so the raw map serves and+ ;; neither can mask a resolution failure with one of its own+ :error-msg (google-res->msg credentials)+ :on-request-error #(rethrow-google-api-error! credentials %)})))(defn google"Call the Gemini Enterprise Agent Platform, return AISDK stream."diff --git a/src/metabase/metabot/self/mistral.clj b/src/metabase/metabot/self/mistral.cljindex c89aac4f4f8..e5436d7ebdd 100644--- a/src/metabase/metabot/self/mistral.clj+++ b/src/metabase/metabot/self/mistral.clj@@ -5,44 +5,35 @@https://docs.mistral.ai/api/"(:require+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json]- [metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)(def ^:private default-model "mistral-medium-3-5")-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for Mistral")- {:api-error true- :error-code :proxy-unsupported}))--(defn- mistral-error-msg- "Canonical, status-specific Mistral error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "Mistral API key expired or invalid")- 404 (tru "Mistral API endpoint was not found — check the base URL")- 429 (tru "Mistral has rate limited us")- 500 (tru "Mistral returned an internal server error")- (tru "Mistral API error (HTTP {0})" status))))+(def ^:private provider+ (adapter/provider+ {:slug "mistral"+ :display-name "Mistral"+ :error-fallback #(tru "Mistral API error (HTTP {0})" %)+ :errors {401 #(tru "Mistral API key expired or invalid")+ 404 #(tru "Mistral API endpoint was not found — check the base URL")+ 429 #(tru "Mistral has rate limited us")+ 500 #(tru "Mistral returned an internal server error")}}))(def supported-models"Mistral models offered in the Metabot model picker, keyed by model id.`list-models` returns the intersection of this map with the `/models` catalog."{"mistral-medium-3-5" {:display-name "Mistral Medium 3.5" :context-window 262144}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know.Catalog aliases (e.g. `mistral-medium-latest`) are not resolved."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))(defn reasoning-model?@@ -55,6 +46,11 @@[model](contains? supported-models (str model)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Mistral answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(defn- whitelisted-id"The [[supported-models]] id a `/models` catalog entry resolves to, or nil when unsupported.Mistral models have a generic `:id` like `mistral-medium-latest` but `:aliases` contains version specific aliases@@ -64,34 +60,15 @@(some #(when (contains? supported-models %) %)(cons id aliases)))-(defn- list-all-models- "Fetch the full Mistral model catalog (`GET /models`).- A 2xx whose body isn't a recognizable catalog throws rather than yielding an empty picker — see- [[chat-completions/models-catalog]].- `:ai-proxy?` is not supported for Mistral and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "mistral" "Mistral"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/models"- :as :json- :headers {"Content-Type" "application/json"}})]- (chat-completions/models-catalog "Mistral" res))- (catch Exception e- (core/rethrow-api-error! "mistral" mistral-error-msg e))))--(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the Mistral models supported by this adapter (see [[supported-models]]).++ Resolves catalog aliases rather than intersecting ids directly (see [[whitelisted-id]]), so this does not use+ the shared [[adapter/model-listing]].`:ai-proxy?` is not supported for Mistral and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)+ ([opts :- adapter/ListOpts]+ {:models (->> (adapter/fetch-catalog provider opts)(keep whitelisted-id)distinctsort@@ -151,36 +128,12 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for Mistral and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts+ [{:keys [model] :as opts:or {model default-model}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (mistral-request-body (assoc opts :model model))]- (log/debug "Mistral request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.mistral/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "mistral" "Mistral"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/chat/completions"- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)})]- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "mistral"- :model model- :url "/chat/completions"- :request req})))- (catch Exception e- (core/rethrow-api-error! "mistral" mistral-error-msg e))))))+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path "/chat/completions"+ :body (mistral-request-body opts)})))(def ^:private stop-reasons"Mistral adds `model_length` — the model's own context limit, a truncation just like `length` — and reports adiff --git a/src/metabase/metabot/self/moonshot.clj b/src/metabase/metabot/self/moonshot.cljindex e229f0d59e1..c9177e50b0b 100644--- a/src/metabase/metabot/self/moonshot.clj+++ b/src/metabase/metabot/self/moonshot.clj@@ -5,40 +5,31 @@https://platform.kimi.ai/docs"(:require+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json]- [metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)(def ^:private default-model "kimi-k3")-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for Moonshot")- {:api-error true- :error-code :proxy-unsupported}))--(defn- moonshot-error-msg- "Canonical, status-specific Moonshot error message.-- Mapping 400 only improves the message text: `metabase.metabot.api`'s `provider-client-error?` renders any 4xx- `:api-error` under the admin API-key field, so a generic message sends admins hunting a key problem that does not- exist. 429 covers rate limiting *and* an exhausted account balance, which Moonshot reports with the same status."- [res]- (let [status (long (:status res 0))]- (case status- 400 (tru "Moonshot rejected the request — check the model and request parameters")- 401 (tru "Moonshot API key expired or invalid")- 403 (tru "Moonshot denied access — check the API key''s permissions")- 404 (tru "Moonshot API endpoint or model was not found — check the base URL and model")- 429 (tru "Moonshot has rate limited us, or the account balance is exhausted")- 500 (tru "Moonshot returned an internal server error")- (tru "Moonshot API error (HTTP {0})" status))))+(def ^:private provider+ "Mapping 400 only improves the message text: `metabase.llm.api.provider`'s `provider-client-error?` surfaces any+ 4xx `:api-error` verbatim on the connection form, so a generic message sends admins hunting a key problem that+ does not exist. 429 covers rate limiting *and* an exhausted account balance, which Moonshot reports with the+ same status."+ (adapter/provider+ {:slug "moonshot"+ :display-name "Moonshot"+ :error-fallback #(tru "Moonshot API error (HTTP {0})" %)+ :errors {400 #(tru "Moonshot rejected the request — check the model and request parameters")+ 401 #(tru "Moonshot API key expired or invalid")+ 403 #(tru "Moonshot denied access — check the API key''s permissions")+ 404 #(tru "Moonshot API endpoint or model was not found — check the base URL and model")+ 429 #(tru "Moonshot has rate limited us, or the account balance is exhausted")+ 500 #(tru "Moonshot returned an internal server error")}}))(def supported-models"Moonshot models offered in the Metabot model picker, keyed by model id.@@ -48,9 +39,9 @@{"kimi-k2.6" {:display-name "Kimi K2.6" :context-window 262144}"kimi-k3" {:display-name "Kimi K3" :context-window 1048576}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))(def ^:private thinking-only-models@@ -75,48 +66,21 @@[model](contains? reasoning-models (str model)))-(defn- supported-model?- "Whether a `/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))--(defn- list-all-models- "Fetch the full Moonshot model catalog (`GET /models`).-- The endpoint doubles as the credential round-trip behind the admin Connect button — it 401s on a bad key.- A 2xx whose body isn't a recognizable catalog throws rather than yielding an empty picker — see- [[chat-completions/models-catalog]].- `:ai-proxy?` is not supported for Moonshot and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "moonshot" "Moonshot"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/models"- :as :json- :headers {"Content-Type" "application/json"}})]- (chat-completions/models-catalog "Moonshot" res))- (catch Exception e- (core/rethrow-api-error! "moonshot" moonshot-error-msg e))))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Moonshot answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))-(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the Moonshot models supported by this adapter (see [[supported-models]]).- Display names come from [[supported-models]] rather than the catalog: Moonshot catalog entries carry no `:name`- (and no `:aliases`, so there is no Mistral-style alias resolution to do either).+ The `/models` catalog it intersects doubles as the credential round-trip behind the admin Connect button —+ it 401s on a bad key. Display names come from [[supported-models]] rather than the catalog: Moonshot catalog+ entries carry no `:name` (and no `:aliases`, so there is no Mistral-style alias resolution to do either).`:ai-proxy?` is not supported for Moonshot and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id]}]- {:id id :display_name (get-in supported-models [id :display-name])})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models (adapter/fetch-catalog provider opts))))(def ^:private forced-tool-call-token-floor"Smallest `max_tokens` a forced tool call on a thinking-only model may be capped at.@@ -210,36 +174,12 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for Moonshot and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts+ [{:keys [model] :as opts:or {model default-model}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (moonshot-request-body (assoc opts :model model))]- (log/debug "Moonshot request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.moonshot/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "moonshot" "Moonshot"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/chat/completions"- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)})]- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "moonshot"- :model model- :url "/chat/completions"- :request req})))- (catch Exception e- (core/rethrow-api-error! "moonshot" moonshot-error-msg e))))))+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path "/chat/completions"+ :body (moonshot-request-body opts)})))(defn moonshot->aisdk-chunks-xf"Translates Moonshot Chat Completions streaming chunks into AI SDK v5 protocol chunks.diff --git a/src/metabase/metabot/self/openai.clj b/src/metabase/metabot/self/openai.cljindex 5a6d6b67811..e47ca2a356a 100644--- a/src/metabase/metabot/self/openai.clj+++ b/src/metabase/metabot/self/openai.clj@@ -1,8 +1,8 @@(ns metabase.metabot.self.openai(:require[clojure.string :as str]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.schema :as schema][metabase.util :as u][metabase.util.i18n :refer [tru]]@@ -233,22 +233,18 @@[tool](assoc (schema/tool-function tool) :type "function"))-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for OpenAI")- {:api-error true- :error-code :proxy-unsupported}))--(defn- openai-error-msg- "Canonical, status-specific OpenAI error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "OpenAI API key expired or invalid")- 403 (tru "OpenAI API key has insufficient permissions")- 404 (tru "OpenAI API endpoint or model listing is unavailable")- 429 (tru "OpenAI API has rate limited us")- 500 (tru "OpenAI API is not working but not saying why")- (tru "OpenAI API error (HTTP {0})" status))))+(def ^:private default-model "gpt-5.4")++(def ^:private provider+ (adapter/provider+ {:slug "openai"+ :display-name "OpenAI"+ :error-fallback #(tru "OpenAI API error (HTTP {0})" %)+ :errors {401 #(tru "OpenAI API key expired or invalid")+ 403 #(tru "OpenAI API key has insufficient permissions")+ 404 #(tru "OpenAI API endpoint or model listing is unavailable")+ 429 #(tru "OpenAI API has rate limited us")+ 500 #(tru "OpenAI API is not working but not saying why")}}))(def supported-models"OpenAI chat models offered in the Metabot model picker, keyed by model id.@@ -263,48 +259,21 @@"gpt-5.4-pro" {:display-name "GPT-5.4 Pro" :context-window 922000}"gpt-5.4-mini" {:display-name "GPT-5.4 Mini" :context-window 272000}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))-(defn- supported-model?- "Whether a `/v1/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))--(defn- list-all-models- "Fetch the full OpenAI model catalog (`GET /v1/models`).- `:ai-proxy?` is not supported for OpenAI and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "openai" "OpenAI"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/v1/models"- :as :json- :headers {"Content-Type" "application/json"}})]- (get-in res [:body :data]))- (catch Exception e- (core/rethrow-api-error! "openai" openai-error-msg e))))--(defn list-models- "List the OpenAI chat models supported by this adapter (see [[supported-models]]).+(mu/defn list-models :- adapter/ModelListing+ "List the OpenAI chat models supported by this adapter, by intersecting [[supported-models]] with the+ account's `/v1/models` catalog.Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,- and throws when they are missing. Also supports `:ai-proxy?`.+ and throws when they are missing.`:ai-proxy?` is not supported for OpenAI and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id]}]- {:id id :display_name (get-in supported-models [id :display-name])})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models+ (adapter/fetch-catalog provider opts "/v1/models"))))(defn- strip-vendor-prefix"`model` lowercased and without an optional vendor prefix (e.g. Bedrock's `openai.`).@@ -328,10 +297,15 @@[model](not (model-supports-temperature? model)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. OpenAI answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(mu/defn openai-request-body"Build the OpenAI Responses API request body for an LLM request."[{:keys [model system input tools schema tool_choice temperature max-tokens reasoning?]- :or {model "gpt-5.4" reasoning? true}} :- core/LLMRequestOpts]+ :or {model default-model reasoning? true}} :- core/LLMRequestOpts](let [input (cond->> input(not reasoning?) (remove #(= :reasoning (:type %))))all-tools (or (when schema@@ -367,35 +341,12 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for OpenAI and throws when true."- [{:keys [model credentials ai-proxy?] :as opts- :or {model "gpt-5.4"}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (openai-request-body opts)]- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "openai" "OpenAI"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/v1/responses"- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "openai"- :model model- :url "/v1/responses"- :request req})- (core/reducible-with-api-errors "openai" openai-error-msg)))- (catch Exception e- (core/rethrow-api-error! "openai" openai-error-msg e)))))+ [{:keys [model] :as opts+ :or {model default-model}} :- core/LLMRequestOpts]+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path "/v1/responses"+ :body (openai-request-body opts)})))(defn openai"Call OpenAI API, return AISDK stream."diff --git a/src/metabase/metabot/self/openai/chat_completions.clj b/src/metabase/metabot/self/openai/chat_completions.cljindex 129484496fc..ee5ef704d4e 100644--- a/src/metabase/metabot/self/openai/chat_completions.clj+++ b/src/metabase/metabot/self/openai/chat_completions.clj@@ -374,9 +374,11 @@never actually reached and leaves an empty model picker with no diagnostic. Throw instead.`provider-name` is the display name, used in the message. The exception is tagged `:api-error` so the- adapter's surrounding [[metabase.metabot.self.core/rethrow-api-error!]] rethrows it unchanged, and carries- no `:status`: this isn't a credentials problem, and `metabase.metabot.api`'s `provider-client-error?`- renders any 4xx under the admin API-key field, which would attach the wrong message to the wrong input.+ adapter's surrounding [[metabase.metabot.self.core/rethrow-api-error!]] rethrows it unchanged, and+ `:status-code 400` so it reaches the admin as their misconfiguration. Without it `metabase.llm.api.provider`'s+ `provider-client-error?` does not recognise it, and the Connect path rethrows it as an unhandled 500:+ the admin still sees the sentence, but it bumps the unhandled-error counter and collapses to \"Something+ went wrong\" under `MB_HIDE_STACKTRACES=true`, losing the diagnostic for the operators who enabled that.A well-formed but empty `data` is a legitimate response — an account with no accessible models — and passes.@@ -387,6 +389,7 @@(when-not (sequential? data)(throw (ex-info (cond-> (tru "{0} returned an unexpected model list response" provider-name)detail (str ". " detail))- {:api-error true- :error-code :malformed-model-catalog})))+ {:api-error true+ :status-code 400+ :error-code :malformed-model-catalog})))data)))diff --git a/src/metabase/metabot/self/openrouter.clj b/src/metabase/metabot/self/openrouter.cljindex fff88b142bd..f8a143c6662 100644--- a/src/metabase/metabot/self/openrouter.clj+++ b/src/metabase/metabot/self/openrouter.clj@@ -9,37 +9,33 @@adapter converts those directly to Chat Completions messages."(:require[clojure.string :as str]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json]- [metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for OpenRouter")- {:api-error true- :error-code :proxy-unsupported}))--(defn- openrouter-error-msg- "Canonical, status-specific OpenRouter error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "OpenRouter API key expired or invalid")- 402 (tru "OpenRouter has insufficient credits")- 403 (tru "OpenRouter API key has insufficient permissions")- 404 (tru "OpenRouter model listing endpoint is unavailable")- 429 (tru "OpenRouter has rate limited us")- 500 (tru "OpenRouter returned an internal server error")- 502 (tru "OpenRouter upstream provider returned an error")- 503 (tru "OpenRouter service is unavailable")- (tru "OpenRouter API error (HTTP {0})" status))))+(def ^:private default-model "anthropic/claude-haiku-4.5")++(def ^:private provider+ (adapter/provider+ {:slug "openrouter"+ :display-name "OpenRouter"+ ;; attribution headers OpenRouter shows on the account's activity page+ :headers {"HTTP-Referer" "https://metabase.com"+ "X-Title" "Metabase"}+ :error-fallback #(tru "OpenRouter API error (HTTP {0})" %)+ :errors {401 #(tru "OpenRouter API key expired or invalid")+ 402 #(tru "OpenRouter has insufficient credits")+ 403 #(tru "OpenRouter API key has insufficient permissions")+ 404 #(tru "OpenRouter model listing endpoint is unavailable")+ 429 #(tru "OpenRouter has rate limited us")+ 500 #(tru "OpenRouter returned an internal server error")+ 502 #(tru "OpenRouter upstream provider returned an error")+ 503 #(tru "OpenRouter service is unavailable")}}))(def supported-models"OpenRouter models offered in the Metabot model picker, keyed by model id.@@ -101,9 +97,9 @@"z-ai/glm-5.3" {:display-name "GLM-5.3" :context-window 1048576 :reasoning :renderable :reasoning-mandatory? true}"z-ai/glm-5.2" {:display-name "GLM-5.2" :context-window 1048576 :reasoning :renderable}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))(defn- reasoning-class@@ -120,6 +116,11 @@[model](contains? #{:renderable :renderable-default} (reasoning-class model)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. OpenRouter answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(defn- reasoning-mandatory?"Whether OpenRouter rejects `reasoning {:enabled false}` for `model` with a 400.@@ -128,45 +129,16 @@[model](boolean (get-in supported-models [(str model) :reasoning-mandatory?])))-(defn- supported-model?- "Whether a `/v1/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))--(defn- list-all-models- "Fetch the full OpenRouter model catalog (`GET /v1/models`).- `:ai-proxy?` is not supported for OpenRouter and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "openrouter" "OpenRouter"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/v1/models"- :as :json- :headers {"Content-Type" "application/json"- "HTTP-Referer" "https://metabase.com"- "X-Title" "Metabase"}})]- (get-in res [:body :data]))- (catch Exception e- (core/rethrow-api-error! "openrouter" openrouter-error-msg e))))--(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the OpenRouter models supported by this adapter (see [[supported-models]]).Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,- and throws when they are missing. Also supports `:ai-proxy?`.+ and throws when they are missing.`:ai-proxy?` is not supported for OpenRouter and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id] :as model}]- {:id id :display_name (or (:name model) (get-in supported-models [id :display-name]))})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models+ (adapter/fetch-catalog provider opts "/v1/models")+ :name)));;; Streaming response → AISDK v5 chunks@@ -300,7 +272,7 @@builder instead would apply these OpenRouter-specific rules to every Chat Completions adapter, including vLLM,whose model names are customer-chosen free text."[{:keys [model system] :as opts- :or {model "anthropic/claude-haiku-4.5"}} :- core/LLMRequestOpts]+ :or {model default-model}} :- core/LLMRequestOpts](-> (cond-> (chat-completions/request-body (assoc opts :model model))(and system (anthropic-model? model))(update-in [:messages 0 :content] claude/system->cached-content-blocks)@@ -320,42 +292,12 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for OpenRouter and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts- :or {model "anthropic/claude-haiku-4.5"}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (openrouter-request-body opts)]- (log/debug "OpenRouter request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.openrouter/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "openrouter" "OpenRouter"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/v1/chat/completions"- :as :stream- :headers {"Content-Type" "application/json"- "HTTP-Referer" "https://metabase.com"- "X-Title" "Metabase"}- :body (json/encode req)})]- ;; The SSE body is consumed lazily, after this `try` has exited — wrap- ;; the reducible so mid-stream IO/timeout failures get the same- ;; provider-friendly translation as request-time errors.- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "openrouter"- :model model- :url "/v1/chat/completions"- :request req})- (core/reducible-with-api-errors "openrouter" openrouter-error-msg)))- (catch Exception e- (core/rethrow-api-error! "openrouter" openrouter-error-msg e))))))+ [{:keys [model] :as opts+ :or {model default-model}} :- core/LLMRequestOpts]+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path "/v1/chat/completions"+ :body (openrouter-request-body opts)})))(defn openrouter"Call OpenRouter Chat Completions API, return AISDK stream."diff --git a/src/metabase/metabot/self/registry.clj b/src/metabase/metabot/self/registry.cljnew file mode 100644index 00000000000..08694d990e4--- /dev/null+++ b/src/metabase/metabot/self/registry.clj@@ -0,0 +1,192 @@+(ns metabase.metabot.self.registry+ "Registry and capability lookups for the LLM providers Metabot can serve.++ A provider without a capability omits it from its row. `required` throws when the capability is absent;+ `optional` returns nil."+ (:require+ [metabase.llm.provider :as llm.provider]+ [metabase.metabot.self.adapter :as adapter]+ [metabase.metabot.self.azure :as azure]+ [metabase.metabot.self.bedrock :as bedrock]+ [metabase.metabot.self.claude :as claude]+ [metabase.metabot.self.core :as core]+ [metabase.metabot.self.deepseek :as deepseek]+ [metabase.metabot.self.google :as google]+ [metabase.metabot.self.mistral :as mistral]+ [metabase.metabot.self.moonshot :as moonshot]+ [metabase.metabot.self.openai :as openai]+ [metabase.metabot.self.openrouter :as openrouter]+ [metabase.metabot.self.vllm :as vllm]+ [metabase.metabot.self.zai :as zai]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(def ProviderType+ "The `llm-providers` type string a provider is registered under."+ :string)++(def Capability+ "The capabilities a registry row may expose. Passing any other key fails Malli validation in+ instrumented builds instead of looking like an absent optional capability."+ [:enum :stream :list-models :supported-models :context-window :reasoning? :fast-mode?])++(def AdapterRow+ "One provider's row. Closed for the same reason [[Capability]] is: a mistyped key in the table below would+ otherwise silently mean the provider lacks that capability.++ The `:=>` schemas say what each capability is applied to and answers with. Malli checks one no further+ than `ifn?`, so they are the contract an implementation is held to by its own `mu/defn`, and by+ `self-test/registry-test` comparing the two. `:supported-models` holds a map rather than a function —+ `ifn?` accepted it because a map is `ifn?` — so it stops at `var?`."+ [:map {:closed true}+ [:stream {:optional true} [:=> [:cat core/LLMRequestOpts] :any]]+ [:list-models {:optional true} [:function+ [:=> :cat adapter/ModelListing]+ [:=> [:cat adapter/ListOpts] adapter/ModelListing]]]+ [:supported-models {:optional true} [:fn var?]]+ [:context-window {:optional true} [:=> [:cat [:maybe :string]] [:maybe :int]]]+ [:reasoning? {:optional true} [:=> [:cat adapter/ResolvedRef] :boolean]]+ [:fast-mode? {:optional true} [:=> [:cat adapter/ResolvedRef] :boolean]]])++(def ^:private adapters+ "Every provider Metabot serves, by `llm-providers` type.++ Vars rather than the functions themselves: a map literal captures each value at namespace load, so+ `with-redefs` on an adapter would not be seen. That is why the dispatch this replaces was written as+ `case` forms inside functions; a var is deref'd at call time, so a map of vars keeps the property.++ :stream - the adapter entry point, returning an AISDK stream.+ :list-models - the provider's model listing, and the credential round trip behind Connect.+ :supported-models - the allow-list a listing is intersected with. Absent for a provider that serves+ whatever the operator loaded (vLLM), names its own deployment (Azure), or has a+ catalog fixed in `metabase.llm.provider` (Google, and the managed connection).+ :context-window - model -> its input context window in tokens.+ :reasoning? - resolved model ref -> whether it streams its reasoning back to us.+ :fast-mode? - resolved model ref -> whether it can be served in Anthropic fast mode.++ Whether the Metabase Cloud AI proxy can serve a provider is deliberately not a row here: it is declared on+ the provider's own descriptor, which is what [[metabase.metabot.self.adapter/reject-ai-proxy!]] enforces on+ every request. A second copy in this table would read as authoritative while changing nothing.++ The two capability fns take the whole resolved ref rather than a model string because they do not all+ answer from the model: vLLM answers from what its connect-time probe recorded on the *connection*, and+ fast mode depends on whether the call is proxied. The adapters that do answer from the model name keep a+ small fn of their own so the table holds one shape rather than a per-provider argument list."+ {"anthropic" {:stream #'claude/claude+ :list-models #'claude/list-models+ :supported-models #'claude/supported-models+ :context-window #'claude/context-window-tokens+ :reasoning? #'claude/streams-reasoning?+ :fast-mode? #'claude/supports-fast-mode?}+ "azure" {:stream #'azure/azure+ :list-models #'azure/list-models+ :context-window #'azure/context-window-tokens+ :reasoning? #'azure/streams-reasoning?}+ "bedrock" {:stream #'bedrock/bedrock+ :list-models #'bedrock/list-models+ :supported-models #'bedrock/supported-models+ :context-window #'bedrock/context-window-tokens+ :reasoning? #'bedrock/streams-reasoning?}+ "deepseek" {:stream #'deepseek/deepseek+ :list-models #'deepseek/list-models+ :supported-models #'deepseek/supported-models+ :reasoning? #'deepseek/streams-reasoning?}+ "google" {:stream #'google/google+ :list-models #'google/list-models+ :context-window #'google/context-window-tokens+ :reasoning? #'google/streams-reasoning?}+ "mistral" {:stream #'mistral/mistral+ :list-models #'mistral/list-models+ :supported-models #'mistral/supported-models+ :context-window #'mistral/context-window-tokens+ :reasoning? #'mistral/streams-reasoning?}+ "moonshot" {:stream #'moonshot/moonshot+ :list-models #'moonshot/list-models+ :supported-models #'moonshot/supported-models+ :context-window #'moonshot/context-window-tokens+ :reasoning? #'moonshot/streams-reasoning?}+ "openai" {:stream #'openai/openai+ :list-models #'openai/list-models+ :supported-models #'openai/supported-models+ :context-window #'openai/context-window-tokens+ :reasoning? #'openai/streams-reasoning?}+ "openrouter" {:stream #'openrouter/openrouter+ :list-models #'openrouter/list-models+ :supported-models #'openrouter/supported-models+ :context-window #'openrouter/context-window-tokens+ :reasoning? #'openrouter/streams-reasoning?}+ "vllm" {:stream #'vllm/vllm+ :list-models #'vllm/list-models+ :reasoning? #'vllm/streams-reasoning?}+ "zai" {:stream #'zai/zai+ :list-models #'zai/list-models+ :supported-models #'zai/supported-models+ :context-window #'zai/context-window-tokens+ :reasoning? #'zai/streams-reasoning?}+ ;; the managed connection is served by the wire family its model names, so it has no adapter of its own+ "metabase" {}})++(mu/defn- adapter :- AdapterRow+ "The registry row for `provider`, or a throw naming it — a type with no row is a mistake, not a provider+ that happens to do nothing."+ [provider :- ProviderType]+ (or (get adapters provider)+ (throw (ex-info (str "Unknown LLM provider: " provider)+ {:provider provider}))))++(mu/defn required+ "The `capability` of `provider`, which every provider that serves requests must have.++ No return schema: what a capability yields depends on which one was asked for — a fn for `:stream`, a+ map for `:supported-models`. Naming that would mean a getter per capability rather than one generic+ lookup."+ [provider :- ProviderType+ capability :- Capability]+ (if-let [capability-var (get (adapter provider) capability)]+ @capability-var+ (throw (ex-info (str "LLM provider " provider " has no " capability)+ {:provider provider :capability capability}))))++(mu/defn optional+ "The `capability` of `provider`, or nil where it has none. Still throws for a provider with no row at all:+ a new adapter that forgets to register must fail rather than read as one that simply has no models."+ [provider :- ProviderType+ capability :- Capability]+ (some-> (get (adapter provider) capability) deref))++(mu/defn registered? :- :boolean+ "Whether `provider` has a row. For a caller holding a type that may not name a provider at all, rather+ than one asserting that it does — hence the `:maybe`, since an unresolvable model ref yields no type."+ [provider :- [:maybe ProviderType]]+ (contains? adapters provider))++(mu/defn- model-capability :- :boolean+ "Whether the provider behind `model-ref` answers yes to `capability` for that model. A ref that resolves+ to no provider is a nil answer rather than a mistake, so this asks [[registered?]] first."+ [model-ref :- [:maybe :string]+ capability :- Capability]+ (let [{:keys [type] :as resolved} (llm.provider/resolve-model-ref model-ref)]+ (boolean (when-let [capable? (and (registered? type) (optional type capability))]+ (capable? resolved)))))++(mu/defn streams-reasoning? :- :boolean+ "Whether a model reference names a model that streams its reasoning back to us."+ [model-ref :- [:maybe :string]]+ (model-capability model-ref :reasoning?))++(mu/defn supports-fast-mode? :- :boolean+ "Whether a model reference names a model we can serve in Anthropic fast mode."+ [model-ref :- [:maybe :string]]+ (model-capability model-ref :fast-mode?))++(mu/defn context-window-tokens :- [:maybe :int]+ "The input context window of the model `model-ref` names, or nil where we do not know one.++ Not a [[model-capability]] like the two above: those ask the provider a yes/no about the whole resolved+ ref, while `:context-window` answers a number about the model alone, so a ref that resolves to a+ provider but names no model has nothing to ask about."+ [model-ref :- [:maybe :string]]+ (let [{:keys [type model]} (llm.provider/resolve-model-ref model-ref)]+ (when-let [window-fn (and model (registered? type) (optional type :context-window))]+ (window-fn model))))diff --git a/src/metabase/metabot/self/vllm.clj b/src/metabase/metabot/self/vllm.cljindex ece8f14176b..8130fc9a5ac 100644--- a/src/metabase/metabot/self/vllm.clj+++ b/src/metabase/metabot/self/vllm.clj@@ -9,15 +9,13 @@(:require[clojure.string :as str][metabase.llm.settings :as llm]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json]- [metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]])+ [metabase.util.malli :as mu])(:import(java.io IOException)(java.net SocketTimeoutException)@@ -29,33 +27,42 @@"Smallest `max_model_len` [[preflight!]] will accept. A product floor, not a measurement."16384)-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for vLLM")- {:api-error true- :error-code :proxy-unsupported}))-(defn- missing-base-url-ex [](ex-info (tru "No vLLM base URL is set"){:api-error true:error-code :base-url-missing}))+(defn- vllm-auth+ "vLLM's `:auth`. A server needs a base URL but not a key — one started without `--api-key` is a complete+ configuration — so the map is never nil and [[core/resolve-auth]] cannot reach its `missing-api-key-ex`+ branch."+ [{:keys [slug display-name]} {:keys [credentials ai-proxy?]}]+ (let [base-url (not-empty (:base-url credentials))+ api-key (not-empty (:api-key credentials))]+ (when-not base-url+ (throw (missing-base-url-ex)))+ (core/resolve-auth slug display-name+ (cond-> {:url base-url}+ api-key (assoc :headers {"Authorization" (str "Bearer " api-key)}))+ ai-proxy?)))++(def ^:private provider+ (adapter/provider+ {:slug "vllm"+ :display-name "vLLM"+ :auth vllm-auth+ :error-fallback #(tru "vLLM API error (HTTP {0})" %)+ :errors {400 #(tru "vLLM rejected the request — usually an unsupported schema, or a model that cannot compile the tool grammar")+ 401 #(tru "vLLM API key expired or invalid — check the key your server was started with via --api-key")+ 404 #(tru "vLLM API endpoint was not found — the base URL should end in /v1")+ 429 #(tru "The vLLM server''s request queue is full — reduce concurrent load, or restart it with a larger --max-num-seqs")+ 500 #(tru "vLLM returned an internal server error")}}))+(defn- missing-model-ex [](ex-info (tru "No vLLM model is set"){:api-error true:error-code :model-missing}))-(defn- vllm-error-msg- "Canonical, status-specific vLLM error message."- [res]- (let [status (long (:status res 0))]- (case status- 400 (tru "vLLM rejected the request — usually an unsupported schema, or a model that cannot compile the tool grammar")- 401 (tru "vLLM API key expired or invalid — check the key your server was started with via --api-key")- 404 (tru "vLLM API endpoint was not found — the base URL should end in /v1")- 429 (tru "The vLLM server''s request queue is full — reduce concurrent load, or restart it with a larger --max-num-seqs")- 500 (tru "vLLM returned an internal server error")- (tru "vLLM API error (HTTP {0})" status))))-(def reasoning-config-key"The connection `:config` key [[preflight!]]'s reasoning observation is recorded under. It is not anadmin-entered field: whether a served model reasons depends on the operator's `--reasoning-parser`@@ -69,21 +76,11 @@(let [recorded (get credentials reasoning-config-key)](or (true? recorded) (= "true" recorded))))-(defn- vllm-auth- "Auth map for a vLLM request, built from the connection's credentials alone. The map is never nil, so- `core/resolve-auth` cannot reach its `missing-api-key-ex` branch — a keyless server is a complete- configuration."- [credentials ai-proxy?]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [base-url (not-empty (:base-url credentials))- api-key (not-empty (:api-key credentials))]- (when-not base-url- (throw (missing-base-url-ex)))- (core/resolve-auth "vllm" "vLLM"- (cond-> {:url base-url}- api-key (assoc :headers {"Authorization" (str "Bearer " api-key)}))- ai-proxy?)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. vLLM answers from what its connect-time probe recorded on the connection: the flag+ depends on the operator's `--reasoning-parser` as well as on the model, so the name cannot settle it."+ [{:keys [credentials]} :- adapter/ResolvedRef]+ (reasoning-connection? credentials))(defn- inference-timeouts"Timeouts for a generation request."@@ -91,12 +88,6 @@{:socket-timeout (llm/llm-vllm-request-timeout-ms):connection-timeout (llm/llm-connection-timeout-ms)})-(defn- control-timeouts- "Timeouts for the non-generating `/models` call, on the shared (much shorter) request budget."- []- {:socket-timeout (llm/llm-request-timeout-ms)- :connection-timeout (llm/llm-connection-timeout-ms)})-(def ^:private probe-timeout-ceiling-ms"Upper bound on a single preflight probe, which blocks the admin behind a spinner."120000)@@ -140,25 +131,27 @@A 2xx whose body is not a recognizable catalog fails closed via[[chat-completions/models-catalog]], naming the base URL — the likeliest cause for the one provider- whose base URL the admin types."- [auth]+ whose base URL the admin types.++ No timeouts are passed: this is not a generation, so it rides [[core/request]]'s shared default+ budget rather than vLLM's longer [[inference-timeouts]] one."+ [{{:keys [base-url]} :credentials :as req}](try- (let [res (core/request auth (merge {:method :get- :url "/models"- :as :json- :headers {"Content-Type" "application/json"}}- (control-timeouts)))]- ;; The URL off `auth`, not the setting: a connect verifies request credentials before saving them.+ (let [res (adapter/request! provider (assoc req+ :method :get+ :path "/models"+ :as :json))]+ ;; The URL off the request credentials, not the setting: a connect verifies them before they are saved.(chat-completions/models-catalog"vLLM" res{:detail (tru "Check that {0} is a vLLM server''s OpenAI-compatible API — the base URL should end in /v1."- (str (:url auth)))}))+ (str base-url))}));; Ordered ahead of the generic catch, which a non-2xx still reaches as an `ExceptionInfo`.;; `:as :json` also lands a 2xx whose body is not JSON here, via Jackson's `JsonParseException`.(catch IOException e- (throw (list-models-io-ex e (:url auth))))+ (throw (list-models-io-ex e base-url)))(catch Exception e- (core/rethrow-api-error! "vllm" vllm-error-msg e))))+ (adapter/rethrow! provider e))));;; -------------------------------------------------- Preflight -------------------------------------------------@@ -210,18 +203,19 @@"Run one non-streaming Chat Completions turn against `model` and return the first choice. The`finish_reason` is part of the return value because a generation truncated at[[probe-max-tokens]] and a server that will not call tools both produce empty `tool_calls`."- [auth model tool-choice]- (let [res (core/request auth (merge {:method :post- :url "/chat/completions"- :as :json- :headers {"Content-Type" "application/json"}- :body (json/encode {:model model- :messages probe-messages- :tools [probe-tool]- :tool_choice tool-choice- :temperature 0- :max_tokens probe-max-tokens})}- (probe-timeouts)))]+ [req model tool-choice]+ (let [res (adapter/request! provider+ (assoc req+ :method :post+ :path "/chat/completions"+ :as :json+ :body (json/encode {:model model+ :messages probe-messages+ :tools [probe-tool]+ :tool_choice tool-choice+ :temperature 0+ :max_tokens probe-max-tokens}))+ (probe-timeouts))](get-in res [:body :choices 0])))(defn- check-context-budget!@@ -237,8 +231,8 @@`content` as prose and Metabot chats without ever acting.Returns whether the model emitted reasoning, the only signal anywhere that it is a reasoning model."- [auth model]- (let [{:keys [message finish_reason]} (probe-chat! auth model "auto")+ [req model]+ (let [{:keys [message finish_reason]} (probe-chat! req model "auto")content (str (:content message));; `reasoning` since vLLM 0.26; `reasoning_content` is the deprecated spelling older builds;; and other OpenAI-compatible servers still use.@@ -289,8 +283,8 @@(defn- check-structured-output!"Check that guided decoding works. A different failure from [[check-tool-calling!]]: a model whosegrammar the server cannot compile chats fine but breaks titling and the whole `sql` profile."- [auth model]- (let [{:keys [message finish_reason]} (probe-chat! auth model "required")]+ [req model]+ (let [{:keys [message finish_reason]} (probe-chat! req model "required")](when (empty? (:tool_calls message))(throw (preflight-ex(if (= "length" finish_reason)@@ -342,10 +336,10 @@They run concurrently; deref order fixes the verdict, tool calling being the more actionablediagnosis when a server fails both. The loser is cancelled rather than left generating against theoperator's server long after anyone is listening."- [auth model]+ [req model](try- (let [tool-calling (future (check-tool-calling! auth model))- structured (future (check-structured-output! auth model))]+ (let [tool-calling (future (check-tool-calling! req model))+ structured (future (check-structured-output! req model))](try(let [reasoning? (await-probe! tool-calling)](await-probe! structured)@@ -358,7 +352,7 @@(tru "The vLLM server did not answer the connection test within {0}ms. Check that it is not overloaded — a server this slow to answer a trivial prompt cannot drive Metabot."(str (:socket-timeout (probe-timeouts)))))))(catch Exception e- (core/rethrow-api-error! "vllm" vllm-error-msg e))))+ (adapter/rethrow! provider e))))(defn- preflight!"Exercise the contract the agent loop depends on, against the model that will actually be used, and@@ -368,36 +362,36 @@`:reasoning?` reports whether the probed model streamed reasoning. Only the probe can answer that,and the answer drives which renderer the frontend picks, so the connection records it (see[[reasoning-config-key]])."- [auth entries requested-model]+ [req entries requested-model](let [entry (probe-target entries requested-model)model (:id entry)](check-context-budget! entry){:model model- :reasoning? (run-probes! auth model)}))+ :reasoning? (run-probes! req model)}))-(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the models the connection's vLLM server is serving. Pass-through: there is nothing towhitelist, and `display_name` falls back to the served id.- `:probe?` additionally runs [[preflight!]], and reports what it determined as `:learned-config`,+ `:probe?` additionally runs [[preflight!]], and reports what it determined as `:connection-info`,for the connect path to store on the connection: whether the model reasons, and the model itexercised, which the connect path adopts as the one to run on. Reserved for the connect and editpaths — a tool-call probe on every model listing would stall the admin picker behind a fullprefill. On edit, a `:proposed-model` is re-probed only while the server still advertises it;otherwise the normal candidate selection chooses a replacement."([] (list-models {}))- ([{:keys [credentials ai-proxy? model proposed-model probe?]}]- (let [auth (vllm-auth credentials ai-proxy?)- entries (list-all-models auth)+ ([{:keys [credentials ai-proxy? model proposed-model probe?]} :- adapter/ListOpts]+ (let [req {:credentials credentials :ai-proxy? ai-proxy?}+ entries (list-all-models req)proposed (when (some #(= proposed-model (:id %)) entries)proposed-model)probed (when probe?- (preflight! auth entries (or model proposed)))]+ (preflight! req entries (or model proposed)))](cond-> {:models (mapv (fn [{:keys [id] :as entry}]{:id id :display_name (or (:name entry) id)})entries)}- probed (assoc :learned-config {reasoning-config-key (str (:reasoning? probed))- :probed-model (:model probed)})))))+ probed (assoc :connection-info {reasoning-config-key (str (:reasoning? probed))+ :probed-model (:model probed)})))));;; --------------------------------------------------- Requests -------------------------------------------------@@ -474,40 +468,21 @@Opts map takes `:credentials` (`{:base-url ... :api-key ...}`) from the connection serving thisrequest, and throws without a base URL.`:ai-proxy?` is not supported for vLLM and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))+ [{:keys [model credentials] :as opts} :- core/LLMRequestOpts](when (str/blank? model)(throw (missing-model-ex)))- (let [req (vllm-request-body opts)- timeout-ms (llm/llm-vllm-request-timeout-ms)]- (log/debug "vLLM request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.vllm/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [auth (vllm-auth credentials ai-proxy?)- response (core/request auth- (merge {:method :post- :url "/chat/completions"- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)}- (inference-timeouts)))]- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "vllm"- :model model- :url "/chat/completions"- :request req})- (io-guarded timeout-ms)- (core/reducible-with-api-errors "vllm" vllm-error-msg)))- ;; Ordered: clj-http raises an `IOException` only when there is no response at all, so this- ;; cannot swallow one `vllm-error-msg` would have translated.- (catch IOException e- (throw (request-io-ex e (:base-url credentials) timeout-ms)))- (catch Exception e- (core/rethrow-api-error! "vllm" vllm-error-msg e))))))+ (let [timeout-ms (llm/llm-vllm-request-timeout-ms)]+ (adapter/stream! provider opts+ {:path "/chat/completions"+ :body (vllm-request-body opts)+ :request-options (inference-timeouts)+ :wrap-stream #(io-guarded % timeout-ms)+ ;; clj-http raises an `IOException` only when there is no response at all, so the+ ;; IO branch cannot swallow a failure the provider's own messages would have translated.+ :on-request-error (fn [e]+ (if (instance? IOException e)+ (throw (request-io-ex e (:base-url credentials) timeout-ms))+ (adapter/rethrow! provider e)))})))(defn vllm->aisdk-chunks-xf"Translates vLLM Chat Completions streaming chunks into AI SDK v5 protocol chunks.diff --git a/src/metabase/metabot/self/zai.clj b/src/metabase/metabot/self/zai.cljindex dee411df0db..887a69b705d 100644--- a/src/metabase/metabot/self/zai.clj+++ b/src/metabase/metabot/self/zai.clj@@ -5,34 +5,25 @@https://docs.z.ai/api-reference/llm/chat-completion"(:require+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as core]- [metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.util.i18n :refer [tru]]- [metabase.util.json :as json]- [metabase.util.log :as log]- [metabase.util.malli :as mu]- [metabase.util.o11y :refer [with-span]]))+ [metabase.util.malli :as mu]))(set! *warn-on-reflection* true)(def ^:private default-model "glm-5.2")-(defn- ai-proxy-unsupported-ex []- (ex-info (tru "AI proxy is not supported for Z.AI")- {:api-error true- :error-code :proxy-unsupported}))--(defn- zai-error-msg- "Canonical, status-specific Z.AI error message."- [res]- (let [status (long (:status res 0))]- (case status- 401 (tru "Z.AI API key expired or invalid")- 404 (tru "Z.AI API endpoint was not found — check the base URL")- 429 (tru "Z.AI has rate limited us")- 500 (tru "Z.AI returned an internal server error")- (tru "Z.AI API error (HTTP {0})" status))))+(def ^:private provider+ (adapter/provider+ {:slug "zai"+ :display-name "Z.AI"+ :error-fallback #(tru "Z.AI API error (HTTP {0})" %)+ :errors {401 #(tru "Z.AI API key expired or invalid")+ 404 #(tru "Z.AI API endpoint was not found — check the base URL")+ 429 #(tru "Z.AI has rate limited us")+ 500 #(tru "Z.AI returned an internal server error")}}))(def supported-models"Z.AI models offered in the Metabot model picker, keyed by model id.@@ -43,16 +34,11 @@{"glm-5.3" {:display-name "GLM-5.3" :context-window 1048576 :thinking-only? true}"glm-5.2" {:display-name "GLM-5.2" :context-window 1048576}})-(defn context-window-tokens+(mu/defn context-window-tokens :- [:maybe :int]"The input context window for `model`, or nil when it isn't one we know."- [model]+ [model :- [:maybe :string]](get-in supported-models [model :context-window]))-(defn- supported-model?- "Whether a `/models` catalog entry is one of the [[supported-models]]."- [{:keys [id]}]- (contains? supported-models id))-(defn reasoning-model?"Whether `model` streams reasoning back to us.@@ -61,6 +47,11 @@[model](contains? supported-models (str model)))+(mu/defn streams-reasoning? :- :boolean+ "Registry capability. Z.AI answers from the model name."+ [{:keys [model]} :- adapter/ResolvedRef]+ (reasoning-model? model))+(defn- thinking-only-model?"Whether `model` rejects `thinking {:type \"disabled\"}` outright.@@ -71,42 +62,16 @@[model](boolean (get-in supported-models [(str model) :thinking-only?])))-(defn- list-all-models- "Fetch the full Z.AI model catalog (`GET /models`).-- The endpoint is OpenAI-compatible but undocumented; it doubles as the credential- round-trip behind the admin Connect button (auth is checked before routing, so a 2xx- proves the key and base URL reach an authenticated surface). A 2xx whose body isn't a- recognizable catalog throws rather than yielding an empty picker — see- [[chat-completions/models-catalog]].- `:ai-proxy?` is not supported for Z.AI and throws when true."- [{:keys [credentials ai-proxy?]}]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (try- (let [auth (core/resolve-auth "zai" "Z.AI"- (when-let [k (not-empty (:api-key credentials))]- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " k)}})- ai-proxy?)- res (core/request auth {:method :get- :url "/models"- :as :json- :headers {"Content-Type" "application/json"}})]- (chat-completions/models-catalog "Z.AI" res))- (catch Exception e- (core/rethrow-api-error! "zai" zai-error-msg e))))--(defn list-models+(mu/defn list-models :- adapter/ModelListing"List the Z.AI models supported by this adapter (see [[supported-models]]).++ The `/models` catalog it intersects is OpenAI-compatible but undocumented; it doubles as the credential+ round-trip behind the admin Connect button (auth is checked before routing, so a 2xx proves the key and+ base URL reach an authenticated surface).`:ai-proxy?` is not supported for Z.AI and throws when true."([] (list-models {}))- ([opts]- {:models (->> (list-all-models opts)- (filter supported-model?)- (sort-by :id)- (mapv (fn [{:keys [id] :as model}]- {:id id :display_name (or (:name model) (get-in supported-models [id :display-name]))})))}))+ ([opts :- adapter/ListOpts]+ (adapter/model-listing supported-models (adapter/fetch-catalog provider opts) :name)))(def ^:private forced-tool-call-token-floor"Smallest `max_tokens` a forced tool call on a thinking-only model may be capped at.@@ -160,36 +125,12 @@Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, andthrows when they are missing.`:ai-proxy?` is not supported for Z.AI and throws when true."- [{:keys [model tools credentials ai-proxy?] :as opts+ [{:keys [model] :as opts:or {model default-model}} :- core/LLMRequestOpts]- (when ai-proxy?- (throw (ai-proxy-unsupported-ex)))- (let [req (zai-request-body (assoc opts :model model))]- (log/debug "Z.AI request" {:model model :msg-count (count (:messages req)) :tools (count (or tools []))})- (with-span :info {:name :metabot.zai/request- :model model- :msg-count (count (:messages req))- :tool-count (count (or tools []))}- (try- (let [api-key (not-empty (:api-key credentials))- auth (core/resolve-auth "zai" "Z.AI"- (when api-key- {:url (:base-url credentials)- :headers {"Authorization" (str "Bearer " api-key)}})- ai-proxy?)- response (core/request auth- {:method :post- :url "/chat/completions"- :as :stream- :headers {"Content-Type" "application/json"}- :body (json/encode req)})]- (-> (core/sse-reducible (:body response))- (debug/capture-stream {:provider "zai"- :model model- :url "/chat/completions"- :request req})))- (catch Exception e- (core/rethrow-api-error! "zai" zai-error-msg e))))))+ (let [opts (assoc opts :model model)]+ (adapter/stream! provider opts+ {:path "/chat/completions"+ :body (zai-request-body opts)})))(def ^:private stop-reasons"Z.AI signals a filtered response with `sensitive` rather than OpenAI's `content_filter`, and reports an upstreamdiff --git a/src/metabase/metabot/settings.clj b/src/metabase/metabot/settings.cljindex 1bc01b61fd7..e10cceab98c 100644--- a/src/metabase/metabot/settings.clj+++ b/src/metabase/metabot/settings.clj@@ -3,8 +3,8 @@[clojure.string :as str][metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm.settings]- [metabase.metabot.self.catalog :as catalog][metabase.metabot.self.google :as google]+ [metabase.metabot.self.registry :as registry][metabase.settings.core :as setting :refer [defsetting]][metabase.util.i18n :refer [deferred-tru tru]][metabase.util.log :as log]))@@ -245,7 +245,7 @@:visibility :public:setter :none:export? false- :getter #(catalog/streams-reasoning? (llm-metabot-provider))+ :getter #(registry/streams-reasoning? (llm-metabot-provider)):doc false)(defsetting llm-metabot-supports-fast-mode?@@ -256,7 +256,7 @@:visibility :settings-manager:setter :none:export? false- :getter #(catalog/supports-fast-mode? (llm-metabot-provider))+ :getter #(registry/supports-fast-mode? (llm-metabot-provider)):doc false)(defsetting llm-fast-modediff --git a/test/metabase/llm/api/provider_test.clj b/test/metabase/llm/api/provider_test.cljindex f19a50638bb..d3f2b3290f7 100644--- a/test/metabase/llm/api/provider_test.clj+++ b/test/metabase/llm/api/provider_test.clj@@ -293,10 +293,10 @@(mt/with-dynamic-fn-redefs [metabot.self/list-models(fn [_provider o](reset! opts o)- {:models [{:id "vllm-test" :display_name "vllm-test"}- {:id "other" :display_name "other"}]- :learned-config {:model-reasoning "true"- :probed-model "vllm-test"}})]+ {:models [{:id "vllm-test" :display_name "vllm-test"}+ {:id "other" :display_name "other"}]+ :connection-info {:model-reasoning "true"+ :probed-model "vllm-test"}})](mt/with-temporary-setting-values [llm-providers []](mt/with-temporary-raw-setting-values [llm-metabot-provider nil](is (=? {:key "vllm"@@ -341,6 +341,18 @@:config {:base-url "http://vllm.internal:8000/v1"}}))))(is (= [] (llm.provider/connections)))))))+(deftest create-rejects-a-malformed-model-catalog-test+ (testing (str "a 2xx whose body is not a model list means the base URL reached something that is not the API. "+ "Failing closed is only useful if the admin sees why, so it comes back as the adapter's message "+ "on the form — not as the 500 an untagged error would produce, which `MB_HIDE_STACKTRACES=true` "+ "would collapse to \"Something went wrong\".")+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:object "list"}})]+ (mt/with-temporary-setting-values [llm-providers []]+ (is (= "Anthropic returned an unexpected model list response"+ (:message (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-ant-nope"}}))))+ (is (= [] (llm.provider/connections)))))))+(deftest models-listing-does-not-probe-test(testing "listing models is a page load; only a write may spend a generation on the operator's server"(let [opts (atom nil)]@@ -361,9 +373,9 @@(mt/with-dynamic-fn-redefs [metabot.self/list-models(fn [_provider o](reset! opts o)- {:models [{:id "served-a" :display_name "served-a"}]- :learned-config {:model-reasoning "false"- :probed-model "served-b"}})]+ {:models [{:id "served-a" :display_name "served-a"}]+ :connection-info {:model-reasoning "false"+ :probed-model "served-b"}})](mt/with-temporary-setting-values [llm-providers [(connection "vllm" "vllm"{:base-url "http://old.internal:8000/v1":model-reasoning "true"})]]@@ -387,9 +399,9 @@(when model(throw (ex-info "The vLLM server is not serving Qwen/Qwen3-8B. It is serving: Qwen/Qwen3-32B."{:api-error true :status-code 400})))- {:models [{:id "Qwen/Qwen3-32B" :display_name "Qwen/Qwen3-32B"}]- :learned-config {:model-reasoning "false"- :probed-model "Qwen/Qwen3-32B"}})]+ {:models [{:id "Qwen/Qwen3-32B" :display_name "Qwen/Qwen3-32B"}]+ :connection-info {:model-reasoning "false"+ :probed-model "Qwen/Qwen3-32B"}})](mt/with-temporary-setting-values [llm-providers [(connection "vllm" "vllm"{:base-url "http://old.internal:8000/v1":model-reasoning "false"@@ -413,8 +425,8 @@(mt/with-dynamic-fn-redefs [metabot.self/list-models(fn [_provider o](reset! opts o)- {:models [{:id "google/gemini-3.5-flash" :display_name "Gemini 3.5 Flash"}]- :learned-config {:probed-model (:model o)}})]+ {:models [{:id "google/gemini-3.5-flash" :display_name "Gemini 3.5 Flash"}]+ :connection-info {:probed-model (:model o)}})](mt/with-temporary-setting-values [llm-providers [(connection "google" "google"{:oauth-access-token "ya29.token":project-id "my-project"@@ -1270,8 +1282,8 @@(deftest create-records-the-model-the-probe-verified-test(testing "connecting Google against a partner model records it, so the listing that follows probes it too"(mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [model]}]- {:models []- :learned-config {:probed-model model}})]+ {:models []+ :connection-info {:probed-model model}})](mt/with-temporary-setting-values [llm-providers []](mt/user-http-request :crowberto :post 200 "llm/providers"{:type "google"diff --git a/test/metabase/metabot/self/adapter_test.clj b/test/metabase/metabot/self/adapter_test.cljnew file mode 100644index 00000000000..6d3b550ba62--- /dev/null+++ b/test/metabase/metabot/self/adapter_test.clj@@ -0,0 +1,335 @@+(ns metabase.metabot.self.adapter-test+ (:require+ [clj-http.client :as http]+ [clojure.edn :as edn]+ [clojure.string :as str]+ [clojure.test :refer [deftest is testing]]+ [medley.core :as m]+ [metabase.metabot.self.adapter :as adapter]+ [metabase.metabot.self.azure :as azure]+ [metabase.metabot.self.bedrock :as bedrock]+ [metabase.metabot.self.claude :as claude]+ [metabase.metabot.self.core :as self.core]+ [metabase.metabot.self.debug :as debug]+ [metabase.metabot.self.deepseek :as deepseek]+ [metabase.metabot.self.google :as google]+ [metabase.metabot.self.mistral :as mistral]+ [metabase.metabot.self.moonshot :as moonshot]+ [metabase.metabot.self.openai :as openai]+ [metabase.metabot.self.openrouter :as openrouter]+ [metabase.metabot.self.registry :as registry]+ [metabase.metabot.self.vllm :as vllm]+ [metabase.metabot.self.zai :as zai]+ [metabase.premium-features.core :as premium-features]+ [metabase.test :as mt]+ [metabase.util.malli.registry :as mr]))++(set! *warn-on-reflection* true)++;;; ──────────────────────────────────────────────────────────────────+;;; Descriptor+;;; ──────────────────────────────────────────────────────────────────++(def ^:private expected-spans+ "The request span each adapter's descriptor should carry, spelled out rather than recomputed from+ `:slug`, so that renaming a slug shows up here as a failing test rather than as silently renamed+ telemetry."+ {#'azure/provider :metabot.azure/request+ #'bedrock/provider :metabot.bedrock/request+ ;; Anthropic's span follows its slug, not the `claude` namespace the adapter lives in — the same+ ;; `anthropic` the debug capture and the error translation tag these requests with.+ #'claude/provider :metabot.anthropic/request+ #'deepseek/provider :metabot.deepseek/request+ #'google/provider :metabot.google/request+ #'mistral/provider :metabot.mistral/request+ #'moonshot/provider :metabot.moonshot/request+ #'openai/provider :metabot.openai/request+ #'openrouter/provider :metabot.openrouter/request+ #'vllm/provider :metabot.vllm/request+ #'zai/provider :metabot.zai/request})++(deftest ^:parallel span-name-test+ (testing "every adapter names its request span for its provider slug, with no per-adapter override"+ (doseq [[provider-var expected] expected-spans]+ (is (= expected (:span @provider-var))+ (str provider-var)))))++(def ^:private expected-fallback-messages+ "What each adapter renders for an HTTP status it has no specific message for.++ English only. Each adapter declares its own `:error-fallback` rather than sharing one parameterised+ msgid, so these strings keep the translations already shipped for them in `locales/*.po`; the msgids+ themselves are guarded by review and by the extractor (`clojure -X:build i18n.enumerate/enumerate`),+ not by this test, which only pins the rendered English."+ {#'azure/provider "Azure API error (HTTP 418)"+ #'bedrock/provider "AWS Bedrock API error (HTTP 418)"+ #'claude/provider "Anthropic API error (HTTP 418)"+ #'deepseek/provider "DeepSeek API error (HTTP 418)"+ #'google/provider "Google API error (HTTP 418)"+ #'mistral/provider "Mistral API error (HTTP 418)"+ #'moonshot/provider "Moonshot API error (HTTP 418)"+ #'openai/provider "OpenAI API error (HTTP 418)"+ #'openrouter/provider "OpenRouter API error (HTTP 418)"+ #'vllm/provider "vLLM API error (HTTP 418)"+ #'zai/provider "Z.AI API error (HTTP 418)"})++(deftest ^:parallel error-message-test+ (testing "a status with no specific message falls back to naming the provider and the status"+ (doseq [[provider-var expected] expected-fallback-messages+ :let [render (:error-msg @provider-var)]]+ (is (= expected (render {:status 418}))+ (str provider-var))))+ (testing "a status the provider does have a message for gets that one instead"+ (is (= "Anthropic API key expired or invalid"+ ((:error-msg @#'claude/provider) {:status 401}))))+ (testing "a response with no status at all still renders"+ (is (= "Anthropic API error (HTTP 0)"+ ((:error-msg @#'claude/provider) {})))))++(deftest every-descriptor-brings-its-own-translated-messages-test+ (testing "each adapter declares its own `:error-fallback` rather than inheriting the shared+ parameterised template, which ships no translations. The rendered English is identical either+ way, so `error-message-test` cannot tell the two apart — this can"+ (doseq [provider-var (keys expected-spans)]+ (is (fn? (:error-fallback @provider-var)) (str provider-var))))+ (testing "the proxy refusal deliberately does not get the same treatment: it stays one shared msgid,+ because `:ai-proxy?` is only ever set for the managed connection, whose catalog names only+ Anthropic models — the one provider the proxy can serve — so the refusal is unreachable"+ (is (= "AI proxy is not supported for Z.AI"+ (try (adapter/reject-ai-proxy! @#'zai/provider true)+ (catch clojure.lang.ExceptionInfo e (ex-message e)))))))++(defn- pinned-slugs+ "The provider slugs a spelled-out table above covers."+ [table]+ (set (map (comp :slug deref) (keys table))))++(defn- adapter-slugs+ "Every provider the registry serves with an adapter of its own. The managed connection is served by the+ wire family its model names, so it has no descriptor to pin."+ []+ (disj (set (keys @#'registry/adapters)) "metabase"))++(deftest ^:parallel descriptor-tables-cover-every-adapter-test+ (testing "a new adapter cannot skip the span guard by being left out of the table"+ (is (= (adapter-slugs) (pinned-slugs expected-spans))))+ (testing "a new adapter cannot skip the error-message guard either"+ (is (= (adapter-slugs) (pinned-slugs expected-fallback-messages)))))++;;; ──────────────────────────────────────────────────────────────────+;;; Request counts+;;; ──────────────────────────────────────────────────────────────────++(defn- tool [n]+ {:tool-name (str "tool-" n)+ :doc "A tool."+ :schema [:=> [:cat [:map {:closed true} [:x :string]]] :any]+ :fn (fn [_] "ok")})++(defn- streamed-request!+ "Run `thunk` with streaming stubbed to the identity chain, so the adapter's `stream!` hands back the+ clj-http request map it would have sent."+ [thunk]+ (with-redefs [self.core/sse-reducible identity+ self.core/reducible-with-api-errors (fn [r _ _] r)+ debug/capture-stream (fn [r _] r)+ http/request (fn [req] {:body req})]+ (thunk)))++(defn- logged-span-data!+ "Run `thunk` with HTTP and streaming stubbed out, and return the data [[adapter/stream!]]'s `with-span`+ line logged for it, as a map.++ The log rather than the span: `u.o11y/with-span` renders its whole map into the line but hands only+ `:attributes` to clj-otel, so these keys reach the log and nothing else (BOT-2168). The line is+ `\"<span-name> (<ms>ms) <map>\"`, so the map is everything from the first brace on."+ [thunk]+ (let [messages (mt/with-log-messages-for-level [messages [metabase.metabot.self.adapter :info]]+ (streamed-request! thunk)+ (mapv :message (messages)))+ line (m/find-first #(re-find #"^:metabot\.[a-z]+/request " %) messages)]+ (some-> line (subs (str/index-of line "{")) edn/read-string)))++(defn- request-log-data!+ "[[logged-span-data!]] for a Claude request."+ [opts]+ (logged-span-data!+ #(claude/claude-raw (merge {:model "claude-haiku-4-5"+ :credentials {:api-key "sk-ant-test"+ :base-url "https://api.anthropic.com"}}+ opts))))++(defn- captured-counts!+ "The `:msg-count` / `:tool-count` [[adapter/stream!]] reported for `opts`."+ [opts]+ (select-keys (request-log-data! opts) [:msg-count :tool-count]))++(deftest request-shape-reaches-the-log-test+ (testing "the model and the counts stay flat keys, which is what `with-span` renders into its line;+ nesting them under `:attributes` would reach the trace but bury them in the log"+ (is (= {:model "claude-haiku-4-5" :msg-count 1 :tool-count 0}+ (-> (request-log-data! {:input [{:role :user :content "hi"}]})+ (select-keys [:model :msg-count :tool-count]))))))++(deftest span-attrs-values-are-strings-test+ (testing "`:family` is stringified at the call site, like every other keyword-valued attribute in the+ codebase (`(name driver)`, `(name context)`, ...). A bare keyword renders `:anthropic` in the+ log, and once BOT-2168 hands this map to clj-otel it would export as \":anthropic\" — clj-otel+ `str`s attribute values, so the colon would survive into the trace"+ (doseq [[model expected] [["anthropic/my-deployment" "anthropic"]+ ["openai/my-deployment" "openai"]]]+ (testing model+ (is (= expected+ (:family (logged-span-data!+ #(azure/azure-raw {:model model+ :input [{:role :user :content "hi"}]+ :credentials {:api-key "az-test"+ :base-url "https://r.services.ai.azure.com"}})))))))))++(deftest counts-describe-the-callers-request-test+ (testing "tool-count is the tools the caller offered, not the tools that reach the wire"+ ;; a structured-output request replaces the whole tool array with one synthetic `structured_output`+ ;; tool, so counting the composed body reported 1 however many tools the caller passed+ (is (= {:msg-count 1 :tool-count 15}+ (captured-counts! {:input [{:role :user :content "hi"}]+ :tools (mapv tool (range 15))+ :schema {:type "object" :properties {}}}))))+ (testing "msg-count is the caller's AISDK parts, not the messages the dialect merged them into"+ ;; `parts->claude-messages` collapses consecutive assistant parts into one wire message+ (is (= {:msg-count 3 :tool-count 0}+ (captured-counts! {:input [{:role :user :content "hi"}+ {:type :text :text "one"}+ {:type :text :text "two"}]})))))++(deftest bearer-auth-under-the-proxy-conforms-to-the-auth-schema-test+ (testing "`bearer-auth` carries a `:- Auth` return schema, and `resolve-auth` adds `:network-policy-floor`+ when the proxy URL comes from the environment. `Auth` is closed, so an undeclared floor would+ fail output validation the first time a `bearer-auth` provider supported the proxy — which no+ provider does today, so nothing else exercises this"+ (mt/with-premium-features #{:metabot-v3}+ (mt/with-temp-env-var-value! [mb-llm-proxy-base-url "http://proxy.internal/"]+ (mt/with-dynamic-fn-redefs [premium-features/premium-embedding-token (constantly "proxy-token")]+ (let [proxyable (adapter/provider {:slug "deepseek" :display-name "DeepSeek" :supports-ai-proxy? true})+ auth (adapter/bearer-auth proxyable {:method :get :path "/models" :ai-proxy? true})]+ (is (= :allow-private (:network-policy-floor auth)))+ (is (nil? (mr/explain adapter/Auth auth)))))))))++;;; ──────────────────────────────────────────────────────────────────+;;; Descriptor headers+;;; ──────────────────────────────────────────────────────────────────++(deftest descriptor-headers-reach-the-stream-test+ (testing "a provider's `:headers` are on its streaming request — the only thing that puts them there"+ (testing "Anthropic's API version"+ (is (=? {:method :post+ :url "https://api.anthropic.com/v1/messages"+ :headers {"anthropic-version" "2023-06-01"+ "Content-Type" "application/json"}}+ (streamed-request!+ #(claude/claude-raw {:model "claude-haiku-4-5"+ :input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-ant-test"+ :base-url "https://api.anthropic.com"}})))))+ (testing "OpenRouter's attribution headers, which its account activity page reads"+ (is (=? {:method :post+ :url "https://openrouter.ai/api/v1/chat/completions"+ :headers {"HTTP-Referer" "https://metabase.com"+ "X-Title" "Metabase"+ "Content-Type" "application/json"}}+ (streamed-request!+ #(openrouter/openrouter-raw {:model "anthropic/claude-haiku-4.5"+ :input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-or-test"+ :base-url "https://openrouter.ai/api"}})))))))++(deftest descriptor-headers-merge-with-per-request-headers-test+ (testing "a per-request header joins the descriptor's rather than replacing them"+ ;; Claude's fast mode adds `anthropic-beta`; dropping `anthropic-version` alongside it would 400+ (is (=? {:headers {"anthropic-version" "2023-06-01"+ "anthropic-beta" string?}}+ (streamed-request!+ #(claude/claude-raw {:model "claude-opus-4-8"+ :input [{:role :user :content "hi"}]+ :fast? true+ :credentials {:api-key "sk-ant-test"+ :base-url "https://api.anthropic.com"}}))))))++(deftest request-defaults-to-json-for-a-body-test+ (let [seen (atom nil)+ p (adapter/provider {:slug "deepseek" :display-name "DeepSeek"})+ call (fn [req]+ (with-redefs [http/request (fn [r] (reset! seen r) {:status 200 :body {}})]+ (adapter/request! p (merge {:credentials {:api-key "sk-test"+ :base-url "https://api.deepseek.test"}}+ req))))]+ (testing "a request with a body is JSON, so no call site has to say so — every body any adapter+ sends is JSON, and Bedrock's signing assumes it"+ (call {:method :post :path "/v1/messages" :body "{}"})+ (is (= "application/json" (get (:headers @seen) "Content-Type"))))+ (testing "a bodyless GET sends none: a provider's model listing has nothing to describe"+ (call {:method :get :path "/models"})+ (is (not (contains? (:headers @seen) "Content-Type"))))+ (testing "and a call site can still override it"+ (call {:method :post :path "/v1/messages" :body "{}"+ :headers {"Content-Type" "application/x-ndjson"}})+ (is (= "application/x-ndjson" (get (:headers @seen) "Content-Type"))))))++(deftest descriptor-headers-reach-the-catalog-test+ (testing "a provider's `:headers` are on its catalog request too, not just its stream"+ (let [seen (atom nil)]+ (with-redefs [http/request (fn [req]+ (reset! seen req)+ {:status 200 :body {:data []}})]+ (claude/list-models {:credentials {:api-key "sk-ant-test" :base-url "https://api.anthropic.com"}}))+ (is (=? {:method :get+ :url "https://api.anthropic.com/v1/models"+ :headers {"anthropic-version" "2023-06-01"}}+ @seen)))))++;;; ──────────────────────────────────────────────────────────────────+;;; Listing+;;; ──────────────────────────────────────────────────────────────────++(def ^:private allow-list+ {"b-model" {:display-name "Allow-list B"}+ "a-model" {:display-name "Allow-list A"}})++(deftest listing-test+ (let [entries [{:id "b-model" :name "Catalog B name" :display_name "Catalog B display_name"}+ {:id "a-model" :name "Catalog A name" :display_name "Catalog A display_name"}+ {:id "unlisted" :name "Catalog U"}]]+ (testing "keeps only allow-listed ids, sorted by id"+ (is (= ["a-model" "b-model"]+ (mapv :id (:models (adapter/model-listing allow-list entries))))))+ (testing "with no catalog-name-key the allow-list names win, so a catalog that starts carrying names cannot rename a model"+ (is (= ["Allow-list A" "Allow-list B"]+ (mapv :display_name (:models (adapter/model-listing allow-list entries))))))+ (testing "a provider reads exactly the field it names, and is unaffected by the other being present"+ (is (= ["Catalog A name" "Catalog B name"]+ (mapv :display_name (:models (adapter/model-listing allow-list entries :name)))))+ (is (= ["Catalog A display_name" "Catalog B display_name"]+ (mapv :display_name (:models (adapter/model-listing allow-list entries :display_name))))))+ (testing "an entry missing that field falls back to the allow-list rather than the other field"+ (is (= ["Allow-list A"]+ (mapv :display_name+ (:models (adapter/model-listing allow-list+ [{:id "a-model" :display_name "Catalog A display_name"}]+ :name))))))))++(deftest catalog-name-key-matches-each-providers-catalog-test+ (testing "each provider reads the field its own catalog documents"+ ;; Anthropic sends `display_name`; OpenRouter and Z.AI send `name`. Guessing between them rather+ ;; than letting each provider name its own field would flip Anthropic's preference the day its+ ;; catalog grows a `name` field.+ (let [names (fn [list-models-fn body]+ (with-redefs [http/request (fn [_] {:status 200 :body body})]+ (mapv :display_name (:models (list-models-fn {:credentials {:api-key "k" :base-url "https://x"}})))))]+ (is (= ["From display_name"]+ (names claude/list-models+ {:data [{:id "claude-sonnet-5" :name "From name" :display_name "From display_name"}]})))+ (is (= ["From name"]+ (names openrouter/list-models+ {:data [{:id "anthropic/claude-sonnet-4.5" :name "From name" :display_name "From display_name"}]})))+ (is (= ["From name"]+ (names zai/list-models+ {:data [{:id "glm-5.2" :name "From name" :display_name "From display_name"}]}))))))diff --git a/test/metabase/metabot/self/azure_test.clj b/test/metabase/metabot/self/azure_test.cljindex 6c778872eaa..04e9e20de86 100644--- a/test/metabase/metabot/self/azure_test.clj+++ b/test/metabase/metabot/self/azure_test.clj@@ -4,9 +4,11 @@[clojure.string :as str][clojure.test :refer :all][metabase.llm.settings :as llm.settings]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.self.azure :as azure][metabase.metabot.self.core :as self.core][metabase.metabot.self.debug :as debug]+ [metabase.metabot.settings :as metabot.settings][metabase.test :as mt][metabase.util.json :as json]))@@ -86,6 +88,17 @@:base-url "https://my-resource.services.ai.azure.com/anthropic"}:model "anthropic/claude-sonnet-4-5"}))))))+(deftest list-models-leaves-the-candidate-model-to-its-caller-test+ (testing "with no model there is no family to pick a surface for, so nothing is probed"+ ;; `metabase.llm.api.provider` resolves the model — from the connection's own `:model-fields`, then+ ;; from what the setting names for *that* connection — so this namespace must not fall back to the+ ;; setting itself, which would be both a duplicate and broader than that.+ (llm.tu/with-connections [(llm.tu/connection "azure" {:api-key "saved-key" :base-url test-base-url})]+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"]+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (= {:models []}+ (azure/list-models {:credentials {:api-key "saved-key" :base-url test-base-url}}))))))))+(deftest list-models-skips-validation-without-any-model-test(testing "without a candidate model there is no surface to probe"(with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]@@ -251,7 +264,13 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"AI proxy is not supported for Azure"- (azure/list-models {:model "openai/gpt-4.1-mini" :ai-proxy? true}))))))+ (azure/list-models {:model "openai/gpt-4.1-mini" :ai-proxy? true})))))+ (testing "ai-proxy? throws even with no model, where there is no request to refuse it"+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AI proxy is not supported for Azure"+ (azure/list-models {:ai-proxy? true}))))))(deftest azure-raw-forwards-credentials-test(testing "credentials passed to azure-raw reach the request, without requiring saved settings"diff --git a/test/metabase/metabot/self/bedrock_test.clj b/test/metabase/metabot/self/bedrock_test.cljindex 840d110ec88..382ce491f0d 100644--- a/test/metabase/metabot/self/bedrock_test.clj+++ b/test/metabase/metabot/self/bedrock_test.clj@@ -37,18 +37,18 @@:secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY":region "us-east-1"})-(deftest ^:parallel supported-model?-test+(deftest ^:parallel supported-models-test(testing "whitelisted models are supported"(doseq [id ["anthropic.claude-fable-5" "anthropic.claude-opus-5" "anthropic.claude-opus-4-8""anthropic.claude-sonnet-5" "openai.gpt-5.5" "openai.gpt-6-astra"]]- (is (true? (#'bedrock/supported-model? {:id id})) id)))+ (is (contains? bedrock/supported-models id) id)))(testing "non-whitelisted models are not supported, even for supported vendors"(doseq [id ["anthropic.claude-3-5-sonnet" "openai.gpt-oss-120b""qwen.qwen3-next-80b-a3b-instruct" "deepseek.v3.2"]]- (is (false? (#'bedrock/supported-model? {:id id})) id))))+ (is (not (contains? bedrock/supported-models id)) id))))(deftest list-models-filters-to-whitelist-test- (mt/with-dynamic-fn-redefs [bedrock/list-all-models (constantly fake-catalog)]+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:data fake-catalog}})](testing "only whitelisted models survive sorted by id"(is (= {:models [{:id "anthropic.claude-fable-5" :display_name "Claude Fable 5"}{:id "anthropic.claude-haiku-4-5" :display_name "Claude Haiku 4.5"}@@ -59,21 +59,32 @@(deftest list-models-filters-unavailable-models-test(mt/with-dynamic-fn-redefs- [bedrock/list-all-models+ [http/request(constantly- [{:id "anthropic.claude-fable-5"- :object "model"- :status "unavailable"- :status_reason "This model is not available under data retention mode 'default'."- :data_retention {:allowed_modes ["provider_data_share"] :mode "default" :source "model_default"}}- {:id "anthropic.claude-sonnet-5"- :object "model"- :status "available"- :data_retention {:allowed_modes ["default" "provider_data_share" "none"] :mode "default" :source "model_default"}}])]+ {:status 200+ :body+ {:data+ [{:id "anthropic.claude-fable-5"+ :object "model"+ :status "unavailable"+ :status_reason "This model is not available under data retention mode 'default'."+ :data_retention {:allowed_modes ["provider_data_share"] :mode "default" :source "model_default"}}+ {:id "anthropic.claude-sonnet-5"+ :object "model"+ :status "available"+ :data_retention {:allowed_modes ["default" "provider_data_share" "none"] :mode "default" :source "model_default"}}]}})](testing "whitelisted models whose catalog status is not \"available\" are excluded"(is (= {:models [{:id "anthropic.claude-sonnet-5" :display_name "Claude Sonnet 5"}]}(bedrock/list-models {:credentials credentials}))))))+(deftest list-models-malformed-catalog-throws-test+ (testing "a 2xx whose body carries no model list throws instead of reporting an empty catalog"+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:object "list"}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AWS Bedrock returned an unexpected model list response"+ (bedrock/list-models {:credentials credentials}))))))+(deftest list-models-missing-credentials-uses-default-chain-test(testing "a connection with no credentials signs with the AWS default chain rather than picking up the single-provider settings"(mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.cljindex 85f2a5d09ad..47707a04c9b 100644--- a/test/metabase/metabot/self/claude_test.clj+++ b/test/metabase/metabot/self/claude_test.clj@@ -747,7 +747,7 @@:headers {"anthropic-version" "2023-06-01""x-api-key" "sk-ant-byok"}}req))- {:body "{\"data\":[]}"})]+ {:status 200 :body {:data []}})](is (= {:models []}(claude/list-models {:credentials byok-credentials})))))(testing "Uses ai proxy when explicitly requested"@@ -757,7 +757,7 @@:headers {"anthropic-version" "2023-06-01""x-metabase-instance-token" "proxy-token"}}req))- {:body "{\"data\":[]}"})]+ {:status 200 :body {:data []}})](is (= {:models []}(claude/list-models {:ai-proxy? true})))))(testing "Does not fall back to ai proxy when the connection carries no key"@@ -794,7 +794,7 @@(mt/with-dynamic-fn-redefs [http/request (fn [req](is (=? {:headers {"x-api-key" "sk-ant-explicit"}}req))- {:body "{\"data\":[]}"})]+ {:status 200 :body {:data []}})](is (= {:models []}(claude/list-models {:credentials {:api-key "sk-ant-explicit"}})))))))@@ -814,22 +814,23 @@#"No Anthropic API key is set"(claude/list-models {:credentials {:api-key ""}}))))))-(deftest ^:parallel supported-model?-test+(deftest ^:parallel supported-models-test(testing "whitelisted models are supported"(doseq [id ["claude-fable-5" "claude-opus-5" "claude-opus-4-8" "claude-sonnet-5" "claude-haiku-4-5-20251001"]]- (is (true? (#'claude/supported-model? {:id id})) id)))+ (is (contains? claude/supported-models id) id)))(testing "non-whitelisted models are not supported"(doseq [id ["claude-3-5-sonnet-20241022" "claude-opus-4-0" "claude-sonnet-4-20250514"]]- (is (false? (#'claude/supported-model? {:id id})) id))))+ (is (not (contains? claude/supported-models id)) id))))(deftest list-models-filters-catalog-to-whitelist-test(testing "list-models keeps only whitelisted models sorted by id, preserving display_name"+ ;; the catalog request is `:as :json`, so clj-http hands the adapter an already-decoded body(with-redefs [http/request (fn [_]- {:body (json/encode- {:data [{:id "claude-sonnet-5" :display_name "Claude Sonnet 5" :created_at "2026-01-01"}- {:id "claude-opus-4-8" :display_name "Claude Opus 4.8" :created_at "2026-02-01"}- {:id "claude-3-5-sonnet-20241022" :display_name "Claude 3.5" :created_at "2024-10-22"}- {:id "claude-fable-5" :display_name "Claude Fable 5" :created_at "2026-03-01"}]})})]+ {:status 200+ :body {:data [{:id "claude-sonnet-5" :display_name "Claude Sonnet 5" :created_at "2026-01-01"}+ {:id "claude-opus-4-8" :display_name "Claude Opus 4.8" :created_at "2026-02-01"}+ {:id "claude-3-5-sonnet-20241022" :display_name "Claude 3.5" :created_at "2024-10-22"}+ {:id "claude-fable-5" :display_name "Claude Fable 5" :created_at "2026-03-01"}]}})](is (= [{:id "claude-fable-5" :display_name "Claude Fable 5"}{:id "claude-opus-4-8" :display_name "Claude Opus 4.8"}{:id "claude-sonnet-5" :display_name "Claude Sonnet 5"}]diff --git a/test/metabase/metabot/self/google_test.clj b/test/metabase/metabot/self/google_test.cljindex 3f66df883c3..a62cd084119 100644--- a/test/metabase/metabot/self/google_test.clj+++ b/test/metabase/metabot/self/google_test.clj@@ -664,10 +664,21 @@(mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))](is (thrown-with-msg?clojure.lang.ExceptionInfo- #"AI proxy is not supported for the Google provider"+ #"AI proxy is not supported for Google"(google-raw {:model "google/gemini-3.5-flash":input [{:role :user :content "hi"}]- :ai-proxy? true}))))))+ :ai-proxy? true})))))+ (testing (str "and still wins once the credentials are present but unusable. Google resolves its credentials "+ "inside the request span — late enough that the refusal has to be checked first, or a proxied "+ "request would be told about a project ID belonging to a connection it will never use")+ (mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AI proxy is not supported for Google"+ (google-raw {:model "google/gemini-3.5-flash"+ :input [{:role :user :content "hi"}]+ :credentials {:oauth-access-token "token" :project-id "Not A Project ID"}+ :ai-proxy? true}))))));;; ──────────────────────────────────────────────────────────────────;;; End-to-end stream translation.@@ -979,8 +990,8 @@llm.settings/llm-google-project-id "my-project"llm.settings/llm-google-location nil](mt/with-dynamic-fn-redefs [http/request (stub-count-tokens (atom []))]- (is (= {:models []- :learned-config {:probed-model "google/gemini-3.5-flash"}}+ (is (= {:models []+ :connection-info {:probed-model "google/gemini-3.5-flash"}}(list-models {:model "google/gemini-3.5-flash" :probe? true})))))))(deftest list-models-anthropic-reports-the-probed-model-test@@ -990,12 +1001,12 @@llm.settings/llm-google-project-id "my-project"llm.settings/llm-google-location nil](mt/with-dynamic-fn-redefs [http/request (stub-error (atom []) 400 anthropic-validation-error-body)]- (is (= {:models []- :learned-config {:probed-model "anthropic/claude-haiku-4-5@20251001"}}+ (is (= {:models []+ :connection-info {:probed-model "anthropic/claude-haiku-4-5@20251001"}}(list-models {:model "anthropic/claude-haiku-4-5@20251001" :probe? true})))))))(deftest list-models-without-probe-keeps-the-model-to-itself-test- (testing "a plain listing still validates the credentials but reports no `:learned-config` to the client"+ (testing "a plain listing still validates the credentials but reports no `:connection-info` to the client"(mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.pasted-access-token"llm.settings/llm-google-service-account-key nilllm.settings/llm-google-project-id "my-project"@@ -1071,7 +1082,7 @@(mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))](is (thrown-with-msg?clojure.lang.ExceptionInfo- #"AI proxy is not supported for the Google provider"+ #"AI proxy is not supported for Google"(list-models {:model "google/gemini-3.5-flash" :ai-proxy? true}))))))(def ^:private ^String html-404-bodydiff --git a/test/metabase/metabot/self/mistral_test.clj b/test/metabase/metabot/self/mistral_test.cljindex cc7d6e9b2ef..d260b7f64ec 100644--- a/test/metabase/metabot/self/mistral_test.clj+++ b/test/metabase/metabot/self/mistral_test.clj@@ -385,6 +385,7 @@(mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url "https://proxy.example"](testing "Uses the connection's own credentials"(with-redefs [self.core/sse-reducible identity+ self.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})](is (=? {:method :postdiff --git a/test/metabase/metabot/self/moonshot_test.clj b/test/metabase/metabot/self/moonshot_test.cljindex 8eb4332fe21..47fda381a50 100644--- a/test/metabase/metabot/self/moonshot_test.clj+++ b/test/metabase/metabot/self/moonshot_test.clj@@ -327,6 +327,7 @@(mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url "https://proxy.example"](testing "Uses the connection's own credentials"(with-redefs [self.core/sse-reducible identity+ self.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})](is (=? {:method :postdiff --git a/test/metabase/metabot/self/openai/chat_completions_test.clj b/test/metabase/metabot/self/openai/chat_completions_test.cljindex a5afe573cd3..20309c6fac6 100644--- a/test/metabase/metabot/self/openai/chat_completions_test.clj+++ b/test/metabase/metabot/self/openai/chat_completions_test.clj@@ -428,12 +428,12 @@#"Mistral returned an unexpected model list response$"(chat-completions/models-catalog "Mistral" {:status 200 :body {:object "list"}} nil)))))-(deftest ^:parallel models-catalog-error-is-tagged-api-error-without-status-test- (testing "the thrown error is tagged :api-error so rethrow-api-error! passes it through, and carries no status"- ;; `metabase.metabot.api/provider-client-error?` renders any 4xx :api-error under the admin API-key- ;; field. A malformed catalog is not a credentials problem, so it must not claim a 4xx status.+(deftest ^:parallel models-catalog-error-is-tagged-api-error-with-client-status-test+ (testing "the thrown error is tagged :api-error so rethrow-api-error! passes it through, and 400 so the admin sees it"+ ;; `metabase.llm.api.provider/provider-client-error?` only surfaces an :api-error carrying a 4xx; anything+ ;; else escapes the Connect path as an unhandled 500, which `MB_HIDE_STACKTRACES=true` collapses to+ ;; "Something went wrong". A malformed catalog is the admin's to fix, so it has to claim the 4xx.(let [data (try(chat-completions/models-catalog "Mistral" {:status 200 :body {:object "list"}})(catch clojure.lang.ExceptionInfo e (ex-data e)))]- (is (= {:api-error true :error-code :malformed-model-catalog} data))- (is (not (contains? data :status))))))+ (is (= {:api-error true :status-code 400 :error-code :malformed-model-catalog} data)))))diff --git a/test/metabase/metabot/self/openai_test.clj b/test/metabase/metabot/self/openai_test.cljindex 7aa3e95cb85..ca27d887d88 100644--- a/test/metabase/metabot/self/openai_test.clj+++ b/test/metabase/metabot/self/openai_test.clj@@ -395,13 +395,13 @@;;; list-models filtering tests;;; ──────────────────────────────────────────────────────────────────-(deftest ^:parallel supported-model?-test+(deftest ^:parallel supported-models-test(testing "whitelisted models are supported"(doseq [id ["gpt-6-astra" "gpt-5.6-sol" "gpt-5.6-terra" "gpt-5.6-luna" "gpt-5.5" "gpt-5.4-mini"]]- (is (true? (#'openai/supported-model? {:id id})) id)))+ (is (contains? openai/supported-models id) id)))(testing "non-white-listed models are not supported"(doseq [id ["gpt-5" "gpt-4.1" "gpt-4.1-mini" "gpt-4o" "o3" "text-embedding-3-small"]]- (is (false? (#'openai/supported-model? {:id id})) id))))+ (is (not (contains? openai/supported-models id)) id))))(deftest list-models-filters-catalog-to-whitelist-test(testing "list-models keeps only whitelisted models sorted by id"diff --git a/test/metabase/metabot/self/vllm_test.clj b/test/metabase/metabot/self/vllm_test.cljindex f4bce46b8bc..0e7050102e9 100644--- a/test/metabase/metabot/self/vllm_test.clj+++ b/test/metabase/metabot/self/vllm_test.clj@@ -4,13 +4,15 @@[clojure.test :refer :all][metabase.llm.settings :as llm.settings][metabase.metabot.self :as self]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.core :as self.core][metabase.metabot.self.debug :as debug][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.metabot.self.vllm :as vllm][metabase.metabot.test-util :as metabot.tu][metabase.test :as mt]- [metabase.util.json :as json])+ [metabase.util.json :as json]+ [metabase.util.malli.registry :as mr])(:import(java.net ConnectException SocketTimeoutException)(java.util.concurrent CountDownLatch TimeUnit)))@@ -560,7 +562,7 @@(is (false? (#'self/retryable-error? e))))))))(deftest vllm-http-errors-still-reach-the-status-specific-message-test- (testing "the IOException catch runs first, so a non-2xx must still be translated by `vllm-error-msg`"+ (testing "the IOException catch runs first, so a non-2xx must still be translated by the descriptor's `:errors`"(with-redefs [http/request (fn [_] (throw (ex-info "clj-http: status 401"{:status 401 :body "{\"message\":\"Unauthorized\"}"})))](is (thrown-with-msg?@@ -753,11 +755,18 @@[models chat-message](probe-choice! models {:message chat-message :finish_reason "tool_calls"}))+(deftest probe-return-conforms-to-the-listing-schema-test+ (testing "a probing listing still validates against [[adapter/ModelListing]] — `:connection-info` is a+ declared key, not an undeclared extra the closed schema would reject"+ (let [listing (probe! [{:id "vllm-test" :max_model_len 32768}] tool-calling-message)]+ (is (contains? listing :connection-info))+ (is (nil? (mr/explain adapter/ModelListing listing))))))+(deftest preflight-passes-on-a-correctly-configured-server-test(testing "a server that returns a well-formed tool call passes and still lists its models"- (is (= {:models [{:id "vllm-test" :display_name "vllm-test"}]- :learned-config {vllm/reasoning-config-key "false"- :probed-model "vllm-test"}}+ (is (= {:models [{:id "vllm-test" :display_name "vllm-test"}]+ :connection-info {vllm/reasoning-config-key "false"+ :probed-model "vllm-test"}}(probe! [{:id "vllm-test" :max_model_len 32768}] tool-calling-message)))))(deftest preflight-reports-the-model-it-probed-test@@ -766,11 +775,11 @@(testing "the first catalog entry, in the order the server lists it"(is (= "vllm-test"(get-in (probe! recorded-multi-model-catalog tool-calling-message)- [:learned-config :probed-model]))))+ [:connection-info :probed-model]))))(testing "including a slash-bearing repo id, which the connect path stores as `vllm/{id}`"(is (= "mlx-community/Qwen3-14B-4bit"(get-in (probe! recorded-no-alias-catalog tool-calling-message)- [:learned-config :probed-model])))))+ [:connection-info :probed-model])))))(testing "and a listing that did not probe reports nothing"(mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:data [{:id "vllm-test"}]}})](is (= [:models] (keys (vllm/list-models {:credentials credentials})))))))@@ -790,7 +799,7 @@:max_model_len 32768}]}}(do (reset! probed (:model (json/decode+kw (str body)))){:status 200 :body {:choices [{:message tool-calling-message}]}})))]- (is (= "vllm-test" (get-in (vllm/list-models {:credentials credentials :probe? true}) [:learned-config :probed-model])))+ (is (= "vllm-test" (get-in (vllm/list-models {:credentials credentials :probe? true}) [:connection-info :probed-model])))(is (= "vllm-test" @probed)))))(testing "an explicitly requested adapter is still honoured — the filter only picks the default"(mt/with-dynamic-fn-redefs [http/request (fn [{:keys [url]}]@@ -802,7 +811,7 @@(is (= "sql-lora" (get-in (vllm/list-models {:credentials credentials:probe? true:model "sql-lora"})- [:learned-config :probed-model]))))))+ [:connection-info :probed-model]))))))(deftest preflight-skipped-without-probe-flag-test(testing "without :probe? no generation request is made at all — listing models must stay cheap"@@ -879,7 +888,7 @@(get-in (vllm/list-models {:credentials credentials:probe? true:proposed-model "previous"})- [:learned-config :probed-model])))+ [:connection-info :probed-model])))(is (= #{"previous"} @probed))))))(deftest preflight-falls-back-when-the-proposed-model-is-no-longer-served-test@@ -896,7 +905,7 @@(get-in (vllm/list-models {:credentials credentials:probe? true:proposed-model "removed"})- [:learned-config :probed-model])))+ [:connection-info :probed-model])))(is (= #{"first"} @probed))))))(deftest preflight-rejects-a-model-the-server-does-not-serve-test@@ -995,21 +1004,21 @@saw is reported back for the connection to record";; Recorded shape: vLLM 0.26 puts it on the non-streaming message under `reasoning`.(is (=? {vllm/reasoning-config-key "true"}- (:learned-config+ (:connection-info(probe-choice! [{:id "vllm-test" :max_model_len 32768}]{:message (assoc tool-calling-message:reasoning "\nOkay, the user wants me to record \"orders\"."):finish_reason "tool_calls"})))))(testing "and under the deprecated spelling older servers still use"(is (=? {vllm/reasoning-config-key "true"}- (:learned-config+ (:connection-info(probe-choice! [{:id "vllm-test" :max_model_len 32768}]{:message (assoc tool-calling-message:reasoning_content "Older vLLM builds spell it this way."):finish_reason "tool_calls"})))))(testing "a model that answers without reasoning reports false"(is (=? {vllm/reasoning-config-key "false"}- (:learned-config (probe! [{:id "vllm-test" :max_model_len 32768}] tool-calling-message)))))+ (:connection-info (probe! [{:id "vllm-test" :max_model_len 32768}] tool-calling-message)))))(testing "and a connection carrying the flag is what the request body reads it from"(is (true? (vllm/reasoning-connection? {vllm/reasoning-config-key "true"})))(is (false? (vllm/reasoning-connection? {vllm/reasoning-config-key "false"})))@@ -1121,9 +1130,9 @@(deftest preflight-accepts-a-catalog-without-a-context-window-test(testing "`max_model_len` is vLLM's own field: Ollama, LM Studio, and TGI omit it. The floor isbest-effort, so a catalog without it connects rather than being rejected on a missing field"- (is (= {:models [{:id "vllm-test" :display_name "vllm-test"}]- :learned-config {vllm/reasoning-config-key "false"- :probed-model "vllm-test"}}+ (is (= {:models [{:id "vllm-test" :display_name "vllm-test"}]+ :connection-info {vllm/reasoning-config-key "false"+ :probed-model "vllm-test"}}(probe! [{:id "vllm-test"}] tool-calling-message)))))(deftest preflight-does-not-leak-the-context-window-to-the-client-testdiff --git a/test/metabase/metabot/self/zai_test.clj b/test/metabase/metabot/self/zai_test.cljindex d5c1294f52d..ec420686e6a 100644--- a/test/metabase/metabot/self/zai_test.clj+++ b/test/metabase/metabot/self/zai_test.clj@@ -286,6 +286,7 @@(mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url "https://proxy.example"](testing "Uses the connection's own credentials"(with-redefs [self.core/sse-reducible identity+ self.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})](is (=? {:method :postdiff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.cljindex ab7791e20c3..ad8323c3993 100644--- a/test/metabase/metabot/self_test.clj+++ b/test/metabase/metabot/self_test.clj@@ -4,6 +4,7 @@[clojure.java.io :as io][clojure.string :as str][clojure.test :refer :all]+ [malli.core :as mc][metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test][metabase.llm.provider :as llm.provider]@@ -11,6 +12,7 @@[metabase.metabot.schema.v2 :as schema.v2][metabase.metabot.scope :as scope][metabase.metabot.self :as self]+ [metabase.metabot.self.adapter :as adapter][metabase.metabot.self.bedrock :as bedrock][metabase.metabot.self.claude :as self.claude][metabase.metabot.self.core :as self.core]@@ -18,6 +20,7 @@[metabase.metabot.self.moonshot :as moonshot][metabase.metabot.self.openai :as openai][metabase.metabot.self.openrouter :as openrouter]+ [metabase.metabot.self.registry :as registry][metabase.metabot.self.zai :as zai][metabase.metabot.settings :as metabot.settings][metabase.metabot.test-util :as test-util]@@ -101,20 +104,54 @@(#'self/parse-provider-model model-ref)))](is (= :llm-not-configured (:error-code (ex-data e)))))))))-(deftest ^:parallel resolve-adapter-test- (testing "resolves known providers to adapter functions"- (is (fn? (#'self/resolve-adapter "anthropic")))- (is (fn? (#'self/resolve-adapter "openai")))- (is (fn? (#'self/resolve-adapter "openrouter")))- (is (fn? (#'self/resolve-adapter "zai")))- (is (fn? (#'self/resolve-adapter "mistral")))- (is (fn? (#'self/resolve-adapter "moonshot")))- (is (fn? (#'self/resolve-adapter "deepseek")))- (is (fn? (#'self/resolve-adapter "google")))- (is (fn? (#'self/resolve-adapter "vllm"))))- (testing "throws for unknown provider"+(deftest ^:parallel registry-test+ (testing "every registered provider resolves to an adapter and a listing"+ ;; derived from the rows rather than listed here, so a provider added to the platform and the registry+ ;; with an empty row fails this instead of going unchecked. The managed connection is excluded because it+ ;; is served by the wire family its model names and so has no adapter of its own — asked of the platform+ ;; rather than named, for the same reason.+ (let [serving (remove (comp llm.provider/managed-type? key) @#'registry/adapters)]+ (is (seq serving))+ (doseq [[provider _row] serving]+ (is (ifn? (registry/required provider :stream)) provider)+ (is (ifn? (registry/required provider :list-models)) provider))))+ (testing "a capability a provider does not have is absent, not a default"+ (is (nil? (registry/optional "vllm" :supported-models)))+ (is (nil? (registry/optional "deepseek" :context-window)))+ (is (some? (registry/optional "anthropic" :supported-models))))+ (testing "the registry covers exactly the provider types the platform knows about"+ (is (= (set (map :type (llm.provider/provider-types)))+ (set (keys @#'registry/adapters)))))+ (testing "every row conforms to the schema, so a mistyped capability key cannot read as an absent one"+ (doseq [[provider row] @#'registry/adapters]+ (is (nil? (mr/explain registry/AdapterRow row)) provider)))+ (testing "an adapter's own schema and the schema its row declares are the same contract, so a change to+ one without the other fails here rather than at a call site"+ ;; compared rather than exercised: the row's `:=>` forms are documentation — Malli checks one no further+ ;; than `ifn?` — and `mu/defn` already validates each implementation against its own schema on every+ ;; call. What nothing else covers is the two declarations drifting apart.+ ;; Skipped: `:stream` is registered as a plain `defn` and carries no schema to compare, and+ ;; `:supported-models` is a var holding a map rather than a function.+ (let [declared (into {} (map (fn [[capability _props schema]] [capability (mc/form schema)]))+ (mc/children (mc/schema registry/AdapterRow)))]+ (doseq [[provider row] @#'registry/adapters+ [capability implementation] (dissoc row :stream :supported-models)]+ (is (= (declared capability)+ (some-> (:schema (meta implementation)) mc/form))+ (str provider " " capability)))))+ (testing "every `:supported-models` allow-list conforms to the schema the shared listing helper takes."+ (doseq [[provider row] @#'registry/adapters+ :let [models (some-> (:supported-models row) deref)]+ :when models]+ (is (nil? (mr/explain adapter/SupportedModels models)) provider)))+ (testing "the capability enum and the row schema name the same capabilities, so the two hand-written+ lists cannot drift apart — a capability in one but not the other would either be unlookupable+ or unstorable"+ (is (= (set (mc/children (mc/schema registry/Capability)))+ (set (map first (mc/children (mc/schema registry/AdapterRow)))))))+ (testing "throws for an unknown provider"(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Unknown LLM provider"- (#'self/resolve-adapter "unknown")))))+ (registry/required "unknown" :stream)))))(deftest call-llm-tool-choice-test(llm.tu/with-default-connections@@ -2193,7 +2230,7 @@(let [models (self/known-models "anthropic")](is (seq models))(is (every? (comp :display-name val) models))))- (testing "DeepSeek keys model id straight to a display name, and is normalized to the same shape"+ (testing "DeepSeek records only a display name, and still comes back in the same shape"(let [models (self/known-models "deepseek")](is (seq models))(is (every? (comp string? :display-name val) models))))@@ -2204,7 +2241,19 @@(is (thrown-with-msg? clojure.lang.ExceptionInfo#"Unknown LLM provider"(self/known-models "brand-new"))))- (testing "an entry that is neither a map nor a string throws"+ (testing "an entry that is not a map throws"(is (thrown-with-msg? clojure.lang.ExceptionInfo#"Unrecognized supported-models entry"- (#'self/normalize-known-model "anthropic" "some-model" 42)))))+ (#'self/normalize-known-model "anthropic" "some-model" 42))))+ (testing "so does a map with no display name — the dox table would print the model id as its name"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Unrecognized supported-models entry"+ (#'self/normalize-known-model "anthropic" "some-model" {})))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Unrecognized supported-models entry"+ (#'self/normalize-known-model "anthropic" "some-model" {:context-window 200000}))))+ (testing "every provider that publishes an allow-list names every model in it"+ (doseq [provider ["anthropic" "bedrock" "deepseek" "mistral" "moonshot" "openai" "openrouter" "zai"]]+ (let [models (self/known-models provider)]+ (is (seq models) provider)+ (is (every? (comp string? :display-name val) models) provider)))))