Skip to content

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

BOT-2546 chars seeded from a saved question (#86079)

Voytek Pituła42d954dc7f29
diff --git a/resources/metabot/prompts/llm_shape.selmer b/resources/metabot/prompts/llm_shape.selmer
index d01582e2dc5..b31df5d626f 100644
--- a/resources/metabot/prompts/llm_shape.selmer
+++ b/resources/metabot/prompts/llm_shape.selmer
@@ -194,7 +194,7 @@ The chart is powered by the following queries:
</chart>
{% endif %}
{% if is_chart %}
-<chart id="{{chart_id}}" type="{{chart_type}}" query-id="{{chart_query_id}}">
+<chart id="{{chart_id}}" type="{{chart_type}}"{% if chart_query_id %} query-id="{{chart_query_id}}"{% endif %}>
Chart link: metabase://chart/{{chart_id}}
</chart>
{% endif %}
diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.clj
index d99908d1363..15da1d68f77 100644
--- a/src/metabase/metabot/agent/core.clj
+++ b/src/metabase/metabot/agent/core.clj
@@ -462,13 +462,13 @@
(:user_is_viewing context)))
(defn chart-config->chart
- "Create chart structure from chart-config"
- [id {:keys [query timeline_events] :as chart-config}]
- {:chart_id id
- ;; `id` is the same viewing-context item id seed-state stores this chart's query
- ;; under; reusing it as :query_id lets edit_chart carry a query-id through (see
- ;; extract-charts below) instead of leaving charts seeded this way uncaptured.
- :query_id id
+ "Create chart structure from chart-config.
+ `query-id` is the ID that seed-state stores the chart's query under, or nil if it stores no query for the chart."
+ [chart-id query-id {:keys [query timeline_events] :as chart-config}]
+ {:chart_id chart-id
+ ;; edit_chart carries :query_id through (see extract-charts). It is never a card ID: run_query runs the saved
+ ;; card by that ID.
+ :query_id query-id
:queries [query]
:timeline_events (or timeline_events [])
;; TODO (lbrdnk 2026-03-25): Viz settings seem to be redundant wrt fix this PR is implementing. Figure out
@@ -482,7 +482,7 @@
(reduce
;; This logic is flawed for items with more than 1 chart config. It expects at most 1 chart config per viewing item.
;; TODO (lbrdnk 2026-03-24): Figure out what is reasonable solution here. (ie no overwriting single config)
- (fn [acc {:keys [id chart_configs] :as _item}]
+ (fn [acc {:keys [id chart_configs] :as item}]
;; TODO (lbrdnk 2026-03-24): This is developed against adhoc queries. Ensure other cases work too!
(if-not (seq chart_configs)
acc
@@ -490,7 +490,10 @@
(into {}
(map (comp
(juxt :chart_id identity)
- (partial chart-config->chart id)))
+ ;; The ID of a saved card is an integer, and the chart tools get charts by a string ID.
+ (partial chart-config->chart
+ (str id)
+ (first (extract-query-from-context-item item)))))
chart_configs))))
state
(:user_is_viewing context)))
@@ -503,7 +506,6 @@
[context]
(let [seeded (-> {} (seed-state context) (seed-charts context))]
(into (set (keys (:queries seeded)))
- (map str)
(keys (:charts seeded)))))
;;; Main loop
diff --git a/test/metabase/metabot/agent/core_test.clj b/test/metabase/metabot/agent/core_test.clj
index 3675fa367de..942eca23967 100644
--- a/test/metabase/metabot/agent/core_test.clj
+++ b/test/metabase/metabot/agent/core_test.clj
@@ -1760,7 +1760,9 @@
:timeline_events []}
memory (-> (#'agent/init-agent {:profile-id :internal
:context {:user_is_viewing
- [{:chart_configs [chart-config]}]}})
+ [{:type "adhoc"
+ :query query
+ :chart_configs [chart-config]}]}})
:memory-atom
deref)
@@ -1770,8 +1772,10 @@
chart-key (first (keys charts))]
(testing "Loaded charts from chart configs into memory"
(is (string? chart-key))
- ;; :query_id must match :chart_id — it's how edit_chart later carries a
- ;; query-id through for this chart (see chart-config->chart, extract-charts).
+ ;; :query_id is the ID that seed-state stores the ad hoc item's query under, which is the item ID.
+ ;; It is how edit_chart later carries a query-id through for this chart (see chart-config->chart,
+ ;; extract-charts).
+ (is (contains? (get-in memory [:state :queries]) chart-key))
(is (=? {chart-key {:chart_id chart-key
:query_id chart-key
:timeline_events []
@@ -1783,6 +1787,64 @@
(is (=? {chart-configs-key chart-config}
chart-configs)))))
+(deftest edit-chart-of-viewed-saved-card-test
+ ;; A real user, not `mt/as-admin`: tools refuse a caller without a user id.
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2}]
+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections
+ llm-metabot-provider test-provider]
+ (let [card-id 157
+ query {:lib/type :mbql/query
+ :database db-id
+ :stages [{:lib/type :mbql.stage/native :native "SELECT 1"}]}
+ run-turn (fn [card-type]
+ (let [shown-id (atom nil)
+ calls (atom 0)
+ parts (mt/with-dynamic-fn-redefs
+ [openrouter/openrouter
+ (fn [request]
+ (if (= 1 (swap! calls inc))
+ (do (reset! shown-id (second (re-find #"Query ID: ([^\s\\\"]+)"
+ (pr-str request))))
+ (mut/mock-llm-response
+ [{:type :tool-input
+ :id "edit"
+ :function "edit_chart"
+ :arguments {:chart_id (str card-id)
+ :new_viz_settings {:chart_type "table"}
+ :title "Results"
+ :description "All rows."}}]))
+ (mut/mock-llm-response [{:type :text :text "Done."}])))]
+ (into [] (run-agent-loop
+ {:messages [{:role :user :content "Show it as a table"}]
+ :state {}
+ :profile-id :internal
+ :context {:user_is_viewing
+ [{:type card-type
+ :id card-id
+ :query query
+ :chart_configs [{:title "Results"
+ :query query
+ :display_type "bar"}]}]}})))]
+ {:shown-id @shown-id
+ :result (some #(when (and (= :tool-output (:type %)) (= "edit" (:id %))) (:result %))
+ parts)
+ :state (some #(when (= "state" (:data-type %)) (:data %)) parts)}))]
+ (testing "a saved question's chart points at the query on screen"
+ (let [{:keys [shown-id result state]} (run-turn "question")]
+ (is (nil? (:error result)) (:output result))
+ (is (some? shown-id))
+ (is (= shown-id (get-in state [:charts (str card-id) :query_id])))
+ (testing "and the card ID stays free for run_query to run the saved question"
+ (is (not (contains? (:queries state) (str card-id)))))))
+ (testing "a saved model's query is not seeded, so its chart points at no query"
+ (let [{:keys [result state]} (run-turn "model")]
+ (is (nil? (:error result)) (:output result))
+ (is (nil? (get-in state [:charts (str card-id) :query_id])))
+ (is (not (contains? (:queries state) (str card-id))))
+ (testing "and the chart in the tool output has no query-id attribute"
+ (is (not (str/includes? (:output result) "query-id=")))))))))))
+
(deftest viewing-context-item-id-persists-once-stored-test
(let [query {:database 1 :type :query :query {:source-table 1}}
agent (#'agent/init-agent {:profile-id :internal