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
Support multiple LLM provider connections at once (#78860)
Maksym Yakubych35a1199a3570
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 1d80345b160..22779e1fba5 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -403,6 +403,7 @@metabase.channel.render.core channel.rendermetabase.cloud-migration.core cloud-migrationmetabase.eid-translation.core eid-translation+ metabase.llm.test-util llm.tumetabase.login-history.core login-historymetabase.model-persistence.core model-persistencemetabase.notification.core notificationdiff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 920cbde8ce8..33040c9354a 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1268,6 +1268,7 @@{:team "Metabot":api #{metabase.llm.apimetabase.llm.init+ metabase.llm.providermetabase.llm.settingsmetabase.llm.startup}:uses #{analytics@@ -1278,6 +1279,7 @@lib-bemetabotmodels+ permissionspremium-featuresrequestsettings@@ -2274,6 +2276,7 @@channelformatterlib+ llmmetabotpermissionsquery-processor@@ -3116,7 +3119,7 @@enterprise/cloud-add-ons{:team "Cloud"- :api #{metabase-enterprise.cloud-add-ons.api}+ :api #{metabase-enterprise.cloud-add-ons.api metabase-enterprise.cloud-add-ons.core}:uses #{apieventspremium-features@@ -3469,6 +3472,7 @@metabot}:uses #{apilib-be+ llmmetabotmodelspermissions@@ -3476,6 +3480,7 @@settingstaskutil+ enterprise/cloud-add-onsenterprise/dependenciesenterprise/transforms-python}:model-imports #{:model/AiUsageLogdiff --git a/dev/src/dev/migrate.clj b/dev/src/dev/migrate.cljindex 38dbeba20fa..b204e32538d 100644--- a/dev/src/dev/migrate.clj+++ b/dev/src/dev/migrate.clj@@ -57,11 +57,12 @@[id](->> (t2/query-one {:select [[:%count.* :count]]:from [databasechangelog-name]- :where [:> :orderexecuted {:select [:orderexecuted]- :from [databasechangelog-name]- :where [:like :id (format "%s%%" id)]- :order-by [[:orderexecuted :desc]]- :limit 1}]+ :where [:> :orderexecuted ^:allow-subquery+ {:select [:orderexecuted]+ :from [databasechangelog-name]+ :where [:like :id (format "%s%%" id)]+ :order-by [[:orderexecuted :desc]]+ :limit 1}]:limit 1}):count;; includes the selected id@@ -83,10 +84,11 @@0 ;; don't rollback if there was just one deployment of everything(:count (t2/query-one {:select [[:%count.* :count]]:from [databasechangelog-name]- :where [:= :deployment_id {:select [:deployment_id]- :from [databasechangelog-name]- :order-by [[:orderexecuted :desc]]- :limit 1}]})))))+ :where [:= :deployment_id ^:allow-subquery+ {:select [:deployment_id]+ :from [databasechangelog-name]+ :order-by [[:orderexecuted :desc]]+ :limit 1}]})))))(defn reset-checksums![]@@ -203,7 +205,7 @@^ChangeSet change-set (first (filter #(= id (.getId ^ChangeSet %)) (.getSeenChangeSets list-visitor)))sql-generator-factory (SqlGeneratorFactory/getInstance)](reduce (fn [acc data]- ;; merge all changes in one change set into one single :forward and :rollback+ ;; merge all changes in one change set into one single :forward and :rollback(merge-with (fn [x y](str x "\n" y)) acc data)){}diff --git a/docs/configuring-metabase/config-template.md b/docs/configuring-metabase/config-template.mdindex b9ffe6cafb1..496de808d5b 100644--- a/docs/configuring-metabase/config-template.md+++ b/docs/configuring-metabase/config-template.md@@ -212,6 +212,8 @@ config:llm-anthropic-model: claude-opus-4-5-20251101llm-azure-api-base-url: nullllm-azure-api-key: null+ llm-azure-deployment-name: null+ llm-azure-model-family: nullllm-bedrock-access-key-id: nullllm-bedrock-region: us-east-1llm-bedrock-secret-access-key: null@@ -233,6 +235,7 @@ config:llm-openai-model: gpt-5.4llm-openrouter-api-base-url: https://openrouter.ai/apillm-openrouter-api-key: null+ llm-providers: []llm-rate-limit-per-ip: 100llm-rate-limit-per-user: 20llm-request-timeout-ms: 120000diff --git a/docs/configuring-metabase/environment-variables.md b/docs/configuring-metabase/environment-variables.mdindex a9a8da7fafd..179f6d4f356 100644--- a/docs/configuring-metabase/environment-variables.md+++ b/docs/configuring-metabase/environment-variables.md@@ -1298,6 +1298,8 @@ The array of last two ISO8601 dates when an admin dismissed the license token miThe Anthropic API base URL.+Backed by the anthropic connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_ANTHROPIC_API_KEY`- Type: string@@ -1306,6 +1308,8 @@ The Anthropic API base URL.The Anthropic API Key.+Backed by the anthropic connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_ANTHROPIC_MODEL`- Type: string@@ -1322,6 +1326,8 @@ The Anthropic model to use.The base URL of the Azure resource's OpenAI- or Anthropic-compatible surface, e.g. `https://<resource>.services.ai.azure.com/openai`.+Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_AZURE_API_KEY`- Type: string@@ -1330,6 +1336,28 @@ The base URL of the Azure resource's OpenAI- or Anthropic-compatible surface, e.The API key for the Azure resource hosting your models.+Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.++### `MB_LLM_AZURE_DEPLOYMENT_NAME`++- Type: string+- Default: `null`+- [Configuration file name](./config-file.md): `llm-azure-deployment-name`++The name of the model deployment served by the Azure connection configured from the environment.++Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.++### `MB_LLM_AZURE_MODEL_FAMILY`++- Type: string+- Default: `null`+- [Configuration file name](./config-file.md): `llm-azure-model-family`++Whether the Azure deployment configured from the environment serves an `openai` or an `anthropic` model. Defaults to `openai`.++Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_BEDROCK_ACCESS_KEY_ID`- Type: string@@ -1338,6 +1366,8 @@ The API key for the Azure resource hosting your models.The AWS Access Key ID for Amazon Bedrock.+Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_BEDROCK_REGION`- Type: string@@ -1346,6 +1376,8 @@ The AWS Access Key ID for Amazon Bedrock.The AWS region for Amazon Bedrock (e.g. us-east-1).+Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_BEDROCK_SECRET_ACCESS_KEY`- Type: string@@ -1354,6 +1386,8 @@ The AWS region for Amazon Bedrock (e.g. us-east-1).The AWS Secret Access Key for Amazon Bedrock.+Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_BEDROCK_SESSION_TOKEN`- Type: string@@ -1362,6 +1396,8 @@ The AWS Secret Access Key for Amazon Bedrock.The AWS Session Token for Amazon Bedrock. Only needed for temporary credentials.+Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_CONNECTION_TIMEOUT_MS`- Type: integer@@ -1378,6 +1414,8 @@ TCP connection timeout in milliseconds for LLM API requests. A provider that isThe Gemini Enterprise Agent Platform API base URL. Leave unset to derive it from the location.+Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_GOOGLE_LOCATION`- Type: string@@ -1386,6 +1424,8 @@ The Gemini Enterprise Agent Platform API base URL. Leave unset to derive it fromThe Google Cloud location for the Gemini Enterprise Agent Platform (e.g. us-central1). Defaults to global.+Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_GOOGLE_OAUTH_ACCESS_TOKEN`- Type: string@@ -1394,6 +1434,8 @@ The Google Cloud location for the Gemini Enterprise Agent Platform (e.g. us-centA short-lived OAuth2 access token for the Gemini Enterprise Agent Platform (e.g. from `gcloud auth print-access-token`). Useful for testing.+Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_GOOGLE_PROJECT_ID`- Type: string@@ -1402,6 +1444,8 @@ A short-lived OAuth2 access token for the Gemini Enterprise Agent Platform (e.g.The Google Cloud project ID for the Gemini Enterprise Agent Platform.+Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_GOOGLE_SERVICE_ACCOUNT_KEY`- Type: string@@ -1410,6 +1454,8 @@ The Google Cloud project ID for the Gemini Enterprise Agent Platform.A Google Cloud service account key JSON for the Gemini Enterprise Agent Platform. Takes precedence over the OAuth access token when both are set.+Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_MAX_TOKENS`- Type: integer@@ -1424,7 +1470,7 @@ Maximum tokens for LLM responses.- Default: `anthropic/claude-sonnet-4-6`- [Configuration file name](./config-file.md): `llm-metabot-provider`-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`.+The AI provider connection and model for Metabot. Format: connection-key/model-name, e.g. `anthropic/claude-haiku-4-5`, `openai/gpt-5.4`, `openrouter/anthropic/claude-haiku-4.5`. The connection key names an entry in the `llm-providers` setting and defaults to the provider type.### `MB_LLM_MISTRAL_API_BASE_URL`@@ -1434,6 +1480,8 @@ The AI provider and model for Metabot. Format: provider/model-name, e.g. `anthroThe Mistral API base URL used for Chat Completions.+Backed by the mistral connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_MISTRAL_API_KEY`- Type: string@@ -1442,6 +1490,8 @@ The Mistral API base URL used for Chat Completions.The Mistral API Key.+Backed by the mistral connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_MOONSHOT_API_BASE_URL`- Type: string@@ -1450,6 +1500,8 @@ The Mistral API Key.The Moonshot AI API base URL used for Chat Completions. Repoint this to use the `.cn` platform; keys are not interchangeable between the two.+Backed by the moonshot connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_MOONSHOT_API_KEY`- Type: string@@ -1458,6 +1510,8 @@ The Moonshot AI API base URL used for Chat Completions. Repoint this to use theThe Moonshot AI API Key.+Backed by the moonshot connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_OPENAI_API_BASE_URL`- Type: string@@ -1466,6 +1520,8 @@ The Moonshot AI API Key.The OpenAI API base URL.+Backed by the openai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_OPENAI_API_KEY`- Type: string@@ -1474,6 +1530,8 @@ The OpenAI API base URL.The OpenAI API Key.+Backed by the openai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_OPENAI_MODEL`- Type: string@@ -1490,6 +1548,8 @@ The OpenAI Model (e.g. 'gpt-5.5', 'gpt-5.4-mini').The OpenRouter API base URL used for Chat Completions.+Backed by the openrouter connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_OPENROUTER_API_KEY`- Type: string@@ -1498,6 +1558,20 @@ The OpenRouter API base URL used for Chat Completions.The OpenRouter API Key.+Backed by the openrouter connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.++### `MB_LLM_PROVIDERS`++- Type: json+- Default: `[]`+- [Configuration file name](./config-file.md): `llm-providers`++JSON array of configured LLM provider connections. Each entry has a `key` (a URL-safe slug identifying the connection), a `type` (the provider type, e.g. `anthropic`), a display `name`, and a `config` map of that provider type's credential fields.++Connections are normally managed from the admin AI settings page. Setting this environment variable puts the whole list under environment control and makes it read-only in the UI.++Configuring a provider through the single-provider variables (`MB_LLM_ANTHROPIC_API_KEY` and friends) is equally supported, and is the simpler option when you only need one connection per provider and would rather not hand-write JSON. Each such provider becomes a read-only connection whose key is the provider type, resolved from the environment on every read, so editing one of those variables is picked up on the next restart. A provider configured this way takes precedence over a stored connection with the same key.+### `MB_LLM_RATE_LIMIT_PER_IP`- Type: integer@@ -1530,6 +1604,8 @@ Socket (inter-byte read) timeout in milliseconds for LLM API requests. For streaThe Z.AI API base URL used for Chat Completions.+Backed by the zai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LLM_ZAI_API_KEY`- Type: string@@ -1538,6 +1614,8 @@ The Z.AI API base URL used for Chat Completions.The Z.AI API Key.+Backed by the zai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.+### `MB_LOAD_ANALYTICS_CONTENT`- Type: booleandiff --git a/e2e/support/helpers/e2e-explorations-helpers.ts b/e2e/support/helpers/e2e-explorations-helpers.tsindex 69e804c747e..84877320e53 100644--- a/e2e/support/helpers/e2e-explorations-helpers.ts+++ b/e2e/support/helpers/e2e-explorations-helpers.ts@@ -3,6 +3,7 @@ import type { StaticResponse } from "cypress/types/net-stubbing";import type { GetExplorationDataResponse } from "metabase-types/api";import { updateSetting } from "./api";+import { setupAnthropicLlmProvider } from "./e2e-metabot-helpers";import { activateToken } from "./e2e-token-helpers";const MOCK_LLM_PORT = 6125;@@ -10,16 +11,12 @@ const MOCK_LLM_RESPONSE = "Hello from Explorations!";export function enableExplorations(): void {activateToken("pro-self-hosted");- updateSetting("llm-anthropic-api-key", "sk-ant-test-key");updateSetting("metabot-enabled?", true);cy.task("startMockLlmServer", {port: MOCK_LLM_PORT,responseText: MOCK_LLM_RESPONSE,});- updateSetting(- "llm-anthropic-api-base-url",- `http://localhost:${MOCK_LLM_PORT}`,- );+ setupAnthropicLlmProvider({ baseUrl: `http://localhost:${MOCK_LLM_PORT}` });cy.intercept({method: "GET",pathname: "/api/exploration/dimensions",diff --git a/e2e/support/helpers/e2e-metabot-helpers.ts b/e2e/support/helpers/e2e-metabot-helpers.tsindex 3fd5976c521..bf8f79c1570 100644--- a/e2e/support/helpers/e2e-metabot-helpers.ts+++ b/e2e/support/helpers/e2e-metabot-helpers.ts@@ -8,6 +8,44 @@ import type {import { appBar } from "./e2e-ui-elements-helpers";+type LlmProviderConnectionInput = {+ key: string;+ type?: string;+ name?: string;+ config?: Record<string, string>;+};++export function setLlmProviders(connections: LlmProviderConnectionInput[]) {+ return cy.request("PUT", "/api/setting/llm-providers", {+ value: connections.map(({ key, type = key, name = key, config = {} }) => ({+ key,+ type,+ name,+ config,+ })),+ });+}++export function setupAnthropicLlmProvider({+ apiKey = "sk-ant-test-key",+ baseUrl,+}: { apiKey?: string; baseUrl?: string } = {}) {+ return setLlmProviders([+ {+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ config: baseUrl+ ? { "api-key": apiKey, "base-url": baseUrl }+ : { "api-key": apiKey },+ },+ ]);+}++export function clearLlmProviders() {+ return setLlmProviders([]);+}+export function metabotChatSidebar() {return cy.findByTestId("metabot-chat");}diff --git a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsxindex 13488d32279..8058e2b3f26 100644--- a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx+++ b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx@@ -67,7 +67,7 @@ const metabotRetryResponse = H.createMetabotSSEBody(describe("scenarios > embedding-sdk > metabot-question", () => {const setup = (response: string) => {signInAsAdminAndEnableEmbeddingSdk();- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();H.mockMetabotResponse({statusCode: 200,@@ -381,7 +381,7 @@ describe("scenarios > embedding-sdk > metabot-question", () => {describe("scenarios > embedding-sdk > metabot-question > enablement", () => {it("should show an error when embedded-metabot-enabled? is false", () => {signInAsAdminAndEnableEmbeddingSdk();- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.log("Disable embedded metabot");H.updateEnterpriseSettings({ "embedded-metabot-enabled?": false });diff --git a/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsxindex f65a7734922..91713f489a1 100644--- a/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx+++ b/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx@@ -112,7 +112,7 @@ const MetabotConsumer = ({ prompts, drills = false }: MetabotConsumerProps) => {describe("scenarios > embedding-sdk > use-metabot hook", () => {beforeEach(() => {signInAsAdminAndEnableEmbeddingSdk();- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.signOut();mockAuthProviderAndJwtSignIn();diff --git a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.tsindex 5e88e89e49b..397ef45f963 100644--- a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts@@ -702,7 +702,7 @@ LIMIT});it("should show the metabot button", () => {- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();visitTransformListPage();cy.button("Create a transform").click();H.popover().findByText("Query builder").click();diff --git a/e2e/test/scenarios/documents/document-metabot.cy.spec.ts b/e2e/test/scenarios/documents/document-metabot.cy.spec.tsindex 050a83065d1..932aa8c6e53 100644--- a/e2e/test/scenarios/documents/document-metabot.cy.spec.ts+++ b/e2e/test/scenarios/documents/document-metabot.cy.spec.ts@@ -24,11 +24,9 @@ describe("documents > metabot (#73690)", () => {},},});- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");- H.updateSetting(- "llm-anthropic-api-base-url",- `http://localhost:${MOCK_LLM_PORT}`,- );+ H.setupAnthropicLlmProvider({+ baseUrl: `http://localhost:${MOCK_LLM_PORT}`,+ });});afterEach(() => {diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.tsindex bae8305dc1d..6e074a57e92 100644--- a/e2e/test/scenarios/documents/documents.cy.spec.ts+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts@@ -779,7 +779,7 @@ describe("documents", () => {it("should support keyboard and mouse selection in suggestions without double highlight", () => {H.activateToken("pro-self-hosted");- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();H.visitDocument("@documentId");H.documentContent().click();diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-experience.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-experience.cy.spec.tsindex 541324481ec..23dc3459052 100644--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-experience.cy.spec.ts+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-experience.cy.spec.ts@@ -26,7 +26,7 @@ describe(suiteTitle, () => {H.updateSetting("enable-embedding-simple", true);H.updateSetting("enable-embedding-static", true);- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.intercept("GET", "/api/dashboard/*").as("dashboard");cy.intercept("POST", "/api/card/*/query").as("cardQuery");diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-options.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-options.cy.spec.tsindex 3a23045d922..13a6cb9b385 100644--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-options.cy.spec.ts+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding-setup/select-embed-options.cy.spec.ts@@ -90,7 +90,7 @@ describe(suiteTitle, () => {H.activateToken("pro-self-hosted");H.enableTracking();H.updateSetting("enable-embedding-simple", true);- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.intercept("GET", "/api/dashboard/**").as("dashboard");cy.intercept("POST", "/api/card/*/query").as("cardQuery");diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.tsindex 78bfaeb2d6b..1a9a966a53a 100644--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts@@ -433,7 +433,7 @@ describe("scenarios > embedding > sdk iframe embedding > custom elements api", (describe("<metabase-metabot>", () => {beforeEach(() => {- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();});it("should handle scrolling gracefully (metabase#67399)", () => {diff --git a/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts b/e2e/test/scenarios/metabot/ai-controls.cy.spec.tsindex d5bd69b4a37..536840cb020 100644--- a/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts+++ b/e2e/test/scenarios/metabot/ai-controls.cy.spec.ts@@ -740,11 +740,9 @@ function llmMockServerSetup() {responseText: MOCK_LLM_RESPONSE,});- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");- H.updateSetting(- "llm-anthropic-api-base-url",- `http://localhost:${MOCK_LLM_PORT}`,- );+ H.setupAnthropicLlmProvider({+ baseUrl: `http://localhost:${MOCK_LLM_PORT}`,+ });}function llmMockServerTeardown() {diff --git a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.tsindex fe5a75cc9ec..57364fac97b 100644--- a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts+++ b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts@@ -20,12 +20,12 @@ describe("Metabot Query Builder", () => {H.restore();cy.signInAsAdmin();H.activateToken("pro-self-hosted");- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");});it("should show setup guidance when llm-metabot-configured? is false", () => {- H.updateSetting("llm-anthropic-api-key", "");+ H.clearLlmProviders();cy.visit("/question/ask");cy.url().should("include", "/question/ask");cy.findByRole("button", { name: "connect to a model" }).should(diff --git a/e2e/test/scenarios/metabot/metabot.cy.spec.ts b/e2e/test/scenarios/metabot/metabot.cy.spec.tsindex 232332101d0..5ef6b783d2e 100644--- a/e2e/test/scenarios/metabot/metabot.cy.spec.ts+++ b/e2e/test/scenarios/metabot/metabot.cy.spec.ts@@ -10,7 +10,7 @@ describe("Metabot UI", () => {H.restore();cy.signInAsAdmin();H.activateToken("pro-self-hosted");- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");cy.intercept("GET", "/api/automagic-dashboards/database/*/candidates").as("xrayCandidates",@@ -123,7 +123,7 @@ describe("Metabot UI", () => {cy.signInAsAdmin();H.enableTracking();H.activateToken("pro-self-hosted");- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();});afterEach(() => {@@ -217,7 +217,7 @@ describe("Metabot in full-app embedding", () => {H.restore();cy.signInAsAdmin();H.activateToken("pro-self-hosted");- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();});it("should show the metabot button when embedded-metabot-enabled? is true", () => {diff --git a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.tsindex c4c15e27fdf..271b4e353ed 100644--- a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts+++ b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts@@ -40,9 +40,7 @@ describe("Native SQL generation", () => {H.restore();cy.signInAsAdmin();H.activateToken("pro-self-hosted");- cy.request("PUT", "/api/setting/llm-anthropic-api-key", {- value: "sk-ant-api03-test-token",- });+ H.setupAnthropicLlmProvider({ apiKey: "sk-ant-api03-test-token" });cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");});@@ -128,9 +126,7 @@ describe("Native SQL generation", () => {H.restore("postgres-12");cy.signInAsAdmin();H.activateToken("pro-self-hosted");- cy.request("PUT", "/api/setting/llm-anthropic-api-key", {- value: "sk-ant-api03-test-token",- });+ H.setupAnthropicLlmProvider({ apiKey: "sk-ant-api03-test-token" });cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");});diff --git a/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts b/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.tsindex 0091811b9ff..073b7298b08 100644--- a/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts+++ b/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts@@ -85,7 +85,7 @@ describe(cy.signInAsAdmin();H.activateToken("pro-self-hosted");H.updateSetting("transforms-enabled", true);- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });cy.intercept("POST", "/api/metabot/agent-streaming").as("agentReq");diff --git a/e2e/test/scenarios/monitor/ai-auditing/usage-auditing.cy.spec.ts b/e2e/test/scenarios/monitor/ai-auditing/usage-auditing.cy.spec.tsindex 821affdee72..43618139fa7 100644--- a/e2e/test/scenarios/monitor/ai-auditing/usage-auditing.cy.spec.ts+++ b/e2e/test/scenarios/monitor/ai-auditing/usage-auditing.cy.spec.ts@@ -77,7 +77,7 @@ describe("scenarios > monitor > ai auditing > usage auditing", () => {H.activateToken("pro-self-hosted");// MetabotAnalyticsAvailabilityLayout (usage stats + conversations) shows an empty// state with a "Go to AI Settings" CTA unless llm-metabot-configured? is true.- H.updateSetting("llm-anthropic-api-key", "sk-ant-test-key");+ H.setupAnthropicLlmProvider();seedUsageAuditingData();});diff --git a/e2e/test/scenarios/onboarding/setup/setup.cy.spec.ts b/e2e/test/scenarios/onboarding/setup/setup.cy.spec.tsindex 4a608ef1341..75705a7d6de 100644--- a/e2e/test/scenarios/onboarding/setup/setup.cy.spec.ts+++ b/e2e/test/scenarios/onboarding/setup/setup.cy.spec.ts@@ -560,22 +560,17 @@ describe("scenarios > setup > AI config step", () => {it("should offer BYOK providers without the managed option and allow skipping", () => {navigateToAiConfigStep();- cy.findByLabelText("Connect to an AI provider")- .findByLabelText("Provider")- .click();-- H.popover().within(() => {- cy.findByText("Anthropic").should("be.visible");- cy.findByText("OpenAI").should("be.visible");- cy.findByText("OpenRouter").should("be.visible");- cy.findByText("Microsoft Azure").should("be.visible");- cy.findByText("Amazon Bedrock").should("be.visible");- cy.findByText("Metabase").should("not.exist");- });- H.popover().findByText("Anthropic").click();-cy.findByLabelText("Connect to an AI provider").within(() => {- cy.findByLabelText("API key").should("be.visible");+ cy.findByRole("button", { name: /OpenAI/ }).should("be.visible");+ cy.findByRole("button", { name: /OpenRouter/ }).should("be.visible");+ cy.findByRole("button", { name: /Microsoft Azure/ }).should("be.visible");+ cy.findByRole("button", { name: /Amazon Bedrock/ }).should("be.visible");+ // the managed provider is offered but not connectable without the LLM proxy,+ // which e2e does not configure+ cy.findByRole("button", { name: /Metabase/ }).should("be.disabled");++ cy.findByRole("button", { name: /Anthropic/ }).click();+ cy.findByLabelText(/API key/).should("be.visible");cy.button("I'll set this up later").click();});@@ -591,25 +586,21 @@ describe("scenarios > setup > AI config step", () => {port: MOCK_LLM_PORT,responseText: "Hello from mock LLM!",});- cy.intercept("PUT", "/api/metabot/settings").as("connectProvider");+ cy.intercept("POST", "/api/llm/providers").as("connectProvider");navigateToAiConfigStep();- // The wizard runs as the freshly created admin, so settings can be- // updated mid-flow. Point the Anthropic client at the mock server that- // answers the credential-validating /v1/models call.- H.updateSetting(- "llm-anthropic-api-base-url",- `http://localhost:${MOCK_LLM_PORT}`,- );-- cy.findByLabelText("Connect to an AI provider")- .findByLabelText("Provider")- .click();- H.popover().findByText("Anthropic").click();-cy.findByLabelText("Connect to an AI provider").within(() => {- cy.findByLabelText("API key").type("sk-ant-api03-e2e-test-key");+ cy.findByRole("button", { name: /Anthropic/ }).click();+ cy.findByLabelText(/API key/).type("sk-ant-api03-e2e-test-key");++ // The base URL belongs to the connection now rather than to a global setting, so+ // this is what points the credential-verifying /v1/models call at the mock server.+ cy.findByRole("button", { name: /Advanced settings/ }).click();+ cy.findByLabelText("API base URL").type(+ `http://localhost:${MOCK_LLM_PORT}`,+ );+cy.button("Connect").click();cy.wait("@connectProvider");@@ -650,6 +641,16 @@ describe("scenarios > setup > AI config step", () => {});};+ const managedConnection = {+ key: "metabase",+ type: "metabase",+ name: "Metabase AI service",+ source: "db",+ usable: true,+ env_vars: [],+ config: {},+ };+let isConnected = false;cy.intercept("GET", "/api/session/properties", (req) => {req.reply((res) => {@@ -665,6 +666,20 @@ describe("scenarios > setup > AI config step", () => {}});});+ // the managed type is only connectable once the LLM proxy is configured, which has no+ // backing in e2e, so mark it available the same way the token features are patched+ cy.intercept("GET", "/api/llm/provider-types", (req) => {+ req.reply((res) => {+ res.body = res.body.map((providerType: { type: string }) =>+ providerType.type === "metabase"+ ? { ...providerType, available: true }+ : providerType,+ );+ });+ });+ cy.intercept("GET", "/api/llm/providers", (req) => {+ req.reply(isConnected ? [managedConnection] : []);+ });cy.intercept("GET", "/api/ee/cloud-add-ons/addons", [{id: 1,@@ -695,9 +710,9 @@ describe("scenarios > setup > AI config step", () => {updated_at: null,});cy.intercept("POST", "/api/premium-features/token/refresh", {});- cy.intercept("PUT", "/api/metabot/settings", (req) => {+ cy.intercept("POST", "/api/llm/providers", (req) => {isConnected = true;- req.reply({ value: "metabase/anthropic/claude-sonnet-4-6", models: [] });+ req.reply(managedConnection);}).as("connectManaged");cy.visit(@@ -728,16 +743,18 @@ describe("scenarios > setup > AI config step", () => {startAiConfigStep();cy.findByLabelText("Connect to an AI provider").within(() => {- cy.findByLabelText("Provider").should("have.value", "Metabase");- cy.findByText("About Metabase AI service").should("be.visible");+ cy.findByRole("button", { name: /Metabase/ }).click();+ cy.findByText(+ /The simplest way to get started with AI in Metabase/,+ ).should("be.visible");cy.findByText(/You get 1M tokens for free/).should("be.visible");cy.button("Connect").click();});cy.wait("@connectManaged").its("request.body")- .should("deep.equal", { provider: "metabase", model: "" });- cy.findByLabelText("Connected to Metabase").should("be.visible");+ .should("deep.equal", { type: "metabase" });+ cy.findByLabelText("Connected to Metabase AI service").should("be.visible");});it("should not offer the step when AI features are disabled", () => {diff --git a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.cljindex 64789345cba..bd9220dc7ef 100644--- a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj+++ b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj@@ -2,6 +2,7 @@"/api/ee/cloud-add-ons endpoints. "(:require[clj-http.client :as http]+ [metabase-enterprise.cloud-add-ons.core :as cloud-add-ons][metabase-enterprise.harbormaster.client :as hm.client][metabase.api.common :as api][metabase.api.macros :as api.macros]@@ -65,11 +66,7 @@"dwh-rent""etl-connections"])-(def ^:private add-on-bundles- "Product types whose purchase provisions additional add-ons in the same upsert call. Purchasing- Storage (`dwh-rent`) also provisions `etl-connections`, mirroring the store's storage purchase flow."- {"dwh-rent" [{:product-type "dwh-rent" :prepaid-units 0}- {:product-type "etl-connections" :prepaid-units 1}]})+(def ^:private add-on-bundles cloud-add-ons/add-on-bundles)(def ^:private bundle-only-product-types"Product types that are only ever provisioned as part of a bundle (see `add-on-bundles`) and can@@ -89,14 +86,6 @@[(cond-> {:product-type product-type}quantity (assoc :prepaid-units quantity))]))-(defn- add-ons-for-removal- "Add-ons to remove for a given `product-type`. Bundled product types (see `add-on-bundles`) expand- into all their members; everything else is a single add-on."- [product-type]- (if-let [bundle (add-on-bundles product-type)]- (mapv #(select-keys % [:product-type]) bundle)- [{:product-type product-type}]))-(defn- handle-store-api-error"Handle exceptions from Store API calls and return appropriate error response."[exception & [extra-status-mappings]]@@ -228,8 +217,7 @@:else(try- (hm.client/call :change-add-ons :remove-add-ons (add-ons-for-removal product-type))- (premium-features/clear-cache!)+ (cloud-add-ons/remove-add-on! product-type)response-success-empty(catch Exception e(log/warnf "Error removing add-on '%s': %s" product-type (ex-message e))diff --git a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/core.clj b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/core.cljnew file mode 100644index 00000000000..e3c2a3289c4--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/core.clj@@ -0,0 +1,27 @@+(ns metabase-enterprise.cloud-add-ons.core+ "Provisioning and removal of Metabase Cloud add-ons, shared by the `/api/ee/cloud-add-ons` endpoints and by+ features that cancel their own add-on when they are turned off."+ (:require+ [metabase-enterprise.harbormaster.client :as hm.client]+ [metabase.premium-features.core :as premium-features]))++(def add-on-bundles+ "Product types whose purchase provisions additional add-ons in the same upsert call. Purchasing+ Storage (`dwh-rent`) also provisions `etl-connections`, mirroring the store's storage purchase flow."+ {"dwh-rent" [{:product-type "dwh-rent" :prepaid-units 0}+ {:product-type "etl-connections" :prepaid-units 1}]})++(defn add-ons-for-removal+ "Add-ons to remove for a given `product-type`. Bundled product types (see [[add-on-bundles]]) expand+ into all their members; everything else is a single add-on."+ [product-type]+ (if-let [bundle (add-on-bundles product-type)]+ (mapv #(select-keys % [:product-type]) bundle)+ [{:product-type product-type}]))++(defn remove-add-on!+ "Cancel `product-type` with the Store and refresh this instance's token features. Throws whatever the Store+ client throws, so callers decide how to surface a failure."+ [product-type]+ (hm.client/call :change-add-ons :remove-add-ons (add-ons-for-removal product-type))+ (premium-features/clear-cache!))diff --git a/enterprise/backend/src/metabase_enterprise/metabot/api.clj b/enterprise/backend/src/metabase_enterprise/metabot/api.cljindex ff2c27b7790..8d5a7a0aefc 100644--- a/enterprise/backend/src/metabase_enterprise/metabot/api.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot/api.clj@@ -3,7 +3,7 @@(:require[clojure.string :as str][metabase.api.macros :as api.macros]- [metabase.metabot.provider-util :as provider-util]+ [metabase.llm.provider :as llm.provider][metabase.metabot.settings :as metabot.settings][metabase.permissions.core :as perms][metabase.premium-features.core :as premium-features]@@ -22,7 +22,7 @@(defn- default-metabase-meter-key[](some-> metabot.settings/default-metabase-llm-metabot-provider- provider-util/strip-metabase-prefix+ llm.provider/strip-managed-prefixu/qualified-name(str/replace-first "/" ":")(str ":tokens")))diff --git a/enterprise/backend/src/metabase_enterprise/metabot/provider.clj b/enterprise/backend/src/metabase_enterprise/metabot/provider.cljnew file mode 100644index 00000000000..55d0e9d4bc0--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/metabot/provider.clj@@ -0,0 +1,37 @@+(ns metabase-enterprise.metabot.provider+ "Enterprise behaviour for LLM provider connections."+ (:require+ [metabase-enterprise.cloud-add-ons.core :as cloud-add-ons]+ [metabase.premium-features.core :as premium-features :refer [defenterprise]]+ [metabase.util.log :as log]))++(def ^:private managed-ai-product-type "metabase-ai-managed")+(def ^:private tiered-ai-product-type "metabase-ai-tiered")++(defn- managed-ai-product-type-to-cancel+ "The add-on backing this instance's managed AI, or nil when there is nothing to cancel.++ An instance still on legacy tiered pricing only has its add-on cancelled when it could have moved to metered+ pricing; without that offer the tiered add-on is the only thing keeping Metabot working, so removing the+ connection leaves the subscription alone."+ []+ (cond+ (premium-features/has-feature? :metabase-ai-managed)+ managed-ai-product-type++ (and (premium-features/has-feature? :offer-metabase-ai-managed)+ (premium-features/has-feature? :metabot-v3))+ tiered-ai-product-type))++(defenterprise cancel-managed-ai-subscription!+ "Cancel the Metabase Cloud add-on that backs the Metabase-managed provider."+ :feature :none+ []+ (when (premium-features/is-hosted?)+ (when-let [product-type (managed-ai-product-type-to-cancel)]+ (try+ (cloud-add-ons/remove-add-on! product-type)+ (catch Exception e+ (log/warnf e "Error cancelling the %s add-on after removing the managed provider connection"+ product-type)+ (throw e))))))diff --git a/enterprise/backend/test/metabase_enterprise/internal_stats/metabot_test.clj b/enterprise/backend/test/metabase_enterprise/internal_stats/metabot_test.cljindex aa7bcbc0fc5..87b20d361d3 100644--- a/enterprise/backend/test/metabase_enterprise/internal_stats/metabot_test.clj+++ b/enterprise/backend/test/metabase_enterprise/internal_stats/metabot_test.clj@@ -3,6 +3,7 @@[clojure.test :refer [deftest is testing use-fixtures]][java-time.api :as t][metabase.internal-stats.metabot :as sut]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.conversation-title :as conversation-title][metabase.metabot.example-question-generator :as eqg][metabase.metabot.self.claude :as claude]@@ -17,6 +18,11 @@(use-fixtures :once (fixtures/initialize :db))+(use-fixtures :each (fn [thunk]+ (testing "with every provider type connected"+ (llm.tu/with-default-connections+ (thunk)))))+;; ---------------------------------------------------------------------------;; Helpers;; ---------------------------------------------------------------------------diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/AIProviderListUsage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/AIProviderListUsage.unit.spec.tsxnew file mode 100644index 00000000000..c72771a91cd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/AIProviderListUsage.unit.spec.tsx@@ -0,0 +1,201 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";+import {+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+ setupMetabaseManagedAiEndpoints,+ setupPropertiesEndpoints,+ setupSettingsEndpoints,+} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, within } from "__support__/ui";+import { AIProviderList } from "metabase/metabot";+import { reinitialize } from "metabase/plugins";+import { createMockState } from "metabase/redux/store/mocks";+import {+ createMockLlmProviderConnection,+ createMockLlmProviderType,+ createMockSettings,+ createMockTokenFeatures,+ createMockTokenStatus,+ createMockUser,+} from "metabase-types/api/mocks";++function setup({+ tokens = 4_000_000,+ freeTokens = null,+ hasManagedAi = true,+ hasDeprecatedAi = false,+ isLocked = false,+}: {+ tokens?: number | null;+ freeTokens?: number | null;+ hasManagedAi?: boolean;+ hasDeprecatedAi?: boolean;+ isLocked?: boolean;+} = {}) {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();++ const sessionProperties = createMockSettings({+ "is-hosted?": true,+ "llm-metabot-configured?": true,+ "token-features": createMockTokenFeatures({+ hosting: true,+ "metabase-ai-managed": hasManagedAi,+ "metabot-v3": hasDeprecatedAi,+ }),+ "token-status": createMockTokenStatus({+ features: [+ ...(hasManagedAi ? (["metabase-ai-managed"] as const) : []),+ ...(hasDeprecatedAi ? (["metabot-v3"] as const) : []),+ ],+ }),+ });++ setupPropertiesEndpoints(sessionProperties);+ setupSettingsEndpoints([]);+ setupMetabaseManagedAiEndpoints({+ metabasePricePerUnit: 3,+ metabotUsageQuota: {+ tokens,+ free_tokens: freeTokens,+ is_locked: isLocked,+ updated_at: null,+ },+ });+ fetchMock.post(+ "path:/api/premium-features/token/refresh",+ () => sessionProperties["token-status"],+ );++ setupLlmProviderTypesEndpoint([+ createMockLlmProviderType({+ type: "metabase",+ label: "Metabase AI service",+ managed: true,+ singleton: true,+ fields: [],+ }),+ createMockLlmProviderType(),+ ]);+ setupLlmProvidersEndpoint([+ createMockLlmProviderConnection({+ key: "metabase",+ type: "metabase",+ name: "Metabase AI service",+ }),+ createMockLlmProviderConnection(),+ ]);+ setupLlmModelsEndpoint([]);++ const storeInitialState = createMockState({+ currentUser: createMockUser({ is_superuser: true }),+ settings: mockSettings(sessionProperties),+ });++ setupEnterpriseOnlyPlugin("metabot");++ renderWithProviders(<AIProviderList />, { storeInitialState });+}++describe("AIProviderList managed provider usage", () => {+ afterEach(() => {+ reinitialize();+ });++ it("shows the billing cycle usage under the managed provider by default", async () => {+ setup({ tokens: 4_000_000 });++ const row = await screen.findByTestId("provider-metabase");+ expect(await within(row).findByText("Current billing cycle")).toBeVisible();+ expect(within(row).getByText("4,000,000")).toBeInTheDocument();+ expect(+ await within(row).findByText("$3.00 per 1M tokens"),+ ).toBeInTheDocument();+ expect(within(row).getByText("$12.00")).toBeInTheDocument();+ });++ it("shows the remaining free trial tokens when there are any left", async () => {+ setup({ tokens: 250_000, freeTokens: 1_000_000 });++ const row = await screen.findByTestId("provider-metabase");+ expect(await within(row).findByText("Included use")).toBeVisible();+ expect(within(row).getByText("Free trial tokens")).toBeInTheDocument();+ expect(within(row).getByText("250,000 / 1,000,000")).toBeInTheDocument();+ });++ it("collapses the usage details on demand", async () => {+ setup();++ const row = await screen.findByTestId("provider-metabase");+ expect(await within(row).findByText("Current billing cycle")).toBeVisible();++ await userEvent.click(+ within(row).getByRole("button", { name: "Usage and pricing" }),+ );++ expect(within(row).getByText("Current billing cycle")).not.toBeVisible();+ });++ it("collapses the usage details when the provider name itself is clicked", async () => {+ setup();++ const row = await screen.findByTestId("provider-metabase");+ expect(await within(row).findByText("Current billing cycle")).toBeVisible();++ await userEvent.click(within(row).getByText("Metabase AI service"));++ expect(within(row).getByText("Current billing cycle")).not.toBeVisible();+ });++ it("does not offer usage details for a self-managed provider", async () => {+ setup();++ const row = await screen.findByTestId("provider-anthropic");+ expect(+ within(row).queryByRole("button", { name: "Usage and pricing" }),+ ).not.toBeInTheDocument();+ });++ it("does not offer usage details on legacy tiered pricing with nothing to report", async () => {+ setup({ hasManagedAi: false, hasDeprecatedAi: true });++ const row = await screen.findByTestId("provider-metabase");+ expect(+ within(row).queryByRole("button", { name: "Usage and pricing" }),+ ).not.toBeInTheDocument();+ });++ it("offers to add a provider alongside the locked one, leaving the subscription and the connection alone", async () => {+ setup({ isLocked: true });++ const row = await screen.findByTestId("provider-metabase");+ await userEvent.click(+ await within(row).findByRole("button", {+ name: "Use a different AI provider",+ }),+ );++ const modal = await screen.findByRole("dialog", {+ name: "Add an AI provider",+ });+ expect(+ await within(modal).findByRole("button", { name: /Anthropic/ }),+ ).toBeInTheDocument();+ expect(+ within(modal).queryByRole("button", { name: /Metabase/ }),+ ).not.toBeInTheDocument();++ expect(+ fetchMock.callHistory.calls(+ "path:/api/ee/cloud-add-ons/metabase-ai-managed",+ { method: "DELETE" },+ ),+ ).toHaveLength(0);+ expect(screen.getByTestId("provider-metabase")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.tsxindex e70a13a1fef..7bff01e835c 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.tsx@@ -1,14 +1,14 @@+import type { ReactNode } from "react";import { useCallback, useEffect, useState } from "react";import { match } from "ts-pattern";import { jt, t } from "ttag";-import { useRefreshTokenStatusMutation } from "metabase/api";+import {+ useCreateLlmProviderMutation,+ useRefreshTokenStatusMutation,+} from "metabase/api";import { getErrorMessage } from "metabase/api/utils";import { getUserIsAdmin } from "metabase/current-user";-import {- useAIProviderConfigurationContext,- useUpdateMetabotSettingsMutation,-} from "metabase/metabot";import { MetabotManagedProviderLimitActions } from "metabase/metabot/components/MetabotManagedProviderLimit";import type { MetabaseAIProviderSetupProps } from "metabase/plugins";import { useSelector } from "metabase/redux";@@ -16,6 +16,7 @@ import { useSetting } from "metabase/settings";import {Anchor,Box,+ Button,Checkbox,Flex,Group,@@ -23,7 +24,6 @@ import {Skeleton,Stack,Text,- Title,Tooltip,UnstyledButton,} from "metabase/ui";@@ -31,15 +31,12 @@ import { formatNumber } from "metabase/utils/formatting";import {type MetabotUsageResponse,useGetMetabotUsageQuery,- useRemoveCloudAddOnMutation,} from "metabase-enterprise/api";import { hasPremiumFeature } from "metabase-enterprise/settings";import {METABASE_MANAGED_AI_FEATURE,- METABASE_MANAGED_AI_PRODUCT_TYPE,METABASE_MANAGED_AI_TERMS_URL,- METABASE_TIERED_AI_PRODUCT_TYPE,METABOT_V3_FEATURE,OFFER_METABASE_MANAGED_AI_FEATURE,} from "../../constants";@@ -52,8 +49,18 @@ import { usePurchaseMetabaseManagedAi } from "../../usePurchaseMetabaseManagedAiimport { MetabotSettingUpModal } from "./MetabotSettingUpModal";+export function hasMetabaseManagedProviderDetails() {+ return (+ !!hasPremiumFeature(METABASE_MANAGED_AI_FEATURE) ||+ (!!hasPremiumFeature(METABOT_V3_FEATURE) &&+ !!hasPremiumFeature(OFFER_METABASE_MANAGED_AI_FEATURE))+ );+}+export function MetabaseAIProviderSetup({onConnect,+ onCancel,+ isConnected,}: MetabaseAIProviderSetupProps) {const offerMetabaseManagedAi = !!hasPremiumFeature(OFFER_METABASE_MANAGED_AI_FEATURE,@@ -63,17 +70,18 @@ export function MetabaseAIProviderSetup({);const hasDeprecatedMetabaseAiProvider =!!hasPremiumFeature(METABOT_V3_FEATURE);- const isConfigured = !!useSetting("llm-metabot-configured?");+ const isMetabotConfigured = !!useSetting("llm-metabot-configured?");+ const isConfigured = isConnected ?? isMetabotConfigured;const isAdmin = useSelector(getUserIsAdmin);- const [updateMetabotSettings, updateMetabotSettingsResult] =- useUpdateMetabotSettingsMutation();+ const [createLlmProvider, createLlmProviderResult] =+ useCreateLlmProviderMutation();const handleConnect = useCallback(async () => {- await updateMetabotSettings({ provider: "metabase", model: "" }).unwrap();+ await createLlmProvider({ type: "metabase" }).unwrap();onConnect?.();- }, [onConnect, updateMetabotSettings]);+ }, [onConnect, createLlmProvider]);const {pricing: metabaseManagedAiPricing,@@ -83,9 +91,6 @@ export function MetabaseAIProviderSetup({);const metabaseManagedAiPurchase = usePurchaseMetabaseManagedAi();- const [removeCloudAddOn, removeCloudAddOnResult] =- useRemoveCloudAddOnMutation();-const [hasAcceptedTerms, setHasAcceptedTerms] = useState(false);const [isSettingUpModalOpen, setIsSettingUpModalOpen] = useState(false);@@ -103,7 +108,6 @@ export function MetabaseAIProviderSetup({}, [handleConnect, hasAcceptedTerms, metabaseManagedAiPurchase]);const connectAction = match({- hasAcceptedTerms,hasMetabaseManagedAiProviderFeature,hasDeprecatedMetabaseAiProvider,isConfigured,@@ -112,60 +116,14 @@ export function MetabaseAIProviderSetup({.with({ isConfigured: true }, () => null).with({ hasMetabaseManagedAiProviderFeature: true }, () => handleConnect).with({ hasDeprecatedMetabaseAiProvider: true }, () => handleConnect)- .with(- { hasMetabaseManagedAiProviderFeature: false, hasAcceptedTerms: false },- () => null,- ).with({ isAdmin: false }, () => null).otherwise(() => handleMetabasePurchase);- const onDisconnect = useCallback(async () => {- const feature = match({- offerMetabaseManagedAi,- hasMetabaseManagedAiProviderFeature,- hasDeprecatedMetabaseAiProvider,- })- .returnType<- | typeof METABASE_MANAGED_AI_PRODUCT_TYPE- | typeof METABASE_TIERED_AI_PRODUCT_TYPE- | null- >()- .with(- { hasMetabaseManagedAiProviderFeature: true },- () => METABASE_MANAGED_AI_PRODUCT_TYPE,- )- .with(- { offerMetabaseManagedAi: true, hasDeprecatedMetabaseAiProvider: true },- () => METABASE_TIERED_AI_PRODUCT_TYPE,- )- .with(- {- offerMetabaseManagedAi: false,- hasDeprecatedMetabaseAiProvider: true,- },- // If we can't upgrade to managed AI, we don't want to disable the existing one.- () => null,- )- .otherwise(() => {- throw new Error("No feature is enabled to cancel");- });-- if (!feature) {- return;- }-- await removeCloudAddOn({- product_type: feature,- }).unwrap();- }, [- offerMetabaseManagedAi,- hasMetabaseManagedAiProviderFeature,- hasDeprecatedMetabaseAiProvider,- removeCloudAddOn,- ]);+ const needsTermsAcceptance =+ connectAction === handleMetabasePurchase && !hasAcceptedTerms;- const { isMutating, handleDisconnect, resetProvider, isModal } =- useAIProviderConfigurationContext(connectAction, onDisconnect);+ const isMutating =+ createLlmProviderResult.isLoading || metabaseManagedAiPurchase.isLoading;const metabaseManagedAiPurchaseError = metabaseManagedAiPurchase.error? getErrorMessage(@@ -174,20 +132,13 @@ export function MetabaseAIProviderSetup({): undefined;- const updateMetabotSettingsError = updateMetabotSettingsResult.error+ const createLlmProviderError = createLlmProviderResult.error? getErrorMessage(- updateMetabotSettingsResult.error,+ createLlmProviderResult.error,t`Unable to connect to this AI provider.`,): undefined;- const removeMetabaseManagedAiError = removeCloudAddOnResult.error- ? getErrorMessage(- removeCloudAddOnResult.error,- t`Unable to disconnect from this AI provider.`,- )- : undefined;-return (<>{isConfigured ? (@@ -199,26 +150,19 @@ export function MetabaseAIProviderSetup({hasMetabaseManagedAiProviderFeature}offerMetabaseManagedAi={offerMetabaseManagedAi}- isModal={isModal}- resetProvider={resetProvider}- handleDisconnect={handleDisconnect}/>) : (<><Stack gap="md">- <Title order={4}>{- // eslint-disable-next-line metabase/no-literal-metabase-strings -- Metabase AI service- t`About Metabase AI service`- }</Title><Text>{// eslint-disable-next-line metabase/no-literal-metabase-strings -- Metabase AI servicet`The simplest way to get started with AI in Metabase. We pick a benchmarked, cost effective model for you, and billing is managed through your Metabase account.`}</Text>{isLoadingMetabaseManagedAiPricing ? (- <Group gap="xs" align="center">+ <PricingCallout><Skeleton h="1rem" w="14rem" />- <Skeleton h={14} w={14} circle />- </Group>+ <Skeleton h="1rem" w="18rem" />+ </PricingCallout>) : metabaseManagedAiPricing ? (<MetabasePricingText pricing={metabaseManagedAiPricing} />) : null}@@ -268,6 +212,28 @@ export function MetabaseAIProviderSetup({)}`}/>))}++ {(onCancel || connectAction) && (+ <Flex justify="end">+ <Group gap="sm">+ {onCancel && (+ <Button disabled={isMutating} onClick={onCancel}>+ {t`Back`}+ </Button>+ )}+ {connectAction && (+ <Button+ variant="filled"+ loading={isMutating}+ disabled={isMutating || needsTermsAcceptance}+ onClick={connectAction}+ >+ {t`Connect`}+ </Button>+ )}+ </Group>+ </Flex>+ )}</>)}@@ -277,22 +243,16 @@ export function MetabaseAIProviderSetup({</Text>)}- {updateMetabotSettingsError && (- <Text size="sm" c="feedback-negative">- {updateMetabotSettingsError}- </Text>- )}-- {removeMetabaseManagedAiError && (+ {createLlmProviderError && (<Text size="sm" c="feedback-negative">- {removeMetabaseManagedAiError}+ {createLlmProviderError}</Text>)}<MetabotSettingUpModalisSavingConfiguration={isSettingUpModalOpen &&- (updateMetabotSettingsResult.isLoading ||+ (createLlmProviderResult.isLoading ||metabaseManagedAiPurchase.isLoading)}opened={isSettingUpModalOpen}@@ -308,18 +268,12 @@ function MetabaseManagedProviderCard({hasDeprecatedMetabaseAiProvider,hasMetabaseManagedAiProviderFeature,offerMetabaseManagedAi,- isModal,- resetProvider,- handleDisconnect,}: {isLoadingPricing: boolean;pricing: MetabaseManagedAiPricing | null;hasDeprecatedMetabaseAiProvider: boolean;hasMetabaseManagedAiProviderFeature: boolean;offerMetabaseManagedAi: boolean;- isModal: boolean;- resetProvider: VoidFunction;- handleDisconnect: VoidFunction;}) {const { data: metabotUsage } = useGetMetabotUsageQuery();const isLocked = metabotUsage?.is_locked;@@ -357,11 +311,7 @@ function MetabaseManagedProviderCard({<Text c="text-secondary" fz="sm" lh={1.4}>{t`You've used all of your included AI service tokens. To keep using AI features you can either end your trial early and start your subscription, or stay in the trial and add your own AI provider API key.`}</Text>- <MetabotManagedProviderLimitActions- inline- mt="sm"- onConfigure={isModal ? resetProvider : handleDisconnect}- />+ <MetabotManagedProviderLimitActions inline mt="sm" /></Flex>)).with(@@ -479,32 +429,47 @@ function MetabasePricingRow({);}+function PricingCallout({ children }: { children: ReactNode }) {+ return (+ <Stack gap="xs" bg="background-secondary" p="md" bdrs="md">+ {children}+ </Stack>+ );+}+export function MetabasePricingText({pricing,}: {pricing: MetabaseManagedAiPricing;}) {return (- <Group gap="xs" align="center">- <Text lh="1">- {pricing.freeUnits- ? t`You get ${pricing.freeUnits} tokens for free. Price per token afterward - ${pricing.price} per ${pricing.unit} tokens`- : t`Price per token - ${pricing.price} per ${pricing.unit} tokens`}- </Text>- <Tooltip- label={t`Tokens are chunks of text used by AI models. Usage includes both prompts and responses.`}- multiline- maw="20rem"- >- <UnstyledButton- aria-label={t`AI pricing details`}- data-testid="metabase-ai-pricing-details"- style={{ lineHeight: 0 }}+ <PricingCallout>+ {pricing.freeUnits && (+ <Text fw="bold" lh="1">+ {t`You get ${pricing.freeUnits} tokens for free.`}+ </Text>+ )}+ <Group gap="xs" align="center">+ <Text lh="1">+ {pricing.freeUnits+ ? t`Price per token afterward - ${pricing.price} per ${pricing.unit} tokens`+ : t`Price per token - ${pricing.price} per ${pricing.unit} tokens`}+ </Text>+ <Tooltip+ label={t`Tokens are chunks of text used by AI models. Usage includes both prompts and responses.`}+ multiline+ maw="20rem">- <Icon name="info" size={14} c="text-secondary" />- </UnstyledButton>- </Tooltip>- </Group>+ <UnstyledButton+ aria-label={t`AI pricing details`}+ data-testid="metabase-ai-pricing-details"+ style={{ lineHeight: 0 }}+ >+ <Icon name="info" size={14} c="text-secondary" />+ </UnstyledButton>+ </Tooltip>+ </Group>+ </PricingCallout>);}diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.unit.spec.tsxindex 91ea9e3c6a5..5153550c8e9 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabaseAIProviderSetup.unit.spec.tsx@@ -1,8 +1,9 @@import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";+import fetchMock, { type RouteResponse } from "fetch-mock";import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import {+ setupCreateLlmProviderEndpoint,setupMetabaseManagedAiEndpoints,setupPropertiesEndpoints,} from "__support__/server-mocks";@@ -13,6 +14,7 @@ import { createMockState } from "metabase/redux/store/mocks";import type { MetabotUsageResponse } from "metabase-enterprise/api";import type { TokenStatusFeature } from "metabase-types/api";import {+ createMockLlmProviderConnection,createMockSettings,createMockTokenFeatures,createMockTokenStatus,@@ -41,7 +43,10 @@ type SetupOptions = {offerMetabaseManagedAi?: boolean;metabasePricePerUnit?: number;pauseAddOnsResponse?: boolean;+ purchaseCloudAddOnResponse?: RouteResponse;metabotUsageQuota?: MetabotUsageQuota | null;+ onConnect?: () => void;+ onCancel?: () => void;};function setup({@@ -52,7 +57,10 @@ function setup({offerMetabaseManagedAi = false,metabasePricePerUnit = 3.0,pauseAddOnsResponse = false,+ purchaseCloudAddOnResponse = 200,metabotUsageQuota = null,+ onConnect,+ onCancel,}: SetupOptions = {}) {fetchMock.removeRoutes();fetchMock.clearHistory();@@ -86,6 +94,7 @@ function setup({setupMetabaseManagedAiEndpoints({metabasePricePerUnit,+ purchaseCloudAddOnResponse,metabotUsageQuota: metabotUsageQuota? {tokens: metabotUsageQuota.tokens ?? null,@@ -109,6 +118,14 @@ function setup({() => sessionProperties["token-status"],);+ setupCreateLlmProviderEndpoint(+ createMockLlmProviderConnection({+ key: "metabase",+ type: "metabase",+ name: "Metabase AI",+ }),+ );+const storeInitialState = createMockState({currentUser: createMockUser({ is_superuser: isAdmin }),settings: mockSettings(sessionProperties),@@ -116,9 +133,12 @@ function setup({setupEnterpriseOnlyPlugin("metabot");- return renderWithProviders(<MetabaseAIProviderSetup />, {- storeInitialState,- });+ return renderWithProviders(+ <MetabaseAIProviderSetup onConnect={onConnect} onCancel={onCancel} />,+ {+ storeInitialState,+ },+ );}const PRICING: MetabaseManagedAiPricing = {@@ -151,10 +171,9 @@ describe("MetabaseAIProviderSetup", () => {setup();expect(- await screen.findByText("About Metabase AI service"),- ).toBeInTheDocument();- expect(- screen.getByText(/The simplest way to get started with AI in Metabase/),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();expect(await screen.findByText("Price per token - $3.00 per 1M tokens"),@@ -165,7 +184,9 @@ describe("MetabaseAIProviderSetup", () => {setup({ pauseAddOnsResponse: true });expect(screen.queryByText(/Price per token/i)).not.toBeInTheDocument();- expect(screen.getByText("About Metabase AI service")).toBeInTheDocument();+ expect(+ screen.getByText(/The simplest way to get started with AI in Metabase/),+ ).toBeInTheDocument();});it("shows the Terms of Service checkbox when no managed AI feature is enabled", async () => {@@ -214,7 +235,9 @@ describe("MetabaseAIProviderSetup", () => {setup({ hasManagedAi: true });expect(- await screen.findByText("About Metabase AI service"),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();expect(screen.queryByRole("checkbox", {@@ -230,7 +253,9 @@ describe("MetabaseAIProviderSetup", () => {});expect(- await screen.findByText("About Metabase AI service"),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();expect(screen.queryByRole("checkbox", {@@ -240,6 +265,139 @@ describe("MetabaseAIProviderSetup", () => {expect(screen.queryByText(/legacy tiered AI/i)).not.toBeInTheDocument();});+ it("creates a managed provider connection when the managed AI feature is already enabled", async () => {+ const onConnect = jest.fn();+ setup({ hasManagedAi: true, onConnect });++ await userEvent.click(+ await screen.findByRole("button", { name: "Connect" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: { type: "metabase" },+ }),+ ).toBe(true);+ });++ expect(+ fetchMock.callHistory.called(+ "path:/api/ee/cloud-add-ons/metabase-ai-managed",+ { method: "POST" },+ ),+ ).toBe(false);+ await waitFor(() => {+ expect(onConnect).toHaveBeenCalledTimes(1);+ });+ });++ it("keeps the Connect button visible but disabled until the Terms are accepted", async () => {+ setup();++ const connect = await screen.findByRole("button", { name: "Connect" });+ expect(connect).toBeDisabled();++ await userEvent.click(+ screen.getByRole("checkbox", {+ name: /I agree with the Metabase AI Service/i,+ }),+ );++ expect(connect).toBeEnabled();+ });++ it("returns to provider selection when requested by the connection flow", async () => {+ const onCancel = jest.fn();+ setup({ hasManagedAi: true, onCancel });++ await userEvent.click(+ await screen.findByRole("button", { name: "Back" }),+ );++ expect(onCancel).toHaveBeenCalledTimes(1);+ });++ it("waits for the add-on purchase to finish before creating the connection", async () => {+ let completePurchase = () => {};+ const purchased = new Promise<void>((resolve) => {+ completePurchase = resolve;+ });+ setup({ purchaseCloudAddOnResponse: () => purchased.then(() => 200) });++ await userEvent.click(+ await screen.findByRole("checkbox", {+ name: /I agree with the Metabase AI Service/i,+ }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ "path:/api/ee/cloud-add-ons/metabase-ai-managed",+ { method: "POST", body: { terms_of_service: true } },+ ),+ ).toBe(true);+ });+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ }),+ ).toBe(false);++ completePurchase();++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: { type: "metabase" },+ }),+ ).toBe(true);+ });+ });++ it("does not create the connection when the add-on purchase fails", async () => {+ setup({ purchaseCloudAddOnResponse: 500 });++ await userEvent.click(+ await screen.findByRole("checkbox", {+ name: /I agree with the Metabase AI Service/i,+ }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ "path:/api/ee/cloud-add-ons/metabase-ai-managed",+ { method: "POST" },+ ),+ ).toBe(true);+ });++ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ }),+ ).toBe(false);+ });++ it("does not offer a Connect button to non-admin users who must accept terms", async () => {+ setup({ isAdmin: false });++ expect(+ await screen.findByText(+ "Please ask an Admin user to enable this for you.",+ ),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Connect" }),+ ).not.toBeInTheDocument();+ });+it("asks non-admin users to contact an admin instead of showing the Terms checkbox", async () => {setup({ isAdmin: false });@@ -380,10 +538,9 @@ describe("MetabasePricingText", () => {<MetabasePricingText pricing={{ ...PRICING, freeUnits: "1M" }} />,);+ expect(screen.getByText("You get 1M tokens for free.")).toBeInTheDocument();expect(- screen.getByText(- "You get 1M tokens for free. Price per token afterward - $3.00 per 1M tokens",- ),+ screen.getByText("Price per token afterward - $3.00 per 1M tokens"),).toBeInTheDocument();});diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/index.ts b/enterprise/frontend/src/metabase-enterprise/metabot/index.tsindex f4fa3e51926..ad9987a6494 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/index.ts@@ -1,7 +1,10 @@import { PLUGIN_METABOT } from "metabase/plugins";import { hasPremiumFeature } from "metabase-enterprise/settings";-import { MetabaseAIProviderSetup } from "./components/MetabotAdmin/MetabaseAIProviderSetup";+import {+ MetabaseAIProviderSetup,+ hasMetabaseManagedProviderDetails,+} from "./components/MetabotAdmin/MetabaseAIProviderSetup";export function initializePlugin() {if (@@ -11,5 +14,7 @@ export function initializePlugin() {) {PLUGIN_METABOT.isEnabled = true;PLUGIN_METABOT.MetabaseAIProviderSetup = MetabaseAIProviderSetup;+ PLUGIN_METABOT.hasMetabaseManagedProviderDetails =+ hasMetabaseManagedProviderDetails;}}diff --git a/frontend/src/metabase-types/api/llm.ts b/frontend/src/metabase-types/api/llm.tsindex f5c574e2fc3..0b77b151b88 100644--- a/frontend/src/metabase-types/api/llm.ts+++ b/frontend/src/metabase-types/api/llm.ts@@ -32,3 +32,92 @@ export interface ExtractSourcesResponse {tables: ExtractSourcesTable[];card_ids: number[];}++export type LlmProviderTypeName =+ | "anthropic"+ | "openai"+ | "openrouter"+ | "mistral"+ | "zai"+ | "moonshot"+ | "google"+ | "azure"+ | "bedrock"+ | "metabase";++export type LlmProviderFieldType =+ | "text"+ | "password"+ | "select"+ | "segmented"+ | "file";++export interface LlmProviderField {+ key: string;+ label: string;+ type: LlmProviderFieldType;+ required: boolean;+ advanced: boolean;+ placeholder?: string | null;+ default?: string | null;+ help?: string | null;+ docs_url?: string | null;+ prefix?: string | null;+ options?: { value: string; label: string }[] | null;+ show_when?: { field: string; value: string } | null;+}++export interface LlmProviderType {+ type: LlmProviderTypeName;+ label: string;+ managed: boolean;+ singleton: boolean;+ available: boolean;+ default_model: string | null;+ models: LlmModel[];+ required_any: string[][];+ fields: LlmProviderField[];+}++export type LlmProviderConfig = Record<string, string | null>;++export type LlmProviderSource = "db" | "env";++export interface LlmProviderConnection {+ key: string;+ type: LlmProviderTypeName;+ name: string;+ source: LlmProviderSource;+ usable: boolean;+ env_vars: string[];+ env_fields: string[];+ config: LlmProviderConfig;+}++export interface LlmModel {+ id: string;+ display_name: string;+}++export interface LlmConnectionModels {+ key: string;+ name: string;+ type: LlmProviderTypeName;+ models: LlmModel[];+ error?: string | null;+}++export interface CreateLlmProviderRequest {+ type: string;+ name?: string;+ key?: string;+ config?: LlmProviderConfig;+ model?: string;+}++export interface UpdateLlmProviderRequest {+ key: string;+ name?: string;+ config?: LlmProviderConfig;+ model?: string;+}diff --git a/frontend/src/metabase-types/api/metabot.ts b/frontend/src/metabase-types/api/metabot.tsindex 8b167e200ed..5a43107c4b5 100644--- a/frontend/src/metabase-types/api/metabot.ts+++ b/frontend/src/metabase-types/api/metabot.ts@@ -191,57 +191,6 @@ export type ListMetabotConversationsResponse = PaginationResponse & {data: MetabotConversation[];};-export type MetabotProvider =- | "metabase"- | "anthropic"- | "azure"- | "bedrock"- | "google"- | "mistral"- | "moonshot"- | "openai"- | "openrouter"- | "zai";--export interface BedrockCredentials {- "access-key-id"?: string | null;- "secret-access-key"?: string | null;- region?: string | null;- "session-token"?: string | null;-}--export interface AzureCredentials {- "api-key"?: string | null;- "base-url"?: string | null;-}--export interface GoogleCredentials {- "service-account-key"?: string | null;- "oauth-access-token"?: string | null;- "project-id"?: string | null;- location?: string | null;-}--export interface MetabotCredentials- extends BedrockCredentials, AzureCredentials, GoogleCredentials {}--export interface MetabotSettingsResponse {- value: string | null;- "credentials-error"?: string | null;- models: {- id: string;- display_name: string;- group?: string | null;- }[];-}--export interface UpdateMetabotSettingsRequest {- provider: MetabotProvider;- model?: string;- "api-key"?: string | null;- credentials?: MetabotCredentials | null;-}-/* Metabot - Suggested Prompts */export type SuggestedMetabotPrompt = {diff --git a/frontend/src/metabase-types/api/mocks/index.ts b/frontend/src/metabase-types/api/mocks/index.tsindex 47f559085fb..6bd6f254e18 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -18,6 +18,7 @@ export * from "./geojson";export * from "./group";export * from "./index-manager";export * from "./insight";+export * from "./llm";export * from "./logger";export * from "./measure";export * from "./metric";diff --git a/frontend/src/metabase-types/api/mocks/llm.ts b/frontend/src/metabase-types/api/mocks/llm.tsnew file mode 100644index 00000000000..47dd5bd316b--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/llm.ts@@ -0,0 +1,63 @@+import type {+ LlmConnectionModels,+ LlmModel,+ LlmProviderConnection,+ LlmProviderField,+ LlmProviderType,+} from "../llm";++export const createMockLlmProviderField = (+ opts?: Partial<LlmProviderField>,+): LlmProviderField => ({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ advanced: false,+ ...opts,+});++export const createMockLlmProviderType = (+ opts?: Partial<LlmProviderType>,+): LlmProviderType => ({+ type: "anthropic",+ label: "Anthropic",+ managed: false,+ singleton: false,+ available: true,+ default_model: null,+ models: [],+ required_any: [],+ fields: [createMockLlmProviderField()],+ ...opts,+});++export const createMockLlmProviderConnection = (+ opts?: Partial<LlmProviderConnection>,+): LlmProviderConnection => ({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ source: "db",+ usable: true,+ env_vars: [],+ env_fields: [],+ config: {},+ ...opts,+});++export const createMockLlmModel = (opts?: Partial<LlmModel>): LlmModel => ({+ id: "claude-sonnet-4-5",+ display_name: "Claude Sonnet 4.5",+ ...opts,+});++export const createMockLlmConnectionModels = (+ opts?: Partial<LlmConnectionModels>,+): LlmConnectionModels => ({+ key: "anthropic",+ name: "Anthropic",+ type: "anthropic",+ models: [createMockLlmModel()],+ ...opts,+});diff --git a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.tsx b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.tsxindex 05d57259ac4..c0efb7253f9 100644--- a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.tsx+++ b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.tsx@@ -1,80 +1,26 @@-import { useMemo } from "react";-import { P, match } from "ts-pattern";import { t } from "ttag";import { SettingsSection } from "metabase/admin/components/SettingsSection";-import { skipToken } from "metabase/api";-import {- AIProviderConfigurationForm,- getProviderOptions,- parseProviderAndModel,- useGetMetabotSettingsQuery,-} from "metabase/metabot";-import { PLUGIN_METABOT } from "metabase/plugins";-import { useAdminSetting, useSetting } from "metabase/settings";-import { Badge, Flex, Group } from "metabase/ui";+import { useListLlmProvidersQuery } from "metabase/api";+import { AIProviderList } from "metabase/metabot";export function AIProviderSettingsSection({ id }: { id?: string }) {- const offerMetabaseAiManaged = PLUGIN_METABOT.isEnabled;- const { value: savedProviderValue } = useAdminSetting("llm-metabot-provider");- const config = useMemo(- () => parseProviderAndModel(savedProviderValue),- [savedProviderValue],- );- const isConfigured = !!useSetting("llm-metabot-configured?");- const connectedProvider = isConfigured ? config?.provider : undefined;- const connectedProviderSettingsQuery = useGetMetabotSettingsQuery(- connectedProvider && connectedProvider !== "metabase"- ? { provider: connectedProvider }- : skipToken,- );- const hasCredentialsError =- !!connectedProviderSettingsQuery.currentData?.["credentials-error"];+ const { data: connections = [] } = useListLlmProvidersQuery();++ const hasConnections = connections.length > 0;return (<SettingsSectionid={id}- title={- <Flex justify="space-between" align="center">- <Group gap="xs" wrap="nowrap">- {connectedProvider && (- <Badge- color={hasCredentialsError ? "negative" : "positive"}- indicator- mr="sm"- />- )}- <div>- {match({ connectedProvider, hasCredentialsError })- .with(- {- connectedProvider: P.nonNullable,- hasCredentialsError: true,- },- ({ connectedProvider }) =>- t`Error connecting to ${getProviderOptions(offerMetabaseAiManaged)[connectedProvider]?.label}`,- )- .with(- { connectedProvider: P.nonNullable },- ({ connectedProvider }) =>- t`Connected to ${getProviderOptions(offerMetabaseAiManaged)[connectedProvider]?.label}`,- )- .with(- { connectedProvider: P.nullish },- () => t`Connect to an AI provider`,- )- .exhaustive()}- </div>- </Group>- </Flex>- }+ stackProps={{ gap: "sm" }}+ title={hasConnections ? t`AI providers` : t`Connect to an AI provider`}description={- !connectedProvider+ !hasConnections? t`Select your AI provider to use AI explorations, SQL generation and Metabot.`- : undefined+ : t`Metabot can use models from any of these providers.`}>- <AIProviderConfigurationForm />+ <AIProviderList /></SettingsSection>);}diff --git a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsxindex f9bb12684d5..c329285a847 100644--- a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx@@ -1,3291 +1,800 @@import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";-import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import {- findRequests,- setupBillingEndpoints,- setupMetabaseManagedAiEndpoints,setupPropertiesEndpoints,+ setupSettingsEndpoints,+ setupUpdateSettingEndpoint,} from "__support__/server-mocks";-import { mockSettings } from "__support__/settings";-import { act, renderWithProviders, screen, waitFor } from "__support__/ui";-import { Api } from "metabase/api";import {- AIProviderConfigurationForm,- type MetabotApiKeyProvider,-} from "metabase/metabot";+ setupCreateLlmProviderEndpoint,+ setupDeleteLlmProviderEndpoint,+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+ setupUpdateLlmProviderEndpoint,+} from "__support__/server-mocks/metabot";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import { UndoListing } from "metabase/common/components/UndoListing";+import { AIProviderSetup } from "metabase/metabot";import { reinitialize } from "metabase/plugins";-import { Route } from "metabase/router";-import { defer } from "metabase/utils/promise";import type {- BedrockCredentials,- GoogleCredentials,- MetabotCredentials,- MetabotProvider,- MetabotSettingsResponse,- SettingDefinition,- TokenStatusFeature,+ LlmConnectionModels,+ LlmProviderConnection,+ LlmProviderType,} from "metabase-types/api";import {+ createMockLlmConnectionModels,+ createMockLlmModel,+ createMockLlmProviderConnection,+ createMockLlmProviderField,+ createMockLlmProviderType,createMockSettingDefinition,createMockSettings,- createMockTokenFeatures,- createMockTokenStatus,createMockUser,} from "metabase-types/api/mocks";import { AIProviderSettingsSection } from "./AIProviderSettingsSection";-const DEFAULT_RESPONSES: Record<MetabotProvider, MetabotSettingsResponse> = {- metabase: {- value: "metabase/anthropic/claude-sonnet-4-6",- models: [- {- id: "anthropic/claude-haiku-4-5",- display_name: "Claude Haiku 4.5",- },- {- id: "anthropic/claude-sonnet-4-6",- display_name: "Claude Sonnet 4.6",- },- {- id: "anthropic/claude-opus-4-1",- display_name: "Claude Opus 4.1",- },- ],- },- anthropic: {- value: "anthropic/claude-haiku-4-5",- models: [- {- id: "claude-haiku-4-5",- display_name: "Claude Haiku 4.5",- group: "Haiku",- },- {- id: "claude-sonnet-4-5",- display_name: "Claude Sonnet 4.5",- group: "Sonnet",- },- ],- },- azure: {- // Azure has no model dropdown — deployment names are free text.- value: "azure/anthropic/claude-sonnet-4-5",- models: [],- },- google: {- // Google has no model dropdown. Model IDs are free text, validated at connect time.- value: "google/google/gemini-3.5-flash",- models: [],- },- bedrock: {- value: "bedrock/anthropic.claude-haiku-4-5",- models: [- {- id: "anthropic.claude-haiku-4-5",- display_name: "anthropic.claude-haiku-4-5",- group: "Anthropic",- },- {- id: "openai.gpt-5.5",- display_name: "openai.gpt-5.5",- group: "OpenAI",- },- ],- },- mistral: {- 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: [- { id: "gpt-5.4", display_name: "gpt-5.4" },- { id: "gpt-5.4-mini", display_name: "gpt-5.4-mini" },- ],- },- openrouter: {- value: "openrouter/openai/gpt-5.4-mini",- models: [- {- id: "openai/gpt-5.4-mini",- display_name: "OpenAI: GPT-5.4 Mini",- group: "OpenAI",- },- ],- },- zai: {- value: "zai/glm-5.2",- models: [{ id: "glm-5.2", display_name: "GLM-5.2" }],- },-};--type MetabotUsageQuota = {- is_locked?: boolean;- tokens: number | null;- free_tokens?: number | null;- updated_at: string | null;-};--type MetabotSettingsApiResponse =- | MetabotSettingsResponse- | (() => Promise<MetabotSettingsResponse>);--type GoogleSettingKey =- | "llm-google-service-account-key"- | "llm-google-oauth-access-token"- | "llm-google-project-id"- | "llm-google-location";--type MetabotSettingKey =- | "llm-metabot-provider"- | "llm-anthropic-api-key"- | "llm-azure-api-key"- | "llm-azure-api-base-url"- | GoogleSettingKey- | "llm-mistral-api-key"- | "llm-moonshot-api-key"- | "llm-openai-api-key"- | "llm-openrouter-api-key"- | "llm-zai-api-key"- | "llm-bedrock-access-key-id"- | "llm-bedrock-secret-access-key"- | "llm-bedrock-region"- | "llm-bedrock-session-token";--const API_KEY_SETTING_BY_PROVIDER: Partial<- Record<MetabotProvider, MetabotSettingKey>-> = {- 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",-};--type MetabotSettingDefinition = SettingDefinition<MetabotSettingKey>;-type MetabotSettingsUpdateBody = {- provider: MetabotProvider;- model?: string;- "api-key"?: string | null;- credentials?: MetabotCredentials | null;-};--type SetupOptions = {- isHosted?: boolean;- hasDeprecatedMetabaseAiProvider?: boolean;- offerMetabaseManagedAi?: boolean;- llmProxyConfigured?: boolean;- savedProviderValue?: string | null;- isConfigured?: boolean;- providerSettingIsEnv?: boolean;- providerSettingEnvName?: string;- apiKeySettingIsEnv?: boolean;- apiKeySettingEnvName?: string;- isAdmin?: boolean;- anyStoreUserEmailAddress?: string;- metabasePricePerUnit?: number;- metabaseBillingPeriodMonths?: number;- metabotUsageQuotas?: MetabotUsageQuota[] | null;- tokenStatusFeatures?: TokenStatusFeature[];- refreshedTokenStatusFeatures?: TokenStatusFeature[];- purchaseCloudAddOnResponse?: number | { status: number; body: unknown };- deferPurchaseCloudAddOnResponse?: boolean;- removeCloudAddOnResponse?: number | { status: number; body: unknown };- apiKeyValues?: Partial<Record<MetabotProvider, string | null>>;- googleProjectIdValue?: string | null;- googleServiceAccountValue?: string | null;- googleOauthTokenValue?: string | null;- googleLocationValue?: string | null;- googleEnvSettings?: Partial<Record<GoogleSettingKey, string>>;- pauseUpdateResponse?: boolean;- deferMetabotSettingsUpdateResponse?: boolean;- settingUpdateResponse?: number | { status: number; body?: unknown };- metabotSettingsUpdateResponse?: number | { status: number; body?: unknown };- responses?: Partial<Record<MetabotProvider, MetabotSettingsApiResponse>>;- updateResponse?: MetabotSettingsResponse;- renderAsModal?: boolean;- onClose?: jest.Mock;-};--type GoogleSettingValues = Pick<- SetupOptions,- | "googleProjectIdValue"- | "googleServiceAccountValue"- | "googleOauthTokenValue"- | "googleLocationValue"- | "googleEnvSettings"->;--function createGoogleSettingDefinitions({- googleProjectIdValue = "my-project",- googleServiceAccountValue = null,- googleOauthTokenValue = null,- googleLocationValue = null,- googleEnvSettings = {},-}: GoogleSettingValues): Record<GoogleSettingKey, MetabotSettingDefinition> {- const createGoogleSetting = (key: GoogleSettingKey, value: string | null) => {- const envName = googleEnvSettings[key];- return createMockSettingDefinition({- key,- value: value ?? undefined,- is_env_setting: Boolean(envName),- env_name: envName,- });- };-- return {- "llm-google-service-account-key": createGoogleSetting(- "llm-google-service-account-key",- googleServiceAccountValue,- ),- "llm-google-oauth-access-token": createGoogleSetting(- "llm-google-oauth-access-token",- googleOauthTokenValue,- ),- // The project ID is saved with an OAuth token. A service account key includes its own.- "llm-google-project-id": createGoogleSetting(- "llm-google-project-id",- googleOauthTokenValue ? (googleProjectIdValue ?? null) : null,- ),- "llm-google-location": createGoogleSetting(- "llm-google-location",- googleLocationValue,- ),- };-}--async function setup({- isHosted = false,- hasDeprecatedMetabaseAiProvider,- offerMetabaseManagedAi,- llmProxyConfigured = isHosted,- savedProviderValue = "anthropic/claude-haiku-4-5",- isConfigured = true,- providerSettingIsEnv = false,- providerSettingEnvName = "LLM_METABOT_PROVIDER",- apiKeySettingIsEnv = false,- apiKeySettingEnvName = "LLM_ANTHROPIC_API_KEY",- isAdmin = false,- metabasePricePerUnit = 3.75,- metabaseBillingPeriodMonths = 1,- metabotUsageQuotas = null,- tokenStatusFeatures = [],- refreshedTokenStatusFeatures = tokenStatusFeatures,- purchaseCloudAddOnResponse = 200,- deferPurchaseCloudAddOnResponse = false,- removeCloudAddOnResponse = 200,- apiKeyValues,- googleProjectIdValue,- googleServiceAccountValue,- googleOauthTokenValue,- googleLocationValue,- googleEnvSettings,- pauseUpdateResponse = false,- deferMetabotSettingsUpdateResponse = false,- settingUpdateResponse = 204,- metabotSettingsUpdateResponse,- responses,- updateResponse = {- value: "anthropic/claude-sonnet-4-5",- models: DEFAULT_RESPONSES.anthropic.models,- },- renderAsModal = false,- onClose = jest.fn(),-}: SetupOptions = {}) {- fetchMock.removeRoutes();- fetchMock.clearHistory();-- const purchaseCloudAddOnDeferred = defer<void>();- const updateMetabotSettingsDeferred = defer<void>();-- const mergedApiKeyValues: Record<- MetabotApiKeyProvider | "azure" | "bedrock",- string | null- > = {- anthropic: "**********45",- azure: null,- bedrock: null,- mistral: null,- moonshot: null,- openai: null,- openrouter: null,- zai: null,- ...apiKeyValues,- };-- const createTokenFeatureFlags = (features: TokenStatusFeature[]) =>- createMockTokenFeatures({- hosting: isHosted,- "offer-metabase-ai-managed":- offerMetabaseManagedAi ??- (isHosted || features.includes("offer-metabase-ai-managed")),- "metabase-ai-managed": features.includes("metabase-ai-managed"),- "metabot-v3":- hasDeprecatedMetabaseAiProvider ?? features.includes("metabot-v3"),- });-- const sessionProperties = createMockSettings({- "is-hosted?": isHosted,- "llm-proxy-configured?": llmProxyConfigured,- "llm-metabot-provider": savedProviderValue,- "llm-metabot-configured?": isConfigured,- "token-features": createTokenFeatureFlags(tokenStatusFeatures),- "token-status": createMockTokenStatus({- features: tokenStatusFeatures,- }),- });-- setupPropertiesEndpoints(sessionProperties);-- const settingsDefinitions: Record<- MetabotSettingKey,- MetabotSettingDefinition- > = {- "llm-metabot-provider": createMockSettingDefinition({- key: "llm-metabot-provider",- value: savedProviderValue,- is_env_setting: providerSettingIsEnv,- env_name: providerSettingIsEnv ? providerSettingEnvName : undefined,- }),- "llm-anthropic-api-key": createMockSettingDefinition({- key: "llm-anthropic-api-key",- value: mergedApiKeyValues.anthropic ?? undefined,- is_env_setting: apiKeySettingIsEnv,- env_name: apiKeySettingIsEnv ? apiKeySettingEnvName : undefined,- }),- "llm-azure-api-key": createMockSettingDefinition({- key: "llm-azure-api-key",- value: mergedApiKeyValues.azure ?? undefined,- }),- // The base URL is configured whenever the Azure API key is — they are saved together.- "llm-azure-api-base-url": createMockSettingDefinition({- key: "llm-azure-api-base-url",- value: mergedApiKeyValues.azure- ? "https://my-resource.services.ai.azure.com/anthropic"- : undefined,+const ANTHROPIC_TYPE = createMockLlmProviderType({+ type: "anthropic",+ label: "Anthropic",+ fields: [+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ prefix: "sk-ant-",}),- ...createGoogleSettingDefinitions({- googleProjectIdValue,- googleServiceAccountValue,- googleOauthTokenValue,- googleLocationValue,- googleEnvSettings,+ createMockLlmProviderField({+ key: "base-url",+ label: "API base URL",+ type: "text",+ required: false,+ advanced: true,+ default: "https://api.anthropic.com",}),- "llm-mistral-api-key": createMockSettingDefinition({- key: "llm-mistral-api-key",- value: mergedApiKeyValues.mistral ?? undefined,- }),- "llm-moonshot-api-key": createMockSettingDefinition({- key: "llm-moonshot-api-key",- value: mergedApiKeyValues.moonshot ?? undefined,+ ],+});++const AZURE_TYPE = createMockLlmProviderType({+ type: "azure",+ label: "Azure",+ fields: [+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,}),- "llm-openai-api-key": createMockSettingDefinition({- key: "llm-openai-api-key",- value: mergedApiKeyValues.openai ?? undefined,+ createMockLlmProviderField({+ key: "api-base-url",+ label: "Base URL",+ type: "text",+ required: true,}),- "llm-openrouter-api-key": createMockSettingDefinition({- key: "llm-openrouter-api-key",- value: mergedApiKeyValues.openrouter ?? undefined,+ createMockLlmProviderField({+ key: "model-family",+ label: "Model provider",+ type: "select",+ required: true,+ default: "openai",+ options: [+ { value: "openai", label: "OpenAI" },+ { value: "anthropic", label: "Anthropic" },+ ],}),- "llm-zai-api-key": createMockSettingDefinition({- key: "llm-zai-api-key",- value: mergedApiKeyValues.zai ?? undefined,+ createMockLlmProviderField({+ key: "deployment-name",+ label: "Deployment name",+ type: "text",+ required: true,}),- "llm-bedrock-access-key-id": createMockSettingDefinition({- key: "llm-bedrock-access-key-id",- value: mergedApiKeyValues.bedrock ?? undefined,+ ],+});++const BEDROCK_TYPE = createMockLlmProviderType({+ type: "bedrock",+ label: "Amazon Bedrock",+ fields: [+ createMockLlmProviderField({+ key: "access-key-id",+ label: "Access key ID",+ type: "password",+ required: true,}),- // The secret access key is configured whenever the access key ID is — they are saved together.- "llm-bedrock-secret-access-key": createMockSettingDefinition({- key: "llm-bedrock-secret-access-key",- value: mergedApiKeyValues.bedrock ? "**********ET" : undefined,+ createMockLlmProviderField({+ key: "secret-access-key",+ label: "Secret access key",+ type: "password",+ required: true,}),- "llm-bedrock-region": createMockSettingDefinition({- key: "llm-bedrock-region",- value: mergedApiKeyValues.bedrock ? "us-east-1" : undefined,+ createMockLlmProviderField({+ key: "region",+ label: "Region",+ type: "select",+ required: false,+ advanced: false,+ default: "us-east-1",+ options: [+ { value: "us-east-1", label: "us-east-1" },+ { value: "eu-central-1", label: "eu-central-1" },+ ],}),- "llm-bedrock-session-token": createMockSettingDefinition({- key: "llm-bedrock-session-token",- value: mergedApiKeyValues.bedrock ? "**********EN" : undefined,+ createMockLlmProviderField({+ key: "session-token",+ label: "Session token",+ type: "password",+ required: false,+ advanced: true,}),- };-- fetchMock.get("path:/api/setting", () => Object.values(settingsDefinitions));-- const responseMap: Record<MetabotProvider, MetabotSettingsApiResponse> = {- ...DEFAULT_RESPONSES,- ...responses,- };-- if (isHosted) {- setupBillingEndpoints({- billingPeriodMonths: metabaseBillingPeriodMonths,- hasBasicTransformsAddOn: false,- hasAdvancedTransformsAddOn: false,- skipCloudAddOns: true,- });- setupMetabaseManagedAiEndpoints({- billingPeriodMonths: metabaseBillingPeriodMonths,- metabasePricePerUnit,- metabotUsageQuota: metabotUsageQuotas?.[0] ?? null,- purchaseCloudAddOnResponse: deferPurchaseCloudAddOnResponse- ? async () => {- await purchaseCloudAddOnDeferred.promise;- return purchaseCloudAddOnResponse;- }- : purchaseCloudAddOnResponse,- removeCloudAddOnResponse,- });-- fetchMock.post("path:/api/premium-features/token/refresh", () => {- sessionProperties["token-features"] = createTokenFeatureFlags(- refreshedTokenStatusFeatures,- );- sessionProperties["token-status"] = createMockTokenStatus({- features: refreshedTokenStatusFeatures,- });-- return sessionProperties["token-status"];- });- }-- const settings = mockSettings(sessionProperties);- setupEnterpriseOnlyPlugin("metabot");+ ],+});- // Unjustified type cast. FIXME- for (const provider of Object.keys(responseMap) as MetabotProvider[]) {- const response = responseMap[provider];+const ANTHROPIC_CONNECTION = createMockLlmProviderConnection({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ config: { "api-key": "sk-ant-saved" },+});- fetchMock.get({- url: "path:/api/metabot/settings",- query: { provider },- response,- });- }+const AZURE_CONNECTION = createMockLlmProviderConnection({+ key: "azure-prod",+ type: "azure",+ name: "Azure prod",+ config: { "api-key": "azure-saved", "api-base-url": "https://azure.test" },+});- fetchMock.put("path:/api/metabot/settings", (call) => {- if (metabotSettingsUpdateResponse !== undefined) {- return metabotSettingsUpdateResponse;- }+const CONNECTION_MODELS = [+ createMockLlmConnectionModels({+ key: "anthropic",+ name: "Anthropic",+ type: "anthropic",+ models: [+ createMockLlmModel({+ id: "claude-sonnet-4-5",+ display_name: "Claude Sonnet 4.5",+ }),+ createMockLlmModel({+ id: "claude-haiku-4-5",+ display_name: "Claude Haiku 4.5",+ }),+ ],+ }),+ createMockLlmConnectionModels({+ key: "azure-prod",+ name: "Azure prod",+ type: "azure",+ models: [createMockLlmModel({ id: "gpt-5", display_name: "GPT-5" })],+ }),+];- if (pauseUpdateResponse) {- return new Promise(() => undefined);- }+type SetupOpts = {+ connections?: LlmProviderConnection[];+ providerTypes?: LlmProviderType[];+ models?: LlmConnectionModels[];+ modelRef?: string | null;+ modelRefEnvVar?: string;+ providerTypesFail?: boolean;+ createdConnection?: LlmProviderConnection;+ updatedConnection?: LlmProviderConnection;+};- if (deferMetabotSettingsUpdateResponse) {- return updateMetabotSettingsDeferred.promise.then(() =>- handleMetabotSettingsUpdate(call),- );- }+async function setup({+ connections = [],+ providerTypes = [ANTHROPIC_TYPE, AZURE_TYPE, BEDROCK_TYPE],+ models = [],+ modelRef = null,+ modelRefEnvVar,+ providerTypesFail = false,+ createdConnection = ANTHROPIC_CONNECTION,+ updatedConnection = ANTHROPIC_CONNECTION,+}: SetupOpts = {}) {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();- return handleMetabotSettingsUpdate(call);+ const sessionProperties = createMockSettings({+ "llm-metabot-provider": modelRef,});- const handleMetabotSettingsUpdate = (call: { options?: RequestInit }) => {- // Unjustified type cast. FIXME- const body = JSON.parse(- String(call.options?.body ?? "{}"),- ) as MetabotSettingsUpdateBody;-- const apiKeySettingKey = API_KEY_SETTING_BY_PROVIDER[body.provider];-- if ("api-key" in body && apiKeySettingKey) {- const maskedApiKey = body["api-key"]- ? `**********${String(body["api-key"]).slice(-2)}`- : undefined;-- settingsDefinitions[apiKeySettingKey] = createMockSettingDefinition({- ...settingsDefinitions[apiKeySettingKey],- key: apiKeySettingKey,- value: maskedApiKey,- });- }-- if (body.provider === "bedrock" && "credentials" in body) {- const mask = (value: string | null | undefined) =>- value ? `**********${String(value).slice(-2)}` : undefined;-- // `credentials: null` is an explicit clear — the backend wipes all the saved key material.- // Fields inside the map follow the same presence contract: an absent field keeps the saved- // value, a null field clears it.- const requestCredentials = body.credentials ?? null;- const updateBedrockSetting = (- settingKey:- | "llm-bedrock-access-key-id"- | "llm-bedrock-secret-access-key"- | "llm-bedrock-session-token",- field: keyof BedrockCredentials,- ) => {- if (requestCredentials !== null && !(field in requestCredentials)) {- return;- }- settingsDefinitions[settingKey] = createMockSettingDefinition({- ...settingsDefinitions[settingKey],- key: settingKey,- value: mask(requestCredentials?.[field]),- });- };-- updateBedrockSetting("llm-bedrock-access-key-id", "access-key-id");- updateBedrockSetting(- "llm-bedrock-secret-access-key",- "secret-access-key",- );- updateBedrockSetting("llm-bedrock-session-token", "session-token");- }-- if (body.provider === "google" && "credentials" in body) {- const mask = (value: string | null | undefined) =>- value ? `**********${String(value).slice(-2)}` : undefined;-- // Same rules as Bedrock. `credentials: null` clears everything. In the map, an absent- // field keeps the saved value, and a null field clears it.- const requestCredentials = body.credentials ?? null;- const updateGoogleSetting = (- settingKey: GoogleSettingKey,- field: keyof GoogleCredentials,- masked: boolean,- ) => {- if (requestCredentials !== null && !(field in requestCredentials)) {- return;- }- const nextValue = requestCredentials?.[field];- settingsDefinitions[settingKey] = createMockSettingDefinition({- ...settingsDefinitions[settingKey],- key: settingKey,- value: masked ? mask(nextValue) : (nextValue ?? undefined),- });- };-- updateGoogleSetting(- "llm-google-service-account-key",- "service-account-key",- true,- );- updateGoogleSetting(- "llm-google-oauth-access-token",- "oauth-access-token",- true,- );- updateGoogleSetting("llm-google-project-id", "project-id", false);- updateGoogleSetting("llm-google-location", "location", false);- }-- if ("model" in body) {- sessionProperties["llm-metabot-provider"] = updateResponse.value;- sessionProperties["llm-metabot-configured?"] = true;- settingsDefinitions["llm-metabot-provider"] = createMockSettingDefinition(- {- ...settingsDefinitions["llm-metabot-provider"],- key: "llm-metabot-provider",- value: updateResponse.value,- },- );-- return updateResponse;- }-- const providerResponse = responseMap[body.provider];- const providerSettings =- typeof providerResponse === "function"- ? DEFAULT_RESPONSES[body.provider]- : providerResponse;-- return {- value: sessionProperties["llm-metabot-provider"],- models: providerSettings.models,- };- };-- fetchMock.put("path:/api/setting", (call) => {- if (settingUpdateResponse !== 204) {- return settingUpdateResponse;- }-- // Unjustified type cast. FIXME- const body = JSON.parse(String(call.options?.body ?? "{}")) as Partial<- Record<MetabotSettingKey, string | null>- >;-- Object.entries(body).forEach(([key, nextValue]) => {- if (key in settingsDefinitions) {- // Unjustified type cast. FIXME- settingsDefinitions[key as keyof typeof settingsDefinitions] =- createMockSettingDefinition({- // Unjustified type cast. FIXME- ...settingsDefinitions[key as keyof typeof settingsDefinitions],- // Unjustified type cast. FIXME- key: key as keyof typeof settingsDefinitions,- // Unjustified type cast. FIXME- value: (nextValue ??- undefined) as MetabotSettingDefinition["value"],- });- }-- if (key === "llm-metabot-provider") {- sessionProperties["llm-metabot-provider"] = nextValue ?? null;- sessionProperties["llm-metabot-configured?"] = Boolean(nextValue);- }- });-- return 204;- });+ setupPropertiesEndpoints(sessionProperties);+ setupSettingsEndpoints([+ createMockSettingDefinition({+ key: "llm-metabot-provider",+ value: modelRef,+ is_env_setting: modelRefEnvVar != null,+ env_name: modelRefEnvVar,+ }),+ ]);+ setupUpdateSettingEndpoint();+ if (providerTypesFail) {+ fetchMock.get("path:/api/llm/provider-types", {+ status: 500,+ body: { message: "Provider types are unavailable" },+ });+ } else {+ setupLlmProviderTypesEndpoint(providerTypes);+ }+ setupLlmProvidersEndpoint(connections);+ setupLlmModelsEndpoint(models);+ setupCreateLlmProviderEndpoint(createdConnection);+ setupUpdateLlmProviderEndpoint(updatedConnection);+ setupDeleteLlmProviderEndpoint();++ const view = renderWithProviders(+ <>+ <AIProviderSettingsSection />+ <UndoListing />+ </>,+ {+ storeInitialState: {+ settings: mockSettings(sessionProperties),+ currentUser: createMockUser({ is_superuser: true }),+ },+ },+ );- const user = createMockUser({ is_superuser: isAdmin });-- const storeInitialState = { settings, currentUser: user };- const view = renderAsModal- ? renderWithProviders(- <AIProviderConfigurationForm isModal onClose={onClose} />,- {- storeInitialState,- },- )- : renderWithProviders(- <Route- path="/admin/metabot*"- element={<AIProviderSettingsSection />}- />,- {- withRouter: true,- initialRoute: "/admin/metabot",- storeInitialState,- },- );-- if (!isHosted && !renderAsModal) {- await screen.findByText(- isConfigured- ? /Connected to|Connect to an AI provider/- : "Connect to an AI provider",- );+ if (providerTypesFail) {+ await screen.findByText(/Provider types are unavailable/);+ } else if (connections.length > 0) {+ await screen.findByText(connections[0].name);+ } else {+ await screen.findByRole("button", { name: /Add a provider/ });}- return {- ...view,- onClose,- resolvePurchaseCloudAddOnResponse: () =>- purchaseCloudAddOnDeferred.resolve(),- resolveMetabotSettingsUpdateResponse: () =>- updateMetabotSettingsDeferred.resolve(),- };+ return view;}-async function openModelSelector() {- await userEvent.click(screen.getByLabelText("Model"));- await userEvent.keyboard("{ArrowDown}");+async function openAddProviderModal() {+ await userEvent.click(screen.getByRole("button", { name: /Add a provider/ }));+ return await screen.findByRole("dialog");}-async function selectProvider(name: string) {- await userEvent.click(screen.getByLabelText("Provider"));+async function selectOption(name: string) {await userEvent.click(await screen.findByRole("option", { name }));}-async function openDisconnectProviderModal() {+async function openProviderMenu() {await userEvent.click(- await screen.findByRole("button", { name: "Disconnect" }),+ screen.getByRole("button", { name: "Provider options" }),);}-async function confirmDisconnectProvider() {- await openDisconnectProviderModal();+async function openAdvancedSettings(modal: HTMLElement) {await userEvent.click(- await screen.findByRole("button", { name: "Disconnect provider" }),+ within(modal).getByRole("button", { name: /Advanced settings/ }),);}-const METABOT_SETTINGS_PATH = "/api/metabot/settings";-const SETTING_PATH = "/api/setting";--async function findPutRequests() {- const requests = await findRequests("PUT");- return requests.map(({ url, body }) => ({- path: new URL(url, location.origin).pathname,- body,- }));-}--async function findPutBodies(path: string) {- return (await findPutRequests())- .filter((request) => request.path === path)- .map(({ body }) => body);+async function openModelPicker() {+ await userEvent.click(screen.getByLabelText("Model"));+ return await screen.findByRole("listbox");}-const findMetabotSettingsUpdates = () => findPutBodies(METABOT_SETTINGS_PATH);--const findSettingUpdates = () => findPutBodies(SETTING_PATH);-describe("AIProviderSettingsSection", () => {afterEach(() => {reinitialize();});- it("shows the env var message and disables both provider and model inputs when provider is env-backed", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- providerSettingIsEnv: true,- apiKeyValues: { anthropic: "**********45" },- });+ it("reports a failure to load the providers instead of an empty list", async () => {+ await setup({ providerTypesFail: true });expect(- await screen.findByTestId("setting-env-var-message"),- ).toHaveTextContent(- "This has been set by the LLM_METABOT_PROVIDER environment variable.",- );- expect(screen.getByLabelText("Provider")).toBeDisabled();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();- });-- it("shows the providers as selectable in the provider dropdown", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await userEvent.click(screen.getByLabelText("Provider"));-- for (const name of [- /Anthropic/,- /OpenAI/,- /OpenRouter/,- /Mistral/,- /Moonshot AI/,- /Z\.AI/,- /Microsoft Azure/,- /Amazon Bedrock/,- /Gemini Enterprise Agent Platform/,- ]) {- const option = await screen.findByRole("option", { name });- expect(option).toBeInTheDocument();- expect(option).not.toHaveAttribute("aria-disabled", "true");- expect(option).not.toHaveAttribute("data-combobox-disabled");- }-- expect(screen.queryByText("Coming soon")).not.toBeInTheDocument();- });-- it("BOT-1429: keeps the form interactive while session-properties refetches in the background", async () => {- const { store } = await setup();- const apiKey = await screen.findByLabelText("API key");- const model = await screen.findByLabelText("Model");- const disconnect = await screen.findByRole("button", {- name: "Disconnect",- });-- expect(apiKey).toBeEnabled();- expect(model).toBeEnabled();- expect(disconnect).toBeEnabled();- expect(disconnect).not.toHaveAttribute("data-loading", "true");-- const sessionPropertiesDeferred = defer<unknown>();- fetchMock.removeRoute("get-session-properties");- fetchMock.get(- "path:/api/session/properties",- () => sessionPropertiesDeferred.promise,- );-- act(() => {- store.dispatch(Api.util.invalidateTags(["session-properties"]));- });-- await waitFor(() => {- expect(- fetchMock.callHistory.calls("path:/api/session/properties").length,- ).toBeGreaterThan(1);- });-- expect(apiKey).toBeEnabled();- expect(model).toBeEnabled();- expect(disconnect).toBeEnabled();- expect(disconnect).not.toHaveAttribute("data-loading", "true");-- sessionPropertiesDeferred.resolve({});+ screen.getByText("Provider types are unavailable"),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /Add a provider/ }),+ ).not.toBeInTheDocument();});- it("shows the connected badge with the saved provider and model", async () => {+ it("shows the empty state when nothing is connected", async () => {await setup();- await screen.findByLabelText("API key");- await screen.findByLabelText("Model");+ expect(screen.getByText("Connect to an AI provider")).toBeInTheDocument();expect(- await screen.findByText("Connected to Anthropic"),+ screen.getByText(+ "Select your AI provider to use AI explorations, SQL generation and Metabot.",+ ),).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();expect(- screen.getByRole("button", { name: "Disconnect" }),+ screen.getByRole("button", { name: /Add a provider/ }),).toBeInTheDocument();- expect(screen.queryByText("Not connected")).not.toBeInTheDocument();+ expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();});- it("shows Connect instead of Disconnect when the configured API key input is dirty", async () => {- await setup();- // Wait for the saved key from the setting-details- await waitFor(() =>- expect(screen.getByLabelText("API key")).not.toHaveValue(""),- );+ it("lists the configured connections", async () => {+ await setup({+ connections: [ANTHROPIC_CONNECTION, AZURE_CONNECTION],+ models: CONNECTION_MODELS,+ });+ expect(screen.getByText("AI providers")).toBeInTheDocument();expect(- screen.getByRole("button", { name: "Disconnect" }),+ screen.getByText("Metabot can use models from any of these providers."),).toBeInTheDocument();+ expect(screen.getByText("Anthropic")).toBeInTheDocument();+ expect(screen.getByText("Azure prod")).toBeInTheDocument();expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();+ screen.getByRole("button", { name: /Add another provider/ }),+ ).toBeInTheDocument();+ expect(screen.getByLabelText("Model")).toBeInTheDocument();+ });- await userEvent.clear(screen.getByLabelText("API key"));- await userEvent.type(screen.getByLabelText("API key"), "sk-ant-rotated");+ it("picks the provider and fills the key in when one is pasted", async () => {+ await setup();- expect(screen.getByRole("button", { name: "Connect" })).toBeInTheDocument();+ const modal = await openAddProviderModal();expect(- screen.queryByRole("button", { name: "Disconnect" }),+ within(modal).queryByRole("button", { name: "Connect" }),).not.toBeInTheDocument();- await userEvent.click(screen.getByRole("button", { name: "Connect" }));+ await userEvent.paste("sk-ant-api03-pasted");- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "anthropic", "api-key": "sk-ant-rotated" },- ]);- });+ const apiKey = await within(modal).findByLabelText(/API key/);+ expect(apiKey).toHaveValue("sk-ant-api03-pasted");+ expect(within(modal).getByText("Anthropic")).toBeInTheDocument();+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeEnabled();++ await userEvent.click(+ within(modal).getByRole("button", { name: "Connect" }),+ );await waitFor(() => {expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: {+ type: "anthropic",+ name: "Anthropic",+ config: { "api-key": "sk-ant-api03-pasted" },+ },+ }),+ ).toBe(true);});});- it("shows a saved API key validation error without disconnecting", async () => {- await setup({- responses: {- anthropic: {- value: "anthropic/claude-haiku-4-5",- "credentials-error": "Anthropic API key expired or invalid",- models: [],- },- },- });+ it("stays on the picker when the pasted text is not a recognizable key", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.paste("just some text");- expect(await screen.findByLabelText("API key")).toHaveValue("**********45");+ expect(within(modal).queryByLabelText(/API key/)).not.toBeInTheDocument();expect(- await screen.findByText("Anthropic API key expired or invalid"),+ within(modal).getByRole("button", { name: "Anthropic" }),).toBeInTheDocument();+ });++ it("adds a provider through the modal", async () => {+ await setup();++ const modal = await openAddProviderModal();++ expect(+ within(modal).queryByRole("button", { name: "Connect" }),+ ).not.toBeInTheDocument();expect(- screen.getByText("Error connecting to Anthropic"),+ within(modal).getByRole("button", { name: /Anthropic/ }),).toBeInTheDocument();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Anthropic" }),+ );++ await openAdvancedSettings(modal);+ expect(within(modal).getByLabelText("Display name")).toHaveValue(+ "Anthropic",+ );expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeDisabled();- const settingsRequest = fetchMock.callHistory.calls(- "path:/api/metabot/settings",- )[0];- expect(settingsRequest?.url).toContain("provider=anthropic");- expect(settingsRequest?.url).not.toContain("api-key");- });+ await userEvent.type(within(modal).getByLabelText(/API key/), "sk-ant-new");+ await userEvent.click(+ within(modal).getByRole("button", { name: "Connect" }),+ );- it("disables an env-backed API key field with a saved key validation error", async () => {- await setup({- apiKeySettingIsEnv: true,- responses: {- anthropic: {- value: "anthropic/claude-haiku-4-5",- "credentials-error": "Anthropic API key expired or invalid",- models: [],- },- },+ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: {+ type: "anthropic",+ name: "Anthropic",+ config: { "api-key": "sk-ant-new" },+ },+ }),+ ).toBe(true);});- // Wait for the env-backed state from setting-detailsawait waitFor(() => {- expect(screen.getByLabelText("API key")).toBeDisabled();+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();});- expect(- await screen.findByText("Anthropic API key expired or invalid"),- ).toBeInTheDocument();- expect(screen.getByTestId("setting-env-var-message")).toHaveTextContent(- "This has been set by the LLM_ANTHROPIC_API_KEY environment variable.",+ });++ it("offers the Azure deployment as two fields rather than a hand-typed prefix", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.click(within(modal).getByRole("button", { name: "Azure" }));++ expect(within(modal).getByLabelText("Model provider")).toBeVisible();+ expect(within(modal).getByLabelText("Model provider")).toHaveValue(+ "OpenAI",+ );+ expect(within(modal).getByLabelText(/Deployment name/)).toBeVisible();++ await userEvent.type(within(modal).getByLabelText(/API key/), "azure-new");+ await userEvent.type(+ within(modal).getByLabelText(/Base URL/),+ "https://azure.test",+ );+ await userEvent.type(+ within(modal).getByLabelText(/Deployment name/),+ "gpt-4.1-mini",);++ // the pre-selected model provider counts as filled in, so it does not block Connectexpect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeEnabled();});- it("clears the saved API key error and shows Connect when the key changes", async () => {- await setup({- responses: {- anthropic: {- value: "anthropic/claude-haiku-4-5",- "credentials-error": "Anthropic API key expired or invalid",- models: [],- },- },- });+ it("submits every descriptor field the provider type declares", async () => {+ await setup({ createdConnection: AZURE_CONNECTION });- await screen.findByText("Anthropic API key expired or invalid");+ const modal = await openAddProviderModal();- await userEvent.clear(screen.getByLabelText("API key"));- await userEvent.type(screen.getByLabelText("API key"), "sk-ant-rotated");+ await userEvent.click(within(modal).getByRole("button", { name: "Azure" }));- expect(- screen.queryByText("Anthropic API key expired or invalid"),- ).not.toBeInTheDocument();- expect(screen.getByRole("button", { name: "Connect" })).toBeInTheDocument();+ await userEvent.type(within(modal).getByLabelText(/API key/), "azure-new");+ await userEvent.type(+ within(modal).getByLabelText(/Base URL/),+ "https://azure.test",+ );++ await userEvent.type(+ within(modal).getByLabelText(/Deployment name/),+ "my-deployment",+ );+ await userEvent.click(within(modal).getByLabelText("Model provider"));+ await selectOption("Anthropic");++ await userEvent.click(+ within(modal).getByRole("button", { name: "Connect" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: {+ type: "azure",+ name: "Azure",+ config: {+ "api-key": "azure-new",+ "api-base-url": "https://azure.test",+ "deployment-name": "my-deployment",+ "model-family": "anthropic",+ },+ },+ }),+ ).toBe(true);+ });});- it("shows the disconnected title when not configured", async () => {- await setup({ savedProviderValue: null, isConfigured: false });+ it("returns to provider selection from the configuration step", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Anthropic" }),+ );++ expect(within(modal).getByLabelText(/API key/)).toBeInTheDocument();++ await userEvent.click(within(modal).getByRole("button", { name: "Back" }));expect(- await screen.findByText("Connect to an AI provider"),+ within(modal).getByRole("button", { name: /Anthropic/ }),).toBeInTheDocument();- expect(screen.queryByText(/^Connected to /)).not.toBeInTheDocument();+ expect(within(modal).queryByLabelText(/API key/)).not.toBeInTheDocument();});- it("does not preselect a provider in the disconnected state", async () => {- await setup({- savedProviderValue: "anthropic/claude-haiku-4-5",- isConfigured: false,- });+ it("keeps the optional fields behind advanced settings", async () => {+ await setup();- expect(await screen.findByLabelText("Provider")).toHaveValue("");- });+ const modal = await openAddProviderModal();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Anthropic" }),+ );- // TODO: Add these tests back once we allow configuring the model provider from the UI.- // eslint-disable-next-line jest/no-commented-out-tests- // it("shows the model loader when switching providers", async () => {- // const openAIRequest = { resolve: null as null | (() => void) };- //- // await setup({- // apiKeyValues: { anthropic: "**********45", openai: "**********54" },- // responses: {- // openai: async () => {- // await new Promise<void>((resolve) => {- // openAIRequest.resolve = resolve;- // });- //- // return DEFAULT_RESPONSES.openai;- // },- // },- // });- //- // await selectProvider("OpenAI");- //- // await waitFor(() => {- // expect(screen.getByLabelText("Model")).toHaveAttribute(- // "placeholder",- // "Loading models...",- // );- // });- //- // if (openAIRequest.resolve) {- // openAIRequest.resolve();- // }- // });- //- // eslint-disable-next-line jest/no-commented-out-tests- // it("shows the new model list and resets the selected model when switching providers", async () => {- // await setup({- // apiKeyValues: { anthropic: "**********45", openai: "**********54" },- // });- // await screen.findByLabelText("Model");- //- // await selectProvider("OpenAI");- //- // await waitFor(() => {- // expect(screen.getByLabelText("Model")).toHaveValue("");- // });- //- // await waitFor(() => {- // expect(screen.getByLabelText("Model")).toHaveAttribute(- // "placeholder",- // "Select a model",- // );- // });- //- // await waitFor(() => {- // expect(- // fetchMock.callHistory.called(- // "path:/api/metabot/settings?provider=openai",- // ),- // ).toBe(true);- // });- //- // expect(screen.getByLabelText("Model")).toHaveAttribute(- // "placeholder",- // "Select a model",- // );- // });-- it("shows model groups from the backend in the model picker", async () => {- await setup();- await screen.findByLabelText("Model");+ expect(within(modal).getByLabelText(/API key/)).toBeVisible();+ expect(within(modal).getByLabelText("Display name")).not.toBeVisible();+ expect(within(modal).getByLabelText("API base URL")).not.toBeVisible();- await openModelSelector();+ await openAdvancedSettings(modal);- expect(await screen.findByText("Sonnet")).toBeInTheDocument();+ await waitFor(() =>+ expect(within(modal).getByLabelText("Display name")).toBeVisible(),+ );+ expect(within(modal).getByLabelText("API base URL")).toBeVisible();});- it("does not show the API key input when no provider is selected", async () => {- await setup({ savedProviderValue: null, isConfigured: false });+ it("keeps an optional field up front unless it is marked advanced", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Amazon Bedrock" }),+ );- expect(screen.queryByLabelText("API key")).not.toBeInTheDocument();+ expect(within(modal).getByLabelText(/Access key ID/)).toBeVisible();+ expect(within(modal).getByLabelText(/Secret access key/)).toBeVisible();+ expect(within(modal).getByLabelText("Region")).toBeVisible();+ expect(within(modal).getByLabelText("Session token")).not.toBeVisible();});- it("does not show the model selector when there is no API key", async () => {+ it("opens advanced settings for a connection that already customized one", async () => {await setup({- apiKeyValues: { anthropic: null },+ connections: [+ createMockLlmProviderConnection({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ config: {+ "api-key": "sk-ant-saved",+ "base-url": "https://proxy.internal",+ },+ }),+ ],+ models: CONNECTION_MODELS,});- await screen.findByLabelText("API key");- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();+ await openProviderMenu();+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Edit/ }),+ );++ const modal = await screen.findByRole("dialog");+ expect(within(modal).getByLabelText("API base URL")).toBeVisible();+ expect(within(modal).getByLabelText("API base URL")).toHaveValue(+ "https://proxy.internal",+ );});- it("keeps the provider selector visible after verifying an API key", async () => {+ it("edits an existing connection", async () => {await setup({- savedProviderValue: null,- isConfigured: false,- apiKeyValues: { anthropic: null },+ connections: [ANTHROPIC_CONNECTION],+ models: CONNECTION_MODELS,});- await selectProvider("Anthropic");- await userEvent.type(screen.getByLabelText("API key"), "sk-ant-valid");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));+ await openProviderMenu();+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Edit/ }),+ );- await screen.findByLabelText("Model");- expect(screen.getByLabelText("Provider")).toHaveValue("Anthropic");- });+ const modal = await screen.findByRole("dialog");+ expect(within(modal).queryByLabelText("Provider")).not.toBeInTheDocument();- it("shows the connected Metabase-managed state without an API key or model picker", async () => {- await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- });+ await openAdvancedSettings(modal);+ const displayName = within(modal).getByLabelText("Display name");+ await userEvent.clear(displayName);+ await userEvent.type(displayName, "Anthropic prod");- expect(- await screen.findByText("Current billing cycle"),- ).toBeInTheDocument();- expect(screen.queryByLabelText("API key")).not.toBeInTheDocument();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });+ await userEvent.click(within(modal).getByRole("button", { name: "Save" }));- it("explains the legacy Metabase AI pricing migration", async () => {- await setup({- isHosted: true,- hasDeprecatedMetabaseAiProvider: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",+ await waitFor(() => {+ expect(+ fetchMock.callHistory.called("path:/api/llm/providers/anthropic", {+ method: "PUT",+ body: {+ name: "Anthropic prod",+ config: { "api-key": "sk-ant-saved" },+ },+ }),+ ).toBe(true);});-- expect(- await screen.findByText(- "You're on legacy tiered AI pricing today. On your next billing cycle, you'll switch to metered AI pricing. If you'd like to switch to a third-party AI provider and use their API, click Disconnect.",- ),- ).toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();});- it("shows pricing details in a tooltip for the Metabase provider", async () => {+ it("removes a connection after confirming", async () => {await setup({- isHosted: true,- isAdmin: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- isConfigured: false,+ connections: [ANTHROPIC_CONNECTION],+ models: CONNECTION_MODELS,});- await selectProvider("Metabase");- await screen.findByText("About Metabase AI service");- await screen.findByTestId("metabase-ai-pricing-details");-- await userEvent.hover(screen.getByTestId("metabase-ai-pricing-details"));-- expect(- await screen.findByText(- "Tokens are chunks of text used by AI models. Usage includes both prompts and responses.",- ),- ).toBeInTheDocument();-- expect(- screen.getByRole("link", { name: "Terms of Service" }),- ).toHaveAttribute("href", "https://www.metabase.com/license/hosting");- });-- it("shows a contact-admin notice for non-admin users on the Metabase provider", async () => {- await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- isConfigured: false,- isAdmin: false,- anyStoreUserEmailAddress: "[email protected]",- });+ await openProviderMenu();+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Remove/ }),+ );- await selectProvider("Metabase");expect(- await screen.findByText(- "Please ask an Admin user to enable this for you.",- ),+ await screen.findByText("Remove this provider?"),).toBeInTheDocument();expect(- screen.queryByRole("button", { name: "Accept terms & connect" }),- ).not.toBeInTheDocument();- });-- it("shows a direct connect button when the Metabase provider feature is already enabled", async () => {- const user = userEvent.setup();-- await setup({- isHosted: true,- savedProviderValue: null,- isConfigured: false,- isAdmin: false,- tokenStatusFeatures: ["metabase-ai-managed"],- updateResponse: {- value: "metabase/anthropic/claude-sonnet-4-6",- models: DEFAULT_RESPONSES.metabase.models,- },- });-- await selectProvider("Metabase");-- const connectButton = await screen.findByRole("button", {- name: "Connect",- });-- expect(- screen.queryByRole("checkbox", {- name: /I agree with the Metabase AI Service/i,+ fetchMock.callHistory.called("path:/api/llm/providers/anthropic", {+ method: "DELETE",}),- ).not.toBeInTheDocument();-- await user.click(connectButton);-- await waitFor(() => {- expect(fetchMock.callHistory.called("path:/api/metabot/settings")).toBe(- true,- );- });-- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),).toBe(false);- const [settingsRequest] = fetchMock.callHistory.calls(- "path:/api/metabot/settings",- {- method: "PUT",- },- );-- expect(settingsRequest?.options?.body).toBe(- JSON.stringify({ provider: "metabase", model: "" }),- );- });-- it("calls onClose after directly connecting to the Metabase provider in modal mode", async () => {- const onClose = jest.fn();-- await setup({- isHosted: true,- savedProviderValue: null,- isConfigured: false,- isAdmin: false,- tokenStatusFeatures: ["metabase-ai-managed"],- updateResponse: {- value: "metabase/anthropic/claude-sonnet-4-6",- models: DEFAULT_RESPONSES.metabase.models,- },- renderAsModal: true,- onClose,- });-- await selectProvider("Metabase");await userEvent.click(- await screen.findByRole("button", { name: "Connect" }),+ screen.getByRole("button", { name: "Remove provider" }),);await waitFor(() => {expect(- fetchMock.callHistory.called("path:/api/metabot/settings", {- method: "PUT",+ fetchMock.callHistory.called("path:/api/llm/providers/anthropic", {+ method: "DELETE",}),).toBe(true);- expect(onClose).toHaveBeenCalledTimes(1);- });- });-- it("waits for the purchase and settings save before showing Metabase AI as ready", async () => {- let resolvePurchaseCloudAddOnResponse = () => {};- let resolveMetabotSettingsUpdateResponse = () => {};-- try {- jest.useFakeTimers({ advanceTimers: true });- const user = userEvent.setup({- advanceTimers: jest.advanceTimersByTime,- });-- ({- resolvePurchaseCloudAddOnResponse,- resolveMetabotSettingsUpdateResponse,- } = await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- isConfigured: false,- isAdmin: true,- tokenStatusFeatures: [],- refreshedTokenStatusFeatures: ["metabase-ai-managed"],- deferPurchaseCloudAddOnResponse: true,- deferMetabotSettingsUpdateResponse: true,- updateResponse: {- value: "metabase/anthropic/claude-sonnet-4-6",- models: DEFAULT_RESPONSES.metabase.models,- },- }));-- await selectProvider("Metabase");- const termsCheckbox = await screen.findByRole("checkbox", {- name: /I agree with the Metabase AI Service/i,- });- const connectButton = await screen.findByRole("button", {- name: "Connect",- });-- expect(connectButton).toBeDisabled();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();-- await user.click(termsCheckbox);-- expect(connectButton).toBeEnabled();-- await user.click(connectButton);-- expect(connectButton).toBeDisabled();- expect(- await screen.findByText("Setting up Metabot AI, please wait"),- ).toBeInTheDocument();- expect(screen.queryByText("Metabot AI is ready")).not.toBeInTheDocument();- expect(- screen.queryByText("Current billing cycle"),- ).not.toBeInTheDocument();-- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),- ).toBe(true);- });-- const [request] = fetchMock.callHistory.calls(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- {- method: "POST",- },- );-- expect(request?.options?.body).toBe(- JSON.stringify({ terms_of_service: true }),- );-- expect(- fetchMock.callHistory- .calls("path:/api/metabot/settings")- .some(- (call) =>- call.request?.method === "PUT" || call.options?.method === "PUT",- ),- ).toBe(false);-- act(() => {- jest.advanceTimersByTime(1000);- });-- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/premium-features/token/refresh",- ),- ).toBe(true);- });-- expect(- screen.getByText("Setting up Metabot AI, please wait"),- ).toBeInTheDocument();- expect(screen.queryByText("Metabot AI is ready")).not.toBeInTheDocument();- expect(- fetchMock.callHistory- .calls("path:/api/metabot/settings")- .some(- (call) =>- call.request?.method === "PUT" || call.options?.method === "PUT",- ),- ).toBe(false);-- await act(async () => {- resolvePurchaseCloudAddOnResponse();- await Promise.resolve();- });-- await waitFor(() => {- expect(- fetchMock.callHistory- .calls("path:/api/metabot/settings")- .some(- (call) =>- call.request?.method === "PUT" ||- call.options?.method === "PUT",- ),- ).toBe(true);- });-- expect(- screen.getByText("Setting up Metabot AI, please wait"),- ).toBeInTheDocument();- expect(screen.queryByText("Metabot AI is ready")).not.toBeInTheDocument();- expect(- screen.queryByText("Current billing cycle"),- ).not.toBeInTheDocument();-- await act(async () => {- resolveMetabotSettingsUpdateResponse();- await Promise.resolve();- });-- expect(- await screen.findByRole("button", { name: "Done" }),- ).toBeInTheDocument();- expect(screen.getByText("Metabot AI is ready")).toBeInTheDocument();- expect(screen.getByText("Current billing cycle")).toBeInTheDocument();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();-- const settingsRequest = fetchMock.callHistory- .calls("path:/api/metabot/settings")- .find(- (call) =>- call.request?.method === "PUT" || call.options?.method === "PUT",- );-- expect(settingsRequest?.options?.body).toBe(- JSON.stringify({ provider: "metabase", model: "" }),- );-- const callHistory = fetchMock.callHistory.calls();- const [purchaseRequest] = fetchMock.callHistory.calls(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- {- method: "POST",- },- );-- if (!settingsRequest || !purchaseRequest) {- throw new Error("Expected purchase and settings requests to exist");- }-- expect(callHistory.indexOf(purchaseRequest)).toBeLessThan(- callHistory.indexOf(settingsRequest),- );- } finally {- resolvePurchaseCloudAddOnResponse();- resolveMetabotSettingsUpdateResponse();- jest.useRealTimers();- }- });-- it("calls onClose after purchasing the Metabase add-on and connecting in modal mode", async () => {- const onClose = jest.fn();- const {- resolvePurchaseCloudAddOnResponse,- resolveMetabotSettingsUpdateResponse,- } = await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- isConfigured: false,- isAdmin: true,- tokenStatusFeatures: [],- refreshedTokenStatusFeatures: ["metabase-ai-managed"],- deferPurchaseCloudAddOnResponse: true,- deferMetabotSettingsUpdateResponse: true,- updateResponse: {- value: "metabase/anthropic/claude-sonnet-4-6",- models: DEFAULT_RESPONSES.metabase.models,- },- renderAsModal: true,- onClose,- });-- await selectProvider("Metabase");-- await userEvent.click(- await screen.findByRole("checkbox", {- name: /I agree with the Metabase AI Service/i,- }),- );- await userEvent.click(- await screen.findByRole("button", { name: "Connect" }),- );-- expect(onClose).not.toHaveBeenCalled();-- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),- ).toBe(true);- });-- await act(async () => {- resolvePurchaseCloudAddOnResponse();- await Promise.resolve();- });-- await waitFor(() => {- expect(- fetchMock.callHistory- .calls("path:/api/metabot/settings")- .some(- (call) =>- call.request?.method === "PUT" || call.options?.method === "PUT",- ),- ).toBe(true);- });-- expect(onClose).not.toHaveBeenCalled();-- await act(async () => {- resolveMetabotSettingsUpdateResponse();- await Promise.resolve();- });-- await waitFor(() => {- expect(onClose).toHaveBeenCalledTimes(1);- });- });-- it("shows live pricing for the Metabase provider", async () => {- await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- metabasePricePerUnit: 4.25,- });-- expect(await screen.findByText("Price per token")).toBeInTheDocument();- expect(screen.getByText("$4.25 per 1M tokens")).toBeInTheDocument();- });-- it("shows included usage for the connected Metabase provider while still within the free limit", async () => {- const updatedAt = "2026-04-02T19:29:12Z";- await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- metabasePricePerUnit: 4.25,- metabotUsageQuotas: [- {- tokens: 250000,- free_tokens: 1000000,- updated_at: updatedAt,- },- ],});-- expect(await screen.findByText("Included use")).toBeInTheDocument();- expect(screen.getByText("Free trial tokens")).toBeInTheDocument();- expect(screen.getByText("250,000 / 1,000,000")).toBeInTheDocument();- expect(screen.getByText("Price per token afterward")).toBeInTheDocument();- expect(screen.queryByText("Current billing cycle")).not.toBeInTheDocument();- expect(screen.queryByText("Total tokens")).not.toBeInTheDocument();});- it("shows the normal usage summary for the connected Metabase provider after free tokens run out", async () => {- const updatedAt = "2026-04-02T19:29:12Z";+ it("renders an env-backed connection as read-only", async () => {await setup({- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- metabasePricePerUnit: 4.25,- metabotUsageQuotas: [- {- tokens: 1250000,- free_tokens: 1000000,- updated_at: updatedAt,- },+ connections: [+ createMockLlmProviderConnection({+ key: "openai",+ type: "openai",+ name: "OpenAI",+ source: "env",+ env_vars: ["MB_LLM_OPENAI_API_KEY"],+ }),],});expect(- await screen.findByText("Current billing cycle"),+ screen.getByText("MB_LLM_OPENAI_API_KEY", { exact: false }),).toBeInTheDocument();- expect(await screen.findByText("1,250,000")).toBeInTheDocument();- expect(screen.queryByText("Unavailable")).not.toBeInTheDocument();- expect(screen.getByText("Total tokens")).toBeInTheDocument();- expect(screen.getByText("Total cost")).toBeInTheDocument();- expect(screen.getByText("Price per token")).toBeInTheDocument();- expect(screen.getByText("$1.06")).toBeInTheDocument();- expect(screen.queryByText("Included use")).not.toBeInTheDocument();expect(- screen.queryByText("Price per token afterward"),+ screen.queryByRole("button", { name: "Provider options" }),).not.toBeInTheDocument();});- it("disconnects when clicking use a different AI provider from the locked managed-provider state", async () => {+ it("lists models grouped by connection and saves the picked one", async () => {await setup({- isAdmin: true,- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- metabotUsageQuotas: [- {- is_locked: true,- tokens: 250000,- updated_at: "2026-04-02T19:29:12Z",- },- ],+ connections: [ANTHROPIC_CONNECTION, AZURE_CONNECTION],+ models: CONNECTION_MODELS,+ modelRef: "anthropic/claude-sonnet-4-5",});- expect(- await screen.findByText("You've run out of AI service tokens"),- ).toBeInTheDocument();- expect(- screen.getByText(/You've used all of your included AI service tokens\./),- ).toBeInTheDocument();- expect(screen.queryByText("Current billing cycle")).not.toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Use a different AI provider" }),- ).toBeInTheDocument();- expect(- screen.getByRole("link", { name: "Start paid subscription" }),- ).toBeInTheDocument();-- await userEvent.click(- screen.getByRole("button", { name: "Use a different AI provider" }),- );-- expect(- await screen.findByText("Disconnect AI provider?"),- ).toBeInTheDocument();- expect(- fetchMock.callHistory.calls("path:/api/setting", { method: "PUT" }),- ).toHaveLength(0);-- await userEvent.click(- screen.getByRole("button", { name: "Disconnect provider" }),+ // closed, the picker names the provider too — the dropdown does not, because its group+ // heading already does+ await waitFor(() =>+ expect(screen.getByLabelText("Model")).toHaveValue(+ "Anthropic · Claude Sonnet 4.5",+ ),);- await waitFor(() => {- expect(fetchMock.callHistory.called("path:/api/setting")).toBe(true);- });-- const request = fetchMock.callHistory- .calls("path:/api/setting")- .find((call) => call.request?.method === "PUT");-- expect(request?.options?.body).toBe(- JSON.stringify({- "llm-metabot-provider": null,- }),- );+ const listbox = await openModelPicker();+ expect(within(listbox).getByText("Anthropic")).toBeInTheDocument();+ expect(within(listbox).getByText("Azure prod")).toBeInTheDocument();expect(- await screen.findByText("Connect to an AI provider"),+ within(listbox).getByRole("option", { name: "Claude Haiku 4.5" }),).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("");- expect(- screen.queryByText("You've run out of AI service tokens"),- ).not.toBeInTheDocument();- });-- it("resets the form in modal mode without updating settings", async () => {- await setup({- isAdmin: true,- isHosted: true,- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- tokenStatusFeatures: ["metabase-ai-managed"],- metabotUsageQuotas: [- {- is_locked: true,- tokens: 250000,- updated_at: "2026-04-02T19:29:12Z",- },- ],- renderAsModal: true,- });-- expect(await screen.findByLabelText("Provider")).toHaveValue("");- await selectProvider("Metabase");await userEvent.click(- await screen.findByRole("button", {- name: "Use a different AI provider",- }),+ within(listbox).getByRole("option", { name: "GPT-5" }),);- expect(await screen.findByLabelText("Provider")).toHaveValue("");- expect(- screen.queryByRole("button", { name: "Use a different AI provider" }),- ).not.toBeInTheDocument();- expect(- fetchMock.callHistory- .calls("path:/api/setting")- .some((call) => call.request?.method === "PUT"),- ).toBe(false);- });-- it("saves when picking a model", async () => {- const { store } = await setup({- savedProviderValue: null,- isConfigured: false,- });- await selectProvider("Anthropic");- await screen.findByLabelText("Model");-- await openModelSelector();- await userEvent.click(await screen.findByText("Claude Sonnet 4.5"));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "anthropic", model: "claude-sonnet-4-5" },- ]);- });-await waitFor(() => {expect(- store- .getState()- .undo.some(- (toast) => toast.message === "Settings saved successfully",- ),+ fetchMock.callHistory.called("path:/api/setting/llm-metabot-provider", {+ method: "PUT",+ body: { value: "azure-prod/gpt-5" },+ }),).toBe(true);});- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();+ expect(await screen.findAllByText("Changes saved")).toHaveLength(1);});- it("connects to OpenAI by saving the API key and selecting a model", async () => {+ it("surfaces a failure to save the picked model", async () => {await setup({- savedProviderValue: null,- isConfigured: false,- apiKeyValues: { openai: null },- updateResponse: {- value: "openai/gpt-5.4",- models: DEFAULT_RESPONSES.openai.models,- },+ connections: [ANTHROPIC_CONNECTION, AZURE_CONNECTION],+ models: CONNECTION_MODELS,+ modelRef: "anthropic/claude-sonnet-4-5",});- await selectProvider("OpenAI");- await userEvent.type(screen.getByLabelText("API key"), "sk-openai-test");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));+ // closed, the picker names the provider too — the dropdown does not, because its group+ // heading already does+ await waitFor(() =>+ expect(screen.getByLabelText("Model")).toHaveValue(+ "Anthropic · Claude Sonnet 4.5",+ ),+ );- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "openai", "api-key": "sk-openai-test" },- ]);+ fetchMock.modifyRoute("update-setting", {+ response: { status: 400, body: { message: "Unsupported model" } },});- await screen.findByLabelText("Model");- await openModelSelector();- await userEvent.click(await screen.findByText("gpt-5.4"));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "openai", "api-key": "sk-openai-test" },- { provider: "openai", model: "gpt-5.4" },- ]);- });+ const listbox = await openModelPicker();+ await userEvent.click(+ within(listbox).getByRole("option", { name: "GPT-5" }),+ );- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();+ expect(await screen.findByText("Unsupported model")).toBeInTheDocument();+ expect(screen.queryByText("Changes saved")).not.toBeInTheDocument();});- it("disconnects OpenAI by clearing both the provider and API key settings", async () => {+ it("locks the model picker and names the env var when the model is set by one", async () => {await setup({- savedProviderValue: "openai/gpt-5.4",- isConfigured: true,- apiKeyValues: { openai: "**********ey" },- });-- await screen.findByText("Connected to OpenAI");- await screen.findByLabelText("API key");- await confirmDisconnectProvider();-- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- "llm-openai-api-key": null,- });+ connections: [ANTHROPIC_CONNECTION],+ models: CONNECTION_MODELS,+ modelRef: "anthropic/claude-sonnet-4-5",+ modelRefEnvVar: "MB_LLM_METABOT_PROVIDER",});expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("");+ await screen.findByTestId("setting-env-var-message"),+ ).toHaveTextContent("MB_LLM_METABOT_PROVIDER");+ expect(screen.getByLabelText("Model")).toBeDisabled();});+});- it("connects to OpenRouter by saving the API key and selecting a model", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- apiKeyValues: { openrouter: null },- updateResponse: {- value: "openrouter/anthropic/claude-sonnet-4.6",- models: DEFAULT_RESPONSES.openrouter.models,- },- });-- await selectProvider("OpenRouter");- await userEvent.type(- screen.getByLabelText("API key"),- "sk-or-v1-openrouter-test",- );- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "openrouter", "api-key": "sk-or-v1-openrouter-test" },- ]);- });-- await screen.findByLabelText("Model");- await openModelSelector();- await userEvent.click(await screen.findByText("OpenAI: GPT-5.4 Mini"));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "openrouter", "api-key": "sk-or-v1-openrouter-test" },- { provider: "openrouter", model: "openai/gpt-5.4-mini" },- ]);- });-- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();+describe("AIProviderSetup (ad-hoc connect modal)", () => {+ afterEach(() => {+ reinitialize();});- it("disconnects OpenRouter by clearing both the provider and API key settings", async () => {- await setup({- savedProviderValue: "openrouter/openai/gpt-5.4-mini",- isConfigured: true,- apiKeyValues: { openrouter: "**********st" },- });-- await screen.findByText("Connected to OpenRouter");- await screen.findByLabelText("API key");- await confirmDisconnectProvider();+ async function setupModal() {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- "llm-openrouter-api-key": null,- });+ const sessionProperties = createMockSettings({+ "llm-metabot-provider": null,});- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("");- });+ setupPropertiesEndpoints(sessionProperties);+ setupSettingsEndpoints([+ createMockSettingDefinition({ key: "llm-metabot-provider", value: null }),+ ]);+ setupUpdateSettingEndpoint();+ setupLlmProviderTypesEndpoint([ANTHROPIC_TYPE]);+ setupLlmProvidersEndpoint([]);+ setupLlmModelsEndpoint(CONNECTION_MODELS);+ setupCreateLlmProviderEndpoint(ANTHROPIC_CONNECTION);- it("connects to Mistral by saving the API key and selecting a model", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- apiKeyValues: { mistral: null },- updateResponse: {- value: "mistral/mistral-medium-3-5",- models: DEFAULT_RESPONSES.mistral.models,+ const onDone = jest.fn();+ renderWithProviders(<AIProviderSetup onDone={onDone} />, {+ storeInitialState: {+ settings: mockSettings(sessionProperties),+ currentUser: createMockUser({ is_superuser: true }),},});- await selectProvider("Mistral");- await userEvent.type(screen.getByLabelText("API key"), "mistral-key.test");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));+ return { onDone };+ }- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "mistral", "api-key": "mistral-key.test" },- ]);- });+ it("shows the model picker after connecting rather than closing", async () => {+ const { onDone } = await setupModal();- await screen.findByLabelText("Model");- await openModelSelector();- await userEvent.click(await screen.findByText("Mistral Medium 3.5"));+ await userEvent.click(+ await screen.findByRole("button", { name: "Anthropic" }),+ );+ await userEvent.type(screen.getByLabelText(/API key/), "sk-ant-test");+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "mistral", "api-key": "mistral-key.test" },- { provider: "mistral", model: "mistral-medium-3-5" },- ]);- });+ expect(await screen.findByLabelText("Model")).toBeInTheDocument();+ expect(onDone).not.toHaveBeenCalled();- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();+ await userEvent.click(screen.getByRole("button", { name: "Done" }));+ expect(onDone).toHaveBeenCalled();});- it("disconnects Mistral by clearing both the provider and API key settings", async () => {- await setup({- savedProviderValue: "mistral/mistral-medium-3-5",- isConfigured: true,- apiKeyValues: { mistral: "**********st" },- });-- await screen.findByText("Connected to Mistral");- await screen.findByLabelText("API key");- await confirmDisconnectProvider();-- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- "llm-mistral-api-key": null,- });- });+ it("starts with provider selection and does not offer a provider list action", async () => {+ await setupModal();expect(- await screen.findByText("Connect to an AI provider"),+ screen.queryByRole("button", { name: /Add a provider/ }),+ ).not.toBeInTheDocument();+ expect(+ await screen.findByRole("button", { name: "Anthropic" }),).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("");- });-- it("connects to Z.AI by saving the API key and selecting a model", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- apiKeyValues: { zai: null },- updateResponse: {- value: "zai/glm-5.2",- models: DEFAULT_RESPONSES.zai.models,- },- });-- await selectProvider("Z.AI");- await userEvent.type(screen.getByLabelText("API key"), "zai-key.test");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "zai", "api-key": "zai-key.test" },- ]);- });-- await screen.findByLabelText("Model");- await openModelSelector();- await userEvent.click(await screen.findByText("GLM-5.2"));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- { provider: "zai", "api-key": "zai-key.test" },- { provider: "zai", model: "glm-5.2" },- ]);- });-- expect(- screen.queryByRole("button", { name: "Connect" }),- ).not.toBeInTheDocument();- });-- it("disconnects Z.AI by clearing both the provider and API key settings", async () => {- await setup({- savedProviderValue: "zai/glm-5.2",- isConfigured: true,- apiKeyValues: { zai: "**********st" },- });-- await screen.findByText("Connected to Z.AI");- await screen.findByLabelText("API key");- await confirmDisconnectProvider();-- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- "llm-zai-api-key": null,- });- });-- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- 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();-- await screen.findByLabelText("API key");- await openDisconnectProviderModal();-- expect(- await screen.findByText("Disconnect AI provider?"),- ).toBeInTheDocument();- expect(- screen.getByText(- "This will disconnect your AI provider and disable AI features across your instance until you connect a provider again.",- ),- ).toBeInTheDocument();- expect(await findSettingUpdates()).toEqual([]);-- await userEvent.click(- screen.getByRole("button", { name: "Disconnect provider" }),- );-- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- "llm-anthropic-api-key": null,- });- });-- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("");- expect(- screen.queryByRole("button", { name: "Disconnect" }),- ).not.toBeInTheDocument();- });-- it("disconnects an env-backed API-key provider by clearing only the provider setting", async () => {- await setup({- apiKeySettingIsEnv: true,- responses: {- anthropic: {- value: "anthropic/claude-haiku-4-5",- "credentials-error": "Anthropic API key expired or invalid",- models: [],- },- },- });-- await screen.findByText("Anthropic API key expired or invalid");-- await confirmDisconnectProvider();-- await waitFor(async () => {- expect(await findSettingUpdates()).toContainEqual({- "llm-metabot-provider": null,- });- });-- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- });-- it("disconnects the Metabase-managed provider by removing the add-on before clearing the provider setting", async () => {- await setup({- isHosted: true,- tokenStatusFeatures: ["metabase-ai-managed", "offer-metabase-ai-managed"],- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- });-- await screen.findByText("Connected to Metabase");- await confirmDisconnectProvider();-- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-tiered",- ),- ).toBe(false);- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),- ).toBe(true);- });-- await waitFor(() => {- expect(fetchMock.callHistory.called("path:/api/setting")).toBe(true);- });-- const [removeRequest] = fetchMock.callHistory.calls(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- {- method: "DELETE",- },- );- const [request] = fetchMock.callHistory.calls("path:/api/setting", {- method: "PUT",- });-- expect(removeRequest).toBeDefined();- expect(request?.options?.body).toBe(- JSON.stringify({- "llm-metabot-provider": null,- }),- );-- const callHistory = fetchMock.callHistory.calls();-- expect(callHistory.indexOf(removeRequest)).toBeLessThan(- callHistory.indexOf(request),- );- });-- it("disconnects the tiered Metabase provider by removing the add-on before clearing the provider setting", async () => {- await setup({- isHosted: true,- tokenStatusFeatures: ["metabot-v3", "offer-metabase-ai-managed"],- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- });-- await screen.findByText("Connected to Metabase");- await confirmDisconnectProvider();-- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),- ).toBe(false);- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-tiered",- ),- ).toBe(true);- });-- await waitFor(() => {- expect(fetchMock.callHistory.called("path:/api/setting")).toBe(true);- });-- const [removeRequest] = fetchMock.callHistory.calls(- "path:/api/ee/cloud-add-ons/metabase-ai-tiered",- {- method: "DELETE",- },- );- const [request] = fetchMock.callHistory.calls("path:/api/setting", {- method: "PUT",- });-- expect(removeRequest).toBeDefined();- expect(request?.options?.body).toBe(- JSON.stringify({- "llm-metabot-provider": null,- }),- );-- const callHistory = fetchMock.callHistory.calls();-- expect(callHistory.indexOf(removeRequest)).toBeLessThan(- callHistory.indexOf(request),- );- });-- it("does not disconnect the tiered Metabase provider if no offer-metabase-ai-managed is set", async () => {- await setup({- isHosted: true,- offerMetabaseManagedAi: false,- tokenStatusFeatures: ["metabot-v3"],- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- });-- await screen.findByText("Connected to Metabase");-- await confirmDisconnectProvider();-- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-managed",- ),- ).toBe(false);- expect(- fetchMock.callHistory.called(- "path:/api/ee/cloud-add-ons/metabase-ai-tiered",- ),- ).toBe(false);-- await waitFor(() => {- expect(- fetchMock.callHistory.called("path:/api/setting", { method: "PUT" }),- ).toBe(true);- });-- const [request] = fetchMock.callHistory.calls("path:/api/setting", {- method: "PUT",- });-- expect(request?.options?.body).toBe(- JSON.stringify({- "llm-metabot-provider": null,- }),- );- });-- it("does not clear the provider setting if removing the Metabase-managed add-on fails", async () => {- await setup({- isHosted: true,- tokenStatusFeatures: ["metabase-ai-managed"],- savedProviderValue: "metabase/anthropic/claude-sonnet-4-6",- removeCloudAddOnResponse: 500,- });-- await screen.findByText("Current billing cycle");- await confirmDisconnectProvider();-- await waitFor(() => {- expect(- screen.getByText("Unable to disconnect from this AI provider."),- ).toBeInTheDocument();- });-- expect(- fetchMock.callHistory- .calls("path:/api/setting")- .some((call) => call.request?.method === "PUT"),- ).toBe(false);- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });-- it("shows an error toast when disconnect fails", async () => {- const { store } = await setup({ settingUpdateResponse: { status: 500 } });-- await confirmDisconnectProvider();-- await waitFor(() => {- expect(- store- .getState()- .undo.some(- (toast) => toast.message === "Unable to save provider settings.",- ),- ).toBe(true);- });- });-- describe("Microsoft Azure", () => {- it("shows the Azure fields without a model dropdown when selected", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Microsoft Azure");-- expect(- await screen.findByLabelText("Model provider"),- ).toBeInTheDocument();- expect(screen.getByLabelText("Base URL")).toBeInTheDocument();- expect(screen.getByLabelText("API key")).toBeInTheDocument();- expect(screen.getByLabelText("Deployment name")).toBeInTheDocument();- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();- });-- it("connects Azure by sending the composed model and the credentials object", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- updateResponse: {- value: "azure/openai/my-deployment",- models: [],- },- });-- await selectProvider("Microsoft Azure");-- const connectButton = screen.getByRole("button", { name: "Connect" });- expect(connectButton).toBeDisabled();-- await userEvent.click(await screen.findByLabelText("Model provider"));- await userEvent.click(- await screen.findByRole("option", { name: "OpenAI" }),- );- await userEvent.type(- screen.getByLabelText("Base URL"),- "https://my-resource.services.ai.azure.com/openai",- );- await userEvent.type(screen.getByLabelText("API key"), "azure-key");- await userEvent.type(- screen.getByLabelText("Deployment name"),- "my-deployment",- );-- expect(connectButton).toBeEnabled();- await userEvent.click(connectButton);-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "azure",- model: "openai/my-deployment",- credentials: {- "api-key": "azure-key",- "base-url": "https://my-resource.services.ai.azure.com/openai",- },- },- ]);- });- });-- it("shows the saved family, base URL, and deployment for a connected Azure provider", async () => {- await setup({- savedProviderValue: "azure/anthropic/claude-sonnet-4-5",- apiKeyValues: { azure: "**********ey" },- });-- // The saved values hydrate from the async setting-details fetch, so- // wait for them rather than asserting synchronously.- await waitFor(() =>- expect(screen.getByLabelText("Model provider")).toHaveValue(- "Anthropic",- ),- );- await waitFor(() =>- expect(screen.getByLabelText("Base URL")).toHaveValue(- "https://my-resource.services.ai.azure.com/anthropic",- ),- );- expect(screen.getByLabelText("Deployment name")).toHaveValue(- "claude-sonnet-4-5",- );- expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });-- it("sends untouched Azure credential fields as null when editing only the deployment", async () => {- await setup({- savedProviderValue: "azure/anthropic/claude-sonnet-4-5",- apiKeyValues: { azure: "**********ey" },- updateResponse: {- value: "azure/anthropic/renamed-deployment",- models: [],- },- });-- const deploymentInput = await screen.findByLabelText("Deployment name");- // Wait for the saved deployment to hydrate before editing it — clearing- // the still-empty input is a no-op the arriving value then overwrites.- await waitFor(() =>- expect(deploymentInput).toHaveValue("claude-sonnet-4-5"),- );- await userEvent.clear(deploymentInput);- await userEvent.type(deploymentInput, "renamed-deployment");-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // The untouched key and base URL round-trip as displayed values in the form, but must be- // sent as null so the backend keeps the real saved values.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "azure",- model: "anthropic/renamed-deployment",- credentials: {- "api-key": null,- "base-url": null,- },- },- ]);- });- });-- it("disconnects Azure by clearing the credentials before the provider setting", async () => {- await setup({- savedProviderValue: "azure/anthropic/claude-sonnet-4-5",- apiKeyValues: { azure: "**********ey" },- });-- await screen.findByLabelText("Deployment name");- await confirmDisconnectProvider();-- // The credentials must be cleared before the provider setting.- await waitFor(async () => {- expect(await findPutRequests()).toEqual([- {- path: METABOT_SETTINGS_PATH,- body: { provider: "azure", credentials: null },- },- {- path: SETTING_PATH,- body: { "llm-metabot-provider": null },- },- ]);- });-- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- });- });-- describe("Gemini Enterprise Agent Platform", () => {- // The "OAuth token" label on the auth toggle is the same as the label on the OAuth token- // input. Thus the queries for credential inputs also give the element type.- const CREDENTIAL_INPUT = { selector: "input[type='password']" };-- it("shows the project ID, location, model, and auth type toggle with the service account key picker when selected", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Gemini Enterprise Agent Platform");-- expect(await screen.findByLabelText("Project ID")).toBeInTheDocument();- expect(screen.getByLabelText("Location")).toBeInTheDocument();- expect(screen.getByLabelText("Model")).toBeInTheDocument();- expect(- screen.getByLabelText("Authentication method"),- ).toBeInTheDocument();- expect(screen.getByText("Service account key file")).toBeInTheDocument();- // Only the credential input for the selected auth type is shown.- expect(- screen.queryByLabelText("OAuth token", CREDENTIAL_INPUT),- ).not.toBeInTheDocument();- });-- it("explains that model availability varies by location and links to the location docs", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Model");-- expect(- screen.getByText(/Model availability varies by location/),- ).toBeInTheDocument();- expect(- screen.getByRole("link", {- name: "See which Gemini models are available in each location.",- }),- ).toHaveAttribute(- "href",- "https://docs.cloud.google.com/gemini-enterprise-agent-platform/resources/locations#google-models",- );- });-- const typeModel = async (model: string) => {- await setup({ savedProviderValue: null, isConfigured: false });- await selectProvider("Gemini Enterprise Agent Platform");- await userEvent.type(await screen.findByLabelText("Model"), model);- };-- const MODEL_WARNING =- "Metabot works best with these models: gemini-3.5-flash, gemini-3.6-flash, gemini-3.7-flash. Other models may not work as expected.";-- it.each(["gemini-3.5-flash", "gemini-3.6-flash", "gemini-3.7-flash"])(- "shows no advisory warning for %s",- async (model) => {- await typeModel(model);-- expect(screen.queryByText(MODEL_WARNING)).not.toBeInTheDocument();- },- );-- it("shows no advisory warning for a recommended model entered with the google publisher prefix", async () => {- await typeModel("google/gemini-3.5-flash");-- expect(screen.queryByText(MODEL_WARNING)).not.toBeInTheDocument();- });-- it("lists the recommended models when the model is another Gemini model", async () => {- await typeModel("gemini-2.5-flash");-- expect(screen.getByText(MODEL_WARNING)).toBeInTheDocument();- });-- it("lists the recommended models when the model is not a Gemini model", async () => {- await typeModel("claude-sonnet-4-6");-- expect(screen.getByText(MODEL_WARNING)).toBeInTheDocument();- });-- it("shows no advisory warning while the model is still blank", async () => {- await setup({ savedProviderValue: null, isConfigured: false });- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Model");-- expect(screen.queryByText(MODEL_WARNING)).not.toBeInTheDocument();- });-- it("advisory warnings do not block connect", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- updateResponse: {- value: "google/google/gemini-2.5-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.test-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- await userEvent.type(screen.getByLabelText("Model"), "gemini-2.5-flash");-- expect(screen.getByText(MODEL_WARNING)).toBeInTheDocument();- expect(screen.getByRole("button", { name: "Connect" })).toBeEnabled();-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-2.5-flash",- credentials: {- "oauth-access-token": "ya29.test-token",- "project-id": "my-project",- },- },- ]);- });- });-- it("shows a single credential input matching the selected auth type", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- expect(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- ).toBeInTheDocument();- expect(- screen.queryByText("Service account key file"),- ).not.toBeInTheDocument();-- await userEvent.click(screen.getByText("Service account key"));- expect(screen.getByText("Service account key file")).toBeInTheDocument();- expect(screen.getByText("Click to select a file")).toBeInTheDocument();- expect(- screen.getByLabelText("Service account key file input"),- ).toBeInTheDocument();- expect(- screen.queryByLabelText("OAuth token", CREDENTIAL_INPUT),- ).not.toBeInTheDocument();- });-- it("keeps Connect disabled until the OAuth token, project ID, and model are filled in", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.test-token",- );- expect(screen.getByRole("button", { name: "Connect" })).toBeDisabled();-- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- expect(screen.getByRole("button", { name: "Connect" })).toBeDisabled();-- await userEvent.type(screen.getByLabelText("Model"), "gemini-3.5-flash");- expect(screen.getByRole("button", { name: "Connect" })).toBeEnabled();- });-- it("connects with a service account key file alone and sends its contents as credentials", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- updateResponse: {- value: "google/google/gemini-3.5-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- await selectProvider("Gemini Enterprise Agent Platform");-- expect(- await screen.findByRole("button", { name: "Connect" }),- ).toBeDisabled();- await userEvent.type(screen.getByLabelText("Model"), "gemini-3.5-flash");- await userEvent.upload(- screen.getByLabelText("Service account key file input"),- new File(['{"type": "service_account"}'], "service-account.json", {- type: "application/json",- }),- );- await waitFor(() => {- expect(screen.getByRole("button", { name: "Connect" })).toBeEnabled();- });-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.5-flash",- credentials: {- "service-account-key": '{"type": "service_account"}',- },- },- ]);- });- });-- it("clears a selected service account file with the clear button", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.type(screen.getByLabelText("Model"), "gemini-3.5-flash");- await userEvent.upload(- screen.getByLabelText("Service account key file input"),- new File(['{"type": "service_account"}'], "service-account.json", {- type: "application/json",- }),- );- expect(- await screen.findByText("service-account.json"),- ).toBeInTheDocument();- await waitFor(() => {- expect(screen.getByRole("button", { name: "Connect" })).toBeEnabled();- });-- await userEvent.click(- screen.getByRole("button", { name: "Clear the selected file" }),- );- expect(- screen.queryByText("service-account.json"),- ).not.toBeInTheDocument();- expect(screen.getByRole("button", { name: "Connect" })).toBeDisabled();- });-- it("connects by sending the qualified model and the credentials object in one request", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- updateResponse: {- value: "google/google/gemini-3.5-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.test-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- // A bare Gemini model ID gets the `google/` publisher prefix.- await userEvent.type(screen.getByLabelText("Model"), "gemini-3.5-flash");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // The location field did not change, thus it is not sent. Only changed fields are sent.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.5-flash",- credentials: {- "oauth-access-token": "ya29.test-token",- "project-id": "my-project",- },- },- ]);- });- });-- it("keeps an explicitly publisher-qualified model ID unchanged", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- updateResponse: {- value: "google/google/gemini-3.6-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.test-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- await userEvent.type(- screen.getByLabelText("Model"),- "google/gemini-3.6-flash",- );- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.6-flash",- credentials: {- "oauth-access-token": "ya29.test-token",- "project-id": "my-project",- },- },- ]);- });- });-- it("indicates a saved service account key without echoing it and shows the saved model", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleServiceAccountValue: "**********\n}",- });-- // The auth toggle defaults to the saved credential type.- expect(- await screen.findByText("Service account key file"),- ).toBeInTheDocument();- expect(- screen.getByText("A service account key is saved."),- ).toBeInTheDocument();- expect(- screen.queryByLabelText("OAuth token", CREDENTIAL_INPUT),- ).not.toBeInTheDocument();- // The saved model is shown as the bare Gemini ID, without the `google/` publisher prefix.- expect(await screen.findByLabelText("Model")).toHaveValue(- "gemini-3.5-flash",- );- });-- it("clears a saved service account key when connecting with an OAuth token instead", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleServiceAccountValue: "**********\n}",- updateResponse: {- value: "google/google/gemini-3.5-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- // The toggle starts on the saved service account. Change to the OAuth token auth type.- await screen.findByText("Service account key file");- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.new-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // The service account field goes as null. The backend prefers the service account key to- // the OAuth token, thus the old key must not stay active.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.5-flash",- credentials: {- "oauth-access-token": "ya29.new-token",- "project-id": "my-project",- "service-account-key": null,- },- },- ]);- });- });-- it("opens on the service account key when both credentials are saved, matching the backend's precedence", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleServiceAccountValue: "**********\n}",- googleOauthTokenValue: "**********en",- });-- expect(- await screen.findByText("Service account key file"),- ).toBeInTheDocument();- expect(- screen.queryByLabelText("OAuth token", CREDENTIAL_INPUT),- ).not.toBeInTheDocument();- });-- it("leaves an env-backed service account key alone when connecting with an OAuth token", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleServiceAccountValue: "**********\n}",- googleEnvSettings: {- "llm-google-service-account-key": "MB_LLM_GOOGLE_SERVICE_ACCOUNT_KEY",- },- updateResponse: {- value: "google/google/gemini-3.5-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- await screen.findByLabelText("Project ID");- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.new-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // An env-set key cannot be cleared, so the request leaves it out.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.5-flash",- credentials: {- "oauth-access-token": "ya29.new-token",- "project-id": "my-project",- },- },- ]);- });- });-- it("shows the saved credentials and model for a connected provider", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleOauthTokenValue: "**********en",- });-- expect(- await screen.findByLabelText("OAuth token", CREDENTIAL_INPUT),- ).toHaveValue("**********en");- expect(screen.getByLabelText("Project ID")).toHaveValue("my-project");- expect(screen.getByLabelText("Model")).toHaveValue("gemini-3.5-flash");- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });-- it("shows Connect instead of Disconnect when the connected model is edited", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleOauthTokenValue: "**********en",- updateResponse: {- value: "google/google/gemini-3.6-flash",- models: DEFAULT_RESPONSES.google.models,- },- });-- expect(await screen.findByLabelText("Model")).toHaveValue(- "gemini-3.5-flash",- );- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();-- await userEvent.clear(screen.getByLabelText("Model"));- await userEvent.type(screen.getByLabelText("Model"), "gemini-3.6-flash");-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "google",- model: "google/gemini-3.6-flash",- credentials: {},- },- ]);- });- });-- it("shows the connect error when validating the model fails", async () => {- await setup({- savedProviderValue: null,- isConfigured: false,- metabotSettingsUpdateResponse: {- status: 400,- body: {- message:- "Google API endpoint is unavailable or the model was not found.",- },- },- });-- await selectProvider("Gemini Enterprise Agent Platform");- await screen.findByLabelText("Project ID");-- await userEvent.click(screen.getByText("OAuth token"));- await userEvent.type(- screen.getByLabelText("OAuth token", CREDENTIAL_INPUT),- "ya29.test-token",- );- await userEvent.type(screen.getByLabelText("Project ID"), "my-project");- await userEvent.type(screen.getByLabelText("Model"), "gemini-9000");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- expect(- await screen.findByText(- "Google API endpoint is unavailable or the model was not found.",- ),- ).toBeInTheDocument();- });-- it("disconnects by clearing the credentials before the provider setting", async () => {- await setup({- savedProviderValue: "google/google/gemini-3.5-flash",- googleOauthTokenValue: "**********en",- });-- await screen.findByLabelText("Project ID");- await confirmDisconnectProvider();-- // The credentials must be cleared before the provider setting.- await waitFor(async () => {- expect(await findPutRequests()).toEqual([- {- path: METABOT_SETTINGS_PATH,- body: { provider: "google", credentials: null },- },- {- path: SETTING_PATH,- body: { "llm-metabot-provider": null },- },- ]);- });- });- });-- describe("Amazon Bedrock", () => {- it("shows the AWS credential fields when Amazon Bedrock is selected", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Amazon Bedrock");-- expect(await screen.findByLabelText("Access key ID")).toBeInTheDocument();- expect(screen.getByLabelText("Secret access key")).toBeInTheDocument();- expect(screen.getByLabelText("Region")).toBeInTheDocument();- expect(screen.getByLabelText("Session token")).toBeInTheDocument();- expect(screen.queryByLabelText("API key")).not.toBeInTheDocument();- });-- it("connects Amazon Bedrock by sending the credentials object", async () => {- await setup({ savedProviderValue: null, isConfigured: false });-- await selectProvider("Amazon Bedrock");-- await userEvent.type(- await screen.findByLabelText("Access key ID"),- "AKIDEXAMPLE",- );- await userEvent.type(- screen.getByLabelText("Secret access key"),- "test-secret",- );- await userEvent.type(screen.getByLabelText("Region"), "us-east-2");- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // The untouched session token field is omitted entirely — only changed fields are sent.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "bedrock",- credentials: {- "access-key-id": "AKIDEXAMPLE",- "secret-access-key": "test-secret",- region: "us-east-2",- },- },- ]);- });- });-- it("does not echo obfuscated credentials when editing only the region of a connected Bedrock provider", async () => {- await setup({- savedProviderValue: "bedrock/anthropic.claude-haiku-4-5",- apiKeyValues: { bedrock: "**********LE" },- });-- const regionInput = await screen.findByLabelText("Region");- await userEvent.clear(regionInput);- await userEvent.type(regionInput, "us-west-2");-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // The untouched access key, secret, and session token round-trip as obfuscated placeholders- // in the form, and must be omitted so the backend leaves the real saved values intact.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "bedrock",- credentials: {- region: "us-west-2",- },- },- ]);- });- });-- it("clears the session token by sending an explicit null without touching the other fields", async () => {- await setup({- savedProviderValue: "bedrock/anthropic.claude-haiku-4-5",- apiKeyValues: { bedrock: "**********LE" },- });-- const sessionTokenInput = await screen.findByLabelText("Session token");- await userEvent.clear(sessionTokenInput);-- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-- // null means an explicit clear; the untouched fields are omitted so the saved keys survive.- await waitFor(async () => {- expect(await findMetabotSettingsUpdates()).toEqual([- {- provider: "bedrock",- credentials: {- "session-token": null,- },- },- ]);- });- });-- it("shows the grouped model picker for a connected Bedrock provider", async () => {- await setup({- savedProviderValue: "bedrock/anthropic.claude-haiku-4-5",- apiKeyValues: { bedrock: "**********LE" },- });-- await screen.findByLabelText("Access key ID");- await screen.findByLabelText("Model");-- await openModelSelector();-- expect(await screen.findByText("Anthropic")).toBeInTheDocument();- expect(screen.getByText("OpenAI")).toBeInTheDocument();- });-- it("disconnects Bedrock by clearing the credentials before the provider setting", async () => {- await setup({- savedProviderValue: "bedrock/anthropic.claude-haiku-4-5",- apiKeyValues: { bedrock: "**********LE" },- });-- await screen.findByLabelText("Access key ID");- await confirmDisconnectProvider();-- // The credentials must be cleared before the provider setting.- await waitFor(async () => {- expect(await findPutRequests()).toEqual([- {- path: METABOT_SETTINGS_PATH,- body: { provider: "bedrock", credentials: null },- },- {- path: SETTING_PATH,- body: { "llm-metabot-provider": null },- },- ]);- });-- expect(- await screen.findByText("Connect to an AI provider"),- ).toBeInTheDocument();- });-- it("does not clear the provider setting if clearing the Bedrock credentials fails", async () => {- const { store } = await setup({- savedProviderValue: "bedrock/anthropic.claude-haiku-4-5",- apiKeyValues: { bedrock: "**********LE" },- metabotSettingsUpdateResponse: { status: 500 },- });-- await screen.findByLabelText("Access key ID");- await confirmDisconnectProvider();-- await waitFor(() => {- expect(- store- .getState()- .undo.some(- (toast) => toast.message === "Unable to save provider settings.",- ),- ).toBe(true);- });-- expect(await findSettingUpdates()).toEqual([]);- expect(- screen.getByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });- });-});--const DEFAULT_PROVIDER_VALUE = "anthropic/claude-sonnet-4-6";-const DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-4-6";-const DIVERGENT_MODELS = [- { id: "claude-sonnet-4-6", display_name: "Claude Sonnet 4.6" },- { id: "claude-sonnet-5", display_name: "Claude Sonnet 5" },-];--function setupDivergentReadsBackend({- delayAdminSettingDetailsRefetch = false,-}: { delayAdminSettingDetailsRefetch?: boolean } = {}) {- fetchMock.removeRoutes();- fetchMock.clearHistory();-- const backend: { apiKeySaved: boolean; providerRow: string | null } = {- apiKeySaved: false,- providerRow: null,- };-- const providerValue = () => backend.providerRow ?? DEFAULT_PROVIDER_VALUE;- const isConfigured = () => backend.apiKeySaved;-- const sessionPropertiesResponse = () =>- createMockSettings({- "llm-metabot-provider": providerValue(),- "llm-metabot-configured?": isConfigured(),- });-- const disconnectedSnapshot = sessionPropertiesResponse();- let pendingStaleSessionPropertiesReads = 0;-- fetchMock.get("path:/api/session/properties", () => {- if (pendingStaleSessionPropertiesReads > 0) {- pendingStaleSessionPropertiesReads -= 1;- return disconnectedSnapshot;- }- return sessionPropertiesResponse();- });-- const adminSettingDetailsResponse = () => [- createMockSettingDefinition({- key: "llm-metabot-provider",- value: backend.providerRow ?? undefined,- }),- createMockSettingDefinition({- key: "llm-anthropic-api-key",- value: backend.apiKeySaved ? "**********ey" : undefined,- }),- ];-- const detailsRefetchDeferred = defer<void>();- let adminSettingDetailsRequestCount = 0;-- fetchMock.get("path:/api/setting", async () => {- adminSettingDetailsRequestCount += 1;- if (- delayAdminSettingDetailsRefetch &&- adminSettingDetailsRequestCount > 1- ) {- await detailsRefetchDeferred.promise;- }- return adminSettingDetailsResponse();- });-- fetchMock.get("path:/api/metabot/settings", () => ({- value: providerValue(),- models: backend.apiKeySaved ? DIVERGENT_MODELS : [],- }));-- fetchMock.put("path:/api/metabot/settings", (call) => {- const body: MetabotSettingsUpdateBody = JSON.parse(- String(call.options?.body ?? "{}"),- );- const currentProviderFromGetter = providerValue().split("/")[0];- const providerChanged = currentProviderFromGetter !== body.provider;-- if ("api-key" in body) {- backend.apiKeySaved = Boolean(body["api-key"]);- }-- const model =- body.model ?? (providerChanged ? DEFAULT_ANTHROPIC_MODEL : null);- if (model) {- backend.providerRow = `${body.provider}/${model}`;- }-- return { value: providerValue(), models: DIVERGENT_MODELS };- });-- renderWithProviders(- <Route path="/admin/metabot*" element={<AIProviderSettingsSection />} />,- {- withRouter: true,- initialRoute: "/admin/metabot",- storeInitialState: {- settings: mockSettings(disconnectedSnapshot),- currentUser: createMockUser({ is_superuser: true }),- },- },- );-- return {- backend,- releaseAdminSettingDetailsRefetch: () => detailsRefetchDeferred.resolve(),- markNextSessionPropertiesReadStale: () => {- pendingStaleSessionPropertiesReads += 1;- },- };-}--async function connectToAnthropicFromScratch() {- await screen.findByText("Connect to an AI provider");- await userEvent.click(await screen.findByLabelText("Provider"));- await userEvent.click(- await screen.findByRole("option", { name: "Anthropic" }),- );- await userEvent.type(- await screen.findByLabelText("API key"),- "sk-ant-test-key",- );- await userEvent.click(screen.getByRole("button", { name: "Connect" }));-}--describe("AIProviderSettingsSection with divergent settings reads", () => {- it("keeps the entered API key and offers the model picker while the setting-details refetch lags behind session-properties", async () => {- const { releaseAdminSettingDetailsRefetch } = setupDivergentReadsBackend({- delayAdminSettingDetailsRefetch: true,- });-- await connectToAnthropicFromScratch();-- expect(await screen.findByLabelText("Model")).toBeInTheDocument();- expect(screen.getByLabelText("API key")).toHaveValue("sk-ant-test-key");-- await userEvent.click(screen.getByLabelText("Model"));- expect(- await screen.findByRole("option", { name: "Claude Sonnet 5" }),- ).toBeInTheDocument();- await userEvent.keyboard("{Escape}");-- releaseAdminSettingDetailsRefetch();-- await waitFor(() => {- expect(screen.getByLabelText("API key")).toHaveValue("**********ey");- });- expect(- await screen.findByText("Connected to Anthropic"),- ).toBeInTheDocument();- expect(- await screen.findByRole("button", { name: "Disconnect" }),- ).toBeInTheDocument();- });-- it("keeps the provider and model selection when a stale node serves the session-properties refetch after picking a model", async () => {- const { backend, markNextSessionPropertiesReadStale } =- setupDivergentReadsBackend();-- await connectToAnthropicFromScratch();- await screen.findByText("Connected to Anthropic");- await waitFor(() => {- expect(screen.getByLabelText("Model")).toHaveValue("Claude Sonnet 4.6");- });-- // Let the session-properties refetch land before arming the one-shot stale marker.- // Otherwise that in-flight refetch consumes it and the model-pick refetch below- // gets fresh data instead of stale.- await waitFor(() => {- expect(- fetchMock.callHistory.calls("path:/api/session/properties"),- ).toHaveLength(2);- });- await act(async () => {- await fetchMock.callHistory.flush(true);- });-- markNextSessionPropertiesReadStale();-- await userEvent.click(screen.getByLabelText("Model"));- await userEvent.click(- await screen.findByRole("option", { name: "Claude Sonnet 5" }),- );-- await waitFor(() => {- expect(backend.providerRow).toBe("anthropic/claude-sonnet-5");- });-- // Wait for the stale refetch to flip the section back to the setup layout- // then check the form kept the new selection instead of resetting to the stale snapshot.- expect(await screen.findByLabelText("Provider")).toHaveValue("Anthropic");- expect(screen.getByLabelText("API key")).toBeInTheDocument();- expect(screen.getByLabelText("Model")).toHaveValue("Claude Sonnet 5");});});diff --git a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsxindex 55a74d242c3..307d52fc1ef 100644--- a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx@@ -12,6 +12,9 @@ import {setupUpdateSettingEndpoint,} from "__support__/server-mocks";import {+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,setupMetabotPromptSuggestionsEndpoint,setupMetabotsEndpoints,} from "__support__/server-mocks/metabot";@@ -101,32 +104,11 @@ const setup = async ({key: "llm-metabot-provider",value: null,}),- createMockSettingDefinition({- key: "llm-anthropic-api-key",- value: undefined,- }),- createMockSettingDefinition({- key: "llm-openai-api-key",- value: undefined,- }),- createMockSettingDefinition({- key: "llm-openrouter-api-key",- value: undefined,- }),- createMockSettingDefinition({- key: "llm-zai-api-key",- value: undefined,- }),- createMockSettingDefinition({- key: "llm-mistral-api-key",- value: undefined,- }),- createMockSettingDefinition({- key: "llm-moonshot-api-key",- value: undefined,- }),]);setupUpdateSettingEndpoint();+ setupLlmProviderTypesEndpoint();+ setupLlmProvidersEndpoint();+ setupLlmModelsEndpoint();setupCollectionByIdEndpoint({ collections });setupRootCollectionItemsEndpoint({ rootCollectionItems: [] });setupCollectionsEndpoints({ collections: [] });diff --git a/frontend/src/metabase/api/llm.ts b/frontend/src/metabase/api/llm.tsindex b1164f6da50..afc6d5e2af4 100644--- a/frontend/src/metabase/api/llm.ts+++ b/frontend/src/metabase/api/llm.ts@@ -1,9 +1,15 @@import type {+ CreateLlmProviderRequest,ExtractSourcesRequest,ExtractSourcesResponse,+ LlmConnectionModels,+ LlmProviderConnection,+ LlmProviderType,+ UpdateLlmProviderRequest,} from "metabase-types/api";import { Api } from "./api";+import { invalidateTags, listTag } from "./tags";export const llmApi = Api.injectEndpoints({endpoints: (builder) => ({@@ -17,7 +23,79 @@ export const llmApi = Api.injectEndpoints({body,}),}),+ listLlmProviderTypes: builder.query<LlmProviderType[], void>({+ query: () => ({+ method: "GET",+ url: "/api/llm/provider-types",+ }),+ }),+ listLlmProviders: builder.query<LlmProviderConnection[], void>({+ query: () => ({+ method: "GET",+ url: "/api/llm/providers",+ }),+ providesTags: () => [listTag("llm-providers")],+ }),+ listLlmModels: builder.query<LlmConnectionModels[], void>({+ query: () => ({+ method: "GET",+ url: "/api/llm/models",+ }),+ providesTags: () => [listTag("llm-models")],+ }),+ createLlmProvider: builder.mutation<+ LlmProviderConnection,+ CreateLlmProviderRequest+ >({+ query: (body) => ({+ method: "POST",+ url: "/api/llm/providers",+ body,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [+ listTag("llm-providers"),+ listTag("llm-models"),+ "session-properties",+ ]),+ }),+ updateLlmProvider: builder.mutation<+ LlmProviderConnection,+ UpdateLlmProviderRequest+ >({+ query: ({ key, ...body }) => ({+ method: "PUT",+ url: `/api/llm/providers/${encodeURIComponent(key)}`,+ body,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [+ listTag("llm-providers"),+ listTag("llm-models"),+ "session-properties",+ ]),+ }),+ deleteLlmProvider: builder.mutation<void, string>({+ query: (key) => ({+ method: "DELETE",+ url: `/api/llm/providers/${encodeURIComponent(key)}`,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [+ listTag("llm-providers"),+ listTag("llm-models"),+ "session-properties",+ ]),+ }),}),});-export const { useExtractSourcesQuery } = llmApi;+export const {+ useExtractSourcesQuery,+ useListLlmProviderTypesQuery,+ useListLlmProvidersQuery,+ useListLlmModelsQuery,+ useCreateLlmProviderMutation,+ useUpdateLlmProviderMutation,+ useDeleteLlmProviderMutation,+} = llmApi;diff --git a/frontend/src/metabase/api/tags/constants.ts b/frontend/src/metabase/api/tags/constants.tsindex 2eed1eca48b..525c9296525 100644--- a/frontend/src/metabase/api/tags/constants.ts+++ b/frontend/src/metabase/api/tags/constants.ts@@ -32,6 +32,7 @@ export const TAG_TYPES = ["indexed-entity","table-index","llm-models",+ "llm-providers","logger-preset","measure","metric-dimension",diff --git a/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.tsx b/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.tsxindex c85233c6d98..37536e9a3a0 100644--- a/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.tsx+++ b/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.tsx@@ -69,6 +69,7 @@ export const ConfirmModal = ({<Buttoncolor="feedback-negative"variant="filled"+ data-autofocus{...confirmButtonProps}disabled={confirmButtonProps.disabled || confirming}onClick={handleConfirm}diff --git a/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.unit.spec.tsx b/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.unit.spec.tsxnew file mode 100644index 00000000000..433791a3623--- /dev/null+++ b/frontend/src/metabase/common/components/ConfirmModal/ConfirmModal.unit.spec.tsx@@ -0,0 +1,55 @@+import userEvent from "@testing-library/user-event";++import { renderWithProviders, screen } from "__support__/ui";++import { ConfirmModal } from "./ConfirmModal";++interface SetupOpts {+ confirmButtonText?: string;+}++const setup = ({ confirmButtonText }: SetupOpts = {}) => {+ const onConfirm = jest.fn();+ const onClose = jest.fn();++ renderWithProviders(+ <ConfirmModal+ opened+ title="Remove this provider?"+ message="This cannot be undone."+ confirmButtonText={confirmButtonText}+ onConfirm={onConfirm}+ onClose={onClose}+ />,+ );++ return { onConfirm, onClose };+};++describe("ConfirmModal", () => {+ it("confirms when pressing enter instead of dismissing the modal", async () => {+ const { onConfirm, onClose } = setup({ confirmButtonText: "Remove" });++ await userEvent.keyboard("{Enter}");++ expect(onConfirm).toHaveBeenCalled();+ expect(onClose).not.toHaveBeenCalled();+ });++ it("closes when pressing escape", async () => {+ const { onConfirm, onClose } = setup();++ await userEvent.keyboard("{Escape}");++ expect(onClose).toHaveBeenCalled();+ expect(onConfirm).not.toHaveBeenCalled();+ });++ it("confirms when clicking the confirm button", async () => {+ const { onConfirm } = setup({ confirmButtonText: "Remove" });++ await userEvent.click(screen.getByRole("button", { name: "Remove" }));++ expect(onConfirm).toHaveBeenCalled();+ });+});diff --git a/frontend/src/metabase/home/components/Onboarding/tests/setup.tsx b/frontend/src/metabase/home/components/Onboarding/tests/setup.tsxindex d3b63d918a3..7ee9f199cef 100644--- a/frontend/src/metabase/home/components/Onboarding/tests/setup.tsx+++ b/frontend/src/metabase/home/components/Onboarding/tests/setup.tsx@@ -1,6 +1,8 @@import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import {setupBugReportingDetailsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,setupPropertiesEndpoints,setupSettingsEndpoints,} from "__support__/server-mocks";@@ -48,6 +50,8 @@ export const setup = ({setupBugReportingDetailsEndpoint();// The AI item's "Connect to an AI provider" modal reads admin settings.setupSettingsEndpoints([]);+ setupLlmProvidersEndpoint();+ setupLlmProviderTypesEndpoint();const state = createMockState({app: createMockAppState({tempStorage: {diff --git a/frontend/src/metabase/metabot/api.ts b/frontend/src/metabase/metabot/api.tsindex fad92398976..0d3294db1ac 100644--- a/frontend/src/metabase/metabot/api.ts+++ b/frontend/src/metabase/metabot/api.ts@@ -12,23 +12,17 @@ import type {MetabotGenerateContentResponse,MetabotId,MetabotInfo,- MetabotProvider,- MetabotSettingsResponse,MetabotSlackSettings,MetabotSourceFeedback,RegenerateSuggestedMetabotPromptsResponse,SaveMetabotEntityRequest,SuggestedMetabotPromptsRequest,SuggestedMetabotPromptsResponse,- UpdateMetabotSettingsRequest,UserMetabotPermissionsResponse,} from "metabase-types/api";import type { MetabotConversationDetail } from "./utils/normalize-fetched-chat-messages";-const touchesCredentials = (body: UpdateMetabotSettingsRequest) =>- "credentials" in body || "api-key" in body;-export const metabotApi = Api.injectEndpoints({endpoints: (builder) => ({listMetabots: builder.query<{ items: MetabotInfo[] }, void>({@@ -77,34 +71,6 @@ export const metabotApi = Api.injectEndpoints({url: `/api/metabot/conversations/${conversationId}/title`,}),}),- getMetabotSettings: builder.query<- MetabotSettingsResponse,- { provider: MetabotProvider }- >({- query: ({ provider }) => ({- method: "GET",- url: "/api/metabot/settings",- params: { provider },- }),- providesTags: () => [listTag("llm-models")],- }),- updateMetabotSettings: builder.mutation<- MetabotSettingsResponse,- UpdateMetabotSettingsRequest- >({- query: (body) => ({- method: "PUT",- url: "/api/metabot/settings",- body,- }),- invalidatesTags: (_, error, body) =>- invalidateTags(error, [- "session-properties",- // A credential write can change which models the provider serves, e.g. a different Bedrock- // region, Google location, or Azure resource.- ...(touchesCredentials(body) ? [listTag("llm-models")] : []),- ]),- }),updateMetabot: builder.mutation<MetabotInfo,{ id: MetabotId } & Partial<@@ -219,12 +185,10 @@ export const metabotApi = Api.injectEndpoints({});export const {- useGetMetabotSettingsQuery,useGetMetabotConversationQuery,useForkMetabotConversationMutation,useListMetabotConversationsQuery,useListMetabotsQuery,- useUpdateMetabotSettingsMutation,useUpdateMetabotMutation,useGetSuggestedMetabotPromptsQuery,useDeleteSuggestedMetabotPromptMutation,diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationContext.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationContext.tsdeleted file mode 100644index fb3684fb849..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationContext.ts+++ /dev/null@@ -1,69 +0,0 @@-import {- type MutableRefObject,- createContext,- useContext,- useEffect,-} from "react";--export const AIProviderConfigurationContext = createContext<{- connectHandlerRef: MutableRefObject<(() => Promise<void>) | null> | null;- disconnectHandlerRef: MutableRefObject<(() => Promise<void>) | null> | null;- isMutating: boolean;- isConnectButtonEnabled: boolean;- setIsConnectButtonEnabled: (enabled: boolean) => void;- resetProvider: VoidFunction;- handleDisconnect: VoidFunction;- isModal: boolean;-}>({- isMutating: false,- connectHandlerRef: null,- disconnectHandlerRef: null,- isConnectButtonEnabled: false,- setIsConnectButtonEnabled: () => {},- resetProvider: () => {},- handleDisconnect: () => {},- isModal: false,-});--export function useAIProviderConfigurationContext(- onConnect: (() => Promise<void>) | null,- onDisconnect: (() => Promise<void>) | null = null,-) {- const {- connectHandlerRef,- disconnectHandlerRef,- isMutating,- setIsConnectButtonEnabled,- resetProvider,- handleDisconnect,- isModal,- } = useContext(AIProviderConfigurationContext);-- useEffect(() => {- if (!connectHandlerRef) {- return;- }-- connectHandlerRef.current = onConnect;- setIsConnectButtonEnabled(!!onConnect);-- return () => {- setIsConnectButtonEnabled(false);- connectHandlerRef.current = null;- };- }, [connectHandlerRef, onConnect, setIsConnectButtonEnabled]);-- useEffect(() => {- if (!disconnectHandlerRef) {- return;- }-- disconnectHandlerRef.current = onDisconnect;-- return () => {- disconnectHandlerRef.current = null;- };- }, [disconnectHandlerRef, onDisconnect]);-- return { isMutating, resetProvider, handleDisconnect, isModal };-}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsxdeleted file mode 100644index ff69e977982..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm.tsx+++ /dev/null@@ -1,432 +0,0 @@-import { useCallback, useEffect, useMemo, useRef, useState } from "react";-import { P, match } from "ts-pattern";-import { t } from "ttag";--import { getErrorMessage } from "metabase/api/utils";-import { ConfirmModal } from "metabase/common/components/ConfirmModal";-import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";-import { useToast } from "metabase/common/hooks";-import { PLUGIN_METABOT } from "metabase/plugins";-import {- useAdminSetting,- useAdminSettings,- useSetting,- useUpdateSettingsMutation,-} from "metabase/settings";-import {- Box,- Button,- Center,- Divider,- Flex,- Group,- Loader,- Select,- Stack,- Text,-} from "metabase/ui";-import type { MetabotProvider } from "metabase-types/api";--import { useUpdateMetabotSettingsMutation } from "../../api";--import { AIProviderConfigurationContext } from "./AIProviderConfigurationContext";-import { ApiKeyProviderFields } from "./ApiKeyProviderFields";-import { AzureProviderFields } from "./AzureProviderFields";-import { BedrockProviderFields } from "./BedrockProviderFields";-import { GoogleProviderFields } from "./GoogleProviderFields";-import {- API_KEY_SETTING_BY_PROVIDER,- getProviderOptions,- isAvailableProvider,- parseProviderAndModel,-} from "./utils";--interface AIProviderConfigurationFormProps {- isModal?: boolean;- defaultProvider?: MetabotProvider;- onClose?: (connectedProvider?: MetabotProvider) => void;- onSkip?: VoidFunction;-}--export function AIProviderConfigurationForm(- props: AIProviderConfigurationFormProps,-) {- const { isLoading } = useAdminSetting("llm-metabot-provider");-- // The saved provider loads asynchronously, and the form body computes its- // initial selection once at mount. Mounting before the value arrives would- // let `defaultProvider` win over an existing connection.- if (props.defaultProvider && isLoading) {- return (- <Center p="lg">- <Loader data-testid="loading-indicator" />- </Center>- );- }-- return <AIProviderConfigurationFormBody {...props} />;-}--function AIProviderConfigurationFormBody({- isModal = false,- defaultProvider,- onClose,- onSkip,-}: AIProviderConfigurationFormProps) {- const MetabaseAIProviderSetup = PLUGIN_METABOT.MetabaseAIProviderSetup;- const offerMetabaseAiManaged = PLUGIN_METABOT.isEnabled;- const [sendToast] = useToast();-- const { value: savedProviderValue, settingDetails } = useAdminSetting(- "llm-metabot-provider",- );- const isEnvSetting =- !!settingDetails &&- !!settingDetails.is_env_setting &&- !!settingDetails.env_name;- const envSettingName = isEnvSetting ? settingDetails?.env_name : undefined;-- const isConfigured = !!useSetting("llm-metabot-configured?");-- const config = useMemo(- () => parseProviderAndModel(savedProviderValue),- [savedProviderValue],- );- const connectedProvider = isConfigured ? config?.provider : undefined;- const connectedModel = isConfigured ? config?.model : undefined;- // `defaultProvider` is only a suggestion for the unconfigured state — an- // existing connection always wins over it. A modal without a suggestion- // starts unselected on purpose, even when connected (the "switch provider"- // flow).- const [provider, setProvider] = useState<MetabotProvider | undefined>(() => {- if (!isModal) {- return connectedProvider;- }- return defaultProvider ? (connectedProvider ?? defaultProvider) : undefined;- });-- const isCurrentConfigured = connectedProvider === provider && isConfigured;-- useEffect(() => {- if (isModal || !connectedProvider) {- return;- }- setProvider(connectedProvider);- }, [isModal, connectedProvider]);-- const [updateSettings, updateSettingsResult] = useUpdateSettingsMutation();- const [updateMetabotSettings] = useUpdateMetabotSettingsMutation();- const disconnectHandlerRef = useRef<(() => Promise<void>) | null>(null);-- // Setting details come from a single admin endpoint, so one `isLoading`- // covers every provider. Gate the provider fields on it below: their forms- // use Formik `enableReinitialize`/hydrate-once effects that would wipe- // in-progress input if they rendered before the saved credentials arrived.- const { details: providerApiKeyDetails, isLoading: areDetailsLoading } =- 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",- ] as const);-- const disconnectProvider = useCallback(async () => {- if (!connectedProvider) {- return;- }-- try {- await disconnectHandlerRef.current?.();- } catch {- return;- }-- const settingsToClear: Record<string, null> = {- "llm-metabot-provider": null,- };-- if (- connectedProvider !== "metabase" &&- connectedProvider !== "bedrock" &&- connectedProvider !== "azure" &&- connectedProvider !== "google"- ) {- const apiKeySettingKey = API_KEY_SETTING_BY_PROVIDER[connectedProvider];- const apiKeySetting = providerApiKeyDetails[apiKeySettingKey];-- if (!apiKeySetting?.is_env_setting) {- settingsToClear[apiKeySettingKey] = null;- }- }-- try {- if (- connectedProvider === "bedrock" ||- connectedProvider === "azure" ||- connectedProvider === "google"- ) {- // Bedrock, Azure, and Google key material spans several settings; an explicit- // `credentials: null` clears them all in one call. It runs before the provider- // is deselected so a failure can't leave saved keys behind.- await updateMetabotSettings({- provider: connectedProvider,- credentials: null,- }).unwrap();- }-- const response = await updateSettings(settingsToClear);-- if (response.error) {- const message = getErrorMessage(- response.error,- t`Unable to save provider settings.`,- );-- sendToast({- message,- icon: "warning",- toastColor: "feedback-negative",- });- } else {- setProvider(undefined);- }- } catch (error) {- const message = getErrorMessage(- error,- t`Unable to save provider settings.`,- );-- sendToast({- message,- icon: "warning",- toastColor: "feedback-negative",- });- }- }, [- connectedProvider,- disconnectHandlerRef,- providerApiKeyDetails,- updateMetabotSettings,- updateSettings,- sendToast,- ]);-- const providerOptions = useMemo(() => {- const options = Object.values(getProviderOptions(offerMetabaseAiManaged));- return options.map((option) => ({- ...option,- disabled: !isAvailableProvider(option.value),- }));- }, [offerMetabaseAiManaged]);-- const connectHandlerRef = useRef<(() => Promise<void>) | null>(null);-- const [isDisconnecting, setIsDisconnecting] = useState(false);- const [isDisconnectConfirmOpen, setIsDisconnectConfirmOpen] = useState(false);- const [isConnecting, setIsConnecting] = useState(false);- const handleConnect = async () => {- if (!connectHandlerRef.current) {- return;- }- setIsConnecting(true);- try {- await connectHandlerRef.current();- } finally {- setIsConnecting(false);- }- };-- const resetProvider = () => {- setProvider(undefined);- };-- const handleDisconnect = useCallback(() => {- setIsDisconnectConfirmOpen(true);- }, []);-- const handleConfirmDisconnect = async () => {- setIsDisconnecting(true);- try {- await disconnectProvider();- setIsDisconnectConfirmOpen(false);- } finally {- setIsDisconnecting(false);- }- };-- const isMutating =- isConnecting || isDisconnecting || updateSettingsResult.isLoading;-- const [isConnectButtonEnabled, setIsConnectButtonEnabled] = useState(false);-- return (- <AIProviderConfigurationContext.Provider- value={{- connectHandlerRef,- disconnectHandlerRef,- isMutating,- setIsConnectButtonEnabled,- isConnectButtonEnabled,- resetProvider,- handleDisconnect,- isModal: !!isModal,- }}- >- <Stack gap="md">- {!isCurrentConfigured && (- <Select- label={t`Provider`}- placeholder={t`Select a provider`}- data={providerOptions}- value={provider}- onChange={setProvider}- disabled={isEnvSetting || isMutating}- renderOption={({ option }) => (- <Group- gap="xs"- p="sm"- justify="space-between"- wrap="nowrap"- w="100%"- >- <Text- lh="1rem"- c={option.disabled ? "text-disabled" : undefined}- >- {option.label}- </Text>- {/* Unjustified type cast. FIXME */}- {!isAvailableProvider(option.value as MetabotProvider) && (- <Text c="text-disabled" lh="1rem" size="sm">- {t`Coming soon`}- </Text>- )}- </Group>- )}- />- )}-- {areDetailsLoading- ? null- : match(provider)- .with("metabase", () => (- <MetabaseAIProviderSetup- onConnect={() => onClose?.("metabase")}- />- ))- .with("azure", () => (- <AzureProviderFields- connectedModel={connectedModel}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- ))- .with("bedrock", () => (- <BedrockProviderFields- connectedModel={connectedModel}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- ))- .with("google", () => (- <GoogleProviderFields- connectedModel={connectedModel}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- ))- .with(- "anthropic",- "mistral",- "moonshot",- "openai",- "openrouter",- "zai",- (selectedProvider) => (- <ApiKeyProviderFields- key={selectedProvider}- selectedProvider={selectedProvider}- connectedModel={connectedModel}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- ),- )- .with(P.nullish, () => null)- .exhaustive()}-- {envSettingName && <SetByEnvVar varName={envSettingName} />}-- <Flex justify={onSkip ? "flex-start" : "end"}>- {match({ isCurrentConfigured, isConnectButtonEnabled, isModal })- .with({ isModal: true, isCurrentConfigured: true }, () => (- <Button- variant="filled"- loading={isMutating}- disabled={isMutating}- onClick={() => onClose?.(provider)}- >- {t`Done`}- </Button>- ))- .with(- { isCurrentConfigured: true, isConnectButtonEnabled: false },- () => (- <Button- c="feedback-negative"- loading={isMutating}- disabled={isMutating}- onClick={handleDisconnect}- >- {t`Disconnect`}- </Button>- ),- )- .with(- { isCurrentConfigured: false },- { isCurrentConfigured: true, isConnectButtonEnabled: true },- () => (- <Button- variant="filled"- loading={isMutating}- disabled={isMutating || !isConnectButtonEnabled}- onClick={handleConnect}- >- {t`Connect`}- </Button>- ),- )- .exhaustive()}- </Flex>-- {onSkip && !isCurrentConfigured && (- <>- <Divider mx={{ base: "-2rem", sm: "-4rem" }} mt="xl" mb="md" />- <Box>- <Button- onClick={onSkip}- variant="subtle"- px={0}- fw="normal"- disabled={isMutating}- >{t`I'll set this up later`}</Button>- <Text c="text-disabled" size="sm">- {t`You won't be able to use AI features until you connect a provider.`}- </Text>- </Box>- </>- )}- <ConfirmModal- opened={isDisconnectConfirmOpen}- onClose={() => setIsDisconnectConfirmOpen(false)}- title={t`Disconnect AI provider?`}- message={t`This will disconnect your AI provider and disable AI features across your instance until you connect a provider again.`}- confirmButtonText={t`Disconnect provider`}- onConfirm={handleConfirmDisconnect}- />- </Stack>- </AIProviderConfigurationContext.Provider>- );-}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.tsxnew file mode 100644index 00000000000..798b0341c50--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.tsx@@ -0,0 +1,353 @@+import { useDisclosure } from "@mantine/hooks";+import type { ReactNode } from "react";+import { Fragment, useId, useState } from "react";+import { t } from "ttag";++import {+ useDeleteLlmProviderMutation,+ useListLlmProviderTypesQuery,+ useListLlmProvidersQuery,+} from "metabase/api";+import { getErrorMessage } from "metabase/api/utils";+import { ConfirmModal } from "metabase/common/components/ConfirmModal";+import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";+import { useToast } from "metabase/common/hooks";+import { useLlmConnectionModels } from "metabase/metabot/hooks";+import { PLUGIN_METABOT } from "metabase/plugins";+import {+ ActionIcon,+ Box,+ Button,+ Collapse,+ Divider,+ Group,+ Icon,+ Menu,+ Skeleton,+ Stack,+ Text,+ Tooltip,+ UnstyledButton,+} from "metabase/ui";+import type {+ LlmProviderConnection,+ LlmProviderType,+} from "metabase-types/api";++import { LlmModelPicker } from "./LlmModelPicker";+import { ProviderConnectionModal } from "./ProviderConnectionModal";+import { ProviderTypeIcon } from "./ProviderTypeIcon";+import { getAddableProviderTypes } from "./addable-provider-types";++const PROVIDER_ICON_SIZE = 32;+const ROW_LINES = 2;+const ROW_LINE_HEIGHT = `${PROVIDER_ICON_SIZE / ROW_LINES}px`;+const PROVIDER_DETAILS_INDENT = "2.5rem";+// The warning glyph fills its viewBox, so it only sits on the label's cap band at this size.+const WARNING_ICON_SIZE = 12;++// mirrors a two-connection list: rows the height of PROVIDER_ICON_SIZE plus their py="sm",+// divided the same way, then the button that follows them+export function ProviderListSkeleton() {+ return (+ <Stack gap="xs" data-testid="provider-list-skeleton">+ <Stack gap={0}>+ <ProviderRowSkeleton />+ <Divider />+ <ProviderRowSkeleton />+ </Stack>+ <Skeleton h="1.5rem" w="10rem" />+ </Stack>+ );+}++function ProviderRowSkeleton() {+ return (+ <Box py="sm">+ <Skeleton h={PROVIDER_ICON_SIZE} />+ </Box>+ );+}++export function AIProviderList() {+ const {+ data: connections = [],+ isLoading: isLoadingConnections,+ error: connectionsError,+ } = useListLlmProvidersQuery();+ const {+ data: providerTypes = [],+ isLoading: isLoadingProviderTypes,+ error: providerTypesError,+ } = useListLlmProviderTypesQuery();+ const [deleteProvider] = useDeleteLlmProviderMutation();+ const { errorByConnectionKey } = useLlmConnectionModels();++ const [isAdding, { open: startAdding, close: stopAdding }] =+ useDisclosure(false);+ const [editing, setEditing] = useState<LlmProviderConnection | undefined>();+ const [deleting, setDeleting] = useState<LlmProviderConnection | undefined>();+ const [sendToast] = useToast();++ const handleModalClose = () => {+ stopAdding();+ setEditing(undefined);+ };++ const handleConfirmDelete = async () => {+ if (!deleting) {+ return;+ }+ const { error } = await deleteProvider(deleting.key);+ if (error) {+ sendToast({+ message: getErrorMessage(error, t`Unable to remove this provider.`),+ icon: "warning",+ toastColor: "feedback-negative",+ });+ return;+ }+ setDeleting(undefined);+ };++ if (isLoadingConnections || isLoadingProviderTypes) {+ return <ProviderListSkeleton />;+ }++ const loadError = connectionsError ?? providerTypesError;+ if (loadError) {+ return (+ <Text c="error">+ {getErrorMessage(loadError, t`Unable to load your AI providers.`)}+ </Text>+ );+ }++ const hasConnections = connections.length > 0;++ const addableProviderTypes = getAddableProviderTypes(+ providerTypes,+ connections,+ );++ return (+ <Stack gap="md">+ <Stack gap="xs">+ {hasConnections && (+ <Stack gap={0}>+ {connections.map((connection, index) => (+ <Fragment key={connection.key}>+ {index > 0 && <Divider />}+ <ProviderConnectionRow+ connection={connection}+ providerType={providerTypes.find(+ (type) => type.type === connection.type,+ )}+ modelsError={errorByConnectionKey[connection.key]}+ onEdit={() => setEditing(connection)}+ onDelete={() => setDeleting(connection)}+ />+ </Fragment>+ ))}+ </Stack>+ )}++ <Button+ variant={hasConnections ? "subtle" : "filled"}+ p={hasConnections ? 0 : undefined}+ w="fit-content"+ leftSection={<Icon name="add" />}+ onClick={startAdding}+ >+ {hasConnections ? t`Add another provider` : t`Add a provider`}+ </Button>+ </Stack>++ {hasConnections && <LlmModelPicker />}++ {(isAdding || editing) && (+ <ProviderConnectionModal+ providerTypes={editing ? providerTypes : addableProviderTypes}+ connection={editing}+ onClose={handleModalClose}+ />+ )}++ <ConfirmModal+ opened={deleting != null}+ onClose={() => setDeleting(undefined)}+ title={t`Remove this provider?`}+ message={getDeleteWarning(deleting, providerTypes)}+ confirmButtonText={t`Remove provider`}+ onConfirm={handleConfirmDelete}+ />+ </Stack>+ );+}++// Features that read a fixed connection key directly rather than following the Metabot selection: deleting+// the connection they name turns them off, which the admin deserves to hear before confirming.+const KEYED_DEPENDENTS: Record<string, () => string> = {+ anthropic: () =>+ t`SQL generation also runs on this connection, and will stop working without it.`,+ openai: () =>+ t`Semantic search also runs on this connection, and will stop working without it.`,+};++function getDeleteWarning(+ deleting: LlmProviderConnection | undefined,+ providerTypes: LlmProviderType[],+) {+ const base = providerTypes.find((type) => type.type === deleting?.type)+ ?.managed+ ? // eslint-disable-next-line metabase/no-literal-metabase-strings -- Metabase AI service+ t`This cancels your Metabase AI service subscription, and its models will no longer be available.`+ : t`This provider's models will no longer be available, and its saved credentials will be deleted.`;+ const dependent = deleting && KEYED_DEPENDENTS[deleting.key]?.();+ return dependent ? `${base} ${dependent}` : base;+}++function RowActions({ children }: { children: ReactNode }) {+ return (+ <Group gap="xs" wrap="nowrap" h={PROVIDER_ICON_SIZE} align="center">+ {children}+ </Group>+ );+}++function ProviderConnectionRow({+ connection,+ providerType,+ modelsError,+ onEdit,+ onDelete,+}: {+ connection: LlmProviderConnection;+ providerType?: LlmProviderType;+ modelsError?: string;+ onEdit: () => void;+ onDelete: () => void;+}) {+ const isEnvManaged = connection.source === "env";+ const isEditable = !providerType?.managed;+ const typeLabel =+ providerType && providerType.label !== connection.name+ ? providerType.label+ : undefined;++ const MetabaseAIProviderSetup = PLUGIN_METABOT.MetabaseAIProviderSetup;+ const hasUsageDetails =+ Boolean(providerType?.managed) &&+ PLUGIN_METABOT.hasMetabaseManagedProviderDetails();+ const [isShowingDetails, { toggle: toggleDetails }] = useDisclosure(true);+ const detailsId = useId();++ const summary = (+ <Group gap="sm" wrap="nowrap" flex={1} align="flex-start">+ <ProviderTypeIcon type={connection.type} size={PROVIDER_ICON_SIZE} />+ <Stack+ gap={0}+ align="flex-start"+ justify="center"+ mih={PROVIDER_ICON_SIZE}+ >+ <Group gap="xs" wrap="nowrap">+ <Text fw="bold" lh={ROW_LINE_HEIGHT}>+ {connection.name}+ </Text>+ {!connection.usable && (+ <Tooltip+ label={t`Some required settings are missing, so Metabot can't use this provider.`}+ >+ <Icon+ name="warning"+ c="error"+ size={WARNING_ICON_SIZE}+ aria-label={t`Incomplete configuration`}+ />+ </Tooltip>+ )}+ </Group>+ {typeLabel && (+ <Text size="sm" c="text-secondary" lh={ROW_LINE_HEIGHT}>+ {typeLabel}+ </Text>+ )}+ {modelsError && (+ <Text size="sm" c="error" lh={ROW_LINE_HEIGHT}>+ {modelsError}+ </Text>+ )}+ </Stack>+ </Group>+ );++ return (+ <Stack gap={0} data-testid={`provider-${connection.key}`}>+ <Group justify="space-between" wrap="nowrap" align="flex-start" py="sm">+ {hasUsageDetails ? (+ <UnstyledButton+ flex={1}+ aria-label={t`Usage and pricing`}+ aria-expanded={isShowingDetails}+ aria-controls={detailsId}+ onClick={toggleDetails}+ >+ <Group justify="space-between" wrap="nowrap" align="flex-start">+ {summary}+ <RowActions>+ <Icon+ name={isShowingDetails ? "chevronup" : "chevrondown"}+ size={12}+ />+ </RowActions>+ </Group>+ </UnstyledButton>+ ) : (+ summary+ )}++ <RowActions>+ {!isEnvManaged && (+ <Menu position="bottom-end">+ <Menu.Target>+ <ActionIcon aria-label={t`Provider options`}>+ <Icon name="ellipsis" />+ </ActionIcon>+ </Menu.Target>+ <Menu.Dropdown>+ {isEditable && (+ <Menu.Item+ leftSection={<Icon name="pencil" />}+ onClick={onEdit}+ >+ {t`Edit`}+ </Menu.Item>+ )}+ <Menu.Item+ leftSection={<Icon name="trash" />}+ c="error"+ onClick={onDelete}+ >+ {t`Remove`}+ </Menu.Item>+ </Menu.Dropdown>+ </Menu>+ )}+ </RowActions>+ </Group>++ {connection.env_vars.map((varName) => (+ <SetByEnvVar key={varName} varName={varName} />+ ))}++ {hasUsageDetails && (+ <Collapse id={detailsId} in={isShowingDetails}>+ <Box pl={PROVIDER_DETAILS_INDENT} pb="md">+ <MetabaseAIProviderSetup isConnected />+ </Box>+ </Collapse>+ )}+ </Stack>+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.unit.spec.tsxnew file mode 100644index 00000000000..54c4c7e07ef--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderList.unit.spec.tsx@@ -0,0 +1,159 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupPropertiesEndpoints,+ setupSettingsEndpoints,+} from "__support__/server-mocks";+import {+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+} from "__support__/server-mocks/metabot";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, within } from "__support__/ui";+import type { LlmConnectionModels } from "metabase-types/api";+import {+ createMockLlmConnectionModels,+ createMockLlmProviderConnection,+ createMockLlmProviderType,+ createMockSettings,+ createMockUser,+} from "metabase-types/api/mocks";++import { AIProviderList } from "./AIProviderList";++const setup = ({+ usable = true,+ models = [],+}: { usable?: boolean; models?: LlmConnectionModels[] } = {}) => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();++ const sessionProperties = createMockSettings();+ setupPropertiesEndpoints(sessionProperties);+ setupSettingsEndpoints([]);+ setupLlmProviderTypesEndpoint([createMockLlmProviderType()]);+ setupLlmProvidersEndpoint([+ createMockLlmProviderConnection({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ usable,+ }),+ createMockLlmProviderConnection({+ key: "openai",+ type: "openai",+ name: "OpenAI",+ }),+ ]);+ setupLlmModelsEndpoint(models);++ renderWithProviders(<AIProviderList />, {+ storeInitialState: {+ settings: mockSettings(sessionProperties),+ currentUser: createMockUser({ is_superuser: true }),+ },+ });+};++describe("AIProviderList", () => {+ afterEach(() => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();+ });++ it("does not badge a connection that has everything it needs", async () => {+ setup({ usable: true });++ expect(await screen.findByText("Anthropic")).toBeInTheDocument();+ expect(+ screen.queryByLabelText("Incomplete configuration"),+ ).not.toBeInTheDocument();+ });++ it("warns about a connection that is missing required settings", async () => {+ setup({ usable: false });++ expect(await screen.findByText("Anthropic")).toBeInTheDocument();+ expect(+ screen.getByLabelText("Incomplete configuration"),+ ).toBeInTheDocument();+ });++ it("shows a model-loading failure on the provider it belongs to", async () => {+ setup({+ models: [+ createMockLlmConnectionModels({+ key: "anthropic",+ name: "Anthropic",+ type: "anthropic",+ models: [],+ error: "Anthropic API key expired or invalid",+ }),+ createMockLlmConnectionModels({+ key: "openai",+ name: "OpenAI",+ type: "openai",+ }),+ ],+ });++ expect(+ await within(await screen.findByTestId("provider-anthropic")).findByText(+ "Anthropic API key expired or invalid",+ ),+ ).toBeInTheDocument();+ expect(+ within(screen.getByTestId("provider-openai")).queryByText(+ "Anthropic API key expired or invalid",+ ),+ ).not.toBeInTheDocument();+ });++ it("fetches the models for every provider in a single shared request", async () => {+ setup({+ models: [+ createMockLlmConnectionModels({ key: "anthropic", name: "Anthropic" }),+ ],+ });++ expect(await screen.findByTestId("provider-anthropic")).toBeInTheDocument();+ expect(await screen.findByText("Model")).toBeInTheDocument();++ expect(fetchMock.callHistory.calls("path:/api/llm/models")).toHaveLength(1);+ });++ it.each([+ ["anthropic", "SQL generation also runs on this connection"],+ ["openai", "Semantic search also runs on this connection"],+ ])(+ "warns that removing the %s connection also turns off the feature reading it",+ async (key, warning) => {+ setup();++ const row = await screen.findByTestId(`provider-${key}`);+ await userEvent.click(within(row).getByLabelText("Provider options"));+ await userEvent.click(await screen.findByText("Remove"));++ const modal = await screen.findByRole("dialog", {+ name: "Remove this provider?",+ });+ expect(within(modal).getByText(new RegExp(warning))).toBeInTheDocument();+ expect(+ within(modal).getByText(/saved credentials will be deleted/),+ ).toBeInTheDocument();+ },+ );++ it("shows the skeleton until the connections have loaded", async () => {+ setup();++ expect(screen.getByTestId("provider-list-skeleton")).toBeInTheDocument();++ expect(await screen.findByTestId("provider-anthropic")).toBeInTheDocument();+ expect(+ screen.queryByTestId("provider-list-skeleton"),+ ).not.toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderSetup.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderSetup.tsxnew file mode 100644index 00000000000..79c367eb2fb--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AIProviderSetup.tsx@@ -0,0 +1,85 @@+import { useDisclosure } from "@mantine/hooks";+import { t } from "ttag";++import {+ useListLlmProviderTypesQuery,+ useListLlmProvidersQuery,+} from "metabase/api";+import { getErrorMessage } from "metabase/api/utils";+import { Button, Flex, Stack, Text } from "metabase/ui";++import { ProviderListSkeleton } from "./AIProviderList";+import { LlmModelPicker } from "./LlmModelPicker";+import { ProviderConnectionForm } from "./ProviderConnectionForm";+import { getAddableProviderTypes } from "./addable-provider-types";++export function AIProviderSetup({+ onDone,+ startOnConnectionForm = false,+}: {+ onDone?: () => void;+ // Callers that already have a connection but are here to add another one — the out-of-tokens flow, where the+ // managed connection exists and the point is to bring your own key alongside it.+ startOnConnectionForm?: boolean;+}) {+ const {+ data: connections = [],+ isLoading: isLoadingConnections,+ error: connectionsError,+ } = useListLlmProvidersQuery();+ const {+ data: providerTypes = [],+ isLoading: isLoadingProviderTypes,+ error: providerTypesError,+ } = useListLlmProviderTypesQuery();++ const [hasJustConnected, { open: markConnected }] = useDisclosure(false);++ if (isLoadingConnections || isLoadingProviderTypes) {+ return <ProviderListSkeleton />;+ }++ const loadError = connectionsError ?? providerTypesError;+ if (loadError) {+ return (+ <Text c="error">+ {getErrorMessage(loadError, t`Unable to load your AI providers.`)}+ </Text>+ );+ }++ const hasUsableConnection = connections.some(+ (connection) => connection.usable,+ );+ const isPickingModel =+ hasJustConnected || (!startOnConnectionForm && hasUsableConnection);++ if (isPickingModel) {+ return (+ <Stack gap="lg">+ <LlmModelPicker />+ <Flex justify="end">+ <Button variant="filled" onClick={onDone}>+ {t`Done`}+ </Button>+ </Flex>+ </Stack>+ );+ }++ return (+ <ProviderConnectionForm+ providerTypes={getAddableProviderTypes(providerTypes, connections)}+ onSaved={(saved) => {+ const providerType = providerTypes.find(+ (type) => type.type === saved?.type,+ );+ if (providerType && !providerType.managed) {+ markConnected();+ } else {+ onDone?.();+ }+ }}+ />+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsxdeleted file mode 100644index 543dcf23c00..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ApiKeyProviderFields.tsx+++ /dev/null@@ -1,133 +0,0 @@-import { type ChangeEvent, useEffect, useState } from "react";-import { c, t } from "ttag";--import { getErrorMessage } from "metabase/api/utils";-import { ExternalLink } from "metabase/common/components/ExternalLink";-import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";-import { useAdminSettings } from "metabase/settings";-import { Text, TextInput } from "metabase/ui";--import { useUpdateMetabotSettingsMutation } from "../../api";--import { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";-import {- ProviderModelPicker,- useProviderModelsQuery,-} from "./ProviderModelPicker";-import {- API_KEY_SETTING_BY_PROVIDER,- type MetabotApiKeyProvider,- getProviderOptions,- hasConfiguredSettingValue,-} from "./utils";--export const ApiKeyProviderFields = ({- selectedProvider,- connectedModel,- isCurrentConfigured,- isEnvSetting,-}: {- selectedProvider: MetabotApiKeyProvider;- connectedModel: string | undefined;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const [localApiKey, setLocalApiKey] = useState<string | null>(null);- const [updateMetabotSettings, updateMetabotSettingsResult] =- useUpdateMetabotSettingsMutation();-- 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",- ] as const);- const apiKeySetting = details[API_KEY_SETTING_BY_PROVIDER[selectedProvider]];- const apiKeyEnvSettingName = apiKeySetting?.is_env_setting- ? apiKeySetting.env_name- : undefined;-- const onConnect = async () => {- await updateMetabotSettings({- provider: selectedProvider,- ...(localApiKey !== null && { "api-key": localApiKey || null }),- }).unwrap();- };-- const hasDirtyApiKey = localApiKey !== null;- const connectHandler =- !isCurrentConfigured || hasDirtyApiKey ? onConnect : null;- const { isMutating } = useAIProviderConfigurationContext(connectHandler);-- const hasVerifiedApiKey = updateMetabotSettingsResult.isSuccess;- const needsApiKey =- !hasConfiguredSettingValue(apiKeySetting) && !hasVerifiedApiKey;- const { modelsQuery, credentialsError: savedCredentialsError } =- useProviderModelsQuery(selectedProvider, { skip: needsApiKey });- const credentialsError = hasDirtyApiKey ? undefined : savedCredentialsError;-- const queriedModels = modelsQuery.currentData?.models ?? [];- const verifiedModels = updateMetabotSettingsResult.data?.models ?? [];- const models = queriedModels.length > 0 ? queriedModels : verifiedModels;-- const apiKeySettingValue = apiKeySetting?.value;-- useEffect(() => {- if (apiKeySettingValue) {- setLocalApiKey(null);- }- }, [apiKeySettingValue]);-- const handleApiKeyChange = (event: ChangeEvent<HTMLInputElement>) => {- setLocalApiKey(event.target.value);- };-- const providerDetails = getProviderOptions(true)[selectedProvider];-- return (- <>- <TextInput- label={t`API key`}- type="password"- description={- <ExternalLink href={providerDetails.apiKey.addKeyUrl}>- {c("{0} is the name of an AI provider")- .t`Get or manage keys in ${providerDetails.label}`}- </ExternalLink>- }- placeholder={providerDetails.apiKey.placeholder}- value={localApiKey ?? String(apiKeySettingValue ?? "")}- error={credentialsError}- onChange={handleApiKeyChange}- disabled={isMutating || isEnvSetting || !!apiKeyEnvSettingName}- w="100%"- />-- {apiKeyEnvSettingName ? (- <SetByEnvVar varName={apiKeyEnvSettingName} />- ) : null}-- {!needsApiKey && !credentialsError && (- <ProviderModelPicker- provider={selectedProvider}- connectedModel={connectedModel}- models={models}- isLoading={modelsQuery.isLoading && models.length === 0}- loadError={modelsQuery.error}- disabled={isEnvSetting || isMutating}- />- )}-- {updateMetabotSettingsResult.error && (- <Text size="sm" c="feedback-negative">- {getErrorMessage(- updateMetabotSettingsResult.error,- t`Unable to save provider settings.`,- )}- </Text>- )}- </>- );-};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsxdeleted file mode 100644index 388e979a2fa..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/AzureProviderFields.tsx+++ /dev/null@@ -1,175 +0,0 @@-import { type FormikHelpers, useFormikContext } from "formik";-import { useMemo } from "react";-import { c, t } from "ttag";--import { ExternalLink } from "metabase/common/components/ExternalLink";-import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";-import {- FormErrorMessage,- FormProvider,- FormSelect,- FormTextInput,-} from "metabase/forms";-import { useAdminSettings } from "metabase/settings";--import { useUpdateMetabotSettingsMutation } from "../../api";--import { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";-import {- AZURE_MODEL_FAMILIES,- getProviderOptions,- parseAzureModel,-} from "./utils";--const AZURE_SETTING_KEYS = [- "llm-azure-api-key",- "llm-azure-api-base-url",-] as const;--type AzureCredentialValues = {- family: string;- apiKey: string;- baseUrl: string;- deployment: string;-};--export const AzureProviderFields = ({- connectedModel,- isCurrentConfigured,- isEnvSetting,-}: {- connectedModel: string | undefined;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const [updateMetabotSettings] = useUpdateMetabotSettingsMutation();- const { details } = useAdminSettings(AZURE_SETTING_KEYS);-- const initialValues = useMemo<AzureCredentialValues>(() => {- const { family, deployment } = parseAzureModel(- isCurrentConfigured ? connectedModel : undefined,- );-- return {- family: family ?? "",- apiKey: String(details["llm-azure-api-key"]?.value ?? ""),- baseUrl: String(details["llm-azure-api-base-url"]?.value ?? ""),- deployment: deployment ?? "",- };- }, [connectedModel, details, isCurrentConfigured]);-- const handleSubmit = async (- values: AzureCredentialValues,- { resetForm }: FormikHelpers<AzureCredentialValues>,- ) => {- const changedValueOrNull = (field: "apiKey" | "baseUrl") =>- values[field] !== initialValues[field] ? values[field] || null : null;-- await updateMetabotSettings({- provider: "azure",- model: `${values.family}/${values.deployment}`,- credentials: {- "api-key": changedValueOrNull("apiKey"),- "base-url": changedValueOrNull("baseUrl"),- },- }).unwrap();-- resetForm({ values });- };-- return (- <FormProvider- initialValues={initialValues}- onSubmit={handleSubmit}- enableReinitialize- >- <AzureCredentialFields- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- </FormProvider>- );-};--const AzureCredentialFields = ({- isCurrentConfigured,- isEnvSetting,-}: {- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const { dirty, submitForm, values } =- useFormikContext<AzureCredentialValues>();-- const { details } = useAdminSettings(AZURE_SETTING_KEYS);- const apiKeySetting = details["llm-azure-api-key"];- const baseUrlSetting = details["llm-azure-api-base-url"];-- const apiKeyEnvName = apiKeySetting?.is_env_setting- ? apiKeySetting.env_name- : undefined;- const baseUrlEnvName = baseUrlSetting?.is_env_setting- ? baseUrlSetting.env_name- : undefined;-- const isComplete =- !!values.family &&- !!values.apiKey.trim() &&- !!values.baseUrl.trim() &&- !!values.deployment.trim();- const connectHandler =- isComplete && (!isCurrentConfigured || dirty) ? submitForm : null;- const { isMutating } = useAIProviderConfigurationContext(connectHandler);-- const providerDetails = getProviderOptions(true).azure;-- return (- <>- <FormSelect- name="family"- label={t`Model provider`}- description={t`Whether your deployment serves an Anthropic or an OpenAI model.`}- placeholder={t`Select a model provider`}- data={[...AZURE_MODEL_FAMILIES]}- disabled={isMutating || isEnvSetting}- />-- <FormTextInput- name="baseUrl"- label={t`Base URL`}- description={t`The full URL of your Azure resource's model-provider surface, including the provider segment.`}- placeholder="https://<resource>.services.ai.azure.com/openai"- disabled={isMutating || isEnvSetting || !!baseUrlEnvName}- w="100%"- />- {baseUrlEnvName && <SetByEnvVar varName={baseUrlEnvName} />}-- <FormTextInput- name="apiKey"- label={t`API key`}- type="password"- description={- <ExternalLink href={providerDetails.apiKey.addKeyUrl}>- {c("{0} is the name of an AI provider")- .t`Get or manage keys in ${providerDetails.label}`}- </ExternalLink>- }- placeholder={providerDetails.apiKey.placeholder}- disabled={isMutating || isEnvSetting || !!apiKeyEnvName}- w="100%"- />- {apiKeyEnvName && <SetByEnvVar varName={apiKeyEnvName} />}-- <FormTextInput- name="deployment"- label={t`Deployment name`}- description={t`The name of the model deployment on your Azure resource. We recommend naming deployments after the model they serve.`}- placeholder={t`Enter your Azure deployment name`}- disabled={isMutating || isEnvSetting}- w="100%"- />-- <FormErrorMessage />- </>- );-};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/BedrockProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/BedrockProviderFields.tsxdeleted file mode 100644index 3e50946f795..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/BedrockProviderFields.tsx+++ /dev/null@@ -1,215 +0,0 @@-import { type FormikHelpers, useFormikContext } from "formik";-import { useMemo } from "react";-import { t } from "ttag";--import { ExternalLink } from "metabase/common/components/ExternalLink";-import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";-import { FormErrorMessage, FormProvider, FormTextInput } from "metabase/forms";-import { useAdminSettings } from "metabase/settings";-import { Text } from "metabase/ui";-import type { BedrockCredentials } from "metabase-types/api";--import { useUpdateMetabotSettingsMutation } from "../../api";--import { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";-import {- ProviderModelPicker,- useProviderModelsQuery,-} from "./ProviderModelPicker";-import { getProviderOptions, hasConfiguredSettingValue } from "./utils";--const BEDROCK_SETTING_KEYS = [- "llm-bedrock-access-key-id",- "llm-bedrock-secret-access-key",- "llm-bedrock-region",- "llm-bedrock-session-token",-] as const;--type BedrockCredentialValues = {- accessKeyId: string;- secretAccessKey: string;- region: string;- sessionToken: string;-};--export const BedrockProviderFields = ({- connectedModel,- isCurrentConfigured,- isEnvSetting,-}: {- connectedModel: string | undefined;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const [updateMetabotSettings] = useUpdateMetabotSettingsMutation();- const { details } = useAdminSettings(BEDROCK_SETTING_KEYS);-- const initialValues = useMemo<BedrockCredentialValues>(- () => ({- accessKeyId: String(details["llm-bedrock-access-key-id"]?.value ?? ""),- secretAccessKey: String(- details["llm-bedrock-secret-access-key"]?.value ?? "",- ),- region: String(details["llm-bedrock-region"]?.value ?? ""),- sessionToken: String(details["llm-bedrock-session-token"]?.value ?? ""),- }),- [details],- );-- const handleSubmit = async (- values: BedrockCredentialValues,- { resetForm }: FormikHelpers<BedrockCredentialValues>,- ) => {- // Per-field presence contract: an unchanged field is omitted so the backend keeps the saved- // value, while a field the user blanked is sent as an explicit null to clear the saved value.- const credentials: BedrockCredentials = {};- const setIfChanged = (- apiField: keyof BedrockCredentials,- field: keyof BedrockCredentialValues,- ) => {- if (values[field] !== initialValues[field]) {- credentials[apiField] = values[field] || null;- }- };-- setIfChanged("access-key-id", "accessKeyId");- setIfChanged("secret-access-key", "secretAccessKey");- setIfChanged("region", "region");- setIfChanged("session-token", "sessionToken");-- await updateMetabotSettings({- provider: "bedrock",- credentials,- }).unwrap();-- resetForm({ values });- };-- return (- <FormProvider- initialValues={initialValues}- onSubmit={handleSubmit}- enableReinitialize- >- <BedrockCredentialFields- connectedModel={connectedModel}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- </FormProvider>- );-};--const BedrockCredentialFields = ({- connectedModel,- isCurrentConfigured,- isEnvSetting,-}: {- connectedModel: string | undefined;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const { dirty, submitForm } = useFormikContext<BedrockCredentialValues>();- const connectHandler = !isCurrentConfigured || dirty ? submitForm : null;- const { isMutating } = useAIProviderConfigurationContext(connectHandler);-- const { details } = useAdminSettings(BEDROCK_SETTING_KEYS);- const accessKeyIdSetting = details["llm-bedrock-access-key-id"];- const secretAccessKeySetting = details["llm-bedrock-secret-access-key"];- const regionSetting = details["llm-bedrock-region"];- const sessionTokenSetting = details["llm-bedrock-session-token"];-- const accessKeyIdEnvName = accessKeyIdSetting?.is_env_setting- ? accessKeyIdSetting.env_name- : undefined;- const secretAccessKeyEnvName = secretAccessKeySetting?.is_env_setting- ? secretAccessKeySetting.env_name- : undefined;- const regionEnvName = regionSetting?.is_env_setting- ? regionSetting.env_name- : undefined;- const sessionTokenEnvName = sessionTokenSetting?.is_env_setting- ? sessionTokenSetting.env_name- : undefined;-- const needsCredentials =- !hasConfiguredSettingValue(accessKeyIdSetting) ||- !hasConfiguredSettingValue(secretAccessKeySetting);-- const { modelsQuery, credentialsError: savedCredentialsError } =- useProviderModelsQuery("bedrock", { skip: needsCredentials });- const credentialsError = dirty ? undefined : savedCredentialsError;-- const bedrockOption = getProviderOptions(true).bedrock;-- return (- <>- <FormTextInput- name="accessKeyId"- label={t`Access key ID`}- type="password"- description={- <ExternalLink href={bedrockOption.apiKey.addKeyUrl}>- {t`Get or manage access keys in the AWS console`}- </ExternalLink>- }- placeholder={bedrockOption.apiKey.placeholder}- disabled={isMutating || isEnvSetting || !!accessKeyIdEnvName}- w="100%"- />- {accessKeyIdEnvName && <SetByEnvVar varName={accessKeyIdEnvName} />}- {credentialsError && (- <Text size="sm" c="feedback-negative" role="alert">- {credentialsError}- </Text>- )}-- <FormTextInput- name="secretAccessKey"- label={t`Secret access key`}- type="password"- description={t`Paired with the access key ID. AWS shows it only when the key is created.`}- placeholder={t`Enter your AWS secret access key`}- disabled={isMutating || isEnvSetting || !!secretAccessKeyEnvName}- w="100%"- />- {secretAccessKeyEnvName && (- <SetByEnvVar varName={secretAccessKeyEnvName} />- )}-- <FormTextInput- name="region"- label={t`Region`}- description={t`The AWS region to use for Bedrock.`}- placeholder="us-east-1"- disabled={isMutating || isEnvSetting || !!regionEnvName}- w="100%"- />- {regionEnvName && <SetByEnvVar varName={regionEnvName} />}-- <FormTextInput- name="sessionToken"- label={t`Session token`}- type="password"- description={t`Optional. Only needed for temporary AWS credentials.`}- placeholder={t`Enter your AWS session token`}- disabled={isMutating || isEnvSetting || !!sessionTokenEnvName}- w="100%"- />- {sessionTokenEnvName && <SetByEnvVar varName={sessionTokenEnvName} />}-- {!needsCredentials && !credentialsError && (- <ProviderModelPicker- provider="bedrock"- connectedModel={connectedModel}- models={modelsQuery.currentData?.models ?? []}- isLoading={modelsQuery.isLoading}- loadError={modelsQuery.error}- disabled={isEnvSetting || isMutating}- />- )}-- <FormErrorMessage />- </>- );-};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/GoogleProviderFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/GoogleProviderFields.tsxdeleted file mode 100644index dba9e366f65..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/GoogleProviderFields.tsx+++ /dev/null@@ -1,376 +0,0 @@-import { type FormikHelpers, useFormikContext } from "formik";-import { useMemo, useState } from "react";-import { jt, t } from "ttag";--import { ExternalLink } from "metabase/common/components/ExternalLink";-import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";-import {- FormErrorMessage,- FormFileInput,- FormProvider,- FormTextInput,-} from "metabase/forms";-import { useAdminSettings } from "metabase/settings";-import { Alert, Box, Code, Icon, SegmentedControl, Text } from "metabase/ui";-import type {- GoogleCredentials,- SettingDefinitionMap,-} from "metabase-types/api";--import { useUpdateMetabotSettingsMutation } from "../../api";--import { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";-import { hasConfiguredSettingValue } from "./utils";--const GOOGLE_SETTING_KEYS = [- "llm-google-service-account-key",- "llm-google-oauth-access-token",- "llm-google-project-id",- "llm-google-location",-] as const;--const GOOGLE_MODEL_LOCATIONS_URL =- "https://docs.cloud.google.com/gemini-enterprise-agent-platform/resources/locations#google-models";--// The models Metabot is known to work well with. Which of them a project can actually reach depends-// on its location, thus the input still accepts any model ID and only warns about the rest.-// The first entry in this list is used as the placeholder for the model form input below.-const RECOMMENDED_MODELS = [- "gemini-3.5-flash",- "gemini-3.6-flash",- "gemini-3.7-flash",-] as const;--type GoogleSettingDetails = SettingDefinitionMap<- (typeof GOOGLE_SETTING_KEYS)[number]->;--type GoogleCredentialValues = {- serviceAccountFile: File | null;- oauthToken: string;- projectId: string;- location: string;- model: string;-};--type GoogleAuthType = "service-account" | "oauth-token";--const isGoogleAuthType = (value: string): value is GoogleAuthType =>- value === "service-account" || value === "oauth-token";--// The setting keeps publisher-qualified model IDs, e.g. `google/gemini-3.5-flash`. The input shows-// and accepts the bare Gemini ID. Thus we remove the default `google` publisher for the display and-// add it again on submit.-const stripGooglePublisher = (model: string | undefined) =>- model?.replace(/^google\//, "") ?? "";--const qualifyGoogleModel = (model: string) =>- model.includes("/") ? model : `google/${model}`;--const isRecommendedModel = (model: string) =>- RECOMMENDED_MODELS.some(- (recommended) =>- recommended === stripGooglePublisher(model.trim().toLowerCase()),- );--// Advisory only — a model outside the list still connects, it just may not work well with Metabot.-const getModelWarning = (model: string): string | null => {- if (!model.trim() || isRecommendedModel(model)) {- return null;- }- const recommendedModels = RECOMMENDED_MODELS.join(", ");- return t`Metabot works best with these models: ${recommendedModels}. Other models may not work as expected.`;-};--const readFileText = (file: File) =>- new Promise<string>((resolve, reject) => {- const reader = new FileReader();- reader.onload = () => resolve(String(reader.result));- reader.onerror = () => reject(new Error(t`Could not read the file`));- reader.readAsText(file);- });--export const GoogleProviderFields = ({- connectedModel,- isCurrentConfigured,- isEnvSetting,-}: {- connectedModel: string | undefined;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const [updateMetabotSettings] = useUpdateMetabotSettingsMutation();- const { details } = useAdminSettings(GOOGLE_SETTING_KEYS);-- const initialValues = useMemo<GoogleCredentialValues>(- () => ({- // The saved key JSON never goes back into the form. The file picker starts empty, and an- // empty picker means "keep the saved key". See the placeholder of the picker.- serviceAccountFile: null,- oauthToken: String(details["llm-google-oauth-access-token"]?.value ?? ""),- projectId: String(details["llm-google-project-id"]?.value ?? ""),- location: String(details["llm-google-location"]?.value ?? ""),- model: stripGooglePublisher(- isCurrentConfigured ? connectedModel : undefined,- ),- }),- [connectedModel, details, isCurrentConfigured],- );-- const handleSubmit = async (- values: GoogleCredentialValues,- { resetForm }: FormikHelpers<GoogleCredentialValues>,- ) => {- // Send only the fields that changed. The backend keeps the saved value for a field that is- // absent. A field that the user made blank goes as null, which clears the saved value.- const credentials: GoogleCredentials = {};- const setIfChanged = (- apiField: keyof GoogleCredentials,- field: "oauthToken" | "projectId" | "location",- ) => {- if (values[field] !== initialValues[field]) {- credentials[apiField] = values[field] || null;- }- };-- setIfChanged("oauth-access-token", "oauthToken");- setIfChanged("project-id", "projectId");- setIfChanged("location", "location");-- if (values.serviceAccountFile) {- credentials["service-account-key"] = await readFileText(- values.serviceAccountFile,- );- }-- // A connection with one credential type clears the other. An env-backed credential is left alone: the env var- // supplies it on every read, so clearing it would do nothing.- const clearOtherCredential = (- apiField: "service-account-key" | "oauth-access-token",- settingKey:- | "llm-google-service-account-key"- | "llm-google-oauth-access-token",- ) => {- const setting = details[settingKey];- const isClearable =- hasConfiguredSettingValue(setting) && !setting?.is_env_setting;- if (isClearable && !(apiField in credentials)) {- credentials[apiField] = null;- }- };-- if (credentials["service-account-key"]) {- clearOtherCredential(- "oauth-access-token",- "llm-google-oauth-access-token",- );- } else if (credentials["oauth-access-token"]) {- clearOtherCredential(- "service-account-key",- "llm-google-service-account-key",- );- }-- await updateMetabotSettings({- provider: "google",- model: qualifyGoogleModel(values.model.trim()),- credentials,- }).unwrap();-- // Remove the key material after the save. The picker shows its placeholder again.- resetForm({ values: { ...values, serviceAccountFile: null } });- };-- return (- <FormProvider- initialValues={initialValues}- onSubmit={handleSubmit}- enableReinitialize- >- <GoogleCredentialFields- details={details}- isCurrentConfigured={isCurrentConfigured}- isEnvSetting={isEnvSetting}- />- </FormProvider>- );-};--const GoogleCredentialFields = ({- details,- isCurrentConfigured,- isEnvSetting,-}: {- details: GoogleSettingDetails;- isCurrentConfigured: boolean;- isEnvSetting: boolean;-}) => {- const { dirty, submitForm, values, setFieldValue, initialValues } =- useFormikContext<GoogleCredentialValues>();-- const serviceAccountSetting = details["llm-google-service-account-key"];- const oauthTokenSetting = details["llm-google-oauth-access-token"];- const projectIdSetting = details["llm-google-project-id"];- const locationSetting = details["llm-google-location"];-- const hasSavedServiceAccount = hasConfiguredSettingValue(- serviceAccountSetting,- );-- // Ensure auth toggle selects correct option based on current settings.- const [authTypeOverride, setAuthTypeOverride] =- useState<GoogleAuthType | null>(null);- const savedAuthType: GoogleAuthType =- !hasSavedServiceAccount && hasConfiguredSettingValue(oauthTokenSetting)- ? "oauth-token"- : "service-account";- const authType = authTypeOverride ?? savedAuthType;-- const hasCompleteCredential =- authType === "service-account"- ? !!values.serviceAccountFile || hasSavedServiceAccount- : !!values.oauthToken.trim() && !!values.projectId.trim();- // The file picker never shows a saved key, thus an empty picker is still complete if a key is saved.- const isComplete = hasCompleteCredential && !!values.model.trim();- const modelWarning = getModelWarning(values.model);- const connectHandler =- isComplete && (!isCurrentConfigured || dirty) ? submitForm : null;- const { isMutating } = useAIProviderConfigurationContext(connectHandler);-- const handleAuthTypeChange = (value: string) => {- if (!isGoogleAuthType(value)) {- return;- }- setAuthTypeOverride(value);- // Only the credential that is visible can go in the request. Reset what the admin entered for- // the other auth type.- if (value === "service-account") {- setFieldValue("oauthToken", initialValues.oauthToken);- } else {- setFieldValue("serviceAccountFile", null);- }- };-- const serviceAccountEnvName = serviceAccountSetting?.is_env_setting- ? serviceAccountSetting.env_name- : undefined;- const oauthTokenEnvName = oauthTokenSetting?.is_env_setting- ? oauthTokenSetting.env_name- : undefined;- const projectIdEnvName = projectIdSetting?.is_env_setting- ? projectIdSetting.env_name- : undefined;- const locationEnvName = locationSetting?.is_env_setting- ? locationSetting.env_name- : undefined;-- return (- <>- <FormTextInput- name="projectId"- label={t`Project ID`}- description={t`The Google Cloud project to use. Optional if the service account key provides it.`}- placeholder={t`Enter your Google Cloud project ID`}- disabled={isMutating || isEnvSetting || !!projectIdEnvName}- w="100%"- />- {projectIdEnvName && <SetByEnvVar varName={projectIdEnvName} />}-- <FormTextInput- name="location"- label={t`Location`}- description={t`Optional. Defaults to global.`}- placeholder="global"- disabled={isMutating || isEnvSetting || !!locationEnvName}- w="100%"- />- {locationEnvName && <SetByEnvVar varName={locationEnvName} />}-- <FormTextInput- name="model"- label={t`Model`}- description={jt`Model availability varies by location. ${(- <ExternalLink key="model-locations" href={GOOGLE_MODEL_LOCATIONS_URL}>- {t`See which Gemini models are available in each location.`}- </ExternalLink>- )}`}- placeholder={RECOMMENDED_MODELS[0]}- disabled={isMutating || isEnvSetting}- w="100%"- />- {modelWarning && (- <Alert size="compact" color="warning" icon={<Icon name="warning" />}>- {modelWarning}- </Alert>- )}-- <Box>- <Text fw="bold">{t`Authentication method`}</Text>- <Text size="sm" c="text-secondary">- {t`Authenticate with a service account key or an OAuth access token.`}- </Text>- </Box>-- <SegmentedControl- value={authType}- onChange={handleAuthTypeChange}- disabled={isMutating || isEnvSetting}- data={[- { value: "service-account", label: t`Service account key` },- { value: "oauth-token", label: t`OAuth token` },- ]}- aria-label={t`Authentication method`}- fullWidth- />-- {authType === "service-account" && (- <>- <FormFileInput- name="serviceAccountFile"- label={t`Service account key file`}- description={- hasSavedServiceAccount ? (- <>- <strong>{t`A service account key is saved.`}</strong>{" "}- {t`Choose a new file to replace it.`}- </>- ) : (- t`Upload a service account key file to authenticate with.`- )- }- placeholder={t`Click to select a file`}- leftSection={<Icon name="upload" />}- accept="application/json"- clearable- clearButtonProps={{ "aria-label": t`Clear the selected file` }}- disabled={isMutating || isEnvSetting || !!serviceAccountEnvName}- fileInputProps={{ "aria-label": t`Service account key file input` }}- w="100%"- />- {serviceAccountEnvName && (- <SetByEnvVar varName={serviceAccountEnvName} />- )}- </>- )}-- {authType === "oauth-token" && (- <>- <FormTextInput- name="oauthToken"- label={t`OAuth token`}- type="password"- description={jt`Paste a short-lived OAuth access token, e.g. the output of ${(- <Code key="gcloud">{"gcloud auth print-access-token"}</Code>- )}. Useful for testing.`}- placeholder="ya29..."- disabled={isMutating || isEnvSetting || !!oauthTokenEnvName}- w="100%"- />- {oauthTokenEnvName && <SetByEnvVar varName={oauthTokenEnvName} />}- </>- )}-- <FormErrorMessage />- </>- );-};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.tsxnew file mode 100644index 00000000000..28cc4855c9f--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.tsx@@ -0,0 +1,52 @@+import { t } from "ttag";++import { getErrorMessage } from "metabase/api/utils";+import { SetByEnvVar } from "metabase/common/components/SetByEnvVar";+import { useLlmConnectionModels } from "metabase/metabot/hooks";+import { useAdminSetting } from "metabase/settings";+import { DefaultSelectItem, Select, Stack } from "metabase/ui";++export function LlmModelPicker() {+ const {+ value: modelRef,+ updateSetting,+ settingDetails,+ } = useAdminSetting("llm-metabot-provider");+ const { modelOptions, modelNameByRef, isLoading, error } =+ useLlmConnectionModels();++ const isEnvSetting = !!settingDetails?.is_env_setting;+ const envVarName = isEnvSetting ? settingDetails?.env_name : undefined;++ const handleChange = async (value: string | null) => {+ if (!value) {+ return;+ }+ await updateSetting({ key: "llm-metabot-provider", value });+ };++ return (+ <Stack gap="sm">+ <Select+ label={t`Model`}+ description={t`Metabot uses this model by default. Models are listed per connected provider.`}+ placeholder={isLoading ? t`Loading models...` : t`Select a model`}+ error={error ? getErrorMessage(error, t`Unable to load models.`) : null}+ data={modelOptions}+ value={modelRef ?? null}+ onChange={handleChange}+ disabled={isEnvSetting || isLoading}+ searchable+ nothingFoundMessage={t`No models found`}+ renderOption={(item) => (+ <DefaultSelectItem+ {...item.option}+ selected={item.checked}+ label={modelNameByRef[item.option.value] ?? item.option.label}+ />+ )}+ />+ {envVarName && <SetByEnvVar varName={envVarName} />}+ </Stack>+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.unit.spec.tsxnew file mode 100644index 00000000000..357b1436505--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/LlmModelPicker.unit.spec.tsx@@ -0,0 +1,79 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupLlmModelsEndpoint,+ setupPropertiesEndpoints,+ setupSettingsEndpoints,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import {+ createMockLlmConnectionModels,+ createMockLlmModel,+ createMockSettings,+} from "metabase-types/api/mocks";++import { LlmModelPicker } from "./LlmModelPicker";++const setup = (putResponse: number | object) => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();+ setupPropertiesEndpoints(createMockSettings());+ setupSettingsEndpoints([]);+ setupLlmModelsEndpoint([+ createMockLlmConnectionModels({+ key: "anthropic",+ name: "Anthropic",+ models: [+ createMockLlmModel({ id: "claude-x", display_name: "Claude X" }),+ ],+ }),+ ]);+ fetchMock.put("path:/api/setting/llm-metabot-provider", putResponse);+ return renderWithProviders(<LlmModelPicker />);+};++const pickModel = async () => {+ // the picker is disabled until the model list lands, and a click on a disabled input opens nothing+ const input = await screen.findByRole("textbox");+ await waitFor(() => expect(input).toBeEnabled());+ await userEvent.click(input);+ await userEvent.click(+ await screen.findByRole("option", { name: /Claude X/ }),+ );+};++describe("LlmModelPicker save feedback", () => {+ it("surfaces the server's message when saving fails", async () => {+ const { store } = setup({+ status: 400,+ body: { message: 'Invalid Azure model "nope".' },+ });++ await pickModel();++ await waitFor(() => {+ expect(store.getState().undo).toContainEqual(+ expect.objectContaining({+ message: 'Invalid Azure model "nope".',+ toastColor: "feedback-negative",+ }),+ );+ });+ });++ it("confirms the save when it succeeds", async () => {+ const { store } = setup(200);++ await pickModel();++ await waitFor(() => {+ expect(store.getState().undo).toContainEqual(+ expect.objectContaining({ message: "Changes saved" }),+ );+ });+ expect(+ fetchMock.callHistory.calls("path:/api/setting/llm-metabot-provider"),+ ).toHaveLength(1);+ });+});diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsxnew file mode 100644index 00000000000..5f90bc1ac1c--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsx@@ -0,0 +1,201 @@+import { useState } from "react";+import { t } from "ttag";++import { ExternalLink } from "metabase/common/components/ExternalLink";+import {+ FileInput,+ Icon,+ Input,+ SegmentedControl,+ Select,+ Stack,+ Text,+ TextInput,+} from "metabase/ui";+import type { LlmProviderConfig, LlmProviderField } from "metabase-types/api";++import { isVisibleField } from "./visible-fields";++export function ProviderConfigFields({+ fields,+ values,+ onChange,+ disabled,+ disabledFields = [],+ autoFocusFirstField,+}: {+ fields: LlmProviderField[];+ values: LlmProviderConfig;+ onChange: (key: string, value: string) => void;+ disabled?: boolean;+ // fields the environment owns: shadowed by an MB_LLM_* variable, so editing them here would do nothing+ disabledFields?: string[];+ autoFocusFirstField?: boolean;+}) {+ const visibleFields = fields.filter((field) =>+ isVisibleField(field, fields, values),+ );++ if (visibleFields.length === 0) {+ return null;+ }++ return (+ <Stack gap="md">+ {visibleFields.map((field, index) => (+ <ProviderConfigField+ key={field.key}+ field={field}+ value={values[field.key] ?? ""}+ onChange={(value) => onChange(field.key, value)}+ disabled={disabled || disabledFields.includes(field.key)}+ autoFocus={autoFocusFirstField && index === 0}+ />+ ))}+ </Stack>+ );+}++function ProviderConfigField({+ field,+ value,+ onChange,+ disabled,+ autoFocus,+}: {+ field: LlmProviderField;+ value: string;+ onChange: (value: string) => void;+ disabled?: boolean;+ autoFocus?: boolean;+}) {+ const focusProps = autoFocus ? { autoFocus, "data-autofocus": true } : {};+ const externalDocsUrl = field.docs_url;+ const description = externalDocsUrl ? (+ <ExternalLink href={externalDocsUrl}>+ {t`Where do I find this?`}+ </ExternalLink>+ ) : (+ field.help && <Text size="sm">{field.help}</Text>+ );++ if (field.type === "segmented") {+ return (+ <Input.Wrapper label={field.label} description={description}>+ <SegmentedControl+ mt="xs"+ data={field.options ?? []}+ value={value || field.default || ""}+ onChange={onChange}+ disabled={disabled}+ aria-label={field.label}+ fullWidth+ />+ </Input.Wrapper>+ );+ }++ if (field.type === "file") {+ return (+ <ProviderConfigFileField+ field={field}+ value={value}+ onChange={onChange}+ disabled={disabled}+ />+ );+ }++ if (field.type === "select") {+ return (+ <Select+ label={field.label}+ description={description}+ data={withCurrentValue(field.options ?? [], value)}+ value={value || field.default || null}+ onChange={(next) => onChange(next ?? "")}+ disabled={disabled}+ searchable+ {...focusProps}+ />+ );+ }++ return (+ <TextInput+ label={field.label}+ description={description}+ type={field.type === "password" ? "password" : "text"}+ placeholder={field.placeholder ?? field.default ?? undefined}+ value={value}+ onChange={(event) => onChange(event.currentTarget.value)}+ disabled={disabled}+ required={field.required}+ {...focusProps}+ />+ );+}++// A connection saved with a model the registry no longer offers still has to show what it runs on.+function withCurrentValue(+ options: { value: string; label: string }[],+ value: string,+) {+ return value && !options.some((option) => option.value === value)+ ? [...options, { value, label: value }]+ : options;+}++function ProviderConfigFileField({+ field,+ value,+ onChange,+ disabled,+}: {+ field: LlmProviderField;+ value: string;+ onChange: (value: string) => void;+ disabled?: boolean;+}) {+ const [file, setFile] = useState<File | null>(null);+ const [error, setError] = useState<string | undefined>();+ // The stored file is a secret the API only ever echoes back masked, so the picker starts empty+ // and an empty picker means the saved one is kept.+ const hasSavedValue = value !== "" && file == null;++ const handleChange = (nextFile: File | null) => {+ setFile(nextFile);+ setError(undefined);+ if (!nextFile) {+ onChange("");+ return;+ }+ const reader = new FileReader();+ reader.onload = () => onChange(String(reader.result));+ reader.onerror = () => setError(t`Could not read the file.`);+ reader.readAsText(nextFile);+ };++ return (+ <FileInput+ label={field.label}+ description={+ hasSavedValue+ ? t`A file is saved. Choose a new one to replace it.`+ : field.help+ }+ placeholder={field.placeholder ?? undefined}+ leftSection={<Icon name="upload" />}+ accept="application/json"+ value={file}+ onChange={handleChange}+ error={error}+ disabled={disabled}+ required={field.required}+ clearable+ clearButtonProps={{ "aria-label": t`Clear the selected file` }}+ // the visible control is a button; the input it drives needs a name of its own+ fileInputProps={{ "aria-label": t`${field.label} file input` }}+ />+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsxnew file mode 100644index 00000000000..1dcdc221396--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsx@@ -0,0 +1,364 @@+import { useDisclosure } from "@mantine/hooks";+import type { FormEvent, ReactNode } from "react";+import { useCallback, useEffect, useMemo, useState } from "react";+import { P, match } from "ts-pattern";+import { t } from "ttag";++import {+ useCreateLlmProviderMutation,+ useUpdateLlmProviderMutation,+} from "metabase/api";+import { getErrorMessage } from "metabase/api/utils";+import { PLUGIN_METABOT } from "metabase/plugins";+import { useSetting } from "metabase/settings";+import {+ Button,+ Collapse,+ Flex,+ Group,+ Icon,+ Select,+ Stack,+ Text,+ TextInput,+} from "metabase/ui";+import type {+ LlmProviderConfig,+ LlmProviderConnection,+ LlmProviderType,+} from "metabase-types/api";++import { ProviderConfigFields } from "./ProviderConfigFields";+import { ProviderTypeIcon } from "./ProviderTypeIcon";+import { ProviderTypePicker } from "./ProviderTypePicker";+import { findProviderTypeForApiKey } from "./api-key";+import { getHiddenFieldKeys, isVisibleField } from "./visible-fields";++export function ProviderConnectionForm({+ providerTypes,+ connection,+ onSaved,+ onCancel,+}: {+ providerTypes: LlmProviderType[];+ connection?: LlmProviderConnection;+ onSaved: (connection?: LlmProviderConnection) => void;+ onCancel?: () => void;+}) {+ const isEditing = connection != null;+ const [typeName, setTypeName] = useState<string | undefined>(+ connection?.type,+ );+ const [name, setName] = useState(connection?.name ?? "");+ const [config, setConfig] = useState<LlmProviderConfig>(+ connection?.config ?? {},+ );+ // On edit, start the picker on the model the connection is actually serving Metabot, not the type default.+ const modelRef = useSetting("llm-metabot-provider");+ const [model, setModel] = useState<string | undefined>(() => {+ const type = providerTypes.find(+ (option) => option.type === connection?.type,+ );+ const [refKey, ...refModelParts] = (modelRef ?? "").split("/");+ const refModel = refModelParts.join("/");+ if (+ connection != null &&+ refKey === connection.key &&+ type?.models.some((typeModel) => typeModel.id === refModel)+ ) {+ return refModel;+ }+ return type?.default_model ?? undefined;+ });+ const [error, setError] = useState<string | undefined>();++ const [createProvider, createResult] = useCreateLlmProviderMutation();+ const [updateProvider, updateResult] = useUpdateLlmProviderMutation();+ const isSaving = createResult.isLoading || updateResult.isLoading;++ const providerType = useMemo(+ () => providerTypes.find((option) => option.type === typeName),+ [providerTypes, typeName],+ );++ const [primaryFields, advancedFields] = useMemo(() => {+ const fields = providerType?.fields ?? [];+ return [+ fields.filter((field) => !field.advanced),+ fields.filter((field) => field.advanced),+ ];+ }, [providerType]);++ const [isAdvancedOpen, { toggle: toggleAdvanced }] = useDisclosure(+ hasStoredAdvancedValues(providerType, connection),+ );++ const selectProviderType = useCallback(+ (selected: LlmProviderType, nextConfig: LlmProviderConfig = {}) => {+ setTypeName(selected.type);+ setName(selected.label);+ setConfig(nextConfig);+ setModel(selected.default_model ?? undefined);+ setError(undefined);+ },+ [],+ );++ const handleTypeChange = (nextType: string) => {+ const selected = providerTypes.find((option) => option.type === nextType);+ if (selected) {+ selectProviderType(selected);+ }+ };++ const isPickingType = !isEditing && providerType == null;++ useEffect(() => {+ if (!isPickingType) {+ return;+ }+ const handlePaste = (event: ClipboardEvent) => {+ const pasted = event.clipboardData?.getData("text").trim();+ const match = pasted+ ? findProviderTypeForApiKey(providerTypes, pasted)+ : undefined;+ if (!pasted || !match) {+ return;+ }+ event.preventDefault();+ selectProviderType(match.providerType, { [match.fieldKey]: pasted });+ };+ document.addEventListener("paste", handlePaste);+ return () => document.removeEventListener("paste", handlePaste);+ }, [isPickingType, providerTypes, selectProviderType]);++ const handleBack = () => {+ setTypeName(undefined);+ setName("");+ setConfig({});+ setModel(undefined);+ setError(undefined);+ };++ // A required field the registry gives a default is already satisfied — the form shows that value pre-selected,+ // and the backend fills it in for a connection that never touched it. A type with alternative credential+ // groups (Google: a service account key, or an OAuth token with a project ID) additionally needs one group+ // filled in full; its fields are individually optional because either group will do.+ const hasValue = (key: string) => (config[key] ?? "").trim() !== "";+ const isComplete =+ providerType != null &&+ providerType.fields+ .filter(+ (field) =>+ field.required &&+ !field.default &&+ isVisibleField(field, providerType.fields, config),+ )+ .every((field) => hasValue(field.key)) &&+ (providerType.required_any.length === 0 ||+ providerType.required_any.some((group) => group.every(hasValue)));++ const handleSave = async () => {+ if (!providerType) {+ return;+ }+ setError(undefined);+ // A field the form hid is not part of the connection: clearing it is what makes switching+ // Google's authentication method drop the credential the other one replaced.+ const cleared = getHiddenFieldKeys(providerType.fields, config).filter(+ (key) => (config[key] ?? "") !== "",+ );+ const savedConfig = {+ ...config,+ ...Object.fromEntries(cleared.map((key) => [key, ""])),+ };+ try {+ const saved = isEditing+ ? await updateProvider({+ key: connection.key,+ name,+ config: savedConfig,+ model,+ }).unwrap()+ : await createProvider({+ type: providerType.type,+ name,+ config: savedConfig,+ model,+ }).unwrap();+ onSaved(saved);+ } catch (caught) {+ setError(getErrorMessage(caught, t`Unable to save this provider.`));+ }+ };++ const handleSubmit = (event: FormEvent<HTMLFormElement>) => {+ event.preventDefault();+ handleSave();+ };++ const MetabaseAIProviderSetup = PLUGIN_METABOT.MetabaseAIProviderSetup;++ return (+ <Stack gap="lg">+ {match({ isEditing, providerType })+ .with({ isEditing: false, providerType: P.nullish }, () => (+ <ProviderTypePicker+ providerTypes={providerTypes}+ onSelect={handleTypeChange}+ />+ ))+ .with(+ { providerType: { managed: true } },+ ({ providerType: selected }) => (+ <Stack gap="lg">+ {!isEditing && <SelectedProvider providerType={selected} />}+ <MetabaseAIProviderSetup+ isConnected={isEditing}+ onConnect={() => onSaved()}+ onCancel={isEditing ? undefined : handleBack}+ />+ </Stack>+ ),+ )+ .with({ providerType: P.nonNullable }, ({ providerType: selected }) => (+ <form onSubmit={handleSubmit}>+ <Stack gap="lg">+ {!isEditing && <SelectedProvider providerType={selected} />}+ <ProviderConfigFields+ fields={primaryFields}+ values={config}+ onChange={(key, value) =>+ setConfig((current) => ({ ...current, [key]: value }))+ }+ disabled={isSaving}+ disabledFields={connection?.env_fields}+ autoFocusFirstField+ />+ {selected.models.length > 0 && (+ <Select+ label={t`Model`}+ description={t`Connecting checks your credentials against this model, and Metabot starts on it.`}+ data={selected.models.map(({ id, display_name }) => ({+ value: id,+ label: display_name,+ }))}+ value={model ?? null}+ onChange={(next) => setModel(next ?? undefined)}+ disabled={isSaving}+ />+ )}+ <AdvancedSettings+ isOpened={isAdvancedOpen}+ onToggle={toggleAdvanced}+ >+ <TextInput+ label={t`Display name`}+ description={t`What this connection is called in the model picker.`}+ value={name}+ onChange={(event) => setName(event.currentTarget.value)}+ disabled={isSaving}+ />+ <ProviderConfigFields+ fields={advancedFields}+ values={config}+ onChange={(key, value) =>+ setConfig((current) => ({ ...current, [key]: value }))+ }+ disabled={isSaving}+ disabledFields={connection?.env_fields}+ />+ </AdvancedSettings>+ {error && <Text c="error">{error}</Text>}+ <Flex justify="end">+ <Group gap="sm">+ {match({ isEditing, onCancel })+ .with(+ { isEditing: true, onCancel: P.nonNullable },+ ({ onCancel }) => (+ <Button+ type="button"+ onClick={onCancel}+ disabled={isSaving}+ >+ {t`Cancel`}+ </Button>+ ),+ )+ .with({ isEditing: false }, () => (+ <Button+ type="button"+ onClick={handleBack}+ disabled={isSaving}+ >+ {t`Back`}+ </Button>+ ))+ .otherwise(() => null)}+ <Button+ type="submit"+ variant="filled"+ loading={isSaving}+ disabled={isSaving || !isComplete}+ >+ {isEditing ? t`Save` : t`Connect`}+ </Button>+ </Group>+ </Flex>+ </Stack>+ </form>+ ))+ .otherwise(() => null)}+ </Stack>+ );+}++function hasStoredAdvancedValues(+ providerType: LlmProviderType | undefined,+ connection: LlmProviderConnection | undefined,+) {+ return (providerType?.fields ?? []).some(+ (field) =>+ field.advanced && (connection?.config[field.key] ?? "").trim() !== "",+ );+}++function AdvancedSettings({+ isOpened,+ onToggle,+ children,+}: {+ isOpened: boolean;+ onToggle: () => void;+ children: ReactNode;+}) {+ return (+ <Stack gap="md">+ <Button+ type="button"+ variant="subtle"+ p={0}+ w="fit-content"+ aria-expanded={isOpened}+ onClick={onToggle}+ leftSection={+ <Icon name={isOpened ? "chevrondown" : "chevronright"} size={12} />+ }+ >+ {t`Advanced settings`}+ </Button>+ <Collapse in={isOpened}>+ <Stack gap="lg">{children}</Stack>+ </Collapse>+ </Stack>+ );+}++function SelectedProvider({ providerType }: { providerType: LlmProviderType }) {+ return (+ <Group gap="sm" wrap="nowrap">+ <ProviderTypeIcon type={providerType.type} />+ <Text fw="bold">{providerType.label}</Text>+ </Group>+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.unit.spec.tsxnew file mode 100644index 00000000000..24d3eae2248--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.unit.spec.tsx@@ -0,0 +1,378 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupCreateLlmProviderEndpoint,+ setupUpdateLlmProviderEndpoint,+} from "__support__/server-mocks/metabot";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import type { LlmProviderConnection } from "metabase-types/api";+import {+ createMockLlmProviderConnection,+ createMockLlmProviderField,+ createMockLlmProviderType,+} from "metabase-types/api/mocks";++import { ProviderConnectionForm } from "./ProviderConnectionForm";++const PROVIDER_TYPE = createMockLlmProviderType({+ type: "anthropic",+ label: "Anthropic",+ fields: [createMockLlmProviderField({ key: "api_key", label: "API key" })],+});++const setup = () => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();+ setupCreateLlmProviderEndpoint();+ const onSaved = jest.fn();++ renderWithProviders(+ <ProviderConnectionForm+ providerTypes={[PROVIDER_TYPE]}+ onSaved={onSaved}+ />,+ );++ return { onSaved };+};++const selectProviderAndFillKey = async (apiKey: string) => {+ await userEvent.click(screen.getByRole("button", { name: /Anthropic/ }));+ await userEvent.type(await screen.findByLabelText(/API key/), apiKey);+};++describe("ProviderConnectionForm", () => {+ it("focuses the first field after picking a provider", async () => {+ setup();++ await userEvent.click(screen.getByRole("button", { name: /Anthropic/ }));++ expect(await screen.findByLabelText(/API key/)).toHaveFocus();+ });++ it("connects when pressing enter in a field", async () => {+ const { onSaved } = setup();++ await selectProviderAndFillKey("sk-ant-123");+ await userEvent.keyboard("{Enter}");++ await waitFor(() => {+ expect(onSaved).toHaveBeenCalled();+ });+ expect(+ await fetchMock.callHistory+ .lastCall("path:/api/llm/providers", { method: "POST" })+ ?.request?.json(),+ ).toEqual({+ type: "anthropic",+ name: "Anthropic",+ config: { api_key: "sk-ant-123" },+ });+ });++ it("does not connect when pressing enter before the required field is filled", async () => {+ const { onSaved } = setup();++ await userEvent.click(screen.getByRole("button", { name: /Anthropic/ }));+ await userEvent.click(await screen.findByLabelText(/API key/));+ await userEvent.keyboard("{Enter}");++ expect(onSaved).not.toHaveBeenCalled();+ expect(+ fetchMock.callHistory.calls("path:/api/llm/providers", {+ method: "POST",+ }),+ ).toHaveLength(0);+ });++ it("does not connect when toggling advanced settings", async () => {+ const { onSaved } = setup();++ await selectProviderAndFillKey("sk-ant-123");+ await userEvent.click(+ screen.getByRole("button", { name: /Advanced settings/ }),+ );++ expect(onSaved).not.toHaveBeenCalled();+ expect(+ fetchMock.callHistory.calls("path:/api/llm/providers", {+ method: "POST",+ }),+ ).toHaveLength(0);+ });++ it("does not connect when going back to the provider picker", async () => {+ const { onSaved } = setup();++ await selectProviderAndFillKey("sk-ant-123");+ await userEvent.click(screen.getByRole("button", { name: "Back" }));++ expect(onSaved).not.toHaveBeenCalled();+ expect(+ fetchMock.callHistory.calls("path:/api/llm/providers", {+ method: "POST",+ }),+ ).toHaveLength(0);+ expect(screen.getByRole("button", { name: /Anthropic/ })).toBeEnabled();+ });+});++const GOOGLE_TYPE = createMockLlmProviderType({+ type: "google",+ label: "Google Gemini Enterprise",+ default_model: "google/gemini-3.5-flash",+ models: [+ { id: "google/gemini-3.5-flash", display_name: "gemini-3.5-flash" },+ { id: "google/gemini-3.6-flash", display_name: "gemini-3.6-flash" },+ ],+ required_any: [["service-account-key"], ["oauth-access-token", "project-id"]],+ fields: [+ createMockLlmProviderField({+ key: "project-id",+ label: "Project ID",+ type: "text",+ required: false,+ }),+ createMockLlmProviderField({+ key: "auth-method",+ label: "Authentication method",+ type: "segmented",+ required: true,+ options: [+ { value: "service-account-key", label: "Service account key" },+ { value: "oauth-token", label: "OAuth token" },+ ],+ default: "service-account-key",+ }),+ createMockLlmProviderField({+ key: "service-account-key",+ label: "Service account key file",+ type: "file",+ required: false,+ placeholder: "Click to select a file",+ show_when: { field: "auth-method", value: "service-account-key" },+ }),+ createMockLlmProviderField({+ key: "oauth-access-token",+ label: "OAuth access token",+ type: "password",+ required: false,+ show_when: { field: "auth-method", value: "oauth-token" },+ }),+ ],+});++const setupGoogle = (+ connection?: LlmProviderConnection,+ { modelRef }: { modelRef?: string } = {},+) => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();+ setupCreateLlmProviderEndpoint();+ setupUpdateLlmProviderEndpoint();+ const onSaved = jest.fn();++ renderWithProviders(+ <ProviderConnectionForm+ providerTypes={[GOOGLE_TYPE]}+ connection={connection}+ onSaved={onSaved}+ />,+ {+ storeInitialState: {+ settings: mockSettings({ "llm-metabot-provider": modelRef ?? null }),+ },+ },+ );++ return { onSaved };+};++const pickGoogle = async () => {+ await userEvent.click(+ screen.getByRole("button", { name: /Google Gemini Enterprise/ }),+ );+ await screen.findByLabelText("Authentication method");+};++describe("ProviderConnectionForm with fields behind a choice", () => {+ it("shows the credential the chosen authentication method needs", async () => {+ setupGoogle();++ await pickGoogle();++ expect(+ screen.getByLabelText("Service account key file"),+ ).toBeInTheDocument();+ expect(+ screen.queryByLabelText("OAuth access token"),+ ).not.toBeInTheDocument();++ await userEvent.click(screen.getByRole("radio", { name: "OAuth token" }));++ expect(+ await screen.findByLabelText("OAuth access token"),+ ).toBeInTheDocument();+ expect(+ screen.queryByLabelText("Service account key file"),+ ).not.toBeInTheDocument();+ });++ it("saves the contents of the uploaded key file", async () => {+ const { onSaved } = setupGoogle();+ const key = '{"type":"service_account"}';++ await pickGoogle();+ await userEvent.upload(+ screen.getByLabelText("Service account key file file input"),+ new File([key], "key.json", { type: "application/json" }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(() => {+ expect(onSaved).toHaveBeenCalled();+ });+ expect(+ await fetchMock.callHistory+ .lastCall("path:/api/llm/providers", { method: "POST" })+ ?.request?.json(),+ ).toEqual({+ type: "google",+ name: "Google Gemini Enterprise",+ // the authentication method is left to the default the registry already applies+ config: { "service-account-key": key },+ model: "google/gemini-3.5-flash",+ });+ });++ it("clears the credential the other authentication method left behind", async () => {+ const { onSaved } = setupGoogle(+ createMockLlmProviderConnection({+ key: "google",+ type: "google",+ name: "Google Gemini Enterprise",+ config: {+ "auth-method": "service-account-key",+ "service-account-key": "**********nt",+ },+ }),+ );++ await userEvent.click(screen.getByRole("radio", { name: "OAuth token" }));+ await userEvent.type(+ await screen.findByLabelText("OAuth access token"),+ "ya29.x",+ );+ await userEvent.click(screen.getByRole("button", { name: "Save" }));++ await waitFor(() => {+ expect(onSaved).toHaveBeenCalled();+ });+ expect(+ await fetchMock.callHistory+ .lastCall("express:/api/llm/providers/:key", { method: "PUT" })+ ?.request?.json(),+ ).toEqual({+ name: "Google Gemini Enterprise",+ config: {+ "auth-method": "oauth-token",+ "service-account-key": "",+ "oauth-access-token": "ya29.x",+ },+ model: "google/gemini-3.5-flash",+ });+ });++ it("keeps Connect disabled until one credential group is complete", async () => {+ setupGoogle();++ await pickGoogle();++ expect(screen.getByRole("button", { name: "Connect" })).toBeDisabled();++ await userEvent.click(screen.getByRole("radio", { name: "OAuth token" }));+ await userEvent.type(+ await screen.findByLabelText("OAuth access token"),+ "ya29.x",+ );++ expect(screen.getByRole("button", { name: "Connect" })).toBeDisabled();++ await userEvent.type(screen.getByLabelText("Project ID"), "my-project");++ expect(screen.getByRole("button", { name: "Connect" })).toBeEnabled();+ });++ it("validates the connection against the model picked in the form", async () => {+ const { onSaved } = setupGoogle();+ const key = '{"type":"service_account"}';++ await pickGoogle();+ await userEvent.click(screen.getByLabelText("Model"));+ await userEvent.click(+ await screen.findByRole("option", { name: "gemini-3.6-flash" }),+ );+ await userEvent.upload(+ screen.getByLabelText("Service account key file file input"),+ new File([key], "key.json", { type: "application/json" }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Connect" }));++ await waitFor(() => {+ expect(onSaved).toHaveBeenCalled();+ });+ expect(+ await fetchMock.callHistory+ .lastCall("path:/api/llm/providers", { method: "POST" })+ ?.request?.json(),+ ).toEqual({+ type: "google",+ name: "Google Gemini Enterprise",+ config: { "service-account-key": key },+ model: "google/gemini-3.6-flash",+ });+ });+});++describe("ProviderConnectionForm editing a fixed-catalog connection", () => {+ it("starts the model picker on the model the connection is serving, not the type default", async () => {+ setupGoogle(+ createMockLlmProviderConnection({+ key: "google",+ type: "google",+ name: "Google Gemini Enterprise",+ config: {+ "auth-method": "oauth-token",+ "oauth-access-token": "**********en",+ "project-id": "my-project",+ },+ }),+ { modelRef: "google/google/gemini-3.6-flash" },+ );++ expect(await screen.findByLabelText("Model")).toHaveValue(+ "gemini-3.6-flash",+ );+ });++ it("disables the fields the environment owns and leaves the rest editable", async () => {+ setupGoogle(+ createMockLlmProviderConnection({+ key: "google",+ type: "google",+ name: "Google Gemini Enterprise",+ env_fields: ["project-id"],+ config: {+ "auth-method": "oauth-token",+ "oauth-access-token": "**********en",+ "project-id": "env-project",+ },+ }),+ );++ expect(await screen.findByLabelText("Project ID")).toBeDisabled();+ expect(screen.getByLabelText("OAuth access token")).toBeEnabled();+ });+});diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionModal.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionModal.tsxnew file mode 100644index 00000000000..c6377855a3f--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionModal.tsx@@ -0,0 +1,36 @@+import { t } from "ttag";++import { Modal } from "metabase/ui";+import type {+ LlmProviderConnection,+ LlmProviderType,+} from "metabase-types/api";++import { ProviderConnectionForm } from "./ProviderConnectionForm";++export function ProviderConnectionModal({+ providerTypes,+ connection,+ onClose,+}: {+ providerTypes: LlmProviderType[];+ connection?: LlmProviderConnection;+ onClose: (saved?: LlmProviderConnection) => void;+}) {+ return (+ <Modal+ opened+ onClose={() => onClose()}+ title={connection ? t`Edit provider` : t`Add a provider`}+ padding="xl"+ size="lg"+ >+ <ProviderConnectionForm+ providerTypes={providerTypes}+ connection={connection}+ onSaved={onClose}+ onCancel={() => onClose()}+ />+ </Modal>+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderModelPicker.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderModelPicker.tsxdeleted file mode 100644index 73e93bf839e..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderModelPicker.tsx+++ /dev/null@@ -1,122 +0,0 @@-import { useEffect, useMemo, useState } from "react";-import { t } from "ttag";-import _ from "underscore";--import { getErrorMessage } from "metabase/api/utils";-import { useToast } from "metabase/common/hooks";-import { type ComboboxItem, Select } from "metabase/ui";-import type {- MetabotProvider,- MetabotSettingsResponse,-} from "metabase-types/api";--import {- useGetMetabotSettingsQuery,- useUpdateMetabotSettingsMutation,-} from "../../api";--type MetabotModelOption = ComboboxItem & {- group?: string | null;-};--export function useProviderModelsQuery(- provider: MetabotProvider,- { skip }: { skip: boolean },-) {- const modelsQuery = useGetMetabotSettingsQuery({ provider }, { skip });-- return {- modelsQuery,- credentialsError:- modelsQuery.currentData?.["credentials-error"] ?? undefined,- };-}--export const ProviderModelPicker = ({- provider,- connectedModel,- models,- isLoading,- loadError,- disabled,-}: {- provider: Exclude<MetabotProvider, "metabase">;- connectedModel: string | undefined;- models: MetabotSettingsResponse["models"];- isLoading: boolean;- loadError: unknown;- disabled: boolean;-}) => {- const [model, setModel] = useState<string | undefined>(connectedModel);- const [updateMetabotSettings, updateMetabotSettingsResult] =- useUpdateMetabotSettingsMutation();- const [sendToast] = useToast();-- useEffect(() => {- if (connectedModel) {- setModel(connectedModel);- }- }, [connectedModel]);-- const modelOptions = useMemo(() => getLlmModelOptions(models), [models]);-- const handleModelChange = async (value: string) => {- setModel(value);-- if (!value) {- return;- }-- const result = await updateMetabotSettings({ provider, model: value });-- if (!result.error) {- sendToast({- message: t`Settings saved successfully`,- icon: "check",- });- }- };-- const loadErrorMessage = loadError- ? getErrorMessage(loadError, t`Unable to load models.`)- : undefined;- const saveErrorMessage = updateMetabotSettingsResult.error- ? getErrorMessage(- updateMetabotSettingsResult.error,- t`Unable to save provider settings.`,- )- : undefined;- const error = loadErrorMessage ?? saveErrorMessage;-- return (- <Select- label={t`Model`}- placeholder={isLoading ? t`Loading models...` : t`Select a model`}- description={t`Available models are fetched from the selected provider using its configured credentials.`}- error={error}- data={modelOptions}- value={model}- onChange={handleModelChange}- disabled={disabled}- searchable- nothingFoundMessage={t`No models found`}- />- );-};--const getLlmModelOptions = (models: MetabotSettingsResponse["models"]) => {- const modelOptions = models.map((m) => ({- value: m.id,- label: m.display_name,- group: m.group,- }));-- const sel = (o: MetabotModelOption) => _.pick(o, ["value", "label"]);- // group model options if needed- return _.every(modelOptions, (o) => !o.group)- ? modelOptions.map(sel)- : _.map(- _.groupBy(modelOptions, (o) => o.group ?? t`Other`),- (items, group) => ({ group, items: items.map(sel) }),- );-};diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.module.css b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.module.cssnew file mode 100644index 00000000000..519262ec356--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.module.css@@ -0,0 +1,10 @@+.icon {+ flex-shrink: 0;+ color: var(--mb-color-icon-brand);+ background-color: var(--mb-color-background-secondary);+ border-radius: var(--mantine-radius-md);+}++.mark {+ color: var(--mb-color-text-primary);+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.tsxnew file mode 100644index 00000000000..641a20f52bb--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.tsx@@ -0,0 +1,73 @@+import type { ComponentType, SVGProps } from "react";++import { DefaultLogoIcon } from "metabase/common/components/LogoIcon";+import { Flex, Icon } from "metabase/ui";+import type { LlmProviderTypeName } from "metabase-types/api";++import S from "./ProviderTypeIcon.module.css";+import AnthropicMark from "./logos/anthropic.svg?component";+import AzureMark from "./logos/azure.svg?component";+import BedrockMark from "./logos/bedrock.svg?component";+import GoogleMark from "./logos/google.svg?component";+import MistralMark from "./logos/mistral.svg?component";+import MoonshotMark from "./logos/moonshot.svg?component";+import OpenAiMark from "./logos/openai.svg?component";+import OpenRouterMark from "./logos/openrouter.svg?component";+import ZaiMark from "./logos/zai.svg?component";++const GENERIC_PROVIDER_ICON = "ai";++// Every mark is inlined rather than loaded by URL: it lets the single-colour ones take the theme's text+// colour the way their brands publish them, and it keeps them out of the emitted-asset pipeline. Metabase+// is null because it is drawn from its own logo component.+const PROVIDER_LOGOS: Record<+ LlmProviderTypeName,+ ComponentType<SVGProps<SVGSVGElement>> | null+> = {+ anthropic: AnthropicMark,+ openai: OpenAiMark,+ openrouter: OpenRouterMark,+ bedrock: BedrockMark,+ mistral: MistralMark,+ zai: ZaiMark,+ moonshot: MoonshotMark,+ google: GoogleMark,+ azure: AzureMark,+ metabase: null,+};++export function ProviderTypeIcon({+ type,+ size = 40,+}: {+ type: LlmProviderTypeName;+ size?: number;+}) {+ return (+ <Flex className={S.icon} w={size} h={size} align="center" justify="center">+ <ProviderTypeMark type={type} size={size / 2} />+ </Flex>+ );+}++function ProviderTypeMark({+ type,+ size,+}: {+ type: LlmProviderTypeName;+ size: number;+}) {+ if (type === "metabase") {+ return <DefaultLogoIcon height={size} />;+ }++ const Mark = PROVIDER_LOGOS[type];++ if (!Mark) {+ return <Icon name={GENERIC_PROVIDER_ICON} size={size} aria-hidden />;+ }++ return (+ <Mark className={S.mark} width={size} height={size} role="presentation" />+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.unit.spec.tsxnew file mode 100644index 00000000000..ff40f74fc95--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.unit.spec.tsx@@ -0,0 +1,68 @@+import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";+import { mockSettings } from "__support__/settings";+import { renderWithProviders, screen } from "__support__/ui";+import { reinitialize } from "metabase/plugins";+import type { LlmProviderTypeName } from "metabase-types/api";+import {+ createMockSettings,+ createMockTokenFeatures,+} from "metabase-types/api/mocks";++import { ProviderTypeIcon } from "./ProviderTypeIcon";++describe("ProviderTypeIcon", () => {+ afterEach(() => {+ reinitialize();+ });++ it("renders the Metabase logo for the managed provider", () => {+ renderWithProviders(<ProviderTypeIcon type="metabase" />);++ expect(screen.getByTestId("main-logo")).toBeInTheDocument();+ expect(screen.queryByRole("presentation")).not.toBeInTheDocument();+ });++ it.each([+ "anthropic",+ "openai",+ "openrouter",+ "mistral",+ "zai",+ "moonshot",+ "google",+ "azure",+ "bedrock",+ ] as const)("renders the vendor logo for %s", (type) => {+ renderWithProviders(<ProviderTypeIcon type={type} />);++ expect(screen.queryByTestId("main-logo")).not.toBeInTheDocument();+ expect(screen.getByRole("presentation")).toBeInTheDocument();+ });++ it("falls back to a generic icon for a type the frontend does not know yet", () => {+ // the registry is the backend's, so it can serve a type this union does not list yet;+ // the cast reproduces that server response, which is unreachable through the type alone+ const unknownType = "evilai" as LlmProviderTypeName;+ renderWithProviders(<ProviderTypeIcon type={unknownType} />);++ expect(screen.queryByRole("presentation")).not.toBeInTheDocument();+ });++ it("keeps the Metabase mark on a whitelabelled instance", () => {+ // mockSettings feeds the settings singleton hasPremiumFeature reads, so the token has to+ // land before the plugin initializes or it never registers its logo+ const settingsState = mockSettings(+ createMockSettings({+ "application-logo-url": "https://example.com/customer-logo.svg",+ "token-features": createMockTokenFeatures({ whitelabel: true }),+ }),+ );+ setupEnterpriseOnlyPlugin("whitelabel");++ renderWithProviders(<ProviderTypeIcon type="metabase" />, {+ storeInitialState: { settings: settingsState },+ });++ expect(screen.getByTestId("main-logo")).toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.module.css b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.module.cssnew file mode 100644index 00000000000..dc2fe74c729--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.module.css@@ -0,0 +1,31 @@+.option {+ display: block;+ width: 100%;+ min-height: 4.5rem;+ padding: var(--mantine-spacing-md);+ border: 1px solid var(--mb-color-border-neutral);+ border-radius: var(--mantine-radius-md);+ transition:+ border-color 120ms ease,+ background-color 120ms ease;+}++.option:hover:not(:disabled) {+ background-color: var(--mb-color-background-secondary);+ border-color: var(--mb-color-border-brand);+}++.option:focus-visible {+ outline: 2px solid var(--mb-color-focus);+ outline-offset: 2px;+}++.option:disabled {+ color: var(--mb-color-text-disabled);+ cursor: not-allowed;+ background-color: var(--mb-color-background-secondary);+}++.option:disabled .content {+ opacity: 0.6;+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.tsxnew file mode 100644index 00000000000..67deb0d698d--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypePicker.tsx@@ -0,0 +1,45 @@+import { t } from "ttag";++import { Group, SimpleGrid, Text, UnstyledButton } from "metabase/ui";+import type { LlmProviderType } from "metabase-types/api";++import { ProviderTypeIcon } from "./ProviderTypeIcon";+import S from "./ProviderTypePicker.module.css";++export function ProviderTypePicker({+ providerTypes,+ onSelect,+}: {+ providerTypes: LlmProviderType[];+ onSelect: (type: string) => void;+}) {+ return (+ <SimpleGrid cols={{ base: 1, sm: 2 }} spacing="sm">+ {providerTypes.map((providerType) => (+ <UnstyledButton+ className={S.option}+ key={providerType.type}+ disabled={!providerType.available}+ onClick={() => onSelect(providerType.type)}+ >+ <Group+ className={S.content}+ gap="sm"+ justify="space-between"+ wrap="nowrap"+ >+ <Group gap="sm" wrap="nowrap">+ <ProviderTypeIcon type={providerType.type} />+ <Text fw="bold">{providerType.label}</Text>+ </Group>+ {!providerType.available && (+ <Text c="text-disabled" size="sm">+ {t`Unavailable`}+ </Text>+ )}+ </Group>+ </UnstyledButton>+ ))}+ </SimpleGrid>+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/addable-provider-types.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/addable-provider-types.tsnew file mode 100644index 00000000000..3795ce7b9b0--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/addable-provider-types.ts@@ -0,0 +1,15 @@+import type {+ LlmProviderConnection,+ LlmProviderType,+} from "metabase-types/api";++export function getAddableProviderTypes(+ providerTypes: LlmProviderType[],+ connections: LlmProviderConnection[],+) {+ return providerTypes.filter(+ (providerType) =>+ !providerType.singleton ||+ !connections.some((connection) => connection.type === providerType.type),+ );+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.tsnew file mode 100644index 00000000000..09177fa008e--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.ts@@ -0,0 +1,31 @@+import type { LlmProviderType } from "metabase-types/api";++export type ApiKeyMatch = {+ providerType: LlmProviderType;+ fieldKey: string;+};++function getRecognizableFields(providerTypes: LlmProviderType[]) {+ return providerTypes+ .filter((providerType) => providerType.available && !providerType.managed)+ .flatMap((providerType) =>+ providerType.fields.flatMap((field) =>+ field.prefix+ ? [{ providerType, fieldKey: field.key, prefix: field.prefix }]+ : [],+ ),+ );+}++export function findProviderTypeForApiKey(+ providerTypes: LlmProviderType[],+ apiKey: string,+): ApiKeyMatch | undefined {+ const matches = getRecognizableFields(providerTypes).filter((field) =>+ apiKey.startsWith(field.prefix),+ );++ // OpenAI's `sk-` also matches an Anthropic `sk-ant-` key, so the longest prefix wins.+ const [best] = matches.sort((a, b) => b.prefix.length - a.prefix.length);+ return best;+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.unit.spec.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.unit.spec.tsnew file mode 100644index 00000000000..077a07f742a--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/api-key.unit.spec.ts@@ -0,0 +1,92 @@+import {+ createMockLlmProviderField,+ createMockLlmProviderType,+} from "metabase-types/api/mocks";++import { findProviderTypeForApiKey } from "./api-key";++const apiKeyField = (prefix?: string) =>+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ prefix,+ });++const ANTHROPIC = createMockLlmProviderType({+ type: "anthropic",+ label: "Anthropic",+ fields: [apiKeyField("sk-ant-")],+});++const OPENAI = createMockLlmProviderType({+ type: "openai",+ label: "OpenAI",+ fields: [apiKeyField("sk-")],+});++const OPENROUTER = createMockLlmProviderType({+ type: "openrouter",+ label: "OpenRouter",+ fields: [apiKeyField("sk-or-v1-")],+});++const AZURE = createMockLlmProviderType({+ type: "azure",+ label: "Microsoft Azure",+ fields: [apiKeyField()],+});++const ALL = [ANTHROPIC, OPENAI, OPENROUTER, AZURE];++describe("findProviderTypeForApiKey", () => {+ it.each([+ ["sk-ant-api03-abc123", "anthropic"],+ ["sk-proj-abc123", "openai"],+ ["sk-or-v1-abc123", "openrouter"],+ ])("matches %s to %s", (apiKey, expected) => {+ expect(findProviderTypeForApiKey(ALL, apiKey)?.providerType.type).toBe(+ expected,+ );+ });++ it("prefers the most specific prefix over a shorter one that also matches", () => {+ // Both `sk-` (OpenAI) and `sk-ant-` (Anthropic) match, regardless of list order+ expect(+ findProviderTypeForApiKey([OPENAI, ANTHROPIC], "sk-ant-api03-abc123")+ ?.providerType.type,+ ).toBe("anthropic");+ expect(+ findProviderTypeForApiKey([ANTHROPIC, OPENAI], "sk-ant-api03-abc123")+ ?.providerType.type,+ ).toBe("anthropic");+ });++ it("reports which field the key belongs in", () => {+ expect(findProviderTypeForApiKey(ALL, "sk-ant-api03-abc")?.fieldKey).toBe(+ "api-key",+ );+ });++ it("ignores a key that matches no declared prefix", () => {+ expect(findProviderTypeForApiKey(ALL, "azure-key-abc")).toBeUndefined();+ expect(findProviderTypeForApiKey(ALL, "")).toBeUndefined();+ });++ it("skips provider types that cannot be connected", () => {+ const unavailable = createMockLlmProviderType({+ ...ANTHROPIC,+ available: false,+ });+ expect(findProviderTypeForApiKey([unavailable], "sk-ant-abc")).toBe(+ undefined,+ );++ const managed = createMockLlmProviderType({+ ...ANTHROPIC,+ managed: true,+ });+ expect(findProviderTypeForApiKey([managed], "sk-ant-abc")).toBe(undefined);+ });+});diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/index.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/index.tsindex 372336f82a2..8820e2d4fbe 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/index.ts+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/index.ts@@ -1,4 +1,3 @@-export { useAIProviderConfigurationContext } from "./AIProviderConfigurationContext";-export { AIProviderConfigurationForm } from "./AIProviderConfigurationForm";-export { getProviderOptions, parseProviderAndModel } from "./utils";-export type { MetabotApiKeyProvider } from "./utils";+export { AIProviderList } from "./AIProviderList";+export { AIProviderSetup } from "./AIProviderSetup";+export { LlmModelPicker } from "./LlmModelPicker";diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/anthropic.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/anthropic.svgnew file mode 100644index 00000000000..32c47343c02--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/anthropic.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" fill-rule="evenodd" viewBox="0 0 24 24"><path d="M13.827 3.76h3.603L24 20.24h-3.603zm-7.258 0h3.767l6.57 16.48h-3.674l-1.343-3.461H5.017l-1.344 3.46H0L6.57 3.762zm4.132 9.959L8.453 7.927 6.205 13.72H10.7z"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/azure.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/azure.svgnew file mode 100644index 00000000000..3dfa1ffb95b--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/azure.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g transform="translate(-1.091 -1.091)scale(.2727)"><defs><linearGradient id="azure-a" x1="-1032.17" x2="-1059.21" y1="145.31" y2="65.43" gradientTransform="matrix(1 0 0 -1 1075 158)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#114a8b"/><stop offset="1" stop-color="#0669bc"/></linearGradient><linearGradient id="azure-b" x1="-1023.73" x2="-1029.98" y1="108.08" y2="105.97" gradientTransform="matrix(1 0 0 -1 1075 158)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-opacity=".3"/><stop offset=".07" stop-opacity=".2"/><stop offset=".32" stop-opacity=".1"/><stop offset=".62" stop-opacity=".05"/><stop offset="1" stop-opacity="0"/></linearGradient><linearGradient id="azure-c" x1="-1027.16" x2="-997.48" y1="147.64" y2="68.56" gradientTransform="matrix(1 0 0 -1 1075 158)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#3ccbf4"/><stop offset="1" stop-color="#2892df"/></linearGradient></defs><path fill="url(#azure-a)" d="M33.34 6.54h26.04l-27.03 80.1a4.15 4.15 0 0 1-3.94 2.81H8.15a4.14 4.14 0 0 1-3.93-5.47L29.4 9.38a4.15 4.15 0 0 1 3.94-2.83z"/><path fill="#0078d4" d="M71.17 60.26H29.88a1.91 1.91 0 0 0-1.3 3.31l26.53 24.76a4.17 4.17 0 0 0 2.85 1.13h23.38z"/><path fill="url(#azure-b)" d="M33.34 6.54a4.12 4.12 0 0 0-3.95 2.88L4.25 83.92a4.14 4.14 0 0 0 3.91 5.54h20.79a4.44 4.44 0 0 0 3.4-2.9l5.02-14.78 17.91 16.7a4.24 4.24 0 0 0 2.67.97h23.29L71.02 60.26H41.24L59.47 6.55z"/><path fill="url(#azure-c)" d="M66.6 9.36a4.14 4.14 0 0 0-3.93-2.82H33.65a4.15 4.15 0 0 1 3.93 2.82l25.18 74.62a4.15 4.15 0 0 1-3.93 5.48h29.02a4.15 4.15 0 0 0 3.93-5.48z"/></g></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/bedrock.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/bedrock.svgnew file mode 100644index 00000000000..f2fcbf3843e--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/bedrock.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="m6.706 10.012.16.719c0 .24.08.4.24.64v.159l-.08.24-.56.32-.16.08-.24-.08-.319-.4-.24-.48c-.639.72-1.438 1.119-2.317 1.119-.719 0-1.278-.24-1.598-.64-.4-.319-.639-.878-.639-1.517s.24-1.199.72-1.598c.478-.48 1.118-.64 1.997-.64a6.3 6.3 0 0 1 1.757.24v-.56c0-.638-.16-1.038-.4-1.278-.239-.32-.638-.399-1.278-.399l-.878.08a6.4 6.4 0 0 0-1.119.4h-.16c-.08 0-.16-.08-.16-.24v-.4l.08-.24c0-.08.08-.16.24-.16l.959-.399 1.278-.16c.959 0 1.598.24 2.078.64.4.479.639 1.118.639 1.997zM3.51 11.29l.799-.16c.32-.08.56-.32.799-.559l.24-.48.08-.718v-.32a6.7 6.7 0 0 0-1.519-.16c-.48 0-.879.08-1.198.32-.24.16-.32.48-.32.879s.08.639.24.879c.24.16.48.32.879.32m6.392.798-.32-.08-.16-.24-1.837-6.232-.08-.32.16-.159h.799l.32.08.159.32 1.358 5.273L11.5 5.458l.16-.32.32-.08h.638l.32.08.16.32 1.278 5.353 1.358-5.353.16-.32.32-.08h.719c.16 0 .24.08.24.16v.16l-.08.16-1.918 6.232-.16.32-.32.08h-.718l-.32-.08-.08-.32L12.3 6.576l-1.2 5.114-.16.32-.32.08zm10.307.24a5.3 5.3 0 0 1-2.157-.48l-.24-.24-.08-.159v-.4c0-.16.08-.24.16-.24h.16l.24.08a4.3 4.3 0 0 0 1.837.4c.48 0 .879-.16 1.119-.32.32-.16.4-.399.4-.718l-.16-.56-.8-.4-1.198-.399c-.56-.16-1.039-.48-1.279-.799a1.92 1.92 0 0 1 .4-2.716l.799-.4a3.5 3.5 0 0 1 1.598-.16 9 9 0 0 1 .959.24l.32.16.239.16.08.32v.319c0 .24-.08.32-.16.32l-.32-.16a4.6 4.6 0 0 0-1.518-.24c-.479 0-.879 0-1.118.16s-.32.4-.32.719c0 .24.08.4.24.56s.4.319.879.479l1.118.32c.56.239.96.479 1.199.798s.4.72.4 1.119l-.24.959-.56.639c-.24.24-.559.4-.879.48z"/><path fill="#f90" d="M21.727 16.244A17.58 17.58 0 0 1 .154 14.646c-.32-.24-.08-.48.16-.32a23.97 23.97 0 0 0 21.014 1.279c.399-.16.799.32.399.64"/><path fill="#f90" d="M22.766 14.966c-.32-.4-2.237-.24-3.036-.08-.32 0-.32-.24-.08-.4 1.518-1.038 3.995-.719 4.234-.4.32.4-.08 2.877-1.438 4.076-.24.16-.48.08-.4-.16.4-.8 1.04-2.637.72-3.036"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/google.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/google.svgnew file mode 100644index 00000000000..a9faf087136--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/google.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g transform="scale(0.080537)"><path fill="#3186FF" d="M141.201 4.886c2.282-6.17 11.042-6.071 13.184.148l5.985 17.37a184.004 184.004 0 0 0 111.257 113.049l19.304 6.997c6.143 2.227 6.156 10.91.02 13.155l-19.35 7.082a184.001 184.001 0 0 0-109.495 109.385l-7.573 20.629c-2.241 6.105-10.869 6.121-13.133.025l-7.908-21.296a184 184 0 0 0-109.02-108.658l-19.698-7.239c-6.102-2.243-6.118-10.867-.025-13.132l20.083-7.467A183.998 183.998 0 0 0 133.291 26.28l7.91-21.394Z"/></g></svg>\ No newline at end of filediff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/mistral.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/mistral.svgnew file mode 100644index 00000000000..ebc4e17b37d--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/mistral.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.464 1.09h4.366v4.367h-4.366z"/><path fill="#F7D046" d="M19.647 1.09h4.366v4.367h-4.366z"/><path d="M0 1.09h4.366v4.367H0zm0 4.367h4.366v4.366H0zm0 4.366h4.366v4.366H0zm0 4.366h4.366v4.366H0zm0 4.366h4.366v4.366H0z"/><path fill="#F7D046" d="M2.183 1.09h4.366v4.367H2.183z"/><path fill="#F2A73B" d="M19.647 5.457h4.366v4.366h-4.366zm-17.464 0h4.366v4.366H2.183z"/><path d="M13.098 5.457h4.366v4.366h-4.366z"/><path fill="#F2A73B" d="M15.281 5.457h4.366v4.366H15.28zm-8.732 0h4.366v4.366H6.549z"/><path fill="#EE792F" d="M10.915 9.823h4.366v4.366h-4.366zm4.366 0h4.366v4.366H15.28zm-8.732 0h4.366v4.366H6.549z"/><path d="M8.732 14.189h4.366v4.366H8.732z"/><path fill="#EB5829" d="M10.915 14.189h4.366v4.366h-4.366z"/><path fill="#EE792F" d="M19.647 9.823h4.366v4.366h-4.366zm-17.464 0h4.366v4.366H2.183z"/><path d="M17.464 14.189h4.366v4.366h-4.366z"/><path fill="#EB5829" d="M19.647 14.189h4.366v4.366h-4.366z"/><path d="M17.464 18.555h4.366v4.366h-4.366z"/><path fill="#EB5829" d="M2.183 14.189h4.366v4.366H2.183z"/><path fill="#EA3326" d="M19.647 18.555h4.366v4.366h-4.366zm-17.464 0h4.366v4.366H2.183z"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/moonshot.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/moonshot.svgnew file mode 100644index 00000000000..13c4c4d8e77--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/moonshot.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" fill-rule="evenodd" viewBox="0 0 24 24"><path d="M1.052 16.916l9.539 2.552a21.007 21.007 0 00.06 2.033l5.956 1.593a11.997 11.997 0 01-5.586.865l-.18-.016-.044-.004-.084-.009-.094-.01a11.605 11.605 0 01-.157-.02l-.107-.014-.11-.016a11.962 11.962 0 01-.32-.051l-.042-.008-.075-.013-.107-.02-.07-.015-.093-.019-.075-.016-.095-.02-.097-.023-.094-.022-.068-.017-.088-.022-.09-.024-.095-.025-.082-.023-.109-.03-.062-.02-.084-.025-.093-.028-.105-.034-.058-.019-.08-.026-.09-.031-.066-.024a6.293 6.293 0 01-.044-.015l-.068-.025-.101-.037-.057-.022-.08-.03-.087-.035-.088-.035-.079-.032-.095-.04-.063-.028-.063-.027a5.655 5.655 0 01-.041-.018l-.066-.03-.103-.047-.052-.024-.096-.046-.062-.03-.084-.04-.086-.044-.093-.047-.052-.027-.103-.055-.057-.03-.058-.032a6.49 6.49 0 01-.046-.026l-.094-.053-.06-.034-.051-.03-.072-.041-.082-.05-.093-.056-.052-.032-.084-.053-.061-.039-.079-.05-.07-.047-.053-.035a7.785 7.785 0 01-.054-.036l-.044-.03-.044-.03a6.066 6.066 0 01-.04-.028l-.057-.04-.076-.054-.069-.05-.074-.054-.056-.042-.076-.057-.076-.059-.086-.067-.045-.035-.064-.052-.074-.06-.089-.073-.046-.039-.046-.039a7.516 7.516 0 01-.043-.037l-.045-.04-.061-.053-.07-.062-.068-.06-.062-.058-.067-.062-.053-.05-.088-.084a13.28 13.28 0 01-.099-.097l-.029-.028-.041-.042-.069-.07-.05-.051-.05-.053a6.457 6.457 0 01-.168-.179l-.08-.088-.062-.07-.071-.08-.042-.049-.053-.062-.058-.068-.046-.056a7.175 7.175 0 01-.027-.033l-.045-.055-.066-.082-.041-.052-.05-.064-.02-.025a11.99 11.99 0 01-1.44-2.402zm-1.02-5.794l11.353 3.037a20.468 20.468 0 00-.469 2.011l10.817 2.894a12.076 12.076 0 01-1.845 2.005L.657 15.923l-.016-.046-.035-.104a11.965 11.965 0 01-.05-.153l-.007-.023a11.896 11.896 0 01-.207-.741l-.03-.126-.018-.08-.021-.097-.018-.081-.018-.09-.017-.084-.018-.094c-.026-.141-.05-.283-.071-.426l-.017-.118-.011-.083-.013-.102a12.01 12.01 0 01-.019-.161l-.005-.047a12.12 12.12 0 01-.034-2.145zm1.593-5.15l11.948 3.196c-.368.605-.705 1.231-1.01 1.875l11.295 3.022c-.142.82-.368 1.612-.668 2.365l-11.55-3.09L.124 10.26l.015-.1.008-.049.01-.067.015-.087.018-.098c.026-.148.056-.295.088-.442l.028-.124.02-.085.024-.097c.022-.09.045-.18.07-.268l.028-.102.023-.083.03-.1.025-.082.03-.096.026-.082.031-.095a11.896 11.896 0 011.01-2.232zm4.442-4.4L17.352 4.59a20.77 20.77 0 00-1.688 1.721l7.823 2.093c.267.852.442 1.744.513 2.665L2.106 5.213l.045-.065.027-.04.04-.055.046-.065.055-.076.054-.072.064-.086.05-.065.057-.073.055-.07.06-.074.055-.069.065-.077.054-.066.066-.077.053-.06.072-.082.053-.06.067-.074.054-.058.073-.078.058-.06.063-.067.168-.17.1-.098.059-.056.076-.071a12.084 12.084 0 012.272-1.677zM12.017 0h.097l.082.001.069.001.054.002.068.002.046.001.076.003.047.002.06.003.054.002.087.005.105.007.144.011.088.007.044.004.077.008.082.008.047.005.102.012.05.006.108.014.081.01.042.006.065.01.207.032.07.012.065.011.14.026.092.018.11.022.046.01.075.016.041.01L14.7.3l.042.01.065.015.049.012.071.017.096.024.112.03.113.03.113.032.05.015.07.02.078.024.073.023.05.016.05.016.076.025.099.033.102.036.048.017.064.023.093.034.11.041.116.045.1.04.047.02.06.024.041.018.063.026.04.018.057.025.11.048.1.046.074.035.075.036.06.028.092.046.091.045.102.052.053.028.049.026.046.024.06.033.041.022.052.029.088.05.106.06.087.051.057.034.053.032.096.059.088.055.098.062.036.024.064.041.084.056.04.027.062.042.062.043.023.017c.054.037.108.075.161.114l.083.06.065.048.056.043.086.065.082.064.04.03.05.041.086.069.079.065.085.071c.712.6 1.353 1.283 1.909 2.031L7.222.994l.062-.027.065-.028.081-.034.086-.035c.113-.045.227-.09.341-.131l.096-.035.093-.033.084-.03.096-.031c.087-.03.176-.058.264-.085l.091-.027.086-.025.102-.03.085-.023.1-.026L9.04.37l.09-.023.091-.022.095-.022.09-.02.098-.021.091-.02.095-.018.092-.018.1-.018.091-.016.098-.017.092-.014.097-.015.092-.013.102-.013.091-.012.105-.012.09-.01.105-.01c.093-.01.186-.018.28-.024l.106-.008.09-.005.11-.006.093-.004.1-.004.097-.002.099-.002.197-.002z"/></svg>\ No newline at end of filediff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openai.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openai.svgnew file mode 100644index 00000000000..e629e86c529--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openai.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24"><path d="M22.288 9.824a6 6 0 0 0-.516-4.912A6.05 6.05 0 0 0 15.26 2.01 6.067 6.067 0 0 0 4.982 4.183a6 6 0 0 0-3.999 2.9 6.05 6.05 0 0 0 .743 7.099 5.98 5.98 0 0 0 .511 4.912 6.05 6.05 0 0 0 6.516 2.9 6 6 0 0 0 4.51 2.012 6.06 6.06 0 0 0 5.774-4.207 6 6 0 0 0 3.998-2.9 6.06 6.06 0 0 0-.747-7.076m-9.025 12.61a4.48 4.48 0 0 1-2.877-1.04l.142-.08 4.78-2.76a.8.8 0 0 0 .392-.681v-6.739l2.021 1.17q.031.017.038.051v5.584a4.505 4.505 0 0 1-4.496 4.496M3.6 18.308a4.47 4.47 0 0 1-.535-3.014l.142.085 4.785 2.759a.77.77 0 0 0 .78 0l5.845-3.37v2.334a.08.08 0 0 1-.034.061l-4.84 2.792A4.5 4.5 0 0 1 3.6 18.308M2.341 7.898a4.5 4.5 0 0 1 2.366-1.974v5.679a.77.77 0 0 0 .388.676l5.816 3.356-2.02 1.168a.08.08 0 0 1-.071 0l-4.832-2.787a4.505 4.505 0 0 1-1.647-6.142zm16.6 3.856-5.834-3.388 2.016-1.164a.08.08 0 0 1 .071 0l4.832 2.792a4.496 4.496 0 0 1-.677 8.106v-5.679a.79.79 0 0 0-.407-.667m2.011-3.024-.142-.085-4.775-2.782a.78.78 0 0 0-.785 0L9.41 9.232V6.899a.07.07 0 0 1 .029-.061L14.27 4.05a4.5 4.5 0 0 1 6.682 4.661zM8.309 12.866l-2.021-1.164a.08.08 0 0 1-.038-.057v-5.57a4.5 4.5 0 0 1 7.378-3.454l-.142.08-4.78 2.76a.8.8 0 0 0-.393.68zM9.406 10.5 12.01 9l2.608 1.5v3L12.019 15 9.41 13.5z"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openrouter.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openrouter.svgnew file mode 100644index 00000000000..a80561c894c--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/openrouter.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" stroke="currentColor" viewBox="0 0 24 24"><path stroke-width="4.23" d="M.141 12.007c.705 0 3.431-.608 4.841-1.407s1.41-.8 4.324-2.867c3.69-2.619 6.298-1.742 10.575-1.742"/><path stroke-width=".047" d="m24.017 6.017-7.226 4.172V1.845z"/><path stroke-width="4.23" d="M0 12.01c.705 0 3.431.608 4.841 1.407s1.41.799 4.324 2.867c3.69 2.618 6.298 1.742 10.575 1.742"/><path stroke-width=".047" d="m23.876 18-7.226-4.173v8.345z"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/zai.svg b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/zai.svgnew file mode 100644index 00000000000..987952f50ab--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/logos/zai.svg@@ -0,0 +1 @@+<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#2D2D2D" stroke="#FFF" stroke-miterlimit="10" stroke-width=".494" d="M19.428 22.557H4.566a3.125 3.125 0 0 1-3.126-3.125V4.57a3.125 3.125 0 0 1 3.126-3.126h14.87A3.125 3.125 0 0 1 22.56 4.57v14.87a3.135 3.135 0 0 1-3.133 3.117z"/><path fill="#FFF" d="m12.364 5.828-1.016 1.445a.86.86 0 0 1-.703.367H5.097V5.82c-.008.008 7.267.008 7.267.008m6.9 0-8.72 12.354H4.73l8.72-12.354zM11.63 18.182l1.023-1.454a.86.86 0 0 1 .703-.367h5.54v1.82z"/></svg>diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.tsdeleted file mode 100644index f9e6be9b00e..00000000000--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/utils.ts+++ /dev/null@@ -1,182 +0,0 @@-import { t } from "ttag";--import type { MetabotProvider, SettingDefinition } from "metabase-types/api";--type ApiKeylessProviders = "metabase" | "google";-type ApiKeyProviders = Exclude<MetabotProvider, ApiKeylessProviders>;--type MetabotApiKeylessProviderOption = {- value: ApiKeylessProviders;- label: string;-};--type MetabotApiKeyProviderOption = {- value: ApiKeyProviders;- label: string;- apiKey: {- placeholder: string;- addKeyUrl: string;- };-};--export type MetabotProviderOption =- | MetabotApiKeylessProviderOption- | MetabotApiKeyProviderOption;--export function getProviderOptions(- hasMetabaseProviderAccess: boolean,-): Partial<Record<"metabase", MetabotApiKeylessProviderOption>> &- Record<"google", MetabotApiKeylessProviderOption> &- Record<ApiKeyProviders, MetabotApiKeyProviderOption> {- return {- ...(hasMetabaseProviderAccess && {- metabase: {- value: "metabase" as const,- // eslint-disable-next-line metabase/no-literal-metabase-strings -- "Metabase" is the product name for the managed AI provider option, only shown to admins configuring AI.- label: "Metabase",- },- }),- anthropic: {- value: "anthropic",- label: "Anthropic",- apiKey: {- placeholder: "sk-ant-api03-...",- addKeyUrl: "https://console.anthropic.com/settings/keys",- },- },- azure: {- value: "azure",- label: "Microsoft Azure",- apiKey: {- // Azure data-plane keys have no recognizable prefix- placeholder: t`Enter your Azure API key`,- addKeyUrl: "https://ai.azure.com",- },- },- bedrock: {- value: "bedrock",- label: "Amazon Bedrock",- apiKey: {- placeholder: "AKIA...",- addKeyUrl:- "https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html",- },- },- google: {- value: "google",- label: "Gemini Enterprise Agent Platform",- },- mistral: {- value: "mistral",- label: "Mistral",- apiKey: {- // Mistral keys have no recognizable prefix- placeholder: t`Enter your Mistral API key`,- 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",- apiKey: {- placeholder: "sk-proj-...",- addKeyUrl: "https://platform.openai.com/api-keys",- },- },- openrouter: {- value: "openrouter",- label: "OpenRouter",- apiKey: {- placeholder: "sk-or-v1-...",- addKeyUrl: "https://openrouter.ai/keys",- },- },- zai: {- value: "zai",- label: "Z.AI",- apiKey: {- // Z.AI keys have no recognizable prefix- placeholder: t`Enter your Z.AI API key`,- addKeyUrl: "https://z.ai/manage-apikey/apikey-list",- },- },- };-}--export type MetabotApiKeyProvider = Exclude<- MetabotProvider,- "metabase" | "azure" | "bedrock" | "google"->;--export function isMetabotProvider(- value: string | null | undefined,-): value is MetabotProvider {- return !!value && value in getProviderOptions(true);-}--export function isAvailableProvider(provider: MetabotProvider): boolean {- return (- provider === "anthropic" ||- provider === "azure" ||- provider === "bedrock" ||- provider === "google" ||- provider === "metabase" ||- provider === "mistral" ||- provider === "moonshot" ||- provider === "openai" ||- provider === "openrouter" ||- provider === "zai"- );-}--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",-};--export const AZURE_MODEL_FAMILIES = [- { value: "anthropic", label: "Anthropic" },- { value: "openai", label: "OpenAI" },-] as const;--export function parseAzureModel(model: string | undefined) {- const [family, deployment] = model?.split(/\/(.+)/, 2) ?? [];- return { family, deployment };-}--export function parseProviderAndModel(value: string | null | undefined) {- if (!value) {- return undefined;- }-- const [provider, model] = value.split(/\/(.+)/, 2);- if (!isMetabotProvider(provider) || !model) {- return undefined;- }-- return { provider, model };-}--export const hasConfiguredSettingValue = (- setting: SettingDefinition | undefined,-) => Boolean(setting?.value || setting?.is_env_setting);diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/visible-fields.ts b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/visible-fields.tsnew file mode 100644index 00000000000..fb904c562bc--- /dev/null+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/visible-fields.ts@@ -0,0 +1,27 @@+import type { LlmProviderConfig, LlmProviderField } from "metabase-types/api";++// A field can hang off another one — Google's credential inputs follow its authentication method —+// and a hidden one is not part of the connection: its value is cleared on save so switching the+// method leaves the credential it replaced behind.+export function isVisibleField(+ field: LlmProviderField,+ fields: LlmProviderField[],+ values: LlmProviderConfig,+) {+ const condition = field.show_when;+ if (!condition) {+ return true;+ }+ const controlling = fields.find((other) => other.key === condition.field);+ const value = values[condition.field] || controlling?.default || "";+ return value === condition.value;+}++export function getHiddenFieldKeys(+ fields: LlmProviderField[],+ values: LlmProviderConfig,+) {+ return fields+ .filter((field) => !isVisibleField(field, fields, values))+ .map((field) => field.key);+}diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationModal.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationModal.tsxindex 57c6c0d1c5d..807258a77d9 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationModal.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationModal.tsx@@ -2,7 +2,7 @@ import { t } from "ttag";import { Modal, type ModalProps } from "metabase/ui";-import { AIProviderConfigurationForm } from "./AIProviderConfigurationForm";+import { AIProviderSetup } from "./AIProviderConfigurationForm";export function AIProviderConfigurationModal({opened,@@ -16,7 +16,7 @@ export function AIProviderConfigurationModal({size="lg"data-testid="ai-provider-configuration-modal">- <AIProviderConfigurationForm isModal onClose={onClose} />+ <AIProviderSetup onDone={onClose} /></Modal>);}diff --git a/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.tsx b/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.tsxindex 1d8070156ad..26f5552ce82 100644--- a/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.tsx+++ b/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.tsx@@ -16,39 +16,36 @@ import {Text,} from "metabase/ui";-import { AIProviderConfigurationForm } from "./AIProviderConfigurationForm";+import { AIProviderSetup } from "./AIProviderConfigurationForm";const METABOT_MANAGED_PROVIDER_LIMIT_TOAST_ID ="metabot-managed-provider-limit";type MetabotManagedProviderLimitActionsProps = {inline?: boolean;- onConfigure?: VoidFunction;onConfigureClose?: VoidFunction;} & FlexProps;export const MetabotManagedProviderLimitActions = ({inline = false,- onConfigure,onConfigureClose,...rest}: MetabotManagedProviderLimitActionsProps) => {const canConfigureAi = useSelector(canAccessSettings);- const [isOpen, { open, close }] = useDisclosure(false, {+ const [isOpen, { open: handleConfigure, close }] = useDisclosure(false, {onClose: onConfigureClose,});- const handleConfigure = useCallback(() => {- (onConfigure ?? open)();- }, [onConfigure, open]);+ // The managed connection is left exactly as it is: running out of included tokens is a reason to add a provider+ // alongside it, not to cancel the subscription behind it.const configureModal = (<Modal- title={t`Connect to an AI provider`}+ title={t`Add an AI provider`}onClose={close}opened={isOpen}size="lg">- <AIProviderConfigurationForm isModal onClose={close} />+ <AIProviderSetup startOnConnectionForm onDone={close} /></Modal>);diff --git a/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.unit.spec.tsxindex d137160bd9f..21ad7d9032b 100644--- a/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.unit.spec.tsx+++ b/frontend/src/metabase/metabot/components/MetabotManagedProviderLimit.unit.spec.tsx@@ -1,19 +1,23 @@import userEvent from "@testing-library/user-event";+import {+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,+} from "__support__/server-mocks/metabot";import { renderWithProviders, screen, waitFor } from "__support__/ui";-import * as aiProviderConfigurationFormModule from "metabase/metabot/components/AIProviderConfigurationForm/AIProviderConfigurationForm";-import { createMockUser } from "metabase-types/api/mocks";+import {+ createMockLlmProviderType,+ createMockUser,+} from "metabase-types/api/mocks";import { getMetabotManagedProviderLimitToastProps } from "./MetabotManagedProviderLimit";describe("getMetabotManagedProviderLimitToastProps", () => {beforeEach(() => {- jest.restoreAllMocks();- jest- .spyOn(aiProviderConfigurationFormModule, "AIProviderConfigurationForm")- .mockImplementation(() => (- <div>Mocked AI provider configuration form</div>- ));+ setupLlmProviderTypesEndpoint([+ createMockLlmProviderType({ type: "anthropic", label: "Anthropic" }),+ ]);+ setupLlmProvidersEndpoint();});it("dismisses the toast only when the configure modal closes", async () => {@@ -37,7 +41,7 @@ describe("getMetabotManagedProviderLimitToastProps", () => {);expect(- screen.getByText("Mocked AI provider configuration form"),+ await screen.findByRole("button", { name: /Anthropic/ }),).toBeInTheDocument();expect(store.getState().undo).toHaveLength(1);diff --git a/frontend/src/metabase/metabot/hooks/index.ts b/frontend/src/metabase/metabot/hooks/index.tsindex 01ef67d1087..e6113e3e07b 100644--- a/frontend/src/metabase/metabot/hooks/index.ts+++ b/frontend/src/metabase/metabot/hooks/index.ts@@ -1,5 +1,6 @@export { useIsAskPage } from "./use-is-ask-page";export { useIsFullPageMetabot } from "./use-is-full-page-metabot";+export { useLlmConnectionModels } from "./use-llm-connection-models";export { useMetabotEnabledEmbeddingAware } from "./use-metabot-embedding-aware-enabled";export { useBranchableMessages } from "./use-branchable-messages";export { useMetabotAgent } from "./use-metabot-agent";diff --git a/frontend/src/metabase/metabot/hooks/use-llm-connection-models.ts b/frontend/src/metabase/metabot/hooks/use-llm-connection-models.tsnew file mode 100644index 00000000000..954d295eb5f--- /dev/null+++ b/frontend/src/metabase/metabot/hooks/use-llm-connection-models.ts@@ -0,0 +1,64 @@+import { useMemo } from "react";++import { useListLlmModelsQuery } from "metabase/api";+import type { LlmConnectionModels } from "metabase-types/api";++export function useLlmConnectionModels() {+ const { data: connections = [], isLoading, error } = useListLlmModelsQuery();++ const modelOptions = useMemo(+ () => getModelOptions(connections),+ [connections],+ );++ const errorByConnectionKey = useMemo(+ () => getErrorByConnectionKey(connections),+ [connections],+ );++ const modelNameByRef = useMemo(+ () => getModelNameByRef(connections),+ [connections],+ );++ return {+ connections,+ modelOptions,+ modelNameByRef,+ errorByConnectionKey,+ isLoading,+ error,+ };+}++// The label is what a closed Select shows, and a model name alone does not say which provider+// serves it. The dropdown renders [[modelNameByRef]] instead, where the group heading already does.+function getModelOptions(connections: LlmConnectionModels[]) {+ return connections+ .filter((connection) => connection.models.length > 0)+ .map((connection) => ({+ group: connection.name,+ items: connection.models.map((model) => ({+ value: `${connection.key}/${model.id}`,+ label: `${connection.name} · ${model.display_name}`,+ })),+ }));+}++function getModelNameByRef(connections: LlmConnectionModels[]) {+ return Object.fromEntries(+ connections.flatMap((connection) =>+ connection.models.map((model) => [+ `${connection.key}/${model.id}`,+ model.display_name,+ ]),+ ),+ );+}++function getErrorByConnectionKey(connections: LlmConnectionModels[]) {+ const entries = connections.flatMap((connection) =>+ connection.error ? [[connection.key, connection.error] as const] : [],+ );+ return Object.fromEntries(entries);+}diff --git a/frontend/src/metabase/metabot/index.tsx b/frontend/src/metabase/metabot/index.tsxindex 6a03a3aca3f..eb8f56dbb1c 100644--- a/frontend/src/metabase/metabot/index.tsx+++ b/frontend/src/metabase/metabot/index.tsx@@ -1,18 +1,14 @@export {useDeleteSuggestedMetabotPromptMutation,- useGetMetabotSettingsQuery,useGetSuggestedMetabotPromptsQuery,useListMetabotsQuery,useRegenerateSuggestedMetabotPromptsMutation,useUpdateMetabotMutation,- useUpdateMetabotSettingsMutation,useUpdateMetabotSlackSettingsMutation,} from "./api";export * from "./context";export {- AIProviderConfigurationForm,- getProviderOptions,- parseProviderAndModel,- useAIProviderConfigurationContext,+ AIProviderList,+ AIProviderSetup,+ LlmModelPicker,} from "./components/AIProviderConfigurationForm";-export type { MetabotApiKeyProvider } from "./components/AIProviderConfigurationForm";diff --git a/frontend/src/metabase/plugins/oss/metabot.ts b/frontend/src/metabase/plugins/oss/metabot.tsindex 96924645b45..7139070535a 100644--- a/frontend/src/metabase/plugins/oss/metabot.ts+++ b/frontend/src/metabase/plugins/oss/metabot.ts@@ -4,6 +4,8 @@ import { PluginPlaceholder } from "metabase/plugins/components/PluginPlaceholderexport type MetabaseAIProviderSetupProps = {onConnect?: VoidFunction;+ onCancel?: VoidFunction;+ isConnected?: boolean;};const getDefaultPluginMetabot = () => ({@@ -11,11 +13,13 @@ const getDefaultPluginMetabot = () => ({MetabaseAIProviderSetup:// Unjustified type cast. FIXMEPluginPlaceholder as ComponentType<MetabaseAIProviderSetupProps>,+ hasMetabaseManagedProviderDetails: () => false,});export const PLUGIN_METABOT: {isEnabled: boolean;MetabaseAIProviderSetup: ComponentType<MetabaseAIProviderSetupProps>;+ hasMetabaseManagedProviderDetails: () => boolean;} = getDefaultPluginMetabot();/**diff --git a/frontend/src/metabase/settings/api.ts b/frontend/src/metabase/settings/api.tsindex a1a84598a1f..fbf647d0309 100644--- a/frontend/src/metabase/settings/api.ts+++ b/frontend/src/metabase/settings/api.ts@@ -91,7 +91,6 @@ export const settingsApi = Api.injectEndpoints({return invalidateTags(error, [tag("session-properties"),...(key === "uploads-settings" ? [listTag("database")] : []),- ...(key === "llm-anthropic-api-key" ? [listTag("llm-models")] : []),...(key === "mfa-enforcement" ? [tag("mfa-status")] : []),// Enabling tenants creates the "all-external-users" permission groupdiff --git a/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.tsx b/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.tsxindex 7af5b81a0f2..e77ef5b3207 100644--- a/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.tsx+++ b/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.tsx@@ -1,16 +1,13 @@-import { P, match } from "ts-pattern";import { t } from "ttag";import {- AIProviderConfigurationForm,- getProviderOptions,- parseProviderAndModel,-} from "metabase/metabot";-import { PLUGIN_METABOT } from "metabase/plugins";+ useListLlmProviderTypesQuery,+ useListLlmProvidersQuery,+} from "metabase/api";+import { AIProviderSetup } from "metabase/metabot";import { useDispatch } from "metabase/redux";import { useSetting } from "metabase/settings";-import { Text } from "metabase/ui";-import type { MetabotProvider } from "metabase-types/api";+import { Button, Flex, Text } from "metabase/ui";import { skipAiConfig, submitAiConfig } from "../../actions";import { useStep } from "../../useStep";@@ -21,16 +18,24 @@ import type { NumberedStepProps } from "../types";export const AIConfigStep = ({ stepLabel }: NumberedStepProps) => {const { isStepActive, isStepCompleted } = useStep("ai_config");const dispatch = useDispatch();- const offerMetabaseAiManaged = PLUGIN_METABOT.isEnabled;- const isConfigured = !!useSetting("llm-metabot-configured?");- const savedProviderValue = useSetting("llm-metabot-provider");- const connectedProvider = isConfigured- ? parseProviderAndModel(savedProviderValue)?.provider- : undefined;+ const { data: connections = [], isLoading } = useListLlmProvidersQuery();+ const { data: providerTypes = [] } = useListLlmProviderTypesQuery();- const handleSubmit = (provider?: MetabotProvider) => {- dispatch(submitAiConfig(provider ?? connectedProvider));+ // The provider this step reports is the one Metabot actually landed on, not the first usable+ // connection: with several connected, the model picked in the picker decides.+ const modelRef = useSetting("llm-metabot-provider");+ const modelRefConnectionKey = modelRef?.split("/")[0];+ const connectedProvider = connections.find(+ (connection) =>+ connection.usable && connection.key === modelRefConnectionKey,+ );+ const connectedLabel = providerTypes.find(+ (providerType) => providerType.type === connectedProvider?.type,+ )?.label;++ const handleDone = () => {+ dispatch(submitAiConfig(connectedProvider?.type));};const handleSkip = () => {@@ -41,9 +46,9 @@ export const AIConfigStep = ({ stepLabel }: NumberedStepProps) => {return (<InactiveSteptitle={getStepTitle({- connectedProvider,+ connectedLabel,+ hasConnectedProvider: connectedProvider != null,isStepCompleted,- offerMetabaseAiManaged,})}label={stepLabel}isStepCompleted={isStepCompleted}@@ -56,30 +61,35 @@ export const AIConfigStep = ({ stepLabel }: NumberedStepProps) => {<Text mb="lg" c="text-secondary">{t`Select your AI provider to use AI explorations, SQL generation and Metabot.`}</Text>- <AIProviderConfigurationForm- isModal- defaultProvider={offerMetabaseAiManaged ? "metabase" : undefined}- onClose={handleSubmit}- onSkip={handleSkip}- />+ <AIProviderSetup onDone={handleDone} />+ {!isLoading && connectedProvider == null && (+ <Flex justify="end" mt="md">+ <Button variant="subtle" onClick={handleSkip}>+ {t`I'll set this up later`}+ </Button>+ </Flex>+ )}</ActiveStep>);};const getStepTitle = ({- connectedProvider,+ connectedLabel,+ hasConnectedProvider,isStepCompleted,- offerMetabaseAiManaged,}: {- connectedProvider: MetabotProvider | undefined;+ connectedLabel: string | undefined;+ hasConnectedProvider: boolean;isStepCompleted: boolean;- offerMetabaseAiManaged: boolean;-}): string =>- match({ isStepCompleted, connectedProvider })- .with({ isStepCompleted: false }, () => t`Connect to an AI provider`)- .with({ connectedProvider: P.nonNullable }, ({ connectedProvider }) => {- return t`Connected to ${- getProviderOptions(offerMetabaseAiManaged)[connectedProvider]?.label- }`;- })- .otherwise(() => t`I'll set up AI later`);+}): string => {+ if (!isStepCompleted) {+ return t`Connect to an AI provider`;+ }+ if (!hasConnectedProvider) {+ return t`I'll set up AI later`;+ }+ // the label comes from a separate request, so a connected provider can briefly have no name+ return connectedLabel+ ? t`Connected to ${connectedLabel}`+ : t`Connected to an AI provider`;+};diff --git a/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.unit.spec.tsx b/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.unit.spec.tsxindex bcf6a9ca10a..d585974d761 100644--- a/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.unit.spec.tsx+++ b/frontend/src/metabase/setup/components/AIConfigStep/AIConfigStep.unit.spec.tsx@@ -3,8 +3,11 @@ import fetchMock from "fetch-mock";import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import {+ setupCreateLlmProviderEndpoint,+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,setupMetabaseManagedAiEndpoints,- setupMetabotSettingsEndpoint,setupPropertiesEndpoints,} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";@@ -17,11 +20,16 @@ import {} from "metabase/redux/store/mocks";import type {EnterpriseSettings,- SettingDefinition,+ LlmProviderConnection,+ LlmProviderType,TokenFeatures,} from "metabase-types/api";import {- createMockSettingDefinition,+ createMockLlmConnectionModels,+ createMockLlmModel,+ createMockLlmProviderConnection,+ createMockLlmProviderField,+ createMockLlmProviderType,createMockSettings,createMockTokenFeatures,createMockUser,@@ -29,12 +37,68 @@ import {import { AIConfigStep } from "./AIConfigStep";+const ANTHROPIC_TYPE = createMockLlmProviderType({+ type: "anthropic",+ label: "Anthropic",+ fields: [+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ prefix: "sk-ant-",+ }),+ ],+});++const OPENAI_TYPE = createMockLlmProviderType({+ type: "openai",+ label: "OpenAI",+ fields: [+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ }),+ ],+});++const METABASE_TYPE = createMockLlmProviderType({+ type: "metabase",+ label: "Metabase AI service",+ managed: true,+ singleton: true,+ fields: [],+});++const ANTHROPIC_CONNECTION = createMockLlmProviderConnection({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+});++const OPENAI_CONNECTION = createMockLlmProviderConnection({+ key: "openai",+ type: "openai",+ name: "OpenAI",+});++const METABASE_CONNECTION = createMockLlmProviderConnection({+ key: "metabase",+ type: "metabase",+ name: "Metabase AI service",+});+interface SetupOpts {step?: SetupStep;settings?: Partial<EnterpriseSettings>;tokenFeatures?: Partial<TokenFeatures>;hasMetabotPlugin?: boolean;- settingOverrides?: SettingDefinition[];+ providerTypes?: LlmProviderType[];+ providerTypesFail?: boolean;+ connections?: LlmProviderConnection[];+ createdConnection?: LlmProviderConnection;}const setup = ({@@ -42,72 +106,72 @@ const setup = ({settings = {},tokenFeatures = {},hasMetabotPlugin = false,- settingOverrides = [],+ providerTypes = [ANTHROPIC_TYPE, OPENAI_TYPE],+ providerTypesFail = false,+ connections = [],+ createdConnection = ANTHROPIC_CONNECTION,}: SetupOpts = {}) => {+ fetchMock.removeRoutes();+ fetchMock.clearHistory();++ const sessionProperties = createMockSettings({+ ...settings,+ "token-features": createMockTokenFeatures(tokenFeatures),+ });+const state = createMockState({currentUser: createMockUser({ is_superuser: true }),setup: createMockSetupState({ step, isAiConfigRequested: true }),- settings: mockSettings({- ...settings,- "token-features": createMockTokenFeatures(tokenFeatures),- }),+ settings: mockSettings(sessionProperties),});if (hasMetabotPlugin) {setupEnterpriseOnlyPlugin("metabot");+ setupMetabaseManagedAiEndpoints();+ fetchMock.post("path:/api/premium-features/token/refresh", 200);}- // Served through closures so tests can mutate them to simulate the backend- // state changing after a connect.- const sessionProperties = createMockSettings({- ...settings,- "token-features": createMockTokenFeatures(tokenFeatures),- });- const settingDefinitions = [...settingOverrides];-- fetchMock.get("path:/api/setting", () => settingDefinitions);+ fetchMock.get("path:/api/setting", []);setupPropertiesEndpoints(sessionProperties);+ if (providerTypesFail) {+ fetchMock.get("path:/api/llm/provider-types", {+ status: 500,+ body: { message: "Provider types are unavailable" },+ });+ } else {+ setupLlmProviderTypesEndpoint(providerTypes);+ }+ setupLlmProvidersEndpoint(connections);+ setupLlmModelsEndpoint([+ createMockLlmConnectionModels({+ key: "anthropic",+ name: "Anthropic",+ type: "anthropic",+ models: [+ createMockLlmModel({+ id: "claude-haiku-4-5",+ display_name: "Claude Haiku 4.5",+ }),+ ],+ }),+ ]);+ setupCreateLlmProviderEndpoint(createdConnection);renderWithProviders(<AIConfigStep stepLabel={5} />, {storeInitialState: state,});-- return { sessionProperties, settingDefinitions };};-const ANTHROPIC_MODELS = [- { id: "claude-haiku-4-5", display_name: "Claude Haiku 4.5" },- { id: "claude-sonnet-4-6", display_name: "Claude Sonnet 4.6" },-];--const connectAnthropic = async ({- sessionProperties,- settingDefinitions,-}: ReturnType<typeof setup>) => {- setupMetabotSettingsEndpoint({- provider: "anthropic",- response: { value: "anthropic/claude-haiku-4-5", models: ANTHROPIC_MODELS },- });- fetchMock.put("path:/api/metabot/settings", () => {- sessionProperties["llm-metabot-provider"] = "anthropic/claude-haiku-4-5";- sessionProperties["llm-metabot-configured?"] = true;- settingDefinitions.push(- createMockSettingDefinition({- key: "llm-anthropic-api-key",- value: "**********ey",- }),- );- return { value: "anthropic/claude-haiku-4-5", models: ANTHROPIC_MODELS };- });-- await userEvent.click(screen.getByLabelText("Provider"));+const connectAnthropic = async () => {await userEvent.click(- await screen.findByRole("option", { name: "Anthropic" }),+ await screen.findByRole("button", { name: "Anthropic" }),);await userEvent.type(- screen.getByLabelText("API key"),+ screen.getByLabelText(/API key/),"sk-ant-api03-unit-test-key",);++ setupLlmProvidersEndpoint([ANTHROPIC_CONNECTION]);await userEvent.click(screen.getByRole("button", { name: "Connect" }));};@@ -120,39 +184,38 @@ describe("AIConfigStep", () => {setup({ step: "db_connection" });expect(screen.getByText("Connect to an AI provider")).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Anthropic" }),+ ).not.toBeInTheDocument();});it("should not offer the managed provider without access to it", async () => {setup();- await userEvent.click(screen.getByLabelText("Provider"));-expect(- await screen.findByRole("option", { name: "Anthropic" }),+ await screen.findByRole("button", { name: "Anthropic" }),).toBeInTheDocument();- expect(screen.getByRole("option", { name: /OpenAI/ })).toBeInTheDocument();+ expect(screen.getByRole("button", { name: "OpenAI" })).toBeInTheDocument();expect(- screen.queryByRole("option", { name: "Metabase" }),+ screen.queryByRole("button", { name: "Metabase AI service" }),).not.toBeInTheDocument();});- it("should not preselect a provider without access to the managed provider", () => {- setup();-- expect(screen.getByLabelText("Provider")).toHaveValue("");- });-- it("should suggest the managed provider when the instance has access to it", async () => {- setupMetabaseManagedAiEndpoints();+ it("should offer the managed provider when the instance has access to it", async () => {setup({tokenFeatures: { "offer-metabase-ai-managed": true },hasMetabotPlugin: true,+ providerTypes: [METABASE_TYPE, ANTHROPIC_TYPE, OPENAI_TYPE],});- expect(await screen.findByLabelText("Provider")).toHaveValue("Metabase");+ await userEvent.click(+ await screen.findByRole("button", { name: "Metabase AI service" }),+ );+expect(- await screen.findByText("About Metabase AI service"),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();expect(await screen.findByRole("checkbox", {@@ -161,38 +224,18 @@ describe("AIConfigStep", () => {).toBeInTheDocument();});- it("should show the existing connection instead of the managed suggestion when already connected", async () => {- setupMetabaseManagedAiEndpoints();- setupMetabotSettingsEndpoint({- provider: "anthropic",- response: { value: "anthropic/claude-haiku-4-5", models: [] },- });+ it("should show the model picker instead of the provider picker when already connected", async () => {setup({- tokenFeatures: { "offer-metabase-ai-managed": true },- hasMetabotPlugin: true,- settings: {- "llm-metabot-configured?": true,- "llm-metabot-provider": "anthropic/claude-haiku-4-5",- },- settingOverrides: [- createMockSettingDefinition({- key: "llm-metabot-provider",- value: "anthropic/claude-haiku-4-5",- }),- createMockSettingDefinition({- key: "llm-anthropic-api-key",- value: "**********45",- }),- ],+ connections: [ANTHROPIC_CONNECTION],+ settings: { "llm-metabot-provider": "anthropic/claude-sonnet-4-6" },});expect(await screen.findByRole("button", { name: "Done" }),).toBeInTheDocument();expect(- screen.queryByText("About Metabase AI service"),+ screen.queryByRole("button", { name: "Anthropic" }),).not.toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();expect(screen.queryByRole("button", { name: "I'll set this up later" }),).not.toBeInTheDocument();@@ -202,97 +245,138 @@ describe("AIConfigStep", () => {setup();await userEvent.click(- screen.getByRole("button", { name: "I'll set this up later" }),+ await screen.findByRole("button", { name: "I'll set this up later" }),);expect(await screen.findByText("I'll set up AI later")).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Anthropic" }),+ ).not.toBeInTheDocument();});it("should show the model picker after connecting a provider", async () => {- const mocks = setup();+ setup();- await connectAnthropic(mocks);+ await connectAnthropic();expect(await screen.findByLabelText("Model")).toBeInTheDocument();expect(await screen.findByRole("button", { name: "Done" }),).toBeInTheDocument();- expect(- screen.queryByRole("button", { name: "I'll set this up later" }),- ).not.toBeInTheDocument();});it("should advance to the next step after confirming the connection", async () => {- const mocks = setup();+ // the properties the client refetches after connecting already carry the reference the+ // backend wrote when it pointed Metabot at the new connection+ setup({+ settings: { "llm-metabot-provider": "anthropic/claude-sonnet-4-6" },+ });- await connectAnthropic(mocks);+ await connectAnthropic();await userEvent.click(await screen.findByRole("button", { name: "Done" }));expect(await screen.findByText("Connected to Anthropic"),).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();expect(screen.queryByLabelText("Model")).not.toBeInTheDocument();});it("should connect the managed provider and advance to the next step", async () => {- setupMetabaseManagedAiEndpoints();- fetchMock.post("path:/api/premium-features/token/refresh", 200);-- const { sessionProperties } = setup({+ setup({+ settings: {+ "llm-metabot-provider": "metabase/anthropic/claude-sonnet-4-6",+ },tokenFeatures: {"offer-metabase-ai-managed": true,"metabase-ai-managed": true,},hasMetabotPlugin: true,+ providerTypes: [METABASE_TYPE, ANTHROPIC_TYPE, OPENAI_TYPE],+ createdConnection: METABASE_CONNECTION,});- fetchMock.put("path:/api/metabot/settings", () => {- sessionProperties["llm-metabot-provider"] =- "metabase/anthropic/claude-sonnet-4-6";- sessionProperties["llm-metabot-configured?"] = true;- return { value: "metabase/anthropic/claude-sonnet-4-6", models: [] };- });-+ await userEvent.click(+ await screen.findByRole("button", { name: "Metabase AI service" }),+ );expect(- await screen.findByText("About Metabase AI service"),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();- expect(- screen.queryByRole("checkbox", {- name: /I agree with the Metabase AI Service/i,- }),- ).not.toBeInTheDocument();+ setupLlmProvidersEndpoint([METABASE_CONNECTION]);await userEvent.click(screen.getByRole("button", { name: "Connect" }));expect(- await screen.findByText("Connected to Metabase"),+ await screen.findByText("Connected to Metabase AI service"),).toBeInTheDocument();- expect(screen.queryByLabelText("Provider")).not.toBeInTheDocument();expect(- fetchMock.callHistory.called("path:/api/metabot/settings", {- method: "PUT",- body: { provider: "metabase", model: "" },+ fetchMock.callHistory.called("path:/api/llm/providers", {+ method: "POST",+ body: { type: "metabase" },}),).toBe(true);});- it("should show the connected provider when completed after connecting", () => {+ it("should show the connected provider when completed after connecting", async () => {setup({step: "completed",- settings: {- "llm-metabot-configured?": true,- "llm-metabot-provider": "anthropic/claude-haiku-4-5",- },+ connections: [ANTHROPIC_CONNECTION],+ settings: { "llm-metabot-provider": "anthropic/claude-sonnet-4-6" },});- expect(screen.getByText("Connected to Anthropic")).toBeInTheDocument();+ expect(+ await screen.findByText("Connected to Anthropic"),+ ).toBeInTheDocument();+ });++ it("should report the provider the picked model belongs to, not the first usable connection", async () => {+ setup({+ step: "completed",+ connections: [ANTHROPIC_CONNECTION, OPENAI_CONNECTION],+ settings: { "llm-metabot-provider": "openai/gpt-5.4" },+ });++ expect(await screen.findByText("Connected to OpenAI")).toBeInTheDocument();});- it("should show the skipped title when completed without connecting", () => {+ it("should show the skipped title when completed without connecting", async () => {setup({ step: "completed" });- expect(screen.getByText("I'll set up AI later")).toBeInTheDocument();+ expect(await screen.findByText("I'll set up AI later")).toBeInTheDocument();+ });++ it("should report a failure to load the providers rather than an empty picker", async () => {+ setup({ providerTypesFail: true });++ expect(+ await screen.findByText("Provider types are unavailable"),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Anthropic" }),+ ).not.toBeInTheDocument();+ });++ it("should offer a way out when only an unusable connection is listed", async () => {+ setup({+ connections: [+ createMockLlmProviderConnection({+ key: "anthropic",+ type: "anthropic",+ name: "Anthropic",+ usable: false,+ }),+ ],+ });++ expect(+ await screen.findByRole("button", { name: "I'll set this up later" }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: "Anthropic" }),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Done" }),+ ).not.toBeInTheDocument();});});diff --git a/frontend/src/metabase/setup/tests/enterprise.unit.spec.tsx b/frontend/src/metabase/setup/tests/enterprise.unit.spec.tsxindex 48faa511174..c3347e2ac9d 100644--- a/frontend/src/metabase/setup/tests/enterprise.unit.spec.tsx+++ b/frontend/src/metabase/setup/tests/enterprise.unit.spec.tsx@@ -166,7 +166,7 @@ describe("setup (EE build, but no token)", () => {});describe("AI config step with the managed AI offer", () => {- it("should preselect the managed provider and ask the admin to accept the terms", async () => {+ it("should offer the managed provider and ask the admin to accept the terms", async () => {await setupEnterprise({tokenFeatures: createMockTokenFeatures({hosting: true,@@ -181,11 +181,15 @@ describe("setup (EE build, but no token)", () => {await clickNextStep();await userEvent.click(screen.getByText("Continue with sample data"));await startAiConfigStep();+ await userEvent.click(+ await screen.findByRole("button", { name: "Metabase AI service" }),+ );expect(- await screen.findByText("About Metabase AI service"),+ await screen.findByText(+ /The simplest way to get started with AI in Metabase/,+ ),).toBeInTheDocument();- expect(screen.getByLabelText("Provider")).toHaveValue("Metabase");expect(await screen.findByRole("checkbox", {name: /I agree with the Metabase AI Service/i,diff --git a/frontend/src/metabase/setup/tests/setup.tsx b/frontend/src/metabase/setup/tests/setup.tsxindex 742ecc0e21d..0f69986a9b1 100644--- a/frontend/src/metabase/setup/tests/setup.tsx+++ b/frontend/src/metabase/setup/tests/setup.tsx@@ -8,6 +8,9 @@ import {} from "__support__/enterprise";import {setupCurrentUserEndpoint,+ setupLlmModelsEndpoint,+ setupLlmProviderTypesEndpoint,+ setupLlmProvidersEndpoint,setupPropertiesEndpoints,setupSettingsEndpoints,} from "__support__/server-mocks";@@ -24,6 +27,8 @@ import type {UsageReason,} from "metabase-types/api";import {+ createMockLlmProviderField,+ createMockLlmProviderType,createMockSettings,createMockTokenFeatures,createMockUser,@@ -31,6 +36,28 @@ import {import { Setup } from "../components/Setup";+const ANTHROPIC_TYPE = createMockLlmProviderType({+ type: "anthropic",+ label: "Anthropic",+ fields: [+ createMockLlmProviderField({+ key: "api-key",+ label: "API key",+ type: "password",+ required: true,+ prefix: "sk-ant-",+ }),+ ],+});++const METABASE_TYPE = createMockLlmProviderType({+ type: "metabase",+ label: "Metabase AI service",+ managed: true,+ singleton: true,+ fields: [],+});+export interface SetupOpts {step?: SetupStep;tokenFeatures?: TokenFeatures;@@ -76,6 +103,9 @@ export async function setup({);setupSettingsEndpoints([]);fetchMock.put("path:/api/setting", 200);+ setupLlmProviderTypesEndpoint([ANTHROPIC_TYPE, METABASE_TYPE]);+ setupLlmProvidersEndpoint([]);+ setupLlmModelsEndpoint([]);renderWithProviders(<Setup />, { storeInitialState: state });@@ -174,9 +204,11 @@ export const skipTokenStep = async (name = "Skip") =>await userEvent.click(screen.getByRole("button", { name }));export const startAiConfigStep = async () =>- await userEvent.click(screen.getByRole("button", { name: "Set up AI" }));+ await userEvent.click(+ await screen.findByRole("button", { name: "Set up AI" }),+ );export const skipAiConfigStep = async () =>await userEvent.click(- screen.getByRole("button", { name: "I'll set this up later" }),+ await screen.findByRole("button", { name: "I'll set this up later" }),);diff --git a/frontend/test/__support__/server-mocks/metabot.ts b/frontend/test/__support__/server-mocks/metabot.tsindex 9b648436e32..d402f19420a 100644--- a/frontend/test/__support__/server-mocks/metabot.ts+++ b/frontend/test/__support__/server-mocks/metabot.ts@@ -5,6 +5,9 @@ import fetchMock, {import type { MetabotConversationDetail } from "metabase/metabot/utils/normalize-fetched-chat-messages";import type {+ LlmConnectionModels,+ LlmProviderConnection,+ LlmProviderType,MetabotConversation,MetabotConversationTitleResponse,MetabotGroupLimit,@@ -12,15 +15,14 @@ import type {MetabotId,MetabotInfo,MetabotInstanceLimit,- MetabotSettingsResponse,MetabotTenantLimit,PurchaseCloudAddOnRequest,RegenerateSuggestedMetabotPromptsResponse,SuggestedMetabotPrompt,SuggestedMetabotPromptsResponse,- UpdateMetabotSettingsRequest,UserMetabotPermissionsResponse,} from "metabase-types/api";+import { createMockLlmProviderConnection } from "metabase-types/api/mocks/llm";import { createMockUserMetabotPermissions } from "metabase-types/api/mocks/metabot";const METABASE_MANAGED_AI_PRODUCT_TYPE: PurchaseCloudAddOnRequest["product_type"] =@@ -217,27 +219,82 @@ export function setupMetabotSlackSettingsEndpointWithError();}-export function setupMetabotSettingsEndpoint({- provider,- response,-}: {- provider: UpdateMetabotSettingsRequest["provider"];- response: MetabotSettingsResponse;-}) {- fetchMock.get(`path:/api/metabot/settings?provider=${provider}`, response);+const LLM_PROVIDER_TYPES_ROUTE_NAME = "llm-provider-types";++export function setupLlmProviderTypesEndpoint(+ providerTypes: LlmProviderType[] = [],+) {+ fetchMock.removeRoute(LLM_PROVIDER_TYPES_ROUTE_NAME);+ fetchMock.get("path:/api/llm/provider-types", providerTypes, {+ name: LLM_PROVIDER_TYPES_ROUTE_NAME,+ });+}++const LLM_PROVIDERS_ROUTE_NAME = "llm-providers";++export function setupLlmProvidersEndpoint(+ connections: LlmProviderConnection[] = [],+) {+ fetchMock.removeRoute(LLM_PROVIDERS_ROUTE_NAME);+ fetchMock.get("path:/api/llm/providers", connections, {+ name: LLM_PROVIDERS_ROUTE_NAME,+ });}-export function setupUpdateMetabotSettingsEndpoint(- response: MetabotSettingsResponse,+const LLM_MODELS_ROUTE_NAME = "llm-models";++export function setupLlmModelsEndpoint(+ connections: LlmConnectionModels[] = [],+) {+ fetchMock.removeRoute(LLM_MODELS_ROUTE_NAME);+ fetchMock.get("path:/api/llm/models", connections, {+ name: LLM_MODELS_ROUTE_NAME,+ });+}++const CREATE_LLM_PROVIDER_ROUTE_NAME = "create-llm-provider";++export function setupCreateLlmProviderEndpoint(+ connection: LlmProviderConnection = createMockLlmProviderConnection(),) {- fetchMock.put("path:/api/metabot/settings", response);+ fetchMock.removeRoute(CREATE_LLM_PROVIDER_ROUTE_NAME);+ fetchMock.post("path:/api/llm/providers", connection, {+ name: CREATE_LLM_PROVIDER_ROUTE_NAME,+ });}-export function setupUpdateMetabotSettingsEndpointWithError(+export function setupCreateLlmProviderEndpointWithError(status: number,body: string,) {- fetchMock.put("path:/api/metabot/settings", { status, body });+ fetchMock.removeRoute(CREATE_LLM_PROVIDER_ROUTE_NAME);+ fetchMock.post(+ "path:/api/llm/providers",+ { status, body },+ { name: CREATE_LLM_PROVIDER_ROUTE_NAME },+ );+}++const UPDATE_LLM_PROVIDER_ROUTE_NAME = "update-llm-provider";++export function setupUpdateLlmProviderEndpoint(+ connection: LlmProviderConnection = createMockLlmProviderConnection(),+) {+ fetchMock.removeRoute(UPDATE_LLM_PROVIDER_ROUTE_NAME);+ fetchMock.put("express:/api/llm/providers/:key", connection, {+ name: UPDATE_LLM_PROVIDER_ROUTE_NAME,+ });+}++const DELETE_LLM_PROVIDER_ROUTE_NAME = "delete-llm-provider";++export function setupDeleteLlmProviderEndpoint() {+ fetchMock.removeRoute(DELETE_LLM_PROVIDER_ROUTE_NAME);+ fetchMock.delete(+ "express:/api/llm/providers/:key",+ { status: 204 },+ { name: DELETE_LLM_PROVIDER_ROUTE_NAME },+ );}type SetupMetabaseManagedAiEndpointsOptions = {diff --git a/resources/migrations/064/20260807_llm_provider_connections.yaml b/resources/migrations/064/20260807_llm_provider_connections.yamlnew file mode 100644index 00000000000..996cbf9abce--- /dev/null+++ b/resources/migrations/064/20260807_llm_provider_connections.yaml@@ -0,0 +1,17 @@+## Move the per-provider LLM credential settings (`llm-anthropic-api-key` and friends) onto the+## `llm-providers` connection list, and delete them.+##+## Reversible on purpose: code that predates the list reads those settings directly, so an instance+## rolled back to it would otherwise run on whatever was last written there — a credential that may+## since have been rotated through the new UI — rather than on what is actually configured. The+## rollback writes the list back out into the settings, and rewrites `llm-metabot-provider` when it+## names a connection the old code cannot resolve.++databaseChangeLog:+ - changeSet:+ id: v64.7qmx3p+ author: undrfined+ comment: Migrate the per-provider LLM credential settings onto llm-providers+ changes:+ - customChange:+ class: metabase.app_db.custom_migrations.MigrateLlmProviderSettingsdiff --git a/src/metabase/app_db/custom_migrations.clj b/src/metabase/app_db/custom_migrations.cljindex 0e53c8f2685..edab549a126 100644--- a/src/metabase/app_db/custom_migrations.clj+++ b/src/metabase/app_db/custom_migrations.clj@@ -23,6 +23,7 @@[clojurewerkz.quartzite.triggers :as triggers][medley.core :as m][metabase.app-db.connection :as mdb.connection]+ [metabase.app-db.custom-migrations.llm-providers :as llm-providers][metabase.app-db.custom-migrations.metrics-v2 :as metrics-v2][metabase.app-db.custom-migrations.pulse-to-notification :as pulse-to-notification][metabase.app-db.custom-migrations.reserve-at-symbol-user-attributes :as reserve-at-symbol-user-attributes]@@ -2237,3 +2238,7 @@:where [:and[:= :provider "totp"][:= :confirmed_at nil]]})))++(define-reversible-migration MigrateLlmProviderSettings+ (llm-providers/migrate-up!)+ (llm-providers/migrate-down!))diff --git a/src/metabase/app_db/custom_migrations/llm_providers.clj b/src/metabase/app_db/custom_migrations/llm_providers.cljnew file mode 100644index 00000000000..e3101d19734--- /dev/null+++ b/src/metabase/app_db/custom_migrations/llm_providers.clj@@ -0,0 +1,209 @@+(ns metabase.app-db.custom-migrations.llm-providers+ "Moves the per-provider LLM credential settings onto the `llm-providers` connection list.++ Before the list existed, an instance configured one provider per settings group — `llm-anthropic-api-key` and+ friends — and Metabot named it by provider type. The list supersedes those settings, so the migration copies them+ into it and deletes them: a credential nothing writes to any more should not sit in the app DB, and a build rolled+ back to code that still reads those settings would otherwise pick up whatever was last written there rather than+ what is actually configured. The rollback writes the list back out into them for the same reason.++ The provider table below is a frozen copy of `metabase.llm.provider`'s registry as it stood when this was written.+ Migrations must not call application code — see the `metabase.app-db.custom-migrations` namespace docstring."+ (:require+ [clojure.string :as str]+ [metabase.util.encryption :as encryption]+ [metabase.util.json :as json]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(def ^:private connections-setting "llm-providers")+(def ^:private model-ref-setting "llm-metabot-provider")+(def ^:private managed-type "metabase")++(def ^:private provider-types+ "Every provider type that could be configured by its own settings, in the order the connection list shows them.++ `:settings` maps a `:config` key to the setting that carried it. `:required-keys` are the keys that have to be+ present for the provider to count as configured — the rest are optional, or carry a default of their own.+ `:model-fields` name the config keys that compose the model such a connection serves, which for Azure is where its+ `{family}/{deployment}` pair ends up."+ [{:type "anthropic"+ :name "Anthropic"+ :settings {:api-key "llm-anthropic-api-key" :base-url "llm-anthropic-api-base-url"}+ :required-keys [:api-key]}+ {:type "openai"+ :name "OpenAI"+ :settings {:api-key "llm-openai-api-key" :base-url "llm-openai-api-base-url"}+ :required-keys [:api-key]}+ {:type "openrouter"+ :name "OpenRouter"+ :settings {:api-key "llm-openrouter-api-key" :base-url "llm-openrouter-api-base-url"}+ :required-keys [:api-key]}+ {:type "mistral"+ :name "Mistral"+ :settings {:api-key "llm-mistral-api-key" :base-url "llm-mistral-api-base-url"}+ :required-keys [:api-key]}+ {:type "zai"+ :name "Z.AI"+ :settings {:api-key "llm-zai-api-key" :base-url "llm-zai-api-base-url"}+ :required-keys [:api-key]}+ {:type "moonshot"+ :name "Moonshot AI"+ :settings {:api-key "llm-moonshot-api-key" :base-url "llm-moonshot-api-base-url"}+ :required-keys [:api-key]}+ {:type "azure"+ :name "Microsoft Azure"+ :settings {:api-key "llm-azure-api-key"+ :base-url "llm-azure-api-base-url"+ :model-family "llm-azure-model-family"+ :deployment-name "llm-azure-deployment-name"}+ :required-keys [:api-key :base-url]+ :model-fields [:model-family :deployment-name]}+ {:type "google"+ :name "Google Gemini Enterprise"+ :settings {:service-account-key "llm-google-service-account-key"+ :oauth-access-token "llm-google-oauth-access-token"+ :project-id "llm-google-project-id"+ :location "llm-google-location"+ :base-url "llm-google-api-base-url"}+ ;; either credential will do, so completeness is checked against the pair rather than against every key+ :required-keys []+ :any-of [:service-account-key :oauth-access-token]}+ {:type "bedrock"+ :name "Amazon Bedrock"+ :settings {:access-key-id "llm-bedrock-access-key-id"+ :secret-access-key "llm-bedrock-secret-access-key"+ :session-token "llm-bedrock-session-token"+ :region "llm-bedrock-region"}+ :required-keys [:access-key-id :secret-access-key]}])++(def ^:private credential-setting-keys+ (into [] (mapcat (comp vals :settings)) provider-types))++(def ^:private sensitive-settings+ "The settings the pre-migration code stored encrypted (`:sensitive? true`). The downgrade writes them back the+ same way, and leaves the rest plaintext to match their `:encryption :no` declarations."+ #{"llm-anthropic-api-key" "llm-openai-api-key" "llm-openrouter-api-key" "llm-mistral-api-key"+ "llm-zai-api-key" "llm-moonshot-api-key" "llm-azure-api-key"+ "llm-google-service-account-key" "llm-google-oauth-access-token"+ "llm-bedrock-access-key-id" "llm-bedrock-secret-access-key" "llm-bedrock-session-token"})++(defn- setting-value+ [setting-key]+ (some-> (t2/query-one {:select [:value] :from :setting :where [:= :key setting-key]})+ :value+ encryption/maybe-decrypt))++(defn- non-blank+ [value]+ (when (string? value)+ (not-empty (str/trim value))))++(defn- write-setting!+ "Writes `value` for `setting-key`, encrypted only when `encrypt?` — a setting declared `:encryption :no` should+ not come out of a downgrade holding ciphertext, even though reads would decrypt it opportunistically."+ [setting-key value encrypt?]+ (t2/query {:delete-from :setting :where [:= :key setting-key]})+ (t2/insert! :setting {:key setting-key :value (cond-> value encrypt? encryption/maybe-encrypt)}))++;;; ------------------------------------------------------ Up ------------------------------------------------------++(defn- stored-config+ [{:keys [settings]}]+ (into {} (keep (fn [[config-key setting-key]]+ (when-let [value (non-blank (setting-value setting-key))]+ [config-key value])))+ settings))++(defn- model-fields-from-model-ref+ "Azure's deployment lived only in `llm-metabot-provider` before it had settings of its own, so a connection whose+ model reference already names it takes its `{family}/{deployment}` pair from there."+ [{:keys [type model-fields]} config model-ref]+ (let [[ref-key ref-model] (some-> (non-blank model-ref) (str/split #"/" 2))+ parts (when (and (seq model-fields) (= type ref-key) ref-model)+ (str/split ref-model #"/" (count model-fields)))]+ (if (= (count model-fields) (count parts))+ (merge config (zipmap model-fields parts))+ config)))++(defn- adopted-connections+ [model-ref]+ (into (if (str/starts-with? (str model-ref) (str managed-type "/"))+ [{:key managed-type :type managed-type :name "Metabase AI service" :config {}}]+ [])+ (keep (fn [{:keys [type name required-keys any-of] :as provider}]+ (let [config (stored-config provider)]+ (when (and (every? config required-keys)+ (or (empty? any-of) (some config any-of)))+ {:key type+ :type type+ :name name+ :config (model-fields-from-model-ref provider config model-ref)}))))+ provider-types))++(defn migrate-up!+ "Copy the per-provider credential settings onto `llm-providers`, then delete them.++ The list wins when it already holds something: an instance that has configured connections has already moved on+ from these settings, and the values left in them are a stale snapshot at best."+ []+ (when-not (setting-value connections-setting)+ (let [conns (adopted-connections (setting-value model-ref-setting))]+ (when (seq conns)+ (log/infof "Migrating %d LLM provider credential setting(s) onto %s: %s"+ (count conns) connections-setting (str/join ", " (map :key conns)))+ ;; the list holds every credential, and the live setting is declared :when-encryption-key-set+ (write-setting! connections-setting (json/encode conns) true))))+ (t2/query {:delete-from :setting :where [:in :key credential-setting-keys]}))++;;; ----------------------------------------------------- Down -----------------------------------------------------++(defn- connection-settings+ "The setting rows that carry `conn`'s credentials for code that predates the connection list, or nil when there is+ nothing to write them to."+ [{:keys [key type config]}]+ (when-let [provider (and (= key type) (first (filter #(= type (:type %)) provider-types)))]+ ;; not-empty, not an empty seq: a known-type connection whose config is entirely blank writes nothing, and an+ ;; empty seq is truthy — it would silently skip both the settings and the dropped-with-a-warning contract+ (not-empty+ (keep (fn [[config-key setting-key]]+ (when-let [value (non-blank (get config config-key))]+ [setting-key value]))+ (:settings provider)))))++(defn- downgraded-model-ref+ "A model reference code that predates the connection list can resolve: it addresses providers by type, so a+ connection key that is not one leaves Metabot pointed at nothing. Nil drops the setting, falling back to its+ default."+ [model-ref conns]+ (when-let [[conn-key model] (some-> (non-blank model-ref) (str/split #"/" 2))]+ (cond+ (= managed-type conn-key) model-ref+ (some #(= conn-key (:type %)) provider-types) model-ref+ :else (when-let [type (:type (first (filter #(= conn-key (:key %)) conns)))]+ (when (some #(= type (:type %)) provider-types)+ (str type "/" model))))))++(defn migrate-down!+ "Write the connection list back into the per-provider settings, and drop the list so an upgrade re-reads them.++ Only a connection keyed by its own provider type can be represented: that is all the settings can express, and it+ is what the list holds for anything this migration created. Connections beyond that are dropped with a warning —+ there is nowhere to put them."+ []+ (when-let [conns (some-> (setting-value connections-setting) json/decode+kw)]+ (let [written (into {} (mapcat connection-settings) conns)+ ;; the managed connection is not dropped in any meaningful sense: it holds no credentials, and the+ ;; model reference naming it survives the rollback, so it is simply re-materialized on upgrade+ dropped (remove #(or (= managed-type (:type %)) (connection-settings %)) conns)]+ (doseq [[setting-key value] written]+ (write-setting! setting-key value (contains? sensitive-settings setting-key)))+ (when (seq dropped)+ (log/warnf "Dropping %d LLM provider connection(s) with no equivalent setting: %s"+ (count dropped) (str/join ", " (map :key dropped))))+ (if-let [model-ref (downgraded-model-ref (setting-value model-ref-setting) conns)]+ (write-setting! model-ref-setting model-ref false)+ (t2/query {:delete-from :setting :where [:= :key model-ref-setting]}))+ (t2/query {:delete-from :setting :where [:= :key connections-setting]}))))diff --git a/src/metabase/llm/api.clj b/src/metabase/llm/api.cljindex 2364a5dc341..1576562dec2 100644--- a/src/metabase/llm/api.clj+++ b/src/metabase/llm/api.clj@@ -10,11 +10,10 @@[metabase.api.util.handlers :as handlers][metabase.driver :as driver][metabase.llm.anthropic :as llm.anthropic]+ [metabase.llm.api.provider][metabase.llm.context :as llm.context][metabase.llm.settings :as llm.settings][metabase.metabot.core :as metabot]- [metabase.metabot.self :as metabot.self]- [metabase.metabot.settings :as metabot.settings][metabase.request.core :as request][metabase.util :as u][metabase.util.i18n :refer [tru]]@@ -86,23 +85,6 @@:result result}api/*current-user-id*))-(api.macros/defendpoint :get "/list-models"- :- [:map [:models [:sequential [:map- [:id :string]- [:display_name :string]]]]]- "List available LLM models from the configured provider.-- Requires LLM to be configured for the selected provider in admin settings."- [_route-params- _query-params]- (when-not (metabot.settings/llm-metabot-configured?)- (throw (ex-info (tru "LLM is not configured. Please configure the selected provider in admin settings.")- {:status-code 403})))- (let [provider-and-model (metabot.settings/llm-metabot-provider)- ai-proxy? (metabot/metabase-provider? provider-and-model)- provider (metabot/provider-and-model->provider provider-and-model)]- (metabot.self/list-models provider {:ai-proxy? ai-proxy?})))-(def ^:private table-with-columns-schema"Schema for table metadata with columns returned by /extract-sources."[:map@@ -272,4 +254,5 @@(def ^{:arglists '([request respond raise])} routes"`/api/llm` routes."(handlers/routes+ (+auth metabase.llm.api.provider/routes)(api.macros/ns-handler *ns* +auth)))diff --git a/src/metabase/llm/api/provider.clj b/src/metabase/llm/api/provider.cljnew file mode 100644index 00000000000..7e39c1d693e--- /dev/null+++ b/src/metabase/llm/api/provider.clj@@ -0,0 +1,447 @@+(ns metabase.llm.api.provider+ "Admin endpoints for managing LLM provider connections at `/api/llm`.++ Connections are stored in the `llm-providers` setting, but the setting is never edited directly from the client:+ these endpoints present it as a collection of rows so secrets can be masked on the way out and preserved on the+ way back in, and so a connection is only saved once its credentials have been shown to work."+ (:require+ [clojure.core.cache :as cache]+ [clojure.core.cache.wrapped :as cache.wrapped]+ [clojure.string :as str]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.llm.provider :as llm.provider]+ [metabase.metabot.self :as metabot.self]+ [metabase.metabot.settings :as metabot.settings]+ [metabase.permissions.core :as perms]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.settings.core :as setting]+ [metabase.util.i18n :refer [tru]]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++;;; --------------------------------------------------- Schemas ----------------------------------------------------++(def ^:private field-response-schema+ [:map+ [:key :string]+ [:label :string]+ [:type [:enum "text" "password" "select" "segmented" "file"]]+ [:required :boolean]+ [:advanced :boolean]+ [:placeholder {:optional true} [:maybe :string]]+ [:default {:optional true} [:maybe :string]]+ [:help {:optional true} [:maybe :string]]+ [:docs_url {:optional true} [:maybe :string]]+ [:prefix {:optional true} [:maybe :string]]+ [:options {:optional true} [:maybe [:sequential [:map [:value :string] [:label :string]]]]]+ ;; the field is only shown, and only sent, while the named field holds this value+ [:show_when {:optional true} [:maybe [:map [:field :string] [:value :string]]]]])++(def ^:private provider-type-response-schema+ [:map+ [:type :string]+ [:label :string]+ [:managed :boolean]+ [:singleton :boolean]+ [:available :boolean]+ [:default_model [:maybe :string]]+ ;; the fixed catalog, for types whose models cannot be listed from the provider; the connection form offers+ ;; these so the connect-time credential probe runs against the model the admin actually wants+ [:models [:sequential [:map [:id :string] [:display_name :string]]]]+ ;; alternative credential groups: the connection is complete when one group is filled in full+ [:required_any [:sequential [:sequential :string]]]+ [:fields [:sequential field-response-schema]]])++(def ^:private connection-response-schema+ [:map+ [:key :string]+ [:type :string]+ [:name :string]+ [:source [:enum "db" "env"]]+ [:usable :boolean]+ [:env_vars [:sequential :string]]+ [:env_fields [:sequential :string]]+ [:config [:map-of :keyword [:maybe :string]]]])++(def ^:private llm-model-response-schema+ [:map+ [:id :string]+ [:display_name :string]])++(def ^:private models-response-schema+ [:sequential+ [:map+ [:key :string]+ [:name :string]+ [:type :string]+ [:models [:sequential llm-model-response-schema]]+ [:error {:optional true} [:maybe :string]]]])++(def ^:private config-schema+ [:map-of :keyword [:maybe :string]])++(def ^:private connection-key-schema+ [:string {:api/regex #"[a-z0-9][a-z0-9-]*"}])++;;; -------------------------------------------------- Responses ---------------------------------------------------++(defn- option-response+ [{:keys [value label]}]+ {:value value :label (str label)})++(defn- field-response+ [{:keys [key label type required? advanced? placeholder default help docs-url prefix options show-when]}]+ (cond-> {:key (name key)+ :label (str label)+ :type (name type)+ :required (boolean required?)+ :advanced (boolean advanced?)}+ placeholder (assoc :placeholder (str placeholder))+ default (assoc :default default)+ help (assoc :help (str help))+ docs-url (assoc :docs_url docs-url)+ prefix (assoc :prefix prefix)+ options (assoc :options (mapv option-response options))+ show-when (assoc :show_when {:field (name (:field show-when)) :value (:value show-when)})))++(defn- provider-type-response+ [{:keys [type label managed? singleton? default-model required-any fields]}]+ {:type type+ :label (str label)+ :managed (boolean managed?)+ :singleton (boolean singleton?)+ :available (llm.provider/type-available? type)+ :default_model default-model+ :models (mapv #(select-keys % [:id :display_name]) (llm.provider/fixed-models type))+ :required_any (mapv #(mapv name %) required-any)+ :fields (mapv field-response fields)})++(defn- connection-response+ [{conn-key :key conn-name :name :keys [type source config env-vars env-fields] :as conn}]+ {:key conn-key+ :type type+ :name conn-name+ :source (name (or source :db))+ :usable (llm.provider/config-complete? type config)+ :env_vars (vec env-vars)+ ;; the config keys the environment owns; the form disables exactly these inputs+ :env_fields (mapv name env-fields)+ :config (or (:config (llm.provider/redact conn)) {})})++;;; ------------------------------------------------ Model listing -------------------------------------------------++(defn- provider-client-error?+ "Whether a provider api-error is a client-side 4xx we should surface rather than treat as an outage. Covers+ rejected or missing credentials (401/403) and a request the provider refused outright."+ [error]+ (let [{:keys [api-error status status-code]} (ex-data error)+ status (or status status-code)]+ (and api-error (number? status) (<= 400 status 499))))++(defn- list-connection-models*+ "List `conn`'s models. `config-override` stands in for the stored credentials when validating a connection that has+ not been saved yet. Returns `{:models [...]}` or `{:models [] :error msg}` for a credential-level failure.++ The managed provider is answered from the registry — it serves one model through the proxy, there is nothing to+ fetch, and calling out would fail on instances that cannot reach it.++ A type whose catalog is fixed but whose credentials are its own (Google, whose listing endpoint reports models+ that are not really available) still makes the call, because it is what verifies those credentials; the catalog+ comes from the registry and the call is made against its first model.++ A type that names its model in `:config` (Azure, whose deployments its listing endpoint does not return) makes+ the call for the same reason, but the model it serves comes from the connection rather than from the empty list+ that comes back."+ [{:keys [type config]} config-override model]+ (let [fixed (llm.provider/fixed-models type)]+ (if (llm.provider/managed-type? type)+ {:models (vec fixed)}+ (let [config (llm.provider/with-field-defaults type (or config-override config))+ configured-model (llm.provider/connection-model type config)+ model (or model configured-model (:id (first fixed)))]+ (try+ (let [listed (:models (metabot.self/list-models type (cond-> {:credentials config}+ model (assoc :model model))))]+ {:models (cond+ configured-model [{:id configured-model :display_name (last (str/split configured-model #"/"))}]+ fixed (vec fixed)+ :else (vec listed))})+ (catch clojure.lang.ExceptionInfo e+ (if (provider-client-error? e)+ {:models [] :error (.getMessage e)}+ (throw e))))))))++(def ^:private models-cache-ttl-ms+ "How long a connection's model list is reused. Long enough that an admin page load does not fan out to every+ provider on every render, short enough that a model a provider has just granted shows up without a restart."+ 60000)++(defonce ^:private models-cache+ (atom (cache/ttl-cache-factory {} :ttl models-cache-ttl-ms)))++(defn- models-cache-key+ "What a cached model list is filed under. The config is reduced to a hash rather than held as-is: it carries the+ connection's API key, and a cache entry outlives the connection that produced it. Hashing it also retires the+ entry the moment a credential is rotated, instead of serving the old list until the TTL runs out."+ [{conn-key :key :keys [type config]}]+ [conn-key type (hash config)])++(defn- connection-models-response+ [{conn-key :key conn-name :name :keys [type] :as conn}]+ (merge {:key conn-key :name conn-name :type type}+ (try+ (cache.wrapped/lookup-or-miss models-cache (models-cache-key conn)+ (fn [_] (list-connection-models* conn nil nil)))+ (catch Exception e+ (log/warn e "Failed to list models for LLM provider connection" {:connection conn-key})+ {:models [] :error (.getMessage e)}))))++;;; -------------------------------------------------- Validation --------------------------------------------------++(defn- check-not-env-connection!+ "Throw a 400 when `conn` is configured by environment variables. Writes to env-shadowed settings persist to the+ app DB but the env var wins on every read, so they silently do nothing."+ [{:keys [key source]}]+ (when (= :env (keyword source))+ (throw (ex-info (tru "The {0} connection is configured by environment variables and cannot be changed here."+ (pr-str key))+ {:status-code 400 :connection-key key}))))++(defn- check-connections-not-env-managed!+ []+ (when (some? (setting/env-var-value :llm-providers))+ (throw (ex-info (tru "LLM provider connections are set by the {0} environment variable and cannot be changed via the API."+ (setting/env-var-name :llm-providers))+ {:status-code 400}))))++(defn- without-blank-values+ "Drop `config` entries whose value is blank. The form clears a field it hid by sending an empty string — switching+ Google's authentication method blanks the credential the other method used — and a blank left in the stored config+ would read as present to everything that checks with `some?` rather than `non-blank`."+ [config]+ (into {} (remove (fn [[_ v]] (str/blank? v))) config))++(defn- verify-credentials!+ "Confirm `config` can actually reach `conn`'s provider before anything is persisted, by listing its models.+ Throws a 400 carrying the provider's own message when the credentials are rejected.++ The listing that verified the credentials is seeded into [[models-cache]] under the connection as it will be+ stored, so the model refetch the client fires right after saving is answered from it instead of round-tripping+ to the provider a second time."+ [conn config model]+ (when-not (llm.provider/managed-type? (:type conn))+ (let [{:keys [error] :as listed} (list-connection-models* conn config model)]+ (when error+ (throw (ex-info error {:status-code 400 :api-error true})))+ (swap! models-cache cache/miss (models-cache-key (assoc conn :config config)) listed))))++(defn- connection-model-ref+ "The `connection-key/model` reference that points Metabot at `conn`: the model the connection's own config names+ (Azure's deployment) when it has one, and the type's default model otherwise. Nil when the type neither names nor+ defaults to a model."+ [{conn-key :key :keys [type config]}]+ (when-let [model (or (llm.provider/connection-model type (llm.provider/with-field-defaults type config))+ (llm.provider/default-model type))]+ (str conn-key "/" model)))++(defn- fallback-model-ref+ "A model reference to fall back to once the connection Metabot was pointed at is gone: the first remaining+ connection that names a model, or nil to leave Metabot unconfigured."+ []+ (some connection-model-ref (llm.provider/connections)))++(defenterprise cancel-managed-ai-subscription!+ "Cancel the Metabase Cloud add-on that backs the Metabase-managed provider, called when its connection is+ removed. OSS instances have no subscription to cancel."+ metabase-enterprise.metabot.provider+ []+ nil)++(defn- repoint-metabot!+ "Write `model-ref` as the Metabot selection — unless `MB_LLM_METABOT_PROVIDER` pins it, in which case the write+ would land in the app DB and lose to the env var on every read, leaving the UI claiming a selection the instance+ is not actually using."+ [model-ref]+ (if (setting/env-var-value :llm-metabot-provider)+ (log/warnf "Leaving llm-metabot-provider alone: %s is set by the environment"+ (setting/env-var-name :llm-metabot-provider))+ (setting/set! :llm-metabot-provider model-ref)))++(defn- metabot-has-a-usable-model?+ "Whether `llm-metabot-provider` currently names a connection that can serve requests. Callers must read this+ *before* saving a new connection: the setting's default names the `anthropic` connection, which a freshly saved+ Anthropic connection would satisfy retroactively."+ []+ (llm.provider/connection-usable?+ (llm.provider/model-ref->connection-key (metabot.settings/llm-metabot-provider))))++(defn- select-model-for-new-connection!+ "Point Metabot at a freshly created connection when it had nothing usable to run on, so connecting the first+ provider leaves the instance working rather than connected-but-with-no-model-selected. An existing selection that+ still resolves is left alone — adding a second provider must not silently switch Metabot over to it."+ [{conn-key :key :as conn} requested-model]+ (when-let [model-ref (if (not-empty requested-model)+ (str conn-key "/" requested-model)+ (connection-model-ref conn))]+ (repoint-metabot! model-ref)))++(defn- follow-edited-connection-model!+ "Keep `llm-metabot-provider` on the model an edited connection actually serves.++ A model reference stores the model as a string rather than as a live lookup, so for a type whose model comes from+ its own config — Azure, whose reference bakes in `{family}/{deployment-name}` — editing the connection can leave+ the selection naming a deployment that no longer exists. The connection still reports usable, and the next+ request fails at the provider. For a type with a fixed catalog the model in the edit form is a pick, not a+ probe input: when the selection points at this connection, the pick follows through to it."+ [{conn-key :key :keys [type config] :as conn} requested-model]+ (when (= conn-key (llm.provider/model-ref->connection-key (metabot.settings/llm-metabot-provider)))+ (when-let [model-ref (cond+ (llm.provider/connection-model type (llm.provider/with-field-defaults type config))+ (connection-model-ref conn)++ (and (not-empty requested-model) (seq (llm.provider/fixed-models type)))+ (str conn-key "/" requested-model))]+ (when-not (= model-ref (metabot.settings/llm-metabot-provider))+ (repoint-metabot! model-ref)))))++;;; -------------------------------------------------- Endpoints ---------------------------------------------------++(defn- offer-managed-first+ "Lead with the Metabase-managed provider on instances that can actually use it — it is the option we want admins+ to reach for before bringing their own key. `sort-by` is stable, so everything else keeps its registry order."+ [provider-types]+ (sort-by (fn [{:keys [type managed?]}]+ (if (and managed? (llm.provider/type-available? type)) 0 1))+ provider-types))++(api.macros/defendpoint :get "/provider-types"+ :- [:sequential provider-type-response-schema]+ "List the provider types a connection can be created for, with the credential fields each one needs."+ []+ (perms/check-has-application-permission :setting)+ (mapv provider-type-response (offer-managed-first (llm.provider/provider-types))))++(api.macros/defendpoint :get "/providers"+ :- [:sequential connection-response-schema]+ "List the configured provider connections, with their secrets masked."+ []+ (perms/check-has-application-permission :setting)+ (mapv connection-response (llm.provider/connections)))++(api.macros/defendpoint :post "/providers"+ :- connection-response-schema+ "Create a provider connection. The credentials are verified before the connection is saved."+ [_route-params+ _query-params+ {:keys [type name key config model]} :- [:map+ [:type :string]+ [:name {:optional true} [:maybe :string]]+ [:key {:optional true} [:maybe :string]]+ [:config {:optional true} [:maybe config-schema]]+ [:model {:optional true} [:maybe :string]]]]+ (perms/check-has-application-permission :setting)+ (check-connections-not-env-managed!)+ (let [provider-type (llm.provider/provider-type type)]+ (api/check-400 provider-type (tru "Unknown provider type {0}." (pr-str type)))+ (api/check-400 (llm.provider/type-available? type)+ (tru "The {0} provider is not available on this instance." (pr-str type)))+ (api/check-400 (not (and (llm.provider/singleton-type? type)+ (some #(= type (:type %)) (llm.provider/connections))))+ (tru "The {0} provider is already connected." (pr-str type)))+ (api/check-400 (not (and (llm.provider/managed-type? type)+ (llm.provider/connection llm.provider/managed-connection-key)))+ (tru "Another connection holds the {0} key. Remove it before connecting the Metabase AI service."+ (pr-str llm.provider/managed-connection-key)))+ (let [conn-key (if (llm.provider/managed-type? type)+ llm.provider/managed-connection-key+ (llm.provider/unique-key (or (not-empty key) type)))+ ;; the `metabase/` model-ref prefix means "route through the AI proxy", so a connection of any other+ ;; type holding that key would smuggle its requests into managed billing and usage accounting. Checked+ ;; against the key that will actually be stored, so a value that merely slugs to it cannot slip past.+ _ (api/check-400 (or (llm.provider/managed-type? type)+ (not= llm.provider/managed-connection-key conn-key))+ (tru "The {0} connection key is reserved for the Metabase AI service."+ (pr-str llm.provider/managed-connection-key)))+ config (without-blank-values config)+ conn {:key conn-key+ :type type+ :name (or (not-empty name) (str (:label provider-type)))+ :config config}]+ (llm.provider/validate-config! type config)+ (verify-credentials! conn config model)+ (let [had-usable-model? (metabot-has-a-usable-model?)]+ (llm.provider/set-connections! (conj (llm.provider/stored-connections) conn))+ (when-not had-usable-model?+ (select-model-for-new-connection! conn model)))+ (connection-response (assoc conn :source :db)))))++(api.macros/defendpoint :put "/providers/:key"+ :- connection-response-schema+ "Update a provider connection. Secret fields the client echoes back masked keep their stored value."+ [{conn-key :key} :- [:map [:key connection-key-schema]]+ _query-params+ {:keys [name config model]} :- [:map+ [:name {:optional true} [:maybe :string]]+ [:config {:optional true} [:maybe config-schema]]+ [:model {:optional true} [:maybe :string]]]]+ (perms/check-has-application-permission :setting)+ (check-connections-not-env-managed!)+ (let [stored (llm.provider/stored-connections)+ idx (first (keep-indexed (fn [i c] (when (= (:key c) conn-key) i)) stored))+ _ (api/check-404 idx)+ existing (nth stored idx)+ live (llm.provider/connection conn-key)+ _ (check-not-env-connection! live)+ ;; fields the environment owns are not the client's to edit — the form disables them, and what it echoes+ ;; back for them is the mask of the env value, which must not end up stored+ env-config (select-keys (:config live) (:env-fields live))+ merged (cond-> existing+ (some? config) (assoc :config (without-blank-values+ (llm.provider/merge-config+ (:type existing)+ (:config existing)+ (apply dissoc config (:env-fields live)))))+ (not-empty name) (assoc :name name))+ ;; what the connection will actually run on: the stored config with the environment layered back over it+ effective (merge (:config merged) env-config)]+ (llm.provider/validate-config! (:type merged) effective)+ (verify-credentials! merged effective model)+ (llm.provider/set-connections! (assoc stored idx merged))+ (follow-edited-connection-model! (assoc merged :config effective) model)+ (connection-response (assoc (merge live merged) :config effective))))++(api.macros/defendpoint :delete "/providers/:key" :- :nil+ "Delete a provider connection."+ [{conn-key :key} :- [:map [:key connection-key-schema]]]+ (perms/check-has-application-permission :setting)+ (check-connections-not-env-managed!)+ (let [conn (llm.provider/connection conn-key)]+ (api/check-404 conn)+ (check-not-env-connection! conn)+ (when (llm.provider/managed-type? (:type conn))+ ;; removing the managed connection cancels the Store subscription behind it, and everything else that can+ ;; cancel add-ons is superuser-only — settings access must not be enough to end a paid contract+ (api/check-superuser)+ (cancel-managed-ai-subscription!))+ (let [remaining (vec (remove #(= (:key %) conn-key) (llm.provider/stored-connections)))]+ (llm.provider/set-connections! remaining)+ (when (= conn-key (llm.provider/model-ref->connection-key (metabot.settings/llm-metabot-provider)))+ (repoint-metabot! (fallback-model-ref)))))+ nil)++(api.macros/defendpoint :get "/models"+ :- models-response-schema+ "List the models available from every configured connection, grouped by connection.++ A connection whose credentials are rejected comes back with an empty model list and an `error`, so one bad+ connection does not blank out the others."+ []+ (perms/check-has-application-permission :setting)+ (into []+ (pmap connection-models-response (llm.provider/connections))))++(def ^{:arglists '([request respond raise])} routes+ "`/api/llm` provider routes."+ (api.macros/ns-handler *ns*))diff --git a/src/metabase/llm/provider.clj b/src/metabase/llm/provider.cljnew file mode 100644index 00000000000..32562de19cc--- /dev/null+++ b/src/metabase/llm/provider.clj@@ -0,0 +1,776 @@+(ns metabase.llm.provider+ "LLM provider types and provider connections.++ A *provider type* describes a kind of LLM connection: its label, the credential fields it needs, and its default+ model. The registry below is pure data, so adding a provider type does not mean editing a `case` in every+ namespace that touches credentials, and the admin UI can render a connection form without knowing the type.++ A *connection* is one configured instance of a provider type. Connections live in the [[llm-providers]] setting as+ a list of maps, so an instance can hold several connections of the same type with different credentials:++ {:key \"anthropic-eval\" :type \"anthropic\" :name \"Anthropic (evals)\" :config {:api-key \"sk-ant-...\"}}++ `:key` is a URL-safe slug that identifies the connection. It is what the first segment of a+ `llm-metabot-provider` string refers to, and it defaults to the provider type, so a single-connection instance+ reads exactly as it did when there was one connection per type.++ A *model reference* is the `connection-key/model` string stored in `llm-metabot-provider` and friends;+ [[resolve-model-ref]] turns one into the provider type, model, and credentials an adapter needs."+ (:require+ [clojure.string :as str]+ [metabase.llm.settings :as llm.settings]+ [metabase.settings.core :as setting]+ [metabase.util :as u]+ [metabase.util.i18n :refer [deferred-tru tru]]))++(set! *warn-on-reflection* true)++;;; ------------------------------------------------ Provider types ------------------------------------------------++(defn- strip-trailing-slashes+ "Trim whitespace and trailing slashes from an admin-entered base URL, so joining it with a `/path` cannot produce+ a double slash. The URL is otherwise passed through as entered."+ [value]+ (str/replace (str/trim value) #"/+$" ""))++(def ^:private aws-region-options+ (mapv (fn [region] {:value region :label region})+ (sort llm.settings/known-aws-regions)))++(def ^:private provider-type-registry+ "Every provider type Metabase can connect to, in the order the admin UI offers them.++ `:fields` describes the credential inputs for a connection's `:config` map. A `:password` field is treated as+ secret everywhere: it is masked on the way out of the API and preserved when a client echoes the mask back."+ [{:type "anthropic"+ :label (deferred-tru "Anthropic")+ :default-model "claude-sonnet-4-6"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ :placeholder "sk-ant-api03-..."+ :prefix "sk-ant-"+ :docs-url "https://console.anthropic.com/settings/keys"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://api.anthropic.com"}]}+ {:type "openai"+ :label (deferred-tru "OpenAI")+ :default-model "gpt-5.4"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ :placeholder "sk-proj-..."+ :prefix "sk-"+ :docs-url "https://platform.openai.com/api-keys"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://api.openai.com"}]}+ {:type "openrouter"+ :label (deferred-tru "OpenRouter")+ :default-model "anthropic/claude-sonnet-4.6"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ :placeholder "sk-or-v1-..."+ :prefix "sk-or-v1-"+ :docs-url "https://openrouter.ai/keys"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://openrouter.ai/api"}]}+ {:type "mistral"+ :label (deferred-tru "Mistral")+ :default-model "mistral-medium-3-5"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ ;; Mistral keys have no recognizable prefix.+ :placeholder (deferred-tru "Enter your Mistral API key")+ :docs-url "https://console.mistral.ai/api-keys"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://api.mistral.ai/v1"}]}+ {:type "zai"+ :label (deferred-tru "Z.AI")+ :default-model "glm-5.2"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ ;; Z.AI keys are `{id}.{secret}` pairs with no documented prefix.+ :placeholder (deferred-tru "Enter your Z.AI API key")+ :docs-url "https://z.ai/manage-apikey/apikey-list"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://api.z.ai/api/paas/v4"}]}+ {:type "moonshot"+ :label (deferred-tru "Moonshot AI")+ :default-model "kimi-k3"+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ :placeholder "sk-..."+ :docs-url "https://platform.kimi.ai/console/api-keys"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default "https://api.moonshot.ai/v1"+ :help (deferred-tru "Point this at the .cn platform to use it instead; keys are not interchangeable between the two.")}]}+ {:type "google"+ ;; "Google Gemini Enterprise" (nearly the official "Gemini Enterprise Agent Platform" name), not "Google+ ;; Gemini": the Gemini API is a separate surface with its own credentials, and may become a provider type of+ ;; its own one day.+ :label (deferred-tru "Google Gemini Enterprise")+ :default-model "google/gemini-3.5-flash"+ ;; The Gemini Enterprise Agent Platform has no listing endpoint we can trust — the one it exposes reports models+ ;; that are not really available and omits ones that are — so the models Metabot is known to work with are fixed+ ;; here, and connecting validates the credentials against the model chosen in the connection form with a free+ ;; `countTokens` probe. Which of them a project can actually reach depends on its location.+ :models [{:id "google/gemini-3.5-flash" :display_name "gemini-3.5-flash"}+ {:id "google/gemini-3.6-flash" :display_name "gemini-3.6-flash"}+ {:id "google/gemini-3.7-flash" :display_name "gemini-3.7-flash"}]+ ;; A service account key authenticates on its own (it can carry the project); an OAuth token needs the project+ ;; named beside it.+ :required-any [[:service-account-key] [:oauth-access-token :project-id]]+ :fields [{:key :project-id+ :label (deferred-tru "Project ID")+ :type :text+ :placeholder (deferred-tru "my-project")+ :validate (fn [value]+ (when-not (llm.settings/valid-google-project-id? value)+ (tru "\"{0}\" is not a valid Google Cloud project ID. Use the project ID — 6 to 30 lowercase letters, digits and hyphens — rather than the project name or number." value)))+ :help (deferred-tru "The Google Cloud project to use. Optional if the service account key provides it.")+ :docs-url "https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects"}+ {:key :location+ :label (deferred-tru "Location")+ :type :text+ :placeholder "global"+ :validate (fn [value]+ (when-not (llm.settings/valid-google-location? value)+ (tru "\"{0}\" is not a valid Google Cloud location." value)))+ :help (deferred-tru "Optional. Defaults to global.")}+ {:key :auth-method+ :label (deferred-tru "Authentication method")+ :type :segmented+ :required? true+ :options [{:value "service-account-key" :label (deferred-tru "Service account key")}+ {:value "oauth-token" :label (deferred-tru "OAuth token")}]+ :default "service-account-key"+ :help (deferred-tru "Authenticate with a service account key or an OAuth access token.")}+ {:key :service-account-key+ :label (deferred-tru "Service account key file")+ :type :file+ ;; the file is the whole credential — private key included — so it is redacted like a password+ :sensitive? true+ :show-when {:field :auth-method :value "service-account-key"}+ :placeholder (deferred-tru "Click to select a file")+ :help (deferred-tru "Upload a service account key file to authenticate with.")+ :docs-url "https://docs.cloud.google.com/iam/docs/keys-create-delete"}+ {:key :oauth-access-token+ ;; not "OAuth token", which is what the method above is called: one label per control+ :label (deferred-tru "OAuth access token")+ :type :password+ :show-when {:field :auth-method :value "oauth-token"}+ :placeholder "ya29..."+ :help (deferred-tru "A short-lived token, e.g. the output of gcloud auth print-access-token. Useful for testing.")}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :advanced? true+ :default llm.settings/google-global-api-base-url+ :help (deferred-tru "Derived from the location when left at the global host.")}]}+ {:type "azure"+ :label (deferred-tru "Microsoft Azure")+ :default-model nil+ ;; Azure serves deployments the customer names, and its listing endpoint returns the regional catalog rather+ ;; than those deployments, so there is nothing to fetch. The admin names the one this connection serves, and+ ;; picks the wire family separately rather than typing it as a prefix.+ :model-fields [:model-family :deployment-name]+ :fields [{:key :api-key+ :label (deferred-tru "API key")+ :type :password+ :required? true+ :placeholder (deferred-tru "Enter your Azure API key")+ :docs-url "https://ai.azure.com"}+ {:key :base-url+ :normalize strip-trailing-slashes+ :label (deferred-tru "API base URL")+ :type :text+ :required? true+ :placeholder "https://<resource>.services.ai.azure.com/openai"}+ {:key :model-family+ :label (deferred-tru "Model provider")+ :type :select+ :required? true+ :options [{:value "openai" :label "OpenAI"}+ {:value "anthropic" :label "Anthropic"}]+ :default "openai"+ :help (deferred-tru "Whether your deployment serves an Anthropic or an OpenAI model.")}+ {:key :deployment-name+ :label (deferred-tru "Deployment name")+ :type :text+ :required? true+ :placeholder (deferred-tru "Enter your Azure deployment name")+ :help (deferred-tru "The name of the model deployment on your Azure resource. We recommend naming deployments after the model they serve.")}]}+ {:type "bedrock"+ :label (deferred-tru "Amazon Bedrock")+ :default-model "anthropic.claude-opus-4-8"+ :fields [{:key :access-key-id+ :label (deferred-tru "Access key ID")+ :type :password+ :required? true+ :placeholder "AKIA..."+ :docs-url "https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html"}+ {:key :secret-access-key+ :label (deferred-tru "Secret access key")+ :type :password+ :required? true}+ {:key :region+ :label (deferred-tru "Region")+ :type :select+ :options aws-region-options+ :default "us-east-1"}+ {:key :session-token+ :label (deferred-tru "Session token")+ :type :password+ :advanced? true+ :help (deferred-tru "Only needed for temporary credentials.")}]}+ {:type "metabase"+ :label (deferred-tru "Metabase AI service")+ :managed? true+ :singleton? true+ :default-model "anthropic/claude-sonnet-4-6"+ ;; The proxy serves one benchmarked model rather than a listable catalog, so the models are fixed here instead+ ;; of fetched. This is also the allow-list `llm-metabot-provider` is validated against.+ :models [{:id "anthropic/claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}]+ :fields []}])++(def ^:private provider-type-by-name+ (into {} (map (juxt :type identity)) provider-type-registry))++(defn provider-type+ "The registry entry for `type-name`, or nil when it is not a known provider type."+ [type-name]+ (get provider-type-by-name type-name))++(defn provider-types+ "Every registered provider type."+ []+ provider-type-registry)++(defn managed-type?+ "Whether `type-name` is the Metabase-managed provider, which authenticates with the instance token through the LLM+ proxy instead of with credentials of its own."+ [type-name]+ (boolean (:managed? (provider-type type-name))))++(defn type-available?+ "Whether a connection of this type can currently be created. The managed provider needs the LLM proxy configured;+ everything else is always available."+ [type-name]+ (if (managed-type? type-name)+ (some? (llm.settings/llm-proxy-base-url))+ (some? (provider-type type-name))))++(defn secret-field-keys+ "The `:config` keys of `type-name` that hold secrets: every `:password` field, plus any field marked+ `:sensitive?` — named for the defsetting option it mirrors — regardless of its input type. Google's service+ account key is a file upload, but it is the whole credential."+ [type-name]+ (into #{}+ (comp (filter (fn [{:keys [type sensitive?]}] (or (= :password type) sensitive?)))+ (map :key))+ (:fields (provider-type type-name))))++(defn singleton-type?+ "Whether at most one connection of `type-name` can exist. True for the Metabase-managed provider, which is a+ property of the instance's subscription rather than a credential an admin can hold several of."+ [type-name]+ (boolean (:singleton? (provider-type type-name))))++(defn fixed-models+ "The models `type-name` serves, for types whose catalog is fixed rather than fetched from the provider. Returns nil+ for types whose models are listed over the wire."+ [type-name]+ (:models (provider-type type-name)))++(defn model-fields+ "The `:config` keys whose values compose the model a connection of `type-name` serves, for types whose models+ cannot be listed from the provider. Returns nil for types whose catalog is fetched or fixed."+ [type-name]+ (:model-fields (provider-type type-name)))++(defn default-model+ "The model a new connection of `type-name` starts on, or nil when the type has no sensible default (Azure, whose+ models are deployment names the admin chooses)."+ [type-name]+ (:default-model (provider-type type-name)))++;;; -------------------------------------------------- Validation --------------------------------------------------++(defn connection-model+ "The model `config` names, composed from its type's [[model-fields]] — Azure's `{family}/{deployment}` comes from+ two inputs so the admin picks the family rather than typing it as a prefix. Returns nil for types that list their+ models, and for a connection that has not filled every part in yet."+ [type-name config]+ (when-let [field-keys (seq (model-fields type-name))]+ (let [parts (map #(u/trimmed-string (get config %)) field-keys)]+ (when (every? some? parts)+ (str/join "/" parts)))))++(defn- validate-field!+ [type-name {:keys [key label required? prefix default options validate]} config]+ (let [value (u/trimmed-string (get config key))]+ (when (and required? (not value) (not default))+ (throw (ex-info (tru "{0} is required for {1}." (str label) type-name)+ {:status-code 400 :field key})))+ (when (and value prefix (not (str/starts-with? value prefix)))+ (throw (ex-info (tru "Invalid {0} for {1}. It must start with ''{2}''." (str label) type-name prefix)+ {:status-code 400 :field key})))+ (when (and value (seq options) (not-any? #(= value (:value %)) options))+ (throw (ex-info (tru "Invalid {0} for {1}." (str label) type-name)+ {:status-code 400 :field key})))+ (when-let [problem (and value validate (validate value))]+ (throw (ex-info (str problem) {:status-code 400 :field key})))))++(defn- validate-config-field!+ "Run [[validate-field!]]'s checks for the single field `field-key` of `type-name` against `config`."+ [type-name field-key config]+ (when-let [field (u/find-first-map (:fields (provider-type type-name)) [:key] field-key)]+ (validate-field! type-name field config)))++(defn- validate-required-any!+ "Throw a 400 unless `config` satisfies one of `type-name`'s `:required-any` credential groups — for Google, a+ service account key on its own or an OAuth token together with a project ID."+ [type-name config]+ (let [{:keys [required-any fields]} (provider-type type-name)+ label-for (into {} (map (juxt :key :label)) fields)+ carried? (fn [group] (every? #(u/trimmed-string (get config %)) group))]+ (when (and (seq required-any) (not-any? carried? required-any))+ (throw (ex-info (tru "{0} needs one of: {1}."+ type-name+ (str/join (str " " (tru "or") " ")+ (map (fn [group] (str/join " + " (map (comp str label-for) group)))+ required-any)))+ {:status-code 400 :required-any required-any})))))++(defn validate-config!+ "Check a connection's `:config` against its provider type's field descriptors: required fields are present, fields+ that declare a `:prefix` start with it, `:options` values are among the options, per-field `:validate` hooks pass,+ and one of the type's `:required-any` credential groups is carried. Throws a 400 on the first problem."+ [type-name config]+ (when-not (provider-type type-name)+ (throw (ex-info (tru "Unknown provider type {0}." (pr-str type-name))+ {:status-code 400 :type type-name})))+ (doseq [field (:fields (provider-type type-name))]+ (validate-field! type-name field config))+ (validate-required-any! type-name config))++(defn credentials-complete?+ "Whether `config` carries the credentials a request needs.++ A required field the registry gives a `:default` counts as carried: [[with-field-defaults]] supplies it when the+ connection is resolved, so leaving it untouched is the admin accepting the value its form showed. A type with+ `:required-any` groups additionally needs one of them carried in full — Google's fields are individually optional+ because either credential will do, which without the groups would make an empty config count as complete.++ [[model-fields]] are exempt: they name what to call, not what authenticates the call, and a connection can+ legitimately take its model from the `connection-key/model` reference instead — which is where an Azure+ deployment configured before the connection list existed still lives. [[validate-config!]] still requires them+ of anything saved through the API, so only the environment and a hand-written `llm-providers` can omit them."+ [type-name config]+ (let [{:keys [fields required-any]} (provider-type type-name)+ model-keys (set (model-fields type-name))]+ (and (every? (fn [{:keys [key required? default]}]+ (or (not required?)+ default+ (contains? model-keys key)+ (u/trimmed-string (get config key))))+ fields)+ (or (empty? required-any)+ (boolean (some (fn [group] (every? #(u/trimmed-string (get config %)) group))+ required-any))))))++(defn config-complete?+ "Whether a connection of `type-name` can make requests: [[credentials-complete?]], or for the Metabase-managed+ provider — which authenticates with the instance token rather than with credentials of its own — whether the LLM+ proxy is configured."+ [type-name config]+ (if (managed-type? type-name)+ (some? (llm.settings/llm-proxy-base-url))+ (credentials-complete? type-name config)))++;;; ---------------------------------------- Connections configured by env var ------------------------------------++(def managed-connection-key+ "The connection key reserved for the Metabase-managed provider. Requests for it are routed through the LLM proxy+ and authenticated with the instance token."+ "metabase")++(def ^:private single-provider-settings+ "The per-type credential settings, keyed by the connection they configure.++ Pointing one of these at an environment variable is a supported way to configure a provider: it sets up a single+ connection without writing JSON into [[llm-providers]]. `:settings` maps a `:config` key to the setting that+ supplies it; a connection is configured only when at least one of the settings marked `:credential?` is set by an+ env var."+ {"anthropic" {:type "anthropic"+ :settings {:api-key {:setting :llm-anthropic-api-key :credential? true}+ :base-url {:setting :llm-anthropic-api-base-url}}}+ "openai" {:type "openai"+ :settings {:api-key {:setting :llm-openai-api-key :credential? true}+ :base-url {:setting :llm-openai-api-base-url}}}+ "openrouter" {:type "openrouter"+ :settings {:api-key {:setting :llm-openrouter-api-key :credential? true}+ :base-url {:setting :llm-openrouter-api-base-url}}}+ "mistral" {:type "mistral"+ :settings {:api-key {:setting :llm-mistral-api-key :credential? true}+ :base-url {:setting :llm-mistral-api-base-url}}}+ "zai" {:type "zai"+ :settings {:api-key {:setting :llm-zai-api-key :credential? true}+ :base-url {:setting :llm-zai-api-base-url}}}+ "moonshot" {:type "moonshot"+ :settings {:api-key {:setting :llm-moonshot-api-key :credential? true}+ :base-url {:setting :llm-moonshot-api-base-url}}}+ "google" {:type "google"+ :settings {:service-account-key {:setting :llm-google-service-account-key :credential? true}+ :oauth-access-token {:setting :llm-google-oauth-access-token :credential? true}+ :project-id {:setting :llm-google-project-id}+ :location {:setting :llm-google-location}+ :base-url {:setting :llm-google-api-base-url}}}+ "azure" {:type "azure"+ ;; the base URL is not a credential: on its own it can shadow a stored connection's field, but it+ ;; must not bring a standalone — and unusable — connection into existence+ :settings {:api-key {:setting :llm-azure-api-key :credential? true}+ :base-url {:setting :llm-azure-api-base-url}+ :model-family {:setting :llm-azure-model-family}+ :deployment-name {:setting :llm-azure-deployment-name}}}+ "bedrock" {:type "bedrock"+ :settings {:access-key-id {:setting :llm-bedrock-access-key-id :credential? true}+ :secret-access-key {:setting :llm-bedrock-secret-access-key :credential? true}+ :session-token {:setting :llm-bedrock-session-token}+ :region {:setting :llm-bedrock-region}}}})++(defn- env-supplied-fields+ "The `:config` fields the environment supplies for one [[single-provider-settings]] group:+ `{:config {field value} :vars {field \"MB_...\"} :credential-set? bool}`."+ [{:keys [settings]}]+ (reduce-kv+ (fn [acc config-key {:keys [setting credential?]}]+ (if-some [value (u/trimmed-string (setting/env-var-value setting))]+ (cond-> (-> acc+ (assoc-in [:config config-key] value)+ (assoc-in [:vars config-key] (setting/env-var-name setting)))+ credential? (assoc :credential-set? true))+ acc))+ {:config {} :vars {}}+ settings))++(defn- env-overlays+ "The environment's contribution to each connection key, resolved on every read so editing a variable takes effect+ on the next restart without anything having to be migrated or re-saved."+ []+ (into {}+ (keep (fn [[conn-key spec]]+ (let [{:keys [config] :as supplied} (env-supplied-fields spec)]+ (when (seq config)+ [conn-key (assoc supplied :type (:type spec))]))))+ single-provider-settings))++(defn- env-managed-connection+ "The managed connection `MB_LLM_METABOT_PROVIDER` demands: a `metabase/...` reference pinned by the environment+ has to resolve even though the managed connection holds no credentials a variable could supply."+ []+ (when (some-> (setting/env-var-value :llm-metabot-provider)+ (str/starts-with? (str managed-connection-key "/")))+ {:key managed-connection-key+ :type managed-connection-key+ :name (str (:label (provider-type managed-connection-key)))+ :source :env+ :env-vars (sorted-set (setting/env-var-name :llm-metabot-provider))+ :env-fields #{}+ :config {}}))++;;; --------------------------------------------------- Connections -------------------------------------------------++(defn stored-connections+ "The connection list exactly as persisted in [[llm-providers]], without the environment layered over it.++ Writes edit *this* list rather than [[connections]]. A stored connection whose key an env var shadows is absent+ from [[connections]], so rebuilding the list from there would drop it from the setting the next time an admin+ saved anything — the credentials would be gone for good once the env var came back off."+ []+ (vec (llm.settings/llm-providers)))++(defn- annotated-stored-connections+ []+ (let [env-managed? (some? (setting/env-var-value :llm-providers))+ annotate (fn [conn]+ (cond-> (assoc conn :source (if env-managed? :env :db))+ env-managed? (assoc :env-vars #{(setting/env-var-name :llm-providers)})))]+ (into [] (map annotate) (stored-connections))))++(defn connections+ "Every connection this instance can use, in admin-facing order.++ Connections stored in [[llm-providers]] come first, then any synthesized from the single-provider environment+ variables. On a key collision the environment shadows the stored connection *field by field*, the way an env var+ shadows any other setting: a lone `MB_LLM_ANTHROPIC_API_BASE_URL` reaches the stored connection's base URL+ instead of doing nothing, and everything the environment does not supply stays editable. `:env-fields` names the+ config keys the environment owns, and `:env-vars` the variables supplying them, so the form can disable exactly+ those inputs.++ A standalone `:env` connection is synthesized only when a variable marked `:credential?` is set — credentials are+ what bring a connection into existence; a base URL alone shadows but does not create. The managed connection is+ synthesized whenever `MB_LLM_METABOT_PROVIDER` pins a `metabase/...` reference, so that reference resolves."+ []+ (let [overlays (env-overlays)+ overlaid (mapv (fn [{conn-key :key :keys [type] :as conn}]+ (let [{env-config :config vars :vars :as overlay} (get overlays conn-key)]+ ;; only a same-typed overlay applies: the fields describe this provider type's config+ (if (and overlay (= type (:type overlay)))+ (-> conn+ (update :config merge env-config)+ (update :env-vars (fnil into (sorted-set)) (vals vars))+ (assoc :env-fields (set (keys env-config))))+ conn)))+ (annotated-stored-connections))+ taken (into #{} (map :key) overlaid)+ standalone (into []+ (keep (fn [[conn-key {:keys [type config vars credential-set?]}]]+ (when (and credential-set? (not (contains? taken conn-key)))+ {:key conn-key+ :type type+ :name (str (:label (provider-type type)))+ :source :env+ :env-vars (into (sorted-set) (vals vars))+ :env-fields (set (keys config))+ :config config})))+ overlays)+ managed (env-managed-connection)+ all (into overlaid standalone)]+ (cond-> all+ (and managed (not-any? #(= (:key managed) (:key %)) all))+ (conj managed))))++(defn connection+ "The connection identified by `conn-key`, or nil."+ [conn-key]+ (u/find-first-map (connections) [:key] conn-key))++(defn credentials+ "The `:config` map to authenticate `conn-key`'s requests with, or nil when there is no such connection."+ [conn-key]+ (:config (connection conn-key)))++(defn connection-usable?+ "Whether `conn-key` names a connection with everything it needs to make requests."+ [conn-key]+ (boolean+ (when-let [{:keys [type config]} (connection conn-key)]+ (config-complete? type config))))++(defn set-connections!+ "Persist `conns` as the stored connection list, dropping the derived annotation keys."+ [conns]+ (llm.settings/llm-providers! (mapv #(dissoc % :source :env-vars :env-fields) conns)))++;;; --------------------------------------------------- Slugs ------------------------------------------------------++(defn- ->slug+ "Lowercase `s` and collapse anything that is not a letter or digit into single hyphens, so the result satisfies+ [[metabase.util/valid-slug?]]. Returns nil when nothing usable is left."+ [s]+ (-> (u/lower-case-en (str s))+ (str/replace #"[^a-z0-9]+" "-")+ (str/replace #"^-+|-+$" "")+ not-empty))++(defn unique-key+ "A URL-safe connection key derived from `desired`, suffixed until it does not collide with an existing connection.++ New connections default to their provider type, so the first Anthropic connection is `anthropic` and reads the+ same way the old single-provider setting did."+ [desired]+ (let [base (or (->slug desired) "provider")+ used (into #{} (map :key) (connections))]+ (if-not (contains? used base)+ base+ (first (remove #(contains? used %)+ (map #(str base "-" %) (iterate inc 2)))))))++;;; ----------------------------------------------- Model references ------------------------------------------------++(defn managed-model-ref?+ "Whether `model-ref` names the Metabase-managed connection."+ [model-ref]+ (boolean (some-> model-ref (str/starts-with? (str managed-connection-key "/")))))++(defn model-ref->connection-key+ "The connection key of a `connection-key/model` string."+ [model-ref]+ (when model-ref+ (first (str/split model-ref #"/" 2))))++(defn model-ref->model+ "The model part of a `connection-key/model` string — everything after the first segment."+ [model-ref]+ (when model-ref+ (second (str/split model-ref #"/" 2))))++(defn strip-managed-prefix+ "Drop the `metabase/` routing prefix from a model reference, leaving the `provider/model` pair the proxy forwards.+ Returns `model-ref` unchanged when it has no such prefix."+ [model-ref]+ (if (managed-model-ref? model-ref)+ (str/replace-first model-ref (str managed-connection-key "/") "")+ model-ref))++(defn with-field-defaults+ "Fill in each field's registry `:default` wherever `config` left it blank, and run each field's `:normalize` over+ the value that results, so a resolved connection carries everything the adapter needs in the shape it needs it.+ Normalizing here rather than on write covers every source of a value — the stored config, an environment+ variable, and the default itself — so a base URL entered with a trailing slash cannot double up the `/` when a+ path is joined onto it."+ [type-name config]+ (reduce (fn [config {:keys [key default normalize]}]+ (let [config (cond-> config+ (and default (not (u/trimmed-string (get config key))))+ (assoc key default))]+ (cond-> config+ (and normalize (u/trimmed-string (get config key)))+ (update key normalize))))+ (or config {})+ (:fields (provider-type type-name))))++(defn resolve-model-ref+ "Resolve a `connection-key/model` string against the configured connections.++ Returns `{:connection-key :type :model :credentials :ai-proxy?}`, or nil when no such connection exists. `:type`+ is the provider type whose adapter should serve the request: for the managed connection that is the wire family+ named by the model's own first segment (`metabase/anthropic/claude-...` is served by the Anthropic adapter over+ the proxy), and `:model` is what remains."+ [model-ref]+ (let [conn-key (model-ref->connection-key model-ref)+ model (model-ref->model model-ref)]+ (when-let [{:keys [type config]} (connection conn-key)]+ (if (managed-type? type)+ {:connection-key conn-key+ :type (model-ref->connection-key model)+ :model (model-ref->model model)+ :credentials nil+ :ai-proxy? true}+ {:connection-key conn-key+ :type type+ :model model+ :credentials (with-field-defaults type config)+ :ai-proxy? false}))))++;;; ------------------------------------------- Per-provider settings ----------------------------------------------++(def ^:private setting->connection-field+ "Which connection field each per-provider credential setting configures: setting keyword -> [conn-key field]."+ (into {}+ (mapcat (fn [[conn-key {:keys [settings]}]]+ (map (fn [[field {:keys [setting]}]] [setting [conn-key field]]) settings)))+ single-provider-settings))++(defn single-provider-setting-value+ "What the per-provider credential setting `setting-kw` resolves to: the field on the connection its settings group+ configures — environment overlay and registry defaults included — or the environment value and registry default+ alone when no such connection exists.++ Backs those settings' getters, so code that has always read e.g. `llm-anthropic-api-key` keeps working now that+ the value lives on the connection list."+ [setting-kw]+ (let [[conn-key field] (get setting->connection-field setting-kw)+ group-type (:type (get single-provider-settings conn-key))+ {:keys [type config]} (connection conn-key)]+ (if (= type group-type)+ (get (with-field-defaults type config) field)+ (or (u/trimmed-string (setting/env-var-value setting-kw))+ (get (with-field-defaults group-type {}) field)))))++(defn set-single-provider-setting!+ "Write `new-value` for the per-provider credential setting `setting-kw` into the connection its settings group+ configures, creating the connection when a non-blank value arrives for one that does not exist yet; blank clears+ the field. Values are checked against the field's own registry validation, the way the settings' old setters did.++ Backs those settings' setters, so `config.yml` provisioning and code that has always written the setting keep+ working now that the value lives on the connection list."+ [setting-kw new-value]+ (let [[conn-key field] (get setting->connection-field setting-kw)+ group-type (:type (get single-provider-settings conn-key))+ value (u/trimmed-string new-value)+ stored (stored-connections)+ idx (first (keep-indexed (fn [i conn] (when (= conn-key (:key conn)) i)) stored))]+ ;; a client echoing back the mask [[metabase.settings.core/obfuscate-value]] handed it is not entering a new+ ;; value, the same way [[metabase.settings.core/set!]] treats sensitive settings+ (when-not (setting/obfuscated-value? value)+ (when value+ (validate-config-field! group-type field {field value}))+ (cond+ idx (set-connections! (update-in stored [idx :config]+ (fn [config]+ (if value (assoc config field value) (dissoc config field)))))+ value (set-connections! (conj stored {:key conn-key+ :type group-type+ :name (str (:label (provider-type group-type)))+ :config {field value}}))))))++;;; -------------------------------------------------- Redaction ----------------------------------------------------++(defn- secret-or-unknown-field-keys+ "The config keys to treat as secret for `type-name`, given `config`: the registry's secret fields, or — for a+ type the registry does not know, which a hand-written `llm-providers` can hold — every key, because nothing says+ which of them are credentials."+ [type-name config]+ (if (provider-type type-name)+ (secret-field-keys type-name)+ (set (keys config))))++(defn redact+ "Mask a connection's secret fields so it can be returned over the API."+ [{:keys [type config] :as conn}]+ (update conn :config+ (fn [config']+ (reduce (fn [config' field-key]+ (cond-> config'+ (u/trimmed-string (get config' field-key))+ (update field-key setting/obfuscate-value)))+ (or config' {})+ (secret-or-unknown-field-keys type config)))))++(defn merge-config+ "Layer a client-supplied `config` over the `existing` one, keeping the stored secret whenever the client echoed+ back the mask [[redact]] handed it. Sending an explicitly blank value still clears the field."+ [type-name existing config]+ (reduce (fn [merged field-key]+ (cond-> merged+ (setting/obfuscated-value? (get config field-key))+ (assoc field-key (get existing field-key))))+ (merge existing config)+ (secret-or-unknown-field-keys type-name existing)))diff --git a/src/metabase/llm/settings.clj b/src/metabase/llm/settings.cljindex 06871c2ed31..a323e6685b1 100644--- a/src/metabase/llm/settings.clj+++ b/src/metabase/llm/settings.clj@@ -4,7 +4,7 @@[clojure.string :as str][metabase.config.core :as config][metabase.premium-features.core :as premium-features]- [metabase.settings.core :as setting :refer [defsetting]]+ [metabase.settings.core :refer [defsetting]][metabase.util :as u][metabase.util.i18n :refer [deferred-tru tru]])(:import@@ -18,16 +18,6 @@Used to validate [[llm-bedrock-region]]."(into #{} (map str) (Region/regions)))-(defn- trimmed-string- [value]- (when (string? value)- (not-empty (str/trim value))))--(defn- set-trimmed-string!- "Set a string setting to the trimmed `new-value`; blank values are stored as nil."- [setting-key new-value]- (setting/set-value-of-type! :string setting-key (trimmed-string new-value)))-(def ^:private loopback-hosts"Hostnames that resolve to the local machine. `URL.getHost` returns IPv6 hostswrapped in brackets, e.g. `[::1]`."@@ -51,27 +41,21 @@{:status-code 400:llm-url url}))))))-(defn- set-prefixed-api-key!- [setting-key prefix deferred-message new-value]- (let [trimmed (trimmed-string new-value)]- (when (and trimmed (not (str/starts-with? trimmed prefix)))- (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)))+;; TODO (Chris 2026-08-17) -- BOT-2005: generate-sql and semantic search read these settings directly, so+;; deleting the connection they key off turns those features off. They should name a connection instead.+(defn- connection-field-getter+ "Getter for a per-provider credential setting whose value lives on the `llm-providers` connection list.+ Resolved late: [[metabase.llm.provider]] requires this namespace."+ [setting-kw]+ (fn []+ ((requiring-resolve 'metabase.llm.provider/single-provider-setting-value) setting-kw)))++(defn- connection-field-setter+ "Setter counterpart of [[connection-field-getter]], writing through to the connection list so `config.yml`+ provisioning and code that has always written these settings keep working."+ [setting-kw]+ (fn [new-value]+ ((requiring-resolve 'metabase.llm.provider/set-single-provider-setting!) setting-kw new-value)));;; ------------------------------------------------- Anthropic -------------------------------------------------@@ -81,10 +65,9 @@:visibility :settings-manager:export? false:deprecated-name :ee-anthropic-api-key- :setter (partial set-prefixed-api-key!- :llm-anthropic-api-key- "sk-ant-"- (deferred-tru "Invalid Anthropic API key format. Key must start with ''sk-ant-''.")))+ :getter (connection-field-getter :llm-anthropic-api-key)+ :setter (connection-field-setter :llm-anthropic-api-key)+ :doc "Backed by the anthropic connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-anthropic-api-key-configured?"Whether an Anthropic API key has been configured."@@ -108,7 +91,10 @@:visibility :settings-manager:default "https://api.anthropic.com":export? false- :deprecated-name :ee-anthropic-api-base-url)+ :getter (connection-field-getter :llm-anthropic-api-base-url)+ :setter (connection-field-setter :llm-anthropic-api-base-url)+ :deprecated-name :ee-anthropic-api-base-url+ :doc "Backed by the anthropic connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-anthropic-api-version(deferred-tru "The Anthropic API version.")@@ -134,7 +120,10 @@:visibility :settings-manager:default "https://api.openai.com":export? false- :deprecated-name :ee-openai-api-base-url)+ :getter (connection-field-getter :llm-openai-api-base-url)+ :setter (connection-field-setter :llm-openai-api-base-url)+ :deprecated-name :ee-openai-api-base-url+ :doc "Backed by the openai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-openai-api-key(deferred-tru "The OpenAI API Key.")@@ -142,10 +131,9 @@:visibility :settings-manager:export? false:deprecated-name :ee-openai-api-key- :setter (partial set-prefixed-api-key!- :llm-openai-api-key- "sk-"- (deferred-tru "Invalid OpenAI API key format. Key must start with ''sk-''.")))+ :getter (connection-field-getter :llm-openai-api-key)+ :setter (connection-field-setter :llm-openai-api-key)+ :doc "Backed by the openai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; ------------------------------------------------- OpenRouter ------------------------------------------------@@ -155,7 +143,10 @@:visibility :settings-manager:default "https://openrouter.ai/api":export? false- :deprecated-name :ee-openrouter-api-base-url)+ :getter (connection-field-getter :llm-openrouter-api-base-url)+ :setter (connection-field-setter :llm-openrouter-api-base-url)+ :deprecated-name :ee-openrouter-api-base-url+ :doc "Backed by the openrouter connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-openrouter-api-key(deferred-tru "The OpenRouter API Key.")@@ -163,10 +154,9 @@:visibility :settings-manager:export? false:deprecated-name :ee-openrouter-api-key- :setter (partial set-prefixed-api-key!- :llm-openrouter-api-key- "sk-or-v1-"- (deferred-tru "Invalid OpenRouter API key format. Key must start with ''sk-or-v1-''.")))+ :getter (connection-field-getter :llm-openrouter-api-key)+ :setter (connection-field-setter :llm-openrouter-api-key)+ :doc "Backed by the openrouter connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; --------------------------------------------------- Z.AI ----------------------------------------------------@@ -176,7 +166,9 @@:visibility :settings-manager:default "https://api.z.ai/api/paas/v4":export? false- :setter (partial set-normalized-base-url! :llm-zai-api-base-url))+ :getter (connection-field-getter :llm-zai-api-base-url)+ :setter (connection-field-setter :llm-zai-api-base-url)+ :doc "Backed by the zai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-zai-api-key(deferred-tru "The Z.AI API Key.")@@ -185,7 +177,9 @@:sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-zai-api-key))+ :getter (connection-field-getter :llm-zai-api-key)+ :setter (connection-field-setter :llm-zai-api-key)+ :doc "Backed by the zai connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; -------------------------------------------------- Mistral ---------------------------------------------------@@ -195,14 +189,18 @@:visibility :settings-manager:default "https://api.mistral.ai/v1":export? false- :setter (partial set-normalized-base-url! :llm-mistral-api-base-url))+ :getter (connection-field-getter :llm-mistral-api-base-url)+ :setter (connection-field-setter :llm-mistral-api-base-url)+ :doc "Backed by the mistral connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-mistral-api-key(deferred-tru "The Mistral API Key."):sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-mistral-api-key))+ :getter (connection-field-getter :llm-mistral-api-key)+ :setter (connection-field-setter :llm-mistral-api-key)+ :doc "Backed by the mistral connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; ------------------------------------------------- Moonshot --------------------------------------------------@@ -212,14 +210,18 @@:visibility :settings-manager:default "https://api.moonshot.ai/v1":export? false- :setter (partial set-normalized-base-url! :llm-moonshot-api-base-url))+ :getter (connection-field-getter :llm-moonshot-api-base-url)+ :setter (connection-field-setter :llm-moonshot-api-base-url)+ :doc "Backed by the moonshot connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(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))+ :getter (connection-field-getter :llm-moonshot-api-key)+ :setter (connection-field-setter :llm-moonshot-api-key)+ :doc "Backed by the moonshot connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; ------------------------------------ Google Gemini Enterprise Agent Platform --------------------------------;;; The Gemini Enterprise Agent Platform (formerly Vertex AI). Every request applies to one Google Cloud project. The@@ -230,14 +232,18 @@:sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-google-service-account-key))+ :getter (connection-field-getter :llm-google-service-account-key)+ :setter (connection-field-setter :llm-google-service-account-key)+ :doc "Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-google-oauth-access-token(deferred-tru "A short-lived OAuth2 access token for the Gemini Enterprise Agent Platform (e.g. from `gcloud auth print-access-token`). Useful for testing."):sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-google-oauth-access-token))+ :getter (connection-field-getter :llm-google-oauth-access-token)+ :setter (connection-field-setter :llm-google-oauth-access-token)+ :doc "Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(def ^:private google-project-id-pattern"Matches a Google Cloud project ID: 6 to 30 characters of lowercase letters, digits and hyphens, starting with a@@ -269,39 +275,23 @@(boolean (and (<= (count location) google-location-max-length)(re-matches google-location-pattern location))))-(defn- set-google-project-id!- [new-value]- (let [project-id (trimmed-string new-value)]- (when (and project-id (not (valid-google-project-id? project-id)))- (throw (ex-info (tru (str "{0} is not a valid Google Cloud project ID. Use the project ID — 6 to 30 lowercase "- "letters, digits and hyphens — rather than the project name or number.")- (pr-str project-id))- {:status-code 400})))- (setting/set-value-of-type! :string :llm-google-project-id project-id)))-(defsetting llm-google-project-id(deferred-tru "The Google Cloud project ID for the Gemini Enterprise Agent Platform."):encryption :no:visibility :settings-manager:export? false- :setter set-google-project-id!)--(defn- set-google-location!- [new-value]- (let [location (trimmed-string new-value)]- (when (and location (not (valid-google-location? location)))- (throw (ex-info (tru (str "{0} is not a valid Google Cloud location. Use a location ID like \"us-central1\", "- "or leave it blank to use the global location.")- (pr-str location))- {:status-code 400})))- (setting/set-value-of-type! :string :llm-google-location location)))+ :getter (connection-field-getter :llm-google-project-id)+ :setter (connection-field-setter :llm-google-project-id)+ :doc "Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-google-location(deferred-tru "The Google Cloud location for the Gemini Enterprise Agent Platform (e.g. us-central1). Defaults to global."):encryption :no:visibility :settings-manager:export? false- :setter set-google-location!)+ :getter (connection-field-getter :llm-google-location)+ :setter (connection-field-setter :llm-google-location)+ :doc "Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(def google-global-api-base-url"Google's global Gemini Enterprise Agent Platform host, and the default for [[llm-google-api-base-url]].@@ -315,7 +305,9 @@:visibility :settings-manager:default google-global-api-base-url:export? false- :setter (partial set-normalized-base-url! :llm-google-api-base-url))+ :getter (connection-field-getter :llm-google-api-base-url)+ :setter (connection-field-setter :llm-google-api-base-url)+ :doc "Backed by the google connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; ----------------------------------------------- Amazon Bedrock ----------------------------------------------@@ -324,28 +316,27 @@:sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-bedrock-access-key-id))+ :getter (connection-field-getter :llm-bedrock-access-key-id)+ :setter (connection-field-setter :llm-bedrock-access-key-id)+ :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-bedrock-secret-access-key(deferred-tru "The AWS Secret Access Key for Amazon Bedrock."):sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-bedrock-secret-access-key))+ :getter (connection-field-getter :llm-bedrock-secret-access-key)+ :setter (connection-field-setter :llm-bedrock-secret-access-key)+ :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-bedrock-session-token(deferred-tru "The AWS Session Token for Amazon Bedrock. Only needed for temporary credentials."):sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-bedrock-session-token))--(defn- set-bedrock-region!- [new-value]- (let [region (trimmed-string new-value)]- (when (and region (not (contains? known-aws-regions region)))- (throw (ex-info (tru "Invalid AWS region {0}." (pr-str region)) {:status-code 400})))- (setting/set-value-of-type! :string :llm-bedrock-region region)))+ :getter (connection-field-getter :llm-bedrock-session-token)+ :setter (connection-field-setter :llm-bedrock-session-token)+ :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-bedrock-region(deferred-tru "The AWS region for Amazon Bedrock (e.g. us-east-1).")@@ -353,7 +344,9 @@:visibility :settings-manager:default "us-east-1":export? false- :setter set-bedrock-region!)+ :getter (connection-field-getter :llm-bedrock-region)+ :setter (connection-field-setter :llm-bedrock-region)+ :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.");;; ----------------------------------------------- Microsoft Azure ---------------------------------------------@@ -363,14 +356,56 @@:sensitive? true:visibility :settings-manager:export? false- :setter (partial set-trimmed-string! :llm-azure-api-key))+ :getter (connection-field-getter :llm-azure-api-key)+ :setter (connection-field-setter :llm-azure-api-key)+ :doc "Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(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 (partial set-normalized-base-url! :llm-azure-api-base-url))+ :getter (connection-field-getter :llm-azure-api-base-url)+ :setter (connection-field-setter :llm-azure-api-base-url)+ :doc "Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")++(defsetting llm-azure-model-family+ (deferred-tru "Whether the Azure deployment configured from the environment serves an `openai` or an `anthropic` model. Defaults to `openai`.")+ :encryption :no+ :visibility :settings-manager+ :export? false+ :getter (connection-field-getter :llm-azure-model-family)+ :setter (connection-field-setter :llm-azure-model-family)+ :doc "Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")++(defsetting llm-azure-deployment-name+ (deferred-tru "The name of the model deployment served by the Azure connection configured from the environment.")+ :encryption :no+ :visibility :settings-manager+ :export? false+ :getter (connection-field-getter :llm-azure-deployment-name)+ :setter (connection-field-setter :llm-azure-deployment-name)+ :doc "Backed by the azure connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")++;;; ---------------------------------------------- Provider connections ------------------------------------------++;;; The per-provider credential settings above are read-only at runtime: they configure a connection only when set+;;; by an environment variable, which [[metabase.llm.provider/connections]] resolves on every read. Editing one in+;;; the app DB would not reach the connection serving requests, so a write is rejected rather than silently ignored.+;;; Connections are managed through the `/api/llm/providers` endpoints instead.++(defsetting llm-providers+ (deferred-tru "JSON array of configured LLM provider connections. Each entry has a `key` (a URL-safe slug identifying the connection), a `type` (the provider type, e.g. `anthropic`), a display `name`, and a `config` map of that provider type''s credential fields.")+ :type :json+ :default []+ :encryption :when-encryption-key-set+ :sensitive? true+ :visibility :settings-manager+ :export? false+ :audit :no-value+ :doc "Connections are normally managed from the admin AI settings page. Setting this environment variable puts the whole list under environment control and makes it read-only in the UI.++Configuring a provider through the single-provider variables (`MB_LLM_ANTHROPIC_API_KEY` and friends) is equally supported, and is the simpler option when you only need one connection per provider and would rather not hand-write JSON. Each such provider becomes a read-only connection whose key is the provider type, resolved from the environment on every read, so editing one of those variables is picked up on the next restart. A provider configured this way takes precedence over a stored connection with the same key.");;; --------------------------------------------------- Proxy ---------------------------------------------------@@ -378,6 +413,7 @@(deferred-tru "Base URL for the LLM proxy. When set, requests to the managed Metabase AI service are routed through this proxy and authenticated with the instance token instead of a provider API key. Harbormaster adds /llm component into the url.");; For details on llm component see the https://github.com/metabase/metabase/pull/74526#discussion_r3282553435.:enabled? #(or (premium-features/has-feature? :metabase-ai-managed)+ (premium-features/has-feature? :offer-metabase-ai-managed)(premium-features/has-feature? :metabot-v3)):encryption :no:visibility :internaldiff --git a/src/metabase/llm/startup.clj b/src/metabase/llm/startup.cljindex 418425d3937..5f864172276 100644--- a/src/metabase/llm/startup.clj+++ b/src/metabase/llm/startup.clj@@ -2,6 +2,7 @@"Startup-time reconciliation for managed Metabot configuration."(:require[clojure.string :as str]+ [metabase.llm.provider :as llm.provider][metabase.metabot.settings :as metabot.settings][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting]@@ -10,17 +11,45 @@(set! *warn-on-reflection* true)+(defn- managed-connection+ []+ {:key llm.provider/managed-connection-key+ :type llm.provider/managed-connection-key+ :name (str (:label (llm.provider/provider-type llm.provider/managed-connection-key)))+ :config {}})++(defn- ensure-managed-connection!+ "Materialize the connection for the Metabase-managed provider. It holds no credentials of its own, so it exists+ purely so that a `metabase/...` model reference resolves. Returns whether the connection exists afterwards.++ Nothing is written when `llm-providers` is set by an env var: the write would land in the app DB and then lose to+ the env var on every read, leaving the model reference naming a connection that never resolves."+ []+ (cond+ (llm.provider/connection llm.provider/managed-connection-key)+ true++ (some? (setting/env-var-value :llm-providers))+ false++ :else+ (do (llm.provider/set-connections! (conj (llm.provider/stored-connections) (managed-connection)))+ true)))+(defn- sync-managed-metabot-providerconfigured? (metabot.settings/llm-metabot-configured?)]- (if (and (str/blank? raw-provider)- (not configured?))- (do- (log/infof "Configuring llm-metabot-provider to %s for legacy Metabot entitlement"- metabot.settings/default-metabase-llm-metabot-provider)- (setting/set! :llm-metabot-provider metabot.settings/default-metabase-llm-metabot-provider))- nil)))+ (when (and (str/blank? raw-provider)+ (not configured?))+ (if (ensure-managed-connection!)+ (do+ (log/infof "Configuring llm-metabot-provider to %s for legacy Metabot entitlement"+ metabot.settings/default-metabase-llm-metabot-provider)+ (setting/set! :llm-metabot-provider metabot.settings/default-metabase-llm-metabot-provider))+ (log/warnf "Leaving llm-metabot-provider unset for legacy Metabot entitlement: %s is set by the environment and lists no %s connection"+ (setting/env-var-name :llm-providers)+ llm.provider/managed-connection-key)))))(defn- maybe-sync-managed-metabot-provider![]@@ -31,13 +60,11 @@(and legacy-result (not managed-result)) (sync-managed-metabot-provider!))))(defn check-and-sync-settings-on-startup!- "For legacy `:metabot-v3` customers that do not have `:metabase-ai-managed`,- switch the default unmanaged Metabot provider to the managed `metabase/...`- provider on startup.+ "Reconcile LLM provider configuration at startup: for legacy `:metabot-v3` customers that do not have+ `:metabase-ai-managed`, switch the default unmanaged Metabot provider to the managed `metabase/...` provider.- This is a migration for existing instances only: it is skipped until initial- setup has completed, so fresh instances choose their AI provider in the setup- wizard instead of booting pre-configured."+ This is for existing instances only: it is skipped until initial setup has completed, so fresh instances choose+ their AI provider in the setup wizard instead of booting pre-configured."[](when (setup/has-user-setup)(maybe-sync-managed-metabot-provider!)))diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.cljindex 94cd831e9b2..019ca79488b 100644--- a/src/metabase/metabot/agent/core.clj+++ b/src/metabase/metabot/agent/core.clj@@ -8,13 +8,13 @@[metabase.api-scope.core :as api-scope][metabase.api.common :as api][metabase.config.core :as config]+ [metabase.llm.provider :as llm.provider][metabase.metabot.agent.links :as links][metabase.metabot.agent.memory :as memory][metabase.metabot.agent.messages :as messages][metabase.metabot.agent.profiles :as profiles][metabase.metabot.agent.streaming :as streaming][metabase.metabot.metadata-perms :as metabot.perms]- [metabase.metabot.provider-util :as provider-util][metabase.metabot.schema :as metabot.schema][metabase.metabot.scope :as scope][metabase.metabot.self :as self]@@ -505,7 +505,7 @@whether the request was routed through the AI proxy.Non-usage parts pass through unchanged."[usage-atom provider-and-model]- (let [model (or (some-> provider-and-model provider-util/strip-metabase-prefix)+ (let [model (or (some-> provider-and-model llm.provider/strip-managed-prefix)"unknown")](map (fn [part](if (= (:type part) :usage)diff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.cljindex 0ee6555a4f7..7b3bed55980 100644--- a/src/metabase/metabot/api.clj+++ b/src/metabase/metabot/api.clj@@ -2,7 +2,6 @@"`/api/metabot/` routes"(:require[clojure.core.async :as a]- [clojure.string :as str][medley.core :as m][metabase.ai-tracing.core :as ait][metabase.analytics.core :as analytics.core]@@ -13,7 +12,6 @@[metabase.config.core :as config][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]- [metabase.llm.settings :as llm.settings][metabase.metabot.agent.core :as agent][metabase.metabot.agent.memory :as memory][metabase.metabot.api.conversations]@@ -26,16 +24,11 @@[metabase.metabot.envelope :as metabot.envelope][metabase.metabot.feedback :as metabot.feedback][metabase.metabot.persistence :as metabot.persistence]- [metabase.metabot.provider-util :as provider-util]- [metabase.metabot.self :as metabot.self][metabase.metabot.self.core :as self.core]- [metabase.metabot.settings :as metabot.settings][metabase.metabot.usage :as metabot.usage][metabase.models.interface :as mi]- [metabase.permissions.core :as perms][metabase.request.core :as request][metabase.server.streaming-response :as sr]- [metabase.settings.core :as setting][metabase.slackbot.api][metabase.util :as u][metabase.util.i18n :refer [tru]]@@ -447,435 +440,6 @@(metabot.feedback/persist-source-feedback! body)api/generic-204-no-content)-(def ^:private metabot-provider-schema- (into [:enum] metabot.settings/supported-metabot-providers))--(def ^:private llm-model-response-schema- [:map- [:id :string]- [:display_name :string]- [:group {:optional true} [:maybe :string]]])--(def ^:private metabot-settings-response-schema- [:map- [:value [:maybe :string]]- [:credentials-error {:optional true} [:maybe :string]]- [:models [:sequential llm-model-response-schema]]])--(def ^:private provider-credentials-schema- "Provider credentials carried by the request body's `:credentials` map."- [:map- ;; bedrock- [:access-key-id {:optional true} [:maybe :string]]- [:secret-access-key {:optional true} [:maybe :string]]- [:region {:optional true} [:maybe :string]]- [:session-token {:optional true} [:maybe :string]]- ;; azure- [:api-key {:optional true} [:maybe :string]]- [:base-url {:optional true} [:maybe :string]]- [:project-id {:optional true} [:maybe :string]]- [:location {:optional true} [:maybe :string]]- [:service-account-key {:optional true} [:maybe :string]]- [:oauth-access-token {:optional true} [:maybe :string]]])--(def ^:private metabot-settings-request-schema- [:map- [:provider metabot-provider-schema]- [:model {:optional true} [:maybe :string]]- [:api-key {:optional true} [:maybe :string]]- [:credentials {:optional true} [:maybe provider-credentials-schema]]])--(defn- provider-api-key-setting-key- [provider]- (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))--(defn- non-blank-string- [value]- (when (string? value)- (let [trimmed (str/trim value)]- (when-not (str/blank? trimmed)- trimmed))))--(defn- effective-provider-model- [provider model]- (when (some? model)- (or (non-blank-string model)- (metabot.settings/default-model-for-provider provider))))--(defn- provider-client-error?- "Whether a provider api-error is a client-side 4xx we should surface rather than treat as an- outage. Covers rejected or missing credentials (401/403) and a request the provider refused- outright (e.g. a custom base URL pointing at the wrong surface, which 400s). `rethrow-api-error!`- tags these with `:status`; other callers throw with `:status-code`. Provider 5xx and network- failures are left to propagate as 500s so outages aren't reported as client errors."- [error]- (let [{:keys [api-error status status-code]} (ex-data error)- status (or status status-code)]- (and api-error- (number? status)- (<= 400 status 499))))--(defn- title-case-token- [token]- (case token- "openai" "OpenAI"- "claude" "Claude"- (str/capitalize token)))--(defn- anthropic-model-group- [{:keys [id]}]- (let [tokens (str/split id #"-")]- (or (some->> tokens- (filter #{"haiku" "sonnet" "opus"})- first- title-case-token)- (some->> tokens- (take 2)- seq- (map title-case-token)- (str/join " ")))))--(defn- bedrock-model-group- [{:keys [id]}]- (cond- (str/starts-with? id "anthropic.") "Anthropic"- (str/starts-with? id "openai.") "OpenAI"- :else nil))--(defn- openai-model-group- "Group an OpenAI model by version family for the picker."- [{:keys [id]}]- (when-let [version (second (re-find #"^gpt-(\d+(?:\.\d+)?)" id))]- (str "GPT-" version)))--(defn- openrouter-model-group- [{:keys [display_name id]}]- (cond- (and display_name (str/includes? display_name ": "))- (-> display_name- (str/split #": " 2)- first)-- (and id (str/includes? id "/"))- (-> id- (str/split #"/" 2)- first- title-case-token)))--(defn- decorate-provider-model- [provider model]- (case provider- "anthropic" (assoc model :group (anthropic-model-group model))- "bedrock" (assoc model :group (bedrock-model-group model))- "openai" (assoc model :group (openai-model-group model))- "openrouter" (assoc model :group (openrouter-model-group model))- model))--(defn- normalize-metabase-model- [model]- (update model :id (fn [id]- (when id- (if (str/includes? id "/")- id- (str "anthropic/" id))))))--(defn- decorate-provider-models- [provider models]- (let [models (if (= provider provider-util/metabase-provider-prefix)- (map normalize-metabase-model models)- models)- decorated-models (map #(decorate-provider-model provider %) models)]- (if (contains? #{"anthropic" "bedrock" "openai" "openrouter"} provider)- (let [grouped-models (group-by :group decorated-models)]- (->> grouped-models- keys- sort- (mapcat #(get grouped-models %))- vec))- (vec decorated-models))))--(defn- provider-models-response- "List a provider's models.- Validate against `:credentials` in `opts` when provided and the provider's saved credentials otherwise. The shape- of the credentials map varies by provider (see [[metabot.settings/configured-provider-credentials]]). `:model` in- `opts` is the candidate model for providers whose validation depends on it (Azure's wire family)."- ([provider]- (provider-models-response provider nil))- ([provider {credentials-override :credentials model :model}]- (if (= provider provider-util/metabase-provider-prefix)- {:models (decorate-provider-models- provider- (:models (metabot.self/list-models "anthropic" {:ai-proxy? true})))}- (let [credentials (or credentials-override- (metabot.settings/configured-provider-credentials provider))]- (if (and provider (metabot.settings/provider-credentials-complete? provider credentials))- (try- {:models (decorate-provider-models- provider- (:models (metabot.self/list-models provider (cond-> {:credentials credentials}- model (assoc :model model)))))}- (catch clojure.lang.ExceptionInfo e- (if (provider-client-error? e)- {:models []- :credentials-error (.getMessage e)}- (throw e))))- {:models []})))))--(defn- settings-response- ([provider]- (settings-response provider nil))- ([provider opts]- (merge- {:value (metabot.settings/llm-metabot-provider)}- (provider-models-response provider opts))))--(defn- current-provider- []- (provider-util/provider-and-model->provider (metabot.settings/llm-metabot-provider)))--(defn- current-setting-provider- []- (provider-util/provider-and-model->outer-provider (metabot.settings/llm-metabot-provider)))--(defn- throw-credentials-error!- [response]- (when-let [credentials-error (:credentials-error response)]- (throw (ex-info credentials-error- {:status-code 400- :api-error true})))- response)--(api.macros/defendpoint :get "/settings"- :- metabot-settings-response-schema- "Return available models for a provider using its configured credentials."- [_route-params- {:keys [provider]} :- [:map- [:provider {:optional true} metabot-provider-schema]]]- (perms/check-has-application-permission :setting)- (settings-response (or provider (current-provider))))--(def ^:private bedrock-credential-fields- [:access-key-id :secret-access-key :region :session-token])--(defn- effective-bedrock-credentials- "The Bedrock credentials a settings request resolves to.-- Each field follows the same presence contract as the top-level `:credentials` key: a field present in the request- replaces the saved `llm-bedrock-*` value, while an absent field keeps the saved value. Nil or blank means an- explicit clear. So an admin can blank a stale session token without re-entering the keys, and a region-only edit- doesn't touch anything else."- [supplied-creds]- (reduce (fn [creds field]- (cond-> creds- (contains? supplied-creds field) (assoc field (non-blank-string (get supplied-creds field)))))- (metabot.settings/configured-provider-credentials "bedrock")- bedrock-credential-fields))--(defn- effective-azure-credentials- "The Azure credentials a settings request resolves to.-- Non-blank request fields are layered over the saved `llm-azure-*` settings, so e.g. a key-only rotation keeps the- saved base URL. The settings are read individually (not via the all-or-nothing configured-credentials map) so a- partially-configured or env-set field still participates in layering — completeness is the caller's check. The- base URL is normalized the same way its setter normalizes it (whitespace/trailing-slash trim) so the validation- round-trip exercises exactly what would be persisted."- [{:keys [api-key base-url]}]- {:api-key (or (non-blank-string api-key)- (non-blank-string (llm.settings/llm-azure-api-key)))- :base-url (or (llm.settings/normalize-llm-base-url base-url)- (llm.settings/normalize-llm-base-url (llm.settings/llm-azure-api-base-url)))})--(def ^:private google-credential-fields- [:service-account-key :oauth-access-token :project-id :location])--(defn- effective-google-credentials- "The Google credentials a settings request resolves to.-- Each field follows the same contract as the top-level `:credentials` key: a field present in the request replaces- the saved `llm-google-*` value, while an absent field keeps the saved value. Nil or blank means an explicit clear."- [supplied-creds]- (reduce (fn [creds field]- (cond-> creds- (contains? supplied-creds field) (assoc field (non-blank-string (get supplied-creds field)))))- (metabot.settings/configured-provider-credentials "google")- google-credential-fields))--(defn- request-credentials- "The credentials override carried by a `PUT /api/metabot/settings` request body as a provider credentials map.-- nil when the request does not touch credentials for `provider`.-- An explicitly nil credential field in the body — `:api-key` for API-key providers, `:credentials` for Bedrock,- Azure, and Google — resolves to a credentials map whose key material is nil: an explicit clear. Fields *inside* the- Bedrock and Google credentials maps follow that map's presence contract (see [[effective-bedrock-credentials]] and- [[effective-google-credentials]]); blank fields *inside* the Azure credentials map mean \"keep the saved value\" (see- [[effective-azure-credentials]]), so e.g. a key-only rotation can't wipe the base URL. Throws a 400 when non-nil- Bedrock/Azure/Google credentials don't resolve to a complete set."- [provider {:keys [api-key credentials] :as body}]- (case provider- "bedrock"- (when (contains? body :credentials)- (if (nil? credentials)- {:access-key-id nil- :secret-access-key nil- :session-token nil- :region nil}- (let [creds (effective-bedrock-credentials credentials)]- (when-not (metabot.settings/provider-credentials-complete? provider creds)- (throw (ex-info (tru "AWS Bedrock credentials are incomplete.")- {:status-code 400- :api-error true- :missing-keys (vec (remove #(non-blank-string (get creds %))- [:access-key-id :secret-access-key]))})))- creds)))-- "azure"- (when (contains? body :credentials)- (if (nil? credentials)- {:api-key nil- :base-url nil}- (let [creds (effective-azure-credentials credentials)]- (when-not (metabot.settings/provider-credentials-complete? provider creds)- (throw (ex-info (tru "Azure credentials are incomplete.")- {:status-code 400- :api-error true- :missing-keys (vec (remove #(non-blank-string (get creds %))- [:api-key :base-url]))})))- creds)))-- "google"- (when (contains? body :credentials)- (if (nil? credentials)- {:service-account-key nil- :oauth-access-token nil- :project-id nil- :location nil}- (let [creds (effective-google-credentials credentials)]- (when-not (metabot.settings/provider-credentials-complete? provider creds)- (throw (ex-info (tru "Google credentials are incomplete.")- {:status-code 400- :api-error true- :missing-keys (vec (remove #(non-blank-string (get creds %))- [:service-account-key :oauth-access-token :project-id]))})))- creds)))-- (when (contains? body :api-key)- {:api-key (non-blank-string api-key)})))--(defn- check-not-env-shadowed!- "Throw a 400 when writing `value` to `setting-key` would be silently ignored because an env var shadows it."- [setting-key value]- (when (and (some? (setting/env-var-value setting-key))- ;; If value is nil we are attempting to clear the setting. We allow this so that callers can still- ;; disconnect a provider even if some settings are shadowed by env vars.- (some? value)- (not= value (setting/get setting-key)))- (throw (ex-info (tru "This setting is set by the {0} environment variable and cannot be changed via the API."- (setting/env-var-name setting-key))- {:status-code 400- :setting setting-key}))))--(defn- credential-setting-writes- "The settings that should be saved to store `credentials` for `provider`.- Returns `[setting-key value]` pairs. [[save-credentials!]] applies them.- An optional field is written only when actually present in the `credentials` map.- A disconnect includes every field explicitly, so it still clears them all."- [provider credentials]- (let [always (fn [setting-key field]- [[setting-key (get credentials field)]])- optional (fn [setting-key field]- (when (contains? credentials field)- [[setting-key (get credentials field)]]))]- (case provider- "bedrock" (concat (always :llm-bedrock-access-key-id :access-key-id)- (always :llm-bedrock-secret-access-key :secret-access-key)- (always :llm-bedrock-session-token :session-token)- (optional :llm-bedrock-region :region))- "azure" (concat (always :llm-azure-api-key :api-key)- (always :llm-azure-api-base-url :base-url))- "google" (concat (optional :llm-google-service-account-key :service-account-key)- (optional :llm-google-oauth-access-token :oauth-access-token)- (optional :llm-google-project-id :project-id)- (optional :llm-google-location :location))- (always (provider-api-key-setting-key provider) :api-key))))--(defn- set-setting-unless-env-shadowed!- "Write `value` to `setting-key`, unless an env var supplies the setting and would mask the write.- A nil `value` always goes through. Skipping a clear would leave stale values in the appdb after a disconnect."- [setting-key value]- (when (or (nil? value)- (not (setting/env-var-value setting-key)))- (setting/set! setting-key value)))--(defn- save-credentials!- "Persist the credentials override resolved by [[request-credentials]]; nil leaves the saved settings untouched."- [provider credentials]- (when credentials- (doseq [[setting-key value] (credential-setting-writes provider credentials)]- (set-setting-unless-env-shadowed! setting-key value))))--(api.macros/defendpoint :put "/settings"- :- metabot-settings-response-schema- "Update the Metabot provider credentials and/or model setting and return the refreshed settings payload."- [_route-params- _query-params- body :- metabot-settings-request-schema]- (perms/check-has-application-permission :setting)- (let [{:keys [provider] request-model :model} body- credentials (request-credentials provider body)- current-provider (current-setting-provider)- provider-changed? (not= current-provider provider)- model (cond- (non-blank-string request-model)- (effective-provider-model provider request-model)-- provider-changed?- (or (effective-provider-model provider request-model)- (metabot.settings/default-model-for-provider provider))-- :else- nil)- ;; Azure has no default model: the FE composes `{family}/{deployment}` from required- ;; inputs, so a connect (provider switch) without one is a malformed request, and a- ;; supplied model must parse before the validation round-trip relies on its wire family.- _ (when (= provider "azure")- (when (and provider-changed? (nil? model))- (throw (ex-info (tru "A model provider and deployment name are required to connect Azure.")- {:status-code 400- :api-error true- :provider provider})))- (when model- (metabot.settings/validate-azure-model! (str provider "/" model) model)))- ;; Reject writes to env-shadowed settings before verifying or persisting anything.- _ (when credentials- (run! (partial apply check-not-env-shadowed!)- (credential-setting-writes provider credentials)))- _ (when model- (check-not-env-shadowed! :llm-metabot-provider (str provider "/" model)))- ;; Azure connect validation needs the candidate model's wire family, and the Google connect- ;; probe needs it to check the model is served in the configured location; credential-only- ;; rotations on a connected provider fall back to the saved model.- validation-model (when (contains? #{"azure" "google"} provider)- (or model- (when-not provider-changed?- (provider-util/provider-and-model->model (metabot.settings/llm-metabot-provider)))))- ;; The model listing validates the request credentials before anything is saved.- response (-> (settings-response provider {:credentials credentials- :model validation-model})- throw-credentials-error!)]- (when credentials- (save-credentials! provider credentials))- (when model- (set-setting-unless-env-shadowed! :llm-metabot-provider (str provider "/" model)))- (assoc response :value (metabot.settings/llm-metabot-provider))))-(def ^{:arglists '([request respond raise])} routes"`/api/metabot` routes."(handlers/routesdiff --git a/src/metabase/metabot/core.clj b/src/metabase/metabot/core.cljindex eab8c953556..29b10d60023 100644--- a/src/metabase/metabot/core.clj+++ b/src/metabase/metabot/core.clj@@ -1,7 +1,6 @@(ns metabase.metabot.core"API namespace for the `metabase.metabot` module."(:require- [metabase.metabot.provider-util][metabase.metabot.scope][metabase.metabot.search-models][metabase.metabot.self]@@ -11,9 +10,6 @@[potemkin :as p]))(p/import-vars- [metabase.metabot.provider-util- metabase-provider?- provider-and-model->provider][metabase.metabot.scopeagent-collection-createagent-dashboard-creatediff --git a/src/metabase/metabot/persistence.clj b/src/metabase/metabot/persistence.cljindex b18ba219ba4..cdc0473eed6 100644--- a/src/metabase/metabot/persistence.clj+++ b/src/metabase/metabot/persistence.clj@@ -5,9 +5,9 @@[metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.app-db.core :as app-db]+ [metabase.llm.provider :as llm.provider][metabase.metabot.agent.memory :as memory][metabase.metabot.agent.streaming :as streaming]- [metabase.metabot.provider-util :as provider-util][metabase.metabot.schema :as metabot.schema][metabase.metabot.schema.migrate-v1-to-v2 :as migrate][metabase.metabot.schema.v2 :as schema.v2]@@ -274,7 +274,7 @@originator-id (or user-id api/*current-user-id*)ai-proxy? (if (some? ai-proxy?)ai-proxy?- (provider-util/metabase-provider? (metabot.settings/llm-metabot-provider)))+ (llm.provider/managed-model-ref? (metabot.settings/llm-metabot-provider)))user-external-id (or user-external-id (str (random-uuid)))assistant-external-id (or assistant-external-id (str (random-uuid)))](analytics/inc! :metabase-metabot/turn-started@@ -339,7 +339,7 @@Returns `{:assistant-msg-id <pk> :assistant-external-id <uuid-str> :user-external-id <uuid-str>}`."[conversation-id profile-id retry-message-external-id & {:keys [assistant-external-id delete-message-ids]}]- (let [ai-proxy? (provider-util/metabase-provider? (metabot.settings/llm-metabot-provider))+ (let [ai-proxy? (llm.provider/managed-model-ref? (metabot.settings/llm-metabot-provider))assistant-external-id (or assistant-external-id (str (random-uuid)))](analytics/inc! :metabase-metabot/turn-started{:profile-id (or profile-id "unknown")})diff --git a/src/metabase/metabot/provider_util.clj b/src/metabase/metabot/provider_util.cljdeleted file mode 100644index c693121faad..00000000000--- a/src/metabase/metabot/provider_util.clj+++ /dev/null@@ -1,60 +0,0 @@-(ns metabase.metabot.provider-util- "Utility functions for parsing `provider-and-model` strings.-- These strings have the format `provider/model` (e.g. `anthropic/claude-haiku-4-5`,- `openrouter/anthropic/claude-haiku-4.5`) or `metabase/provider/model` when routed- through the Metabase Cloud AI proxy."- (:require- [clojure.string :as str]))--(def metabase-provider-prefix- "The provider prefix that signals requests should be routed through the- Metabase Cloud AI proxy."- "metabase")--(defn metabase-provider?- "Returns true when the provider-and-model string uses the Metabase AI proxy- (i.e. starts with `metabase/`)."- [provider-and-model]- (boolean (some-> provider-and-model (str/starts-with? (str metabase-provider-prefix "/")))))--(defn provider-and-model->outer-provider- "Extract the top-level provider prefix from a provider-and-model string.- For `metabase/anthropic/model` returns `\"metabase\"`.- For `anthropic/model` returns `\"anthropic\"`."- [provider-and-model]- (when provider-and-model- (first (str/split provider-and-model #"/" 2))))--(defn provider-and-model->provider- "Extract the direct provider from a provider-and-model string.- For `metabase/anthropic/model` returns `\"anthropic\"`.- For `anthropic/model` returns `\"anthropic\"`."- [provider-and-model]- (when provider-and-model- (let [[first-seg rest-seg] (str/split provider-and-model #"/" 2)]- (if (= first-seg metabase-provider-prefix)- (first (str/split rest-seg #"/" 2))- first-seg))))--(defn provider-and-model->model- "Extract the model name from a provider-and-model string.- For `metabase/anthropic/model` returns `\"model\"`.- For `anthropic/model` returns `\"model\"`.- For `openrouter/anthropic/claude-haiku-4.5` returns `\"anthropic/claude-haiku-4.5\"`."- [provider-and-model]- (when provider-and-model- (let [[first-seg rest-seg] (str/split provider-and-model #"/" 2)]- (if (= first-seg metabase-provider-prefix)- (second (str/split rest-seg #"/" 2))- rest-seg))))--(defn strip-metabase-prefix- "Strip the `metabase/` routing prefix from a provider-and-model string.- For `metabase/openrouter/anthropic/claude-haiku-4.5` returns- `\"openrouter/anthropic/claude-haiku-4.5\"`.- Returns the input unchanged if no prefix is present."- [provider-and-model]- (if (metabase-provider? provider-and-model)- (str/replace-first provider-and-model (str metabase-provider-prefix "/") "")- provider-and-model))diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex 564f503e52d..885b6b74db6 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -13,7 +13,7 @@[metabase.analytics-interface.core :as analytics][metabase.analytics.core :as analytics.core][metabase.api.common :as api]- [metabase.metabot.provider-util :as provider-util]+ [metabase.llm.provider :as llm.provider][metabase.metabot.scope :as scope][metabase.metabot.self.azure :as azure][metabase.metabot.self.bedrock :as bedrock]@@ -28,6 +28,7 @@[metabase.metabot.settings :as metabot.settings][metabase.metabot.usage :as usage][metabase.util :as u]+ [metabase.util.i18n :refer [tru]][metabase.util.log :as log][metabase.util.o11y :refer [with-span]]))@@ -63,12 +64,23 @@(throw (ex-info (str "Unknown LLM provider: " provider){:provider provider}))))-(defn- parse-provider-model [s]- (let [provider (provider-util/provider-and-model->provider s)]- {:provider provider- :stream-fn (resolve-adapter provider)- :model (provider-util/provider-and-model->model s)- :ai-proxy? (provider-util/metabase-provider? s)}))+(defn- parse-provider-model+ "Resolve a `connection-key/model` string into the adapter, model, and credentials needed to serve it.+ Throws a 400 when the string names a connection that is not configured, so a stale+ `llm-metabot-provider` surfaces as a clear error rather than an unauthenticated request."+ [s]+ (let [{:keys [type model credentials ai-proxy?]}+ (or (llm.provider/resolve-model-ref s)+ (throw (ex-info (tru "No LLM provider connection named {0} is configured."+ (pr-str (llm.provider/model-ref->connection-key s)))+ {:status-code 400+ :api-error true+ :model-ref s})))]+ {:provider type+ :stream-fn (resolve-adapter type)+ :model model+ :credentials credentials+ :ai-proxy? ai-proxy?}))(defn list-models"List available models for a provider using its configured credentials, or `:credentials` in `opts`.@@ -409,15 +421,16 @@(error-reducible limit-msg "ai_usage_limit_reached"))(when-let [missing (missing-required-permission (:required-permission tracking-opts))](error-reducible (format "Permission denied: %s required" missing) "permission_denied"))- (let [{:keys [provider stream-fn model ai-proxy?]} (parse-provider-model provider-and-model)]+ (let [{:keys [provider stream-fn model credentials ai-proxy?]} (parse-provider-model provider-and-model)](log/info "Calling LLM" {:provider provider :model model :parts (count parts) :tools (count tools):tool-choice tool-choice :ai-proxy? ai-proxy?})(let [tracking-opts (assoc tracking-opts :model provider-and-model :ai-proxy? ai-proxy?)- streaming-opts (cond-> {:model model :input parts :tools (vals tools) :ai-proxy? ai-proxy?}- system-msg (assoc :system system-msg)+ streaming-opts (cond-> {:model model :input parts :tools (vals tools)+ :credentials credentials :ai-proxy? ai-proxy?}+ system-msg (assoc :system system-msg)(and (seq tools)- tool-choice) (assoc :tool_choice tool-choice)- (:session-id tracking-opts) (assoc :prompt-cache-key (:session-id tracking-opts)))+ tool-choice) (assoc :tool_choice tool-choice)+ (:session-id tracking-opts) (assoc :prompt-cache-key (:session-id tracking-opts)))make-source (fn [](eduction (comp (core/tool-executor-xf tools)(core/lite-aisdk-xf)@@ -472,7 +485,7 @@:error-code "ai_usage_limit_reached":message limit-msg})))(check-permission! (:required-permission opts))- (let [{:keys [provider stream-fn model ai-proxy?]} (parse-provider-model provider-and-model)+ (let [{:keys [provider stream-fn model credentials ai-proxy?]} (parse-provider-model provider-and-model)[system-msg input] (if (= "system" (some-> messages first :role name))[(:content (first messages)) (vec (rest messages))][nil messages])@@ -488,6 +501,7 @@:schema json-schema:temperature temperature:max-tokens max-tokens+ :credentials credentials:ai-proxy? ai-proxy?}system-msg (assoc :system system-msg)(contains? opts :cache?) (assoc :cache? (:cache? opts))diff --git a/src/metabase/metabot/self/azure.clj b/src/metabase/metabot/self/azure.cljindex fd85414d863..5c98b5db51d 100644--- a/src/metabase/metabot/self/azure.clj+++ b/src/metabase/metabot/self/azure.clj@@ -16,8 +16,7 @@deployment-scoped endpoints or Entra ID auth)."(:require[clojure.string :as str]- [metabase.llm.settings :as llm]- [metabase.metabot.provider-util :as provider-util]+ [metabase.llm.provider :as llm.provider][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core][metabase.metabot.self.debug :as debug]@@ -42,9 +41,11 @@(str/starts-with? (str model) "openai/") :openai:else(throw (ex-info (tru "Unsupported Azure model {0}. Only anthropic/* and openai/* models are supported." (pr-str model))- {:api-error true- :error-code :unsupported-model- :model model}))))+ {:api-error true+ :error-code :unsupported-model+ ;; a deployment the admin typed, so this is a bad request rather than an outage+ :status-code 400+ :model model}))))(defn- model->deployment"The Azure deployment name carried by a `{family}/{deployment-name}` model string."@@ -53,12 +54,6 @@;;; ------------------------------------------------ HTTP plumbing -----------------------------------------------(defn- settings-credentials- "Azure credentials from the `llm-azure-*` settings."- []- {:api-key (not-empty (llm/llm-azure-api-key))- :base-url (not-empty (llm/llm-azure-api-base-url))})-(defn- missing-credentials-ex [](ex-info (tru "Azure credentials are not configured"){:api-error true@@ -71,13 +66,12 @@:error-code :proxy-unsupported}))(defn- ensure-credentials- "Validate an Azure credentials map, falling back to [[settings-credentials]] when nil.+ "Validate the credentials of the connection serving this request.Throws when the API key or base URL is missing."[credentials]- (let [creds (or credentials (settings-credentials))]- (when-not (metabot.settings/provider-credentials-complete? "azure" creds)- (throw (missing-credentials-ex)))- creds))+ (when-not (llm.provider/credentials-complete? "azure" credentials)+ (throw (missing-credentials-ex)))+ credentials)(defn- azure-error-msg"Canonical, status-specific Azure error message."@@ -93,8 +87,8 @@(defn- azure-request"Perform an HTTP request against the Azure resource's compatible surface.- `credentials` is an optional `{:api-key ... :base-url ...}` map; when nil, the- `llm-azure-*` settings are used. `headers` are extra headers (e.g. `anthropic-version`).+ `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 proxyredirection is already wired up should Azure proxying ever be supported."@@ -141,11 +135,11 @@(throw e)))))(defn- configured-azure-model- "The saved `{family}/{deployment}` model string when the configured Metabot provider is Azure."+ "The saved `{family}/{deployment}` model string when the connection Metabot is pointed at is an Azure one."[]- (let [value (metabot.settings/llm-metabot-provider)]- (when (= (provider-util/provider-and-model->provider value) "azure")- (provider-util/provider-and-model->model value))))+ (let [{:keys [type model]} (llm.provider/resolve-model-ref (metabot.settings/llm-metabot-provider))]+ (when (= type "azure")+ model)))(defn list-models"Validate Azure credentials with a model-free round trip and return an empty model list.@@ -174,8 +168,9 @@(mu/defn azure-raw"Perform a streaming request to an Azure-hosted model deployment.- `:ai-proxy?` is not supported for Azure and throws when true."- [{:keys [model input tools ai-proxy?] :as opts} :- core/LLMRequestOpts]+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws 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](let [family (model->family model)opts (assoc opts :model (model->deployment model) :reasoning? false){:keys [path headers req]}@@ -191,12 +186,13 @@: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)- :ai-proxy? ai-proxy?})]+ (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.diff --git a/src/metabase/metabot/self/bedrock.clj b/src/metabase/metabot/self/bedrock.cljindex 63c2d9cc45d..ec23eeaa4f7 100644--- a/src/metabase/metabot/self/bedrock.clj+++ b/src/metabase/metabot/self/bedrock.clj@@ -17,12 +17,12 @@https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html"(:require[clojure.string :as str]+ [metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm][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.metabot.settings :as metabot.settings][metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json]@@ -100,14 +100,6 @@(throw (invalid-region-ex region)))region)-(defn- settings-credentials- "AWS credentials and region from the `llm-bedrock-*` settings."- []- {:access-key-id (not-empty (llm/llm-bedrock-access-key-id))- :secret-access-key (not-empty (llm/llm-bedrock-secret-access-key))- :session-token (not-empty (llm/llm-bedrock-session-token))- :region (not-empty (llm/llm-bedrock-region))})-(defn- missing-credentials-ex [](ex-info (tru "AWS Bedrock credentials are not configured"){:api-error true@@ -120,13 +112,12 @@:error-code :proxy-unsupported}))(defn- ensure-credentials- "Validate a Bedrock credentials map, falling back to [[settings-credentials]] when nil.+ "Validate the credentials of the connection serving this request.Throws when the access key pair is incomplete or the region is unknown; the region defaults to us-east-1."[credentials]- (let [creds (or credentials (settings-credentials))]- (when-not (metabot.settings/provider-credentials-complete? "bedrock" creds)- (throw (missing-credentials-ex)))- (update creds :region #(validate-region (or (not-empty %) "us-east-1")))))+ (when-not (llm.provider/config-complete? "bedrock" credentials)+ (throw (missing-credentials-ex)))+ (update credentials :region #(validate-region (or (not-empty %) "us-east-1"))))(defn- bedrock-error-msg"Canonical, status-specific Bedrock error message."@@ -142,9 +133,9 @@(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 an optional- AWS credentials map; when nil, the `llm-bedrock-*` settings are used. `ai-proxy?` is accepted- for parity with the other provider adapters but is not supported: throws when true."+ `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)))@@ -248,8 +239,10 @@(mu/defn bedrock-raw"Perform a streaming request to the Bedrock mantle endpoint.+ Opts map takes `:credentials` from the connection serving this request — `:access-key-id`, `:secret-access-key`,+ `:region`, and (for temporary credentials) `:session-token` — and throws when they are missing.`:ai-proxy?` is not supported for Bedrock and throws when true."- [{:keys [model input tools ai-proxy?] :as opts+ [{:keys [model input tools credentials ai-proxy?] :as opts:or {model default-model}} :- core/LLMRequestOpts](let [opts (assoc opts :model model :reasoning? false)family (model->family model)@@ -266,12 +259,13 @@: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)- :ai-proxy? ai-proxy?})]+ (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.diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.cljindex 2c65863bbcf..fa73f58a9a5 100644--- a/src/metabase/metabot/self/claude.clj+++ b/src/metabase/metabot/self/claude.clj@@ -1,7 +1,6 @@(ns metabase.metabot.self.claude(:require[clojure.string :as str]- [metabase.llm.settings :as llm][metabase.metabot.self.core :as core][metabase.metabot.self.debug :as debug][metabase.metabot.self.schema :as schema]@@ -363,13 +362,13 @@(defn- list-all-models"Fetch the full Anthropic model catalog (`GET /v1/models`).- No-arg uses the configured API key. Opts map supports `:credentials` (`{:api-key ...}`) and `:ai-proxy?`."+ 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 (or (not-empty (:api-key credentials))- (not-empty (llm/llm-anthropic-api-key)))]- {:url (llm/llm-anthropic-api-base-url)+ (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@@ -381,7 +380,8 @@(defn list-models"List the Anthropic chat models supported by this adapter (see [[supported-models]]).- No-arg uses the configured API key. Opts map supports `:credentials` (`{:api-key ...}`) and `:ai-proxy?`."+ 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)@@ -477,8 +477,10 @@(assoc :temperature temperature))))(mu/defn claude-raw- "Perform a streaming request to Claude API."- [{:keys [model input tools ai-proxy?] :as opts+ "Perform a streaming request to Claude API.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing."+ [{:keys [model input tools credentials ai-proxy?] :as opts:or {model "claude-haiku-4-5"}} :- core/LLMRequestOpts](let [req (claude-request-body opts)](with-span :info {:name :metabot.claude/request@@ -486,10 +488,10 @@:msg-count (count input):tool-count (count tools)}(try- (let [api-key (not-empty (llm/llm-anthropic-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "anthropic" "Anthropic"(when api-key- {:url (llm/llm-anthropic-api-base-url)+ {:url (:base-url credentials):headers {"x-api-key" api-key}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.cljindex 60ff54cb311..4d79f60bf67 100644--- a/src/metabase/metabot/self/core.clj+++ b/src/metabase/metabot/self/core.clj@@ -47,6 +47,9 @@:max-tokens - Maximum tokens in the response:schema - JSON Schema map for structured output; each provider forces atool call (Claude, OpenRouter) or uses json_schema mode (OpenAI)+ :credentials - Credentials of the provider connection serving this request, in that provider+ type's `:config` shape (e.g. `{:api-key ...}`), with the type's field defaults+ filled in. An adapter serves a request from these alone and throws without them.:ai-proxy? - When true, skip provider auth and use the Metabase AI proxy:reasoning? - When false, don't request thinking/reasoning and strip:reasoning parts from the replayed input (defaults true)@@ -61,6 +64,7 @@[:temperature {:optional true} [:maybe number?]][:max-tokens {:optional true} [:maybe :int]][:schema {:optional true} :any]+ [:credentials {:optional true} [:maybe :map]][:ai-proxy? {:optional true} [:maybe :boolean]][:reasoning? {:optional true} [:maybe :boolean]][:prompt-cache-key {:optional true} [:maybe :string]]])diff --git a/src/metabase/metabot/self/google.clj b/src/metabase/metabot/self/google.cljindex 3409ac5ae39..a10fc40f3dd 100644--- a/src/metabase/metabot/self/google.clj+++ b/src/metabase/metabot/self/google.clj@@ -14,23 +14,24 @@production deployments. Short-lived OAuth access tokens can be generated via `gcloud auth print-access-token` andare useful for local testing. If both are configured, then the service account key is used.- A project id is also required. This can either be extracted from the service account key JSON or provided explicitly- via the [[llm/llm-google-project-id]] setting (required when using an OAuth token).+ A project id is also required. This can either be extracted from the service account key JSON or provided+ explicitly by the connection (required when using an OAuth token).You can also specify which Google Cloud location requests should be served from. The default is `global`, but wealso support the multi-region `us` and `eu` locations, as well as specific locations like `us-east1` or`europe-west2` etc.- The effective endpoint URL depends on the location setting, but can also be overridden via an env var.+ The effective endpoint URL depends on the connection's location, but the connection can also name one outright.See [[api-base-url]] for details."(:require[clojure.string :as str]+ [metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm]- [metabase.metabot.provider-util :as provider-util][metabase.metabot.self.core :as core][metabase.metabot.self.debug :as debug][metabase.metabot.self.google.stream-generate-content :as stream-generate-content][metabase.metabot.settings :as metabot.settings]+ [metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json][metabase.util.malli :as mu]@@ -50,14 +51,6 @@;;; Auth / HTTP plumbing-(defn- settings-credentials- "Returns the Google credentials from the `llm-google-*` settings."- []- {:service-account-key (not-empty (llm/llm-google-service-account-key))- :oauth-access-token (not-empty (llm/llm-google-oauth-access-token))- :project-id (not-empty (llm/llm-google-project-id))- :location (not-empty (llm/llm-google-location))})-(def ^:private cloud-platform-scope"The OAuth2 scope for access tokens made from the service account key.There is also a `cloud-platform.read-only` scope, but inference calls made with the read-only scope are rejected."@@ -163,8 +156,8 @@locations. Thus a non-global location gets its host from [[location-host]], and the admin does not have to set asecond setting. If the admin set a base URL, for example a proxy or a test double, this function returns it withouta change."- [credentials]- (let [configured (llm/llm-google-api-base-url)+ [{:keys [base-url] :as credentials}]+ (let [configured (or (not-empty base-url) llm/google-global-api-base-url)location (effective-location credentials)](if (and (not= location global-location)(= configured llm/google-global-api-base-url))@@ -174,15 +167,21 @@(defn- resolve-google-auth"Resolves the `{:auth {:url ... :headers ...} :credentials ...}` pair for a Google request.- If `credentials` is nil, uses [[settings-credentials]]. 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."+ 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})))- (let [{:keys [service-account-key oauth-access-token project-id] :as creds} (or credentials (settings-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))@@ -250,9 +249,7 @@The `llm-metabot-provider` setting takes the Google model as free text — unlike a whitelisted provider, nothingupstream of this constrains it."[credentials model]- (let [model-str (str model)- publisher (provider-util/provider-and-model->provider model-str)- model-id (provider-util/provider-and-model->model model-str)]+ (let [[publisher model-id] (str/split (str model) #"/" 2)](when (str/blank? model-id)(throw (ex-info (tru "Invalid Google model {0} — expected a publisher-qualified ID like \"google/gemini-3.5-flash\""(pr-str model))@@ -304,7 +301,6 @@- For 404s include a hint to check that the provided location is correct."[credentials e](let [data (ex-data e)- credentials (or credentials (settings-credentials))location (:location credentials)known? (conj multi-region-locations global-location)endpoint (delay (try (api-base-url credentials) (catch Exception _ nil)))@@ -348,11 +344,11 @@nil)(defn- configured-google-model- "Returns the saved model string if the configured Metabot provider is Google."+ "The publisher-qualified model of the connection Metabot is pointed at, when that connection is a Google one."[]- (let [value (metabot.settings/llm-metabot-provider)]- (when (= (provider-util/provider-and-model->provider value) "google")- (provider-util/provider-and-model->model value))))+ (let [{:keys [type model]} (llm.provider/resolve-model-ref (metabot.settings/llm-metabot-provider))]+ (when (= type "google")+ model)))(defn list-models"Validates the Google credentials and the candidate model with a free `countTokens` call.diff --git a/src/metabase/metabot/self/mistral.clj b/src/metabase/metabot/self/mistral.cljindex ece24e25d40..86a5efd4f3f 100644--- a/src/metabase/metabot/self/mistral.clj+++ b/src/metabase/metabot/self/mistral.clj@@ -5,7 +5,6 @@https://docs.mistral.ai/api/"(: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]@@ -59,9 +58,8 @@(throw (ai-proxy-unsupported-ex)))(try(let [auth (core/resolve-auth "mistral" "Mistral"- (when-let [k (or (not-empty (:api-key credentials))- (not-empty (llm/llm-mistral-api-key)))]- {:url (llm/llm-mistral-api-base-url)+ (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@@ -101,8 +99,10 @@(mu/defn mistral-raw"Perform a streaming request to the Mistral Chat Completions API.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing.`:ai-proxy?` is not supported for Mistral and throws when true."- [{:keys [model tools ai-proxy?] :as opts+ [{:keys [model tools credentials ai-proxy?] :as opts:or {model default-model}} :- core/LLMRequestOpts](when ai-proxy?(throw (ai-proxy-unsupported-ex)))@@ -113,10 +113,10 @@:msg-count (count (:messages req)):tool-count (count (or tools []))}(try- (let [api-key (not-empty (llm/llm-mistral-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "mistral" "Mistral"(when api-key- {:url (llm/llm-mistral-api-base-url)+ {:url (:base-url credentials):headers {"Authorization" (str "Bearer " api-key)}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/self/moonshot.clj b/src/metabase/metabot/self/moonshot.cljindex 7f4f07e0343..9e089934c4d 100644--- a/src/metabase/metabot/self/moonshot.clj+++ b/src/metabase/metabot/self/moonshot.clj@@ -5,7 +5,6 @@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]@@ -72,9 +71,8 @@(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)+ (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@@ -123,8 +121,10 @@(mu/defn moonshot-raw"Perform a streaming request to the Moonshot Chat Completions API.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing.`:ai-proxy?` is not supported for Moonshot and throws when true."- [{:keys [model tools ai-proxy?] :as opts+ [{:keys [model tools credentials ai-proxy?] :as opts:or {model default-model}} :- core/LLMRequestOpts](when ai-proxy?(throw (ai-proxy-unsupported-ex)))@@ -135,10 +135,10 @@:msg-count (count (:messages req)):tool-count (count (or tools []))}(try- (let [api-key (not-empty (llm/llm-moonshot-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "moonshot" "Moonshot"(when api-key- {:url (llm/llm-moonshot-api-base-url)+ {:url (:base-url credentials):headers {"Authorization" (str "Bearer " api-key)}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/self/openai.clj b/src/metabase/metabot/self/openai.cljindex 7114276823a..24de5677fbd 100644--- a/src/metabase/metabot/self/openai.clj+++ b/src/metabase/metabot/self/openai.clj@@ -1,7 +1,6 @@(ns metabase.metabot.self.openai(:require[clojure.string :as str]- [metabase.llm.settings :as llm][metabase.metabot.self.core :as core][metabase.metabot.self.debug :as debug][metabase.metabot.self.schema :as schema]@@ -276,9 +275,8 @@(throw (ai-proxy-unsupported-ex)))(try(let [auth (core/resolve-auth "openai" "OpenAI"- (when-let [k (or (not-empty (:api-key credentials))- (not-empty (llm/llm-openai-api-key)))]- {:url (llm/llm-openai-api-base-url)+ (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@@ -291,7 +289,8 @@(defn list-models"List the OpenAI chat models supported by this adapter (see [[supported-models]]).- No-arg uses the configured API key. Opts map supports `:credentials` (`{:api-key ...}`) and `:ai-proxy?`.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,+ and throws when they are missing. Also supports `:ai-proxy?`.`:ai-proxy?` is not supported for OpenAI and throws when true."([] (list-models {}))([opts]@@ -352,17 +351,19 @@(mu/defn openai-raw"Perform a streaming request to OpenAI Responses API.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing.`:ai-proxy?` is not supported for OpenAI and throws when true."- [{:keys [model ai-proxy?] :as opts+ [{: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 (llm/llm-openai-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "openai" "OpenAI"(when api-key- {:url (llm/llm-openai-api-base-url)+ {:url (:base-url credentials):headers {"Authorization" (str "Bearer " api-key)}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/self/openrouter.clj b/src/metabase/metabot/self/openrouter.cljindex 8276ef27e6b..39f16e5d825 100644--- a/src/metabase/metabot/self/openrouter.clj+++ b/src/metabase/metabot/self/openrouter.clj@@ -9,7 +9,6 @@adapter converts those directly to Chat Completions messages."(:require[clojure.string :as str]- [metabase.llm.settings :as llm][metabase.metabot.self.claude :as claude][metabase.metabot.self.core :as core][metabase.metabot.self.debug :as debug]@@ -86,9 +85,8 @@(throw (ai-proxy-unsupported-ex)))(try(let [auth (core/resolve-auth "openrouter" "OpenRouter"- (when-let [k (or (not-empty (:api-key credentials))- (not-empty (llm/llm-openrouter-api-key)))]- {:url (llm/llm-openrouter-api-base-url)+ (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@@ -103,7 +101,8 @@(defn list-models"List the OpenRouter models supported by this adapter (see [[supported-models]]).- No-arg uses the configured API key. Opts map supports `:credentials` (`{:api-key ...}`) and `:ai-proxy?`.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request,+ and throws when they are missing. Also supports `:ai-proxy?`.`:ai-proxy?` is not supported for OpenRouter and throws when true."([] (list-models {}))([opts]@@ -154,8 +153,10 @@Works with OpenRouter, or any OpenAI-compatible endpoint that supports`/v1/chat/completions` (e.g. vLLM, Ollama, Together, etc.).+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing.`:ai-proxy?` is not supported for OpenRouter and throws when true."- [{:keys [model tools ai-proxy?] :as opts+ [{: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)))@@ -166,10 +167,10 @@:msg-count (count (:messages req)):tool-count (count (or tools []))}(try- (let [api-key (not-empty (llm/llm-openrouter-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "openrouter" "OpenRouter"(when api-key- {:url (llm/llm-openrouter-api-base-url)+ {:url (:base-url credentials):headers {"Authorization" (str "Bearer " api-key)}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/self/zai.clj b/src/metabase/metabot/self/zai.cljindex e2c19d171db..61df9e76038 100644--- a/src/metabase/metabot/self/zai.clj+++ b/src/metabase/metabot/self/zai.clj@@ -5,7 +5,6 @@https://docs.z.ai/api-reference/llm/chat-completion"(: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]@@ -59,9 +58,8 @@(throw (ai-proxy-unsupported-ex)))(try(let [auth (core/resolve-auth "zai" "Z.AI"- (when-let [k (or (not-empty (:api-key credentials))- (not-empty (llm/llm-zai-api-key)))]- {:url (llm/llm-zai-api-base-url)+ (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@@ -95,8 +93,10 @@(mu/defn zai-raw"Perform a streaming request to the Z.AI Chat Completions API.+ Opts map takes `:credentials` (`{:api-key ... :base-url ...}`) from the connection serving this request, and+ throws when they are missing.`:ai-proxy?` is not supported for Z.AI and throws when true."- [{:keys [model tools ai-proxy?] :as opts+ [{:keys [model tools credentials ai-proxy?] :as opts:or {model default-model}} :- core/LLMRequestOpts](when ai-proxy?(throw (ai-proxy-unsupported-ex)))@@ -107,10 +107,10 @@:msg-count (count (:messages req)):tool-count (count (or tools []))}(try- (let [api-key (not-empty (llm/llm-zai-api-key))+ (let [api-key (not-empty (:api-key credentials))auth (core/resolve-auth "zai" "Z.AI"(when api-key- {:url (llm/llm-zai-api-base-url)+ {:url (:base-url credentials):headers {"Authorization" (str "Bearer " api-key)}})ai-proxy?)response (core/request authdiff --git a/src/metabase/metabot/settings.clj b/src/metabase/metabot/settings.cljindex 1d49c985feb..da71f4d1f5e 100644--- a/src/metabase/metabot/settings.clj+++ b/src/metabase/metabot/settings.clj@@ -1,8 +1,8 @@(ns metabase.metabot.settings(:require[clojure.string :as str]+ [metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm.settings]- [metabase.metabot.provider-util :as provider-util][metabase.metabot.self.claude :as claude][metabase.metabot.self.openai :as openai][metabase.settings.core :as setting :refer [defsetting]]@@ -98,78 +98,13 @@;;; ------------------------------------------------- LLM Provider -------------------------------------------------(def ^:private direct-providers- "Providers that can be used directly (not via the metabase/ proxy prefix)."- #{"anthropic" "azure" "bedrock" "google" "mistral" "moonshot" "openai" "openrouter" "zai"})--(def ^:private default-anthropic-llm-metabot-model- "Default Anthropic model used for Metabot when no explicit model is selected."- "claude-sonnet-4-6")--(def ^:private default-bedrock-llm-metabot-model- "Default Bedrock model used for Metabot when no explicit model is selected."- "anthropic.claude-opus-4-8")--(def ^:private default-google-llm-metabot-model- "Default Google model used for Metabot when no explicit model is selected.- Note that Google model IDs include their `{publisher}/{model}` qualifier."- "google/gemini-3.5-flash")--(def ^:private default-mistral-llm-metabot-model- "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")--(def ^:private default-openrouter-llm-metabot-model- "Default OpenRouter model used for Metabot when no explicit model is selected.- Note that OpenRouter model IDs use dots in version numbers (`claude-sonnet-4.6`),- unlike the Anthropic API's hyphenated IDs (`claude-sonnet-4-6`)."- "anthropic/claude-sonnet-4.6")--(def ^:private default-zai-llm-metabot-model- "Default Z.AI model used for Metabot when no explicit model is selected."- "glm-5.2")-(def default-llm-metabot-provider- "Default provider/model used for Metabot when no explicit model is selected."- (str "anthropic/" default-anthropic-llm-metabot-model))--(def default-llm-metabot-model-by-provider- "Default model payload keyed by provider for `PUT /api/metabot/settings`.-- Values match the shape expected in the request body for each provider: direct providers use a bare model ID (except- OpenRouter and Google, whose IDs are publisher-qualified), while the managed `metabase` provider uses the proxied- `provider/model` form."- {"anthropic" default-anthropic-llm-metabot-model- "bedrock" default-bedrock-llm-metabot-model- "google" default-google-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- provider-util/metabase-provider-prefix default-llm-metabot-provider})+ "Default model reference used for Metabot when no explicit model is selected."+ "anthropic/claude-sonnet-4-6")(def default-metabase-llm-metabot-provider"Managed-provider version of [[default-llm-metabot-provider]]."- (str provider-util/metabase-provider-prefix "/" default-llm-metabot-provider))--(def ^:private proxied-providers-and-models- "Providers and models that can be used via the metabase managed AI proxy.-- The keys of this map must be a subset of the [[direct-providers]]."- {"anthropic" #{"claude-sonnet-4-6"}})--(def supported-metabot-providers- "Set of supported LLM provider prefixes for the `llm-metabot-provider` setting."- (conj direct-providers provider-util/metabase-provider-prefix))+ (str llm.provider/managed-connection-key "/" default-llm-metabot-provider))(def azure-model-families"Wire-protocol families for models hosted on Azure: the first segment of the azure model@@ -177,7 +112,7 @@#{"anthropic" "openai"})(defn validate-azure-model!- "Validate the model segment of an `azure/{family}/{deployment-name}` provider string:+ "Validate the model segment of an azure connection's `{family}/{deployment-name}` model:a supported wire family followed by a non-blank deployment name without slashes(Azure deployment names cannot contain `/`). Throws on invalid input."[value model]@@ -185,95 +120,47 @@(when-not (and (contains? azure-model-families family)(not (str/blank? deployment))(not (str/includes? deployment "/")))- (throw (ex-info (tru "Invalid Azure model {0}. Expected format: azure/<family>/<deployment-name> where <family> is one of: {1}"+ (throw (ex-info (tru "Invalid Azure model {0}. Expected format: <connection>/<family>/<deployment-name> where <family> is one of: {1}"(pr-str value)(str/join ", " (sort azure-model-families))){:status-code 400:value value})))))-(defn- validate-direct-provider!- "Validate that `value` is a `provider/model` string for one of the [[direct-providers]]- (i.e. *not* using the `metabase/` proxy prefix). Throws on invalid input."- [value]- (when (provider-util/metabase-provider? value)- (throw (ex-info (tru "Invalid direct provider {0}. Must not start with {1}/."- (pr-str value) provider-util/metabase-provider-prefix)- {:status-code 400- :value value})))- (let [provider (provider-util/provider-and-model->provider value)- model (provider-util/provider-and-model->model value)]- (when-not (contains? direct-providers provider)- (throw (ex-info (tru "Unknown provider {0}. Supported providers: {1}"- (pr-str provider)- (str/join ", " (sort supported-metabot-providers)))- {:status-code 400- :provider provider- :supported supported-metabot-providers})))- (when (str/blank? model)- (throw (ex-info (tru "Model name is required. Expected format: provider/model, e.g. \"anthropic/claude-haiku-4-5\"")- {:status-code 400- :value value})))- (when (= provider "azure")- (validate-azure-model! value model))))--(defn- validate-metabase-managed-provider!- "Validate that `value` is a `metabase/provider/model` string whose inner provider and- model are in the [[proxied-providers-and-models]] allow-list. Throws on invalid input."- [value]- (when-not (provider-util/metabase-provider? value)- (throw (ex-info (tru "Invalid metabase managed AI provider {0}. Must start with {1}/."- (pr-str value) provider-util/metabase-provider-prefix)- {:status-code 400- :value value})))- (let [inner-provider (provider-util/provider-and-model->provider value)- inner-model (provider-util/provider-and-model->model value)- allowed-providers (sort (keys proxied-providers-and-models))- allowed-models (get proxied-providers-and-models inner-provider)]- (when-not (contains? proxied-providers-and-models inner-provider)- (throw (ex-info (tru "Unsupported provider {0} for metabase managed AI. Supported providers: {1}"- (pr-str inner-provider)- (str/join ", " allowed-providers))- {:status-code 400- :provider inner-provider- :supported (set (keys proxied-providers-and-models))})))- (when (str/blank? inner-model)- (throw (ex-info (tru "Model name is required. Expected format: metabase/provider/model, e.g. {0}"- (pr-str default-metabase-llm-metabot-provider))- {:status-code 400- :value value})))- (when-not (contains? allowed-models inner-model)- (throw (ex-info (tru "Unsupported model {0} for metabase managed provider {1}. Supported models: {2}"- (pr-str inner-model)- (pr-str inner-provider)- (str/join ", " (sort allowed-models)))- {:status-code 400- :provider inner-provider- :model inner-model- :supported allowed-models})))))-(defn default-model-for-provider- "Return the default request-model payload for a provider.-- When `provider` is nil, fall back to the global default provider/model string."- [provider]- (if (nil? provider)- default-llm-metabot-provider- (get default-llm-metabot-model-by-provider provider)))+(defn- validate-managed-model!+ "Check `model` against the fixed catalog the Metabase AI proxy serves (see the `metabase` entry in+ [[metabase.llm.provider/provider-types]])."+ [model]+ (let [allowed (into #{} (map :id) (llm.provider/fixed-models llm.provider/managed-connection-key))]+ (when-not (contains? allowed model)+ (throw (ex-info (tru "Unsupported model {0} for Metabase managed AI. Supported models: {1}"+ (pr-str model) (str/join ", " (sort allowed)))+ {:status-code 400 :model model})))))(defn- validate-metabot-provider!- "Validate that `value` has the format `provider/model` with a supported provider prefix.- Dispatches to [[validate-metabase-managed-provider!]] when `value` uses the- `metabase/` proxy prefix and to [[validate-direct-provider!]] otherwise.+ "Validate that `value` is a `connection-key/model` string with a non-blank model, plus whatever extra rules the+ named connection's provider type imposes on its models.++ Deliberately does *not* require the connection to exist. The setting can legitimately be written before the+ connection it names — an env var, a config file, or a serdes import can land in either order — and a value naming+ a missing connection is already reported where it is actionable: [[llm-metabot-configured?]] reads false, and+ resolving it for a request throws a 400 that names the connection.+Throws an exception with `:status-code 400` on invalid input."[value](when-not (string? value)(throw (ex-info (tru "Metabot provider must be a string, got: {0}" (pr-str value)){:status-code 400})))- (if (provider-util/metabase-provider? value)- (validate-metabase-managed-provider! value)- (validate-direct-provider! value)))+ (let [model (llm.provider/model-ref->model value)]+ (when (str/blank? model)+ (throw (ex-info (tru "Model name is required. Expected format: connection/model, e.g. \"anthropic/claude-haiku-4-5\"")+ {:status-code 400 :value value})))+ (case (:type (llm.provider/connection (llm.provider/model-ref->connection-key value)))+ "azure" (validate-azure-model! value model)+ "metabase" (validate-managed-model! model)+ nil)))(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`, `moonshot/kimi-k3`, `openrouter/anthropic/claude-haiku-4.5`.")+ (deferred-tru "The AI provider connection and model for Metabot. Format: connection-key/model-name, e.g. `anthropic/claude-haiku-4-5`, `openai/gpt-5.4`, `openrouter/anthropic/claude-haiku-4.5`. The connection key names an entry in the `llm-providers` setting and defaults to the provider type."):type :string:encryption :no:default default-llm-metabot-provider@@ -285,86 +172,21 @@(validate-metabot-provider! new-value))(setting/set-value-of-type! :string :llm-metabot-provider new-value)))-(defn- non-blank- [value]- (when (string? value)- (let [trimmed (str/trim value)]- (when-not (str/blank? trimmed)- trimmed))))--(defn- configured-api-key-credentials- [api-key]- (when-let [k (non-blank api-key)]- {:api-key k}))--(defn provider-credentials-complete?- "Whether a credentials map carries everything `provider` needs."- [provider credentials]- (boolean- (case provider- "bedrock" (and (non-blank (:access-key-id credentials))- (non-blank (:secret-access-key credentials)))- "azure" (and (non-blank (:api-key credentials))- (non-blank (:base-url credentials)))- "google" (or (non-blank (:service-account-key credentials))- (and (non-blank (:oauth-access-token credentials))- (non-blank (:project-id credentials))))- (non-blank (:api-key credentials)))))--(defn configured-provider-credentials- "Returns the configured credentials map for the given provider, or nil if unrecognized or unconfigured."- [provider]- (case provider- "anthropic" (configured-api-key-credentials (llm.settings/llm-anthropic-api-key))- "azure" (let [creds {:api-key (non-blank (llm.settings/llm-azure-api-key))- :base-url (non-blank (llm.settings/llm-azure-api-base-url))}]- (when (provider-credentials-complete? "azure" creds)- creds))- "bedrock" (let [creds {:access-key-id (non-blank (llm.settings/llm-bedrock-access-key-id))- :secret-access-key (non-blank (llm.settings/llm-bedrock-secret-access-key))- :session-token (non-blank (llm.settings/llm-bedrock-session-token))- :region (non-blank (llm.settings/llm-bedrock-region))}]- (when (provider-credentials-complete? "bedrock" creds)- creds))- "google" (let [creds {:service-account-key (non-blank (llm.settings/llm-google-service-account-key))- :oauth-access-token (non-blank (llm.settings/llm-google-oauth-access-token))- :project-id (non-blank (llm.settings/llm-google-project-id))- :location (non-blank (llm.settings/llm-google-location))}]- (when (provider-credentials-complete? "google" creds)- creds))- "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))- nil))--(defn- llm-provider-configured?- "Check if a provider-and-model string has the necessary configuration.- For `metabase/*` providers, checks that the proxy URL is set.- For direct providers, checks that credentials are configured (see [[configured-provider-credentials]])."- [provider-and-model]- (boolean- (if (provider-util/metabase-provider? provider-and-model)- (some? (llm.settings/llm-proxy-base-url))- (some-> provider-and-model- provider-util/provider-and-model->provider- configured-provider-credentials))))-(defsetting llm-metabot-configured?- "Whether credentials for the selected Metabot provider are configured."+ "Whether the connection selected for Metabot has the credentials it needs.":type :boolean:visibility :public:setter :none:export? false- :getter #(llm-provider-configured? (llm-metabot-provider))+ :getter #(llm.provider/connection-usable?+ (llm.provider/model-ref->connection-key (llm-metabot-provider))):doc false)(defn- llm-provider-streams-reasoning?- "Whether a provider-and-model string names a model that streams its reasoning back to us."- [provider-and-model]- (let [model (provider-util/provider-and-model->model provider-and-model)]- (case (provider-util/provider-and-model->provider provider-and-model)+ "Whether a model reference names a model that streams its reasoning back to us."+ [model-ref]+ (let [{:keys [type model]} (llm.provider/resolve-model-ref model-ref)]+ (case type"anthropic" (claude/reasoning-model? model)"openai" (openai/reasoning-model? model)false)))diff --git a/src/metabase/metabot/usage.clj b/src/metabase/metabot/usage.cljindex 83f345b0c10..81de97c2be0 100644--- a/src/metabase/metabot/usage.clj+++ b/src/metabase/metabot/usage.clj@@ -8,7 +8,7 @@(:require[clojure.string :as str][metabase.api.common :as api]- [metabase.metabot.provider-util :as provider-util]+ [metabase.llm.provider :as llm.provider][metabase.metabot.settings :as metabot.settings][metabase.premium-features.core :as premium-features :refer [defenterprise defenterprise-schema]][metabase.util :as u]@@ -60,7 +60,7 @@(defn- default-metabase-meter-key[](some-> metabot.settings/default-metabase-llm-metabot-provider- provider-util/strip-metabase-prefix+ llm.provider/strip-managed-prefixu/qualified-name(str/replace-first "/" ":")(str ":tokens")))@@ -73,7 +73,7 @@(defn managed-free-limit-reached?"True when the configured managed Metabase provider is locked for free-tier usage."- ([] (and (provider-util/metabase-provider? (metabot.settings/llm-metabot-provider))+ ([] (and (llm.provider/managed-model-ref? (metabot.settings/llm-metabot-provider))(some-> (premium-features/token-status) managed-free-limit-reached?)))([token-status](some-> (meter-entry token-status)diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.cljindex 69fb8e97c73..ff4d78a41c0 100644--- a/src/metabase/settings/core.clj+++ b/src/metabase/settings/core.clj@@ -106,6 +106,7 @@has-advanced-setting-access?migrate-encrypted-settings!obfuscate-value+ obfuscated-value?read-settingregistered-settingsregistered?diff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.cljindex 0dfbf2aed68..0f58aa8f776 100644--- a/src/metabase/settings/models/setting.clj+++ b/src/metabase/settings/models/setting.clj@@ -788,7 +788,10 @@(ex-message e))(update-setting! setting-name new-value))))-(defn- obfuscated-value? [v]+(defn obfuscated-value?+ "Whether `v` looks like a value already obfuscated by [[obfuscate-value]], i.e. the client echoed back a masked+ value rather than entering a new one."+ [v](when (seq v)(boolean (re-matches #"^\*{10}.{2}$" v))))diff --git a/src/metabase/slackbot/streaming.clj b/src/metabase/slackbot/streaming.cljindex 05fdec3587b..97a177b41bf 100644--- a/src/metabase/slackbot/streaming.clj+++ b/src/metabase/slackbot/streaming.clj@@ -6,11 +6,11 @@[java-time.api :as t][metabase.api.common :as api][metabase.channel.slack :as channel.slack]+ [metabase.llm.provider :as llm.provider][metabase.metabot.agent.core :as agent][metabase.metabot.agent.memory :as memory][metabase.metabot.config :as metabot.config][metabase.metabot.context :as metabot.context]- [metabase.metabot.core :as metabot][metabase.metabot.envelope :as metabot.envelope][metabase.metabot.persistence :as metabot.persistence][metabase.metabot.settings :as metabot.settings]@@ -178,7 +178,7 @@{:keys [on-text on-tool-start on-tool-end on-data req-slack-msg-id get-res-slack-msg-idrequest-prompt team-id thread-ts]}](let [message (metabot.envelope/user-message prompt)- ai-proxy? (metabot/metabase-provider? (metabot.settings/llm-metabot-provider))+ ai-proxy? (llm.provider/managed-model-ref? (metabot.settings/llm-metabot-provider));; Persist a placeholder assistant row up front so its `created_at` pins;; turn ordering before any retry can sneak in earlier-timestamped rows.;; `:user-id` stamps the author on both rows so participation-baseddiff --git a/src/metabase/util.cljc b/src/metabase/util.cljcindex 5b67fe9a7c5..896c1236e9a 100644--- a/src/metabase/util.cljc+++ b/src/metabase/util.cljc@@ -201,6 +201,12 @@(str/ends-with? text ":") (str (subs text 0 (dec (count text))) "."):else (str text "."))))+(defn trimmed-string+ "`value` trimmed of surrounding whitespace, or nil when it is not a string or has nothing left once trimmed."+ ^String [value]+ (when (string? value)+ (not-empty (str/trim value))))+(defn lower-case-en"Locale-agnostic version of [[clojure.string/lower-case]]. [[clojure.string/lower-case]] uses the default locale inconversions, turning `ID` into `ıd`, in the Turkish locale. This function always uses the `en-US` locale."diff --git a/test/metabase/app_db/custom_migrations/llm_providers_test.clj b/test/metabase/app_db/custom_migrations/llm_providers_test.cljnew file mode 100644index 00000000000..1633512214c--- /dev/null+++ b/test/metabase/app_db/custom_migrations/llm_providers_test.clj@@ -0,0 +1,198 @@+(ns metabase.app-db.custom-migrations.llm-providers-test+ "Tests for the v64.7qmx3p migration that moves the per-provider LLM credential settings onto the `llm-providers`+ connection list. The migration itself is [[metabase.app-db.custom-migrations.llm-providers]], reached through the+ `MigrateLlmProviderSettings` custom change."+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase.app-db.schema-migrations-test.impl :as impl]+ [metabase.util.encryption :as encryption]+ [metabase.util.encryption-test :as encryption-test]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(defn- llm-setting-values+ [setting-keys]+ (into {}+ (map (fn [{:keys [key value]}] [key (encryption/maybe-decrypt value)]))+ (t2/query {:select [:key :value] :from :setting :where [:in :key setting-keys]})))++(defn- insert-llm-settings!+ [settings]+ (t2/insert! :setting (for [[k v] settings]+ {:key k :value (encryption/maybe-encrypt v)})))++(defn- llm-connections+ []+ (some-> (llm-setting-values ["llm-providers"]) (get "llm-providers") json/decode+kw))++(deftest migrate-llm-provider-settings-test+ (testing "v64.7qmx3p : per-provider credential settings move onto llm-providers and are deleted"+ (encryption-test/with-secret-key "dont-tell-anyone-about-this"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-anthropic-api-key" "sk-ant-stored"+ "llm-anthropic-api-base-url" "https://self-hosted.example"+ "llm-openai-api-key" "sk-stored"+ ;; an access key with no secret is not a configured provider+ "llm-bedrock-access-key-id" "AKIAIOSFODNN7EXAMPLE"+ "llm-metabot-provider" "anthropic/claude-opus-4-1"})+ (migrate!)+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {:api-key "sk-ant-stored" :base-url "https://self-hosted.example"}}+ {:key "openai"+ :type "openai"+ :name "OpenAI"+ :config {:api-key "sk-stored"}}]+ (llm-connections)))+ (testing "the credentials they came from are gone, so nothing is left holding a stale copy"+ (is (= {} (llm-setting-values ["llm-anthropic-api-key" "llm-anthropic-api-base-url"+ "llm-openai-api-key" "llm-bedrock-access-key-id"]))))+ (testing "and the model reference that already named the provider by type still resolves"+ (is (= {"llm-metabot-provider" "anthropic/claude-opus-4-1"}+ (llm-setting-values ["llm-metabot-provider"]))))))))++(deftest migrate-llm-provider-settings-azure-test+ (testing "v64.7qmx3p : Azure's deployment is recovered from the model reference it used to live in"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-azure-api-key" "azure-key"+ "llm-azure-api-base-url" "https://r.services.ai.azure.com/openai"+ "llm-metabot-provider" "azure/openai/gpt-4.1-mini"})+ (migrate!)+ (is (= [{:key "azure"+ :type "azure"+ :name "Microsoft Azure"+ :config {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"}}]+ (llm-connections))))))++(deftest migrate-llm-provider-settings-google-test+ (testing "v64.7qmx3p : either Google credential on its own is enough"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-google-oauth-access-token" "ya29.stored-token"+ "llm-google-project-id" "my-project"+ "llm-metabot-provider" "google/google/gemini-3.5-flash"})+ (migrate!)+ (is (= [{:key "google"+ :type "google"+ :name "Google Gemini Enterprise"+ :config {:oauth-access-token "ya29.stored-token"+ :project-id "my-project"}}]+ (llm-connections))))))++(deftest migrate-llm-provider-settings-managed-test+ (testing "v64.7qmx3p : an instance already on the managed provider gets the connection its reference names"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-metabot-provider" "metabase/anthropic/claude-sonnet-4-6"})+ (migrate!)+ (is (= [{:key "metabase" :type "metabase" :name "Metabase AI service" :config {}}]+ (llm-connections))))))++(deftest migrate-llm-provider-settings-keeps-an-existing-list-test+ (testing "v64.7qmx3p : a connection list that already exists is what the instance runs on, and wins"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-providers" (json/encode [{:key "anthropic-evals"+ :type "anthropic"+ :name "Anthropic (evals)"+ :config {:api-key "sk-ant-list"}}])+ "llm-anthropic-api-key" "sk-ant-stale"})+ (migrate!)+ (is (= [{:key "anthropic-evals" :type "anthropic" :name "Anthropic (evals)"+ :config {:api-key "sk-ant-list"}}]+ (llm-connections)))+ (testing "and the setting it superseded is deleted rather than left behind as a stale credential"+ (is (= {} (llm-setting-values ["llm-anthropic-api-key"])))))))++(deftest rollback-llm-provider-settings-test+ (testing "v64.7qmx3p : rolling back writes the connection list back into the settings older code reads"+ (encryption-test/with-secret-key "dont-tell-anyone-about-this"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-anthropic-api-key" "sk-ant-stored"+ "llm-metabot-provider" "anthropic/claude-opus-4-1"})+ (migrate!)+ (t2/query {:update :setting+ :set {:value (encryption/maybe-encrypt+ (json/encode [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {:api-key "sk-ant-rotated"}}]))}+ :where [:= :key "llm-providers"]})+ (migrate! :down 63)+ (testing "the rotated credential is what older code finds, not the one the upgrade started from"+ (is (= {"llm-anthropic-api-key" "sk-ant-rotated"}+ (llm-setting-values ["llm-anthropic-api-key"]))))+ (testing "and the list is dropped, so upgrading again re-reads the settings"+ (is (= {} (llm-setting-values ["llm-providers"]))))))))++(deftest rollback-llm-provider-settings-drops-what-settings-cannot-hold-test+ (testing "v64.7qmx3p : a connection keyed by anything but its provider type has nowhere to go on rollback"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-anthropic-api-key" "sk-ant-stored"})+ (migrate!)+ (t2/query {:update :setting+ :set {:value (encryption/maybe-encrypt+ (json/encode [{:key "anthropic-evals"+ :type "anthropic"+ :name "Anthropic (evals)"+ :config {:api-key "sk-ant-evals"}}]))}+ :where [:= :key "llm-providers"]})+ (insert-llm-settings! {"llm-metabot-provider" "anthropic-evals/claude-opus-4-1"})+ (migrate! :down 63)+ (testing "its credentials are not written under a provider they did not belong to"+ (is (= {} (llm-setting-values ["llm-anthropic-api-key"]))))+ (testing "and the reference naming it is rewritten to something older code can resolve"+ (is (= {"llm-metabot-provider" "anthropic/claude-opus-4-1"}+ (llm-setting-values ["llm-metabot-provider"])))))))++(deftest rollback-llm-provider-settings-encrypts-only-secrets-test+ (testing "v64.7qmx3p : the downgrade encrypts what the pre-migration code stored encrypted, and nothing else"+ (encryption-test/with-secret-key "dont-tell-anyone-about-this"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-anthropic-api-key" "sk-ant-stored"+ "llm-anthropic-api-base-url" "https://self-hosted.example"+ "llm-metabot-provider" "anthropic/claude-opus-4-1"})+ (migrate!)+ (migrate! :down 63)+ (let [raw (into {}+ (map (juxt :key :value))+ (t2/query {:select [:key :value]+ :from :setting+ :where [:in :key ["llm-anthropic-api-key"+ "llm-anthropic-api-base-url"+ "llm-metabot-provider"]]}))]+ (testing "the API key is sensitive, so it comes back as ciphertext"+ (is (not= "sk-ant-stored" (get raw "llm-anthropic-api-key")))+ (is (= "sk-ant-stored" (encryption/maybe-decrypt (get raw "llm-anthropic-api-key")))))+ (testing "settings declared :encryption :no come back as the plaintext they were declared to hold"+ (is (= "https://self-hosted.example" (get raw "llm-anthropic-api-base-url")))+ (is (= "anthropic/claude-opus-4-1" (get raw "llm-metabot-provider")))))))))++(deftest rollback-llm-provider-settings-managed-round-trip-test+ (testing "v64.7qmx3p : a managed-AI instance survives the round trip — the connection holds no credentials, so+ rolling back only needs to keep the model reference that names it"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (insert-llm-settings! {"llm-metabot-provider" "metabase/anthropic/claude-sonnet-4-6"})+ (migrate!)+ (is (= [{:key "metabase" :type "metabase" :name "Metabase AI service" :config {}}]+ (llm-connections)))+ (migrate! :down 63)+ (testing "the reference the pre-migration code resolves off the prefix alone is still there"+ (is (= {"llm-metabot-provider" "metabase/anthropic/claude-sonnet-4-6"}+ (llm-setting-values ["llm-metabot-provider"]))))+ (testing "and the list is dropped, so upgrading again re-materializes the connection"+ (is (= {} (llm-setting-values ["llm-providers"])))))))++(deftest rollback-llm-provider-settings-drops-a-blank-config-connection-test+ (testing "v64.7qmx3p : a known-type connection with nothing in its config writes no settings on rollback"+ (impl/test-migrations ["v64.7qmx3p"] [migrate!]+ (migrate!)+ (insert-llm-settings! {"llm-providers" (json/encode [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {}}])})+ (migrate! :down 63)+ (is (= {} (llm-setting-values ["llm-anthropic-api-key" "llm-anthropic-api-base-url"])))+ (testing "and the list is dropped either way"+ (is (= {} (llm-setting-values ["llm-providers"])))))))diff --git a/test/metabase/llm/api/provider_test.clj b/test/metabase/llm/api/provider_test.cljnew file mode 100644index 00000000000..470590e3807--- /dev/null+++ b/test/metabase/llm/api/provider_test.clj@@ -0,0 +1,779 @@+(ns metabase.llm.api.provider-test+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase.llm.api.provider :as llm.api.provider]+ [metabase.llm.provider :as llm.provider]+ [metabase.metabot.self :as metabot.self]+ [metabase.metabot.settings :as metabot.settings]+ [metabase.permissions.core :as perms]+ [metabase.settings.core :as setting]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]))++(use-fixtures :once (fixtures/initialize :db))++(defn- connection+ ([conn-key type]+ (connection conn-key type {}))+ ([conn-key type config]+ {:key conn-key :type type :name conn-key :config config}))++(defn- stored-config+ [conn-key]+ (:config (llm.provider/connection conn-key)))++(defn- rejected-credentials+ [message]+ (fn [& _]+ (throw (ex-info message {:api-error true :status-code 401}))))++(deftest provider-types-test+ (testing "every provider type is listed with the credential fields a connection needs"+ (let [types (mt/user-http-request :crowberto :get 200 "llm/provider-types")]+ (is (= #{"anthropic" "openai" "openrouter" "mistral" "zai" "moonshot" "google" "azure" "bedrock" "metabase"}+ (set (map :type types))))+ (is (= ["anthropic" "openai" "openrouter" "mistral" "zai" "moonshot" "google" "azure" "bedrock"]+ (remove #{"metabase"} (map :type types)))+ "the bring-your-own-key providers keep their registry order")+ (is (=? {:type "anthropic"+ :label "Anthropic"+ :managed false+ :available true+ :default_model "claude-sonnet-4-6"+ :fields [{:key "api-key"+ :label "API key"+ :type "password"+ :required true+ :advanced false+ :prefix "sk-ant-"+ :docs_url "https://console.anthropic.com/settings/keys"}+ {:key "base-url"+ :label "API base URL"+ :type "text"+ :required false+ :advanced true+ :default "https://api.anthropic.com"}]}+ (->> types (filter #(= "anthropic" (:type %))) first)))+ (testing "select fields carry their options"+ (is (some? (->> types+ (filter #(= "bedrock" (:type %)))+ first+ :fields+ (filter #(= "select" (:type %)))+ first+ :options))))+ (testing "the API-key prefixes reach the client, which uses them to recognize a pasted key"+ (is (= {"anthropic" "sk-ant-"+ "openai" "sk-"+ "openrouter" "sk-or-v1-"}+ (into {}+ (keep (fn [{:keys [type fields]}]+ (when-let [prefix (some :prefix fields)]+ [type prefix])))+ types))))+ (testing "`advanced` marks the fields an admin can ignore, which is not the same as the optional ones:+ Bedrock's region has a default but still decides model availability and data residency, so it+ stays up front while the session token — only used for temporary credentials — does not"+ (is (= {"access-key-id" false+ "secret-access-key" false+ "region" false+ "session-token" true}+ (->> types+ (filter #(= "bedrock" (:type %)))+ first+ :fields+ (into {} (map (juxt :key :advanced))))))))))++(deftest provider-types-google-fields-test+ (testing "Google's credentials hang off the authentication method it is asked for, and its models are a fixed list"+ (let [google (->> (mt/user-http-request :crowberto :get 200 "llm/provider-types")+ (filter #(= "google" (:type %)))+ first)+ fields (into {} (map (juxt :key identity)) (:fields google))]+ (is (= ["project-id" "location" "auth-method" "service-account-key" "oauth-access-token" "base-url"]+ (map :key (:fields google))))+ (is (=? {:type "segmented"+ :default "service-account-key"+ :options [{:value "service-account-key" :label "Service account key"}+ {:value "oauth-token" :label "OAuth token"}]}+ (fields "auth-method")))+ (is (=? {:type "file"+ :show_when {:field "auth-method" :value "service-account-key"}}+ (fields "service-account-key")))+ (is (=? {:type "password"+ :show_when {:field "auth-method" :value "oauth-token"}}+ (fields "oauth-access-token")))+ (testing "the models are the fixed catalog every connection of the type offers, not a field of its own"+ (is (nil? (fields "model")))+ (is (= "google/gemini-3.5-flash" (:default_model google)))+ (testing "and the catalog rides along so the connection form can offer the model to validate against"+ (is (= [{:id "google/gemini-3.5-flash" :display_name "gemini-3.5-flash"}+ {:id "google/gemini-3.6-flash" :display_name "gemini-3.6-flash"}+ {:id "google/gemini-3.7-flash" :display_name "gemini-3.7-flash"}]+ (:models google)))))+ (testing "the alternative credential groups ride along so the form knows when the config is complete"+ (is (= [["service-account-key"] ["oauth-access-token" "project-id"]]+ (:required_any google)))))))++(deftest provider-types-managed-availability-test+ (letfn [(managed [types] (->> types (filter #(= "metabase" (:type %))) first))]+ (testing "the managed provider leads the list when the LLM proxy is configured"+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-proxy-base-url "https://proxy.example.com"]+ (let [types (mt/user-http-request :crowberto :get 200 "llm/provider-types")]+ (is (=? {:type "metabase" :managed true :available true} (managed types)))+ (is (= "metabase" (:type (first types))))))))+ (testing "without a proxy to route through it is unavailable and does not lead"+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-proxy-base-url nil]+ (let [types (mt/user-http-request :crowberto :get 200 "llm/provider-types")]+ (is (=? {:type "metabase" :managed true :available false} (managed types)))+ (is (not= "metabase" (:type (first types))))))))))++(deftest list-providers-masks-secrets-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-secret"+ :base-url "https://api.anthropic.com"})+ (connection "openai" "openai" {:api-key ""})]]+ (testing "secrets come back masked and non-secret fields come back as they are"+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "anthropic"+ :source "db"+ :usable true+ :env_vars []+ :env_fields []+ :config {:api-key "**********et" :base-url "https://api.anthropic.com"}}+ {:key "openai"+ :type "openai"+ :name "openai"+ :source "db"+ :usable false+ :env_vars []+ :env_fields []+ :config {:api-key ""}}]+ (mt/user-http-request :crowberto :get 200 "llm/providers"))))))++(deftest list-providers-marks-env-connections-test+ (testing "a connection synthesized from the single-provider environment variables is reported as read-only"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :source "env"+ :usable true+ :env_vars ["MB_LLM_ANTHROPIC_API_KEY"]+ :env_fields ["api-key"]+ ;; only what the environment supplies: the base URL's registry default is filled in when the+ ;; connection is resolved for a request, not stored on it+ :config {:api-key "**********nv"}}]+ (mt/user-http-request :crowberto :get 200 "llm/providers")))))))++(deftest list-providers-marks-env-shadowed-fields-test+ (testing "a stored connection with an env-shadowed field stays editable, with just that field marked"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-base-url "https://env.example.com"]+ (is (=? [{:key "anthropic"+ :source "db"+ :env_vars ["MB_LLM_ANTHROPIC_API_BASE_URL"]+ :env_fields ["base-url"]+ :config {:api-key "**********ed" :base-url "https://env.example.com"}}]+ (mt/user-http-request :crowberto :get 200 "llm/providers")))))))++(deftest create-verifies-credentials-before-saving-test+ (mt/with-temporary-setting-values [llm-providers []]+ (let [calls (atom [])]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [provider {:keys [credentials]}]+ (swap! calls conj [provider credentials])+ (is (empty? (llm.provider/connections))+ "credentials are verified before the connection is saved")+ {:models [{:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}]})]+ (testing "a created connection defaults its key and name to its provider type"+ (is (= {:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :source "db"+ :usable true+ :env_vars []+ :env_fields []+ :config {:api-key "**********id"}}+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-ant-valid"}}))))+ (testing "the credentials are verified against the provider exactly once, unmasked and with the+ registry defaults the adapter needs filled in"+ (is (= [["anthropic" {:api-key "sk-ant-valid" :base-url "https://api.anthropic.com"}]] @calls)))+ (testing "the unmasked credentials are what gets persisted"+ (is (= {:api-key "sk-ant-valid"} (stored-config "anthropic"))))))))++(deftest create-selects-a-model-for-the-first-connection-test+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [& _] {:models [{:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}]})]+ (testing "connecting the first provider leaves Metabot pointed at one of its models"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-ant-valid"}})+ (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))+ (is (true? (metabot.settings/llm-metabot-configured?))))))+ (testing "an explicitly requested model wins over the provider type's default"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "anthropic"+ :model "claude-haiku-4-5"+ :config {:api-key "sk-ant-valid"}})+ (is (= "anthropic/claude-haiku-4-5" (metabot.settings/llm-metabot-provider))))))+ (testing "adding a second provider does not switch Metabot away from a working selection"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-8"]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "openai" :config {:api-key "sk-valid"}})+ (is (= "anthropic/claude-opus-4-8" (metabot.settings/llm-metabot-provider))))))))++(deftest create-selects-a-model-composed-from-the-config-test+ (testing (str "Azure names its deployment in `:config` rather than listing models, and the form leaves a field it "+ "pre-filled with the registry default out of the payload, so the default has to be filled in before "+ "the model is composed — otherwise connecting Azure first leaves Metabot on nothing.")+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "azure"+ :config {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :deployment-name "gpt-4.1-mini"}})+ (is (= "azure/openai/gpt-4.1-mini" (metabot.settings/llm-metabot-provider))))))))++(deftest writes-keep-a-stored-connection-the-environment-shadows-test+ (testing (str "The environment wins on read, but it must not take the stored credentials with it: they are what "+ "the instance falls back to once the variable comes back off.")+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "openai" :config {:api-key "sk-valid"}})+ (is (= [{:key "anthropic" :type "anthropic" :name "anthropic" :config {:api-key "sk-ant-stored"}}+ {:key "openai" :type "openai" :name "OpenAI" :config {:api-key "sk-valid"}}]+ (llm.provider/stored-connections))))+ (testing "and the shadowed connection stays editable, with the env-owned field left alone"+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (is (=? {:name "Renamed"+ :env_fields ["api-key"]+ ;; the response reflects what the connection runs on: the env value, masked+ :config {:api-key "**********nv"}}+ (mt/user-http-request :crowberto :put 200 "llm/providers/anthropic"+ {:name "Renamed"+ ;; the form echoes the mask of the env value for the disabled field;+ ;; it must not replace the stored credential+ :config {:api-key "**********nv"}})))+ (testing "while the stored credential the environment shadows is untouched"+ (is (= "sk-ant-stored"+ (->> (llm.provider/stored-connections)+ (filter #(= "anthropic" (:key %)))+ first+ :config+ :api-key))))))))))++(deftest create-does-not-save-when-credentials-are-rejected-test+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (rejected-credentials "Anthropic API key expired or invalid")]+ (is (=? {:message "Anthropic API key expired or invalid"}+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-ant-nope"}})))+ (is (= [] (llm.provider/connections))))))++(deftest create-rejects-invalid-config-before-calling-the-provider-test+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _]+ (is false "should reject before verifying credentials"))]+ (testing "a missing required field is rejected"+ (is (=? {:message "API key is required for anthropic."+ :field "api-key"}+ (mt/user-http-request :crowberto :post 400 "llm/providers" {:type "anthropic" :config {}}))))+ (testing "a secret that does not match the type's prefix is rejected"+ (is (=? {:message "Invalid API key for anthropic. It must start with 'sk-ant-'."+ :field "api-key"}+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-openai-shaped"}}))))+ (testing "an unknown provider type is rejected"+ (is (= "Unknown provider type \"evilai\"."+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "evilai" :config {:api-key "whatever"}}))))+ (is (= [] (llm.provider/connections))))))++(deftest create-suffixes-a-colliding-key-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-first"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]+ (is (=? {:key "anthropic-2" :name "Anthropic (evals)"}+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "anthropic"+ :name "Anthropic (evals)"+ :config {:api-key "sk-ant-second"}})))+ (is (= ["anthropic" "anthropic-2"] (map :key (llm.provider/connections))))+ (testing "the existing connection keeps its own credentials"+ (is (= {:api-key "sk-ant-first"} (stored-config "anthropic")))+ (is (= {:api-key "sk-ant-second"} (stored-config "anthropic-2")))))))++(deftest create-reserves-the-managed-key-test+ (testing (str "the metabase/ model-ref prefix means \"route through the AI proxy\", so no other provider type may "+ "hold the key that grants it")+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]+ (is (= "The \"metabase\" connection key is reserved for the Metabase AI service."+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "anthropic"+ :key "metabase"+ :config {:api-key "sk-ant-valid"}})))+ (testing "including a key that merely slugs to it"+ (is (= "The \"metabase\" connection key is reserved for the Metabase AI service."+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "anthropic"+ :key "Metabase!"+ :config {:api-key "sk-ant-valid"}}))))+ (is (= [] (llm.provider/connections))))))+ (testing "a rogue connection already holding the key blocks the managed provider instead of colliding with it"+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-providers [(connection "metabase" "anthropic"+ {:api-key "sk-ant-squatter"})]+ llm-proxy-base-url "https://proxy.example.com"]+ (is (= "Another connection holds the \"metabase\" key. Remove it before connecting the Metabase AI service."+ (mt/user-http-request :crowberto :post 400 "llm/providers" {:type "metabase"})))))))++(deftest create-drops-blank-config-values-test+ (testing "a blank field is not part of the connection, so nothing later mistakes it for a configured value"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "google"+ :config {:auth-method "oauth-token"+ :oauth-access-token "ya29.token"+ :project-id "my-project"+ :service-account-key ""+ :location " "}})+ (is (= {:auth-method "oauth-token"+ :oauth-access-token "ya29.token"+ :project-id "my-project"}+ (stored-config "google")))))))++(deftest update-clears-the-credential-the-other-auth-method-used-test+ (testing "switching Google's authentication method blanks the old credential, and the blank is dropped rather than stored"+ (mt/with-temporary-setting-values [llm-providers [(connection "google" "google"+ {:auth-method "service-account-key"+ :service-account-key "{\"type\":\"service_account\"}"})]]+ (let [probed (atom nil)]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]+ (reset! probed credentials)+ {:models []})]+ (mt/user-http-request :crowberto :put 200 "llm/providers/google"+ {:config {:auth-method "oauth-token"+ :oauth-access-token "ya29.token"+ :project-id "my-project"+ :service-account-key ""}})+ (is (= {:auth-method "oauth-token"+ :oauth-access-token "ya29.token"+ :project-id "my-project"}+ (stored-config "google")))+ (testing "so the credential probe runs with the OAuth token rather than the stale service account key"+ (is (nil? (:service-account-key @probed)))+ (is (= "ya29.token" (:oauth-access-token @probed)))))))))++(deftest list-providers-masks-the-google-service-account-key-test+ (testing "the service account key is a file upload rather than a password input, but it is the whole credential"+ (mt/with-temporary-setting-values [llm-providers [(connection "google" "google"+ {:auth-method "service-account-key"+ :service-account-key "{\"private_key\":\"secret\"}"+ :project-id "my-project"})]]+ (let [config (:config (first (mt/user-http-request :crowberto :get 200 "llm/providers")))]+ (is (setting/obfuscated-value? (:service-account-key config)))+ (is (= "my-project" (:project-id config)))))))++(deftest delete-managed-connection-requires-superuser-test+ (testing (str "removing the managed connection cancels the Store subscription behind it, so settings access via "+ "advanced permissions is not enough — everything else that can cancel add-ons is superuser-only")+ (mt/when-ee-evailable+ (mt/with-premium-features #{:advanced-permissions :metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-providers [(connection "metabase" "metabase")+ (connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"})]+ llm-proxy-base-url "https://proxy.example.com"]+ (mt/with-user-in-groups [group {:name "Settings managers"}+ user [group]]+ (perms/grant-application-permissions! group :setting)+ (testing "settings access is enough to remove an ordinary connection"+ (is (nil? (mt/user-http-request user :delete 204 "llm/providers/anthropic"))))+ (testing "but not the managed one"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request user :delete 403 "llm/providers/metabase"))))+ (testing "which is still there"+ (is (some? (llm.provider/connection "metabase"))))))))))++(deftest create-managed-connection-skips-credential-verification-test+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-providers []+ llm-proxy-base-url "https://proxy.example.com"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _]+ (is false "the managed provider has no credentials to verify"))]+ (is (=? {:key "metabase" :type "metabase" :usable true :config {}}+ (mt/user-http-request :crowberto :post 200 "llm/providers" {:type "metabase"})))))+ (testing "the managed provider cannot be added when the LLM proxy is not configured"+ (mt/with-temporary-setting-values [llm-providers []+ llm-proxy-base-url nil]+ (is (= "The \"metabase\" provider is not available on this instance."+ (mt/user-http-request :crowberto :post 400 "llm/providers" {:type "metabase"})))))))++(deftest update-preserves-a-masked-secret-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"+ :base-url "https://api.anthropic.com"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [_provider {:keys [credentials]}]+ (is (= {:api-key "sk-ant-stored" :base-url "https://new.example.com"} credentials)+ "the stored secret is what gets verified, not the mask")+ {:models []})]+ (is (= {:key "anthropic"+ :type "anthropic"+ :name "Anthropic (prod)"+ :source "db"+ :usable true+ :env_vars []+ :env_fields []+ :config {:api-key "**********ed" :base-url "https://new.example.com"}}+ (mt/user-http-request :crowberto :put 200 "llm/providers/anthropic"+ {:name "Anthropic (prod)"+ :config {:api-key "**********ed"+ :base-url "https://new.example.com"}})))+ (is (= {:api-key "sk-ant-stored" :base-url "https://new.example.com"} (stored-config "anthropic"))))))++(deftest update-replaces-a-freshly-entered-secret-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [_provider {:keys [credentials]}]+ (is (= {:api-key "sk-ant-rotated"+ :base-url "https://api.anthropic.com"}+ credentials))+ {:models []})]+ (mt/user-http-request :crowberto :put 200 "llm/providers/anthropic" {:config {:api-key "sk-ant-rotated"}})+ (is (= {:api-key "sk-ant-rotated"} (stored-config "anthropic"))))))++(deftest update-does-not-save-when-credentials-are-rejected-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (rejected-credentials "Anthropic API key expired or invalid")]+ (is (=? {:message "Anthropic API key expired or invalid"}+ (mt/user-http-request :crowberto :put 400 "llm/providers/anthropic"+ {:config {:api-key "sk-ant-rotated"}})))+ (is (= {:api-key "sk-ant-stored"} (stored-config "anthropic"))))))++(deftest update-unknown-connection-is-a-404-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})]]+ (mt/user-http-request :crowberto :put 404 "llm/providers/nope" {:name "Nope"})))++(deftest update-rejects-an-env-configured-connection-test+ (testing "a connection that comes from the single-provider environment variables cannot be edited"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (mt/user-http-request :crowberto :put 404 "llm/providers/anthropic" {:name "Anthropic"})))))++(deftest update-follows-the-deployment-an-azure-connection-now-serves-test+ (testing (str "Azure's model reference bakes in the deployment name, so renaming the deployment of the connection "+ "Metabot is pointed at has to move the selection with it — otherwise the next request resolves a "+ "deployment that no longer exists.")+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "azure" "azure"+ {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "azure/openai/gpt-4.1-mini"]+ (mt/user-http-request :crowberto :put 200 "llm/providers/azure"+ {:config {:deployment-name "gpt-4.1"}})+ (is (= "azure/openai/gpt-4.1" (metabot.settings/llm-metabot-provider))))))))++(deftest update-follows-the-model-picked-for-a-fixed-catalog-connection-test+ (testing (str "Google's edit form carries a model pick rather than a probe input, so saving with a different "+ "model moves the selection when it points at this connection")+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "google" "google"+ {:oauth-access-token "ya29.token"+ :project-id "my-project"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"]+ (mt/user-http-request :crowberto :put 200 "llm/providers/google"+ {:config {}+ :model "google/gemini-3.6-flash"})+ (is (= "google/google/gemini-3.6-flash" (metabot.settings/llm-metabot-provider))))+ (testing "while a selection on another connection is left alone"+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (mt/user-http-request :crowberto :put 200 "llm/providers/google"+ {:config {}+ :model "google/gemini-3.6-flash"})+ (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))))++(deftest ref-writes-leave-an-env-pinned-selection-alone-test+ (testing (str "MB_LLM_METABOT_PROVIDER pins the selection, so the automatic follow-ups — pointing Metabot at a "+ "first connection, following an edit, falling back after a delete — must not write a value that "+ "loses to the env var on every read")+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models [{:id "claude-sonnet-4-6"+ :display_name "Claude Sonnet 4.6"}]})]+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider "openai/gpt-5.4"]+ (testing "creating a first connection"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/user-http-request :crowberto :post 200 "llm/providers"+ {:type "anthropic" :config {:api-key "sk-ant-valid"}})+ (is (nil? (setting/db-stored-value :llm-metabot-provider))))))+ (testing "deleting the connection the env-pinned selection names"+ (mt/with-temporary-setting-values [llm-providers [(connection "openai" "openai" {:api-key "sk-valid"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/openai")+ (is (nil? (setting/db-stored-value :llm-metabot-provider))))))))))++(deftest update-leaves-a-selection-it-does-not-compose-alone-test+ (testing "a type whose model is chosen independently of its credentials keeps the model the admin selected"+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/user-http-request :crowberto :put 200 "llm/providers/anthropic" {:config {:api-key "sk-ant-rotated"}})+ (is (= "anthropic/claude-opus-4-1" (metabot.settings/llm-metabot-provider))))))))++(deftest update-leaves-a-selection-on-another-connection-alone-test+ (testing "editing an Azure connection Metabot is not pointed at does not steal the selection"+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _] {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})+ (connection "azure" "azure"+ {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"})]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/user-http-request :crowberto :put 200 "llm/providers/azure"+ {:config {:deployment-name "gpt-4.1"}})+ (is (= "anthropic/claude-opus-4-1" (metabot.settings/llm-metabot-provider))))))))++(deftest delete-removes-the-connection-test+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "openai/gpt-4.1-mini"]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})+ (connection "openai" "openai" {:api-key "sk-stored"})]]+ (is (nil? (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")))+ (is (= ["openai"] (map :key (llm.provider/connections))))+ (testing "a selection that did not point at the deleted connection is left alone"+ (is (= "openai/gpt-4.1-mini" (metabot.settings/llm-metabot-provider)))))))++(deftest delete-cancels-the-managed-subscription-test+ (testing "removing the managed connection cancels the add-on backing it"+ (let [cancelled (atom 0)]+ (mt/with-dynamic-fn-redefs [llm.api.provider/cancel-managed-ai-subscription!+ (fn [] (swap! cancelled inc))]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/with-temporary-setting-values [llm-providers [(connection "metabase" "metabase")]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/metabase")+ (is (= 1 @cancelled))+ (is (= [] (llm.provider/connections))))))))+ (testing "removing any other connection leaves the subscription alone"+ (let [cancelled (atom 0)]+ (mt/with-dynamic-fn-redefs [llm.api.provider/cancel-managed-ai-subscription!+ (fn [] (swap! cancelled inc))]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-stored"})]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")+ (is (zero? @cancelled))))))))++(deftest delete-repoints-the-metabot-selection-test+ (testing "deleting the connection Metabot was pointed at moves the selection to a remaining one"+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})+ (connection "openai" "openai" {:api-key "sk-stored"})]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")+ (is (= "openai/gpt-5.4" (metabot.settings/llm-metabot-provider)))))))++(deftest delete-last-connection-clears-the-selection-test+ (testing "with nothing left to fall back to, the stored selection is cleared instead of left dangling"+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")+ (is (= [] (llm.provider/connections)))+ (is (nil? (setting/db-stored-value :llm-metabot-provider)))+ (is (= metabot.settings/default-llm-metabot-provider (metabot.settings/llm-metabot-provider)))))))++(deftest delete-skips-a-fallback-with-no-default-model-test+ (testing "a remaining connection whose type has no default model is not a usable fallback"+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})+ (connection "azure" "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"})+ (connection "openai" "openai" {:api-key "sk-stored"})]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")+ (is (= "openai/gpt-5.4" (metabot.settings/llm-metabot-provider)))))))++(deftest delete-falls-back-to-a-connection-that-composes-its-own-model-test+ (testing "an Azure connection that names its deployment is a usable fallback, even though its type has no default"+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "anthropic/claude-opus-4-1"]+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-stored"})+ (connection "azure" "azure"+ {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"})]]+ (mt/user-http-request :crowberto :delete 204 "llm/providers/anthropic")+ (is (= "azure/openai/gpt-4.1-mini" (metabot.settings/llm-metabot-provider)))))))++(deftest delete-unknown-connection-is-a-404-test+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/user-http-request :crowberto :delete 404 "llm/providers/nope")))++(deftest writes-are-rejected-when-connections-are-env-managed-test+ (mt/with-temp-env-var-value! [mb-llm-providers "[{\"key\":\"anthropic\",\"type\":\"anthropic\",\"name\":\"Anthropic\",\"config\":{\"api-key\":\"sk-ant-env\"}}]"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [& _]+ (is false "should reject before verifying credentials"))]+ (testing "the whole list is read-only and reported as such"+ (is (=? [{:key "anthropic" :source "env"}]+ (mt/user-http-request :crowberto :get 200 "llm/providers"))))+ (testing "creating is rejected"+ (is (re-find #"MB_LLM_PROVIDERS"+ (mt/user-http-request :crowberto :post 400 "llm/providers"+ {:type "openai" :config {:api-key "sk-new"}}))))+ (testing "updating is rejected"+ (is (re-find #"MB_LLM_PROVIDERS"+ (mt/user-http-request :crowberto :put 400 "llm/providers/anthropic"+ {:config {:api-key "sk-ant-new"}}))))+ (testing "deleting is rejected"+ (is (re-find #"MB_LLM_PROVIDERS"+ (mt/user-http-request :crowberto :delete 400 "llm/providers/anthropic")))))))++(deftest permissions-test+ (testing "managing provider connections requires the setting application permission"+ (mt/user-http-request :rasta :get 403 "llm/provider-types")+ (mt/user-http-request :rasta :get 403 "llm/providers")+ (mt/user-http-request :rasta :post 403 "llm/providers" {:type "anthropic" :config {:api-key "sk-ant-x"}})+ (mt/user-http-request :rasta :put 403 "llm/providers/anthropic" {:name "Anthropic"})+ (mt/user-http-request :rasta :delete 403 "llm/providers/anthropic")+ (mt/user-http-request :rasta :get 403 "llm/models")))++(deftest models-are-listed-per-connection-test+ (mt/with-temporary-setting-values [llm-providers [(connection "listed-anthropic" "anthropic" {:api-key "sk-ant-a"})+ (connection "listed-openai" "openai" {:api-key "sk-o"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [provider _opts]+ (case provider+ "anthropic" {:models [{:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}+ {:id "claude-haiku-4-5" :display_name "Claude Haiku 4.5"}]}+ "openai" {:models [{:id "gpt-5.4" :display_name "gpt-5.4"}+ {:id "gpt-4.1-mini" :display_name "gpt-4.1-mini"}]}))]+ (testing "each connection reports its own models, in the order the provider lists them"+ (is (= [{:key "listed-anthropic"+ :name "listed-anthropic"+ :type "anthropic"+ :models [{:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}+ {:id "claude-haiku-4-5" :display_name "Claude Haiku 4.5"}]}+ {:key "listed-openai"+ :name "listed-openai"+ :type "openai"+ :models [{:id "gpt-5.4" :display_name "gpt-5.4"}+ {:id "gpt-4.1-mini" :display_name "gpt-4.1-mini"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models")))))))++(deftest models-are-refetched-when-a-credential-changes-test+ (testing "a rotated key gets a cache entry of its own rather than the model list the old key produced"+ (let [keys-seen (atom [])]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]+ (swap! keys-seen conj (:api-key credentials))+ {:models [{:id "claude-sonnet-4-6"+ :display_name "Claude Sonnet 4.6"}]})]+ (mt/with-temporary-setting-values [llm-providers [(connection "cache-keying" "anthropic"+ {:api-key "sk-ant-first"})]]+ (mt/user-http-request :crowberto :get 200 "llm/models")+ (mt/user-http-request :crowberto :get 200 "llm/models"))+ (mt/with-temporary-setting-values [llm-providers [(connection "cache-keying" "anthropic"+ {:api-key "sk-ant-rotated"})]]+ (mt/user-http-request :crowberto :get 200 "llm/models"))+ (is (= ["sk-ant-first" "sk-ant-rotated"] @keys-seen))))))++(deftest models-for-a-connection-with-a-fixed-catalog-test+ (testing (str "Google's models are the registry's, so every connection of the type offers both of them in the "+ "model picker — but the call is still made, because it is what verifies the credentials")+ (let [probed (atom nil)]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [model]}]+ (reset! probed model)+ {:models []})]+ (mt/with-temporary-setting-values [llm-providers [(connection "gemini-catalog" "google"+ {:oauth-access-token "ya29.token"+ :project-id "my-project"})]]+ (is (= [{:key "gemini-catalog"+ :name "gemini-catalog"+ :type "google"+ :models [{:id "google/gemini-3.5-flash" :display_name "gemini-3.5-flash"}+ {:id "google/gemini-3.6-flash" :display_name "gemini-3.6-flash"}+ {:id "google/gemini-3.7-flash" :display_name "gemini-3.7-flash"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models")))+ (is (= "google/gemini-3.5-flash" @probed)))))))++(deftest models-for-a-connection-that-names-its-own-model-test+ (testing "Azure serves a deployment its listing endpoint never returns, so the connection's own model is reported"+ (mt/with-temporary-setting-values [llm-providers [(connection "azure-prod" "azure"+ {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"})]]+ (let [listed-with (atom nil)]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider opts]+ (reset! listed-with opts)+ {:models []})]+ (is (= [{:key "azure-prod"+ :name "azure-prod"+ :type "azure"+ :models [{:id "openai/gpt-4.1-mini" :display_name "gpt-4.1-mini"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models")))+ (testing "and the call still happens, because it is what verifies the credentials"+ (is (= "openai/gpt-4.1-mini" (:model @listed-with)))))))))++(deftest models-isolate-per-connection-failures-test+ (mt/with-temporary-setting-values [llm-providers [(connection "failing-anthropic" "anthropic" {:api-key "sk-ant-bad"})+ (connection "working-openai" "openai" {:api-key "sk-good"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [provider _opts]+ (if (= "anthropic" provider)+ (throw (ex-info "Anthropic API key expired or invalid"+ {:api-error true :status-code 401}))+ {:models [{:id "gpt-5.4" :display_name "gpt-5.4"}]}))]+ (testing "rejected credentials come back as an error on that connection, not a failed request"+ (is (= [{:key "failing-anthropic"+ :name "failing-anthropic"+ :type "anthropic"+ :models []+ :error "Anthropic API key expired or invalid"}+ {:key "working-openai"+ :name "working-openai"+ :type "openai"+ :models [{:id "gpt-5.4" :display_name "gpt-5.4"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models")))))))++(deftest models-isolate-provider-outages-test+ (mt/with-temporary-setting-values [llm-providers [(connection "outage-anthropic" "anthropic" {:api-key "sk-ant-x"})+ (connection "outage-openai" "openai" {:api-key "sk-good"})]]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [provider _opts]+ (if (= "anthropic" provider)+ (throw (ex-info "Anthropic API is down" {:api-error true :status-code 500}))+ {:models [{:id "gpt-5.4" :display_name "gpt-5.4"}]}))]+ (testing "an outage on one connection does not blank out the others"+ (is (=? [{:key "outage-anthropic" :models [] :error "Anthropic API is down"}+ {:key "outage-openai" :models [{:id "gpt-5.4"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models")))))))++(deftest models-for-the-managed-connection-come-from-the-fixed-catalog-test+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-providers [(connection "metabase" "metabase")]+ llm-proxy-base-url "https://proxy.example.com"]+ (mt/with-dynamic-fn-redefs [metabot.self/list-models+ (fn [& _]+ (throw (ex-info "the proxy must not be listed against" {})))]+ (testing "the proxy serves one fixed model, so nothing is fetched over the wire"+ (is (= [{:key "metabase"+ :name "metabase"+ :type "metabase"+ :models [{:id "anthropic/claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}]}]+ (mt/user-http-request :crowberto :get 200 "llm/models"))))))))diff --git a/test/metabase/llm/api_test.clj b/test/metabase/llm/api_test.cljindex 1e0708f5a36..860b2e454bb 100644--- a/test/metabase/llm/api_test.clj+++ b/test/metabase/llm/api_test.clj@@ -8,8 +8,6 @@[metabase.llm.api :as api][metabase.llm.context :as llm.context][metabase.llm.settings :as llm.settings]- [metabase.metabot.self :as metabot.self]- [metabase.metabot.settings :as metabot.settings][metabase.test :as mt]))(set! *warn-on-reflection* true)@@ -131,44 +129,6 @@:database_id (:id db)})](is (str/includes? (str response) "No tables found")))))))-(deftest list-models-unconfigured-test- (testing "Returns 403 when LLM is not configured"- (mt/with-dynamic-fn-redefs [metabot.settings/llm-metabot-configured? (constantly false)]- (let [response (mt/user-http-request :rasta :get 403 "llm/list-models")]- (is (str/includes? (str response) "not configured"))))))--(deftest list-models-resolves-direct-provider-test- (testing "resolves provider from provider-and-model and passes ai-proxy? = false"- (let [captured (atom nil)]- (mt/with-dynamic-fn-redefs [metabot.settings/llm-metabot-configured? (constantly true)- metabot.settings/llm-metabot-provider (constantly "anthropic/claude-sonnet-4")- metabot.self/list-models (fn [provider opts]- (reset! captured {:provider provider :opts opts})- {:models [{:id "claude-sonnet-4" :display_name "Claude Sonnet 4"}]})]- (let [response (mt/user-http-request :rasta :get 200 "llm/list-models")]- (is (=? {:models [{:id "claude-sonnet-4"- :display_name "Claude Sonnet 4"}]}- response))- (is (=? {:provider "anthropic"- :opts {:ai-proxy? false}}- @captured)))))))--(deftest list-models-resolves-metabase-prefixed-provider-test- (testing "resolves inner provider from metabase/ prefix and passes ai-proxy? = true"- (let [captured (atom nil)]- (mt/with-dynamic-fn-redefs [metabot.settings/llm-metabot-configured? (constantly true)- metabot.settings/llm-metabot-provider (constantly "metabase/openrouter/anthropic/claude-haiku-4-5")- metabot.self/list-models (fn [provider opts]- (reset! captured {:provider provider :opts opts})- {:models [{:id "anthropic/claude-haiku-4-5" :display_name "Claude Haiku 4.5"}]})]- (let [response (mt/user-http-request :rasta :get 200 "llm/list-models")]- (is (=? {:models [{:id "anthropic/claude-haiku-4-5"- :display_name "Claude Haiku 4.5"}]}- response))- (is (=? {:provider "openrouter"- :opts {:ai-proxy? true}}- @captured)))))))-(deftest extract-sources-test(testing "POST /api/llm/extract-sources returns SQL tables and native card/model references"(mt/with-temp [:model/Database db {:engine :h2}diff --git a/test/metabase/llm/provider_test.clj b/test/metabase/llm/provider_test.cljnew file mode 100644index 00000000000..e86fa014db6--- /dev/null+++ b/test/metabase/llm/provider_test.clj@@ -0,0 +1,472 @@+(ns metabase.llm.provider-test+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase.llm.provider :as llm.provider]+ [metabase.llm.settings :as llm.settings]+ [metabase.settings.core :as setting]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]))++(use-fixtures :once (fixtures/initialize :db))++(defn- connection+ ([conn-key type]+ (connection conn-key type {}))+ ([conn-key type config]+ {:key conn-key :type type :name conn-key :config config}))++(deftest unique-key-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic")+ (connection "anthropic-2" "anthropic")]]+ (testing "a key nothing else uses is taken as-is, so the first connection of a type is named after it"+ (is (= "openai" (llm.provider/unique-key "openai"))))+ (testing "a taken key is suffixed past every collision"+ (is (= "anthropic-3" (llm.provider/unique-key "anthropic"))))+ (testing "a display name is slugified into something URL-safe"+ (is (= "anthropic-evals" (llm.provider/unique-key "Anthropic (evals)")))+ (is (= "my-key" (llm.provider/unique-key " My___KEY "))))+ (testing "a name with nothing slug-able left falls back to a generic key"+ (is (= "provider" (llm.provider/unique-key "!!!")))+ (is (= "provider" (llm.provider/unique-key nil))))))++(deftest ^:parallel redact-masks-only-password-fields-test+ (testing "password fields are masked and plain fields are returned as-is"+ (is (= {:api-key "**********34"+ :base-url "https://api.anthropic.com"}+ (:config (llm.provider/redact (connection "anthropic" "anthropic"+ {:api-key "sk-ant-secret1234"+ :base-url "https://api.anthropic.com"}))))))+ (testing "a blank or missing secret is left alone rather than masked into something that looks configured"+ (is (= {:api-key "" :base-url nil}+ (:config (llm.provider/redact (connection "anthropic" "anthropic"+ {:api-key "" :base-url nil}))))))+ (testing "every password field of a multi-secret type is masked, and its select field is not"+ (is (= {:access-key-id "**********LE"+ :secret-access-key "**********et"+ :session-token "**********en"+ :region "us-east-2"}+ (:config (llm.provider/redact (connection "bedrock" "bedrock"+ {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"+ :session-token "test-token"+ :region "us-east-2"}))))))+ (testing "the managed type has no secret fields, so nothing is touched"+ (is (= {} (:config (llm.provider/redact (connection "metabase" "metabase")))))))++(deftest ^:parallel merge-config-test+ (testing "a secret the client echoed back masked keeps its stored value"+ (is (= {:api-key "sk-ant-stored"+ :base-url "https://new.example.com"}+ (llm.provider/merge-config "anthropic"+ {:api-key "sk-ant-stored" :base-url "https://old.example.com"}+ {:api-key (setting/obfuscate-value "sk-ant-stored")+ :base-url "https://new.example.com"}))))+ (testing "a freshly entered secret replaces the stored one"+ (is (= {:api-key "sk-ant-new"}+ (llm.provider/merge-config "anthropic" {:api-key "sk-ant-stored"} {:api-key "sk-ant-new"}))))+ (testing "an explicitly blank secret clears the stored one"+ (is (= {:api-key ""}+ (llm.provider/merge-config "anthropic" {:api-key "sk-ant-stored"} {:api-key ""})))+ (is (= {:api-key nil}+ (llm.provider/merge-config "anthropic" {:api-key "sk-ant-stored"} {:api-key nil}))))+ (testing "fields the client leaves out keep their stored value"+ (is (= {:api-key "sk-ant-stored" :base-url "https://old.example.com"}+ (llm.provider/merge-config "anthropic"+ {:api-key "sk-ant-stored" :base-url "https://old.example.com"}+ {}))))+ (testing "each secret field of a multi-secret type is preserved independently"+ (is (= {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "rotated-secret"+ :region "us-west-1"}+ (llm.provider/merge-config "bedrock"+ {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "old-secret"+ :region "us-east-2"}+ {:access-key-id (setting/obfuscate-value "AKIAIOSFODNN7EXAMPLE")+ :secret-access-key "rotated-secret"+ :region "us-west-1"})))))++(deftest validate-config!-test+ (testing "an unknown provider type is rejected"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Unknown provider type"+ (llm.provider/validate-config! "evilai" {:api-key "whatever"}))))+ (testing "a missing required field is rejected"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"API key is required for anthropic"+ (llm.provider/validate-config! "anthropic" {})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"API key is required for anthropic"+ (llm.provider/validate-config! "anthropic" {:api-key " "}))))+ (testing "a required field is only required when the type declares it"+ (is (nil? (llm.provider/validate-config! "anthropic" {:api-key "sk-ant-valid"})))+ (is (nil? (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"}))))+ (testing "a field that declares a prefix must start with it"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must start with"+ (llm.provider/validate-config! "anthropic" {:api-key "sk-not-anthropic"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must start with"+ (llm.provider/validate-config! "openrouter" {:api-key "sk-or-v2-nope"})))+ (is (nil? (llm.provider/validate-config! "openrouter" {:api-key "sk-or-v1-valid"}))))+ (testing "a type with several required fields rejects the first one missing"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"API base URL is required for azure"+ (llm.provider/validate-config! "azure" {:api-key "azure-key"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Deployment name is required for azure"+ (llm.provider/validate-config! "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"})))+ (testing "a required field the registry defaults is satisfied by that default"+ (is (nil? (llm.provider/validate-config! "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :deployment-name "gpt-4.1-mini"}))))+ (is (nil? (llm.provider/validate-config! "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"}))))+ (testing "the managed type declares no fields, so any config validates"+ (is (nil? (llm.provider/validate-config! "metabase" {})))))++(deftest validate-config!-required-any-test+ (testing "a type with alternative credential groups needs one of them in full"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"google needs one of"+ (llm.provider/validate-config! "google" {})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"google needs one of"+ (llm.provider/validate-config! "google" {:oauth-access-token "ya29.token"})))+ (is (nil? (llm.provider/validate-config! "google" {:service-account-key "{\"type\":\"service_account\"}"})))+ (is (nil? (llm.provider/validate-config! "google" {:oauth-access-token "ya29.token"+ :project-id "my-project"})))))++(deftest validate-config!-field-validator-test+ (testing "a field's own validator runs on a non-blank value"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"not a valid Google Cloud project ID"+ (llm.provider/validate-config! "google" {:service-account-key "{\"type\":\"service_account\"}"+ :project-id "My Project (name, not ID)"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"not a valid Google Cloud location"+ (llm.provider/validate-config! "google" {:service-account-key "{\"type\":\"service_account\"}"+ :location "US Central"})))))++(deftest validate-config!-select-options-test+ (testing "a select field's value must be one of its options"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Model provider for azure"+ (llm.provider/validate-config! "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "gemini"+ :deployment-name "gpt-4.1-mini"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Region for bedrock"+ (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"+ :region "mars-north-1"})))))++(deftest connection-model-test+ (testing "Azure composes its model from the family and deployment the admin picked, so neither is typed as a prefix"+ (is (= "openai/gpt-4.1-mini"+ (llm.provider/connection-model "azure" {:model-family "openai"+ :deployment-name "gpt-4.1-mini"})))+ (is (= "anthropic/claude-sonnet-4-5"+ (llm.provider/connection-model "azure" {:model-family "anthropic"+ :deployment-name "claude-sonnet-4-5"}))))+ (testing "a half-filled connection names no model rather than a malformed one"+ (is (nil? (llm.provider/connection-model "azure" {:model-family "openai"})))+ (is (nil? (llm.provider/connection-model "azure" {:model-family "openai"+ :deployment-name " "})))+ (is (nil? (llm.provider/connection-model "azure" {:deployment-name "gpt-4.1-mini"})))+ (is (nil? (llm.provider/connection-model "azure" nil))))+ (testing "types that list their models over the wire name none in their config"+ (doseq [type ["anthropic" "openai" "openrouter" "bedrock" "metabase"]]+ (is (nil? (llm.provider/connection-model type {:api-key "sk-whatever"})) type))))++(deftest config-complete?-test+ (testing "API-key types are complete exactly when the key is non-blank"+ (doseq [type ["anthropic" "openai" "openrouter"]]+ (testing type+ (is (true? (llm.provider/config-complete? type {:api-key "sk-whatever"})))+ (is (false? (llm.provider/config-complete? type {:api-key ""})))+ (is (false? (llm.provider/config-complete? type {:api-key nil})))+ (is (false? (llm.provider/config-complete? type nil))))))+ (testing "google needs a service account key on its own, or an OAuth token together with a project ID"+ (is (true? (llm.provider/config-complete? "google" {:service-account-key "{\"type\":\"service_account\"}"})))+ (is (true? (llm.provider/config-complete? "google" {:oauth-access-token "ya29.token"+ :project-id "my-project"})))+ (testing "an empty config is not complete just because every field is individually optional"+ (is (false? (llm.provider/config-complete? "google" {})))+ (is (false? (llm.provider/config-complete? "google" nil))))+ (testing "an OAuth token alone is not enough — nothing carries the project it should bill to"+ (is (false? (llm.provider/config-complete? "google" {:oauth-access-token "ya29.token"})))+ (is (false? (llm.provider/config-complete? "google" {:oauth-access-token "ya29.token"+ :project-id " "}))))+ (testing "a project ID alone authenticates nothing"+ (is (false? (llm.provider/config-complete? "google" {:project-id "my-project"})))))+ (testing "azure needs an API key and a base URL"+ (is (true? (llm.provider/config-complete? "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"})))+ (is (false? (llm.provider/config-complete? "azure" {:api-key "azure-key"})))+ (is (false? (llm.provider/config-complete? "azure" {:api-key "azure-key" :base-url " "})))+ (is (false? (llm.provider/config-complete? "azure" {:base-url "https://r.services.ai.azure.com/openai"})))+ (testing (str "the deployment names what to call rather than what authenticates the call, and an instance "+ "configured before the connection list existed carries it in the model reference instead, so a "+ "connection without one still counts as complete")+ (is (true? (llm.provider/config-complete? "azure" {:api-key "azure-key"+ :base-url "https://r.services.ai.azure.com/openai"})))))+ (testing "bedrock needs both AWS keys, and neither the region nor the session token"+ (is (true? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"})))+ (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"})))+ (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key ""})))+ (is (false? (llm.provider/config-complete? "bedrock" {:secret-access-key "test-secret"}))))+ (testing "the managed type carries no credentials and is complete exactly when the LLM proxy is configured"+ (mt/with-premium-features #{:metabase-ai-managed}+ (mt/with-temporary-setting-values [llm-proxy-base-url "https://proxy.example.com"]+ (is (true? (llm.provider/config-complete? "metabase" {})))+ (is (true? (llm.provider/type-available? "metabase"))))+ (mt/with-temporary-setting-values [llm-proxy-base-url nil]+ (is (false? (llm.provider/config-complete? "metabase" {})))+ (is (false? (llm.provider/type-available? "metabase")))))))++(deftest managed-type-is-available-to-instances-that-can-buy-it-test+ (testing "Harbormaster injects the proxy URL into every hosted instance, so availability follows the subscription"+ (mt/with-temp-env-var-value! [mb-llm-proxy-base-url "https://proxy.example.com"]+ (testing "an instance that is only offered the add-on can still reach the purchase flow"+ (mt/with-premium-features #{:offer-metabase-ai-managed}+ (is (true? (llm.provider/type-available? "metabase")))))+ (testing "an instance that already bought the add-on keeps it"+ (mt/with-premium-features #{:metabase-ai-managed}+ (is (true? (llm.provider/type-available? "metabase")))))+ (testing "an instance with no claim on the managed provider cannot select it"+ (mt/with-premium-features #{}+ (is (false? (llm.provider/type-available? "metabase"))))))))++(deftest connections-test+ (testing "stored connections are returned with a :db source"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})]]+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "anthropic"+ :config {:api-key "sk-ant-db"}+ :source :db}]+ (llm.provider/connections)))))+ (testing "the single-provider environment variables synthesize read-only connections keyed by provider type"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"+ mb-llm-anthropic-api-base-url "https://env.example.com"]+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :source :env+ :env-vars #{"MB_LLM_ANTHROPIC_API_KEY" "MB_LLM_ANTHROPIC_API_BASE_URL"}+ :env-fields #{:api-key :base-url}+ :config {:api-key "sk-ant-env" :base-url "https://env.example.com"}}]+ (llm.provider/connections))))))+ (testing "a variable that is not a credential shadows but does not create: alone it makes no connection"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-base-url "https://env.example.com"]+ (is (= [] (llm.provider/connections))))))+ (testing "stored and env-derived connections are merged, stored first"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})]]+ (mt/with-temp-env-var-value! [mb-llm-openai-api-key "sk-env"]+ (is (= [["anthropic" :db] ["openai" :env]]+ (map (juxt :key :source) (llm.provider/connections)))))))+ (testing "the environment shadows a stored connection with the same key field by field, not wholesale"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-db"+ :base-url "https://stored.example.com"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "anthropic"+ ;; the env key wins; the stored base URL survives, so a lone base-url var can equally reach a+ ;; stored connection instead of doing nothing+ :config {:api-key "sk-ant-env" :base-url "https://stored.example.com"}+ :env-vars #{"MB_LLM_ANTHROPIC_API_KEY"}+ :env-fields #{:api-key}+ :source :db}]+ (llm.provider/connections))))))+ (testing "a lone base-url variable reaches the stored connection's base URL"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-base-url "https://env.example.com"]+ (is (= {:api-key "sk-ant-db" :base-url "https://env.example.com"}+ (llm.provider/credentials "anthropic"))))))+ (testing "a metabase/ reference pinned by the environment synthesizes the managed connection it names"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"]+ (is (=? [{:key "metabase" :type "metabase" :source :env}]+ (llm.provider/connections)))+ (is (=? {:connection-key "metabase" :type "anthropic" :ai-proxy? true}+ (llm.provider/resolve-model-ref "metabase/anthropic/claude-sonnet-4-6"))))))+ (testing "the whole stored list is read-only when it comes from the environment"+ (mt/with-temp-env-var-value! [mb-llm-providers "[{\"key\":\"anthropic\",\"type\":\"anthropic\",\"name\":\"Anthropic\",\"config\":{\"api-key\":\"sk-ant-env\"}}]"]+ (is (= [["anthropic" :env]]+ (map (juxt :key :source) (llm.provider/connections)))))))++(deftest stored-connections-keeps-a-connection-the-environment-shadows-test+ (testing (str "The stored list keeps the credentials the environment shadows, so writes rebuild from here and "+ "removing the env var brings the stored value back.")+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (is (= [{:key "anthropic" :type "anthropic" :name "anthropic" :config {:api-key "sk-ant-db"}}]+ (llm.provider/stored-connections)))+ (is (= "sk-ant-env" (:api-key (llm.provider/credentials "anthropic"))))))))++(deftest connection-lookup-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})+ (connection "openai" "openai" {:api-key ""})]]+ (testing "a connection is found by key"+ (is (=? {:key "anthropic" :type "anthropic"} (llm.provider/connection "anthropic")))+ (is (= {:api-key "sk-ant-db"} (llm.provider/credentials "anthropic"))))+ (testing "an unknown key has no connection and no credentials"+ (is (nil? (llm.provider/connection "nope")))+ (is (nil? (llm.provider/credentials "nope"))))+ (testing "a connection is usable only when its config is complete"+ (is (true? (llm.provider/connection-usable? "anthropic")))+ (is (false? (llm.provider/connection-usable? "openai")))+ (is (false? (llm.provider/connection-usable? "nope"))))))++(deftest set-connections!-test+ (mt/with-temporary-setting-values [llm-providers []]+ (llm.provider/set-connections! [(assoc (connection "anthropic" "anthropic" {:api-key "sk-ant-db"}) :source :db)])+ (testing "the derived :source key is not persisted"+ (is (= [{:key "anthropic" :type "anthropic" :name "anthropic" :config {:api-key "sk-ant-db"}}]+ (vec (llm.settings/llm-providers)))))))++(deftest ^:parallel model-ref-parsing-test+ (testing "a model reference splits into its connection key and everything after it"+ (is (= "anthropic" (llm.provider/model-ref->connection-key "anthropic/claude-sonnet-4-6")))+ (is (= "claude-sonnet-4-6" (llm.provider/model-ref->model "anthropic/claude-sonnet-4-6")))+ (is (= "openrouter" (llm.provider/model-ref->connection-key "openrouter/anthropic/claude-haiku-4-5")))+ (is (= "anthropic/claude-haiku-4-5" (llm.provider/model-ref->model "openrouter/anthropic/claude-haiku-4-5"))))+ (testing "the managed connection keeps its wire family in the model part"+ (is (true? (llm.provider/managed-model-ref? "metabase/anthropic/claude-sonnet-4-6")))+ (is (false? (llm.provider/managed-model-ref? "anthropic/claude-sonnet-4-6")))+ (is (false? (llm.provider/managed-model-ref? nil)))+ (is (= "metabase" (llm.provider/model-ref->connection-key "metabase/anthropic/claude-sonnet-4-6")))+ (is (= "anthropic/claude-sonnet-4-6" (llm.provider/model-ref->model "metabase/anthropic/claude-sonnet-4-6"))))+ (testing "the managed routing prefix can be stripped, and stripping is a no-op for everything else"+ (is (= "anthropic/claude-sonnet-4-6" (llm.provider/strip-managed-prefix "metabase/anthropic/claude-sonnet-4-6")))+ (is (= "anthropic/claude-sonnet-4-6" (llm.provider/strip-managed-prefix "anthropic/claude-sonnet-4-6")))+ (is (nil? (llm.provider/strip-managed-prefix nil))))+ (testing "a reference with no model part has no model"+ (is (nil? (llm.provider/model-ref->model "anthropic")))+ (is (nil? (llm.provider/model-ref->connection-key nil)))))++(deftest resolve-model-ref-test+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})+ (connection "openrouter" "openrouter" {:api-key "sk-or-v1-db"})+ (connection "metabase" "metabase")]]+ (testing "a direct connection resolves to its own type, model, and stored credentials"+ (is (= {:connection-key "anthropic"+ :type "anthropic"+ :model "claude-opus-4-1"+ :credentials {:api-key "sk-ant-db" :base-url "https://api.anthropic.com"}+ :ai-proxy? false}+ (llm.provider/resolve-model-ref "anthropic/claude-opus-4-1"))))+ (testing "only the first segment names the connection, so a vendor-prefixed model stays intact"+ (is (= {:connection-key "openrouter"+ :type "openrouter"+ :model "anthropic/claude-haiku-4-5"+ :credentials {:api-key "sk-or-v1-db" :base-url "https://openrouter.ai/api"}+ :ai-proxy? false}+ (llm.provider/resolve-model-ref "openrouter/anthropic/claude-haiku-4-5"))))+ (testing "the managed connection resolves to the wire family named by the model and routes through the proxy"+ (is (= {:connection-key "metabase"+ :type "anthropic"+ :model "claude-sonnet-4-6"+ :credentials nil+ :ai-proxy? true}+ (llm.provider/resolve-model-ref "metabase/anthropic/claude-sonnet-4-6"))))+ (testing "an unknown connection key resolves to nil rather than a half-built request"+ (is (nil? (llm.provider/resolve-model-ref "nope/some-model")))+ (is (nil? (llm.provider/resolve-model-ref "openai/gpt-5.4")))+ (is (nil? (llm.provider/resolve-model-ref nil))))))++(deftest with-field-defaults-normalizes-base-urls-test+ (testing "a base URL keeps no trailing slash, whichever source it comes from, so joining a path cannot double the /"+ (is (= "https://api.mistral.ai/v1"+ (:base-url (llm.provider/with-field-defaults "mistral" {:base-url "https://api.mistral.ai/v1/"}))))+ (is (= "https://self-hosted.example"+ (:base-url (llm.provider/with-field-defaults "anthropic" {:base-url " https://self-hosted.example// "}))))+ (testing "including one supplied by the environment"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-db"})]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-base-url "https://env.example/"]+ (is (= "https://env.example"+ (:base-url (:credentials (llm.provider/resolve-model-ref "anthropic/claude-opus-4-1"))))))))))++(deftest resolve-model-ref-fills-in-field-defaults-test+ (testing "a connection that never set an optional field still resolves with the registry default, so the adapter+ never has to reach back into the single-provider settings for it"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic" {:api-key "sk-ant-db"})+ (connection "bedrock" "bedrock"+ {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "wJalrXUtnFEMI"})]]+ (is (= {:api-key "sk-ant-db" :base-url "https://api.anthropic.com"}+ (:credentials (llm.provider/resolve-model-ref "anthropic/claude-opus-4-1"))))+ (is (= "us-east-1"+ (:region (:credentials (llm.provider/resolve-model-ref "bedrock/anthropic.claude-opus-4-8")))))))+ (testing "a value the admin did set is left alone"+ (mt/with-temporary-setting-values [llm-providers [(connection "anthropic" "anthropic"+ {:api-key "sk-ant-db"+ :base-url "https://proxy.internal"})]]+ (is (= {:api-key "sk-ant-db" :base-url "https://proxy.internal"}+ (:credentials (llm.provider/resolve-model-ref "anthropic/claude-opus-4-1"))))))+ (testing "the single-provider env connection carries its own base URL rather than borrowing another connection's"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"+ mb-llm-anthropic-api-base-url "https://env.example"]+ (is (= {:api-key "sk-ant-env" :base-url "https://env.example"}+ (:credentials (llm.provider/resolve-model-ref "anthropic/claude-opus-4-1"))))))))++(deftest ^:parallel provider-type-names-are-mirrored-on-the-frontend-test+ (testing (str "The frontend mirrors this set by hand as `LlmProviderTypeName` in "+ "frontend/src/metabase-types/api/llm.ts, and keys `PROVIDER_LOGOS` in "+ "frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderTypeIcon.tsx on it "+ "so a type without a decided logo fails to compile. Nothing links the two, so adding a type here "+ "without updating them ships a provider that silently falls back to the generic icon. Update "+ "both, then this list.")+ (is (= #{"anthropic" "openai" "openrouter" "mistral" "zai" "moonshot" "google" "azure" "bedrock" "metabase"}+ (into #{} (map :type) (llm.provider/provider-types))))))++(deftest ^:parallel provider-types-test+ (testing "every registered type is addressable by name, and unknown names are not"+ (is (= (map :type (llm.provider/provider-types))+ (map #(:type (llm.provider/provider-type (:type %))) (llm.provider/provider-types))))+ (is (nil? (llm.provider/provider-type "evilai"))))+ (testing "only the Metabase provider is managed"+ (is (true? (llm.provider/managed-type? "metabase")))+ (is (= "metabase" llm.provider/managed-connection-key))+ (is (false? (llm.provider/managed-type? "anthropic")))+ (is (false? (llm.provider/managed-type? "evilai"))))+ (testing "secret field keys cover exactly the password inputs"+ (is (= #{:api-key} (llm.provider/secret-field-keys "anthropic")))+ (is (= #{:api-key} (llm.provider/secret-field-keys "azure")))+ (is (= #{:access-key-id :secret-access-key :session-token} (llm.provider/secret-field-keys "bedrock")))+ (testing "google's service account key is a file field, but it is the whole credential"+ (is (= #{:service-account-key :oauth-access-token} (llm.provider/secret-field-keys "google"))))+ (is (= #{} (llm.provider/secret-field-keys "metabase"))))+ (testing "every type's default model, which is what a first connection of that type gets selected for it"+ (is (= {"anthropic" "claude-sonnet-4-6"+ "openai" "gpt-5.4"+ "openrouter" "anthropic/claude-sonnet-4.6"+ "mistral" "mistral-medium-3-5"+ "zai" "glm-5.2"+ "moonshot" "kimi-k3"+ "google" "google/gemini-3.5-flash"+ ;; azure's models are deployment names the admin chooses, so there is nothing to default to+ "azure" nil+ "bedrock" "anthropic.claude-opus-4-8"+ "metabase" "anthropic/claude-sonnet-4-6"}+ (into {} (map (juxt :type #(llm.provider/default-model (:type %)))) (llm.provider/provider-types))))+ (is (nil? (llm.provider/default-model "evilai"))))+ (testing "every type other than the managed one is always available"+ (is (true? (llm.provider/type-available? "anthropic")))+ (is (false? (llm.provider/type-available? "evilai")))))diff --git a/test/metabase/llm/settings_test.clj b/test/metabase/llm/settings_test.cljindex 7d247b6410e..b803a8f7350 100644--- a/test/metabase/llm/settings_test.clj+++ b/test/metabase/llm/settings_test.clj@@ -3,31 +3,62 @@[clojure.test :refer :all][metabase.config.core :as config][metabase.llm.settings :as llm.settings]+ [metabase.settings.core :as setting][metabase.test :as mt]))(set! *warn-on-reflection* true)-;;; ------------------------------------------- llm-anthropic-api-key Setter Tests ---------------------------------------------(deftest llm-anthropic-api-key-setter-test- (testing "accepts valid sk-ant- key and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key nil]- (mt/discard-setting-changes [llm-anthropic-api-key]- (llm.settings/llm-anthropic-api-key! " sk-ant-abc123 ")- (is (= "sk-ant-abc123" (llm.settings/llm-anthropic-api-key))))))- (testing "rejects keys without sk-ant- prefix"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Invalid Anthropic API key format"- (llm.settings/llm-anthropic-api-key! "invalid-key"))))- (testing "empty/nil clears the setting"- (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key nil]- (mt/discard-setting-changes [llm-anthropic-api-key]- (llm.settings/llm-anthropic-api-key! "sk-ant-abc123")- (llm.settings/llm-anthropic-api-key! "")- (is (nil? (llm.settings/llm-anthropic-api-key)))))))--;;; ------------------------------------------- llm-anthropic-api-key-configured? Tests -------------------------------------------+(deftest per-provider-settings-write-through-to-the-connection-test+ (testing (str "The per-provider credential settings are a view over the connection list, so config.yml "+ "provisioning — which just calls setting/set! — keeps working now that the value lives there")+ (mt/with-temporary-setting-values [llm-providers []]+ (setting/set! :llm-anthropic-api-key "sk-ant-config-yml")+ (is (= [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {:api-key "sk-ant-config-yml"}}]+ (vec (llm.settings/llm-providers))))+ (testing "and the getter reads the value back off the connection"+ (is (= "sk-ant-config-yml" (llm.settings/llm-anthropic-api-key))))+ (testing "a second setting for the same provider lands on the same connection"+ (setting/set! :llm-anthropic-api-base-url "https://self-hosted.example")+ (is (= {:api-key "sk-ant-config-yml" :base-url "https://self-hosted.example"}+ (:config (first (llm.settings/llm-providers))))))+ (testing "a blank write clears the field"+ (setting/set! :llm-anthropic-api-base-url " ")+ (is (= {:api-key "sk-ant-config-yml"}+ (:config (first (llm.settings/llm-providers)))))))))++(deftest per-provider-settings-keep-their-validation-test+ (testing "the registry's field validation runs on writes, the way the settings' old setters validated"+ (mt/with-temporary-setting-values [llm-providers []]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"must start with 'sk-ant-'"+ (setting/set! :llm-anthropic-api-key "not-an-anthropic-key")))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Invalid Region"+ (setting/set! :llm-bedrock-region "mars-north-1")))+ (is (= [] (vec (llm.settings/llm-providers)))))))++(deftest per-provider-settings-read-defaults-and-env-test+ (testing "with no connection, a setting still resolves its registry default"+ (mt/with-temporary-setting-values [llm-providers []]+ (is (= "https://api.anthropic.com" (llm.settings/llm-anthropic-api-base-url)))))+ (testing "an environment value shadows the stored connection's field"+ (mt/with-temporary-setting-values [llm-providers [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {:api-key "sk-ant-stored"}}]]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key "sk-ant-env"]+ (is (= "sk-ant-env" (llm.settings/llm-anthropic-api-key)))+ (testing "while fields the environment does not supply keep resolving from the connection"+ (is (= "https://api.anthropic.com" (llm.settings/llm-anthropic-api-base-url)))))))+ (testing "an environment value resolves even when no connection exists at all"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-anthropic-api-base-url "https://env-only.example"]+ (is (= "https://env-only.example" (llm.settings/llm-anthropic-api-base-url)))))))++;;; ------------------------------------------- llm-anthropic-api-key Tests -------------------------------------------(deftest llm-anthropic-api-key-configured?-test(testing "returns false when no API key is set"@@ -37,289 +68,47 @@(mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test"](is (true? (llm.settings/llm-anthropic-api-key-configured?))))))-;;; ------------------------------------------- llm-google-project-id Setter Tests ---------------------------------------------(deftest llm-google-project-id-setter-accepts-valid-id-test- (testing "accepts a project ID and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-google-project-id nil]- (mt/discard-setting-changes [llm-google-project-id]- (llm.settings/llm-google-project-id! " my-project-123 ")- (is (= "my-project-123" (llm.settings/llm-google-project-id)))))))--(deftest llm-google-project-id-setter-accepts-length-boundaries-test- (testing "accepts the shortest and longest project IDs Google allows"- (mt/with-temp-env-var-value! [mb-llm-google-project-id nil]- (mt/discard-setting-changes [llm-google-project-id]- (doseq [project-id ["abc123" (apply str "a" (repeat 29 "b"))]]- (llm.settings/llm-google-project-id! project-id)- (is (= project-id (llm.settings/llm-google-project-id))))))))--(deftest llm-google-project-id-setter-rejects-malformed-id-test- (testing "rejects a value that is not a project ID"- (doseq [project-id ["My Project" ; spaces and uppercase- "MY-PROJECT" ; uppercase- "1234567890" ; the project number- "abc" ; shorter than 6 characters- (apply str "a" (repeat 30 "b")) ; longer than 30 characters- "-my-project" ; does not start with a letter- "my-project-" ; ends with a hyphen- "my_project" ; underscores are not allowed- "projects/my-project" ; the resource name+;;; ------------------------------------------- Google credential validation -------------------------------------------++(deftest valid-google-project-id?-test+ (testing "accepts a project ID, trimmed of nothing it does not carry"+ (doseq [project-id ["my-project-123" "abcdef" (apply str (repeat 30 "a"))]]+ (testing project-id+ (is (true? (llm.settings/valid-google-project-id? project-id))))))+ (testing "rejects anything that is not one"+ (doseq [project-id [""+ "My Project" ; the project name+ "123456789012" ; the project number+ "-leading-hyphen"+ "trailing-hyphen-"+ "abcde" ; one short+ (apply str (repeat 31 "a")) ; one long"https://console.cloud.google.com/my-project"]] ; a pasted URL- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"is not a valid Google Cloud project ID"- (llm.settings/llm-google-project-id! project-id))))))--(deftest llm-google-project-id-setter-rejected-value-not-stored-test- (testing "a rejected value leaves the stored project ID alone"- (mt/with-temp-env-var-value! [mb-llm-google-project-id nil]- (mt/discard-setting-changes [llm-google-project-id]- (llm.settings/llm-google-project-id! "my-project-123")- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"\"My Project\" is not a valid Google Cloud project ID"- (llm.settings/llm-google-project-id! "My Project")))- (is (= "my-project-123" (llm.settings/llm-google-project-id)))))))--(deftest llm-google-project-id-setter-clears-on-blank-test- (testing "a whitespace-only value clears the setting"- (mt/with-temp-env-var-value! [mb-llm-google-project-id nil]- (mt/discard-setting-changes [llm-google-project-id]- (llm.settings/llm-google-project-id! "my-project-123")- (llm.settings/llm-google-project-id! " ")- (is (nil? (llm.settings/llm-google-project-id)))))))--;;; ------------------------------------------- llm-google-location Setter Tests ---------------------------------------------(deftest llm-google-location-setter-accepts-served-locations-test- (testing "accepts the location spellings the platform serves, and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-google-location nil]- (mt/discard-setting-changes [llm-google-location]- (doseq [location ["global" "us" "eu" "us-central1" "europe-west4" "asia-northeast1"]]- (llm.settings/llm-google-location! (str " " location " "))- (is (= location (llm.settings/llm-google-location))))))))--(deftest llm-google-location-setter-rejects-malformed-location-test- (testing "rejects a value that cannot be a request host"- (doseq [location ["us central1" ; a space- "us\tcentral1" ; a tab- "us\ncentral1" ; a newline- "US-CENTRAL1" ; uppercase- "us-central1/" ; a slash- "us_central1" ; an underscore- "locations/us-central1" ; the resource name- "https://us-central1-aiplatform.googleapis.com"]] ; a pasted URL- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"is not a valid Google Cloud location"- (llm.settings/llm-google-location! location))))))--(deftest llm-google-location-setter-rejects-malformed-dns-label-test- (testing "rejects a value that is not a well-formed DNS label"- (doseq [location ["-us-central1" ; a leading hyphen- "us-central1-" ; a trailing hyphen- "-" ; a lone hyphen- "us--central1" ; consecutive hyphens- "1us-central"]] ; a leading digit- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"is not a valid Google Cloud location"- (llm.settings/llm-google-location! location))))))--(deftest llm-google-location-setter-rejects-overlong-location-test- (testing "rejects a value too long to be a DNS label once the -aiplatform suffix is added"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"is not a valid Google Cloud location"- (llm.settings/llm-google-location! (apply str (repeat 53 "a")))))))--(deftest llm-google-location-setter-rejected-value-not-stored-test- (testing "a rejected value leaves the stored location alone"- (mt/with-temp-env-var-value! [mb-llm-google-location nil]- (mt/discard-setting-changes [llm-google-location]- (llm.settings/llm-google-location! "us-central1")- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"\"us central1\" is not a valid Google Cloud location"- (llm.settings/llm-google-location! "us central1")))- (is (= "us-central1" (llm.settings/llm-google-location)))))))--(deftest llm-google-location-setter-clears-on-blank-test- (testing "a whitespace-only value clears the setting, falling back to the global location"- (mt/with-temp-env-var-value! [mb-llm-google-location nil]- (mt/discard-setting-changes [llm-google-location]- (llm.settings/llm-google-location! "us-central1")- (llm.settings/llm-google-location! " ")- (is (nil? (llm.settings/llm-google-location)))))))--;;; ------------------------------------------- llm-google-api-base-url Setter Tests ---------------------------------------------(deftest llm-google-api-base-url-setter-test- (testing "trims whitespace and trailing slashes"- (mt/with-temp-env-var-value! [mb-llm-google-api-base-url nil]- (mt/discard-setting-changes [llm-google-api-base-url]- (llm.settings/llm-google-api-base-url! " https://proxy.example.com/aiplatform/ ")- (is (= "https://proxy.example.com/aiplatform" (llm.settings/llm-google-api-base-url))))))- (testing "blank restores the default global host"- (mt/with-temp-env-var-value! [mb-llm-google-api-base-url nil]- (mt/discard-setting-changes [llm-google-api-base-url]- (llm.settings/llm-google-api-base-url! "https://proxy.example.com/aiplatform")- (llm.settings/llm-google-api-base-url! " ")- (is (= "https://aiplatform.googleapis.com" (llm.settings/llm-google-api-base-url)))))))+ (testing project-id+ (is (false? (llm.settings/valid-google-project-id? project-id))))))+ (testing "rejects a non-string"+ (is (false? (llm.settings/valid-google-project-id? nil)))))++(deftest valid-google-location?-test+ (testing "accepts the locations the platform serves"+ (doseq [location ["global" "us" "eu" "us-central1" "europe-west2"]]+ (testing location+ (is (true? (llm.settings/valid-google-location? location))))))+ (testing "rejects anything that cannot be spliced into a request host"+ (doseq [location [""+ "us central1"+ "US-CENTRAL1"+ "us_central1"+ "-us-central1"+ "us-central1-"+ "https://us-central1-aiplatform.googleapis.com"+ ;; a DNS label holds 63 characters and `-aiplatform` takes 11 of them+ (apply str (repeat 53 "a"))]]+ (testing location+ (is (false? (llm.settings/valid-google-location? location)))))));;; ------------------------------------------- llm-bedrock credential Setter Tests --------------------------------------------(deftest llm-bedrock-access-key-id-setter-accepts-valid-key-test- (testing "accepts a valid access key ID and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-bedrock-access-key-id nil]- (mt/discard-setting-changes [llm-bedrock-access-key-id]- (llm.settings/llm-bedrock-access-key-id! " AKIAIOSFODNN7EXAMPLE ")- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))))))--(deftest llm-bedrock-access-key-id-setter-clears-on-empty-test- (testing "empty/nil clears the setting"- (mt/with-temp-env-var-value! [mb-llm-bedrock-access-key-id nil]- (mt/discard-setting-changes [llm-bedrock-access-key-id]- (llm.settings/llm-bedrock-access-key-id! "AKIAIOSFODNN7EXAMPLE")- (llm.settings/llm-bedrock-access-key-id! "")- (is (nil? (llm.settings/llm-bedrock-access-key-id)))))))--(deftest llm-bedrock-secret-access-key-setter-accepts-valid-key-test- (testing "accepts a secret access key and trims surrounding whitespace"- (mt/with-temp-env-var-value! [mb-llm-bedrock-secret-access-key nil]- (mt/discard-setting-changes [llm-bedrock-secret-access-key]- (llm.settings/llm-bedrock-secret-access-key! " wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY ")- (is (= "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest llm-bedrock-secret-access-key-setter-clears-on-blank-test- (testing "a whitespace-only value clears the setting"- (mt/with-temp-env-var-value! [mb-llm-bedrock-secret-access-key nil]- (mt/discard-setting-changes [llm-bedrock-secret-access-key]- (llm.settings/llm-bedrock-secret-access-key! "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY")- (llm.settings/llm-bedrock-secret-access-key! " ")- (is (nil? (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest llm-bedrock-session-token-setter-accepts-valid-token-test- (testing "accepts a session token and trims surrounding whitespace"- (mt/with-temp-env-var-value! [mb-llm-bedrock-session-token nil]- (mt/discard-setting-changes [llm-bedrock-session-token]- (llm.settings/llm-bedrock-session-token! " AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE= ")- (is (= "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE="- (llm.settings/llm-bedrock-session-token)))))))--;;; ------------------------------------------- llm-bedrock-region Setter Tests ---------------------------------------------(deftest llm-bedrock-region-setter-accepts-known-region-test- (testing "accepts a known AWS region and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-bedrock-region nil]- (mt/discard-setting-changes [llm-bedrock-region]- (llm.settings/llm-bedrock-region! " us-west-2 ")- (is (= "us-west-2" (llm.settings/llm-bedrock-region)))))))--(deftest llm-bedrock-region-setter-rejects-unknown-region-test- (testing "rejects a region not in the AWS SDK's known set"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Invalid AWS region \"evil\.example/\?x=\""- (llm.settings/llm-bedrock-region! "evil.example/?x=")))))--(deftest llm-bedrock-region-setter-clears-on-empty-test- (testing "empty/nil clears the setting, falling back to the default"- (mt/with-temp-env-var-value! [mb-llm-bedrock-region nil]- (mt/discard-setting-changes [llm-bedrock-region]- (llm.settings/llm-bedrock-region! "us-west-2")- (llm.settings/llm-bedrock-region! "")- (is (= "us-east-1" (llm.settings/llm-bedrock-region)))))))--;;; ------------------------------------------- llm-azure setting Setter Tests ---------------------------------------------(deftest llm-azure-api-key-setter-test- (testing "accepts an unprefixed Azure data-plane key and trims whitespace"- (mt/with-temp-env-var-value! [mb-llm-azure-api-key nil]- (mt/discard-setting-changes [llm-azure-api-key]- (llm.settings/llm-azure-api-key! " 2QyICJz8sExampleDataPlaneKey ")- (is (= "2QyICJz8sExampleDataPlaneKey" (llm.settings/llm-azure-api-key))))))- (testing "empty/nil clears the setting"- (mt/with-temp-env-var-value! [mb-llm-azure-api-key nil]- (mt/discard-setting-changes [llm-azure-api-key]- (llm.settings/llm-azure-api-key! "2QyICJz8sExampleDataPlaneKey")- (llm.settings/llm-azure-api-key! "")- (is (nil? (llm.settings/llm-azure-api-key)))))))--(deftest llm-azure-api-base-url-setter-test- (testing "trims whitespace and trailing slashes"- (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]- (mt/discard-setting-changes [llm-azure-api-base-url]- (llm.settings/llm-azure-api-base-url! " https://my-resource.services.ai.azure.com/openai/// ")- (is (= "https://my-resource.services.ai.azure.com/openai"- (llm.settings/llm-azure-api-base-url))))))- (testing "is otherwise persisted exactly as entered, with no silent rewriting"- (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]- (mt/discard-setting-changes [llm-azure-api-base-url]- (llm.settings/llm-azure-api-base-url! "https://my-resource.services.ai.azure.com/api/projects/my-project")- (is (= "https://my-resource.services.ai.azure.com/api/projects/my-project"- (llm.settings/llm-azure-api-base-url))))))- (testing "blank clears the setting"- (mt/with-temp-env-var-value! [mb-llm-azure-api-base-url nil]- (mt/discard-setting-changes [llm-azure-api-base-url]- (llm.settings/llm-azure-api-base-url! "https://my-resource.services.ai.azure.com/openai")- (llm.settings/llm-azure-api-base-url! " ")- (is (nil? (llm.settings/llm-azure-api-base-url)))))))--(deftest ^:parallel normalize-llm-base-url-test- (is (= "https://x.example/openai" (llm.settings/normalize-llm-base-url " https://x.example/openai/ ")))- (is (= "https://x.example" (llm.settings/normalize-llm-base-url "https://x.example///")))- (is (nil? (llm.settings/normalize-llm-base-url " ")))- (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-test(testing "can be set and read when :metabase-ai-managed feature is enabled"(mt/with-premium-features #{:metabase-ai-managed}diff --git a/test/metabase/llm/startup_test.clj b/test/metabase/llm/startup_test.cljindex c2909a39607..e3ae7144fd1 100644--- a/test/metabase/llm/startup_test.clj+++ b/test/metabase/llm/startup_test.clj@@ -1,6 +1,8 @@(ns metabase.llm.startup-test(:require[clojure.test :refer [deftest is testing use-fixtures]]+ [metabase.llm.provider :as llm.provider]+ [metabase.llm.settings :as llm.settings][metabase.llm.startup :as llm.startup][metabase.metabot.settings :as metabot.settings][metabase.premium-features.core :as premium-features]@@ -10,46 +12,62 @@(use-fixtures :once (fixtures/initialize :db))+(defn- do-with-entitlements!+ [{:keys [metabot-v3? managed-ai? configured?]} thunk]+ (mt/with-temporary-setting-values [has-user-setup true]+ (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?+ (fn [feature]+ (case feature+ :metabot-v3 metabot-v3?+ :metabase-ai-managed managed-ai?))+ metabot.settings/llm-metabot-configured? (constantly (boolean configured?))]+ (thunk))))++(defmacro ^:private with-entitlements+ "Runs `body` with the token features and Metabot configured-ness pinned. `opts` is a map of `:metabot-v3?` and+ `:managed-ai?` — tri-state, since [[premium-features/canonically-has-feature?]] answers nil before the token+ status is known — and `:configured?`."+ {:style/indent 1}+ [opts & body]+ `(do-with-entitlements! ~opts (fn [] ~@body)))++(defn- do-with-llm-proxy!+ [url thunk]+ (mt/with-premium-features #{:metabot-v3}+ (mt/with-temporary-setting-values [llm-proxy-base-url url]+ (thunk))))++(defmacro ^:private with-llm-proxy+ [url & body]+ `(do-with-llm-proxy! ~url (fn [] ~@body)))+(deftest check-and-sync-settings-on-startup-syncs-legacy-metabot-default-test- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]- (mt/with-temporary-setting-values [has-user-setup true]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 true- :metabase-ai-managed false))- metabot.settings/llm-metabot-configured? (constantly false)]+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false}(llm.startup/check-and-sync-settings-on-startup!)(is (= metabot.settings/default-metabase-llm-metabot-provider(metabot.settings/llm-metabot-provider))))))))(deftest check-and-sync-settings-on-startup-skips-fresh-instances-test- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]- (mt/with-temporary-setting-values [has-user-setup false]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 true- :metabase-ai-managed false))- metabot.settings/llm-metabot-configured? (constantly false)]- (llm.startup/check-and-sync-settings-on-startup!)- (is (nil? (setting/db-stored-value :llm-metabot-provider))))))))+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false}+ (mt/with-temporary-setting-values [has-user-setup false]+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (nil? (setting/db-stored-value :llm-metabot-provider)))+ (is (= [] (vec (llm.settings/llm-providers))))))))))(deftest check-and-sync-settings-on-startup-feature-permutations-test- (doseq [legacy-result [nil false true]+ (doseq [legacy-result [nil false true]managed-result [nil false true]](testing (format ":metabot-v3=%s :metabase-ai-managed=%s" legacy-result managed-result)- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]- (mt/with-temporary-setting-values [has-user-setup true]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 legacy-result- :metabase-ai-managed managed-result))- metabot.settings/llm-metabot-configured? (constantly false)]+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? legacy-result :managed-ai? managed-result}(llm.startup/check-and-sync-settings-on-startup!)(is (= (case [legacy-result managed-result][true false] metabot.settings/default-metabase-llm-metabot-provider@@ -57,43 +75,72 @@(setting/db-stored-value :llm-metabot-provider))))))))))(deftest check-and-sync-settings-on-startup-does-not-overwrite-configured-byok-test- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]- (mt/with-temporary-setting-values [has-user-setup true]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 true- :metabase-ai-managed false))- metabot.settings/llm-metabot-configured? (constantly true)]+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false :configured? true}(llm.startup/check-and-sync-settings-on-startup!)(is (= metabot.settings/default-llm-metabot-provider(metabot.settings/llm-metabot-provider))))))))(deftest check-and-sync-settings-on-startup-does-not-overwrite-explicit-provider-test- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-setting-values [llm-metabot-provider "openai/gpt-4.1-mini"- has-user-setup true]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 true- :metabase-ai-managed false))- metabot.settings/llm-metabot-configured? (constantly false)]- (llm.startup/check-and-sync-settings-on-startup!)- (is (= "openai/gpt-4.1-mini"- (metabot.settings/llm-metabot-provider)))))))+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider "openai/gpt-4.1-mini"]+ (with-entitlements {:metabot-v3? true :managed-ai? false}+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= "openai/gpt-4.1-mini"+ (metabot.settings/llm-metabot-provider))))))))(deftest check-and-sync-settings-on-startup-syncs-blank-provider-test- (mt/discard-setting-changes [llm-metabot-provider]- (mt/with-temporary-raw-setting-values [llm-metabot-provider ""]- (mt/with-temporary-setting-values [has-user-setup true]- (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature?- (fn [feature]- (case feature- :metabot-v3 true- :metabase-ai-managed false))- metabot.settings/llm-metabot-configured? (constantly false)]+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider ""]+ (with-entitlements {:metabot-v3? true :managed-ai? false}(llm.startup/check-and-sync-settings-on-startup!)(is (= metabot.settings/default-metabase-llm-metabot-provider(metabot.settings/llm-metabot-provider))))))))++(deftest managed-connection-is-materialized-only-by-the-legacy-sync-test+ (testing "switching a legacy instance to the managed provider materializes the connection it names"+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false}+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= [{:key "metabase" :type "metabase" :name "Metabase AI service" :config {}}]+ (vec (llm.settings/llm-providers))))+ (testing "and the reference it just wrote resolves against it"+ (is (=? {:connection-key "metabase" :type "anthropic" :ai-proxy? true}+ (llm.provider/resolve-model-ref (metabot.settings/llm-metabot-provider)))))+ (testing "and is not added a second time"+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= ["metabase"] (map :key (llm.settings/llm-providers))))))))))+ (testing "a configured proxy on its own does not connect the managed provider"+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? nil :managed-ai? nil}+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= [] (vec (llm.settings/llm-providers)))))))))+ (testing "an existing connection list keeps its entries"+ (with-llm-proxy "https://proxy.example.com"+ (mt/with-temporary-setting-values [llm-providers [{:key "anthropic"+ :type "anthropic"+ :name "Anthropic"+ :config {:api-key "sk-ant-stored"}}]]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false}+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= ["anthropic" "metabase"] (map :key (llm.settings/llm-providers))))))))))++(deftest managed-connection-is-not-written-when-the-list-is-env-managed-test+ (testing (str "A write would land in the app DB and lose to the env var on every read, so pointing Metabot at the "+ "managed provider would name a connection that never resolves. Leave the selection alone instead.")+ (mt/with-temp-env-var-value! [mb-llm-providers "[{\"key\":\"anthropic\",\"type\":\"anthropic\",\"name\":\"Anthropic\",\"config\":{\"api-key\":\"sk-ant-env\"}}]"]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider nil]+ (with-entitlements {:metabot-v3? true :managed-ai? false}+ (let [stored-before (setting/db-stored-value :llm-providers)]+ (llm.startup/check-and-sync-settings-on-startup!)+ (is (= stored-before (setting/db-stored-value :llm-providers)))+ (is (nil? (setting/db-stored-value :llm-metabot-provider)))))))))diff --git a/test/metabase/llm/test_util.clj b/test/metabase/llm/test_util.cljnew file mode 100644index 00000000000..09be007ee17--- /dev/null+++ b/test/metabase/llm/test_util.clj@@ -0,0 +1,54 @@+(ns metabase.llm.test-util+ (:require+ [metabase.llm.settings :as llm.settings]+ [metabase.test.util :as tu]))++(set! *warn-on-reflection* true)++(def azure-base-url "https://azure-test.services.ai.azure.com/openai")++(def ^:private dummy-configs+ {"anthropic" {:api-key "sk-ant-test"}+ "openai" {:api-key "sk-test"}+ "openrouter" {:api-key "sk-or-v1-test"}+ "mistral" {:api-key "mistral-test-key"}+ "zai" {:api-key "zai-test-key"}+ "moonshot" {:api-key "sk-moonshot-test-key"}+ "google" {:oauth-access-token "ya29.test-token"+ :project-id "my-project"}+ "azure" {:api-key "azure-test-key"+ :base-url azure-base-url+ :model-family "openai"+ :deployment-name "gpt-4.1-mini"}+ "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"+ :region "us-east-1"}+ "metabase" {}})++(defn connection+ ([type-name]+ (connection type-name nil))+ ([type-name config-overrides]+ {:key type-name+ :type type-name+ :name type-name+ :config (merge (get dummy-configs type-name) config-overrides)}))++(def default-connections+ (mapv connection ["anthropic" "openai" "openrouter" "mistral" "zai" "moonshot" "google" "azure" "bedrock"+ "metabase"]))++(defn do-with-connections!+ [connections thunk]+ (tu/with-temporary-setting-values [llm.settings/llm-providers (vec connections)]+ (thunk)))++(defmacro with-connections+ {:style/indent 1}+ [connections & body]+ `(do-with-connections! ~connections (fn [] ~@body)))++(defmacro with-default-connections+ {:style/indent 0}+ [& body]+ `(with-connections default-connections ~@body))diff --git a/test/metabase/metabot/agent/core_test.clj b/test/metabase/metabot/agent/core_test.cljindex 21695b69e1b..bf8ab3a024c 100644--- a/test/metabase/metabot/agent/core_test.clj+++ b/test/metabase/metabot/agent/core_test.clj@@ -8,6 +8,7 @@[metabase.analytics.snowplow-test :as snowplow-test][metabase.lib.core :as lib][metabase.lib.test-metadata :as meta]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.agent.core :as agent][metabase.metabot.agent.memory :as memory][metabase.metabot.persistence :as metabot.persistence]@@ -106,7 +107,8 @@(deftest run-agent-loop-with-mock-test(mt/as-admin- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "runs agent loop with mocked LLM returning text"(mt/with-dynamic-fn-redefs [openrouter/openrouter (fn [_](mut/mock-llm-response@@ -222,7 +224,8 @@(deftest integration-run-agent-loop-test(mt/as-admin- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "runs full agent loop without external calls"(mt/with-dynamic-fn-redefs [openrouter/openrouter (fn [_](mut/mock-llm-response@@ -323,7 +326,8 @@(deftest integration-search-query-chart-flow-test(mt/as-admin- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "Scenario 1: Search → Query → Chart (multi-turn happy path)";; User asks: "Show me the first 10 orders";; - Iteration 1: LLM calls search tool to find orders table@@ -434,7 +438,8 @@;; real. `capture-reducible` binds the capture unconditionally (ignores MB_AI_EVAL_CAPTURE), and;; we redef `emit!` to a no-op so the test never writes a per-session JSONL file.(mt/with-current-user (mt/user->id :crowberto)- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](let [llm-call-count (atom 0)llm-responses [;; Iteration 1: call the search tool (produces a real tool span)[{:type :start :id "msg-1"}@@ -499,7 +504,8 @@(deftest cumulative-usage-test(mt/as-admin- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "usage parts are cumulative across agent loop iterations"(let [call-count (atom 0)](mt/with-dynamic-fn-redefs [openrouter/openrouter@@ -573,7 +579,8 @@(deftest run-agent-loop-retries-on-rate-limit-test(mt/as-admin- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "agent loop retries when LLM returns 429 and then succeeds"(let [call-count (atom 0)](mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)@@ -599,7 +606,8 @@;;; ===================== Prometheus Metrics Tests =====================(deftest run-agent-loop-prometheus-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](mt/with-prometheus-system! [_ system](testing "records agent-requests, agent-iterations, and llm-requests metrics"(let [call-count (atom 0)]@@ -662,7 +670,8 @@;;; ===================== Snowplow Analytics Tests =====================(deftest agent-used-tool-snowplow-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "fires :snowplow/ai_service_event 'agent_used_tool' per tool call"(let [call-count (atom 0)rasta-id (mt/user->id :rasta)]@@ -745,7 +754,8 @@tool-events)))))))))))(deftest token-usage-snowplow-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](testing "fires :snowplow/token_usage event per LLM call"(let [call-count (atom 0)rasta-id (mt/user->id :rasta)]diff --git a/test/metabase/metabot/api/document_test.clj b/test/metabase/metabot/api/document_test.cljindex 7e15d0352ad..0cce77229fa 100644--- a/test/metabase/metabot/api/document_test.clj+++ b/test/metabase/metabot/api/document_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer :all][metabase.analytics.snowplow-test :as snowplow-test]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.scope :as scope][metabase.metabot.self.openrouter :as openrouter][metabase.metabot.test-util :as mut]@@ -17,7 +18,8 @@(use-fixtures :once (fixtures/initialize :db :test-users))(deftest generate-content-backwards-compatible-route-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](mt/with-dynamic-fn-redefs [openrouter/openrouter(fn [_](mut/mock-llm-response@@ -33,7 +35,8 @@{:instructions "Show me sales data"}))))))(deftest generate-content-prometheus-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](mt/with-prometheus-system! [_ system](mt/with-dynamic-fn-redefs [openrouter/openrouter(fn [_]@@ -61,7 +64,8 @@;; resolve the test database *before* process-query gets redefed below, so DB sync (which;; itself calls process-query) isn't affected by the mock(let [db-id (mt/id)]- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](mt/with-dynamic-fn-redefs [create-sql-query-tools/create-sql-query(fn [_]{:validation-result {:valid? true, :dialect "h2"}@@ -96,7 +100,8 @@response))))))))(deftest generate-content-snowplow-test- (mt/with-temporary-setting-values [llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](let [rasta-id (mt/user->id :rasta)](mt/with-dynamic-fn-redefs [openrouter/openrouterdiff --git a/test/metabase/metabot/api_test.clj b/test/metabase/metabot/api_test.cljindex 74509a7a230..8d954952ab8 100644--- a/test/metabase/metabot/api_test.clj+++ b/test/metabase/metabot/api_test.clj@@ -1,6 +1,5 @@(ns metabase.metabot.api-test(:require- [clj-http.client :as http][clojure.core.async :as a][clojure.string :as str][clojure.test :refer :all]@@ -12,6 +11,7 @@[metabase.lib.metadata :as lib.metadata][metabase.lib.test-metadata :as meta][metabase.llm.settings :as llm.settings]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.agent.core :as agent][metabase.metabot.api :as api][metabase.metabot.config :as metabot.config]@@ -38,7 +38,8 @@(def ^:private test-provider "openrouter/anthropic/claude-haiku-4-5")(deftest native-agent-streaming-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](with-redefs [config/is-dev? true](let [conversation-id (str (random-uuid))@@ -247,21 +248,20 @@(try(mt/test-helpers-set-global-values!(search.tu/with-index-disabled- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]- (let [real-http-post http/post]- (with-redefs [llm.settings/llm-openrouter-api-key (constantly "fake-key")- llm.settings/llm-openrouter-api-base-url (constantly llm-url)- scope/resolve-user-permissions (constantly scope/all-yes-permissions)- conversation-title/ensure-title! (constantly {:status :missing})- ;; The fake LLM server doesn't gzip, but clj-http wraps with- ;; GZIPInputStream by default. Closing mid-stream causes ZLIB errors.- http/post (fn [url opts]- (real-http-post url (assoc opts :decompress-body false)))- metabot.context/create-context (fn [ctx & _] ctx)- metabot.persistence/finalize-assistant-turn! (fn [_pk parts & kwargs]- (reset! stored-parts parts)- (reset! stored-kwargs (apply hash-map kwargs)))- sr/async-cancellation-poll-interval-ms 5]+ (mt/with-temporary-setting-values [llm.settings/llm-providers [(llm.tu/connection "openrouter" {:base-url llm-url})]+ metabot.settings/llm-metabot-provider test-provider]+ (let [real-llm-request self.core/request]+ (with-redefs [scope/resolve-user-permissions (constantly scope/all-yes-permissions)+ conversation-title/ensure-title! (constantly {:status :missing})+ ;; The fake LLM server gzips whenever the caller accepts it, and clj-http+ ;; wraps the body in a GZIPInputStream. Closing mid-stream causes ZLIB errors.+ self.core/request (fn [auth req]+ (real-llm-request auth (assoc req :decompress-body false)))+ metabot.context/create-context (fn [ctx & _] ctx)+ metabot.persistence/finalize-assistant-turn! (fn [_pk parts & kwargs]+ (reset! stored-parts parts)+ (reset! stored-kwargs (apply hash-map kwargs)))+ sr/async-cancellation-poll-interval-ms 5](testing "Closing stream body tears down the pipeline and persists the aborted turn"(reset! cnt total-chunks)(reset! stored-parts nil)@@ -302,7 +302,8 @@the reducible is constructed) finalizes the placeholder with:finished? true + a structured :error payload — distinguishable fromboth a successful turn (error nil) and a client abort (finished false)."- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](let [stored-parts (atom nil)stored-kwargs (atom nil)]@@ -347,575 +348,10 @@(is (str/includes? response "data: [DONE]")"the stream terminates with [DONE]"))))))))))-(deftest settings-get-returns-live-models-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-anthropic-api-key "sk-ant-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "anthropic" provider))- {:models [{:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"}]})- ([provider {:keys [credentials]}]- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- {:models [{:id "claude-sonnet-4-5"- :display_name "Claude Sonnet 4.5"}- {:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"}- {:id "claude-opus-4-5"- :display_name "Claude Opus 4.5"}- {:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"}]}))]- (is (= {:value "anthropic/claude-haiku-4-5"- :models [{:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"- :group "Haiku"}- {:id "claude-opus-4-5"- :display_name "Claude Opus 4.5"- :group "Opus"}- {:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}- {:id "claude-sonnet-4-5"- :display_name "Claude Sonnet 4.5"- :group "Sonnet"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings" :provider "anthropic"))))))--(deftest settings-get-normalizes-legacy-anthropic-ids-test- (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:api-key "sk-ant-valid"} credentials))- {:models [{:id "claude-3-haiku-20240307"- :display_name "Claude 3 Haiku"}- {:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"}]})]- (is (= {:value (metabot.settings/llm-metabot-provider)- :models [{:id "claude-3-haiku-20240307"- :display_name "Claude 3 Haiku"- :group "Haiku"}- {:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"- :group "Haiku"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "anthropic"))))))--(deftest settings-get-groups-openrouter-models-test- (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:api-key "sk-or-v1-valid"} credentials))- {:models [{:id "openai/gpt-4.1-mini"- :display_name "OpenAI: GPT-4.1 mini"}- {:id "anthropic/claude-sonnet-4.5"- :display_name "Anthropic: Claude Sonnet 4.5"}]})]- (is (= {:value (metabot.settings/llm-metabot-provider)- :models [{:id "anthropic/claude-sonnet-4.5"- :display_name "Anthropic: Claude Sonnet 4.5"- :group "Anthropic"}- {:id "openai/gpt-4.1-mini"- :display_name "OpenAI: GPT-4.1 mini"- :group "OpenAI"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "openrouter"))))))--(deftest settings-get-groups-openrouter-models-without-vendor-prefix-test- (testing "models whose display_name has no `Vendor: ` prefix are grouped by the vendor from the model id"- (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- {:models [{:id "anthropic/claude-fable-5"- :display_name "Claude Fable 5"}- {:id "openai/gpt-5.5"- :display_name "GPT-5.5"}]})]- (is (= {:value (metabot.settings/llm-metabot-provider)- :models [{:id "anthropic/claude-fable-5"- :display_name "Claude Fable 5"- :group "Anthropic"}- {:id "openai/gpt-5.5"- :display_name "GPT-5.5"- :group "OpenAI"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "openrouter")))))))--(deftest settings-get-groups-openai-models-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "openai/gpt-5-mini"- llm.settings/llm-openai-api-key "sk-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:api-key "sk-valid"} credentials))- {:models [{:id "gpt-5.5" :display_name "gpt-5.5"}- {:id "gpt-5.5-pro" :display_name "gpt-5.5-pro"}- {:id "gpt-5.4" :display_name "gpt-5.4"}- {:id "gpt-5.4-mini" :display_name "gpt-5.4-mini"}- {:id "gpt-4.1-mini" :display_name "gpt-4.1-mini"}]})]- (is (= {:value (metabot.settings/llm-metabot-provider)- :models [{:id "gpt-4.1-mini" :display_name "gpt-4.1-mini" :group "GPT-4.1"}- {:id "gpt-5.4" :display_name "gpt-5.4" :group "GPT-5.4"}- {:id "gpt-5.4-mini" :display_name "gpt-5.4-mini" :group "GPT-5.4"}- {:id "gpt-5.5" :display_name "gpt-5.5" :group "GPT-5.5"}- {:id "gpt-5.5-pro" :display_name "gpt-5.5-pro" :group "GPT-5.5"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "openai"))))))--(deftest settings-get-returns-metabase-models-without-api-key-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is false (str "unexpected list-models call: " provider)))- ([provider opts]- (is (= "anthropic" provider))- (is (= {:ai-proxy? true} opts))- {:models [{:id "claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "claude-opus-4-1" :display_name "Claude Opus 4.1"}]}))]- (is (= {:value "metabase/anthropic/claude-sonnet-4-6"- :models [{:id "anthropic/claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "anthropic/claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "anthropic/claude-opus-4-1" :display_name "Claude Opus 4.1"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "metabase"))))))--(deftest settings-put-updates-provider-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-openai-api-key "sk-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "openai" provider))- {:models [{:id "gpt-4.1-mini"- :display_name "GPT-4.1 mini"}]})- ([provider {:keys [credentials]}]- (is (= "openai" provider))- (is (= {:api-key "sk-valid"} credentials))- {:models [{:id "gpt-4.1-mini"- :display_name "GPT-4.1 mini"}]}))]- (is (= {:value "openai/gpt-4.1-mini"- :models [{:id "gpt-4.1-mini"- :display_name "GPT-4.1 mini"- :group "GPT-4.1"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openai"- :model "gpt-4.1-mini"})))- (is (= "openai/gpt-4.1-mini"- (metabot.settings/llm-metabot-provider))))))--(deftest settings-put-connect-openai-defaults-model-test- (testing "connecting openai with only an api-key switches to the default openai model"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-openai-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "openai" provider))- {:models [{:id "gpt-5.4"- :display_name "gpt-5.4"}]})- ([provider {:keys [credentials]}]- (is (= "openai" provider))- (is (= {:api-key "sk-fresh"} credentials))- {:models [{:id "gpt-5.4"- :display_name "gpt-5.4"}]}))]- (is (= {:value "openai/gpt-5.4"- :models [{:id "gpt-5.4"- :display_name "gpt-5.4"- :group "GPT-5.4"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openai"- :api-key "sk-fresh"})))- (is (= "openai/gpt-5.4"- (metabot.settings/llm-metabot-provider)))- (is (= "sk-fresh"- (llm.settings/llm-openai-api-key)))))))--(deftest settings-put-connect-openrouter-defaults-model-test- (testing "connecting openrouter with only an api-key switches to the default openrouter model"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-openrouter-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "openrouter" provider))- {:models [{:id "anthropic/claude-sonnet-4.6"- :display_name "Anthropic: Claude Sonnet 4.6"}]})- ([provider {:keys [credentials]}]- (is (= "openrouter" provider))- (is (= {:api-key "sk-or-v1-fresh"} credentials))- {:models [{:id "anthropic/claude-sonnet-4.6"- :display_name "Anthropic: Claude Sonnet 4.6"}]}))]- (is (= {:value "openrouter/anthropic/claude-sonnet-4.6"- :models [{:id "anthropic/claude-sonnet-4.6"- :display_name "Anthropic: Claude Sonnet 4.6"- :group "Anthropic"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openrouter"- :api-key "sk-or-v1-fresh"})))- (is (= "openrouter/anthropic/claude-sonnet-4.6"- (metabot.settings/llm-metabot-provider)))- (is (= "sk-or-v1-fresh"- (llm.settings/llm-openrouter-api-key)))))))--(deftest settings-put-connect-zai-defaults-model-test- (testing "connecting zai with only an api-key switches to the default zai model"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-zai-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "zai" provider))- {:models [{:id "glm-5.2"- :display_name "GLM-5.2"}]})- ([provider {:keys [credentials]}]- (is (= "zai" provider))- (is (= {:api-key "zai-key.fresh"} credentials))- {:models [{:id "glm-5.2"- :display_name "GLM-5.2"}]}))]- (is (= {:value "zai/glm-5.2"- :models [{:id "glm-5.2"- :display_name "GLM-5.2"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "zai"- :api-key "zai-key.fresh"})))- (is (= "zai/glm-5.2"- (metabot.settings/llm-metabot-provider)))- (is (= "zai-key.fresh"- (llm.settings/llm-zai-api-key)))))))--(deftest settings-put-connect-mistral-defaults-model-test- (testing "connecting mistral with only an api-key switches to the default mistral model"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-mistral-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn- ([provider]- (is (= "mistral" provider))- {:models [{:id "mistral-medium-3-5"- :display_name "Mistral Medium 3.5"}]})- ([provider {:keys [credentials]}]- (is (= "mistral" provider))- (is (= {:api-key "mistral-key-fresh"} credentials))- {:models [{:id "mistral-medium-3-5"- :display_name "Mistral Medium 3.5"}]}))]- (is (= {:value "mistral/mistral-medium-3-5"- :models [{:id "mistral-medium-3-5"- :display_name "Mistral Medium 3.5"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "mistral"- :api-key "mistral-key-fresh"})))- (is (= "mistral/mistral-medium-3-5"- (metabot.settings/llm-metabot-provider)))- (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 (fn- ([provider]- (is false (str "unexpected list-models call: " provider)))- ([provider opts]- (is (= "anthropic" provider))- (is (= {:ai-proxy? true} opts))- {:models [{:id "claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "claude-opus-4-1" :display_name "Claude Opus 4.1"}]}))]- (is (= {:value "metabase/anthropic/claude-sonnet-4-6"- :models [{:id "anthropic/claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "anthropic/claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "anthropic/claude-opus-4-1" :display_name "Claude Opus 4.1"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "metabase"- :model "anthropic/claude-sonnet-4-6"})))- (is (= "metabase/anthropic/claude-sonnet-4-6"- (metabot.settings/llm-metabot-provider))))))--(deftest settings-put-defaults-empty-metabase-model-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 (fn- ([provider]- (is false (str "unexpected list-models call: " provider)))- ([provider opts]- (is (= "anthropic" provider))- (is (= {:ai-proxy? true} opts))- {:models [{:id "claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "claude-opus-4-1" :display_name "Claude Opus 4.1"}]}))]- (is (= {:value "metabase/anthropic/claude-sonnet-4-6"- :models [{:id "anthropic/claude-haiku-4-5" :display_name "Claude Haiku 4.5"}- {:id "anthropic/claude-sonnet-4-6" :display_name "Claude Sonnet 4.6"}- {:id "anthropic/claude-opus-4-1" :display_name "Claude Opus 4.1"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "metabase"- :model ""})))- (is (= "metabase/anthropic/claude-sonnet-4-6"- (metabot.settings/llm-metabot-provider))))))--(deftest settings-put-verifies-and-saves-api-keys-test- (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key nil]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-anthropic-api-key nil]- (let [calls (atom 0)]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (swap! calls inc)- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- (is (nil? (llm.settings/llm-anthropic-api-key))- "verification should happen before saving the key")- {:models [{:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"}]})]- (is (= {:value "anthropic/claude-haiku-4-5"- :models [{:id "claude-haiku-4-5"- :display_name "Claude Haiku 4.5"- :group "Haiku"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "anthropic"- :api-key "sk-ant-valid"})))- (is (= 1 @calls)- "should verify before saving and reuse the verified response")- (is (= "sk-ant-valid"- (llm.settings/llm-anthropic-api-key))))))))--(deftest settings-put-api-key-rotation-does-not-reset-non-default-model-test- (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key nil]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-opus-4-1"- llm.settings/llm-anthropic-api-key nil]- (let [calls (atom 0)]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (swap! calls inc)- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- (is (nil? (llm.settings/llm-anthropic-api-key))- "verification should happen before saving the key")- {:models [{:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}]})]- (is (= {:value "anthropic/claude-opus-4-1"- :models [{:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "anthropic"- :api-key "sk-ant-valid"})))- (is (= 1 @calls)- "should verify before saving and reuse the verified response")- (is (= "anthropic/claude-opus-4-1"- (metabot.settings/llm-metabot-provider))- "rotating an API key should not reset the selected model")- (is (= "sk-ant-valid"- (llm.settings/llm-anthropic-api-key))))))))--(deftest settings-put-api-key-switches-from-metabase-to-provider-default-model-test- (mt/with-temp-env-var-value! [mb-llm-anthropic-api-key nil]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"- llm.settings/llm-anthropic-api-key nil]- (let [calls (atom 0)]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (swap! calls inc)- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- (is (nil? (llm.settings/llm-anthropic-api-key))- "verification should happen before saving the key")- {:models [{:id "claude-sonnet-4-6"- :display_name "Claude Sonnet 4.6"- :group "Sonnet"}- {:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}]})]- (is (= {:value "anthropic/claude-sonnet-4-6"- :models [{:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}- {:id "claude-sonnet-4-6"- :display_name "Claude Sonnet 4.6"- :group "Sonnet"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "anthropic"- :api-key "sk-ant-valid"})))- (is (= 1 @calls)- "should verify before saving and reuse the verified response")- (is (= "anthropic/claude-sonnet-4-6"- (metabot.settings/llm-metabot-provider))- "switching away from the managed provider should pick the anthropic default model")- (is (= "sk-ant-valid"- (llm.settings/llm-anthropic-api-key))))))))--(deftest settings-put-blank-model-does-not-reset-when-provider-is-unchanged-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-opus-4-1"- llm.settings/llm-anthropic-api-key "sk-ant-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- {:models [{:id "claude-sonnet-4-6"- :display_name "Claude Sonnet 4.6"}- {:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"}]})]- (is (= {:value "anthropic/claude-opus-4-1"- :models [{:id "claude-opus-4-1"- :display_name "Claude Opus 4.1"- :group "Opus"}- {:id "claude-sonnet-4-6"- :display_name "Claude Sonnet 4.6"- :group "Sonnet"}]}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "anthropic"- :model ""})))- (is (= "anthropic/claude-opus-4-1"- (metabot.settings/llm-metabot-provider))- "blank model should not reset the selection when the provider is unchanged"))))--(deftest settings-put-rejects-invalid-api-key-test- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key nil]- (let [calls (atom 0)]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (swap! calls inc)- (is (= "openai" provider))- (is (= {:api-key "sk-invalid"} credentials))- (is (nil? (llm.settings/llm-openai-api-key))- "failed verification should not save the key")- (throw (ex-info "OpenAI API key expired or invalid"- {:api-error true- :status-code 401})))]- (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "openai"- :api-key "sk-invalid"})]- (is (= "OpenAI API key expired or invalid" (:message response)))- (is (= 1 @calls)- "should stop after the failed verification call")- (is (nil? (llm.settings/llm-openai-api-key))))))))--(deftest settings-put-blank-api-key-clears-saved-key-test- (mt/with-temp-env-var-value! [mb-llm-openai-api-key nil]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "openai/gpt-4.1-mini"- llm.settings/llm-openai-api-key "sk-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an explicit nil api-key clears the saved key without validating against the old one"- (is (=? {:value "openai/gpt-4.1-mini"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openai"- :api-key nil})))- (is (nil? (llm.settings/llm-openai-api-key))))))))--(deftest settings-put-does-not-treat-outages-as-invalid-keys-test- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "openai" provider))- (is (= {:api-key "sk-valid"} credentials))- (throw (ex-info "OpenAI API is not working but not saying why"- {:api-error true- :status-code 500})))]- (let [response (mt/user-http-request :crowberto :put 500 "metabot/settings"- {:provider "openai"- :api-key "sk-valid"})]- (is (= "OpenAI API is not working but not saying why" (:message response)))- (is (nil? (llm.settings/llm-openai-api-key)))))))--(deftest settings-put-does-not-save-model-when-preflight-fails-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"- llm.settings/llm-anthropic-api-key "sk-ant-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "anthropic" provider))- (is (= {:api-key "sk-ant-valid"} credentials))- (throw (ex-info "Anthropic API key has insufficient permissions"- {:api-error true- :status-code 403})))]- (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "anthropic"- :model "claude-sonnet-4-5"})]- (is (= "Anthropic API key has insufficient permissions" (:message response)))- (is (= "anthropic/claude-haiku-4-5"- (metabot.settings/llm-metabot-provider)))))))--(deftest settings-get-surfaces-credentials-error-test- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-invalid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (throw (ex-info "OpenAI API key expired or invalid"- {:api-error true- :status-code 401})))]- (is (= {:value (metabot.settings/llm-metabot-provider)- :credentials-error "OpenAI API key expired or invalid"- :models []}- (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "openai"))))))--(deftest settings-get-degrades-non-credential-provider-4xx-test- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-valid"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- ;; e.g. a base URL pointing at the wrong Azure surface;- ;; rethrow-api-error! tags these with :status, not :status-code- (throw (ex-info "OpenAI API error (HTTP 400) — Missing required query parameter: api-version"- {:api-error true- :status 400})))]- (let [response (mt/user-http-request :crowberto :get 200 "metabot/settings"- :provider "openai")]- (is (= [] (:models response)))- (is (re-find #"api-version" (:credentials-error response))- "a non-401 provider 4xx (misconfigured surface) keeps GET /settings loadable and returns the provider message")))))--(deftest settings-put-rejects-env-shadowed-provider-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider "anthropic/claude-haiku-4-5"- mb-llm-openai-api-key nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "openai"- :model "gpt-5.1"- :api-key "sk-new"})]- (is (re-find #"MB_LLM_METABOT_PROVIDER" (:message response))- "a provider/model write is rejected when the provider setting is env-controlled")))))--(deftest settings-put-allows-api-key-rotation-when-provider-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider "openai/gpt-5.1"- mb-llm-openai-api-key nil]- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-old"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:api-key "sk-new"} credentials))- {:models [{:id "gpt-5.1" :display_name "GPT-5.1"}]})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openai"- :api-key "sk-new"})- (is (= "sk-new" (llm.settings/llm-openai-api-key))- "rotating the key for an env-set provider does not require a provider write and so is allowed")))))--(deftest settings-put-env-shadowed-provider-no-op-write-not-persisted-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider "openai/gpt-5.1"- mb-llm-openai-api-key nil]- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-old"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- {:models [{:id "gpt-5.1" :display_name "GPT-5.1"}]})]- (t2/with-transaction [_conn nil {:rollback-only true}]- (t2/delete! :model/Setting :key "llm-metabot-provider")- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "openai"- :model "gpt-5.1"- :api-key "sk-new"})- (testing "a provider/model write that echoes the env var is allowed but persists nothing"- (is (nil? (t2/select-one :model/Setting :key "llm-metabot-provider")))))))))--(deftest settings-permissions-test- (mt/user-http-request :rasta :get 403 "metabot/settings" :provider "anthropic")- (mt/user-http-request :rasta :put 403 "metabot/settings"- {:provider "anthropic"- :model "claude-haiku-4-5"}))-(deftest metabot-provider-without-api-key-is-configured-test(mt/with-premium-features #{:metabase-ai-managed}- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"llm.settings/llm-proxy-base-url "https://proxy.example.com"llm.settings/llm-anthropic-api-key nilllm.settings/llm-openai-api-key nil@@ -979,7 +415,8 @@(t2/delete! :model/MetabotConversation :id conversation-id))))))(deftest metabot-enabled-setting-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](let [base-request {:message "Test":context {}@@ -1069,7 +506,8 @@([thunk] (with-mock-streaming-provider! [] thunk))([responses thunk](let [queue (atom (vec responses))]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](mt/with-dynamic-fn-redefs [openrouter/openrouter(fn [_]@@ -1293,7 +731,8 @@"Runs `thunk` with the provider mocked, appending each provider-call opts mapto `requests` and replying with `reply-text`."[requests reply-text thunk]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](mt/with-dynamic-fn-redefs [openrouter/openrouter(fn [opts]@@ -1378,7 +817,8 @@(deftest agent-streaming-reconstructs-state-from-db-test(testing "the loop is seeded from DB-reconstructed state — no client echo — and a retry rewinds it"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](let [seeded-states (atom [])turn-states (atom [{:queries {"q_1" {:database 1}} :todos [{:id "a" :status "pending"}]}@@ -1483,7 +923,8 @@(binding [mb.api/*current-user-id* (mt/user->id :crowberto)](let [conv-id (str (random-uuid))](try- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider provider](let [{:keys [assistant-msg-id]} (metabot.persistence/start-turn!conv-id "internal"{:role "user" :content "hi"})]@@ -1518,7 +959,8 @@(binding [mb.api/*current-user-id* (mt/user->id :crowberto)](let [conv-id (str (random-uuid))](try- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"](let [{:keys [assistant-msg-id]} (metabot.persistence/start-turn!conv-id "internal"{:role "user" :content "hi"})]@@ -1830,7 +1272,8 @@(deftest agent-streaming-endpoint-captures-embed-referrer-test(testing "POST /metabot/agent-streaming captures x-metabase-embed-referrer as embedding_hostname/embedding_path"(mt/with-premium-features #{:audit-app}- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider test-provider]+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider test-provider](binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions](mt/with-dynamic-fn-redefs [openrouter/openrouter (fn [_](mut/mock-llm-response@@ -1904,7 +1347,8 @@(is (nil? (:sanitized_user_agent convo))))))))))))))(deftest agent-streaming-returns-free-trial-limit-error-when-managed-provider-is-locked-test- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider+ (mt/with-temporary-setting-values [llm.settings/llm-providers llm.tu/default-connections+ metabot.settings/llm-metabot-provider"metabase/anthropic/claude-sonnet-4-6"](mt/with-dynamic-fn-redefs [premium-features/token-status (constantly {:meters {:anthropic:claude-sonnet-4-6:tokens {:meter-value 1000000:is-locked true}}})@@ -1918,962 +1362,3 @@:context {}:conversation_id (str (random-uuid)):state {}}))))--;;; ------------------------------------------------ Bedrock settings --------------------------------------------------(deftest settings-get-groups-bedrock-models-test- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "bedrock" provider))- (is (=? {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"}- credentials)- "the configured AWS credentials are passed to the model lister")- {:models [{:id "openai.gpt-5.5"- :display_name "openai.gpt-5.5"}- {:id "anthropic.claude-haiku-4-5"- :display_name "anthropic.claude-haiku-4-5"}]})]- (is (= {:value (metabot.settings/llm-metabot-provider)- :models [{:id "anthropic.claude-haiku-4-5"- :display_name "anthropic.claude-haiku-4-5"- :group "Anthropic"}- {:id "openai.gpt-5.5"- :display_name "openai.gpt-5.5"- :group "OpenAI"}]}- (mt/user-http-request :crowberto :get 200 "metabot/settings" :provider "bedrock"))))))--(deftest settings-put-saves-bedrock-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-key nil- llm.settings/llm-bedrock-session-token nil- llm.settings/llm-bedrock-region "us-east-1"- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "bedrock" provider))- (is (= {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"- :region "us-east-2"- :session-token "test-token"}- credentials)- "model verification should run against the request credentials")- (is (nil? (llm.settings/llm-bedrock-access-key-id))- "verification should happen before saving the credentials")- {:models [{:id "anthropic.claude-haiku-4-5"- :display_name "anthropic.claude-haiku-4-5"}]})]- (testing "connecting bedrock saves the credentials and selects the default bedrock model"- (is (=? {:value "bedrock/anthropic.claude-opus-4-8"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"- :region "us-east-2"- :session-token "test-token"}}))))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))- (is (= "us-east-2" (llm.settings/llm-bedrock-region)))- (is (= "test-token" (llm.settings/llm-bedrock-session-token)))))))--(deftest settings-put-rejects-env-shadowed-bedrock-credential-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-secret-access-key "env-secret"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (let [response (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"}})]- (is (re-find #"MB_LLM_BEDROCK_SECRET_ACCESS_KEY" (:message response))- "a bedrock credentials write is rejected when one of its settings is env-controlled")))))--(deftest settings-put-bedrock-rejects-incomplete-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-key nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "credentials missing the secret access key fail verification and nothing is saved"- (is (=? {:message "AWS Bedrock credentials are incomplete."- :missing-keys ["secret-access-key"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id "AKIAIOSFODNN7EXAMPLE"}})))- (is (nil? (llm.settings/llm-bedrock-access-key-id)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-bedrock-rejects-blank-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-key nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "all-blank credentials with nothing saved fail verification instead of throwing a 500"- (is (=? {:message "AWS Bedrock credentials are incomplete."- :missing-keys ["access-key-id" "secret-access-key"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id ""- :secret-access-key ""- :session-token ""- :region ""}})))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-bedrock-clears-stale-session-token-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAOLDOLDOLDOLDOLD1"- llm.settings/llm-bedrock-secret-access-key "old-secret"- llm.settings/llm-bedrock-session-token "old-token"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id "AKIANEWNEWNEWNEWNEW1"- :secret-access-key "new-secret"- :session-token nil}})- (testing "an explicit nil session token sent alongside rotated keys clears the saved token"- (is (nil? (llm.settings/llm-bedrock-session-token))))- (is (= "AKIANEWNEWNEWNEWNEW1" (llm.settings/llm-bedrock-access-key-id)))- (is (= "new-secret" (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest settings-put-bedrock-rotation-without-session-token-field-keeps-token-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAOLDOLDOLDOLDOLD1"- llm.settings/llm-bedrock-secret-access-key "old-secret"- llm.settings/llm-bedrock-session-token "old-token"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:access-key-id "AKIANEWNEWNEWNEWNEW1"- :secret-access-key "new-secret"}})- (testing "new key material without a session-token field keeps the saved token; clearing it takes an explicit nil"- (is (= "old-token" (llm.settings/llm-bedrock-session-token))))- (is (= "AKIANEWNEWNEWNEWNEW1" (llm.settings/llm-bedrock-access-key-id)))- (is (= "new-secret" (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest settings-put-bedrock-nil-session-token-clears-token-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "stale-token"- llm.settings/llm-bedrock-region "us-east-2"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "bedrock" provider))- (is (= {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"- :session-token nil- :region "us-east-2"}- credentials)- "validation should run without the cleared session token")- {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:session-token nil}})- (testing "an explicit nil session token clears the saved token without touching the keys"- (is (nil? (llm.settings/llm-bedrock-session-token))))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))- (is (= "us-east-2" (llm.settings/llm-bedrock-region)))))))--(deftest settings-put-bedrock-blank-session-token-clears-token-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "stale-token"- llm.settings/llm-bedrock-region "us-east-2"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:session-token ""}})- (testing "a blank session token field counts as an explicit clear, same as nil"- (is (nil? (llm.settings/llm-bedrock-session-token))))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest settings-put-bedrock-nil-region-resets-to-default-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "test-token"- llm.settings/llm-bedrock-region "us-east-2"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:region nil}})- (testing "an explicit nil region resets the setting to its default"- (is (= "us-east-1" (llm.settings/llm-bedrock-region))))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))- (is (= "test-token" (llm.settings/llm-bedrock-session-token)))))))--(deftest settings-put-bedrock-preserves-session-token-without-new-key-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "test-token"- llm.settings/llm-bedrock-region "us-east-1"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (constantly {:models []})]- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials {:region "us-east-2"}})- (testing "editing an unrelated field without new key material leaves the session token intact"- (is (= "test-token" (llm.settings/llm-bedrock-session-token))))- (is (= "us-east-2" (llm.settings/llm-bedrock-region)))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))))))--(deftest settings-put-nil-bedrock-credentials-clears-saved-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "test-token"- llm.settings/llm-bedrock-region "us-east-2"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an explicit nil credentials clears the saved key material without validating against it"- (is (=? {:value "bedrock/anthropic.claude-opus-4-8"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials nil})))- (is (nil? (llm.settings/llm-bedrock-access-key-id)))- (is (nil? (llm.settings/llm-bedrock-secret-access-key)))- (is (nil? (llm.settings/llm-bedrock-session-token))))- (testing "the clear also resets the region to its default"- (is (= "us-east-1" (llm.settings/llm-bedrock-region))))))))--(deftest settings-put-bedrock-disconnect-allowed-when-region-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region "us-west-2"]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "test-token"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an env-supplied region does not block disconnect"- (is (=? {:value "bedrock/anthropic.claude-opus-4-8"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :credentials nil})))- (is (nil? (llm.settings/llm-bedrock-access-key-id)))- (is (nil? (llm.settings/llm-bedrock-secret-access-key)))- (is (nil? (llm.settings/llm-bedrock-session-token))))- (testing "and the env var keeps supplying the region"- (is (= "us-west-2" (llm.settings/llm-bedrock-region))))))))--(deftest settings-put-bedrock-absent-credentials-leaves-saved-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-bedrock-access-key-id nil- mb-llm-bedrock-secret-access-key nil- mb-llm-bedrock-session-token nil- mb-llm-bedrock-region nil]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "test-secret"- llm.settings/llm-bedrock-session-token "test-token"- llm.settings/llm-bedrock-region "us-east-2"- metabot.settings/llm-metabot-provider "bedrock/anthropic.claude-opus-4-8"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "bedrock" provider))- (is (= {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "test-secret"- :session-token "test-token"- :region "us-east-2"}- credentials)- "a model-only change validates against the saved credentials")- {:models [{:id "anthropic.claude-haiku-4-5"- :display_name "anthropic.claude-haiku-4-5"}]})]- (testing "a body without a credentials key leaves the saved credentials untouched"- (is (=? {:value "bedrock/anthropic.claude-haiku-4-5"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "bedrock"- :model "anthropic.claude-haiku-4-5"})))- (is (= "AKIAIOSFODNN7EXAMPLE" (llm.settings/llm-bedrock-access-key-id)))- (is (= "test-secret" (llm.settings/llm-bedrock-secret-access-key)))- (is (= "test-token" (llm.settings/llm-bedrock-session-token)))- (is (= "us-east-2" (llm.settings/llm-bedrock-region))))))))--;;; ------------------------------------------------ Azure --------------------------------------------------(deftest settings-put-saves-azure-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil- llm.settings/llm-azure-api-base-url nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials model]}]- (is (= "azure" provider))- (is (= {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/anthropic"}- credentials)- "validation runs against the normalized request credentials")- (is (= "anthropic/claude-sonnet-4-5" model)- "the candidate model selects the surface family to validate")- (is (nil? (llm.settings/llm-azure-api-key))- "validation should happen before saving the credentials")- {:models []})]- (testing "connecting azure saves the credentials and the composed provider/model value"- (is (=? {:value "azure/anthropic/claude-sonnet-4-5"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "azure"- :model "anthropic/claude-sonnet-4-5"- :credentials {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/anthropic/"}}))))- (is (= "azure-key" (llm.settings/llm-azure-api-key)))- (testing "the trailing slash is trimmed before persisting"- (is (= "https://my-resource.services.ai.azure.com/anthropic"- (llm.settings/llm-azure-api-base-url))))))))--(deftest settings-put-azure-requires-model-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (testing "switching to azure without a model is rejected — there is no default deployment"- (is (re-find #"model provider and deployment name are required"- (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "azure"- :credentials {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/openai"}}))))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider))))))))--(deftest settings-put-azure-rejects-invalid-model-format-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (testing "an unsupported wire family is rejected before the validation round-trip"- (is (re-find #"Invalid Azure model"- (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "azure"- :model "gemini/some-deployment"- :credentials {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/openai"}}))))))))--(deftest settings-put-azure-rejects-incomplete-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil- llm.settings/llm-azure-api-base-url nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "credentials missing the base URL fail before validation and nothing is saved"- (is (=? {:message "Azure credentials are incomplete."- :missing-keys ["base-url"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "azure"- :model "openai/gpt-4.1-mini"- :credentials {:api-key "azure-key"}})))- (is (nil? (llm.settings/llm-azure-api-key)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-azure-key-rotation-uses-saved-base-url-and-model-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "old-key"- llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"- metabot.settings/llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials model]}]- (is (= {:api-key "new-key"- :base-url "https://my-resource.services.ai.azure.com/anthropic"}- credentials)- "the new key is layered over the saved base URL")- (is (= "anthropic/claude-sonnet-4-5" model)- "a credentials-only rotation validates against the saved model's family")- {:models []})]- (is (=? {:value "azure/anthropic/claude-sonnet-4-5"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "azure"- :credentials {:api-key "new-key"}})))- (is (= "new-key" (llm.settings/llm-azure-api-key)))- (is (= "https://my-resource.services.ai.azure.com/anthropic"- (llm.settings/llm-azure-api-base-url)))))))--(deftest settings-put-nil-azure-credentials-clears-saved-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url nil]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"- metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an explicit nil credentials clears both saved settings without validating against them"- (is (=? {:value "azure/openai/gpt-4.1-mini"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "azure"- :credentials nil})))- (is (nil? (llm.settings/llm-azure-api-key)))- (is (nil? (llm.settings/llm-azure-api-base-url))))))))--(deftest settings-put-rejects-env-shadowed-azure-settings-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key "env-key"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (testing "an azure credentials write is rejected when an azure setting is env-controlled"- (is (re-find #"MB_LLM_AZURE_API_KEY"- (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "azure"- :model "openai/gpt-4.1-mini"- :credentials {:api-key "new-key"- :base-url "https://my-resource.services.ai.azure.com/openai"}})))))))- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url "https://env.services.ai.azure.com/openai"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (testing "the base URL env var is guarded the same way when the request would change it"- (is (re-find #"MB_LLM_AZURE_API_BASE_URL"- (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "azure"- :model "openai/gpt-4.1-mini"- :credentials {:api-key "new-key"- :base-url "https://other.services.ai.azure.com/openai"}}))))))))--(deftest settings-put-allows-azure-key-rotation-when-base-url-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-azure-api-key nil- mb-llm-azure-api-base-url "https://env.services.ai.azure.com/openai"]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "old-key"- metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:api-key "new-key"- :base-url "https://env.services.ai.azure.com/openai"}- credentials))- {:models []})]- (testing "an env-set base URL is layered into the credentials unchanged, so it does not block a key rotation"- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "azure"- :credentials {:api-key "new-key"}})- (is (= "new-key" (llm.settings/llm-azure-api-key))))))))--(deftest settings-get-azure-returns-empty-models-test- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"- metabot.settings/llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "azure" provider))- (is (= {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/anthropic"}- credentials))- {:models []})]- (testing "azure never returns models — deployment names are free text, not a dropdown"- (is (= {:value "azure/anthropic/claude-sonnet-4-5"- :models []}- (mt/user-http-request :crowberto :get 200 "metabot/settings" :provider "azure")))))))--;;; ------------------------------------------------ Google settings --------------------------------------------------(deftest settings-put-saves-google-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "google" provider))- (is (= {:oauth-access-token "ya29.secret-token"- :project-id "my-project"- :location "us-central1"}- credentials)- "validation runs against the resolved request credentials")- (is (nil? (llm.settings/llm-google-oauth-access-token))- "validation should happen before saving the credentials")- {:models []})]- (testing "connecting google saves the credentials and the composed provider/model value"- (is (=? {:value "google/google/gemini-3.5-flash"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"- :location "us-central1"}}))))- (is (= "ya29.secret-token" (llm.settings/llm-google-oauth-access-token)))- (is (= "my-project" (llm.settings/llm-google-project-id)))- (is (= "us-central1" (llm.settings/llm-google-location)))))))--(deftest settings-put-connect-google-defaults-model-test- (testing "connecting google with only credentials switches to the default google model"- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [model]}]- (is (= "google" provider))- (is (= "google/gemini-3.5-flash" model)- "the connect probe validates the defaulted model")- {:models []})]- (is (=? {:value "google/google/gemini-3.5-flash"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"}})))- (is (= "google/google/gemini-3.5-flash"- (metabot.settings/llm-metabot-provider)))- (is (= "ya29.secret-token" (llm.settings/llm-google-oauth-access-token))))))))--(deftest settings-put-google-model-passes-through-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [model]}]- (is (= "google/gemini-3.5-flash" model)- "validation runs against the model as sent — no qualification happens here")- {:models []})]- (testing "a publisher-qualified Gemini model ID is validated and persisted as sent"- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"}}))))))))--(deftest settings-put-google-bare-model-rejected-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "a bare Gemini model ID fails connect validation before any HTTP call, and nothing is persisted"- (mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (=? {:message "Invalid Google model \"gemini-3.5-flash\" — expected a publisher-qualified ID like \"google/gemini-3.5-flash\""}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"}}))))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))- (is (nil? (llm.settings/llm-google-oauth-access-token)))))))--(deftest settings-put-google-rotation-validates-against-saved-model-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.old-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "google/google/gemini-3.6-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [model]}]- (is (= "google/gemini-3.6-flash" model)- "a credential-only rotation validates against the saved model")- {:models []})]- (is (=? {:value "google/google/gemini-3.6-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials {:oauth-access-token "ya29.new-token"}})))- (is (= "ya29.new-token" (llm.settings/llm-google-oauth-access-token)))))))--(deftest settings-put-saves-google-service-account-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider {:keys [credentials]}]- (is (= "google" provider))- (is (= {:service-account-key "{\"type\": \"service_account\"}"}- credentials)- "a service account key alone is a complete credential — the project ID comes from the key JSON")- {:models []})]- (testing "connecting google with only a service account key saves it and the composed provider/model value"- (is (=? {:value "google/google/gemini-3.5-flash"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:service-account-key "{\"type\": \"service_account\"}"}}))))- (is (= "{\"type\": \"service_account\"}" (llm.settings/llm-google-service-account-key)))- (is (nil? (llm.settings/llm-google-oauth-access-token)))- (is (nil? (llm.settings/llm-google-project-id)))))))--(deftest settings-put-google-rejects-missing-project-id-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "an OAuth access token without a project ID fails before validation and nothing is saved"- (is (=? {:message "Google credentials are incomplete."- :missing-keys ["service-account-key" "project-id"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"}})))- (is (nil? (llm.settings/llm-google-oauth-access-token)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-google-rejects-missing-credential-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "credentials without a service account key or OAuth access token fail before validation and nothing is saved"- (is (=? {:message "Google credentials are incomplete."- :missing-keys ["service-account-key" "oauth-access-token"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:project-id "my-project"}})))- (is (nil? (llm.settings/llm-google-project-id)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-google-connect-allowed-when-location-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location "us-central1"]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts] {:models []})]- (testing "an env-supplied location does not block a connect that never touches it"- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"}})))- (is (= "ya29.secret-token" (llm.settings/llm-google-oauth-access-token)))- (is (= "my-project" (llm.settings/llm-google-project-id))))))))--(deftest settings-put-google-rotation-allowed-when-location-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location "us-central1"]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.old-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts] {:models []})]- (testing "the env-set location layered into a connected provider's credentials does not block a rotation"- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials {:oauth-access-token "ya29.new-token"}})- (is (= "ya29.new-token" (llm.settings/llm-google-oauth-access-token))))))))--(deftest settings-put-google-disconnect-allowed-when-location-env-set-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location "us-central1"]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.secret-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an env-supplied location does not block disconnect"- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials nil})))- (is (nil? (llm.settings/llm-google-oauth-access-token)))- (is (nil? (llm.settings/llm-google-project-id))))- (testing "and the env var keeps supplying the location"- (is (= "us-central1" (llm.settings/llm-google-location))))))))--(deftest settings-put-google-disconnect-clears-env-shadowed-credential-row-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token "ya29.env-token"- mb-llm-google-service-account-key nil- mb-llm-google-project-id "env-project"- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-temporary-raw-setting-values [llm-google-oauth-access-token "ya29.stale-db-token"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "disconnect deletes the app-DB row hiding behind the env var"- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials nil})))- (is (nil? (t2/select-one :model/Setting :key "llm-google-oauth-access-token"))- "a skipped clear would let the stale credential resurface once the env var is removed"))- (testing "the env var keeps supplying the value on read"- (is (= "ya29.env-token" (llm.settings/llm-google-oauth-access-token)))))))))--(deftest settings-put-google-rejects-changing-env-shadowed-location-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location "us-central1"]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider _opts]- (is false "should reject before verifying credentials"))]- (testing "a request that would change the env-set location is still rejected"- (is (re-find #"MB_LLM_GOOGLE_LOCATION"- (:message (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"- :location "europe-west4"}})))))))))--(deftest settings-put-google-invalid-service-account-key-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "a service account key the auth library rejects fails connect with its own message, not a generic 500"- (mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (=? {:message #"(?s)Invalid Google service account key: .+"}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:service-account-key "{\"type\": \"service_account\"}"}}))))- (is (nil? (llm.settings/llm-google-service-account-key)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-google-user-credential-key-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "a gcloud user credential uploaded as the key file fails connect with its own message, not a generic 500"- (mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (=? {:message "This Google credential JSON is not a service account key."}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:service-account-key (json/encode {:type "authorized_user"- :client_id "test-client-id"- :client_secret "test-client-secret"- :refresh_token "1//test-refresh-token"})}}))))- (is (nil? (llm.settings/llm-google-service-account-key)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-google-invalid-location-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-service-account-key nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token nil- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id nil- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (testing "a location that cannot be a host fails connect with the location message, not a generic 500"- (mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (=? {:message (str "\"us central1\" is not a valid Google Cloud location. Use a location ID like"- " \"us-central1\", or leave it blank to use the global location.")}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.secret-token"- :project-id "my-project"- :location "us central1"}}))))- (is (nil? (llm.settings/llm-google-location)))- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--(deftest settings-put-google-token-rotation-keeps-project-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.old-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location "us-central1"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:service-account-key nil- :oauth-access-token "ya29.new-token"- :project-id "my-project"- :location "us-central1"}- credentials)- "the new token is layered over the saved project and location")- {:models []})]- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials {:oauth-access-token "ya29.new-token"}})))- (is (= "ya29.new-token" (llm.settings/llm-google-oauth-access-token)))- (is (= "my-project" (llm.settings/llm-google-project-id)))- (is (= "us-central1" (llm.settings/llm-google-location)))))))--(deftest settings-put-google-clears-location-only-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.secret-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location "us-central1"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [_provider {:keys [credentials]}]- (is (= {:service-account-key nil- :oauth-access-token "ya29.secret-token"- :project-id "my-project"- :location nil}- credentials)- "an explicit nil location field clears it (back to the global location)")- {:models []})]- (is (=? {:value "google/google/gemini-3.5-flash"}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials {:location nil}})))- (is (= "ya29.secret-token" (llm.settings/llm-google-oauth-access-token)))- (is (= "my-project" (llm.settings/llm-google-project-id)))- (is (nil? (llm.settings/llm-google-location)))))))--(deftest settings-put-google-rejects-clearing-project-id-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.secret-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location "us-central1"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (testing "an explicit nil project ID leaves incomplete credentials and is rejected; nothing changes"- (is (=? {:message "Google credentials are incomplete."- :missing-keys ["service-account-key" "project-id"]}- (mt/user-http-request :crowberto :put 400 "metabot/settings"- {:provider "google"- :credentials {:project-id nil}})))- (is (= "ya29.secret-token" (llm.settings/llm-google-oauth-access-token)))- (is (= "my-project" (llm.settings/llm-google-project-id)))- (is (= "us-central1" (llm.settings/llm-google-location)))))))--(deftest settings-put-nil-google-credentials-clears-saved-credentials-test- (mt/with-temp-env-var-value! [mb-llm-metabot-provider nil- mb-llm-google-oauth-access-token nil- mb-llm-google-project-id nil- mb-llm-google-location nil]- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.secret-token"- llm.settings/llm-google-service-account-key "{\"type\": \"service_account\"}"- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location "us-central1"- metabot.settings/llm-metabot-provider "google/google/gemini-3.5-flash"]- (mt/with-dynamic-fn-redefs [metabot.self/list-models (fn [provider _opts]- (is false (str "unexpected list-models call: " provider)))]- (testing "an explicit nil credentials clears all saved settings without validating against them"- (is (=? {:value "google/google/gemini-3.5-flash"- :models []}- (mt/user-http-request :crowberto :put 200 "metabot/settings"- {:provider "google"- :credentials nil})))- (is (nil? (llm.settings/llm-google-oauth-access-token)))- (is (nil? (llm.settings/llm-google-service-account-key)))- (is (nil? (llm.settings/llm-google-project-id)))- (is (nil? (llm.settings/llm-google-location))))))))diff --git a/test/metabase/metabot/example_question_generator_test.clj b/test/metabase/metabot/example_question_generator_test.cljindex 8d3af72f7dd..e8f5086b8a7 100644--- a/test/metabase/metabot/example_question_generator_test.clj+++ b/test/metabase/metabot/example_question_generator_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer :all][metabase.analytics.snowplow-test :as snowplow-test]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.example-question-generator :as native-generator][metabase.metabot.self.openrouter :as openrouter][metabase.metabot.test-util :as test-util]@@ -111,7 +112,8 @@;; Mocks openrouter/openrouter rather than native-generator/call-llm to exercise the path through;; self/call-llm-structured and parse-provider-model.(let [captured-opts (atom [])]- (mt/with-temporary-setting-values [llm-metabot-provider "openrouter/anthropic/claude-haiku-4-5"]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider "openrouter/anthropic/claude-haiku-4-5"](mt/with-dynamic-fn-redefs [openrouter/openrouter (fn [opts](swap! captured-opts conj opts)[{:type :start :messageId "msg-1"}@@ -147,7 +149,8 @@(deftest call-llm-prometheus-test(mt/with-prometheus-system! [_ system]- (mt/with-temporary-setting-values [llm-metabot-provider "openrouter/test-model"]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider "openrouter/test-model"](let [labels {:model "openrouter/test-model" :source "example-question-generation"}](testing "increments llm-requests and observes duration on success"(mt/with-dynamic-fn-redefs [openrouter/openrouter@@ -164,7 +167,8 @@(deftest call-llm-snowplow-test(testing "fires token_usage snowplow event for call-llm"(let [rasta-id (mt/user->id :rasta)]- (mt/with-temporary-setting-values [llm-metabot-provider "openrouter/test-model"]+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections+ llm-metabot-provider "openrouter/test-model"](mt/with-dynamic-fn-redefs [openrouter/openrouter(constantly (test-util/mock-llm-response[{:type :start :id "msg-1"}diff --git a/test/metabase/metabot/provider_util_test.clj b/test/metabase/metabot/provider_util_test.cljdeleted file mode 100644index 27c3f0dae38..00000000000--- a/test/metabase/metabot/provider_util_test.clj+++ /dev/null@@ -1,59 +0,0 @@-(ns metabase.metabot.provider-util-test- (:require- [clojure.test :refer :all]- [metabase.metabot.provider-util :as provider-util]))--(deftest ^:parallel metabase-provider?-test- (testing "returns true for metabase/ prefixed providers"- (is (true? (provider-util/metabase-provider? "metabase/anthropic/claude-haiku-4-5")))- (is (true? (provider-util/metabase-provider? "metabase/openai/gpt-4.1-mini")))- (is (true? (provider-util/metabase-provider? "metabase/openrouter/anthropic/claude-haiku-4-5"))))- (testing "returns false for direct providers"- (is (false? (provider-util/metabase-provider? "anthropic/claude-haiku-4-5")))- (is (false? (provider-util/metabase-provider? "openrouter/anthropic/claude-haiku-4-5"))))- (testing "returns false for nil and empty string"- (is (false? (provider-util/metabase-provider? nil)))- (is (false? (provider-util/metabase-provider? "")))))--(deftest ^:parallel provider-and-model->outer-provider-test- (testing "returns top-level prefix"- (is (= "anthropic" (provider-util/provider-and-model->outer-provider "anthropic/claude-haiku-4-5")))- (is (= "openrouter" (provider-util/provider-and-model->outer-provider "openrouter/anthropic/claude-haiku-4-5")))- (is (= "metabase" (provider-util/provider-and-model->outer-provider "metabase/anthropic/claude-haiku-4-5"))))- (testing "returns nil for nil and empty string"- (is (nil? (provider-util/provider-and-model->outer-provider nil)))- (is (= "" (provider-util/provider-and-model->outer-provider "")))))--(deftest ^:parallel provider-and-model->provider-test- (testing "direct providers"- (is (= "anthropic" (provider-util/provider-and-model->provider "anthropic/claude-haiku-4-5")))- (is (= "openai" (provider-util/provider-and-model->provider "openai/gpt-4.1-mini")))- (is (= "openrouter" (provider-util/provider-and-model->provider "openrouter/anthropic/claude-haiku-4-5"))))- (testing "metabase/ prefix extracts inner provider"- (is (= "anthropic" (provider-util/provider-and-model->provider "metabase/anthropic/claude-haiku-4-5")))- (is (= "openai" (provider-util/provider-and-model->provider "metabase/openai/gpt-4.1-mini")))- (is (= "openrouter" (provider-util/provider-and-model->provider "metabase/openrouter/anthropic/claude-haiku-4-5"))))- (testing "returns nil for nil and empty string"- (is (nil? (provider-util/provider-and-model->provider nil)))- (is (= "" (provider-util/provider-and-model->provider "")))))--(deftest ^:parallel provider-and-model->model-test- (testing "direct providers"- (is (= "claude-haiku-4-5" (provider-util/provider-and-model->model "anthropic/claude-haiku-4-5")))- (is (= "gpt-4.1-mini" (provider-util/provider-and-model->model "openai/gpt-4.1-mini")))- (is (= "anthropic/claude-haiku-4-5" (provider-util/provider-and-model->model "openrouter/anthropic/claude-haiku-4-5"))))- (testing "metabase/ prefix extracts model past inner provider"- (is (= "claude-haiku-4-5" (provider-util/provider-and-model->model "metabase/anthropic/claude-haiku-4-5")))- (is (= "gpt-4.1-mini" (provider-util/provider-and-model->model "metabase/openai/gpt-4.1-mini")))- (is (= "anthropic/claude-haiku-4-5" (provider-util/provider-and-model->model "metabase/openrouter/anthropic/claude-haiku-4-5"))))- (testing "returns nil for nil and empty string"- (is (nil? (provider-util/provider-and-model->model nil)))- (is (nil? (provider-util/provider-and-model->model "")))))--(deftest ^:parallel strip-metabase-prefix-test- (testing "strips metabase/ prefix"- (is (= "anthropic/claude-haiku-4-5" (provider-util/strip-metabase-prefix "metabase/anthropic/claude-haiku-4-5")))- (is (= "openrouter/anthropic/claude-haiku-4-5" (provider-util/strip-metabase-prefix "metabase/openrouter/anthropic/claude-haiku-4-5"))))- (testing "returns input unchanged when no prefix"- (is (= "anthropic/claude-haiku-4-5" (provider-util/strip-metabase-prefix "anthropic/claude-haiku-4-5")))- (is (= "openrouter/anthropic/claude-haiku-4-5" (provider-util/strip-metabase-prefix "openrouter/anthropic/claude-haiku-4-5")))))diff --git a/test/metabase/metabot/self/azure_test.clj b/test/metabase/metabot/self/azure_test.cljindex 4b458f59300..9516558ca6e 100644--- a/test/metabase/metabot/self/azure_test.clj+++ b/test/metabase/metabot/self/azure_test.clj@@ -4,6 +4,7 @@[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]@@ -15,25 +16,36 @@(def ^:private test-base-url "https://my-resource.services.ai.azure.com/openai")+(defn- credentials+ "What a resolved Azure connection hands the adapter: adapters read credentials only, never settings."+ ([] (credentials test-base-url))+ ([base-url] {:api-key "azure-key" :base-url base-url}))+;;; ──────────────────────────────────────────────────────────────────;;; Connect validation (list-models);;; ──────────────────────────────────────────────────────────────────(deftest list-models-missing-credentials-test- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil- llm.settings/llm-azure-api-base-url nil]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Azure credentials are not configured"- (azure/list-models {:model "openai/gpt-4.1-mini"})))))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure credentials are not configured"+ (azure/list-models {:model "openai/gpt-4.1-mini"}))))(deftest list-models-requires-both-credential-fields-test- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url nil]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Azure credentials are not configured"- (azure/list-models {:model "openai/gpt-4.1-mini"})))))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure credentials are not configured"+ (azure/list-models {:model "openai/gpt-4.1-mini"+ :credentials {:api-key "azure-key"}}))))++(deftest list-models-does-not-borrow-the-single-provider-setting-test+ (testing "a connection with no credentials fails rather than picking up the single-provider settings"+ (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"+ llm.settings/llm-azure-api-base-url test-base-url]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Azure credentials are not configured"+ (azure/list-models {:model "openai/gpt-4.1-mini"}))))))(deftest list-models-openai-family-round-trips-the-catalog-endpoint-test(testing "validation for the openai family is a GET /v1/models against the candidate credentials"@@ -78,21 +90,22 @@(deftest list-models-falls-back-to-the-configured-azure-model-test(testing "without a candidate model, validation uses the saved Azure model's family"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "azure/openai/gpt-4.1-mini"- llm.settings/llm-azure-api-key "saved-key"- llm.settings/llm-azure-api-base-url test-base-url]- (let [captured (atom nil)]- (with-redefs [http/request (fn [req] (reset! captured req) {:status 200 :body {:data []}})]- (is (= {:models []} (azure/list-models)))- (is (=? {:method :get- :url (str test-base-url "/v1/models")}- @captured)))))))+ (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"]+ (let [captured (atom nil)]+ (with-redefs [http/request (fn [req] (reset! captured req) {:status 200 :body {:data []}})]+ (is (= {:models []}+ (azure/list-models {:credentials {:api-key "saved-key" :base-url test-base-url}})))+ (is (=? {:method :get+ :url (str test-base-url "/v1/models")}+ @captured))))))))(deftest list-models-skips-validation-without-any-model-test(testing "with no candidate model and a non-Azure provider configured, there is no surface to probe"- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (= {:models []} (azure/list-models)))))))+ (llm.tu/with-default-connections+ (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"]+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (= {:models []} (azure/list-models))))))));;; ──────────────────────────────────────────────────────────────────;;; API family dispatch and request construction@@ -102,13 +115,11 @@"Run `azure-raw` with HTTP stubbed out and return the clj-http request map it would send."([opts] (captured-raw-request! test-base-url opts))([base-url opts]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url base-url]- (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})]- (azure/azure-raw opts)))))+ (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})]+ (azure/azure-raw (merge {:credentials (credentials base-url)} opts)))))(deftest anthropic-family-dispatches-to-messages-api-test(let [req (captured-raw-request! "https://my-resource.services.ai.azure.com/anthropic"@@ -171,8 +182,8 @@(deftest unsupported-family-throws-test(is (thrown-with-msg?clojure.lang.ExceptionInfo- #"Unsupported Azure model \"gemini/some-deployment\". Only anthropic/\* and openai/\* models are supported."- (captured-raw-request! {:model "gemini/some-deployment"+ #"Unsupported Azure model \"evilai/some-deployment\". Only anthropic/\* and openai/\* models are supported."+ (captured-raw-request! {:model "evilai/some-deployment":input [{:role :user :content "hi"}]}))));;; ──────────────────────────────────────────────────────────────────@@ -181,25 +192,35 @@(deftest list-models-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil- llm.settings/llm-azure-api-base-url 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 Azure"- (azure/list-models {:model "openai/gpt-4.1-mini" :ai-proxy? true})))))))+ (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 {:model "openai/gpt-4.1-mini" :ai-proxy? true}))))))++(deftest azure-raw-forwards-credentials-test+ (testing "credentials passed to azure-raw reach the request, without requiring saved settings"+ (with-redefs [http/request (fn [req]+ (is (=? {:url (str test-base-url "/v1/responses")+ :headers {"Authorization" "Bearer override-key"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (azure/azure-raw {:model "openai/gpt-4.1-mini"+ :input [{:role :user :content "hi"}]+ :credentials {:api-key "override-key" :base-url test-base-url}}))))))(deftest azure-raw-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key nil- llm.settings/llm-azure-api-base-url 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 Azure"- (azure/azure-raw {:model "openai/gpt-4.1-mini"- :input [{:role :user :content "hi"}]- :ai-proxy? true})))))))+ (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/azure-raw {:model "openai/gpt-4.1-mini"+ :input [{:role :user :content "hi"}]+ :ai-proxy? true}))))));;; ──────────────────────────────────────────────────────────────────;;; Stream translation (xf selection by model family)@@ -213,13 +234,12 @@(.getBytes (str/join (map #(str "data: " (json/encode %) "\n\n") events)) "UTF-8"))})(defn- aisdk-parts-for! [model events]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url test-base-url]- (with-redefs [debug/capture-stream (fn [r _] r)- http/request (fn [_] (sse-response-for events))]- (into [] (self.core/aisdk-xf)- (azure/azure {:model model- :input [{:role :user :content "hi"}]})))))+ (with-redefs [debug/capture-stream (fn [r _] r)+ http/request (fn [_] (sse-response-for events))]+ (into [] (self.core/aisdk-xf)+ (azure/azure {:model model+ :input [{:role :user :content "hi"}]+ :credentials (credentials)}))))(deftest anthropic-family-uses-claude-stream-translation-test(is (=? [{:type :start :id "msg_1"}@@ -254,15 +274,13 @@(defn- list-models-error-message!"The translated message `list-models` throws when the HTTP layer fails with `status`/`body`."[status body]- (mt/with-temporary-setting-values [llm.settings/llm-azure-api-key "azure-key"- llm.settings/llm-azure-api-base-url test-base-url]- (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status status- :headers {"content-type" "application/json"}- :body body})))]- (try- (azure/list-models {:model "openai/gpt-4.1-mini"})- (catch Exception e- (ex-message e))))))+ (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status status+ :headers {"content-type" "application/json"}+ :body body})))]+ (try+ (azure/list-models {:model "openai/gpt-4.1-mini" :credentials (credentials)})+ (catch Exception e+ (ex-message e)))))(deftest auth-error-is-translated-without-body-preview-test(testing "401s get the canonical message; the upstream body is withheld (may carry auth detail)"diff --git a/test/metabase/metabot/self/bedrock_test.clj b/test/metabase/metabot/self/bedrock_test.cljindex 33b259bd982..bb508d952d9 100644--- a/test/metabase/metabot/self/bedrock_test.clj+++ b/test/metabase/metabot/self/bedrock_test.clj@@ -27,6 +27,12 @@{:id "anthropic.claude-3-5-sonnet" :object "model" :status "available"}{:id "openai.gpt-5.4" :object "model" :status "available"}])+(def ^:private credentials+ "What a resolved Bedrock connection hands the adapter: adapters read credentials only, never settings."+ {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"+ :region "us-east-1"})+(deftest ^:parallel supported-model?-test(testing "whitelisted models are supported"(doseq [id ["anthropic.claude-fable-5" "anthropic.claude-opus-5" "anthropic.claude-opus-4-8"@@ -45,7 +51,7 @@{:id "anthropic.claude-opus-4-8" :display_name "Claude Opus 4.8"}{:id "openai.gpt-5.4" :display_name "GPT-5.4"}{:id "openai.gpt-5.5" :display_name "GPT-5.5"}]}- (bedrock/list-models))))))+ (bedrock/list-models {:credentials credentials}))))))(deftest list-models-filters-unavailable-models-test(mt/with-dynamic-fn-redefs@@ -62,19 +68,17 @@: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))))))+ (bedrock/list-models {:credentials credentials}))))))(deftest list-models-missing-credentials-test- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-key nil]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"AWS Bedrock credentials are not configured"- (bedrock/list-models)))))--(deftest list-models-requires-both-keys-test- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key nil]+ (testing "a connection with no credentials fails rather than picking up the single-provider settings"+ (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"+ llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"AWS Bedrock credentials are not configured"+ (bedrock/list-models)))))+ (deftest list-models-requires-both-keys-test(is (thrown-with-msg?clojure.lang.ExceptionInfo#"AWS Bedrock credentials are not configured"@@ -123,25 +127,38 @@(deftest list-models-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-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 AWS Bedrock"- (bedrock/list-models {:ai-proxy? true})))))))+ (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 AWS Bedrock"+ (bedrock/list-models {:ai-proxy? true}))))))++(deftest bedrock-raw-forwards-credentials-test+ (testing "credentials passed to bedrock-raw reach the request, without requiring saved settings"+ (with-redefs [http/request (fn [req]+ (is (= "https://bedrock-mantle.eu-west-1.api.aws/anthropic/v1/messages"+ (:url req)))+ (is (str/includes? (get-in req [:headers "Authorization"])+ "AKIAOVERRIDEOVERRID1"))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (bedrock/bedrock-raw {:model "anthropic.claude-haiku-4-5"+ :input [{:role :user :content "hi"}]+ :credentials {:access-key-id "AKIAOVERRIDEOVERRID1"+ :secret-access-key "override-secret"+ :region "eu-west-1"}}))))))(deftest bedrock-raw-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil- llm.settings/llm-bedrock-secret-access-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 AWS Bedrock"- (bedrock/bedrock-raw {:model "anthropic.claude-haiku-4-5"- :input [{:role :user :content "hi"}]- :ai-proxy? true})))))))+ (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 AWS Bedrock"+ (bedrock/bedrock-raw {:model "anthropic.claude-haiku-4-5"+ :input [{:role :user :content "hi"}]+ :ai-proxy? true}))))));;; ──────────────────────────────────────────────────────────────────;;; API family dispatch@@ -150,15 +167,11 @@(defn- captured-raw-request!"Run `bedrock-raw` with HTTP stubbed out and return the clj-http request map it would send."[opts]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"- llm.settings/llm-bedrock-session-token nil- llm.settings/llm-bedrock-region "us-east-1"]- (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})]- (bedrock/bedrock-raw opts))))+ (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})]+ (bedrock/bedrock-raw (merge {:credentials credentials} opts))))(deftest anthropic-model-dispatches-to-messages-api-test(let [req (captured-raw-request! {:model "anthropic.claude-haiku-4-5"@@ -257,14 +270,12 @@(.getBytes (str/join (map #(str "data: " (json/encode %) "\n\n") events)) "UTF-8"))})(defn- aisdk-parts-for! [model events]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"- llm.settings/llm-bedrock-region "us-east-1"]- (with-redefs [debug/capture-stream (fn [r _] r)- http/request (fn [_] (sse-response-for events))]- (into [] (self.core/aisdk-xf)- (bedrock/bedrock {:model model- :input [{:role :user :content "hi"}]})))))+ (with-redefs [debug/capture-stream (fn [r _] r)+ http/request (fn [_] (sse-response-for events))]+ (into [] (self.core/aisdk-xf)+ (bedrock/bedrock {:model model+ :input [{:role :user :content "hi"}]+ :credentials credentials}))))(deftest anthropic-model-uses-claude-stream-translation-test(is (=? [{:type :start :id "msg_1"}@@ -297,15 +308,12 @@;;; ──────────────────────────────────────────────────────────────────(deftest invalid-region-rejected-before-request-test- (testing "a bogus region set via env var is rejected before the mantle URL is built"- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"]- (mt/with-temp-env-var-value! [mb-llm-bedrock-region "evil.example/?x="]- (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Invalid AWS Bedrock region \"evil\.example/\?x=\""- (bedrock/list-models))))))))+ (testing "a bogus region on the connection is rejected before the mantle URL is built"+ (with-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Invalid AWS Bedrock region \"evil\.example/\?x=\""+ (bedrock/list-models {:credentials (assoc credentials :region "evil.example/?x=")}))))));;; ──────────────────────────────────────────────────────────────────;;; Error translation@@ -314,16 +322,13 @@(defn- list-models-error-message!"The translated message `list-models` throws when the HTTP layer fails with `status`/`body`."[status body]- (mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"- llm.settings/llm-bedrock-region "us-east-1"]- (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status status- :headers {"content-type" "application/json"}- :body body})))]- (try- (bedrock/list-models)- (catch Exception e- (ex-message e))))))+ (with-redefs [http/request (fn [_] (throw (ex-info "HTTP error" {:status status+ :headers {"content-type" "application/json"}+ :body body})))]+ (try+ (bedrock/list-models {:credentials credentials})+ (catch Exception e+ (ex-message e)))))(deftest auth-error-is-translated-without-body-preview-test(testing "403s get the canonical message; the upstream body is withheld (may carry auth detail)"diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.cljindex 6304f69d1db..8acfe6ab94e 100644--- a/test/metabase/metabot/self/claude_test.clj+++ b/test/metabase/metabot/self/claude_test.clj@@ -16,10 +16,16 @@(set! *warn-on-reflection* true)+(def ^:private byok-credentials+ "What a resolved Anthropic connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "sk-ant-byok" :base-url "https://api.anthropic.com"})+(defn- fixture"Load cached Claude raw chunks, or capture from the API when `*live*` / no cache."[fixture-name opts]- (metabot.tu/raw-fixture fixture-name #(claude/claude-raw (merge {:model "claude-haiku-4-5"} opts))))+ (metabot.tu/raw-fixture+ fixture-name+ #(claude/claude-raw (merge {:model "claude-haiku-4-5" :credentials byok-credentials} opts))));;; ──────────────────────────────────────────────────────────────────;;; e2e localhost safeguard@@ -394,9 +400,8 @@(deftest claude-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-anthropic-api-key "sk-ant-byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url "https://proxy.example"]+ (testing "Prefers the connection's own credentials over the ai proxy"(with-redefs [self.core/sse-reducible identityself.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)@@ -405,10 +410,10 @@:url "https://api.anthropic.com/v1/messages":headers {"x-api-key" "sk-ant-byok"}:body string?}- (claude/claude-raw {:input [{:role :user :content "hi"}]})))))+ (claude/claude-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))(testing "Uses ai proxy when explicitly requested"- (with-redefs [llm.settings/llm-anthropic-api-key (constantly nil)- self.core/sse-reducible identity+ (with-redefs [self.core/sse-reducible identityself.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})]@@ -416,21 +421,26 @@:url "https://proxy.example/anthropic/v1/messages":headers {"x-metabase-instance-token" "proxy-token"}:body string?}- (claude/claude-raw {:input [{:role :user :content "hi"}]+ (claude/claude-raw {:input [{:role :user :content "hi"}]:ai-proxy? true})))))- (testing "Does not fall back to ai proxy when BYOK is missing"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Anthropic API key is set"+ (claude/claude-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Anthropic API key is set"- (claude/claude-raw {:input [{:role :user :content "hi"}]})))))+ (claude/claude-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(testing "Throws an error if nothing is defined"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]- (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"No Anthropic API key is set"- (claude/claude-raw {:input [{:role :user :content "hi"}]}))))))))))+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Anthropic API key is set"+ (claude/claude-raw {:input [{:role :user :content "hi"}]})))))))))(defn- capture-claude-request-body!"Invoke `claude-raw` with stubbed HTTP, returning the decoded request body map."@@ -440,7 +450,7 @@http/request (fn [req](reset! captured (json/decode+kw (:body req))){:body req})]- (claude/claude-raw opts))+ (claude/claude-raw (merge {:credentials byok-credentials} opts)))@captured))(deftest claude-tools-cache-breakpoint-test@@ -581,9 +591,8 @@(deftest claude-list-models-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-anthropic-api-key "sk-ant-byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url "https://proxy.example"]+ (testing "Prefers the connection's own credentials over the ai proxy"(mt/with-dynamic-fn-redefs [http/request (fn [req](is (=? {:method :get:url "https://api.anthropic.com/v1/models"@@ -592,35 +601,48 @@req)){:body "{\"data\":[]}"})](is (= {:models []}- (claude/list-models {})))))+ (claude/list-models {:credentials byok-credentials})))))(testing "Uses ai proxy when explicitly requested"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)- http/request (fn [req]- (is (=? {:method :get- :url "https://proxy.example/anthropic/v1/models"- :headers {"anthropic-version" "2023-06-01"- "x-metabase-instance-token" "proxy-token"}}- req))- {:body "{\"data\":[]}"})]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:method :get+ :url "https://proxy.example/anthropic/v1/models"+ :headers {"anthropic-version" "2023-06-01"+ "x-metabase-instance-token" "proxy-token"}}+ req))+ {:body "{\"data\":[]}"})](is (= {:models []}(claude/list-models {:ai-proxy? true})))))- (testing "Does not fall back to ai proxy when BYOK is missing"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Anthropic API key is set"+ (claude/list-models {}))))+ (testing "Throws an error if nothing is defined"+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Anthropic API key is set"- (claude/list-models {})))))- (testing "Throws an error if nothing is defined"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]- (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"No Anthropic API key is set"- (claude/list-models {}))))))))))+ (claude/list-models {})))))))))++(deftest claude-raw-explicit-credentials-test+ (testing "the connection's api-key and base-url are what get used"+ (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-elsewhere"+ llm.settings/llm-anthropic-api-base-url "https://elsewhere.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/v1/messages"+ :headers {"x-api-key" "sk-ant-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (claude/claude-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-ant-explicit"+ :base-url "https://explicit.example"}})))))))(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-anthropic-api-key "sk-ant-setting"]+ (testing "the connection's api-key is what gets used"+ (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-elsewhere"](mt/with-dynamic-fn-redefs [http/request (fn [req](is (=? {:headers {"x-api-key" "sk-ant-explicit"}}req))@@ -628,15 +650,13 @@(is (= {:models []}(claude/list-models {:credentials {:api-key "sk-ant-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-anthropic-api-key "sk-ant-setting"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:headers {"x-api-key" "sk-ant-setting"}}- req))- {:body "{\"data\":[]}"})]- (is (= {:models []}- (claude/list-models {:credentials {:api-key ""}})))))))+(deftest list-models-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Anthropic API key is set"+ (claude/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"@@ -656,21 +676,16 @@(deftest list-models-filters-catalog-to-whitelist-test(testing "list-models keeps only whitelisted models sorted by id, preserving display_name"- (mt/with-temporary-setting-values [llm.settings/llm-anthropic-api-key "sk-ant-byok"]- (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"}]})})]- (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"}]- (:models (claude/list-models))))))))--;;; ──────────────────────────────────────────────────────────────────-;;; temperature support tests-;;; ──────────────────────────────────────────────────────────────────+ (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"}]})})]+ (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"}]+ (:models (claude/list-models {:credentials byok-credentials})))))))(deftest ^:parallel model-supports-temperature?-test(testing "models that accept an explicit temperature"diff --git a/test/metabase/metabot/self/google_test.clj b/test/metabase/metabot/self/google_test.cljindex 20df67e3157..03deb1eb505 100644--- a/test/metabase/metabot/self/google_test.clj+++ b/test/metabase/metabot/self/google_test.clj@@ -5,6 +5,7 @@[clojure.string :as str][clojure.test :refer :all][metabase.llm.settings :as llm.settings]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.self.core :as self.core][metabase.metabot.self.debug :as debug][metabase.metabot.self.google :as google]@@ -40,6 +41,30 @@:client_id "123456789012345678901":token_uri "https://oauth2.googleapis.com/token"}))+;;; The adapter serves a request from the credentials of the connection behind it. These tests bind the+;;; `llm-google-*` settings, which are the environment-configured form of that connection, so the calls below+;;; resolve them into credentials the way `metabase.llm.provider` does before handing them over.++(defn- settings-credentials []+ {:service-account-key (not-empty (llm.settings/llm-google-service-account-key))+ :oauth-access-token (not-empty (llm.settings/llm-google-oauth-access-token))+ :project-id (not-empty (llm.settings/llm-google-project-id))+ :location (not-empty (llm.settings/llm-google-location))+ :base-url (not-empty (llm.settings/llm-google-api-base-url))})++(defn- with-settings-credentials [opts]+ (update opts :credentials #(or % (settings-credentials))))++(defn- google-raw [opts]+ (google/google-raw (with-settings-credentials opts)))++(defn- list-models+ ([] (list-models {}))+ ([opts] (google/list-models (with-settings-credentials opts))))++(defn- google [opts]+ (google/google (with-settings-credentials opts)))+;;; ──────────────────────────────────────────────────────────────────;;; Auth / HTTP tests;;; ──────────────────────────────────────────────────────────────────@@ -58,7 +83,7 @@"/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse"):headers {"Authorization" "Bearer ya29.pasted-access-token"}:body string?}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-default-model-test(testing "a request that does not name a model uses google/gemini-3.5-flash"@@ -71,7 +96,7 @@http/request (fn [req] {:body req})](is (=? {:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global""/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse")}- (google/google-raw {:input [{:role :user :content "hi"}]})))))))+ (google-raw {:input [{:role :user :content "hi"}]})))))))(deftest google-raw-request-body-test(testing "the request streams its response and carries the streamGenerateContent body as JSON"@@ -82,8 +107,8 @@(mt/with-dynamic-fn-redefs [self.core/sse-reducible identitydebug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})]- (let [req (google/google-raw {:model "google/gemini-3.5-flash"- :input [{:role :user :content "hi"}]})]+ (let [req (google-raw {:model "google/gemini-3.5-flash"+ :input [{:role :user :content "hi"}]})](is (=? {:as :stream:headers {"Content-Type" "application/json"}}req))@@ -102,7 +127,7 @@(is (=? {:url (str "https://us-central1-aiplatform.googleapis.com/v1/projects/my-project""/locations/us-central1/publishers/google/models""/gemini-3.5-flash:streamGenerateContent?alt=sse")}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-multi-region-host-test(testing "the us/eu multi-region locations are served by their `rep` host, not the regional spelling"@@ -118,7 +143,7 @@"/v1/projects/my-project/locations/%s""/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse")location location)}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))(deftest google-raw-invalid-location-rejected-test(testing "a location that cannot be a host is rejected with its own message"@@ -129,7 +154,7 @@llm.settings/llm-google-service-account-key nilllm.settings/llm-google-project-id "my-project"](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= (str "\"" location "\" is not a valid Google Cloud location. Use a location ID like"@@ -158,7 +183,7 @@(mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.pasted-access-token"llm.settings/llm-google-service-account-key nil](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= (str "\"" project-id "\"" invalid-project-id-message)@@ -171,9 +196,9 @@(deftest list-models-invalid-credentials-project-id-rejected-test(testing "a connect-time project ID override is validated too"(mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/list-models {:model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.token"- :project-id "my-project/../../evil"}})+ (let [e (try (list-models {:model "google/gemini-3.5-flash"+ :credentials {:oauth-access-token "ya29.token"+ :project-id "my-project/../../evil"}})nil(catch Exception e e))](is (= (str "\"my-project/../../evil\"" invalid-project-id-message)@@ -191,7 +216,7 @@llm.settings/llm-google-project-id nil](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))google/fresh-bearer-headers (constantly {"Authorization" "Bearer test-sa-token"})]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= (str "\"Not-A-Project\"" invalid-project-id-message)@@ -213,7 +238,7 @@http/request (fn [req] {:body req})](is (=? {:url (str "https://gemini.proxy.example.com/v1/projects/my-project/locations/us-central1""/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse")}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-non-google-publisher-model-test(testing "the publisher comes from the model ID's {publisher}/{model} qualifier"@@ -226,7 +251,7 @@http/request (fn [req] {:body req})](is (=? {:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global""/publishers/anthropic/models/claude-sonnet-4-6:streamGenerateContent?alt=sse")}- (google/google-raw {:model "anthropic/claude-sonnet-4-6" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "anthropic/claude-sonnet-4-6" :input [{:role :user :content "hi"}]})))))))(def ^:private bare-model-id-message-re"The message [[metabase.metabot.self.google/model-resource-path]] throws for an unqualified model ID."@@ -242,7 +267,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfobare-model-id-message-re- (google/google-raw {:model "gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-blank-model-id-throws-test(testing "a model ID with a publisher qualifier but no model segment throws before any HTTP call"@@ -254,7 +279,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"Invalid Google model \"google/\""- (google/google-raw {:model "google/" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-invalid-model-characters-rejected-test(testing "a model whose segments cannot be path segments is rejected before any HTTP call"@@ -270,7 +295,7 @@"google/gemini-3.5-flash#""google/gemini 3.5 flash""Google/gemini-3.5-flash"]]- (let [e (try (google/google-raw {:model model :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model model :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= (str "Invalid Google model " (pr-str model) " — a publisher and model ID can hold only letters,"@@ -292,8 +317,8 @@http/request (fn [req] {:body req})](is (=? {:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global""/publishers/anthropic/models/claude-sonnet-4-5@20250929:streamGenerateContent?alt=sse")}- (google/google-raw {:model "anthropic/claude-sonnet-4-5@20250929"- :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "anthropic/claude-sonnet-4-5@20250929"+ :input [{:role :user :content "hi"}]})))))))(deftest google-raw-token-without-project-throws-test(testing "an OAuth access token without a project ID throws before any HTTP call"@@ -304,7 +329,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"A Google Cloud project ID is required for the Google provider"- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-missing-credentials-test(testing "with no credential configured requests throw before any HTTP call"@@ -314,7 +339,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Google API key is set"- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-service-account-bearer-auth-test(testing "a service account key authenticates with a Bearer token and with the project ID read from the key JSON"@@ -330,7 +355,7 @@(is (=? {:url (str "https://aiplatform.googleapis.com/v1/projects/json-project/locations/global""/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse"):headers {"Authorization" "Bearer test-sa-token"}}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))(deftest google-raw-service-account-explicit-project-wins-test(testing "an explicit project ID setting overrides the one embedded in the service account key"@@ -345,7 +370,7 @@google/fresh-bearer-headers (constantly {"Authorization" "Bearer test-sa-token"})](is (=? {:url (str "https://aiplatform.googleapis.com/v1/projects/explicit-project/locations/global""/publishers/google/models/gemini-3.5-flash:streamGenerateContent?alt=sse")}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))(deftest google-raw-service-account-precedence-over-oauth-token-test(testing "a service account key takes precedence over a configured OAuth access token"@@ -359,7 +384,7 @@http/request (fn [req] {:body req})google/fresh-bearer-headers (constantly {"Authorization" "Bearer test-sa-token"})](is (=? {:headers {"Authorization" "Bearer test-sa-token"}}- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))))))))(deftest parse-service-account-credentials-scope-test(testing "service account tokens carry the correct scope"@@ -377,7 +402,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"Invalid Google service account key"- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})))))))(deftest google-raw-invalid-service-account-key-status-test(testing "a rejected service account key is tagged as a 400 so the connect form shows the message"@@ -385,7 +410,7 @@llm.settings/llm-google-service-account-key "{\"type\": \"service_account\"}"llm.settings/llm-google-project-id "my-project"](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (=? {:api-error true@@ -403,7 +428,7 @@llm.settings/llm-google-service-account-key user-credential-jsonllm.settings/llm-google-project-id "my-project"](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= "This Google credential JSON is not a service account key."@@ -419,7 +444,7 @@llm.settings/llm-google-service-account-key "[]"llm.settings/llm-google-project-id "my-project"](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= "Invalid Google service account key" (ex-message e)))@@ -448,7 +473,7 @@(swap! parses inc)(orig json))](dotimes [_ 2]- (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))+ (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]}))(is (= 1 @parses)))))))(deftest fresh-bearer-headers-refresh-failure-test@@ -473,9 +498,9 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"AI proxy is not supported for the Google provider"- (google/google-raw {:model "google/gemini-3.5-flash"- :input [{:role :user :content "hi"}]- :ai-proxy? true}))))))+ (google-raw {:model "google/gemini-3.5-flash"+ :input [{:role :user :content "hi"}]+ :ai-proxy? true}))))));;; ──────────────────────────────────────────────────────────────────;;; End-to-end stream translation.@@ -499,8 +524,8 @@http/request (fn [_] (sse-response-for events))](into [](self.core/aisdk-xf)- (google/google {:model "google/gemini-3.5-flash"- :input [{:role :user :content "hi"}]})))))+ (google {:model "google/gemini-3.5-flash"+ :input [{:role :user :content "hi"}]})))))(deftest google-text-stream-test(testing "streamed text off the wire arrives as one coalesced text part with usage"@@ -552,7 +577,7 @@(let [calls (atom [])](mt/with-dynamic-fn-redefs [http/request (stub-count-tokens calls)](is (= {:models []}- (google/list-models {:model "google/gemini-3.5-flash"})))+ (list-models {:model "google/gemini-3.5-flash"})))(is (=? [{:method :post:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global""/publishers/google/models/gemini-3.5-flash:countTokens")@@ -570,21 +595,22 @@(is (thrown-with-msg?clojure.lang.ExceptionInfobare-model-id-message-re- (google/list-models {:model "gemini-3.5-flash"})))))))+ (list-models {:model "gemini-3.5-flash"})))))))(deftest list-models-defaults-to-saved-model-test- (testing "without a model in opts the probe runs against the saved Google model"- (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.pasted-access-token"- llm.settings/llm-google-service-account-key nil- llm.settings/llm-google-project-id "my-project"- llm.settings/llm-google-location nil- metabot.settings/llm-metabot-provider "google/google/gemini-3.6-flash"]- (let [calls (atom [])]- (mt/with-dynamic-fn-redefs [http/request (stub-count-tokens calls)]- (is (= {:models []} (google/list-models)))- (is (=? [{:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global"- "/publishers/google/models/gemini-3.6-flash:countTokens")}]- @calls)))))))+ (testing "without a model in opts the probe runs against the model the saved reference names"+ (llm.tu/with-connections [(llm.tu/connection "google")]+ (mt/with-temporary-setting-values [llm.settings/llm-google-oauth-access-token "ya29.pasted-access-token"+ llm.settings/llm-google-service-account-key nil+ llm.settings/llm-google-project-id "my-project"+ llm.settings/llm-google-location nil+ metabot.settings/llm-metabot-provider "google/google/gemini-3.6-flash"]+ (let [calls (atom [])]+ (mt/with-dynamic-fn-redefs [http/request (stub-count-tokens calls)]+ (is (= {:models []} (list-models)))+ (is (=? [{:url (str "https://aiplatform.googleapis.com/v1/projects/my-project/locations/global"+ "/publishers/google/models/gemini-3.6-flash:countTokens")}]+ @calls))))))))(deftest list-models-no-model-skips-probe-test(testing "with no candidate model and a non-Google provider configured no call is made"@@ -593,7 +619,7 @@llm.settings/llm-google-project-id "my-project"metabot.settings/llm-metabot-provider "anthropic/claude-sonnet-4-6"](mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (is (= {:models []} (google/list-models)))))))+ (is (= {:models []} (list-models)))))))(deftest list-models-explicit-credentials-test(testing "credentials in opts override the configured settings"@@ -603,10 +629,10 @@(let [calls (atom [])](mt/with-dynamic-fn-redefs [http/request (stub-count-tokens calls)](is (= {:models []}- (google/list-models {:model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.override-token"- :project-id "other-project"- :location "europe-west1"}})))+ (list-models {:model "google/gemini-3.5-flash"+ :credentials {:oauth-access-token "ya29.override-token"+ :project-id "other-project"+ :location "europe-west1"}})))(is (=? [{:url (str "https://europe-west1-aiplatform.googleapis.com/v1/projects/other-project""/locations/europe-west1/publishers/google/models/gemini-3.5-flash:countTokens"):headers {"Authorization" "Bearer ya29.override-token"}}]@@ -620,7 +646,7 @@llm.settings/llm-google-location "eu"](let [calls (atom [])](mt/with-dynamic-fn-redefs [http/request (stub-count-tokens calls)]- (is (= {:models []} (google/list-models {:model "google/gemini-3.5-flash"})))+ (is (= {:models []} (list-models {:model "google/gemini-3.5-flash"})))(is (=? [{:url (str "https://aiplatform.eu.rep.googleapis.com/v1/projects/my-project/locations/eu""/publishers/google/models/gemini-3.5-flash:countTokens")}]@calls)))))))@@ -635,8 +661,8 @@(mt/with-dynamic-fn-redefs [google/fresh-bearer-headers (constantly {"Authorization" "Bearer test-sa-token"})http/request (stub-count-tokens calls)](is (= {:models []}- (google/list-models {:model "google/gemini-3.6-flash"- :credentials {:service-account-key sa-key}})))+ (list-models {:model "google/gemini-3.6-flash"+ :credentials {:service-account-key sa-key}})))(is (=? [{:method :post:url (str "https://aiplatform.googleapis.com/v1/projects/sa-project/locations/global""/publishers/google/models/gemini-3.6-flash:countTokens")@@ -646,8 +672,8 @@(deftest list-models-missing-project-id-status-test(testing "the missing project ID message is tagged as a 400"(mt/with-dynamic-fn-redefs [http/request (fn [_] (throw (ex-info "should never be called" {})))]- (let [e (try (google/list-models {:model "google/gemini-3.5-flash"- :credentials {:oauth-access-token "ya29.token"}})+ (let [e (try (list-models {:model "google/gemini-3.5-flash"+ :credentials {:oauth-access-token "ya29.token"}})nil(catch Exception e e))](is (=? {:api-error true@@ -661,7 +687,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"AI proxy is not supported for the Google provider"- (google/list-models {:model "google/gemini-3.5-flash" :ai-proxy? true}))))))+ (list-models {:model "google/gemini-3.5-flash" :ai-proxy? true}))))))(def ^:private ^String html-404-body"The HTML body the Google front end serves for a 404 from a host that does not exist."@@ -678,7 +704,7 @@{:status 404:headers {"content-type" "text/html; charset=UTF-8"}:body html-404-body})))]- (let [e (try (google/list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))]+ (let [e (try (list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))](is (= (str "Google API endpoint is unavailable or the model was not found ""(endpoint: https://nowhere1-aiplatform.googleapis.com) — ""check that \"nowhere1\" is a valid location")@@ -702,7 +728,7 @@{:error {:code 404:message upstream-message:status "NOT_FOUND"}})})))]- (let [e (try (google/list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))]+ (let [e (try (list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))](is (= (str "Google API endpoint is unavailable or the model was not found ""(endpoint: https://us-central1-aiplatform.googleapis.com) — "upstream-message)@@ -724,7 +750,7 @@{:error {:code 501:message upstream-message:status "UNIMPLEMENTED"}})})))]- (let [e (try (google/list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))]+ (let [e (try (list-models {:model "google/gemini-3.5-flash"}) nil (catch Exception e e))](is (= (str "Google API is not available in this location ""(endpoint: https://us-west8-aiplatform.googleapis.com) — "upstream-message)@@ -743,7 +769,7 @@:headers {"content-type" "text/html; charset=UTF-8"}:body (java.io.ByteArrayInputStream.(.getBytes html-404-body))})))]- (let [e (try (google/google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})+ (let [e (try (google-raw {:model "google/gemini-3.5-flash" :input [{:role :user :content "hi"}]})nil(catch Exception e e))](is (= (str "Google API endpoint is unavailable or the model was not found "@@ -769,7 +795,7 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"Google API rejected the request as invalid — Request contains an invalid argument\."- (google/list-models {:model "google/gemini-3.5-flash"}))))))))+ (list-models {:model "google/gemini-3.5-flash"}))))))))(deftest list-models-error-status-messages-test(testing "credential and quota statuses map to their canonical messages"@@ -790,4 +816,4 @@(is (thrown-with-msg?clojure.lang.ExceptionInfopattern- (google/list-models {:model "google/gemini-3.5-flash"})))))))))+ (list-models {:model "google/gemini-3.5-flash"})))))))))diff --git a/test/metabase/metabot/self/mistral_test.clj b/test/metabase/metabot/self/mistral_test.cljindex fcbdb61090b..a83fd5027e9 100644--- a/test/metabase/metabot/self/mistral_test.clj+++ b/test/metabase/metabot/self/mistral_test.clj@@ -17,6 +17,10 @@;;; mistral-request-body tests;;; ──────────────────────────────────────────────────────────────────+(def ^:private byok-credentials+ "What a resolved Mistral connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "mistral-key-byok" :base-url "https://api.mistral.ai/v1"})+(deftest ^:parallel request-body-default-model-test(testing "the model defaults to mistral-medium-3-5"(is (= "mistral-medium-3-5"@@ -199,9 +203,8 @@(deftest mistral-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-mistral-api-key "mistral-key-byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (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 identitydebug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})]@@ -209,16 +212,22 @@:url "https://api.mistral.ai/v1/chat/completions":headers {"Authorization" "Bearer mistral-key-byok"}:body string?}- (mistral/mistral-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-mistral-api-key nil]+ (mistral/mistral-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Mistral API key is set"+ (mistral/mistral-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Mistral API key is set"- (mistral/mistral-raw {:input [{:role :user :content "hi"}]})))))+ (mistral/mistral-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(testing "Throws an error if nothing is defined"- (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key nil- llm.settings/llm-proxy-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Mistral API key is set"@@ -235,6 +244,31 @@:input [{:role :user :content "hi"}]:ai-proxy? true})))))))+(deftest mistral-raw-explicit-credentials-test+ (testing "a passed-in api-key and base-url are used over the configured ones"+ (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-setting"+ llm.settings/llm-mistral-api-base-url "https://configured.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/chat/completions"+ :headers {"Authorization" "Bearer mistral-key-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (mistral/mistral-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "mistral-key-explicit"+ :base-url "https://explicit.example"}})))))))++(deftest mistral-raw-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Mistral API key is set"+ (mistral/mistral-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}}))))))+(deftest list-models-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"(mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key nil]@@ -250,29 +284,27 @@(deftest list-models-filters-catalog-to-whitelist-test(testing "list-models keeps only whitelisted models with the whitelist display name"- (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-test"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:method :get- :url "https://api.mistral.ai/v1/models"- :headers {"Authorization" "Bearer mistral-key-test"}}- req))- {:status 200 :body {:data [{:id "mistral-large-2512"}- {:id "mistral-medium-3-5"}- {:id "codestral-2508"}]}})]- (is (= {:models [{:id "mistral-medium-3-5" :display_name "Mistral Medium 3.5"}]}- (mistral/list-models)))))))+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:method :get+ :url "https://api.mistral.ai/v1/models"+ :headers {"Authorization" "Bearer mistral-key-byok"}}+ req))+ {:status 200 :body {:data [{:id "mistral-large-2512"}+ {:id "mistral-medium-3-5"}+ {:id "codestral-2508"}]}})]+ (is (= {:models [{:id "mistral-medium-3-5" :display_name "Mistral Medium 3.5"}]}+ (mistral/list-models {:credentials byok-credentials}))))))(deftest list-models-matches-aliases-and-dedupes-test(testing "a catalog entry whose alias is whitelisted resolves to the whitelisted id, deduped across entries"- (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 {:data [{:id "mistral-medium-latest"- :aliases ["mistral-medium-3-5"]}- {:id "mistral-medium-3-5"- :aliases ["mistral-medium-latest"]}]}})]- (is (= {:models [{:id "mistral-medium-3-5" :display_name "Mistral Medium 3.5"}]}- (mistral/list-models)))))))+ (mt/with-dynamic-fn-redefs [http/request (fn [_]+ {:status 200+ :body {:data [{:id "mistral-medium-latest"+ :aliases ["mistral-medium-3-5"]}+ {:id "mistral-medium-3-5"+ :aliases ["mistral-medium-latest"]}]}})]+ (is (= {:models [{:id "mistral-medium-3-5" :display_name "Mistral Medium 3.5"}]}+ (mistral/list-models {:credentials byok-credentials}))))))(deftest list-models-explicit-credentials-test(testing "a passed-in api-key is used over the configured key"@@ -284,15 +316,13 @@(is (= {:models []}(mistral/list-models {:credentials {:api-key "mistral-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-mistral-api-key "mistral-key-setting"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:headers {"Authorization" "Bearer mistral-key-setting"}}- req))- {:status 200 :body {:data []}})]- (is (= {:models []}- (mistral/list-models {:credentials {:api-key ""}})))))))+(deftest list-models-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-mistral-api-key "mistral-key-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Mistral API key is set"+ (mistral/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"@@ -312,15 +342,14 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"Mistral API key expired or invalid"- (mistral/list-models)))))))+ (mistral/list-models {:credentials byok-credentials})))))))(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)))))))+ (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 {:credentials byok-credentials}))))))diff --git a/test/metabase/metabot/self/moonshot_test.clj b/test/metabase/metabot/self/moonshot_test.cljindex 041ed3f91ea..876eef663bc 100644--- a/test/metabase/metabot/self/moonshot_test.clj+++ b/test/metabase/metabot/self/moonshot_test.clj@@ -13,6 +13,10 @@(set! *warn-on-reflection* true)+(def ^:private byok-credentials+ "What a resolved Moonshot connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "sk-moonshot-key-byok" :base-url "https://api.moonshot.ai/v1"})+;;; ──────────────────────────────────────────────────────────────────;;; moonshot-request-body tests;;; ──────────────────────────────────────────────────────────────────@@ -194,9 +198,8 @@(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"+ (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 identitydebug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})]@@ -204,16 +207,22 @@: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]+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Moonshot API key is set"+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Moonshot API key is set"- (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]})))))+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(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]+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Moonshot API key is set"@@ -221,23 +230,37 @@(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" {})))]+ (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 moonshot-raw-explicit-credentials-test+ (testing "a passed-in api-key and base-url are used over the configured ones"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-setting"+ llm.settings/llm-moonshot-api-base-url "https://configured.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/chat/completions"+ :headers {"Authorization" "Bearer sk-moonshot-key-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))](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})))))))+ #"stop"+ (moonshot/moonshot-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-moonshot-key-explicit"+ :base-url "https://explicit.example"}})))))))(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})))))))+ (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@@ -247,31 +270,29 @@(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)))))))+ (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-byok"}}+ 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 {:credentials byok-credentials}))))))(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)))))))+ ;; sees it.+ (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 {:credentials byok-credentials}))))))(deftest list-models-explicit-credentials-test(testing "a passed-in api-key is used over the configured key"@@ -283,15 +304,13 @@(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-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-moonshot-api-key "sk-moonshot-key-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Moonshot API key is set"+ (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"@@ -303,28 +322,27 @@(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)))))))+ (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 {:credentials byok-credentials}))))));;; ──────────────────────────────────────────────────────────────────;;; 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))))))))+ (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 {:credentials byok-credentials})))))))diff --git a/test/metabase/metabot/self/openai_test.clj b/test/metabase/metabot/self/openai_test.cljindex a9d4ee1bd34..4005cc2906c 100644--- a/test/metabase/metabot/self/openai_test.clj+++ b/test/metabase/metabot/self/openai_test.clj@@ -14,10 +14,16 @@(set! *warn-on-reflection* true)+(def ^:private byok-credentials+ "What a resolved OpenAI connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "sk-byok" :base-url "https://api.openai.com"})+(defn- fixture"Load cached OpenAI raw chunks, or capture from the API when `*live*` / no cache."[fixture-name opts]- (metabot.tu/raw-fixture fixture-name #(openai/openai-raw (merge {:model "gpt-4.1-mini"} opts))))+ (metabot.tu/raw-fixture+ fixture-name+ #(openai/openai-raw (merge {:model "gpt-4.1-mini" :credentials byok-credentials} opts))));;; ──────────────────────────────────────────────────────────────────;;; Streaming chunk conversion tests@@ -192,11 +198,14 @@parts (into [] (comp (openai/openai->aisdk-chunks-xf) (self.core/aisdk-xf)) patched)](is (=? [{:type :start}{:type :text :text string?}- {:type :usage- :usage {:promptTokens pos-int?- :completionTokens pos-int?- :cacheCreationTokens nat-int?- :cacheReadTokens nat-int?}}]+ {:type :usage+ ;; a truncated response must not report as a clean completion+ :finish-reason "length"+ :raw-finish-reason "max_output_tokens"+ :usage {:promptTokens pos-int?+ :completionTokens pos-int?+ :cacheCreationTokens nat-int?+ :cacheReadTokens nat-int?}}]parts))(testing "no error chunk is produced for an incomplete (partial-but-valid) response"(is (empty? (filter #(= :error (:type %)) parts)))))))@@ -412,7 +421,32 @@(is (= [{:id "gpt-5.4" :display_name "GPT-5.4"}{:id "gpt-5.6-luna" :display_name "GPT-5.6 Luna"}{:id "gpt-5.6-sol" :display_name "GPT-5.6 Sol"}]- (:models (openai/list-models))))))))+ (:models (openai/list-models {:credentials byok-credentials}))))))))++(deftest openai-raw-explicit-credentials-test+ (testing "a passed-in api-key and base-url are used over the configured ones"+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-setting"+ llm.settings/llm-openai-api-base-url "https://configured.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/v1/responses"+ :headers {"Authorization" "Bearer sk-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (openai/openai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-explicit"+ :base-url "https://explicit.example"}})))))))++(deftest openai-raw-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenAI API key is set"+ (openai/openai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}}))))))(deftest list-models-explicit-credentials-test(testing "a passed-in api-key is used over the configured key"@@ -424,15 +458,13 @@(is (= {:models []}(openai/list-models {:credentials {:api-key "sk-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-openai-api-key "sk-setting"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:headers {"Authorization" "Bearer sk-setting"}}- req))- {:status 200 :body {:data []}})]- (is (= {:models []}- (openai/list-models {:credentials {:api-key ""}})))))))+(deftest list-models-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenAI API key is set"+ (openai/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"@@ -465,47 +497,52 @@(is (true? (#'openai/model-supports-temperature? "openai.gpt-4.1-mini")))))(deftest temperature-omitted-for-reasoning-models-test- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-test"]- (let [request-body (fn [opts]- (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})]- (json/decode+kw (:body (openai/openai-raw- (merge {:input [{:role :user :content "hi"}]- :temperature 0.3}- opts))))))]- (testing "temperature is sent for a non-reasoning model"- (is (= 0.3 (:temperature (request-body {:model "gpt-4.1-mini"})))))- (testing "temperature is omitted for a GPT-5 model"- (is (not (contains? (request-body {:model "gpt-5"}) :temperature))))- (testing "temperature is omitted for an o-series model"- (is (not (contains? (request-body {:model "o3-mini"}) :temperature)))))))+ (let [request-body (fn [opts]+ (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})]+ (json/decode+kw (:body (openai/openai-raw+ (merge {:input [{:role :user :content "hi"}]+ :temperature 0.3+ :credentials byok-credentials}+ opts))))))]+ (testing "temperature is sent for a non-reasoning model"+ (is (= 0.3 (:temperature (request-body {:model "gpt-4.1-mini"})))))+ (testing "temperature is omitted for a GPT-5 model"+ (is (not (contains? (request-body {:model "gpt-5"}) :temperature))))+ (testing "temperature is omitted for an o-series model"+ (is (not (contains? (request-body {:model "o3-mini"}) :temperature))))))(deftest openai-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-openai-api-key "sk-ant-byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (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 identityself.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})](is (=? {:method :post:url "https://api.openai.com/v1/responses"- :headers {"Authorization" "Bearer sk-ant-byok"}+ :headers {"Authorization" "Bearer sk-byok"}:body string?}- (openai/openai-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-openai-api-key nil]+ (openai/openai-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenAI API key is set"+ (openai/openai-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key "sk-elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No OpenAI API key is set"- (openai/openai-raw {:input [{:role :user :content "hi"}]})))))+ (openai/openai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(testing "Throws an error if nothing is defined"- (mt/with-temporary-setting-values [llm.settings/llm-openai-api-key nil- llm.settings/llm-proxy-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No OpenAI API key is set"diff --git a/test/metabase/metabot/self/openrouter_test.clj b/test/metabase/metabot/self/openrouter_test.cljindex 51428133ba7..3190c115b60 100644--- a/test/metabase/metabot/self/openrouter_test.clj+++ b/test/metabase/metabot/self/openrouter_test.clj@@ -13,10 +13,18 @@(set! *warn-on-reflection* true)+(def ^:private byok-credentials+ "What a resolved OpenRouter connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "sk-or-v1-byok" :base-url "https://openrouter.ai/api"})+(defn- fixture"Load cached OpenRouter raw chunks, or capture from the API when `*live*` / no cache."[fixture-name opts]- (metabot.tu/raw-fixture fixture-name #(openrouter/openrouter-raw (merge {:model "anthropic/claude-haiku-4-5"} opts))))+ (metabot.tu/raw-fixture+ fixture-name+ #(openrouter/openrouter-raw (merge {:model "anthropic/claude-haiku-4-5"+ :credentials byok-credentials}+ opts))));;; ──────────────────────────────────────────────────────────────────;;; openrouter-request-body prompt-caching tests@@ -209,9 +217,8 @@(deftest openrouter-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-openrouter-api-key "sk-or-v1-byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (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 identityself.core/reducible-with-api-errors (fn [r _ _] r)debug/capture-stream (fn [r _] r)@@ -220,16 +227,22 @@:url "https://openrouter.ai/api/v1/chat/completions":headers {"Authorization" "Bearer sk-or-v1-byok"}:body string?}- (openrouter/openrouter-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-openrouter-api-key nil]+ (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenRouter API key is set"+ (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No OpenRouter API key is set"- (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]})))))+ (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(testing "Throws an error if nothing is defined"- (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key nil- llm.settings/llm-proxy-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No OpenRouter API key is set"@@ -282,7 +295,32 @@{:id "openai/gpt-5.6-luna" :display_name "OpenAI: GPT-5.6 Luna"}{:id "openai/gpt-5.6-sol" :display_name "OpenAI: GPT-5.6 Sol"}{:id "openai/gpt-5.6-terra" :display_name "OpenAI: GPT-5.6 Terra"}]- (:models (openrouter/list-models))))))))+ (:models (openrouter/list-models {:credentials byok-credentials}))))))))++(deftest openrouter-raw-explicit-credentials-test+ (testing "a passed-in api-key and base-url are used over the configured ones"+ (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-setting"+ llm.settings/llm-openrouter-api-base-url "https://configured.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/v1/chat/completions"+ :headers {"Authorization" "Bearer sk-or-v1-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "sk-or-v1-explicit"+ :base-url "https://explicit.example"}})))))))++(deftest openrouter-raw-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenRouter API key is set"+ (openrouter/openrouter-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}}))))))(deftest list-models-explicit-credentials-test(testing "a passed-in api-key is used over the configured key"@@ -294,15 +332,13 @@(is (= {:models []}(openrouter/list-models {:credentials {:api-key "sk-or-v1-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-openrouter-api-key "sk-or-v1-setting"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:headers {"Authorization" "Bearer sk-or-v1-setting"}}- req))- {:status 200 :body {:data []}})]- (is (= {:models []}- (openrouter/list-models {:credentials {:api-key ""}})))))))+(deftest list-models-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-openrouter-api-key "sk-or-v1-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No OpenRouter API key is set"+ (openrouter/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"diff --git a/test/metabase/metabot/self/zai_test.clj b/test/metabase/metabot/self/zai_test.cljindex 479f19432de..ba34b43b438 100644--- a/test/metabase/metabot/self/zai_test.clj+++ b/test/metabase/metabot/self/zai_test.clj@@ -17,6 +17,10 @@;;; zai-request-body tests;;; ──────────────────────────────────────────────────────────────────+(def ^:private byok-credentials+ "What a resolved Z.AI connection hands the adapter: adapters read credentials only, never settings."+ {:api-key "zai-key.byok" :base-url "https://api.z.ai/api/paas/v4"})+(deftest ^:parallel request-body-default-model-test(testing "the model defaults to glm-5.2"(is (= "glm-5.2"@@ -179,9 +183,8 @@(deftest zai-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-zai-api-key "zai-key.byok"- llm.settings/llm-proxy-base-url "https://proxy.example"]- (testing "Prefers BYOK over ai proxy"+ (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 identitydebug/capture-stream (fn [r _] r)http/request (fn [req] {:body req})]@@ -189,16 +192,22 @@:url "https://api.z.ai/api/paas/v4/chat/completions":headers {"Authorization" "Bearer zai-key.byok"}:body string?}- (zai/zai-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-zai-api-key nil]+ (zai/zai-raw {:input [{:role :user :content "hi"}]+ :credentials byok-credentials})))))+ (testing "Does not fall back to ai proxy when the connection carries no key"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Z\.AI API key is set"+ (zai/zai-raw {:input [{:role :user :content "hi"}]}))))+ (testing "Does not borrow the single-provider setting when the connection carries no key"+ (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key.elsewhere"](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Z\.AI API key is set"- (zai/zai-raw {:input [{:role :user :content "hi"}]})))))+ (zai/zai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}})))))(testing "Throws an error if nothing is defined"- (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key nil- llm.settings/llm-proxy-base-url nil]+ (mt/with-temporary-setting-values [llm.settings/llm-proxy-base-url nil](is (thrown-with-msg?clojure.lang.ExceptionInfo#"No Z\.AI API key is set"@@ -215,6 +224,31 @@:input [{:role :user :content "hi"}]:ai-proxy? true})))))))+(deftest zai-raw-explicit-credentials-test+ (testing "a passed-in api-key and base-url are used over the configured ones"+ (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key-setting"+ llm.settings/llm-zai-api-base-url "https://configured.example"]+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:url "https://explicit.example/chat/completions"+ :headers {"Authorization" "Bearer zai-key-explicit"}}+ req))+ (throw (ex-info "stop" {::stop true})))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"stop"+ (zai/zai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key "zai-key-explicit"+ :base-url "https://explicit.example"}})))))))++(deftest zai-raw-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Z.AI API key is set"+ (zai/zai-raw {:input [{:role :user :content "hi"}]+ :credentials {:api-key ""}}))))))+(deftest list-models-ai-proxy-unsupported-test(testing "ai-proxy? throws before credentials are even consulted"(mt/with-temporary-setting-values [llm.settings/llm-zai-api-key nil]@@ -230,25 +264,23 @@(deftest list-models-filters-catalog-to-whitelist-test(testing "list-models keeps only whitelisted models, falling back to the whitelist display name"- (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key.test"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:method :get- :url "https://api.z.ai/api/paas/v4/models"- :headers {"Authorization" "Bearer zai-key.test"}}- req))- {:status 200 :body {:data [{:id "glm-4.7"}- {:id "glm-5.2"}- {:id "some-other-model"}]}})]- (is (= {:models [{:id "glm-5.2" :display_name "GLM-5.2"}]}- (zai/list-models)))))))+ (mt/with-dynamic-fn-redefs [http/request (fn [req]+ (is (=? {:method :get+ :url "https://api.z.ai/api/paas/v4/models"+ :headers {"Authorization" "Bearer zai-key.byok"}}+ req))+ {:status 200 :body {:data [{:id "glm-4.7"}+ {:id "glm-5.2"}+ {:id "some-other-model"}]}})]+ (is (= {:models [{:id "glm-5.2" :display_name "GLM-5.2"}]}+ (zai/list-models {:credentials byok-credentials}))))))(deftest list-models-prefers-catalog-display-name-test(testing "a display name carried by the catalog entry wins over the whitelist fallback"- (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 {:data [{:id "glm-5.2" :name "GLM-5.2 (catalog)"}]}})]- (is (= {:models [{:id "glm-5.2" :display_name "GLM-5.2 (catalog)"}]}- (zai/list-models)))))))+ (mt/with-dynamic-fn-redefs [http/request (fn [_]+ {:status 200 :body {:data [{:id "glm-5.2" :name "GLM-5.2 (catalog)"}]}})]+ (is (= {:models [{:id "glm-5.2" :display_name "GLM-5.2 (catalog)"}]}+ (zai/list-models {:credentials byok-credentials}))))))(deftest list-models-explicit-credentials-test(testing "a passed-in api-key is used over the configured key"@@ -260,15 +292,13 @@(is (= {:models []}(zai/list-models {:credentials {:api-key "zai-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-zai-api-key "zai-key.setting"]- (mt/with-dynamic-fn-redefs [http/request (fn [req]- (is (=? {:headers {"Authorization" "Bearer zai-key.setting"}}- req))- {:status 200 :body {:data []}})]- (is (= {:models []}- (zai/list-models {:credentials {:api-key ""}})))))))+(deftest list-models-blank-credentials-do-not-borrow-the-setting-test+ (testing "a blank api-key does not fall back to the single-provider setting"+ (mt/with-temporary-setting-values [llm.settings/llm-zai-api-key "zai-key-elsewhere"]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"No Z.AI API key is set"+ (zai/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"@@ -288,15 +318,14 @@(is (thrown-with-msg?clojure.lang.ExceptionInfo#"Z\.AI API key expired or invalid"- (zai/list-models)))))))+ (zai/list-models {:credentials byok-credentials})))))))(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)))))))+ (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 {:credentials byok-credentials}))))))diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.cljindex 945707d664f..c00ee0ce54e 100644--- a/test/metabase/metabot/self_test.clj+++ b/test/metabase/metabot/self_test.clj@@ -6,16 +6,13 @@[clojure.test :refer :all][metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test]+ [metabase.llm.test-util :as llm.tu][metabase.metabot.schema.v2 :as schema.v2][metabase.metabot.scope :as scope][metabase.metabot.self :as self][metabase.metabot.self.claude :as self.claude][metabase.metabot.self.core :as self.core]- [metabase.metabot.self.mistral :as self.mistral]- [metabase.metabot.self.openai :as self.openai]- [metabase.metabot.self.openai.chat-completions :as self.chat-completions][metabase.metabot.self.openrouter :as openrouter]- [metabase.metabot.self.zai :as self.zai][metabase.metabot.settings :as metabot.settings][metabase.metabot.test-util :as test-util][metabase.metabot.usage :as usage]@@ -32,35 +29,36 @@;;; provider resolution tests-(deftest ^:parallel parse-provider-model-test- (testing "parses provider/model format correctly"- (is (=? {:provider "anthropic" :model "claude-haiku-4-5" :ai-proxy? false}- (#'self/parse-provider-model "anthropic/claude-haiku-4-5")))- (is (=? {:provider "openai" :model "gpt-4.1-mini" :ai-proxy? false}- (#'self/parse-provider-model "openai/gpt-4.1-mini")))- (is (=? {:provider "openrouter" :model "anthropic/claude-haiku-4-5" :ai-proxy? false}- (#'self/parse-provider-model "openrouter/anthropic/claude-haiku-4-5")))- (is (=? {:provider "openrouter" :model "google/gemini-2.5-flash" :ai-proxy? false}- (#'self/parse-provider-model "openrouter/google/gemini-2.5-flash")))- (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")))- (is (=? {:provider "moonshot" :model "kimi-k2.6" :ai-proxy? false}- (#'self/parse-provider-model "moonshot/kimi-k2.6")))- (is (=? {:provider "google" :model "google/gemini-3.5-flash" :ai-proxy? false}- (#'self/parse-provider-model "google/google/gemini-3.5-flash"))))- (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")))- (is (=? {:provider "openai" :model "gpt-4.1-mini" :ai-proxy? true}- (#'self/parse-provider-model "metabase/openai/gpt-4.1-mini")))- (is (=? {:provider "openrouter" :model "anthropic/claude-haiku-4-5" :ai-proxy? true}- (#'self/parse-provider-model "metabase/openrouter/anthropic/claude-haiku-4-5"))))- (testing "throws for invalid formats/models"- (is (thrown-with-msg? Exception #"Unknown LLM provider: no-slash" (#'self/parse-provider-model "no-slash")))- (is (thrown-with-msg? Exception #"Unknown LLM provider: " (#'self/parse-provider-model "")))- (is (thrown-with-msg? Exception #"Unknown LLM provider: " (#'self/parse-provider-model "/leading-slash")))))+(deftest parse-provider-model-test+ (llm.tu/with-default-connections+ (testing "resolves connection-key/model against the configured connections"+ (is (=? {:provider "anthropic" :model "claude-haiku-4-5" :ai-proxy? false}+ (#'self/parse-provider-model "anthropic/claude-haiku-4-5")))+ (is (=? {:provider "openai" :model "gpt-4.1-mini" :ai-proxy? false}+ (#'self/parse-provider-model "openai/gpt-4.1-mini")))+ (is (=? {:provider "openrouter" :model "anthropic/claude-haiku-4-5" :ai-proxy? false}+ (#'self/parse-provider-model "openrouter/anthropic/claude-haiku-4-5")))+ (is (=? {:provider "openrouter" :model "google/gemini-2.5-flash" :ai-proxy? false}+ (#'self/parse-provider-model "openrouter/google/gemini-2.5-flash")))+ (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")))+ (is (=? {:provider "moonshot" :model "kimi-k3" :ai-proxy? false}+ (#'self/parse-provider-model "moonshot/kimi-k3")))+ (is (=? {:provider "google" :model "google/gemini-3.5-flash" :ai-proxy? false}+ (#'self/parse-provider-model "google/google/gemini-3.5-flash"))))+ (testing "serves the managed connection through the wire family the model names"+ (is (=? {:provider "anthropic" :model "claude-haiku-4-5" :ai-proxy? true}+ (#'self/parse-provider-model "metabase/anthropic/claude-haiku-4-5")))+ (is (=? {:provider "openai" :model "gpt-4.1-mini" :ai-proxy? true}+ (#'self/parse-provider-model "metabase/openai/gpt-4.1-mini")))+ (is (=? {:provider "openrouter" :model "anthropic/claude-haiku-4-5" :ai-proxy? true}+ (#'self/parse-provider-model "metabase/openrouter/anthropic/claude-haiku-4-5"))))+ (testing "throws when the string names no configured connection"+ (doseq [model-ref ["no-slash" "" "/leading-slash" "nonexistent/some-model"]]+ (is (thrown-with-msg? Exception #"No LLM provider connection named"+ (#'self/parse-provider-model model-ref)))))))(deftest ^:parallel resolve-adapter-test(testing "resolves known providers to adapter functions"@@ -76,33 +74,34 @@(#'self/resolve-adapter "unknown")))))(deftest call-llm-tool-choice-test- (testing "passes required tool choice to LLM providers"- (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.- (mt/with-dynamic-fn-redefs [http/request (fn [opts]- (when (:body opts)- (reset! captured (json/decode+kw (:body opts))))- (throw (ex-info "stop" {::skip true :api-error true})))]- (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"- llm-proxy-base-url "http://proxy.example"- llm-openrouter-api-key "sk-or-v1-test-key"- llm-openai-api-key "sk-test-key"]- (doseq [[model expected] [["anthropic/test-model" {:type "any"}]- ["metabase/anthropic/claude-sonnet-4-6" {:type "any"}]- ["openrouter/test-model" "required"]- ["openai/test-model" "required"]]]- (try- (run! identity (self/call-llm model- nil- []- {"search" (get test-util/TOOLS "get-time")}- {:tag "agent"}- {:tool-choice "required"}))- (catch Exception e- (when-not (::skip (ex-data e))- (throw e))))- (is (= expected (:tool_choice @captured))))))))))+ (llm.tu/with-default-connections+ (testing "passes required tool choice to LLM providers"+ (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.+ (mt/with-dynamic-fn-redefs [http/request (fn [opts]+ (when (:body opts)+ (reset! captured (json/decode+kw (:body opts))))+ (throw (ex-info "stop" {::skip true :api-error true})))]+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"+ llm-proxy-base-url "http://proxy.example"+ llm-openrouter-api-key "sk-or-v1-test-key"+ llm-openai-api-key "sk-test-key"]+ (doseq [[model expected] [["anthropic/test-model" {:type "any"}]+ ["metabase/anthropic/claude-sonnet-4-6" {:type "any"}]+ ["openrouter/test-model" "required"]+ ["openai/test-model" "required"]]]+ (try+ (run! identity (self/call-llm model+ nil+ []+ {"search" (get test-util/TOOLS "get-time")}+ {:tag "agent"}+ {:tool-choice "required"}))+ (catch Exception e+ (when-not (::skip (ex-data e))+ (throw e))))+ (is (= expected (:tool_choice @captured)))))))))))(deftest request-timeout-settings-test(testing "request seeds timeouts from the llm-*-timeout-ms settings, read at call time"@@ -123,90 +122,90 @@(is (= 200 (:socket-timeout @captured)))))))))(deftest call-llm-prompt-cache-key-test- (testing "the conversation id (:session-id) is forwarded as the Mistral prompt_cache_key"- (let [captured (atom nil)]- ;; `:api-error true` makes `rethrow-api-error!` rethrow as-is, so `::skip` survives on the outer ex-data.- (mt/with-dynamic-fn-redefs [http/request (fn [opts]- (when (:body opts)- (reset! captured (json/decode+kw (:body opts))))- (throw (ex-info "stop" {::skip true :api-error true})))]- (mt/with-temporary-setting-values [llm-mistral-api-key "mistral-key-test"]- (let [call! (fn [tracking-opts]- (reset! captured nil)- (try- (run! identity (self/call-llm "mistral/mistral-medium-3-5" nil [] {} tracking-opts))- (catch Exception e- (when-not (::skip (ex-data e))- (throw e)))))]- (call! {:tag "agent" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"})- (is (= "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48" (:prompt_cache_key @captured)))- (testing "no prompt_cache_key without a :session-id"- (call! {:tag "agent"})- (is (not (contains? @captured :prompt_cache_key))))))))))+ (llm.tu/with-default-connections+ (testing "the conversation id (:session-id) is forwarded as the Mistral prompt_cache_key"+ (let [captured (atom nil)]+ ;; `:api-error true` makes `rethrow-api-error!` rethrow as-is, so `::skip` survives on the outer ex-data.+ (mt/with-dynamic-fn-redefs [http/request (fn [opts]+ (when (:body opts)+ (reset! captured (json/decode+kw (:body opts))))+ (throw (ex-info "stop" {::skip true :api-error true})))]+ (mt/with-temporary-setting-values [llm-mistral-api-key "mistral-key-test"]+ (let [call! (fn [tracking-opts]+ (reset! captured nil)+ (try+ (run! identity (self/call-llm "mistral/mistral-medium-3-5" nil [] {} tracking-opts))+ (catch Exception e+ (when-not (::skip (ex-data e))+ (throw e)))))]+ (call! {:tag "agent" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"})+ (is (= "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48" (:prompt_cache_key @captured)))+ (testing "no prompt_cache_key without a :session-id"+ (call! {:tag "agent"})+ (is (not (contains? @captured :prompt_cache_key)))))))))))(deftest call-llm-structured-prompt-cache-key-test- (testing "call-llm-structured forwards :session-id as the prompt_cache_key, same contract as call-llm"- (let [captured (atom nil)]- ;; `:api-error true` makes `rethrow-api-error!` rethrow as-is, so `::skip` survives on the outer ex-data.- (mt/with-dynamic-fn-redefs [http/request (fn [opts]- (when (:body opts)- (reset! captured (json/decode+kw (:body opts))))- (throw (ex-info "stop" {::skip true :api-error true})))]- (mt/with-temporary-setting-values [llm-mistral-api-key "mistral-key-test"]- (let [call! (fn [tracking-opts]- (reset! captured nil)- (try- (self/call-llm-structured "mistral/mistral-medium-3-5"- [{:role "user" :content "hi"}]- {:type "object" :properties {:title {:type "string"}}}- nil- 128- tracking-opts)- (catch Exception e- (when-not (::skip (ex-data e))- (throw e)))))]- (call! {:tag "conversation-title" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"})- (is (= "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48" (:prompt_cache_key @captured)))- (testing "no prompt_cache_key without a :session-id"- (call! {:tag "example-question-generation"})- (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 the adapters whose provider accepts it (mistral, moonshot) forward it to the wire"- (let [captured (atom nil)]- (mt/with-premium-features #{:metabase-ai-managed}+ (llm.tu/with-default-connections+ (testing "call-llm-structured forwards :session-id as the prompt_cache_key, same contract as call-llm"+ (let [captured (atom nil)];; `:api-error true` makes `rethrow-api-error!` rethrow as-is, so `::skip` survives on the outer ex-data.(mt/with-dynamic-fn-redefs [http/request (fn [opts](when (:body opts)(reset! captured (json/decode+kw (:body opts))))(throw (ex-info "stop" {::skip true :api-error true})))]- (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"- llm-proxy-base-url "http://proxy.example"- llm-openrouter-api-key "sk-or-v1-test-key"- llm-openai-api-key "sk-test-key"- llm-zai-api-key "zai-key.test"- llm-google-oauth-access-token "ya29.test-token"- llm-google-project-id "my-project"]- (doseq [model ["anthropic/test-model"- "metabase/anthropic/claude-sonnet-4-6"- "openrouter/test-model"- "openai/test-model"- "zai/glm-5.2"- "google/google/gemini-3.5-flash"]]- (testing model- (reset! captured nil)- (try- (run! identity (self/call-llm model- nil- []- {}- {:tag "agent" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"}))- (catch Exception e- (when-not (::skip (ex-data e))- (throw e))))- (is (map? @captured))- (is (not (contains? @captured :prompt_cache_key)))- (is (not (contains? @captured :prompt-cache-key)))))))))))+ (mt/with-temporary-setting-values [llm-mistral-api-key "mistral-key-test"]+ (let [call! (fn [tracking-opts]+ (reset! captured nil)+ (try+ (self/call-llm-structured "mistral/mistral-medium-3-5"+ [{:role "user" :content "hi"}]+ {:type "object" :properties {:title {:type "string"}}}+ nil+ 128+ tracking-opts)+ (catch Exception e+ (when-not (::skip (ex-data e))+ (throw e)))))]+ (call! {:tag "conversation-title" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"})+ (is (= "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48" (:prompt_cache_key @captured)))+ (testing "no prompt_cache_key without a :session-id"+ (call! {:tag "example-question-generation"})+ (is (not (contains? @captured :prompt_cache_key)))))))))))++(deftest call-llm-prompt-cache-key-not-leaked-to-other-providers-test+ (llm.tu/with-default-connections+ (testing "call-llm hands :prompt-cache-key to every adapter, but only mistral forwards 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.+ (mt/with-dynamic-fn-redefs [http/request (fn [opts]+ (when (:body opts)+ (reset! captured (json/decode+kw (:body opts))))+ (throw (ex-info "stop" {::skip true :api-error true})))]+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"+ llm-proxy-base-url "http://proxy.example"+ llm-openrouter-api-key "sk-or-v1-test-key"+ llm-openai-api-key "sk-test-key"+ llm-zai-api-key "zai-key.test"]+ (doseq [model ["anthropic/test-model"+ "metabase/anthropic/claude-sonnet-4-6"+ "openrouter/test-model"+ "openai/test-model"+ "zai/glm-5.2"]]+ (testing model+ (reset! captured nil)+ (try+ (run! identity (self/call-llm model+ nil+ []+ {}+ {:tag "agent" :session-id "d34d4c93-a5cc-4d5e-b0a6-6b8f89525b48"}))+ (catch Exception e+ (when-not (::skip (ex-data e))+ (throw e))))+ (is (map? @captured))+ (is (not (contains? @captured :prompt_cache_key)))+ (is (not (contains? @captured :prompt-cache-key))))))))))));;; utils tests@@ -566,71 +565,6 @@(str "event does not match the wire schema: " (pr-str event))))events))))-(def ^:private provider-stop-reasons- {:claude @#'self.claude/stop-reasons- :openai @#'self.openai/stop-reasons- :chat-completions self.chat-completions/stop-reasons- :mistral @#'self.mistral/stop-reasons- :openrouter @#'openrouter/stop-reasons- :zai @#'self.zai/stop-reasons})--(deftest ^:parallel stop-reason->finish-reason-test- (testing "every provider maps only to legal AI SDK FinishReasons"- (doseq [[provider stop-reasons] provider-stop-reasons]- (testing provider- (is (every? self.core/finish-reasons (vals stop-reasons))))))- (testing "truncation — the value the agent loop keys off"- (are [provider raw] (= "length" (self.core/stop-reason->finish-reason (provider-stop-reasons provider) raw))- :claude "max_tokens"- :openai "max_output_tokens"- :chat-completions "length"- ;; Mistral's own context limit, which has no OpenAI equivalent- :mistral "model_length"))- (testing "content filtering, which each dialect names differently"- (are [provider raw] (= "content-filter" (self.core/stop-reason->finish-reason (provider-stop-reasons provider) raw))- :claude "refusal"- :openai "content_filter"- :chat-completions "content_filter"- :zai "sensitive"))- (testing "upstream failure, which some dialects report as a finish reason rather than an error event"- (are [provider raw] (= "error" (self.core/stop-reason->finish-reason (provider-stop-reasons provider) raw))- :mistral "error"- :openrouter "error"- :zai "network_error"))- (testing "unmapped → \"other\"; nil → nil"- (is (= "other" (self.core/stop-reason->finish-reason @#'self.claude/stop-reasons "something_new")))- ;; a stop reason belonging to another provider is not silently translated- (is (= "other" (self.core/stop-reason->finish-reason @#'self.claude/stop-reasons "max_output_tokens")))- (is (= "other" (self.core/stop-reason->finish-reason self.chat-completions/stop-reasons "sensitive")))- (is (nil? (self.core/stop-reason->finish-reason @#'self.claude/stop-reasons nil)))))--(deftest parts->aisdk-sse-xf-finish-reason-test- (testing "a truncated turn emits finishReason \"length\" and without an error"- (is (= ["text-start" "text-delta" "text-end" "finish"]- (mapv :type- (sse-events [{:type :text :id "t1" :text "partial"}- {:type :usage :model "m" :usage {:promptTokens 1 :completionTokens 2 :totalTokens 3}- :finish-reason "length" :raw-finish-reason "max_tokens"}]))))- (is (=? {:type "finish" :finishReason "length"}- (last (sse-events [{:type :text :id "t1" :text "partial"}- {:type :usage :model "m" :usage {:promptTokens 1 :completionTokens 2 :totalTokens 3}- :finish-reason "length" :raw-finish-reason "max_tokens"}])))))- (testing "an in-turn error part does not override a length finish"- (is (=? {:type "finish" :finishReason "length"}- (last (sse-events [{:type :error :error {:message "tool blew up mid-turn"}}- {:type :usage :model "m" :usage {:promptTokens 1 :completionTokens 2 :totalTokens 3}- :finish-reason "length" :raw-finish-reason "max_tokens"}])))))- (testing "a filtered turn emits finishReason \"content-filter\""- (is (=? {:type "finish" :finishReason "content-filter"}- (last (sse-events [{:type :text :id "t1" :text "I can't help with that."}- {:type :usage :model "m" :usage {:promptTokens 1 :completionTokens 2 :totalTokens 3}- :finish-reason "content-filter" :raw-finish-reason "refusal"}])))))- (testing "an in-turn error part outranks a content-filter finish — the errored turn is rewound"- (is (=? {:type "finish" :finishReason "error"}- (last (sse-events [{:type :error :error {:message "tool blew up mid-turn"}}- {:type :usage :model "m" :usage {:promptTokens 1 :completionTokens 2 :totalTokens 3}- :finish-reason "content-filter" :raw-finish-reason "refusal"}]))))))-(deftest parts->aisdk-sse-xf-lifecycle-test(testing "first :start opens the message and a step; later :start is a step boundary; completion closes"(is (= [["start" "msg-1"] ["start-step" nil]@@ -965,250 +899,256 @@{:type :tool-input-available :toolCallId "c1" :toolName "json"}]))))(deftest call-llm-structured-rejects-malformed-json-test- (testing "malformed tool-call JSON is rejected as an error, not returned as a bogus result"- (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)- openrouter/openrouter (constantly (malformed-tool-input-response))]- (mt/with-log-level [metabase.metabot.self.core :fatal]- (let [e (try- (self/call-llm-structured "openrouter/test-model"- [{:role "user" :content "test"}]- {:type "object" :properties {:answer {:type "string"}}}- 0.3 1024 {:tag "metabot_agent"})- (catch clojure.lang.ExceptionInfo e e))]- (is (instance? clojure.lang.ExceptionInfo e)- "malformed JSON must throw, not return the {:_raw_arguments ...} sentinel as a result")- (is (= "structured-output-invalid" (:error-code (ex-data e)))))))))+ (llm.tu/with-default-connections+ (testing "malformed tool-call JSON is rejected as an error, not returned as a bogus result"+ (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)+ openrouter/openrouter (constantly (malformed-tool-input-response))]+ (mt/with-log-level [metabase.metabot.self.core :fatal]+ (let [e (try+ (self/call-llm-structured "openrouter/test-model"+ [{:role "user" :content "test"}]+ {:type "object" :properties {:answer {:type "string"}}}+ 0.3 1024 {:tag "metabot_agent"})+ (catch clojure.lang.ExceptionInfo e e))]+ (is (instance? clojure.lang.ExceptionInfo e)+ "malformed JSON must throw, not return the {:_raw_arguments ...} sentinel as a result")+ (is (= "structured-output-invalid" (:error-code (ex-data e))))))))))(deftest call-llm-structured-surfaces-provider-error-test- (testing "a provider mid-stream :error part surfaces its message, not a generic 'no tool call' error"- (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)- openrouter/openrouter (constantly- (test-util/mock-llm-response- [{:type :error :errorText "content policy violation"}]))]- (mt/with-log-level [metabase.metabot.self :fatal]- (let [e (try- (self/call-llm-structured "openrouter/test-model"- [{:role "user" :content "test"}]- {:type "object" :properties {:answer {:type "string"}}}- 0.3 1024 {:tag "metabot_agent"})- (catch clojure.lang.ExceptionInfo e e))]- (is (instance? clojure.lang.ExceptionInfo e))- (is (re-find #"content policy violation" (ex-message e))- "the provider error message is surfaced, not hidden behind 'no tool call'")- (is (= "llm-stream-error" (:error-code (ex-data e)))))))))+ (llm.tu/with-default-connections+ (testing "a provider mid-stream :error part surfaces its message, not a generic 'no tool call' error"+ (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)+ openrouter/openrouter (constantly+ (test-util/mock-llm-response+ [{:type :error :errorText "content policy violation"}]))]+ (mt/with-log-level [metabase.metabot.self :fatal]+ (let [e (try+ (self/call-llm-structured "openrouter/test-model"+ [{:role "user" :content "test"}]+ {:type "object" :properties {:answer {:type "string"}}}+ 0.3 1024 {:tag "metabot_agent"})+ (catch clojure.lang.ExceptionInfo e e))]+ (is (instance? clojure.lang.ExceptionInfo e))+ (is (re-find #"content policy violation" (ex-message e))+ "the provider error message is surfaced, not hidden behind 'no tool call'")+ (is (= "llm-stream-error" (:error-code (ex-data e))))))))))(deftest call-llm-does-not-replay-after-partial-emission-test- (testing "a retryable failure AFTER parts were emitted does not replay the stream (no duplicate output / re-run tools)"- (mt/with-dynamic-fn-redefs- [self/retry-delay-ms (constantly 0)- openrouter/openrouter (constantly- (reify clojure.lang.IReduceInit- (reduce [_ rf init]- ;; emit one chunk, then fail mid-stream with an otherwise-retryable error- (let [_acc (rf init {:type :start :messageId "m1"})]- (throw (ex-info "mid-stream boom" {:status 500}))))))]- (mt/with-log-level [metabase.metabot.self :fatal]- (let [seen (atom [])- collect-rf (fn ([acc] acc) ([acc x] (swap! seen conj x) acc))]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"mid-stream boom"- (reduce collect-rf nil (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))))- (is (= 1 (count (filter #(= :start (:type %)) @seen)))- "the :start part reaches the consumer exactly once, not once per retry attempt"))))))--(deftest call-llm-still-retries-before-any-emission-test- (testing "a retryable failure BEFORE any part is emitted is still retried (clean replay)"- (let [calls (atom 0)]+ (llm.tu/with-default-connections+ (testing "a retryable failure AFTER parts were emitted does not replay the stream (no duplicate output / re-run tools)"(mt/with-dynamic-fn-redefs[self/retry-delay-ms (constantly 0)- openrouter/openrouter (fn [_opts]- (reify clojure.lang.IReduceInit- (reduce [_ rf init]- (if (< (swap! calls inc) 3)- (throw (ex-info "rate limited" {:status 429}))- (reduce rf init (test-util/mock-llm-response [{:type :start :id "m1"}]))))))]+ openrouter/openrouter (constantly+ (reify clojure.lang.IReduceInit+ (reduce [_ rf init]+ ;; emit one chunk, then fail mid-stream with an otherwise-retryable error+ (let [_acc (rf init {:type :start :messageId "m1"})]+ (throw (ex-info "mid-stream boom" {:status 500}))))))](mt/with-log-level [metabase.metabot.self :fatal]- (let [seen (atom [])]- (reduce (fn [acc x] (swap! seen conj x) acc) nil- (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))- (is (= 3 @calls) "retries until the pre-emission failures clear")- (is (= 1 (count (filter #(= :start (:type %)) @seen))))))))))+ (let [seen (atom [])+ collect-rf (fn ([acc] acc) ([acc x] (swap! seen conj x) acc))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"mid-stream boom"+ (reduce collect-rf nil (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))))+ (is (= 1 (count (filter #(= :start (:type %)) @seen)))+ "the :start part reaches the consumer exactly once, not once per retry attempt")))))))++(deftest call-llm-still-retries-before-any-emission-test+ (llm.tu/with-default-connections+ (testing "a retryable failure BEFORE any part is emitted is still retried (clean replay)"+ (let [calls (atom 0)]+ (mt/with-dynamic-fn-redefs+ [self/retry-delay-ms (constantly 0)+ openrouter/openrouter (fn [_opts]+ (reify clojure.lang.IReduceInit+ (reduce [_ rf init]+ (if (< (swap! calls inc) 3)+ (throw (ex-info "rate limited" {:status 429}))+ (reduce rf init (test-util/mock-llm-response [{:type :start :id "m1"}]))))))]+ (mt/with-log-level [metabase.metabot.self :fatal]+ (let [seen (atom [])]+ (reduce (fn [acc x] (swap! seen conj x) acc) nil+ (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))+ (is (= 3 @calls) "retries until the pre-emission failures clear")+ (is (= 1 (count (filter #(= :start (:type %)) @seen)))))))))));;; ===================== Prometheus Metrics Tests =====================(deftest call-llm-prometheus-test- (mt/with-prometheus-system! [_ system]- (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)]- (let [labels {:model "openrouter/test-model" :source "metabot_agent"}]- (testing "increments llm-requests and observes duration on success"- (mt/with-dynamic-fn-redefs [openrouter/openrouter (constantly (test-util/mock-llm-response [{:type :start :id "m1"}]))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- ;; mt/with-prometheus-system! is slow, so clear! metrics between tests rather than creating a fresh system- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-retries on transient failures, no errors on eventual success"- (let [calls (atom 0)]- (mt/with-log-level [metabase.metabot.self :fatal]- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (fn [_opts]- (reify clojure.lang.IReduceInit- (reduce [_ rf init]- (if (< (swap! calls inc) 3)- (throw (ex-info "rate limited" {:status 429}))- (reduce rf init (test-util/mock-llm-response [{:type :start :id "m1"}]))))))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))))- (is (== 3 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 2 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (llm.tu/with-default-connections+ (mt/with-prometheus-system! [_ system]+ (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)]+ (let [labels {:model "openrouter/test-model" :source "metabot_agent"}]+ (testing "increments llm-requests and observes duration on success"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter (constantly (test-util/mock-llm-response [{:type :start :id "m1"}]))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))(is (== 0 (mt/metric-value system :metabase-metabot/llm-errors(assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels))))))- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-retries)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-errors on non-retryable failure, no retries"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (fn [_opts]- (reify clojure.lang.IReduceInit- (reduce [_ _rf _init]- (throw (ex-info "unauthorized" {:status 401})))))]- (is (thrown? Exception (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-errors)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response [{:type :error :errorText "content policy violation"}]))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "llm-sse-error")))))- (testing "reports token usage metrics on :usage parts"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start- :id "m1"}- {:type :usage- :usage {:promptTokens 100 :completionTokens 25}- :model "test-model"}]))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))- (is (== 100 (mt/metric-value system :metabase-metabot/llm-input-tokens labels)))- (is (== 25 (mt/metric-value system :metabase-metabot/llm-output-tokens labels)))- (is (== 125 (:sum (mt/metric-value system :metabase-metabot/llm-tokens-per-call labels)))))- (analytics/clear! :metabase-metabot/llm-input-tokens)- (analytics/clear! :metabase-metabot/llm-output-tokens)- (analytics/clear! :metabase-metabot/llm-cache-creation-tokens)- (analytics/clear! :metabase-metabot/llm-cache-read-tokens)- (testing "increments cache token counters when the :usage part carries cache fields"- ;; :promptTokens is the pre-summed total input (40 fresh + 300 cache_creation + 1200 cache_read = 1540).- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start :id "m1"}- {:type :usage- :usage {:promptTokens 1540- :completionTokens 10- :cacheCreationTokens 300- :cacheReadTokens 1200}- :model "test-model"}]))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))- (is (== 300 (mt/metric-value system :metabase-metabot/llm-cache-creation-tokens labels)))- (is (== 1200 (mt/metric-value system :metabase-metabot/llm-cache-read-tokens labels))))- (analytics/clear! :metabase-metabot/llm-input-tokens)- (analytics/clear! :metabase-metabot/llm-output-tokens)- (analytics/clear! :metabase-metabot/llm-cache-creation-tokens)- (analytics/clear! :metabase-metabot/llm-cache-read-tokens)- (testing "does not increment cache counters when cache fields are absent or zero"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start :id "m1"}- {:type :usage- :usage {:promptTokens 10 :completionTokens 5}- :model "test-model"}]))]- (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))- (is (zero? (mt/metric-value system :metabase-metabot/llm-cache-creation-tokens labels)))- (is (zero? (mt/metric-value system :metabase-metabot/llm-cache-read-tokens labels))))))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))+ ;; mt/with-prometheus-system! is slow, so clear! metrics between tests rather than creating a fresh system+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-retries on transient failures, no errors on eventual success"+ (let [calls (atom 0)]+ (mt/with-log-level [metabase.metabot.self :fatal]+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (fn [_opts]+ (reify clojure.lang.IReduceInit+ (reduce [_ rf init]+ (if (< (swap! calls inc) 3)+ (throw (ex-info "rate limited" {:status 429}))+ (reduce rf init (test-util/mock-llm-response [{:type :start :id "m1"}]))))))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"}))))+ (is (== 3 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 2 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "ExceptionInfo"))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels))))))+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-retries)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-errors on non-retryable failure, no retries"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (fn [_opts]+ (reify clojure.lang.IReduceInit+ (reduce [_ _rf _init]+ (throw (ex-info "unauthorized" {:status 401})))))]+ (is (thrown? Exception (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "ExceptionInfo"))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-errors)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response [{:type :error :errorText "content policy violation"}]))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "llm-sse-error")))))+ (testing "reports token usage metrics on :usage parts"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start+ :id "m1"}+ {:type :usage+ :usage {:promptTokens 100 :completionTokens 25}+ :model "test-model"}]))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))+ (is (== 100 (mt/metric-value system :metabase-metabot/llm-input-tokens labels)))+ (is (== 25 (mt/metric-value system :metabase-metabot/llm-output-tokens labels)))+ (is (== 125 (:sum (mt/metric-value system :metabase-metabot/llm-tokens-per-call labels)))))+ (analytics/clear! :metabase-metabot/llm-input-tokens)+ (analytics/clear! :metabase-metabot/llm-output-tokens)+ (analytics/clear! :metabase-metabot/llm-cache-creation-tokens)+ (analytics/clear! :metabase-metabot/llm-cache-read-tokens)+ (testing "increments cache token counters when the :usage part carries cache fields"+ ;; :promptTokens is the pre-summed total input (40 fresh + 300 cache_creation + 1200 cache_read = 1540).+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start :id "m1"}+ {:type :usage+ :usage {:promptTokens 1540+ :completionTokens 10+ :cacheCreationTokens 300+ :cacheReadTokens 1200}+ :model "test-model"}]))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))+ (is (== 300 (mt/metric-value system :metabase-metabot/llm-cache-creation-tokens labels)))+ (is (== 1200 (mt/metric-value system :metabase-metabot/llm-cache-read-tokens labels))))+ (analytics/clear! :metabase-metabot/llm-input-tokens)+ (analytics/clear! :metabase-metabot/llm-output-tokens)+ (analytics/clear! :metabase-metabot/llm-cache-creation-tokens)+ (analytics/clear! :metabase-metabot/llm-cache-read-tokens)+ (testing "does not increment cache counters when cache fields are absent or zero"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start :id "m1"}+ {:type :usage+ :usage {:promptTokens 10 :completionTokens 5}+ :model "test-model"}]))]+ (run! identity (self/call-llm "openrouter/test-model" nil [] {} {:tag "metabot_agent"})))+ (is (zero? (mt/metric-value system :metabase-metabot/llm-cache-creation-tokens labels)))+ (is (zero? (mt/metric-value system :metabase-metabot/llm-cache-read-tokens labels)))))))))(deftest call-llm-structured-prometheus-test- (mt/with-prometheus-system! [_ system]- (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)]- (let [labels {:model "openrouter/test-model" :source "metabot_agent"}- success-mock (test-util/mock-llm-response- [{:type :start :id "m1"}- {:type :tool-input :id "call-1" :function "json"- :arguments {:answer "42"}}])- call-structured! #(self/call-llm-structured- "openrouter/test-model"- [{:role "user" :content "test"}]- {:type "object" :properties {:answer {:type "string"}}}- 0.3 1024 {:tag "metabot_agent"})]- (testing "increments llm-requests and observes duration on success"- (mt/with-dynamic-fn-redefs [openrouter/openrouter (constantly success-mock)]- (call-structured!))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-retries on transient failures, no errors on eventual success"- (let [calls (atom 0)]- (mt/with-log-level [metabase.metabot.self :fatal]- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (fn [_opts]- (if (< (swap! calls inc) 3)- (throw (ex-info "rate limited" {:status 429}))- success-mock))]- (call-structured!))))- (is (== 3 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 2 (mt/metric-value system :metabase-metabot/llm-retries labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-retries)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-errors on non-retryable failure, no retries"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (fn [_opts] (throw (ex-info "unauthorized" {:status 401})))]- (is (thrown? Exception (call-structured!))))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "ExceptionInfo"))))- (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (analytics/clear! :metabase-metabot/llm-requests)- (analytics/clear! :metabase-metabot/llm-errors)- (analytics/clear! :metabase-metabot/llm-duration-ms)- (testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :error :errorText "content policy violation"}]))]- (is (thrown? Exception (call-structured!))))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))- (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors- (assoc labels :error-type "llm-sse-error")))))- (testing "reports token usage metrics on :usage parts"- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start :id "m1"}- {:type :tool-input :id "call-1" :function "json"- :arguments {:answer "42"}}- {:type :usage :usage {:promptTokens 100 :completionTokens 25}- :model "test-model"}]))]- (call-structured!))- (is (== 100 (mt/metric-value system :metabase-metabot/llm-input-tokens labels)))- (is (== 25 (mt/metric-value system :metabase-metabot/llm-output-tokens labels)))- (is (== 125 (:sum (mt/metric-value system :metabase-metabot/llm-tokens-per-call labels)))))))))+ (llm.tu/with-default-connections+ (mt/with-prometheus-system! [_ system]+ (mt/with-dynamic-fn-redefs [self/retry-delay-ms (constantly 0)]+ (let [labels {:model "openrouter/test-model" :source "metabot_agent"}+ success-mock (test-util/mock-llm-response+ [{:type :start :id "m1"}+ {:type :tool-input :id "call-1" :function "json"+ :arguments {:answer "42"}}])+ call-structured! #(self/call-llm-structured+ "openrouter/test-model"+ [{:role "user" :content "test"}]+ {:type "object" :properties {:answer {:type "string"}}}+ 0.3 1024 {:tag "metabot_agent"})]+ (testing "increments llm-requests and observes duration on success"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter (constantly success-mock)]+ (call-structured!))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "ExceptionInfo"))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-retries on transient failures, no errors on eventual success"+ (let [calls (atom 0)]+ (mt/with-log-level [metabase.metabot.self :fatal]+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (fn [_opts]+ (if (< (swap! calls inc) 3)+ (throw (ex-info "rate limited" {:status 429}))+ success-mock))]+ (call-structured!))))+ (is (== 3 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 2 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "ExceptionInfo"))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-retries)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-errors on non-retryable failure, no retries"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (fn [_opts] (throw (ex-info "unauthorized" {:status 401})))]+ (is (thrown? Exception (call-structured!))))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 0 (mt/metric-value system :metabase-metabot/llm-retries labels)))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "ExceptionInfo"))))+ (is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-errors)+ (analytics/clear! :metabase-metabot/llm-duration-ms)+ (testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :error :errorText "content policy violation"}]))]+ (is (thrown? Exception (call-structured!))))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-requests labels)))+ (is (== 1 (mt/metric-value system :metabase-metabot/llm-errors+ (assoc labels :error-type "llm-sse-error")))))+ (testing "reports token usage metrics on :usage parts"+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start :id "m1"}+ {:type :tool-input :id "call-1" :function "json"+ :arguments {:answer "42"}}+ {:type :usage :usage {:promptTokens 100 :completionTokens 25}+ :model "test-model"}]))]+ (call-structured!))+ (is (== 100 (mt/metric-value system :metabase-metabot/llm-input-tokens labels)))+ (is (== 25 (mt/metric-value system :metabase-metabot/llm-output-tokens labels)))+ (is (== 125 (:sum (mt/metric-value system :metabase-metabot/llm-tokens-per-call labels))))))))));;; ===================== Snowplow Analytics Tests =====================@@ -1219,94 +1159,97 @@:tag "test-tag"})(deftest call-llm-snowplow-test- (testing "fires :snowplow/token_usage and :snowplow/ai_service_event for call-llm with a tool call"- (let [rasta-id (mt/user->id :rasta)]- ;; The adapter pre-sums input + cache_creation + cache_read into :promptTokens,- ;; so the mock supplies the already-summed value (950 = 100 fresh + 50 cache_creation + 800 cache_read).- ;; total_tokens reverts to prompt + completion = 950 + 20 = 970.- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start :id "msg-1"}- {:type :tool-input :id "call-1" :function "get-time"- :arguments {:tz "UTC"}}- {:type :usage :usage {:promptTokens 950- :completionTokens 20- :cacheCreationTokens 50- :cacheReadTokens 800}- :model "test-model" :id "msg-1"}]))]- (mt/with-current-user rasta-id- (snowplow-test/with-fake-snowplow-collector- (run! identity (self/call-llm "openrouter/test-model" nil [] test-util/TOOLS snowplow-tracking-opts))- (let [events (snowplow-test/pop-event-data-and-user-id!)- token-events (filter #(contains? (:data %) "total_tokens") events)- tool-events (filter #(= "agent_used_tool" (get-in % [:data "event"])) events)]- (is (=? [{:user-id (str rasta-id)- :data {"model_id" "openrouter/test-model"- "total_tokens" 970- "prompt_tokens" 950- "completion_tokens" 20- "cache_creation_tokens" 50- "cache_read_tokens" 800- "estimated_costs_usd" 0.0- "duration_ms" nat-int?- "source" "metabot_agent"- "tag" "test-tag"- "session_id" "00000000-0000-0000-0000-000000000002"}}]- token-events))- (is (=? [{:user-id (str rasta-id)- :data {"event" "agent_used_tool"- "source" "metabot_agent"- "result" "success"- "duration_ms" nat-int?- "session_id" "00000000-0000-0000-0000-000000000002"- "event_details" {"tool_name" "get-time"}}}]- tool-events)))))))))+ (llm.tu/with-default-connections+ (testing "fires :snowplow/token_usage and :snowplow/ai_service_event for call-llm with a tool call"+ (let [rasta-id (mt/user->id :rasta)]+ ;; The adapter pre-sums input + cache_creation + cache_read into :promptTokens,+ ;; so the mock supplies the already-summed value (950 = 100 fresh + 50 cache_creation + 800 cache_read).+ ;; total_tokens reverts to prompt + completion = 950 + 20 = 970.+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start :id "msg-1"}+ {:type :tool-input :id "call-1" :function "get-time"+ :arguments {:tz "UTC"}}+ {:type :usage :usage {:promptTokens 950+ :completionTokens 20+ :cacheCreationTokens 50+ :cacheReadTokens 800}+ :model "test-model" :id "msg-1"}]))]+ (mt/with-current-user rasta-id+ (snowplow-test/with-fake-snowplow-collector+ (run! identity (self/call-llm "openrouter/test-model" nil [] test-util/TOOLS snowplow-tracking-opts))+ (let [events (snowplow-test/pop-event-data-and-user-id!)+ token-events (filter #(contains? (:data %) "total_tokens") events)+ tool-events (filter #(= "agent_used_tool" (get-in % [:data "event"])) events)]+ (is (=? [{:user-id (str rasta-id)+ :data {"model_id" "openrouter/test-model"+ "total_tokens" 970+ "prompt_tokens" 950+ "completion_tokens" 20+ "cache_creation_tokens" 50+ "cache_read_tokens" 800+ "estimated_costs_usd" 0.0+ "duration_ms" nat-int?+ "source" "metabot_agent"+ "tag" "test-tag"+ "session_id" "00000000-0000-0000-0000-000000000002"}}]+ token-events))+ (is (=? [{:user-id (str rasta-id)+ :data {"event" "agent_used_tool"+ "source" "metabot_agent"+ "result" "success"+ "duration_ms" nat-int?+ "session_id" "00000000-0000-0000-0000-000000000002"+ "event_details" {"tool_name" "get-time"}}}]+ tool-events))))))))))(deftest call-llm-structured-snowplow-test- (testing "fires :snowplow/token_usage event for call-llm-structured"- (let [rasta-id (mt/user->id :rasta)]- (mt/with-dynamic-fn-redefs [openrouter/openrouter- (constantly (test-util/mock-llm-response- [{:type :start :id "msg-1"}- {:type :tool-input :id "call-1" :function "json"- :arguments {:answer "42"}}- {:type :usage :usage {:promptTokens 50 :completionTokens 10}- :model "test-model" :id "msg-1"}]))]- (mt/with-current-user rasta-id- (snowplow-test/with-fake-snowplow-collector- (self/call-llm-structured "openrouter/test-model"- [{:role "user" :content "test"}]- {:type "object" :properties {:answer {:type "string"}}}- 0.3- 1024- snowplow-tracking-opts)- (let [events (snowplow-test/pop-event-data-and-user-id!)- token-events (filter #(contains? (:data %) "total_tokens") events)]- (is (=? [{:user-id (str rasta-id)- :data {"model_id" "openrouter/test-model"- "total_tokens" 60- "prompt_tokens" 50- "completion_tokens" 10- "estimated_costs_usd" 0.0- "duration_ms" nat-int?- "source" "metabot_agent"- "tag" "test-tag"- "session_id" "00000000-0000-0000-0000-000000000002"}}]- token-events)))))))))+ (llm.tu/with-default-connections+ (testing "fires :snowplow/token_usage event for call-llm-structured"+ (let [rasta-id (mt/user->id :rasta)]+ (mt/with-dynamic-fn-redefs [openrouter/openrouter+ (constantly (test-util/mock-llm-response+ [{:type :start :id "msg-1"}+ {:type :tool-input :id "call-1" :function "json"+ :arguments {:answer "42"}}+ {:type :usage :usage {:promptTokens 50 :completionTokens 10}+ :model "test-model" :id "msg-1"}]))]+ (mt/with-current-user rasta-id+ (snowplow-test/with-fake-snowplow-collector+ (self/call-llm-structured "openrouter/test-model"+ [{:role "user" :content "test"}]+ {:type "object" :properties {:answer {:type "string"}}}+ 0.3+ 1024+ snowplow-tracking-opts)+ (let [events (snowplow-test/pop-event-data-and-user-id!)+ token-events (filter #(contains? (:data %) "total_tokens") events)]+ (is (=? [{:user-id (str rasta-id)+ :data {"model_id" "openrouter/test-model"+ "total_tokens" 60+ "prompt_tokens" 50+ "completion_tokens" 10+ "estimated_costs_usd" 0.0+ "duration_ms" nat-int?+ "source" "metabot_agent"+ "tag" "test-tag"+ "session_id" "00000000-0000-0000-0000-000000000002"}}]+ token-events))))))))));;; ----- gating: usage-limit + permission checks in call-llm-structured-with-trace -----;;; (UXW-4126) The structured-with-trace path enforces usage limits unconditionally and;;; an optional `:required-permission` against the current user's metabot perms.(defn- try-structured-call [opts]- (try- (self/call-llm-structured-with-trace- "anthropic/claude-haiku-4-5"- [{:role "user" :content "hi"}]- {:type "object"}- 0.0 100- opts)- (catch clojure.lang.ExceptionInfo e e)))+ (llm.tu/with-default-connections+ (try+ (self/call-llm-structured-with-trace+ "anthropic/claude-haiku-4-5"+ [{:role "user" :content "hi"}]+ {:type "object"}+ 0.0 100+ opts)+ (catch clojure.lang.ExceptionInfo e e))))(deftest call-llm-structured-with-trace-throws-on-usage-limit-test(testing "When check-usage-limits! returns a message, the call throws an ex-info withdiff --git a/test/metabase/metabot/settings_test.clj b/test/metabase/metabot/settings_test.cljindex 90674a43b7e..ba430346016 100644--- a/test/metabase/metabot/settings_test.clj+++ b/test/metabase/metabot/settings_test.clj@@ -1,7 +1,7 @@(ns metabase.metabot.settings-test(:require[clojure.test :refer [deftest is testing use-fixtures]]- [metabase.llm.settings :as llm.settings]+ [metabase.metabot.self :as metabot.self][metabase.metabot.settings :as metabot.settings][metabase.settings.core :as setting][metabase.test :as mt]@@ -9,6 +9,33 @@(use-fixtures :once (fixtures/initialize :db))+(defn- connection+ ([conn-key type]+ (connection conn-key type {}))+ ([conn-key type config]+ {:key conn-key :type type :name conn-key :config config}))++(def ^:private configured-anthropic+ (connection "anthropic" "anthropic" {:api-key "sk-ant-test"}))++(defn- do-with-connections!+ [conns thunk]+ (mt/with-temporary-setting-values [llm-providers conns]+ (thunk)))++(defmacro ^:private with-connections+ [conns & body]+ `(do-with-connections! ~conns (fn [] ~@body)))++(defn- do-with-selected-model!+ [model-ref thunk]+ (mt/with-temporary-raw-setting-values [llm-metabot-provider model-ref]+ (thunk)))++(defmacro ^:private with-selected-model+ [model-ref & body]+ `(do-with-selected-model! ~model-ref (fn [] ~@body)))+(deftest customization-settings-require-ai-controls-test(testing "without :ai-controls feature, customization settings return defaults and reject writes"(mt/with-premium-features #{}@@ -77,51 +104,96 @@(is (boolean? (metabot.settings/embedded-metabot-enabled?)))(is (string? (metabot.settings/llm-metabot-provider))))))-(deftest metabot-configured-with-metabase-provider-and-proxy-url-test- (testing "returns true when metabase-proxied provider and proxy URL is set"+(deftest metabot-configured-with-managed-connection-and-proxy-url-test+ (testing "returns true when the managed connection is selected and the LLM proxy is configured"(mt/with-premium-features #{:metabase-ai-managed}- (mt/with-temporary-setting-values [llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"- llm-proxy-base-url "https://proxy.example.com"]- (is (true? (metabot.settings/llm-metabot-configured?)))))))+ (with-connections [(connection "metabase" "metabase")]+ (mt/with-temporary-setting-values [llm-proxy-base-url "https://proxy.example.com"]+ (with-selected-model "metabase/anthropic/claude-sonnet-4-6"+ (is (true? (metabot.settings/llm-metabot-configured?)))))))))-(deftest metabot-configured-with-metabase-provider-no-proxy-url-test- (testing "returns false when metabase-proxied provider and no proxy URL"+(deftest metabot-configured-with-managed-connection-no-proxy-url-test+ (testing "returns false when the managed connection is selected but there is no LLM proxy"(mt/with-premium-features #{:metabase-ai-managed}- (mt/with-temporary-setting-values [llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"- llm-proxy-base-url nil]- (is (false? (metabot.settings/llm-metabot-configured?)))))))+ (with-connections [(connection "metabase" "metabase")]+ (mt/with-temporary-setting-values [llm-proxy-base-url nil]+ (with-selected-model "metabase/anthropic/claude-sonnet-4-6"+ (is (false? (metabot.settings/llm-metabot-configured?)))))))))++(deftest metabot-configured-with-direct-connection-and-api-key-test+ (testing "returns true when the selected connection carries the credentials its type needs"+ (with-connections [configured-anthropic]+ (with-selected-model "anthropic/claude-sonnet-4-6"+ (is (true? (metabot.settings/llm-metabot-configured?)))))))-(deftest metabot-configured-with-direct-provider-and-api-key-test- (testing "returns true when direct provider has API key set"- (mt/with-temporary-setting-values [llm-metabot-provider "anthropic/claude-sonnet-4-6"- llm-anthropic-api-key "sk-ant-test"]- (is (true? (metabot.settings/llm-metabot-configured?))))))+(deftest metabot-configured-with-direct-connection-no-api-key-test+ (testing "returns false when the selected connection exists but has no credentials"+ (with-connections [(connection "anthropic" "anthropic")]+ (with-selected-model "anthropic/claude-sonnet-4-6"+ (is (false? (metabot.settings/llm-metabot-configured?)))))))-(deftest metabot-configured-with-direct-provider-no-api-key-test- (testing "returns false when direct provider has no API key"- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]- (mt/with-temporary-setting-values [llm-metabot-provider "anthropic/claude-sonnet-4-6"]+(deftest metabot-configured-with-unknown-connection-test+ (testing "returns false when the selected connection no longer exists"+ (with-connections [configured-anthropic]+ (with-selected-model "deleted-connection/some-model"(is (false? (metabot.settings/llm-metabot-configured?)))))))-(deftest metabot-configured-proxy-url-not-fallback-for-direct-provider-test- (testing "proxy URL alone does not make a direct provider configured"+(deftest metabot-configured-proxy-url-not-fallback-for-direct-connection-test+ (testing "the LLM proxy alone does not make a direct connection configured"(mt/with-premium-features #{:metabase-ai-managed}- (mt/with-dynamic-fn-redefs [llm.settings/llm-anthropic-api-key (constantly nil)]- (mt/with-temporary-setting-values [llm-metabot-provider "anthropic/claude-sonnet-4-6"- llm-proxy-base-url "https://proxy.example.com"]- (is (false? (metabot.settings/llm-metabot-configured?))))))))+ (with-connections [(connection "anthropic" "anthropic")]+ (mt/with-temporary-setting-values [llm-proxy-base-url "https://proxy.example.com"]+ (with-selected-model "anthropic/claude-sonnet-4-6"+ (is (false? (metabot.settings/llm-metabot-configured?)))))))))++(deftest metabot-configured-with-bedrock-credentials-test+ (testing "returns true when bedrock has both the access key ID and secret access key"+ (with-connections [(connection "bedrock" "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"})]+ (with-selected-model "bedrock/anthropic.claude-haiku-4-5"+ (is (true? (metabot.settings/llm-metabot-configured?)))))))++(deftest metabot-configured-with-partial-bedrock-credentials-test+ (testing "returns false when bedrock has an access key ID but no secret access key"+ (with-connections [(connection "bedrock" "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"})]+ (with-selected-model "bedrock/anthropic.claude-haiku-4-5"+ (is (false? (metabot.settings/llm-metabot-configured?)))))))++(deftest metabot-configured-with-azure-credentials-test+ (testing "returns true when azure has the API key, the base URL, and the deployment it serves"+ (with-connections [(connection "azure" "azure" {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"+ :model-family "anthropic"+ :deployment-name "claude-sonnet-4-5"})]+ (with-selected-model "azure/anthropic/claude-sonnet-4-5"+ (is (true? (metabot.settings/llm-metabot-configured?))))))+ (testing (str "returns true without a deployment in the config too: the model reference names it, which is where "+ "an instance configured before the connection list existed carries it")+ (with-connections [(connection "azure" "azure" {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/anthropic"})]+ (with-selected-model "azure/anthropic/claude-sonnet-4-5"+ (is (true? (metabot.settings/llm-metabot-configured?)))))))++(deftest metabot-configured-with-partial-azure-credentials-test+ (testing "returns false when azure has an API key but no base URL"+ (with-connections [(connection "azure" "azure" {:api-key "azure-key"})]+ (with-selected-model "azure/anthropic/claude-sonnet-4-5"+ (is (false? (metabot.settings/llm-metabot-configured?)))))))(deftest metabot-supports-reasoning-test(testing "only anthropic and openai models that stream reasoning report support"- (doseq [[provider-and-model expected]- {"anthropic/claude-sonnet-4-6" true- "anthropic/claude-haiku-4-5" false- "openai/gpt-5.4" true- "openai/gpt-4o" false- "bedrock/anthropic.claude-opus-4-8" false}]- (testing provider-and-model- (mt/with-temporary-setting-values [llm-metabot-provider provider-and-model]- (is (= expected (metabot.settings/llm-metabot-supports-reasoning?))))))))+ (with-connections [(connection "anthropic" "anthropic")+ (connection "openai" "openai")+ (connection "bedrock" "bedrock")]+ (doseq [[model-ref expected]+ {"anthropic/claude-sonnet-4-6" true+ "anthropic/claude-haiku-4-5" false+ "openai/gpt-5.4" true+ "openai/gpt-4o" false+ "bedrock/anthropic.claude-opus-4-8" false}]+ (testing model-ref+ (with-selected-model model-ref+ (is (= expected (metabot.settings/llm-metabot-supports-reasoning?)))))))));;; ------------------------------------------- validate-metabot-provider! Tests -------------------------------------------;; The validator is private; exercise it through the setting setter.@@ -132,121 +204,103 @@clojure.lang.ExceptionInfo #"must be a string"(metabot.settings/llm-metabot-provider! 42)))))-(deftest validate-metabot-provider-rejects-unknown-provider-test- (testing "rejects unknown top-level provider"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Unknown provider"- (metabot.settings/llm-metabot-provider! "foobar/some-model")))))+(deftest validate-metabot-provider-defers-the-unknown-connection-error-test+ (with-connections [configured-anthropic]+ (testing "the setter takes a reference to a connection that does not exist yet, because the setting and the connection it names can land in either order"+ (mt/discard-setting-changes [llm-metabot-provider]+ (metabot.settings/llm-metabot-provider! "foobar/some-model")+ (is (= "foobar/some-model" (metabot.settings/llm-metabot-provider)))))+ (testing "a connection key nothing is configured under reads as unconfigured, and naming it in a request is a 400"+ (with-selected-model "foobar/some-model"+ (is (false? (metabot.settings/llm-metabot-configured?)))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"No LLM provider connection named \"foobar\" is configured"+ (#'metabot.self/parse-provider-model "foobar/some-model")))))+ (testing "a known provider type with no connection on this instance is treated the same way"+ (with-selected-model "openai/gpt-5.4"+ (is (false? (metabot.settings/llm-metabot-configured?)))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"No LLM provider connection named \"openai\" is configured"+ (#'metabot.self/parse-provider-model "openai/gpt-5.4")))))))(deftest validate-metabot-provider-rejects-blank-model-test- (testing "rejects provider-only string with no model"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Model name is required"- (metabot.settings/llm-metabot-provider! "anthropic/")))))--(deftest validate-metabot-provider-rejects-unknown-metabase-managed-provider-test- (testing "rejects unknown inner provider under metabase/ prefix"- (mt/with-premium-features #{:metabase-ai-managed}- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Unsupported provider \"foobar\" for metabase managed AI"- (metabot.settings/llm-metabot-provider! "metabase/foobar/some-model"))))))--(deftest validate-metabot-provider-rejects-direct-only-provider-as-managed-openai-test- (testing "rejects openai 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 \"openai\" for metabase managed AI"- (metabot.settings/llm-metabot-provider! "metabase/openai/gpt-4.1-mini"))))))--(deftest validate-metabot-provider-rejects-direct-only-provider-as-managed-openrouter-test- (testing "rejects openrouter under metabase/ prefix (not in the managed allow-list)"- (mt/with-premium-features #{:metabase-ai-managed}+ (with-connections [configured-anthropic]+ (testing "rejects a connection-only string with no model"(is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Unsupported provider \"openrouter\" for metabase managed AI"- (metabot.settings/llm-metabot-provider! "metabase/openrouter/anthropic/claude-haiku-4-5"))))))--(deftest validate-metabot-provider-rejects-direct-only-provider-as-managed-zai-test- (testing "rejects zai 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 \"zai\" for metabase managed AI"- (metabot.settings/llm-metabot-provider! "metabase/zai/glm-5.2"))))))--(deftest validate-metabot-provider-rejects-direct-only-provider-as-managed-mistral-test- (testing "rejects mistral under metabase/ prefix (not in the managed allow-list)"- (mt/with-premium-features #{:metabase-ai-managed}+ clojure.lang.ExceptionInfo #"Model name is required"+ (metabot.settings/llm-metabot-provider! "anthropic/")))(is (thrown-with-msg?- 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}+ clojure.lang.ExceptionInfo #"Model name is required"+ (metabot.settings/llm-metabot-provider! "anthropic"))))))++(deftest validate-metabot-provider-accepts-configured-connections-test+ (with-connections [configured-anthropic+ (connection "openai" "openai" {:api-key "sk-test"})+ (connection "openrouter" "openrouter" {:api-key "sk-or-v1-test"})+ (connection "bedrock" "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"})]+ (doseq [model-ref ["anthropic/claude-sonnet-4-6"+ "openai/gpt-4.1-mini"+ "openrouter/anthropic/claude-haiku-4-5"+ "bedrock/anthropic.claude-haiku-4-5"]]+ (testing (str "accepts " model-ref)+ (mt/with-temporary-setting-values [llm-metabot-provider model-ref]+ (is (= model-ref (metabot.settings/llm-metabot-provider))))))))++(deftest validate-metabot-provider-accepts-a-second-connection-of-the-same-type-test+ (testing "a model can be selected on a second connection of a type the instance already has"+ (with-connections [configured-anthropic+ (connection "anthropic-2" "anthropic" {:api-key "sk-ant-other"})]+ (mt/with-temporary-setting-values [llm-metabot-provider "anthropic-2/claude-opus-4-1"]+ (is (= "anthropic-2/claude-opus-4-1" (metabot.settings/llm-metabot-provider)))))))++(deftest validate-metabot-provider-azure-model-format-test+ (with-connections [configured-anthropic+ (connection "azure" "azure" {:api-key "azure-key"+ :base-url "https://my-resource.services.ai.azure.com/openai"})]+ (testing "accepts a supported wire family plus a deployment name"+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]+ (is (= "azure/anthropic/claude-sonnet-4-5" (metabot.settings/llm-metabot-provider))))+ (mt/with-temporary-setting-values [llm-metabot-provider "azure/openai/my-gpt-deployment"]+ (is (= "azure/openai/my-gpt-deployment" (metabot.settings/llm-metabot-provider)))))+ (testing "rejects an unsupported wire family"(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-direct-only-provider-as-managed-google-test- (testing "rejects google under metabase/ prefix (not in the managed allow-list)"- (mt/with-premium-features #{:metabase-ai-managed}+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/evilai/some-deployment"))))+ (testing "rejects a missing deployment name"(is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Unsupported provider \"google\" for metabase managed AI"- (metabot.settings/llm-metabot-provider! "metabase/google/google/gemini-3.5-flash"))))))--(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}+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic/")))(is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Unsupported model \"claude-haiku-4-5\" for metabase managed provider \"anthropic\""- (metabot.settings/llm-metabot-provider! "metabase/anthropic/claude-haiku-4-5"))))))--(deftest validate-metabot-provider-rejects-metabase-prefix-without-model-test- (testing "rejects metabase/provider with no model"- (mt/with-premium-features #{:metabase-ai-managed}+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic"))))+ (testing "rejects a deployment name containing a slash, which Azure does not allow"(is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Model name is required"- (metabot.settings/llm-metabot-provider! "metabase/anthropic/"))))))--(deftest validate-metabot-provider-accepts-valid-direct-anthropic-test- (testing "accepts valid direct anthropic provider string"- (mt/with-temporary-setting-values [llm-metabot-provider "anthropic/claude-sonnet-4-6"]- (is (= "anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider))))))--(deftest validate-metabot-provider-accepts-valid-direct-openai-test- (testing "accepts valid direct openai provider string"- (mt/with-temporary-setting-values [llm-metabot-provider "openai/gpt-4.1-mini"]- (is (= "openai/gpt-4.1-mini" (metabot.settings/llm-metabot-provider))))))--(deftest validate-metabot-provider-accepts-valid-direct-openrouter-test- (testing "accepts valid direct openrouter provider string"- (mt/with-temporary-setting-values [llm-metabot-provider "openrouter/anthropic/claude-haiku-4-5"]- (is (= "openrouter/anthropic/claude-haiku-4-5" (metabot.settings/llm-metabot-provider))))))--(deftest validate-metabot-provider-accepts-valid-direct-zai-test- (testing "accepts valid direct zai provider string"- (mt/with-temporary-setting-values [llm-metabot-provider "zai/glm-5.2"]- (is (= "zai/glm-5.2" (metabot.settings/llm-metabot-provider))))))--(deftest validate-metabot-provider-accepts-valid-direct-mistral-test- (testing "accepts valid direct mistral provider string"- (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}- (mt/with-temporary-setting-values [llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"]- (is (= "metabase/anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))))--;;; ------------------------------------------- ai-usage-max-retention-days Tests -------------------------------------------+ clojure.lang.ExceptionInfo #"Invalid Azure model"+ (metabot.settings/llm-metabot-provider! "azure/anthropic/a/b"))))))++(deftest validate-metabot-provider-managed-model-allow-list-test+ (mt/with-premium-features #{:metabase-ai-managed}+ (with-connections [configured-anthropic (connection "metabase" "metabase")]+ (testing "accepts an allow-listed managed wire family and model"+ (mt/with-temporary-setting-values [llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"]+ (is (= "metabase/anthropic/claude-sonnet-4-6" (metabot.settings/llm-metabot-provider)))))+ (testing "rejects a wire family the proxy does not serve"+ (doseq [model-ref ["metabase/foobar/some-model"+ "metabase/openai/gpt-4.1-mini"+ "metabase/openrouter/anthropic/claude-haiku-4-5"]]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"for Metabase managed AI"+ (metabot.settings/llm-metabot-provider! model-ref)))))+ (testing "rejects a model the proxy does not serve"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Unsupported model \"anthropic/claude-haiku-4-5\" for Metabase managed AI"+ (metabot.settings/llm-metabot-provider! "metabase/anthropic/claude-haiku-4-5"))))+ (testing "rejects a managed reference with no model"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Model name is required"+ (metabot.settings/llm-metabot-provider! "metabase/")))))))(deftest ai-usage-max-retention-days-default-test(testing "defaults to 180 days when no env var is set"@@ -274,264 +328,3 @@java.lang.UnsupportedOperationException#"You cannot set ai-usage-max-retention-days"(setting/set! :ai-usage-max-retention-days 30)))))--(deftest validate-metabot-provider-accepts-valid-direct-bedrock-test- (testing "accepts valid direct bedrock provider string"- (mt/with-temporary-setting-values [llm-metabot-provider "bedrock/anthropic.claude-haiku-4-5"]- (is (= "bedrock/anthropic.claude-haiku-4-5" (metabot.settings/llm-metabot-provider))))))--(deftest metabot-configured-with-bedrock-credentials-test- (testing "returns true when bedrock has both the access key ID and secret access key set"- (mt/with-temporary-setting-values [llm-metabot-provider "bedrock/anthropic.claude-haiku-4-5"- llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"]- (is (true? (metabot.settings/llm-metabot-configured?))))))--(deftest metabot-configured-with-partial-bedrock-credentials-test- (testing "returns false when bedrock has an access key ID but no secret access key"- (mt/with-temporary-setting-values [llm-metabot-provider "bedrock/anthropic.claude-haiku-4-5"- llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm-bedrock-secret-access-key nil]- (is (false? (metabot.settings/llm-metabot-configured?))))))--(deftest configured-provider-credentials-zai-test- (testing "returns the api-key map when a zai key is configured, nil when blank or missing"- (mt/with-temporary-setting-values [llm-zai-api-key "zai-key.test"]- (is (= {:api-key "zai-key.test"}- (metabot.settings/configured-provider-credentials "zai"))))- (mt/with-temporary-setting-values [llm-zai-api-key nil]- (is (nil? (metabot.settings/configured-provider-credentials "zai"))))))--(deftest configured-provider-credentials-mistral-test- (testing "returns the api-key map when a mistral key is configured, nil when blank or missing"- (mt/with-temporary-setting-values [llm-mistral-api-key "mistral-key-test"]- (is (= {:api-key "mistral-key-test"}- (metabot.settings/configured-provider-credentials "mistral"))))- (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"- llm-bedrock-secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"- llm-bedrock-session-token nil- llm-bedrock-region "us-east-2"]- (is (= {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"- :session-token nil- :region "us-east-2"}- (metabot.settings/configured-provider-credentials "bedrock"))))))--(deftest configured-provider-credentials-bedrock-partial-credentials-test- (testing "returns nil when bedrock has an access key ID but no secret access key"- (mt/with-temporary-setting-values [llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"- llm-bedrock-secret-access-key nil]- (is (nil? (metabot.settings/configured-provider-credentials "bedrock"))))))--(deftest provider-credentials-complete?-bedrock-test- (testing "bedrock credentials are complete only with both the access key ID and secret access key"- (is (true? (metabot.settings/provider-credentials-complete?- "bedrock"- {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"})))- (is (false? (metabot.settings/provider-credentials-complete?- "bedrock"- {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key ""})))- (is (false? (metabot.settings/provider-credentials-complete?- "bedrock"- {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key nil})))- (is (false? (metabot.settings/provider-credentials-complete?- "bedrock"- {:access-key-id "AKIAIOSFODNN7EXAMPLE"})))- (is (false? (metabot.settings/provider-credentials-complete?- "bedrock"- {:secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"})))- (is (false? (metabot.settings/provider-credentials-complete? "bedrock" nil)))))--(deftest validate-metabot-provider-accepts-valid-direct-azure-test- (testing "accepts azure provider strings with a wire family and deployment name"- (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"]- (is (= "azure/anthropic/claude-sonnet-4-5" (metabot.settings/llm-metabot-provider))))- (mt/with-temporary-setting-values [llm-metabot-provider "azure/openai/my-gpt-deployment"]- (is (= "azure/openai/my-gpt-deployment" (metabot.settings/llm-metabot-provider))))))--(deftest validate-metabot-provider-rejects-invalid-azure-models-test- (testing "rejects an unsupported wire family"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Invalid Azure model"- (metabot.settings/llm-metabot-provider! "azure/gemini/some-deployment"))))- (testing "rejects a missing deployment name"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Invalid Azure model"- (metabot.settings/llm-metabot-provider! "azure/anthropic/")))- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Invalid Azure model"- (metabot.settings/llm-metabot-provider! "azure/anthropic"))))- (testing "rejects a deployment name containing a slash (Azure deployment names cannot contain /)"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo #"Invalid Azure model"- (metabot.settings/llm-metabot-provider! "azure/anthropic/a/b")))))--(deftest metabot-configured-with-azure-credentials-test- (testing "returns true when azure has both the API key and base URL set"- (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"- llm-azure-api-key "azure-key"- llm-azure-api-base-url "https://my-resource.services.ai.azure.com/anthropic"]- (is (true? (metabot.settings/llm-metabot-configured?))))))--(deftest metabot-configured-with-partial-azure-credentials-test- (testing "returns false when azure has an API key but no base URL"- (mt/with-temporary-setting-values [llm-metabot-provider "azure/anthropic/claude-sonnet-4-5"- llm-azure-api-key "azure-key"- llm-azure-api-base-url nil]- (is (false? (metabot.settings/llm-metabot-configured?))))))--(deftest configured-provider-credentials-azure-fully-configured-test- (testing "returns the api-key/base-url credentials map when azure is fully configured"- (mt/with-temporary-setting-values [llm-azure-api-key "azure-key"- llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"]- (is (= {:api-key "azure-key"- :base-url "https://my-resource.services.ai.azure.com/openai"}- (metabot.settings/configured-provider-credentials "azure"))))))--(deftest configured-provider-credentials-azure-partial-credentials-test- (testing "returns nil when azure has a base URL but no API key"- (mt/with-temporary-setting-values [llm-azure-api-key nil- llm-azure-api-base-url "https://my-resource.services.ai.azure.com/openai"]- (is (nil? (metabot.settings/configured-provider-credentials "azure"))))))--(deftest provider-credentials-complete?-azure-test- (testing "azure credentials are complete only with both a non-blank API key and base URL"- (is (true? (metabot.settings/provider-credentials-complete?- "azure"- {:api-key "azure-key" :base-url "https://my-resource.services.ai.azure.com/openai"})))- (is (false? (metabot.settings/provider-credentials-complete? "azure" {:api-key "azure-key"})))- (is (false? (metabot.settings/provider-credentials-complete? "azure" {:api-key "azure-key" :base-url " "})))- (is (false? (metabot.settings/provider-credentials-complete?- "azure"- {:base-url "https://my-resource.services.ai.azure.com/openai"})))- (is (false? (metabot.settings/provider-credentials-complete? "azure" nil)))))--(deftest provider-credentials-complete?-api-key-provider-test- (testing "API-key provider credentials are complete only with a non-blank :api-key"- (is (true? (metabot.settings/provider-credentials-complete? "openai" {:api-key "sk-valid"})))- (is (false? (metabot.settings/provider-credentials-complete? "openai" {:api-key ""})))- (is (false? (metabot.settings/provider-credentials-complete? "openai" {:api-key nil})))- (is (false? (metabot.settings/provider-credentials-complete? "openai" nil)))))--(deftest configured-provider-credentials-api-key-provider-test- (testing "API-key providers return an {:api-key ...} credentials map, or nil when unset"- (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-valid"]- (is (= {:api-key "sk-ant-valid"}- (metabot.settings/configured-provider-credentials "anthropic"))))- (mt/with-temporary-setting-values [llm-anthropic-api-key nil]- (is (nil? (metabot.settings/configured-provider-credentials "anthropic"))))))--(deftest validate-metabot-provider-accepts-valid-direct-google-test- (testing "accepts google provider strings with a model name"- (mt/with-temporary-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"]- (is (= "google/google/gemini-3.5-flash" (metabot.settings/llm-metabot-provider))))))--(deftest metabot-configured-with-google-credentials-test- (testing "returns true when google has an OAuth access token and project ID set — the location is optional"- (mt/with-temporary-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"- llm-google-oauth-access-token "ya29.test-token"- llm-google-project-id "my-project"- llm-google-location nil]- (is (true? (metabot.settings/llm-metabot-configured?))))))--(deftest metabot-configured-with-no-google-credentials-test- (testing "returns false when google has no service account key or OAuth access token"- (mt/with-temporary-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"- llm-google-oauth-access-token nil- llm-google-service-account-key nil]- (is (false? (metabot.settings/llm-metabot-configured?))))))--(deftest metabot-configured-with-google-service-account-test- (testing "returns true when google has only a service account key — the project ID can come from the key JSON"- (mt/with-temporary-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"- llm-google-oauth-access-token nil- llm-google-service-account-key "{\"type\": \"service_account\"}"- llm-google-project-id nil]- (is (true? (metabot.settings/llm-metabot-configured?))))))--(deftest metabot-configured-google-without-project-id-test- (testing "returns false when google has an OAuth access token but no project ID"- (mt/with-temporary-setting-values [llm-metabot-provider "google/google/gemini-3.5-flash"- llm-google-oauth-access-token "ya29.test-token"- llm-google-service-account-key nil- llm-google-project-id nil]- (is (false? (metabot.settings/llm-metabot-configured?))))))--(deftest configured-provider-credentials-google-fully-configured-test- (testing "returns the oauth-access-token/project-id/location credentials map when google is fully configured"- (mt/with-temporary-setting-values [llm-google-oauth-access-token "ya29.test-token"- llm-google-service-account-key nil- llm-google-project-id "my-project"- llm-google-location "us-central1"]- (is (= {:service-account-key nil- :oauth-access-token "ya29.test-token"- :project-id "my-project"- :location "us-central1"}- (metabot.settings/configured-provider-credentials "google"))))))--(deftest configured-provider-credentials-google-nil-location-test- (testing "the location is optional — a nil location still yields a configured credentials map"- (mt/with-temporary-setting-values [llm-google-oauth-access-token "ya29.test-token"- llm-google-service-account-key nil- llm-google-project-id "my-project"- llm-google-location nil]- (is (= {:service-account-key nil- :oauth-access-token "ya29.test-token"- :project-id "my-project"- :location nil}- (metabot.settings/configured-provider-credentials "google"))))))--(deftest configured-provider-credentials-google-missing-project-id-test- (testing "returns nil when google has an OAuth access token but no project ID"- (mt/with-temporary-setting-values [llm-google-oauth-access-token "ya29.test-token"- llm-google-service-account-key nil- llm-google-project-id nil]- (is (nil? (metabot.settings/configured-provider-credentials "google"))))))--(deftest configured-provider-credentials-google-service-account-only-test- (testing "a service account key alone counts as configured — no OAuth access token or project ID needed"- (mt/with-temporary-setting-values [llm-google-oauth-access-token nil- llm-google-service-account-key "{\"type\": \"service_account\"}"- llm-google-project-id nil- llm-google-location nil]- (is (= {:service-account-key "{\"type\": \"service_account\"}"- :oauth-access-token nil- :project-id nil- :location nil}- (metabot.settings/configured-provider-credentials "google"))))))--(deftest configured-provider-credentials-google-missing-credential-test- (testing "returns nil when google has a project ID but no service account key or OAuth access token"- (mt/with-temporary-setting-values [llm-google-oauth-access-token nil- llm-google-service-account-key nil- llm-google-project-id "my-project"]- (is (nil? (metabot.settings/configured-provider-credentials "google"))))))--(deftest provider-credentials-complete?-google-test- (testing "google credentials are complete with a non-blank OAuth access token and project ID; the location is optional"- (is (true? (metabot.settings/provider-credentials-complete?- "google"- {:oauth-access-token "ya29.test-token" :project-id "my-project"})))- (is (true? (metabot.settings/provider-credentials-complete?- "google"- {:oauth-access-token "ya29.test-token" :project-id "my-project" :location "us-central1"})))- (is (false? (metabot.settings/provider-credentials-complete? "google" {:oauth-access-token "ya29.test-token"})))- (is (false? (metabot.settings/provider-credentials-complete? "google" {:oauth-access-token " " :project-id "my-project"})))- (is (false? (metabot.settings/provider-credentials-complete? "google" {:project-id "my-project"})))- (is (false? (metabot.settings/provider-credentials-complete? "google" nil)))))