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-1929] Moonshot provider support for Metabot (#79515)
Tyler Pludef7aa6d0f4090
diff --git a/frontend/src/metabase-types/api/metabot.ts b/frontend/src/metabase-types/api/metabot.tsindex bfd0bf10ad1..9ae32a02f84 100644--- a/frontend/src/metabase-types/api/metabot.ts+++ b/frontend/src/metabase-types/api/metabot.ts@@ -197,6 +197,7 @@ export type MetabotProvider =| "azure"| "bedrock"| "mistral"+ | "moonshot"| "openai"| "openrouter"| "zai";diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 67e29435172..6d560add783 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -540,6 +540,7 @@ interface SettingsManagerSettings {"llm-openrouter-api-key"?: string | null;"llm-zai-api-key"?: string | null;"llm-mistral-api-key"?: string | null;+ "llm-moonshot-api-key"?: string | null;"llm-azure-api-key"?: string | null;"llm-azure-api-base-url"?: string | null;"llm-bedrock-access-key-id"?: string | null;@@ -778,6 +779,7 @@ export interface EnterpriseSettings extends Settings {"llm-openrouter-api-key"?: string | null;"llm-zai-api-key"?: string | null;"llm-mistral-api-key"?: string | null;+ "llm-moonshot-api-key"?: string | null;"session-timeout": TimeoutValue | null;"search-engine": SearchEngineSettingValue | null;"scim-enabled"?: boolean | null;diff --git a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsxindex 19786e3fa64..aab7f58e559 100644--- a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx@@ -93,6 +93,13 @@ const DEFAULT_RESPONSES: Record<MetabotProvider, MetabotSettingsResponse> = {value: "mistral/mistral-medium-3-5",models: [{ id: "mistral-medium-3-5", display_name: "Mistral Medium 3.5" }],},+ moonshot: {+ value: "moonshot/kimi-k3",+ models: [+ { id: "kimi-k2.6", display_name: "Kimi K2.6" },+ { id: "kimi-k3", display_name: "Kimi K3" },+ ],+ },openai: {value: "openai/gpt-5.4",models: [@@ -133,6 +140,7 @@ type MetabotSettingKey =| "llm-azure-api-key"| "llm-azure-api-base-url"| "llm-mistral-api-key"+ | "llm-moonshot-api-key"| "llm-openai-api-key"| "llm-openrouter-api-key"| "llm-zai-api-key"@@ -146,6 +154,7 @@ const API_KEY_SETTING_BY_PROVIDER: Partial<> = {anthropic: "llm-anthropic-api-key",mistral: "llm-mistral-api-key",+ moonshot: "llm-moonshot-api-key",openai: "llm-openai-api-key",openrouter: "llm-openrouter-api-key",zai: "llm-zai-api-key",@@ -238,6 +247,7 @@ async function setup({azure: null,bedrock: null,mistral: null,+ moonshot: null,openai: null,openrouter: null,zai: null,@@ -299,6 +309,10 @@ async function setup({key: "llm-mistral-api-key",value: mergedApiKeyValues.mistral ?? undefined,}),+ "llm-moonshot-api-key": createMockSettingDefinition({+ key: "llm-moonshot-api-key",+ value: mergedApiKeyValues.moonshot ?? undefined,+ }),"llm-openai-api-key": createMockSettingDefinition({key: "llm-openai-api-key",value: mergedApiKeyValues.openai ?? undefined,@@ -630,6 +644,7 @@ describe("AIProviderSettingsSection", () => {/OpenAI/,/OpenRouter/,/Mistral/,+ /Moonshot AI/,/Z\.AI/,/Microsoft Azure/,/Amazon Bedrock/,@@ -1804,6 +1819,67 @@ describe("AIProviderSettingsSection", () => {expect(screen.getByLabelText("Provider")).toHaveValue("");});+ it("connects to Moonshot by saving the API key and selecting a model", async () => {+ await setup({+ savedProviderValue: null,+ isConfigured: false,+ apiKeyValues: { moonshot: null },+ updateResponse: {+ value: "moonshot/kimi-k3",+ models: DEFAULT_RESPONSES.moonshot.models,+ },+ });++ await selectProvider("Moonshot AI");+ await userEvent.type(screen.getByLabelText("API key"), "sk-moonshot-test");+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(async () => {+ expect(await findMetabotSettingsUpdates()).toEqual([+ { provider: "moonshot", "api-key": "sk-moonshot-test" },+ ]);+ });++ await screen.findByLabelText("Model");+ await openModelSelector();+ await userEvent.click(await screen.findByText("Kimi K2.6"));++ await waitFor(async () => {+ expect(await findMetabotSettingsUpdates()).toEqual([+ { provider: "moonshot", "api-key": "sk-moonshot-test" },+ { provider: "moonshot", model: "kimi-k2.6" },+ ]);+ });++ expect(+ screen.queryByRole("button", { name: "Connect" }),+ ).not.toBeInTheDocument();+ });++ it("disconnects Moonshot by clearing both the provider and API key settings", async () => {+ await setup({+ savedProviderValue: "moonshot/kimi-k2.6",+ isConfigured: true,+ apiKeyValues: { moonshot: "**********st" },+ });++ await screen.findByText("Connected to Moonshot AI");+ await screen.findByLabelText("API key");+ await confirmDisconnectProvider();++ await waitFor(async () => {+ expect(await findSettingUpdates()).toContainEqual({+ "llm-metabot-provider": null,+ "llm-moonshot-api-key": null,+ });+ });++ expect(+ await screen.findByText("Connect to an AI provider"),+ ).toBeInTheDocument();+ expect(screen.getByLabelText("Provider")).toHaveValue("");+ });+it("disconnects an API-key provider by clearing both the provider and API key settings", async () => {await setup();diff --git a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsxindex 4418988a0e9..55a74d242c3 100644--- a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx@@ -121,6 +121,10 @@ const setup = async ({key: "llm-mistral-api-key",value: undefined,}),+ createMockSettingDefinition({+ key: "llm-moonshot-api-key",+ value: undefined,+ }),]);setupUpdateSettingEndpoint();setupCollectionByIdEndpoint({ collections });diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsxindex 8bfe8ca7e30..96ad78de90b 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx@@ -124,6 +124,7 @@ function AIProviderConfigurationFormBody({useAdminSettings(["llm-anthropic-api-key","llm-mistral-api-key",+ "llm-moonshot-api-key","llm-openai-api-key","llm-openrouter-api-key","llm-zai-api-key",@@ -325,6 +326,7 @@ function AIProviderConfigurationFormBody({.with("anthropic","mistral",+ "moonshot","openai","openrouter","zai",diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsxindex e1a75a33ada..f7dedd3d3fa 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsx@@ -38,6 +38,7 @@ export const ApiKeyProviderFields = ({const { details } = useAdminSettings(["llm-anthropic-api-key","llm-mistral-api-key",+ "llm-moonshot-api-key","llm-openai-api-key","llm-openrouter-api-key","llm-zai-api-key",diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.tsindex 01291a4668e..a4255117b98 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts@@ -70,6 +70,14 @@ export function getProviderOptions(addKeyUrl: "https://console.mistral.ai/api-keys",},},+ moonshot: {+ value: "moonshot",+ label: "Moonshot AI",+ apiKey: {+ placeholder: "sk-...",+ addKeyUrl: "https://platform.kimi.ai/console/api-keys",+ },+ },openai: {value: "openai",label: "OpenAI",@@ -116,6 +124,7 @@ export function isAvailableProvider(provider: MetabotProvider): boolean {provider === "bedrock" ||provider === "metabase" ||provider === "mistral" ||+ provider === "moonshot" ||provider === "openai" ||provider === "openrouter" ||provider === "zai"@@ -126,12 +135,14 @@ export const API_KEY_SETTING_BY_PROVIDER: Record<MetabotApiKeyProvider,| "llm-anthropic-api-key"| "llm-mistral-api-key"+ | "llm-moonshot-api-key"| "llm-openai-api-key"| "llm-openrouter-api-key"| "llm-zai-api-key"> = {anthropic: "llm-anthropic-api-key",mistral: "llm-mistral-api-key",+ moonshot: "llm-moonshot-api-key",openai: "llm-openai-api-key",openrouter: "llm-openrouter-api-key",zai: "llm-zai-api-key",diff --git a/src/metabase/llm/settings.clj b/src/metabase/llm/settings.cljindex 71e26a41983..53b213f6bab 100644--- a/src/metabase/llm/settings.clj+++ b/src/metabase/llm/settings.clj@@ -58,6 +58,21 @@(throw (ex-info (str deferred-message) {:status-code 400})))(setting/set-value-of-type! :string setting-key trimmed)))+(defn normalize-llm-base-url+ "Trim whitespace and trailing slashes from an admin-entered LLM base URL; blank values become nil.+ The URL is otherwise persisted exactly as entered — admin-entered URLs are not silently rewritten."+ [value]+ (some-> (trimmed-string value)+ (str/replace #"/+$" "")+ not-empty))++(defn- set-normalized-base-url!+ "Set a base-URL setting to `new-value` with trailing slashes trimmed; blank values are stored as nil.+ Adapters build request URLs as `(str base-url path)`, so a pasted trailing slash would otherwise+ produce `//models`."+ [setting-key new-value]+ (setting/set-value-of-type! :string setting-key (normalize-llm-base-url new-value)))+;;; ------------------------------------------------- Anthropic -------------------------------------------------(defsetting llm-anthropic-api-key@@ -160,7 +175,8 @@:encryption :no:visibility :settings-manager:default "https://api.z.ai/api/paas/v4"- :export? false)+ :export? false+ :setter (partial set-normalized-base-url! :llm-zai-api-base-url))(defsetting llm-zai-api-key(deferred-tru "The Z.AI API Key.")@@ -178,7 +194,8 @@:encryption :no:visibility :settings-manager:default "https://api.mistral.ai/v1"- :export? false)+ :export? false+ :setter (partial set-normalized-base-url! :llm-mistral-api-base-url))(defsetting llm-mistral-api-key(deferred-tru "The Mistral API Key.")@@ -187,6 +204,23 @@:export? false:setter (partial set-trimmed-string! :llm-mistral-api-key))+;;; ------------------------------------------------- Moonshot --------------------------------------------------++(defsetting llm-moonshot-api-base-url+ (deferred-tru "The Moonshot AI API base URL used for Chat Completions. Repoint this to use the `.cn` platform; keys are not interchangeable between the two.")+ :encryption :no+ :visibility :settings-manager+ :default "https://api.moonshot.ai/v1"+ :export? false+ :setter (partial set-normalized-base-url! :llm-moonshot-api-base-url))++(defsetting llm-moonshot-api-key+ (deferred-tru "The Moonshot AI API Key.")+ :sensitive? true+ :visibility :settings-manager+ :export? false+ :setter (partial set-trimmed-string! :llm-moonshot-api-key))+;;; ----------------------------------------------- Amazon Bedrock ----------------------------------------------(defsetting llm-bedrock-access-key-id@@ -245,21 +279,12 @@:export? false:setter (partial set-trimmed-string! :llm-azure-api-key))-(defn normalize-llm-base-url- "Trim whitespace and trailing slashes from an admin-entered LLM base URL; blank values become nil.- The URL is otherwise persisted exactly as entered — admin-entered URLs are not silently rewritten."- [value]- (some-> (trimmed-string value)- (str/replace #"/+$" "")- not-empty))-(defsetting llm-azure-api-base-url(deferred-tru "The base URL of the Azure resource''s OpenAI- or Anthropic-compatible surface, e.g. `https://<resource>.services.ai.azure.com/openai`."):encryption :no:visibility :settings-manager:export? false- :setter (fn [new-value]- (setting/set-value-of-type! :string :llm-azure-api-base-url (normalize-llm-base-url new-value))))+ :setter (partial set-normalized-base-url! :llm-azure-api-base-url));;; --------------------------------------------------- Proxy ---------------------------------------------------diff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.cljindex 3cb0df38172..c115164fc86 100644--- a/src/metabase/metabot/api.clj+++ b/src/metabase/metabot/api.clj@@ -485,6 +485,7 @@(case provider"anthropic" :llm-anthropic-api-key"mistral" :llm-mistral-api-key+ "moonshot" :llm-moonshot-api-key"openai" :llm-openai-api-key"openrouter" :llm-openrouter-api-key"zai" :llm-zai-api-key))diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex a332651c04c..5eb2aad815c 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -20,6 +20,7 @@[metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core][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.zai :as zai]@@ -38,6 +39,7 @@"azure" azure/azure"bedrock" bedrock/bedrock"mistral" mistral/mistral+ "moonshot" moonshot/moonshot"openai" openai/openai"openrouter" openrouter/openrouter"zai" zai/zai@@ -51,6 +53,7 @@"azure" azure/list-models"bedrock" bedrock/list-models"mistral" mistral/list-models+ "moonshot" moonshot/list-models"openai" openai/list-models"openrouter" openrouter/list-models"zai" zai/list-modelsdiff --git a/src/metabase/metabot/self/mistral.clj b/src/metabase/metabot/self/mistral.cljindex e138f72af0c..9023bb42da4 100644--- a/src/metabase/metabot/self/mistral.clj+++ b/src/metabase/metabot/self/mistral.clj@@ -51,6 +51,8 @@(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?@@ -66,7 +68,7 @@:url "/models":as :json:headers {"Content-Type" "application/json"}})]- (get-in res [:body :data]))+ (chat-completions/models-catalog "Mistral" res))(catch Exception e(core/rethrow-api-error! "mistral" mistral-error-msg e))))diff --git a/src/metabase/metabot/self/moonshot.clj b/src/metabase/metabot/self/moonshot.cljnew file mode 100644index 00000000000..7f4f07e0343--- /dev/null+++ b/src/metabase/metabot/self/moonshot.clj@@ -0,0 +1,167 @@+(ns metabase.metabot.self.moonshot+ "Moonshot AI (Kimi) / Chat Completions adapter.++ Moonshot exposes an OpenAI-compatible Chat Completions API.++ https://platform.kimi.ai/docs"+ (:require+ [metabase.llm.settings :as llm]+ [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]]))++(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 supported-models+ "Moonshot models offered in the Metabot model picker, as a map of model id -> display name.+ `list-models` returns the intersection of this map with the `/models` catalog.++ The `kimi-k2.7-code` models the catalog also carries are coding models, not agent models, and are excluded."+ {"kimi-k2.6" "Kimi K2.6"+ "kimi-k3" "Kimi K3"})++(def ^:private thinking-only-models+ "Models whose catalog entry reports `supports_thinking_type: \"only\"`: thinking cannot be turned off, so sending+ `thinking {:type \"disabled\"}` is rejected. k3 does not need it — it accepts `tool_choice \"required\"` with+ thinking on, unlike k2.6."+ #{"kimi-k3"})++(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 (or (not-empty (:api-key credentials))+ (not-empty (llm/llm-moonshot-api-key)))]+ {:url (llm/llm-moonshot-api-base-url)+ :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))))++(defn list-models+ "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).+ `: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 (supported-models id)})))}))++(mu/defn moonshot-request-body+ "Build the Chat Completions request body for an LLM request.++ Moonshot's Chat Completions dialect matches what [[chat-completions/request-body]] emits — including+ `stream_options`, unlike Mistral — except:++ - **No `temperature`.** Moonshot 400s on any value but the one its thinking mode allows (0.6 off, 1.0 on), so+ there is no safe value to pass and it is dropped unconditionally.+ - **`thinking {:type \"disabled\"}`.** Thinking is on by default on every Kimi model, and `tool_choice+ \"required\"` — which the structured-output path and the `:sql` and `:document-generate-content` profiles all+ depend on — is rejected while it is on. We drop `reasoning_content` anyway, so thinking is pure cost+ (~12x the completion tokens for the same conversation title). [[thinking-only-models]] cannot disable it and+ does not need to, so they are sent no `thinking` at all and the model default applies.+ - **`prompt_cache_key`.** Moonshot caching is automatic and hits without it, but a `:prompt-cache-key` — the+ conversation id — is forwarded when present."+ [{:keys [model prompt-cache-key] :as opts+ :or {model default-model}} :- core/LLMRequestOpts]+ (-> (chat-completions/request-body (assoc opts :model model))+ (dissoc :temperature)+ (cond-> (not (thinking-only-models model)) (assoc :thinking {:type "disabled"})+ prompt-cache-key (assoc :prompt_cache_key prompt-cache-key))))++(mu/defn moonshot-raw+ "Perform a streaming request to the Moonshot Chat Completions API.+ `:ai-proxy?` is not supported for Moonshot and throws when true."+ [{:keys [model tools ai-proxy?] :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 (llm/llm-moonshot-api-key))+ auth (core/resolve-auth "moonshot" "Moonshot"+ (when api-key+ {:url (llm/llm-moonshot-api-base-url)+ :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))))))++(defn moonshot->aisdk-chunks-xf+ "Translates Moonshot Chat Completions streaming chunks into AI SDK v5 protocol chunks."+ []+ (chat-completions/chat-completions->aisdk-chunks-xf))++(defn moonshot+ "Call the Moonshot Chat Completions API, return AISDK stream."+ [& args]+ (let [raw (apply moonshot-raw args)]+ (eduction (moonshot->aisdk-chunks-xf) raw)))diff --git a/src/metabase/metabot/self/openai/chat_completions.clj b/src/metabase/metabot/self/openai/chat_completions.cljindex d1f4de03801..617a25a2547 100644--- a/src/metabase/metabot/self/openai/chat_completions.clj+++ b/src/metabase/metabot/self/openai/chat_completions.clj@@ -13,6 +13,7 @@[metabase.metabot.self.core :as core][metabase.metabot.self.schema :as schema][metabase.util :as u]+ [metabase.util.i18n :refer [tru]][metabase.util.json :as json][metabase.util.malli :as mu]))@@ -128,8 +129,13 @@Chat Completions has no explicit start/stop events per content block likeClaude or OpenAI Responses do — we infer transitions from the delta shape.- Parallel tool calls arrive with different `index` values; when a new index- appears the previous tool is complete."++ Parallel tool calls are tracked by tool-call `id`, not by `index`, which is+ never read: a tool-call delta whose `id` differs from the open one closes the+ previous block and opens a new one. That relies on providers sending `id` only+ on a tool call's opening chunk — one that repeated it on continuation chunks+ would lose their arguments, since neither the start branch (needs `:name`) nor+ the argument-delta branch (needs no `:id`) would fire."[](fn [rf](let [current-type (volatile! nil) ;; :text | :function_call | nil@@ -246,3 +252,27 @@:else "auto"))temperature (assoc :temperature temperature)max-tokens (assoc :max_tokens max-tokens))))++;;; Model catalog++(defn models-catalog+ "Extract the model list from an OpenAI-compatible `GET /models` response, failing closed.++ `(get-in res [:body :data])` yields nil for any body shape we don't recognize — a base URL pointing at+ something that isn't a model endpoint, an HTML error page, a provider that renamed the key. Returning nil+ leaves the caller's whitelist intersection empty, so the admin Connect flow succeeds against a provider we+ 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.++ A well-formed but empty `data` is a legitimate response — an account with no accessible models — and passes."+ [provider-name res]+ (let [data (get-in res [:body :data])]+ (when-not (sequential? data)+ (throw (ex-info (tru "{0} returned an unexpected model list response" provider-name)+ {:api-error true+ :error-code :malformed-model-catalog})))+ data))diff --git a/src/metabase/metabot/self/zai.clj b/src/metabase/metabot/self/zai.cljindex cafcfdbc7bf..6d41c2ca780 100644--- a/src/metabase/metabot/self/zai.clj+++ b/src/metabase/metabot/self/zai.clj@@ -50,7 +50,9 @@The endpoint is OpenAI-compatible but undocumented; it doubles as the credentialround-trip behind the admin Connect button (auth is checked before routing, so a 2xx- proves the key and base URL reach an authenticated surface).+ 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?@@ -66,7 +68,7 @@:url "/models":as :json:headers {"Content-Type" "application/json"}})]- (get-in res [:body :data]))+ (chat-completions/models-catalog "Z.AI" res))(catch Exception e(core/rethrow-api-error! "zai" zai-error-msg e))))diff --git a/src/metabase/metabot/settings.clj b/src/metabase/metabot/settings.cljindex 3fbddbaf2c9..25628dd4278 100644--- a/src/metabase/metabot/settings.clj+++ b/src/metabase/metabot/settings.clj@@ -106,7 +106,7 @@(def ^:private direct-providers"Providers that can be used directly (not via the metabase/ proxy prefix)."- #{"anthropic" "azure" "bedrock" "mistral" "openai" "openrouter" "zai"})+ #{"anthropic" "azure" "bedrock" "mistral" "moonshot" "openai" "openrouter" "zai"})(def ^:private default-anthropic-llm-metabot-model"Default Anthropic model used for Metabot when no explicit model is selected."@@ -120,6 +120,10 @@"Default Mistral model used for Metabot when no explicit model is selected.""mistral-medium-3-5")+(def ^:private default-moonshot-llm-metabot-model+ "Default Moonshot model used for Metabot when no explicit model is selected."+ "kimi-k3")+(def ^:private default-openai-llm-metabot-model"Default OpenAI model used for Metabot when no explicit model is selected.""gpt-5.4")@@ -146,6 +150,7 @@{"anthropic" default-anthropic-llm-metabot-model"bedrock" default-bedrock-llm-metabot-model"mistral" default-mistral-llm-metabot-model+ "moonshot" default-moonshot-llm-metabot-model"openai" default-openai-llm-metabot-model"openrouter" default-openrouter-llm-metabot-model"zai" default-zai-llm-metabot-model@@ -267,7 +272,7 @@(validate-direct-provider! value)))(defsetting llm-metabot-provider- (deferred-tru "The AI provider and model for Metabot. Format: provider/model-name, e.g. `anthropic/claude-haiku-4-5`, `openai/gpt-5.4`, `openrouter/anthropic/claude-haiku-4.5`.")+ (deferred-tru "The AI provider and model for Metabot. Format: provider/model-name, e.g. `anthropic/claude-haiku-4-5`, `openai/gpt-5.4`, `moonshot/kimi-k3`, `openrouter/anthropic/claude-haiku-4.5`."):type :string:encryption :no:default default-llm-metabot-provider@@ -311,6 +316,7 @@:session-token (non-blank (llm.settings/llm-bedrock-session-token)):region (non-blank (llm.settings/llm-bedrock-region))})"mistral" (configured-api-key-credentials (llm.settings/llm-mistral-api-key))+ "moonshot" (configured-api-key-credentials (llm.settings/llm-moonshot-api-key))"openai" (configured-api-key-credentials (llm.settings/llm-openai-api-key))"openrouter" (configured-api-key-credentials (llm.settings/llm-openrouter-api-key))"zai" (configured-api-key-credentials (llm.settings/llm-zai-api-key))diff --git a/test/metabase/llm/settings_test.clj b/test/metabase/llm/settings_test.cljindex 459fa206c6b..d46280f3c09 100644--- a/test/metabase/llm/settings_test.clj+++ b/test/metabase/llm/settings_test.clj@@ -167,6 +167,50 @@(is (nil? (llm.settings/normalize-llm-base-url nil)))(is (nil? (llm.settings/normalize-llm-base-url "///"))))+;;; ------------------------------------------- Chat Completions base-URL Setter Tests -------------------------------------------++;;; Adapters build request URLs as `(str base-url path)`, so a pasted trailing slash would produce `//models`.++(deftest llm-zai-api-base-url-setter-test+ (testing "trims whitespace and trailing slashes"+ (mt/with-temp-env-var-value! [mb-llm-zai-api-base-url nil]+ (mt/discard-setting-changes [llm-zai-api-base-url]+ (llm.settings/llm-zai-api-base-url! " https://api.z.ai/api/paas/v4/ ")+ (is (= "https://api.z.ai/api/paas/v4" (llm.settings/llm-zai-api-base-url))))))+ (testing "blank restores the default"+ (mt/with-temp-env-var-value! [mb-llm-zai-api-base-url nil]+ (mt/discard-setting-changes [llm-zai-api-base-url]+ (llm.settings/llm-zai-api-base-url! "https://self-hosted.example/v4")+ (llm.settings/llm-zai-api-base-url! " ")+ (is (= "https://api.z.ai/api/paas/v4" (llm.settings/llm-zai-api-base-url)))))))++(deftest llm-mistral-api-base-url-setter-test+ (testing "trims whitespace and trailing slashes"+ (mt/with-temp-env-var-value! [mb-llm-mistral-api-base-url nil]+ (mt/discard-setting-changes [llm-mistral-api-base-url]+ (llm.settings/llm-mistral-api-base-url! " https://api.mistral.ai/v1/ ")+ (is (= "https://api.mistral.ai/v1" (llm.settings/llm-mistral-api-base-url))))))+ (testing "blank restores the default"+ (mt/with-temp-env-var-value! [mb-llm-mistral-api-base-url nil]+ (mt/discard-setting-changes [llm-mistral-api-base-url]+ (llm.settings/llm-mistral-api-base-url! "https://self-hosted.example/v1")+ (llm.settings/llm-mistral-api-base-url! " ")+ (is (= "https://api.mistral.ai/v1" (llm.settings/llm-mistral-api-base-url)))))))++(deftest llm-moonshot-api-base-url-setter-test+ (testing "trims whitespace and trailing slashes"+ (mt/with-temp-env-var-value! [mb-llm-moonshot-api-base-url nil]+ (mt/discard-setting-changes [llm-moonshot-api-base-url]+ (llm.settings/llm-moonshot-api-base-url! " https://api.moonshot.ai/v1/ ")+ (is (= "https://api.moonshot.ai/v1" (llm.settings/llm-moonshot-api-base-url))))))+ (testing "blank restores the default"+ (mt/with-temp-env-var-value! [mb-llm-moonshot-api-base-url nil]+ (mt/discard-setting-changes [llm-moonshot-api-base-url]+ ;; The `.cn` platform is the reason this setting is repointable at all.+ (llm.settings/llm-moonshot-api-base-url! "https://api.moonshot.cn/v1")+ (llm.settings/llm-moonshot-api-base-url! " ")+ (is (= "https://api.moonshot.ai/v1" (llm.settings/llm-moonshot-api-base-url)))))))+;;; ------------------------------------------- llm-proxy-base-url Feature Guard Tests -------------------------------------------(deftest llm-proxy-base-url-feature-guard-testdiff --git a/test/metabase/metabot/api_test.clj b/test/metabase/metabot/api_test.cljindex 0e283e4c8c5..62dd4c7323a 100644--- a/test/metabase/metabot/api_test.clj+++ b/test/metabase/metabot/api_test.clj@@ -597,6 +597,31 @@(is (= "mistral-key-fresh"(llm.settings/llm-mistral-api-key)))))))+(deftest settings-put-connect-moonshot-defaults-model-test+ (testing "connecting moonshot with only an api-key switches to the default moonshot model"+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"+ llm.settings/llm-moonshot-api-key nil]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn+ ([provider]+ (is (= "moonshot" provider))+ {:models [{:id "kimi-k3"+ :display_name "Kimi K3"}]})+ ([provider {:keys [credentials]}]+ (is (= "moonshot" provider))+ (is (= {:api-key "sk-moonshot-key-fresh"} credentials))+ {:models [{:id "kimi-k3"+ :display_name "Kimi K3"}]}))]+ (is (= {:value "moonshot/kimi-k3"+ :models [{:id "kimi-k3"+ :display_name "Kimi K3"}]}+ (mt/user-http-request :crowberto :put 200 "metabot/settings"+ {:provider "moonshot"+ :api-key "sk-moonshot-key-fresh"})))+ (is (= "moonshot/kimi-k3"+ (metabot.settings/llm-metabot-provider)))+ (is (= "sk-moonshot-key-fresh"+ (llm.settings/llm-moonshot-api-key)))))))+(deftest settings-put-updates-metabase-provider-without-api-key-test(mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"](mt/with-dynamic-fn-redefs [metabot.self/list-models (fndiff --git a/test/metabase/metabot/self/mistral_test.clj b/test/metabase/metabot/self/mistral_test.cljindex 990de069e09..fcbdb61090b 100644--- a/test/metabase/metabot/self/mistral_test.clj+++ b/test/metabase/metabot/self/mistral_test.clj@@ -313,3 +313,14 @@clojure.lang.ExceptionInfo#"Mistral API key expired or invalid"(mistral/list-models)))))))++(deftest list-models-malformed-catalog-throws-test+ (testing "a 2xx whose body carries no model list throws instead of reporting an empty catalog"+ ;; Failing open here would let admin Connect succeed against a base URL we never reached,+ ;; leaving an empty model picker with no diagnostic.+ (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-test"]+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:object "list"}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Mistral returned an unexpected model list response"+ (mistral/list-models)))))))diff --git a/test/metabase/metabot/self/moonshot_test.clj b/test/metabase/metabot/self/moonshot_test.cljnew file mode 100644index 00000000000..041ed3f91ea--- /dev/null+++ b/test/metabase/metabot/self/moonshot_test.clj@@ -0,0 +1,330 @@+(ns metabase.metabot.self.moonshot-test+ (:require+ [clj-http.client :as http]+ [clojure.test :refer :all]+ [medley.core :as m]+ [metabase.llm.settings :as llm.settings]+ [metabase.metabot.self.core :as self.core]+ [metabase.metabot.self.debug :as debug]+ [metabase.metabot.self.moonshot :as moonshot]+ [metabase.metabot.test-util :as metabot.tu]+ [metabase.premium-features.core :as premium-features]+ [metabase.test :as mt]))++(set! *warn-on-reflection* true)++;;; ──────────────────────────────────────────────────────────────────+;;; moonshot-request-body tests+;;; ──────────────────────────────────────────────────────────────────++(deftest ^:parallel request-body-default-model-test+ (testing "the model defaults to kimi-k3"+ (is (= "kimi-k3"+ (:model (moonshot/moonshot-request-body {:input [{:role :user :content "hi"}]}))))))++(deftest ^:parallel request-body-system-plain-string-test+ (testing "the system prompt stays a plain string"+ (let [body (moonshot/moonshot-request-body+ {:model "kimi-k2.6"+ :system "You are a helpful assistant."+ :input [{:role :user :content "hi"}]})]+ (is (= {:role "system" :content "You are a helpful assistant."}+ (-> body :messages first))))))++(deftest ^:parallel request-body-keeps-stream-options-test+ (testing "stream_options is kept — Moonshot accepts it, unlike Mistral"+ (let [body (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]})]+ (is (true? (:stream body)))+ (is (= {:include_usage true} (:stream_options body))))))++(deftest ^:parallel request-body-drops-temperature-test+ (testing "temperature is never sent — Moonshot 400s on any value but the one its thinking mode allows"+ (is (not (contains? (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :temperature 0.3})+ :temperature)))))++(deftest ^:parallel request-body-max-tokens-test+ (testing "max-tokens passes through"+ (is (= 128 (:max_tokens (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :max-tokens 128}))))))++(deftest ^:parallel request-body-disables-thinking-test+ (testing "thinking is disabled on models that can disable it"+ ;; `tool_choice "required"` is rejected while thinking is on, and reasoning tokens are billed and then+ ;; discarded — we drop `reasoning_content`.+ (is (= {:type "disabled"}+ (:thinking (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]})))))+ (testing "no thinking parameter is sent for a thinking-only model"+ ;; kimi-k3 reports `supports_thinking_type: "only"` and would reject `{:type "disabled"}`. It accepts+ ;; `tool_choice "required"` with thinking on, so it does not need it.+ (is (not (contains? (moonshot/moonshot-request-body {:model "kimi-k3"+ :input [{:role :user :content "hi"}]})+ :thinking)))+ (testing "including when it is reached as the default model"+ (is (not (contains? (moonshot/moonshot-request-body {:input [{:role :user :content "hi"}]})+ :thinking))))))++(deftest ^:parallel request-body-prompt-cache-key-test+ (testing "a :prompt-cache-key is forwarded as prompt_cache_key, absent otherwise"+ (is (= "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"+ (:prompt_cache_key (moonshot/moonshot-request-body+ {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :prompt-cache-key "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"}))))+ (is (not (contains? (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]})+ :prompt_cache_key)))))++(deftest ^:parallel request-body-tools-test+ (testing "tools are sent in OpenAI function format with tool_choice auto"+ (is (=? {:tools [{:type "function"+ :function {:name "get-time"}}]+ :tool_choice "auto"}+ (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :tools [(metabot.tu/get-time-tool)]})))))++(deftest ^:parallel request-body-schema-forces-structured-output-test+ (testing "a schema forces a structured_output tool call, with thinking disabled so the model accepts it"+ (is (=? {:tools [{:type "function"+ :function {:name "structured_output"+ :parameters {:type "object"}}}]+ :tool_choice "required"+ :thinking {:type "disabled"}}+ (moonshot/moonshot-request-body {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :schema {:type "object"+ :properties {:title {:type "string"}}}})))))++;;; ──────────────────────────────────────────────────────────────────+;;; Streaming chunk conversion tests+;;;+;;; The adapter delegates to the shared Chat Completions transducer,+;;; whose Moonshot-shaped edge cases — usage reported in two places,+;;; cache reads reported flat and nested, serialized parallel tool+;;; calls, reasoning deltas — are pinned in+;;; `metabase.metabot.self.openai.chat-completions-test`. These cases+;;; assert the adapter is wired to it, against chunks transcribed from+;;; real streams (notes/bot-1929-moonshot/fixtures/).+;;; ──────────────────────────────────────────────────────────────────++(deftest ^:parallel moonshot-text-conv-test+ (let [chunks [{:id "chatcmpl-6a73f0a398c4bd9176c05066"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content "Hello"} :finish_reason nil}]}+ {:choices [{:index 0 :delta {:content " there"}}]}+ {:choices [{:index 0 :delta {} :finish_reason "stop"}]}+ {:choices [] :usage {:prompt_tokens 12 :completion_tokens 2 :total_tokens 14}}]]+ (testing "text streaming chunks are mapped correctly"+ (is (=? [{:type :start} {:type :text-start} {:type :text-delta} {:type :text-end} {:type :usage}]+ (into [] (comp (moonshot/moonshot->aisdk-chunks-xf) (m/distinct-by :type)) chunks))))+ (testing "through full pipeline produces text + usage"+ (is (=? [{:type :start}+ {:type :text :text "Hello there"}+ {:type :usage :model "kimi-k2.6"+ :usage {:promptTokens 12 :completionTokens 2}}]+ (into [] (comp (moonshot/moonshot->aisdk-chunks-xf)+ (self.core/aisdk-xf))+ chunks))))))++(deftest ^:parallel moonshot-tool-call-conv-test+ (testing "a streamed tool call preceded by reasoning deltas produces one tool-input and no text block"+ ;; Moonshot opens every stream with an empty-string `content` delta and then streams `reasoning_content`+ ;; before the tool call. Neither may open a text block — that would close the tool call that follows.+ (is (=? [{:type :start}+ {:type :tool-input+ :id "get_weather_0"+ :function "get_weather"+ :arguments {:city "Berlin"}}+ {:type :usage}]+ (into [] (comp (moonshot/moonshot->aisdk-chunks-xf)+ (self.core/aisdk-xf))+ [{:id "chatcmpl-6a73f057ecb22d28d50ec557"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content ""} :finish_reason nil}]}+ {:choices [{:index 0 :delta {:reasoning_content ""} :finish_reason nil}]}+ {:choices [{:index 0+ :delta {:tool_calls [{:index 0+ :id "get_weather_0"+ :type "function"+ :function {:name "get_weather" :arguments ""}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments "{\"city\":"}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments " \"Berlin\"}"}}]}}]}+ {:choices [{:index 0 :delta {} :finish_reason "tool_calls"}]}+ {:choices [] :usage {:prompt_tokens 138 :completion_tokens 36 :total_tokens 174}}])))))++(deftest ^:parallel moonshot-usage-dual-location-test+ (testing "a stream reporting usage twice emits one :usage part, with cache reads from prompt_tokens_details"+ ;; Moonshot puts usage both under the finishing choice and on a final top-level chunk. Emitting both would+ ;; double `ai_usage_log` rows and every token metric downstream.+ (let [usage-chunk {:prompt_tokens 4253+ :completion_tokens 20+ :total_tokens 4273+ :cached_tokens 4096+ :completion_tokens_details {:reasoning_tokens 1}+ :prompt_tokens_details {:cached_tokens 4096}}+ usages (->> (into [] (moonshot/moonshot->aisdk-chunks-xf)+ [{:id "chatcmpl-6a73f0a398c4bd9176c05066"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content "Hi"}}]}+ {:choices [{:index 0+ :delta {}+ :finish_reason "stop"+ :usage usage-chunk}]}+ {:choices [] :usage usage-chunk}])+ (filterv #(= :usage (:type %))))]+ (is (= 1 (count usages)))+ (is (=? {:type :usage+ :id "chatcmpl-6a73f0a398c4bd9176c05066"+ :model "kimi-k2.6"+ :usage {:promptTokens 4253+ :completionTokens 20+ :cacheCreationTokens 0+ :cacheReadTokens 4096}}+ (first usages))))))++;;; ──────────────────────────────────────────────────────────────────+;;; Auth tests+;;; ──────────────────────────────────────────────────────────────────++(deftest moonshot-auth-preferences-test+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-dynamic-fn-redefs [premium-features/premium-embedding-token (constantly "proxy-token")]+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-byok"+ llm.settings/llm-proxy-base-url "https://proxy.example"]+ (testing "Prefers BYOK over ai proxy"+ (with-redefs [self.core/sse-reducible identity+ debug/capture-stream (fn [r _] r)+ http/request (fn [req] {:body req})]+ (is (=? {:method :post+ :url "https://api.moonshot.ai/v1/chat/completions"+ :headers {"Authorization" "Bearer sk-moonshot-key-byok"}+ :body string?}+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]})))))+ (testing "Does not fall back to ai proxy when BYOK is missing"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Moonshot API key is set"+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]})))))+ (testing "Throws an error if nothing is defined"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key nil+ llm.settings/llm-proxy-base-url nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Moonshot API key is set"+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]})))))))))++(deftest moonshot-raw-ai-proxy-unsupported-test+ (testing "ai-proxy? throws before credentials are even consulted"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key nil]+ (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 Moonshot"+ (moonshot/moonshot-raw {:model "kimi-k2.6"+ :input [{:role :user :content "hi"}]+ :ai-proxy? true})))))))++(deftest list-models-ai-proxy-unsupported-test+ (testing "ai-proxy? throws before credentials are even consulted"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key nil]+ (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 Moonshot"+ (moonshot/list-models {:ai-proxy? true})))))))++;;; ──────────────────────────────────────────────────────────────────+;;; list-models tests+;;; ──────────────────────────────────────────────────────────────────++(deftest list-models-filters-catalog-to-whitelist-test+ (testing "list-models keeps only whitelisted models, naming them from the whitelist"+ ;; Moonshot catalog entries carry no `:name`, so the display name has nowhere else to come from. The coding+ ;; models in the live catalog are excluded.+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-test"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:method :get+ :url "https://api.moonshot.ai/v1/models"+ :headers {"Authorization" "Bearer sk-moonshot-key-test"}}+ req))+ {:status 200+ :body {:object "list"+ :data [{:id "kimi-k2.7-code" :object "model"}+ {:id "kimi-k2.7-code-highspeed" :object "model"}+ {:id "kimi-k3" :object "model"}+ {:id "kimi-k2.6" :object "model"}]}})]+ (is (= {:models [{:id "kimi-k2.6" :display_name "Kimi K2.6"}+ {:id "kimi-k3" :display_name "Kimi K3"}]}+ (moonshot/list-models)))))))++(deftest list-models-omits-models-the-key-cannot-reach-test+ (testing "a model missing from the per-key catalog is not offered"+ ;; k3's catalog permission group is `staff` where the other models report `moonshot`, so not every account+ ;; sees it. This is why the static default is k2.6.+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-test"]+ (mt/with-dynamic-fn-redefs [http/request (fn [_]+ {:status 200 :body {:data [{:id "kimi-k2.6"}]}})]+ (is (= {:models [{:id "kimi-k2.6" :display_name "Kimi K2.6"}]}+ (moonshot/list-models)))))))++(deftest list-models-explicit-credentials-test+ (testing "a passed-in api-key is used over the configured key"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-setting"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:headers {"Authorization" "Bearer sk-moonshot-key-explicit"}}+ req))+ {:status 200 :body {:data []}})]+ (is (= {:models []}+ (moonshot/list-models {:credentials {:api-key "sk-moonshot-key-explicit"}})))))))++(deftest list-models-blank-credentials-fall-back-to-configured-key-test+ (testing "a blank passed-in api-key falls back to the configured key"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-setting"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:headers {"Authorization" "Bearer sk-moonshot-key-setting"}}+ req))+ {:status 200 :body {:data []}})]+ (is (= {:models []}+ (moonshot/list-models {:credentials {:api-key ""}})))))))++(deftest list-models-blank-credentials-without-configured-key-test+ (testing "throws when the passed-in api-key is blank and no key is configured"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Moonshot API key is set"+ (moonshot/list-models {:credentials {:api-key ""}}))))))++(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-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-test"]+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body {:object "list"}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Moonshot returned an unexpected model list response"+ (moonshot/list-models)))))))++;;; ──────────────────────────────────────────────────────────────────+;;; Error mapping tests+;;; ──────────────────────────────────────────────────────────────────++(deftest error-status-messages-test+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-test"]+ (doseq [[status body pattern]+ [[400 "{\"error\":{\"type\":\"invalid_request_error\",\"message\":\"invalid temperature\"}}"+ #"Moonshot rejected the request"]+ [401 "{\"error\":{\"type\":\"invalid_authentication_error\",\"message\":\"Invalid Authentication\"}}"+ #"Moonshot API key expired or invalid"]+ [404 "{\"error\":{\"type\":\"resource_not_found_error\",\"message\":\"Not found the model\"}}"+ #"Moonshot API endpoint or model was not found"]]]+ (testing (str "HTTP " status)+ (mt/with-dynamic-fn-redefs [http/request (fn [_]+ (throw (ex-info (str "clj-http: status " status)+ {:status status :body body})))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo pattern (moonshot/list-models))))))))diff --git a/test/metabase/metabot/self/openai/chat_completions_test.clj b/test/metabase/metabot/self/openai/chat_completions_test.cljindex 0802644b144..0464f1df81e 100644--- a/test/metabase/metabot/self/openai/chat_completions_test.clj+++ b/test/metabase/metabot/self/openai/chat_completions_test.clj@@ -1,6 +1,7 @@(ns metabase.metabot.self.openai.chat-completions-test(:require[clojure.test :refer :all]+ [metabase.metabot.self.core :as self.core][metabase.metabot.self.openai.chat-completions :as chat-completions][metabase.metabot.test-util :as metabot.tu]))@@ -149,3 +150,170 @@:input [{:role :user :content "hi"}]:temperature 0.2:max-tokens 128})))))++;;; ──────────────────────────────────────────────────────────────────+;;; Streaming chunk conversion tests+;;;+;;; The chunk shapes below are transcribed from real Moonshot (Kimi)+;;; streams captured on 2026-08-05.+;;; The behaviour they pin is provider-agnostic, but Moonshot is the+;;; provider that exercises every edge at once: usage reported in two+;;; places, cache reads reported both flat and nested, parallel tool+;;; calls serialized on a single choice, and reasoning deltas.+;;; ──────────────────────────────────────────────────────────────────++(def ^:private dual-location-usage+ "A Moonshot usage block. Cache reads are reported both flat (`:cached_tokens`) and nested+ (`:prompt_tokens_details`); reasoning tokens are a subset of `:completion_tokens`."+ {:prompt_tokens 4253+ :completion_tokens 20+ :total_tokens 4273+ :cached_tokens 4096+ :completion_tokens_details {:reasoning_tokens 1}+ :prompt_tokens_details {:cached_tokens 4096}})++(defn- chunk-types+ [chunks]+ (mapv :type (into [] (chat-completions/chat-completions->aisdk-chunks-xf) chunks)))++(defn- usage-parts+ [chunks]+ (filterv #(= :usage (:type %))+ (into [] (chat-completions/chat-completions->aisdk-chunks-xf) chunks)))++(deftest ^:parallel chunks-xf-usage-in-two-places-yields-one-usage-part-test+ (testing "a stream reporting usage both nested under the finishing choice and on a final top-level chunk emits exactly one :usage part"+ ;; Only the top-level `usage` key is read. `report-token-usage-xf` fires once per :usage part, so a+ ;; second one would double `ai_usage_log` rows, Snowplow/Prometheus token counts, and EE limit accounting.+ (is (= 1 (count (usage-parts+ [{:id "chatcmpl-1"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content ""} :finish_reason nil}]}+ {:choices [{:index 0+ :delta {:tool_calls [{:index 0+ :id "todo_read_0"+ :type "function"+ :function {:name "todo_read" :arguments ""}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments "{}"}}]}}]}+ {:choices [{:index 0+ :delta {}+ :finish_reason "tool_calls"+ :usage dual-location-usage}]}+ {:choices [] :usage dual-location-usage}]))))))++(deftest ^:parallel chunks-xf-cache-reads-come-from-prompt-tokens-details-test+ (testing "cacheReadTokens is read from prompt_tokens_details, and cached tokens are a subset of promptTokens"+ (is (=? {:type :usage+ :id "chatcmpl-1"+ :model "kimi-k2.6"+ :usage {:promptTokens 4253+ :completionTokens 20+ :cacheCreationTokens 0+ :cacheReadTokens 4096}}+ (first (usage-parts+ [{:id "chatcmpl-1"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content "hi"}}]}+ {:choices [] :usage dual-location-usage}]))))))++(deftest ^:parallel chunks-xf-flat-cached-tokens-are-not-read-test+ (testing "a flat cached_tokens with no prompt_tokens_details leaves cacheReadTokens at 0"+ ;; `usage->aisdk-usage` reads the nested bucket only. Providers that report cache reads flat *and*+ ;; nested (Moonshot) are covered by the nested read; one that reported only flat would need a+ ;; deliberate change here, not an incidental one.+ (is (=? {:usage {:promptTokens 100+ :cacheReadTokens 0}}+ (first (usage-parts+ [{:id "chatcmpl-2"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content "hi"}}]}+ {:choices []+ :usage {:prompt_tokens 100 :completion_tokens 5 :cached_tokens 64}}]))))))++(deftest ^:parallel chunks-xf-parallel-tool-calls-are-tracked-by-id-test+ (testing "tool calls serialized on one choice are split into separate blocks by tool-call id"+ ;; Both calls arrive on `choices[0]`; only the `tool_calls` entry's `id` distinguishes them.+ ;; The `index` field is present in the wire data and deliberately unread.+ (is (=? [{:type :start}+ {:type :tool-input+ :id "get_weather_0"+ :function "get_weather"+ :arguments {:city "Berlin"}}+ {:type :tool-input+ :id "get_population_1"+ :function "get_population"+ :arguments {:city "Berlin"}}+ {:type :usage}]+ (into [] (comp (chat-completions/chat-completions->aisdk-chunks-xf)+ (self.core/aisdk-xf))+ [{:id "chatcmpl-3"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content ""} :finish_reason nil}]}+ {:choices [{:index 0+ :delta {:tool_calls [{:index 0+ :id "get_weather_0"+ :type "function"+ :function {:name "get_weather" :arguments ""}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments "{\"city\":"}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments " \"Berlin\"}"}}]}}]}+ {:choices [{:index 0+ :delta {:tool_calls [{:index 1+ :id "get_population_1"+ :type "function"+ :function {:name "get_population" :arguments ""}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 1 :function {:arguments "{\"city\":"}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 1 :function {:arguments " \"Berlin\"}"}}]}}]}+ {:choices [{:index 0 :delta {} :finish_reason "tool_calls"}]}+ {:choices [] :usage {:prompt_tokens 138 :completion_tokens 36}}])))))++(deftest ^:parallel chunks-xf-reasoning-deltas-open-no-text-block-test+ (testing "reasoning_content deltas and empty-string content produce no chunks"+ ;; Reasoning is not replayable over Chat Completions, so it is dropped rather than surfaced as text.+ ;; An empty-string `content` between blocks must not open a text block either — that would close+ ;; the tool call that follows it.+ (is (= [:start :tool-input-start :tool-input-delta :tool-input-available :usage]+ (chunk-types+ [{:id "chatcmpl-4"+ :model "kimi-k2.6"+ :choices [{:index 0 :delta {:role "assistant" :content ""} :finish_reason nil}]}+ {:choices [{:index 0 :delta {:reasoning_content ""} :finish_reason nil}]}+ {:choices [{:index 0 :delta {:reasoning_content "Let me think about that"} :finish_reason nil}]}+ {:choices [{:index 0+ :delta {:tool_calls [{:index 0+ :id "todo_read_0"+ :type "function"+ :function {:name "todo_read" :arguments ""}}]}}]}+ {:choices [{:index 0 :delta {:tool_calls [{:index 0 :function {:arguments "{}"}}]}}]}+ {:choices [{:index 0 :delta {} :finish_reason "tool_calls"}]}+ {:choices [] :usage {:prompt_tokens 127 :completion_tokens 288}}])))))++;;; ──────────────────────────────────────────────────────────────────+;;; models-catalog tests+;;; ──────────────────────────────────────────────────────────────────++(deftest ^:parallel models-catalog-returns-data-test+ (testing "a well-formed catalog is returned as-is"+ (is (= [{:id "glm-5.2"}]+ (chat-completions/models-catalog "Z.AI" {:status 200 :body {:object "list" :data [{:id "glm-5.2"}]}}))))+ (testing "an empty catalog is a legitimate response, not a malformed one"+ (is (= []+ (chat-completions/models-catalog "Z.AI" {:status 200 :body {:object "list" :data []}})))))++(deftest ^:parallel models-catalog-throws-on-unrecognized-body-test+ (testing "a 2xx whose body carries no model list throws rather than yielding an empty catalog"+ (doseq [body [{:object "list"} "<html>Not Found</html>" nil {:data {:id "not-a-list"}}]]+ (testing (str "body " (pr-str body))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Mistral returned an unexpected model list response"+ (chat-completions/models-catalog "Mistral" {:status 200 :body body})))))))++(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.+ (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))))))diff --git a/test/metabase/metabot/self/zai_test.clj b/test/metabase/metabot/self/zai_test.cljindex 31bc0fb11e9..479f19432de 100644--- a/test/metabase/metabot/self/zai_test.clj+++ b/test/metabase/metabot/self/zai_test.clj@@ -289,3 +289,14 @@clojure.lang.ExceptionInfo#"Z\.AI API key expired or invalid"(zai/list-models)))))))++(deftest list-models-malformed-catalog-throws-test+ (testing "a 2xx whose body carries no model list throws instead of reporting an empty catalog"+ ;; Failing open here would let admin Connect succeed against a base URL we never reached,+ ;; leaving an empty model picker with no diagnostic.+ (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key.test"]+ (mt/with-dynamic-fn-redefs [http/request (fn [_] {:status 200 :body "<html>Not Found</html>"})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Z\.AI returned an unexpected model list response"+ (zai/list-models)))))))diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.cljindex 9b6ed26d8b7..be87b11be1b 100644--- a/test/metabase/metabot/self_test.clj+++ b/test/metabase/metabot/self_test.clj@@ -41,7 +41,9 @@(is (=? {:provider "zai" :model "glm-5.2" :ai-proxy? false}(#'self/parse-provider-model "zai/glm-5.2")))(is (=? {:provider "mistral" :model "mistral-medium-3-5" :ai-proxy? false}- (#'self/parse-provider-model "mistral/mistral-medium-3-5"))))+ (#'self/parse-provider-model "mistral/mistral-medium-3-5")))+ (is (=? {:provider "moonshot" :model "kimi-k2.6" :ai-proxy? false}+ (#'self/parse-provider-model "moonshot/kimi-k2.6"))))(testing "parses metabase/ prefix (AI proxy)"(is (=? {:provider "anthropic" :model "claude-haiku-4-5" :ai-proxy? true}(#'self/parse-provider-model "metabase/anthropic/claude-haiku-4-5")))@@ -60,7 +62,8 @@(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 "mistral")))+ (is (fn? (#'self/resolve-adapter "moonshot"))))(testing "throws for unknown provider"(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Unknown LLM provider"(#'self/resolve-adapter "unknown")))))@@ -162,7 +165,7 @@(is (not (contains? @captured :prompt_cache_key))))))))))(deftest call-llm-prompt-cache-key-not-leaked-to-other-providers-test- (testing "call-llm hands :prompt-cache-key to every adapter, but only mistral forwards it to the wire"+ (testing "call-llm hands :prompt-cache-key to every adapter, but only the adapters whose provider accepts it (mistral, moonshot) forward it to the wire"(let [captured (atom nil)](mt/with-premium-features #{:metabase-ai-managed};; `:api-error true` makes `rethrow-api-error!` rethrow as-is, so `::skip` survives on the outer ex-data.diff --git a/test/metabase/metabot/settings_test.clj b/test/metabase/metabot/settings_test.cljindex cfb16335980..c331582d137 100644--- a/test/metabase/metabot/settings_test.clj+++ b/test/metabase/metabot/settings_test.clj@@ -179,6 +179,13 @@clojure.lang.ExceptionInfo #"Unsupported provider \"mistral\" for metabase managed AI"(metabot.settings/llm-metabot-provider! "metabase/mistral/mistral-medium-3-5"))))))+(deftest validate-metabot-provider-rejects-direct-only-provider-as-managed-moonshot-test+ (testing "rejects moonshot under metabase/ prefix (not in the managed allow-list)"+ (mt/with-premium-features #{:metabase-ai-managed}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Unsupported provider \"moonshot\" for metabase managed AI"+ (metabot.settings/llm-metabot-provider! "metabase/moonshot/kimi-k2.6"))))))+(deftest validate-metabot-provider-rejects-unsupported-metabase-managed-model-test(testing "rejects unsupported model for an allowed metabase managed provider"(mt/with-premium-features #{:metabase-ai-managed}@@ -219,6 +226,13 @@(mt/with-temporary-setting-values [llm-metabot-provider "mistral/mistral-medium-3-5"](is (= "mistral/mistral-medium-3-5" (metabot.settings/llm-metabot-provider))))))+(deftest validate-metabot-provider-accepts-valid-direct-moonshot-test+ (testing "accepts valid direct moonshot provider strings"+ (mt/with-temporary-setting-values [llm-metabot-provider "moonshot/kimi-k2.6"]+ (is (= "moonshot/kimi-k2.6" (metabot.settings/llm-metabot-provider))))+ (mt/with-temporary-setting-values [llm-metabot-provider "moonshot/kimi-k3"]+ (is (= "moonshot/kimi-k3" (metabot.settings/llm-metabot-provider))))))+(deftest validate-metabot-provider-accepts-allowed-metabase-managed-provider-and-model-test(testing "accepts allow-listed metabase managed provider/model"(mt/with-premium-features #{:metabase-ai-managed}@@ -289,6 +303,14 @@(mt/with-temporary-setting-values [llm-mistral-api-key nil](is (nil? (metabot.settings/configured-provider-credentials "mistral"))))))+(deftest configured-provider-credentials-moonshot-test+ (testing "returns the api-key map when a moonshot key is configured, nil when blank or missing"+ (mt/with-temporary-setting-values [llm-moonshot-api-key "sk-moonshot-key-test"]+ (is (= {:api-key "sk-moonshot-key-test"}+ (metabot.settings/configured-provider-credentials "moonshot"))))+ (mt/with-temporary-setting-values [llm-moonshot-api-key nil]+ (is (nil? (metabot.settings/configured-provider-credentials "moonshot"))))))+(deftest configured-provider-credentials-bedrock-fully-configured-test(testing "returns the AWS credentials map when bedrock is fully configured"(mt/with-temporary-setting-values [llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"