Skip to content

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

Convert Metabot's chart tools to the tool contract (#85091)

Andrei Simionescuc4669bc7e2f7
diff --git a/src/metabase/metabot/tools/charts.clj b/src/metabase/metabot/tools/charts.clj
index 4a4db6ec191..e0e2b7fad2d 100644
--- a/src/metabase/metabot/tools/charts.clj
+++ b/src/metabase/metabot/tools/charts.clj
@@ -1,5 +1,5 @@
(ns metabase.metabot.tools.charts
- "Chart tool wrappers."
+ "The `create_chart` and `edit_chart` tools."
(:require
[metabase.metabot.agent.links :as links]
[metabase.metabot.agent.memory :as memory]
@@ -7,11 +7,10 @@
[metabase.metabot.scope :as scope]
[metabase.metabot.tools.charts.create :as create-chart-tools]
[metabase.metabot.tools.charts.edit :as edit-chart-tools]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.shared :as shared]
[metabase.metabot.tools.shared.instructions :as instructions]
- [metabase.metabot.tools.shared.llm-shape :as llm-shape]
- [metabase.metabot.tools.util :as metabot.tools.u]
- [metabase.util.malli :as mu]))
+ [metabase.metabot.tools.shared.llm-shape :as llm-shape]))
(set! *warn-on-reflection* true)
@@ -33,18 +32,19 @@
[:title :string]
[:description :string]])
-(mu/defn ^{:tool-name "create_chart"
- :scope scope/agent-viz-create}
- create-chart-tool
- "Create a chart from a query.
+(defrecord CreateChartTool []
+ tools/Tool
+ (declaration [_]
+ {:name "create_chart"
+ :description (str "Create a chart from a query.\n\n"
+ "Provide a query_id in data_source, a chart_type in viz_settings, and a short, "
+ "human-friendly `title` shown above the chart. Also provide a concise one- or two-sentence "
+ "`description` explaining what the chart shows (the metric, the grouping, and any notable "
+ "filter); it is used as the saved question's description.")
+ :scope scope/agent-viz-create
+ :args create-chart-schema})
- Provide a query_id in data_source, a chart_type in viz_settings, and a short,
- human-friendly `title` shown above the chart. Also provide a concise one- or
- two-sentence `description` explaining what the chart shows (the metric, the
- grouping, and any notable filter); it is used as the saved question's
- description."
- [{:keys [data_source viz_settings title description]} :- create-chart-schema]
- (try
+ (handle [_ {:keys [data_source viz_settings title description]} _ctx]
(let [result (create-chart-tools/create-chart
{:query-id (get data_source :query_id)
:chart-type (keyword (get viz_settings :chart_type))
@@ -58,9 +58,11 @@
:query (links/->frontend-query (:query result))
:display (:chart-type result)
:title title
- :description description})]})
- (catch Exception e
- (metabot.tools.u/handle-agent-error e))))
+ :description description})]})))
+
+(def create-chart-tool
+ "`create_chart`: chart a query that this conversation holds."
+ (->CreateChartTool))
(def ^:private edit-chart-schema
[:map {:closed true}
@@ -70,16 +72,18 @@
[:title :string]
[:description :string]])
-(mu/defn ^{:tool-name "edit_chart"
- :scope scope/agent-viz-edit}
- edit-chart-tool
- "Edit an existing chart's visualization type.
+(defrecord EditChartTool []
+ tools/Tool
+ (declaration [_]
+ {:name "edit_chart"
+ :description (str "Edit an existing chart's visualization type.\n\n"
+ "Provide a new chart_type in new_viz_settings and a short, human-friendly `title` shown above "
+ "the chart. Also provide a concise `description` of what the chart shows; it is used as the "
+ "saved question's description.")
+ :scope scope/agent-viz-edit
+ :args edit-chart-schema})
- Provide a new chart_type in new_viz_settings and a short, human-friendly `title`
- shown above the chart. Also provide a concise `description` of what the chart
- shows; it is used as the saved question's description."
- [{:keys [chart_id new_viz_settings title description]} :- edit-chart-schema]
- (try
+ (handle [_ {:keys [chart_id new_viz_settings title description]} _ctx]
(let [new-viz (keyword (get new_viz_settings :chart_type))
chart (get (shared/current-charts-state) chart_id)
query (or (first (:queries chart))
@@ -104,6 +108,8 @@
:query (links/->frontend-query query)
:display new-viz
:title title
- :description description})]})
- (catch Exception e
- (metabot.tools.u/handle-agent-error e))))
+ :description description})]})))
+
+(def edit-chart-tool
+ "`edit_chart`: give a chart from this conversation a new chart type, as a new chart."
+ (->EditChartTool))
diff --git a/src/metabase/metabot/tools/charts/create.clj b/src/metabase/metabot/tools/charts/create.clj
index 4f6917c43f5..d92d97d4aff 100644
--- a/src/metabase/metabot/tools/charts/create.clj
+++ b/src/metabase/metabot/tools/charts/create.clj
@@ -1,17 +1,12 @@
(ns metabase.metabot.tools.charts.create
"Tool for creating charts from queries."
(:require
- [clojure.string :as str]
[metabase.metabot.agent.links :as links]
- [metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
[metabase.util.log :as log]))
(set! *warn-on-reflection* true)
-(def ^:private valid-chart-types
- "Valid chart types supported by Metabase."
- (into #{} (map keyword) shared/chart-types))
-
(defn- format-chart-for-llm
"Format chart data as XML for LLM consumption."
[{:keys [chart-id query-id chart-type]}]
@@ -39,29 +34,20 @@
- :chart-link - Metabase link to the chart
- :chart-type - Type of chart created
- :query-id - ID of the source query
- - :reactions - Navigation action to show the chart"
+ - :reactions - Navigation action to show the chart
+
+ Throws [[recoverable.common/unknown-query-id!]] for a query ID that `queries-state` doesn't hold."
[{:keys [query-id chart-type queries-state]}]
(log/info "Creating chart" {:query-id query-id
:chart-type chart-type
:available-queries (keys queries-state)})
- ;; Validate chart type
- (when-not (contains? valid-chart-types chart-type)
- (throw (ex-info (str "Invalid chart type: " (name chart-type)
- ". Valid types are: " (pr-str valid-chart-types))
- {:agent-error? true
- :chart-type chart-type})))
-
;; Look up query from state - try both string and original key
(let [query (or (get queries-state query-id)
(get queries-state (str query-id)))]
(when-not query
- (throw (ex-info (str "Query not found with ID: " query-id
- ". Available queries: [" (str/join ", " (keys queries-state)) "]. "
- "Make sure you're using a query ID from a previous query_model or query_metric result.")
- {:agent-error? true
- :query-id query-id
- :available-queries (keys queries-state)})))
+ (recoverable.common/unknown-query-id! {:query-id query-id
+ :available (sort (keys queries-state))}))
;; Create the chart and generate navigation URL
(let [chart-id (str (random-uuid))
results-url (links/query-and-viz-link query chart-type)
diff --git a/src/metabase/metabot/tools/charts/edit.clj b/src/metabase/metabot/tools/charts/edit.clj
index 31c51bbb56f..280665f2719 100644
--- a/src/metabase/metabot/tools/charts/edit.clj
+++ b/src/metabase/metabot/tools/charts/edit.clj
@@ -1,13 +1,9 @@
(ns metabase.metabot.tools.charts.edit
"Tool for editing chart visualization settings."
(:require
- [metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
[metabase.util.log :as log]))
-(def ^:private valid-chart-types
- "Valid chart types supported by Metabase."
- (into #{} (map keyword) shared/chart-types))
-
(defn- format-chart-link
"Format a metabase:// link to the chart."
[chart-id]
@@ -47,22 +43,16 @@
- :chart-id - New unique ID for the edited chart
- :chart-content - XML representation of the chart
- :chart-link - Metabase link to the chart
- - :chart-type - Type of chart created"
+ - :chart-type - Type of chart created
+
+ Throws [[recoverable.common/unknown-chart-id!]] for a chart ID that `charts-state` doesn't hold."
[{:keys [chart-id new-chart-type charts-state query]}]
(log/info "Editing chart" {:chart-id chart-id :new-chart-type new-chart-type})
- ;; Validate chart type
- (when-not (contains? valid-chart-types new-chart-type)
- (throw (ex-info (str "Invalid chart type: " (name new-chart-type)
- ". Valid types are: " (pr-str valid-chart-types))
- {:agent-error? true
- :chart-type new-chart-type})))
-
(let [chart-data (get charts-state (str chart-id))]
(when-not chart-data
- (throw (ex-info "Sorry, I have issues accessing the chart data. Is there anything else I can help you with?"
- {:agent-error? true
- :chart-id chart-id})))
+ (recoverable.common/unknown-chart-id! {:chart-id chart-id
+ :available (sort (keys charts-state))}))
(let [;; The caller may have resolved the query from somewhere other than
;; charts-state (e.g. queries-state, when the chart carries a
;; :query_id but no :queries). Fall back to that so :query on the
diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.clj
index 6ae5a4c5201..f97428b6f6b 100644
--- a/src/metabase/metabot/tools/construct.clj
+++ b/src/metabase/metabot/tools/construct.clj
@@ -45,7 +45,7 @@
(def ^:private construct-visualization-schema
[:map {:closed true}
- [:chart_type :string]])
+ [:chart_type (into [:enum] shared/chart-types)]])
(def ^:private construct-notebook-query-json-schema
"Hand-authored JSON Schema for the `:query` argument, attached to the deliberately open,
diff --git a/src/metabase/metabot/tools/recoverable/common.clj b/src/metabase/metabot/tools/recoverable/common.clj
index b820cd7f1f1..378363c3b9b 100644
--- a/src/metabase/metabot/tools/recoverable/common.clj
+++ b/src/metabase/metabot/tools/recoverable/common.clj
@@ -7,6 +7,7 @@
(:require
[clojure.string :as str]
[metabase.metabot.tools.error :refer [defrecoverable]]
+ [metabase.util :as u]
[metabase.util.quoting :as quoting]))
(set! *warn-on-reflection* true)
@@ -60,3 +61,35 @@
{:message (str "Database " database-id " was not found.")
:recovery [{:uses #{"read_resource"}
:text "Read `metabase://databases` with `read_resource` to choose an available database ID, then retry."}]})
+
+(def ^:private max-listed-chart-ids
+ "The most chart IDs that [[unknown-chart-id!]] lists.
+ With each ID cut to [[max-chart-id-chars]], the recovery steps after the list stay within the runtime's
+ 4,000-character limit on failure text."
+ 30)
+
+(def ^:private max-chart-id-chars
+ "The most characters of a chart ID that [[unknown-chart-id!]] repeats.
+ The model supplies the requested ID, so its length is otherwise unbounded."
+ 100)
+
+(defrecoverable unknown-chart-id!
+ "The chart ID does not belong to a chart in this conversation."
+ {:payload [:map {:closed true}
+ [:chart-id :string]
+ [:available [:sequential :string]]]}
+ [{:keys [chart-id available]}]
+ (let [more (- (count available) max-listed-chart-ids)]
+ {:message (str "No chart with ID " (pr-str (u/truncate chart-id max-chart-id-chars))
+ " exists in this conversation."
+ (if (seq available)
+ (str " The available chart IDs are "
+ (str/join ", " (map #(u/truncate % max-chart-id-chars)
+ (take max-listed-chart-ids available)))
+ (when (pos? more) (str " and " more " more"))
+ ".")
+ " No charts have been created in this conversation yet."))
+ :recovery [{:uses #{"construct_notebook_query"}
+ :text "To make a new chart, call `construct_notebook_query` and use the chart ID that it returns."}
+ {:uses #{"create_chart"}
+ :text "To chart a query from this conversation, call `create_chart` with its query ID."}]}))
diff --git a/test/metabase/metabot/agent/links_test.clj b/test/metabase/metabot/agent/links_test.clj
index 3f406a25bd6..d4ec04249b3 100644
--- a/test/metabase/metabot/agent/links_test.clj
+++ b/test/metabase/metabot/agent/links_test.clj
@@ -7,6 +7,7 @@
[metabase.lib.test-metadata :as meta]
[metabase.lib.test-util :as lib.tu]
[metabase.metabot.agent.links :as links]
+ [metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.charts :as charts]
[metabase.metabot.tools.shared :as shared]
[metabase.test :as mt]
@@ -158,10 +159,10 @@
(testing "in the chart data part"
(is (= expected
(-> (binding [shared/*memory-atom* (atom {:state {:queries {"q-1" query}}})]
- (charts/create-chart-tool {:data_source {:query_id "q-1"}
- :viz_settings {:chart_type "bar"}
- :title "Check-ins in July"
- :description "Check-ins during July 2014."}))
+ (test-util/call-tool charts/create-chart-tool {:data_source {:query_id "q-1"}
+ :viz_settings {:chart_type "bar"}
+ :title "Check-ins in July"
+ :description "Check-ins during July 2014."}))
(get-in [:data-parts 0 :data :query :query :parameters])
json/encode
json/decode+kw)))))))
diff --git a/test/metabase/metabot/tools/charts/create_test.clj b/test/metabase/metabot/tools/charts/create_test.clj
index 545f3b2c596..2e713ed314e 100644
--- a/test/metabase/metabot/tools/charts/create_test.clj
+++ b/test/metabase/metabot/tools/charts/create_test.clj
@@ -37,21 +37,10 @@
:queries-state queries-state})]
(is (= chart-type (:chart-type result))
(str "Chart type " chart-type " should be set correctly"))))))
- (testing "throws error for invalid chart type"
- (let [queries-state {"q-789" {:query-id "q-789"
- :query-content "SELECT 1"
- :database 1}}]
- (is (thrown-with-msg?
- clojure.lang.ExceptionInfo
- #"Invalid chart type"
- (create-chart/create-chart
- {:query-id "q-789"
- :chart-type :invalid-type
- :queries-state queries-state})))))
(testing "throws error when query not found"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Query not found"
+ #"Query \"nonexistent\" was not found"
(create-chart/create-chart
{:query-id "nonexistent"
:chart-type :bar
diff --git a/test/metabase/metabot/tools/charts/edit_test.clj b/test/metabase/metabot/tools/charts/edit_test.clj
index 968fc521b00..54a3b7ef7fd 100644
--- a/test/metabase/metabot/tools/charts/edit_test.clj
+++ b/test/metabase/metabot/tools/charts/edit_test.clj
@@ -48,19 +48,10 @@
:charts-state charts-state})]
(is (= new-type (:chart-type result))
(str "New chart type " new-type " should be set correctly"))))))
- (testing "throws error for invalid chart type"
- (let [charts-state {"chart-789" {:chart-id "chart-789"}}]
- (is (thrown-with-msg?
- clojure.lang.ExceptionInfo
- #"Invalid chart type"
- (edit-chart/edit-chart
- {:chart-id "chart-789"
- :new-chart-type :invalid-type
- :charts-state charts-state})))))
(testing "throws error when chart not found"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"issues accessing the chart data"
+ #"No chart with ID \"nonexistent\" exists in this conversation"
(edit-chart/edit-chart
{:chart-id "nonexistent"
:new-chart-type :bar
@@ -174,10 +165,10 @@
(is (contains? (tools.shared/current-queries-state) query-id))
(testing "Edit chart can handle charts created using construct-notebook-query-tool"
;; (3) call the edit-chart-tool which uses the shared memory
- (let [edit-result (tools.charts/edit-chart-tool {:chart_id chart-id
- :new_viz_settings {:chart_type "pie"}
- :title "Counts by category"
- :description "Counts by category."})
+ (let [edit-result (test-util/call-tool tools.charts/edit-chart-tool {:chart_id chart-id
+ :new_viz_settings {:chart_type "pie"}
+ :title "Counts by category"
+ :description "Counts by category."})
new-chart-id (get-in edit-result [:structured-output :chart-id])
new-chart-in-memory (get (tools.shared/current-charts-state) new-chart-id)]
(is (= :pie
diff --git a/test/metabase/metabot/tools/charts_test.clj b/test/metabase/metabot/tools/charts_test.clj
index 4fa1904185c..a99532259c0 100644
--- a/test/metabase/metabot/tools/charts_test.clj
+++ b/test/metabase/metabot/tools/charts_test.clj
@@ -1,15 +1,22 @@
(ns metabase.metabot.tools.charts-test
- "Tests that the chart tools always emit a `generated_entity` card data part. The
- inline-vs-navigate decision now lives on the frontend, so the backend no longer
- branches on a capability."
+ "Tests that the chart tools always emit a `generated_entity` card data part, and what the model reads when a call
+ fails. The inline-vs-navigate decision now lives on the frontend, so the backend no longer branches on a
+ capability."
(:require
+ [clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.api-scope.core :as api-scope]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-metadata :as meta]
+ [metabase.metabot.scope :as scope]
+ [metabase.metabot.test-util :as test-util]
+ [metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.charts :as charts]
[metabase.metabot.tools.charts.create :as create-chart-tools]
[metabase.metabot.tools.charts.edit :as edit-chart-tools]
+ [metabase.metabot.tools.construct :as construct]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
[metabase.metabot.tools.shared :as shared]
[metabase.test :as mt]
[metabase.util.json :as json]))
@@ -26,10 +33,10 @@
([chart-type]
(let [memory (atom {:state {:queries {"q-1" stub-query}}})]
(binding [shared/*memory-atom* memory]
- (charts/create-chart-tool {:data_source {:query_id "q-1"}
- :viz_settings {:chart_type chart-type}
- :title "Orders by month"
- :description "Monthly count of orders."})))))
+ (test-util/call-tool charts/create-chart-tool {:data_source {:query_id "q-1"}
+ :viz_settings {:chart_type chart-type}
+ :title "Orders by month"
+ :description "Monthly count of orders."})))))
(deftest create-chart-generated-entity-test
(testing "emits a single generated_entity card part"
@@ -60,15 +67,15 @@
:queries [query]
:visualization_settings {:chart_type :bar}}}}]
(doseq [[tool-name run-tool] [["create_chart"
- #(charts/create-chart-tool {:data_source {:query_id "q-1"}
- :viz_settings {:chart_type "bar"}
- :title "Check-ins in July"
- :description "Check-ins during July 2026."})]
+ #(test-util/call-tool charts/create-chart-tool {:data_source {:query_id "q-1"}
+ :viz_settings {:chart_type "bar"}
+ :title "Check-ins in July"
+ :description "Check-ins during July 2026."})]
["edit_chart"
- #(charts/edit-chart-tool {:chart_id "c-1"
- :new_viz_settings {:chart_type "line"}
- :title "Check-ins in July"
- :description "Check-ins during July 2026."})]]]
+ #(test-util/call-tool charts/edit-chart-tool {:chart_id "c-1"
+ :new_viz_settings {:chart_type "line"}
+ :title "Check-ins in July"
+ :description "Check-ins during July 2026."})]]]
(testing tool-name
(let [result (binding [shared/*memory-atom* (atom {:state state})]
(run-tool))
@@ -98,10 +105,10 @@
(lib/filter filter-clause)
(lib/aggregate (lib/count)))
result (binding [shared/*memory-atom* (atom {:state {:queries {"q-1" query}}})]
- (charts/create-chart-tool {:data_source {:query_id "q-1"}
- :viz_settings {:chart_type "scalar"}
- :title "Check-ins in July 2014"
- :description "Check-ins during July 2014."}))
+ (test-util/call-tool charts/create-chart-tool {:data_source {:query_id "q-1"}
+ :viz_settings {:chart_type "scalar"}
+ :title "Check-ins in July 2014"
+ :description "Check-ins during July 2014."}))
body (-> result (get-in [:data-parts 0 :data :query :query]) json/encode json/decode)]
(is (= (mt/first-row (mt/process-query query))
(mt/first-row (mt/user-http-request :crowberto :post 202 "dataset" body))))))))))
@@ -115,10 +122,10 @@
:visualization_settings {:chart_type :bar}}}}
:context {}})
result (binding [shared/*memory-atom* memory]
- (charts/edit-chart-tool {:chart_id "c-1"
- :new_viz_settings {:chart_type "pie"}
- :title "Orders by month"
- :description "Monthly count of orders."}))
+ (test-util/call-tool charts/edit-chart-tool {:chart_id "c-1"
+ :new_viz_settings {:chart_type "pie"}
+ :title "Orders by month"
+ :description "Monthly count of orders."}))
parts (:data-parts result)
entity (:data (first parts))]
(is (= 1 (count parts)))
@@ -133,20 +140,100 @@
(is (= {:query-id "q-1" :query stub-query}
(select-keys (:structured-output result) [:query-id :query])))))))
-(deftest chart-tools-errors-test
- (testing "a missing query goes back to the agent as output"
- (is (re-find #"^Query not found with ID: q-9"
- (:output (binding [shared/*memory-atom* (atom {:state {:queries {}}})]
- (charts/create-chart-tool {:data_source {:query_id "q-9"}
- :viz_settings {:chart_type "bar"}
- :title "Orders by month"
- :description "Monthly count of orders."}))))))
- (testing "an unexpected error propagates to the agent loop"
+(defn- invoke
+ "Call the tool `tool-name` with `args` the way the agent loop does, in a conversation whose state is `state`. The
+ profile offers both chart tools and `construct_notebook_query`, which the recovery steps name. Returns the result,
+ or `{:output <the text the model reads> :error {:class :code}}` when the call fails."
+ [tool-name args state]
+ (let [entries (binding [scope/*current-user-scope* api-scope/unrestricted]
+ (metabot.tools/->entries [#'metabot.tools/create-chart-tool
+ #'metabot.tools/edit-chart-tool
+ #'metabot.tools/construct-notebook-query-tool]
+ (atom {:state state})
+ nil
+ :internal))]
+ ((:fn (get entries tool-name)) args)))
+
+(def ^:private chart-args
+ {:title "Orders by month" :description "Monthly count of orders."})
+
+(deftest create-chart-unknown-query-id-test
+ (testing "a query ID that the conversation doesn't hold fails, and the model reads the query IDs that it does hold"
+ (is (= {:output (str "Query \"q-9\" was not found. Available query IDs: \"q-1\".\n"
+ "Use an available query ID from this conversation.")
+ :error {:class :recoverable :code ::recoverable.common/unknown-query-id}}
+ (invoke "create_chart"
+ (assoc chart-args :data_source {:query_id "q-9"} :viz_settings {:chart_type "bar"})
+ {:queries {"q-1" stub-query}})))))
+
+(deftest edit-chart-unknown-chart-id-test
+ (let [chart (fn [chart-id] {:chart_id chart-id :query_id "q-1" :queries [stub-query]})
+ args (assoc chart-args :chart_id "c-9" :new_viz_settings {:chart_type "pie"})
+ steps ["To make a new chart, call `construct_notebook_query` and use the chart ID that it returns."
+ "To chart a query from this conversation, call `create_chart` with its query ID."]]
+ (testing "a chart ID that the conversation doesn't hold fails, and the model reads the chart IDs that it does hold"
+ (is (= {:output (str/join "\n" (cons (str "No chart with ID \"c-9\" exists in this conversation."
+ " The available chart IDs are c-1, c-2.")
+ steps))
+ :error {:class :recoverable :code ::recoverable.common/unknown-chart-id}}
+ (invoke "edit_chart" args {:charts {"c-2" (chart "c-2") "c-1" (chart "c-1")}}))))
+ (testing "in a conversation without charts, the model reads how to make one"
+ (is (= {:output (str/join "\n" (cons (str "No chart with ID \"c-9\" exists in this conversation."
+ " No charts have been created in this conversation yet.")
+ steps))
+ :error {:class :recoverable :code ::recoverable.common/unknown-chart-id}}
+ (invoke "edit_chart" args {}))))))
+
+(deftest edit-chart-unknown-chart-id-keeps-recovery-steps-test
+ (let [steps (str "To make a new chart, call `construct_notebook_query` and use the chart ID that it returns.\n"
+ "To chart a query from this conversation, call `create_chart` with its query ID.")
+ charts (fn [ids]
+ {:charts (into {} (map (fn [id] [id {:chart_id id :query_id "q-1" :queries [stub-query]}])) ids)})
+ uuids (map #(format "%08d-0000-4000-8000-000000000000" %) (range 120))
+ long-id (apply str (repeat 4100 "x"))
+ output (fn [chart-id state]
+ (:output (invoke "edit_chart"
+ (assoc chart-args :chart_id chart-id :new_viz_settings {:chart_type "pie"})
+ state)))]
+ (testing "with more than 30 charts, the error lists the first 30 and counts the rest"
+ (is (= (str "No chart with ID \"c-9\" exists in this conversation. The available chart IDs are "
+ (str/join ", " (take 30 uuids)) " and 90 more.\n"
+ steps)
+ (output "c-9" (charts uuids)))))
+ (testing "a long chart ID is cut to its first 100 characters"
+ (is (= (str "No chart with ID \"" (subs long-id 0 100) "\" exists in this conversation."
+ " No charts have been created in this conversation yet.\n"
+ steps)
+ (output long-id {}))))
+ (testing "the recovery steps still fit when every chart ID is long"
+ (is (str/ends-with? (output long-id (charts (map #(str % long-id) (range 120))))
+ (str " and 90 more.\n" steps))))))
+
+(deftest construct-notebook-query-unknown-chart-type-test
+ (testing "construct_notebook_query refuses a chart type that Metabot can't create before it runs the query"
+ (let [ran? (atom false)]
+ (mt/with-dynamic-fn-redefs [construct/execute-representations-query (fn [& _] (reset! ran? true))]
+ (is (= {:output (str "Invalid tool arguments: `visualization` should be either \"table\", \"bar\", \"line\", "
+ "\"pie\", \"sunburst\", \"treemap\", \"boxplot\", \"area\", \"combo\", \"row\", \"pivot\", "
+ "\"scatter\", \"waterfall\", \"sankey\", \"scalar\", \"smartscalar\", \"gauge\", \"progress\", "
+ "\"funnel\", \"object\" or \"map\".")
+ :error {:class :validation :code :invalid-arguments}}
+ (invoke "construct_notebook_query"
+ (assoc chart-args
+ :query {:lib/type "mbql/query" :stages []}
+ :visualization {:chart_type "histogram"})
+ {}))))
+ (is (false? @ran?)))))
+
+(deftest chart-tools-undeclared-failure-test
+ (testing "a failure that no error declares ends the turn instead of reaching the model"
(mt/with-dynamic-fn-redefs [create-chart-tools/create-chart (fn [_] (throw (ex-info "boom" {})))
edit-chart-tools/edit-chart (fn [_] (throw (ex-info "boom" {})))]
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom" (run-create-chart)))
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom"
- (charts/edit-chart-tool {:chart_id "c-1"
- :new_viz_settings {:chart_type "pie"}
- :title "Orders by month"
- :description "Monthly count of orders."}))))))
+ (is (=? {:error {:class :unrecoverable :code :internal}}
+ (invoke "create_chart"
+ (assoc chart-args :data_source {:query_id "q-1"} :viz_settings {:chart_type "bar"})
+ {:queries {"q-1" stub-query}})))
+ (is (=? {:error {:class :unrecoverable :code :internal}}
+ (invoke "edit_chart"
+ (assoc chart-args :chart_id "c-1" :new_viz_settings {:chart_type "pie"})
+ {}))))))
diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.clj
index 92746705d7b..d87af57977e 100644
--- a/test/metabase/metabot/tools_test.clj
+++ b/test/metabase/metabot/tools_test.clj
@@ -343,9 +343,9 @@
(is (some? (:schema entry)))
(is (fn? (:fn entry))))))))
(testing "an unconverted tool's schema comes from its var metadata"
- (let [entries (agent-tools/->entries [#'agent-tools/create-chart-tool] (atom {}) nil :nlq)
- [_:=> [_:cat declared] _out] (:schema (get entries "create_chart"))
- [_:=> [_:cat original] _out] (:schema (meta #'agent-tools/create-chart-tool))]
+ (let [entries (agent-tools/->entries [#'agent-tools/list-snippets-tool] (atom {}) nil :nlq)
+ [_:=> [_:cat declared] _out] (:schema (get entries "list_snippets"))
+ [_:=> [_:cat original] _out] (:schema (meta #'agent-tools/list-snippets-tool))]
(is (= original declared))))
(testing "a converted tool and an unconverted one sit side by side"
(let [entries (agent-tools/->entries [#'agent-tools/list-timelines-tool ; mu/defn var
@@ -360,12 +360,10 @@
(deftest tool-schemas-exclude-state-keys-test
(testing "create_chart schema does not expose state keys"
- (let [{:keys [schema]} (meta #'agent-tools/create-chart-tool)
- [_:=> [_:cat params] _out] schema]
+ (let [params (:args (tools.legacy/declaration-of #'agent-tools/create-chart-tool))]
(is (not-any? #(= :charts_state (first %)) (rest params)))))
(testing "edit_chart schema does not expose state keys"
- (let [{:keys [schema]} (meta #'agent-tools/edit-chart-tool)
- [_:=> [_:cat params] _out] schema]
+ (let [params (:args (tools.legacy/declaration-of #'agent-tools/edit-chart-tool))]
(is (not-any? #(= :queries_state (first %)) (rest params)))))
(doseq [tool [agent-tools/create-sql-query-tool agent-tools/create-sql-query-code-edit-tool
agent-tools/edit-sql-query-tool agent-tools/replace-sql-query-tool]]