The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[BOT-316] Metabase BE support for dynamic agent profiles (#62544)
Tyler Pludeebbe91df3c54
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/api.cljindex 5016c242589..1bc78f04d0d 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj@@ -27,14 +27,15 @@(defn request"Handles an incoming request, making all required tool invocation, LLM call loops, etc."- [{:keys [metabot_id message context history conversation_id state]- :or {metabot_id metabot-v3.config/internal-metabot-id}}]+ [{:keys [metabot_id profile_id message context history conversation_id state]}](let [initial-message (metabot-v3.envelope/user-message message)history (conj (vec history) initial-message)+ metabot-id (metabot-v3.config/resolve-dynamic-metabot-id metabot_id)+ profile-id (metabot-v3.config/resolve-dynamic-profile-id profile_id metabot-id)env (metabot-v3.tools.api/handle-envelope{:context (metabot-v3.context/create-context context)- :metabot-id metabot_id- :profile-id (metabot-v3.config/metabot-profile-id metabot_id)+ :metabot-id metabot-id+ :profile-id profile-id:conversation-id conversation_id:messages history:state state})@@ -49,6 +50,7 @@_query-params{:keys [conversation_id] :as body} :- [:map[:metabot_id {:optional true} :string]+ [:profile_id {:optional true} :string][:message ms/NonBlankString][:context ::metabot-v3.context/context][:conversation_id ms/UUIDString]@@ -62,14 +64,15 @@(defn streaming-request"Handles an incoming request, making all required tool invocation, LLM call loops, etc."- [{:keys [metabot_id message context history conversation_id state]- :or {metabot_id metabot-v3.config/internal-metabot-id}}]+ [{:keys [metabot_id profile_id message context history conversation_id state]}](let [initial-message (metabot-v3.envelope/user-message message)- history (conj (vec history) initial-message)]+ history (conj (vec history) initial-message)+ metabot-id (metabot-v3.config/resolve-dynamic-metabot-id metabot_id)+ profile-id (metabot-v3.config/resolve-dynamic-profile-id profile_id metabot-id)](metabot-v3.tools.api/streaming-handle-envelope{:context (metabot-v3.context/create-context context)- :metabot-id metabot_id- :profile-id (get-in metabot-v3.config/metabot-config [metabot_id :profile-id])+ :metabot-id metabot-id+ :profile-id profile-id:conversation-id conversation_id:messages history:state state})))@@ -80,6 +83,7 @@_query-paramsbody :- [:map+ [:profile_id {:optional true} :string][:metabot_id {:optional true} :string][:message ms/NonBlankString][:context ::metabot-v3.context/context]diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/config.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/config.cljindex fab76da9181..8ef627452f8 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/config.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/config.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.metabot-v3.config(:require[medley.core :as m]+ [metabase-enterprise.metabot-v3.settings :as metabot-v3.settings][toucan2.core :as t2]))(def internal-metabot-id@@ -18,14 +19,13 @@embedded-metabot-id {:profile-id "default":entity-id "embeddedmetabotmetabo"}})-(defn metabot-profile-id+(defn metabot-id->profile-id"Return the profile-id for the Metabot instance with ID `metabot-id` or \"default\" if no profile-id is configured."[metabot-id](or (get-in metabot-config [metabot-id :profile-id])- (m/find-first #(when (= (:entity-id %) metabot-id)- (:profile-id %))- (vals metabot-config))- "default"))+ (:profile-id (m/find-first #(when (= (:entity-id %) metabot-id)+ (:profile-id %))+ (vals metabot-config)))))(defn normalize-metabot-id"Return the primary key for the metabot instance identified by `metabot-id`.@@ -34,3 +34,22 @@The provided ID can be a UUID from [[metabot-config]] or an entity_id of a Metabot instance."[metabot-id](t2/select-one-pk :model/Metabot :entity_id (get-in metabot-config [metabot-id :entity-id] metabot-id)))++(defn resolve-dynamic-metabot-id+ "Resolve dynamic metabot ID with logical fall backs+ Precedence: explicit metabot-id > env metabot-id > default (internal)"+ [metabot-id]+ (or metabot-id+ (metabot-v3.settings/metabot-id)+ internal-metabot-id))++(defn resolve-dynamic-profile-id+ "Resolve the ultimate ai-service profile ID with logical fall backs+ Precedence: explicit profile_id > env profile_id > metabot-id->profile-id > default (embedded)"+ ([profile-id]+ (resolve-dynamic-profile-id profile-id (resolve-dynamic-metabot-id nil)))+ ([profile-id metabot-id]+ (or profile-id+ (metabot-v3.settings/ai-service-profile-id)+ (metabot-id->profile-id metabot-id)+ "default")))diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/settings.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/settings.cljindex d8be5c36daa..636c45e734c 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/settings.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/settings.clj@@ -30,3 +30,19 @@:doc false:export? true:audit :never)++(defsetting metabot-id+ (deferred-tru "Override Metabot ID for agent streaming requests.")+ :type :string+ :visibility :internal+ :feature :metabot-v3+ :encryption :no+ :export? false)++(defsetting ai-service-profile-id+ (deferred-tru "Override Metabot profile ID for agent streaming requests.")+ :type :string+ :visibility :internal+ :feature :metabot-v3+ :encryption :no+ :export? false)diff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.cljindex 56d2c477704..c1bd1bb253f 100644--- a/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj@@ -34,9 +34,7 @@:messages [(update historical-message :role keyword) {:role :user, :content question}]:state {}:conversation-id conversation-id- :profile-id (if metabot-id- "default"- (metabot-v3.config/metabot-profile-id metabot-v3.config/internal-metabot-id))+ :profile-id (metabot-v3.config/resolve-dynamic-profile-id nil (metabot-v3.config/resolve-dynamic-metabot-id metabot-id)):session-id (fn [session-id](when-let [token (#'metabot-v3.tools.api/decode-ai-service-token session-id)](and (= (:metabot-id token) (or metabot-iddiff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/config_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/config_test.cljnew file mode 100644index 00000000000..105c9edf002--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/config_test.clj@@ -0,0 +1,78 @@+(ns metabase-enterprise.metabot-v3.config-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.metabot-v3.config :as metabot-v3.config]+ [metabase-enterprise.metabot-v3.settings :as metabot-v3.settings]+ [metabase.test :as mt]))++(deftest resolve-dynamic-metabot-id-test+ (testing "metabot ID resolution precedence"+ (mt/with-premium-features #{:metabot-v3}+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id nil]+ (testing "explicit metabot-id takes precedence"+ (is (= "explicit-id"+ (metabot-v3.config/resolve-dynamic-metabot-id "explicit-id"))))++ (testing "falls back to environment variable"+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id "env-metabot-id"]+ (is (= "env-metabot-id"+ (metabot-v3.config/resolve-dynamic-metabot-id nil)))))++ (testing "falls back to internal default when no explicit or env value"+ (is (= metabot-v3.config/internal-metabot-id+ (metabot-v3.config/resolve-dynamic-metabot-id nil))))))))++(deftest resolve-dynamic-profile-id-test+ (testing "profile ID resolution precedence"+ (mt/with-premium-features #{:metabot-v3}+ (mt/with-temporary-setting-values [metabot-v3.settings/ai-service-profile-id nil]+ (testing "explicit profile-id takes highest precedence"+ (is (= "explicit-profile"+ (metabot-v3.config/resolve-dynamic-profile-id "explicit-profile" "any-metabot-id"))))++ (testing "environment variable takes second precedence"+ (mt/with-temporary-setting-values [metabot-v3.settings/ai-service-profile-id "env-profile"]+ (is (= "env-profile"+ (metabot-v3.config/resolve-dynamic-profile-id nil "any-metabot-id")))))++ (testing "metabot-id mapping takes third precedence"+ (is (= "experimental"+ (metabot-v3.config/resolve-dynamic-profile-id nil metabot-v3.config/internal-metabot-id)))+ (is (= "default"+ (metabot-v3.config/resolve-dynamic-profile-id nil metabot-v3.config/embedded-metabot-id))))++ (testing "falls back to default when no matches"+ (is (= "default"+ (metabot-v3.config/resolve-dynamic-profile-id nil "unknown-metabot-id"))))++ (testing "single arity version uses dynamic metabot resolution"+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id metabot-v3.config/embedded-metabot-id]+ (is (= "default"+ (metabot-v3.config/resolve-dynamic-profile-id nil)))))))))++(deftest integrated-resolution-test+ (testing "combination of metabot-id and profile-id precedence resolution"+ (mt/with-premium-features #{:metabot-v3}+ (testing "explicit params override everything"+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id nil+ metabot-v3.settings/ai-service-profile-id nil]+ (let [metabot-id (metabot-v3.config/resolve-dynamic-metabot-id "custom-metabot")+ profile-id (metabot-v3.config/resolve-dynamic-profile-id "custom-profile" metabot-id)]+ (is (= "custom-metabot" metabot-id))+ (is (= "custom-profile" profile-id)))))++ (testing "environment variables work together"+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id "env-metabot"+ metabot-v3.settings/ai-service-profile-id "env-profile"]+ (let [metabot-id (metabot-v3.config/resolve-dynamic-metabot-id nil)+ profile-id (metabot-v3.config/resolve-dynamic-profile-id nil metabot-id)]+ (is (= "env-metabot" metabot-id))+ (is (= "env-profile" profile-id)))))++ (testing "defaults work together"+ (mt/with-temporary-setting-values [metabot-v3.settings/metabot-id nil+ metabot-v3.settings/ai-service-profile-id nil]+ (let [metabot-id (metabot-v3.config/resolve-dynamic-metabot-id nil)+ profile-id (metabot-v3.config/resolve-dynamic-profile-id nil metabot-id)]+ (is (= metabot-v3.config/internal-metabot-id metabot-id))+ (is (= "experimental" profile-id))))))))