Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Let Metabot see dashboard filters (#82959)

Andrei Simionescuf01ab65388bc
diff --git a/resources/metabot/prompts/llm_shape.selmer b/resources/metabot/prompts/llm_shape.selmer
index 88d33d201db..9e292a13035 100644
--- a/resources/metabot/prompts/llm_shape.selmer
+++ b/resources/metabot/prompts/llm_shape.selmer
@@ -248,6 +248,8 @@ The question is visualized as follows:
The dashboard is stored in the following collection:
{{dashboard_collection_xml|safe}}
{% endif %}
+{% if dashboard_filters_xml %}
+{{dashboard_filters_xml|safe}}{% endif %}
{% if dashboard_tabs_xml %}
{{dashboard_tabs_xml|safe}}{% endif %}
</dashboard>
diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.clj
index e0245d004b7..57c66c524f2 100644
--- a/src/metabase/metabot/db.clj
+++ b/src/metabase/metabot/db.clj
@@ -892,9 +892,9 @@
(t2/select-one :model/Dashboard dashboard-id))
(mu/defn dashboard-summary
- "The ID, description, name, and Collection ID of the Dashboard with `dashboard-id`, or nil."
+ "The ID, description, name, Collection ID, and parameters of the Dashboard with `dashboard-id`, or nil."
[dashboard-id :- ::lib.schema.id/dashboard]
- (t2/select-one [:model/Dashboard :id :description :name :collection_id] dashboard-id))
+ (t2/select-one [:model/Dashboard :id :description :name :collection_id :parameters] dashboard-id))
(mu/defn dashboard-name
"The name of the Dashboard with `dashboard-id`."
diff --git a/src/metabase/metabot/tools/shared/llm_shape.clj b/src/metabase/metabot/tools/shared/llm_shape.clj
index b0044f7fb79..fa7083bdfd1 100644
--- a/src/metabase/metabot/tools/shared/llm_shape.clj
+++ b/src/metabase/metabot/tools/shared/llm_shape.clj
@@ -190,14 +190,16 @@
(str/replace s "|" "\\u007c")))
(defn- truncate
- "Cap `s` at `max-len` characters, appending an ellipsis when truncated.
+ "Cap `s` at `max-len` characters without splitting a surrogate pair, appending an ellipsis when truncated.
Useful to ensure long free text values (e.g. table descriptions) don't bloat the LLM context.
Returns nil for nil input."
[s max-len]
(when s
(let [s (str s)]
(if (> (count s) max-len)
- (str (subs s 0 max-len) "...")
+ (str (subs s 0 (cond-> max-len
+ (Character/isHighSurrogate (.charAt s (int (dec max-len)))) dec))
+ "...")
s))))
(defn- database-type-or-unknown
@@ -720,10 +722,35 @@
;; Default to viz card
(viz-card->xml card)))
+(def ^:private max-dashboard-filters
+ "Cap on the filters listed for a dashboard."
+ 50)
+
+(def ^:private max-dashboard-filter-name-length
+ "Cap on a dashboard filter's name."
+ 100)
+
+(defn- dashboard-filters->xml
+ "The `<filters>` block for a dashboard's `parameters`, or nil when it has none. Lists at most
+ [[max-dashboard-filters]] of them and says how many were left out."
+ [parameters]
+ (when (seq parameters)
+ (let [total (count parameters)]
+ (str " <filters>\n"
+ (str/join (for [param (take max-dashboard-filters parameters)]
+ (str " <filter id=\"" (escape-xml (:id param))
+ "\" name=\"" (escape-xml (truncate (:name param) max-dashboard-filter-name-length))
+ "\" type=\"" (escape-xml (u/qualified-name (:type param))) "\"/>\n")))
+ (when (> total max-dashboard-filters)
+ (str " <truncation-note>Showing " max-dashboard-filters " of " total " filters; the other "
+ (- total max-dashboard-filters) " are not listed.</truncation-note>\n"))
+ " </filters>\n"))))
+
(defn dashboard->xml
"Format dashboard for LLM consumption.
- Matches Python Dashboard.llm_representation exactly."
- [{:keys [id name description verified collection dashcards]}]
+ Matches Python Dashboard.llm_representation exactly, except we additionally surface the
+ dashboard's filters."
+ [{:keys [id name description verified collection parameters dashcards]}]
;; Group cards by tab and sort
;; TODO (Chris 2026-07-09) -- tabs sort by raw id here but by position in
;; resources/fetch-dashboard-items; align on position
@@ -750,6 +777,7 @@
:dashboard_name name
:dashboard_description description
:dashboard_collection_xml (when collection (collection->xml collection))
+ :dashboard_filters_xml (dashboard-filters->xml parameters)
:dashboard_tabs_xml tabs-xml})))
(defn database-schema->xml
diff --git a/test/metabase/metabot/agent/user_context_test.clj b/test/metabase/metabot/agent/user_context_test.clj
index 334ed94259b..503e092bb2f 100644
--- a/test/metabase/metabot/agent/user_context_test.clj
+++ b/test/metabase/metabot/agent/user_context_test.clj
@@ -405,14 +405,24 @@
(let [result (user-context/format-viewing-context
{:user_is_viewing [{:type "question" :id card-id}]})]
(is (re-find #"display_type=\"pie\"" result))))))
- (testing "dashboard with only type+id fetches name and description from DB"
+ (testing "dashboard with only type+id fetches name, description and filters from DB"
(mt/with-test-user :rasta
(mt/with-temp [:model/Dashboard {dash-id :id} {:name "Executive Dashboard"
- :description "Top-level KPIs"}]
+ :description "Top-level KPIs"
+ :parameters [{:id "p1"
+ :name "Created At"
+ :type "date/all-options"}
+ {:id "p2"
+ :name "State & Province"
+ :type "string/="}]}]
(let [result (user-context/format-viewing-context
{:user_is_viewing [{:type "dashboard" :id dash-id}]})]
(is (re-find #"Executive Dashboard" result))
- (is (re-find #"Top-level KPIs" result))))))
+ (is (re-find #"Top-level KPIs" result))
+ (is (str/includes? result (str " <filters>\n"
+ " <filter id=\"p1\" name=\"Created At\" type=\"date/all-options\"/>\n"
+ " <filter id=\"p2\" name=\"State &amp; Province\" type=\"string/=\"/>\n"
+ " </filters>")))))))
(testing "table with only type+id fetches details including fields from DB"
(mt/with-test-user :rasta
(let [result (user-context/format-viewing-context
diff --git a/test/metabase/metabot/tools/shared/llm_shape_test.clj b/test/metabase/metabot/tools/shared/llm_shape_test.clj
index 41b4b4c6c2d..60a0110c588 100644
--- a/test/metabase/metabot/tools/shared/llm_shape_test.clj
+++ b/test/metabase/metabot/tools/shared/llm_shape_test.clj
@@ -544,6 +544,23 @@
(is (str/includes? xml "<text_card"))
(is (str/ends-with? (str/trim xml) "</dashboard>")))))
+(deftest ^:parallel dashboard->xml-caps-filters-test
+ (testing "lists at most 50 filters, cuts long names, and says how many filters were left out"
+ (let [long-name (apply str "Region " (repeat 120 "x"))
+ parameters (for [i (range 53)]
+ {:id (str "p" i) :name long-name :type :string/=})
+ xml (llm-shape/dashboard->xml {:id 50 :name "Everything" :parameters parameters})]
+ (is (= 50 (count (re-seq #"<filter " xml))))
+ (is (str/includes? xml (str "<filter id=\"p49\" name=\"" (subs long-name 0 100) "...\" type=\"string/=\"/>")))
+ (is (str/includes? xml (str "<truncation-note>Showing 50 of 53 filters; "
+ "the other 3 are not listed.</truncation-note>")))))
+ (testing "cutting a name never splits an emoji"
+ (let [ascii (apply str (repeat 99 "a"))
+ xml (llm-shape/dashboard->xml {:id 51 :name "Emoji" :parameters [{:id "p1"
+ :name (str ascii "😀")
+ :type :string/=}]})]
+ (is (str/includes? xml (str "name=\"" ascii "...\""))))))
+
(deftest ^:parallel user->xml-test
(testing "formats user matching Python"
(let [user {:id 1