Skip to content

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

Resolve emitted field IDs and check card types in Metabot tools

Andrei Simionescu4f99661de7e4
diff --git a/src/metabase/metabot/tools/autogen_dashboard.clj b/src/metabase/metabot/tools/autogen_dashboard.clj
index a179a6f5b20..df1163e0a7d 100644
--- a/src/metabase/metabot/tools/autogen_dashboard.clj
+++ b/src/metabase/metabot/tools/autogen_dashboard.clj
@@ -13,6 +13,7 @@
[metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.recoverable.common :as recoverable.common]
[metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.util :as tools.u]
[metabase.util.log :as log]
[metabase.util.malli.registry :as mr]
[metabase.xrays.core :as xrays]))
@@ -64,9 +65,9 @@
is curated when the Metabot is curated-only."
[kind id]
(let [entity (tools/with-entity {:kind kind :id id}
- (api/read-check (if (= :table kind)
- (metabot.db/table id)
- (metabot.db/card-of-type id kind))))]
+ (if (= :table kind)
+ (api/read-check (metabot.db/table id))
+ (tools.u/get-card id kind)))]
(when (or (:archived entity) (false? (:active entity)))
(unavailable-source! {:kind kind :id id}))
(when (shared/curated-only?)
diff --git a/src/metabase/metabot/tools/entity_details.clj b/src/metabase/metabot/tools/entity_details.clj
index 81cea1c54d0..ed30fb08d60 100644
--- a/src/metabase/metabot/tools/entity_details.clj
+++ b/src/metabase/metabot/tools/entity_details.clj
@@ -10,7 +10,6 @@
[metabase.lib.types.isa :as lib.types.isa]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.metadata-perms :as metabot.perms]
- [metabase.metabot.tools.shared :as shared]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.metabot.tools.util :as metabot.tools.u]
[metabase.models.interface :as mi]
@@ -237,38 +236,10 @@
(map #(nth % 2)))
(tree-seq coll? seq x)))
-(defn- metric-join-required-dimensions
- "Dimensions a metric reaches through an EXPLICIT join with no database foreign key.
-
- A metric's `queryable-dimensions` are computed from a *consumer-framed* query (`source-table` +
- `[:metric …]`), so `filterable-columns` only surfaces the base table and FK-reachable tables — the
- metric's FK-less join dimensions are silently dropped. But the metric's *definition* joins that
- table, so a consumer query that re-adds the same join can group/filter by them. Left unsurfaced,
- the LLM guesses at those columns and dead-ends on `:no-fk-path` (BOT-1612).
-
- We surface them here, grouped per join. Each entry carries the portable join clause to paste into
- `joins:` AND, per dimension, a `:reference` — the alias-qualified field clause
- (`[\"field\" {\"join-alias\" …} [db schema table field]]`) the LLM must reference the column by.
- The bare portable FK does NOT resolve for these columns (there is no FK path — that is the whole
- premise), so only the alias-qualified clause is pasteable. Detection is cheap — the definition
- query's joins are already in hand; a `:fk-field-id` of nil marks an explicit, non-FK join. Returns
- nil when the metric has no such joins (the common case).
-
- Fail closed: a join is surfaced only when BOTH its portable clause exported and it targets a real
- table. These dimensions are unreachable without the join by definition, so an entry lacking the
- clause has zero value — and instructing the LLM to paste a nil/`null` join would be strictly worse
- than the `:no-fk-path` dead-end this feature replaces. A `:source-card`-based join (nil
- `source-table`) is likewise dropped rather than mislabeled with a nil table name. Dimensions are
- permission-filtered on the way out (see `permission-filter-columns`) — the join target is a table
- the consumer query never reaches, so nothing else has checked the user can read it.
-
- The join-key columns (fields referenced in the join `:conditions`) are excluded — the target-side
- key equals a base-table column and is redundant groupable noise. A metric that self-joins one
- table under two aliases yields two entries with the same columns but DIFFERENT `:join_alias`, and
- therefore distinct (correct) references — one per alias — which is the intended behavior.
-
- `base-reachable-ids` is the id set of the consumer-framed `filterable-columns` (already computed by
- the caller); columns in it are reachable without the join and are not re-surfaced here."
+(defn- metric-join-required-columns
+ "Return accessible dimensions grouped by the explicit joins needed to use them.
+
+ Exclude join keys and columns already reachable without the join. Only include joins that can be exported."
[metadata-provider defq base-reachable-ids]
(when defq
(let [explicit-joins (filter #(nil? (:fk-field-id %)) (lib/joins defq -1))]
@@ -295,16 +266,9 @@
key-ids (clause-field-ids (:conditions jn))
dims (when target-tid
(into []
- (comp (filter #(and (= target-tid (:table-id %))
- (not (base-reachable-ids (:id %)))
- (not (key-ids (:id %)))))
- (map #(metabot.tools.u/->result-column defq %))
- (map (fn [rc]
- (cond-> rc
- (vector? (:portable_fk rc))
- (assoc :reference
- ["field" {"join-alias" alias}
- (:portable_fk rc)])))))
+ (filter #(and (= target-tid (:table-id %))
+ (not (base-reachable-ids (:id %)))
+ (not (key-ids (:id %)))))
def-cols))]
(when (and portable target-tid (seq dims))
{:join_alias alias
@@ -313,6 +277,28 @@
:dimensions dims}))))
explicit-joins)))))))
+(defn- metric-join-required-dimensions
+ [metadata-provider defq base-reachable-ids]
+ (not-empty
+ (mapv (fn [{join-alias :join_alias :as join}]
+ (update join :dimensions
+ (partial mapv (fn [column]
+ (let [rc (metabot.tools.u/->result-column defq column)]
+ (cond-> rc
+ (vector? (:portable_fk rc))
+ (assoc :reference ["field" {"join-alias" join-alias} (:portable_fk rc)])))))))
+ (metric-join-required-columns metadata-provider defq base-reachable-ids))))
+
+(defn metric-dimension-columns
+ "Return the accessible columns listed as a metric's dimensions, including dimensions requiring explicit joins."
+ [query metric-id]
+ (let [base-columns (permission-filter-columns (lib/filterable-columns query))]
+ (concat base-columns
+ (lazy-seq
+ (let [mp (lib.metadata/->metadata-provider query)
+ definition (some->> (lib.metadata/card mp metric-id) :dataset-query (lib/query mp))]
+ (mapcat :dimensions (metric-join-required-columns mp definition (set (keep :id base-columns)))))))))
+
(defn- card-read-verdicts-for-metrics
"Return read permissions for the source cards required by `metric-cards`, or nil when none are required.
@@ -343,7 +329,7 @@
"Get metric details as returned by tools."
([id] (metric-details id nil))
([id options]
- (when-let [card (metabot.tools.u/get-card id)]
+ (when-let [card (metabot.tools.u/get-card id :metric)]
(metric-details card (lib-be/application-database-metadata-provider (:database_id card)) options)))
([card metadata-provider options]
(metric-details card metadata-provider options nil))
@@ -355,6 +341,7 @@
with-queryable-dimensions? true
with-segments? false}}
card-read-verdicts]
+ (metabot.tools.u/check-card-type card :metric)
(let [id (:id card)
;; Database metadata so the LLM can build portable FKs — metrics are always
;; referenced from a query targeting a specific database, and the LLM needs the
@@ -399,9 +386,7 @@
;; Definition query (WITH the metric's own joins) — surfaces FK-less join dimensions that
;; the consumer-framed `base-query` drops. Built from the card's `:dataset-query`, not from
;; `metric-query`, which frames the metric as a source and hides its definition joins.
- ;; Only a metric's definition joins are dimensions to offer; a question or model read through the
- ;; metric path exposes its results, as through its own URI, not the tables its definition reads.
- join-required-dims (when (and query-needed? with-queryable-dimensions? (= :metric (:type metric-card)))
+ join-required-dims (when (and query-needed? with-queryable-dimensions?)
(metric-join-required-dimensions
metadata-provider
(some->> (:dataset-query metric-card) (lib/query metadata-provider))
@@ -425,9 +410,7 @@
;; `[metric, {}, <entity_id>]` aggregation clause to reference this metric.
:portable_entity_id (metabot.tools.u/metric-card-shape-get card :entity-id :entity_id)
:verified (verified-review? id "card")}
- ;; Under curated-only, only a metric names what it reads: a question or model read through this path
- ;; exposes its results, as through its own URI, not the table or card its definition reads.
- (or (not (shared/curated-only?)) (= :metric (:type metric-card)))
+ true
(merge (metabot.tools.u/metric-source-fields
{:source-card (when source-card
{:id (:id source-card)
@@ -634,7 +617,7 @@
(when-not (int? id)
(throw (ex-info "Invalid card id format"
{:agent-error? true :status-code 400})))
- (when-let [card (metabot.tools.u/get-card id)]
+ (when-let [card (metabot.tools.u/get-card id (:expected-type options))]
(card-details card (lib-be/application-database-metadata-provider (:database_id card)) options)))
([base metadata-provider {:keys [field-values-fn with-fields? with-related-tables? with-metrics?
with-measures? with-segments?]
@@ -792,20 +775,8 @@
(metabot.perms/with-cache
(let [options (arguments->options arguments)
details (cond
- (= :model entity-type)
- (let [card (card-details entity-id (assoc options :only-model true))]
- (if (= :model (:type card))
- card
- (throw (ex-info (format "ID %s is not a valid model id, it's a question" entity-id)
- {:agent-error? true :status-code 400}))))
-
- (= :question entity-type)
- (let [card (card-details entity-id options)]
- (if (= :question (:type card))
- card
- (throw (ex-info (format "ID %s is not a valid question id, it's a %s"
- entity-id (:type card))
- {:agent-error? true :status-code 400}))))
+ (#{:model :question} entity-type)
+ (card-details entity-id (assoc options :expected-type entity-type))
(and (= :table entity-type)
(int? entity-id))
diff --git a/src/metabase/metabot/tools/field_stats.clj b/src/metabase/metabot/tools/field_stats.clj
index e39cc657527..8da084208bc 100644
--- a/src/metabase/metabot/tools/field_stats.clj
+++ b/src/metabase/metabot/tools/field_stats.clj
@@ -129,12 +129,11 @@
(defn- field-column
[entity-type entity-id field-id columns]
- (let [columns (entity-details/permission-filter-columns columns)]
- (or (metabot.tools.u/find-column-by-field-id field-id columns)
- (let [available-ids (into [] (comp (map metabot.tools.u/column-field-id) (distinct)) columns)]
- (unknown-field! {:field-id field-id :entity-type entity-type :entity-id entity-id
- :available-ids (vec (take max-suggested-field-ids available-ids))
- :remaining-count (max 0 (- (count available-ids) max-suggested-field-ids))})))))
+ (or (metabot.tools.u/find-column-by-field-id field-id columns)
+ (let [available-ids (into [] (comp (map metabot.tools.u/column-field-id) (distinct)) columns)]
+ (unknown-field! {:field-id field-id :entity-type entity-type :entity-id entity-id
+ :available-ids (vec (take max-suggested-field-ids available-ids))
+ :remaining-count (max 0 (- (count available-ids) max-suggested-field-ids))}))))
(defn- check-curated-field-owner!
"Under [[shared/curated-only?]], require that the Field behind `col`, a result column of the card `card-id`, belongs
@@ -151,7 +150,8 @@
(defn- table-field-stats
[table-id field-id limit]
(let [query (metabot.tools.u/table-query table-id)
- col (field-column "table" table-id field-id (lib/visible-columns query))]
+ columns (entity-details/permission-filter-columns (lib/visible-columns query))
+ col (field-column "table" table-id field-id columns)]
(field-metadata-output query col field-id limit)))
(defn- card-field-stats
@@ -166,14 +166,14 @@
columns (if (= entity-type "question")
(lib/returned-columns query)
(distinct (concat (lib/returned-columns query) (lib/visible-columns query))))
- col (field-column entity-type card-id field-id columns)]
+ col (field-column entity-type card-id field-id (entity-details/permission-filter-columns columns))]
(check-curated-field-owner! card-id col)
(field-metadata-output query col field-id limit)))
(defn- metric-field-stats
[metric-id field-id limit]
(let [query (metabot.tools.u/metric-query metric-id)
- col (field-column "metric" metric-id field-id (lib/filterable-columns query))]
+ col (field-column "metric" metric-id field-id (entity-details/metric-dimension-columns query metric-id))]
(check-curated-field-owner! metric-id col)
(field-metadata-output query col field-id limit)))
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index e6f7097c818..aec34f13989 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -85,6 +85,7 @@
[metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.metabot.tools.shared.instructions :as instructions]
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
+ [metabase.metabot.tools.util :as tools.u]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
[metabase.transforms.core :as transforms]
@@ -245,28 +246,6 @@
:text (str "Call `search` and build the URI from a result's numeric `id`"
" attribute, or copy its `uri` attribute as-is.")}]))})
-(tools/defrecoverable not-a-metric!
- "Report a metric URI that refers to a question or model."
- {:payload [:map {:closed true}
- [:id :int]
- [:type [:enum "question" "model"]]
- [:card-uri :string]]}
- [{:keys [id type card-uri]}]
- {:message (str "Card " id " is a " type ", not a metric.")
- :recovery [{:uses #{}
- :text (str "Read `" (uri-preview card-uri) "` instead.")}]})
-
-(tools/defrecoverable wrong-card-kind!
- "Report a question or model URI that names a different kind of card."
- {:payload [:map {:closed true}
- [:id :int]
- [:expected [:enum "question" "model"]]
- [:actual [:enum "question" "model" "metric"]]
- [:uri :string]]}
- [{:keys [id expected actual uri]}]
- {:message (str "Card " id " is a " actual ", not a " expected ".")
- :recovery [{:uses #{} :text (str "Read `" (uri-preview uri) "` instead.")}]})
-
(tools/defrecoverable inactive-table!
"Report a readable table that is no longer active."
{:payload [:map {:closed true} [:id :int] [:database-id :int]]}
@@ -761,12 +740,14 @@
(defn- check-card-kind [id expected & path-segments]
(let [{actual :type :as card} (check-card-resource-database id)]
(when-not (= actual expected)
- (let [path-segments (if (= actual :metric)
- (when (= (first path-segments) "fields")
- (cons "dimensions" (rest path-segments)))
- path-segments)]
- (wrong-card-kind! {:id id :expected (name expected) :actual (name actual)
- :uri (apply llm-shape/metabase-uri actual id path-segments)})))
+ (let [[aspect & more] path-segments
+ path-segments (cond
+ (= actual :metric) (when (= aspect "fields") (cons "dimensions" more))
+ (= aspect "dimensions") (cons "fields" more)
+ :else path-segments)]
+ (tools.u/wrong-source-type!
+ {:id id :expected-type expected :actual-type actual
+ :card-uri (uri-preview (apply llm-shape/metabase-uri actual id path-segments))})))
card))
(defn- usable-metrics
@@ -882,34 +863,23 @@
;; ----- Metric -----
-(defn- check-metric-resource
- "Require that `metric-id` is a metric whose backing database is addressable as a Metabot resource.
-
- The error for a question or model includes its resource URI with `path-segments` appended."
- [metric-id & path-segments]
- (let [{card-type :type} (check-card-resource-database metric-id)]
- (when-not (= :metric card-type)
- (not-a-metric! {:id metric-id
- :type (name card-type)
- :card-uri (apply llm-shape/metabase-uri card-type metric-id path-segments)}))))
-
(defn- fetch-metric [id-str]
(let [metric-id (parse-long id-str)]
- (check-metric-resource metric-id)
+ (check-card-kind metric-id :metric)
(entity-details/get-metric-details {:metric-id metric-id
:with-queryable-dimensions? false
:with-field-values? false})))
(defn- fetch-metric-dimensions [id-str]
(let [metric-id (parse-long id-str)]
- (check-metric-resource metric-id "fields")
+ (check-card-kind metric-id :metric "dimensions")
(entity-details/get-metric-details {:metric-id metric-id
:with-queryable-dimensions? true
:with-field-values? false})))
(defn- fetch-metric-dimension [id-str dim-id]
(let [metric-id (parse-long id-str)]
- (check-metric-resource metric-id "fields" dim-id)
+ (check-card-kind metric-id :metric "dimensions" dim-id)
(field-values {:entity-type "metric"
:entity-id metric-id
:field-id dim-id
diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.clj
index 13bed5e65a5..017d19fc697 100644
--- a/src/metabase/metabot/tools/util.clj
+++ b/src/metabase/metabot/tools/util.clj
@@ -9,16 +9,18 @@
[metabase.lib.util :as lib.util]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.tools.core :as tools]
+ [metabase.metabot.tools.error :as tools.error]
[metabase.models.interface :as mi]
[metabase.util :as u]
[metabase.util.log :as log]))
(defn handle-agent-error
- "Return an agent output for agent errors, re-throw `e` otherwise.
- Preserves :status-code and :terminal-error? from ex-data."
+ "Return an output for legacy agent errors, rethrowing declared tool errors and other exceptions.
+
+ Preserve `:status-code` and `:terminal-error?` from legacy errors."
[e]
(let [{:keys [agent-error? status-code terminal-error?]} (ex-data e)]
- (if agent-error?
+ (if (and agent-error? (not (contains? (ex-data e) tools.error/error-key)))
(cond-> {:output (ex-message e)}
status-code (assoc :status-code status-code)
terminal-error? (assoc :terminal-error? true))
@@ -173,11 +175,30 @@
{:payload [:map {:closed true}
[:id :int]
[:expected-type [:enum :model :question :metric]]
- [:actual-type [:enum :model :metric :question]]]}
- [{:keys [id expected-type actual-type]}]
- {:message (str (if (= expected-type :metric) "Card " "ID ")
- id " is a " (name actual-type) ", not a " (name expected-type) ".")
- :recovery [{:text (str "Retry using a " (name expected-type) " ID.") :uses #{}}]})
+ [:actual-type [:enum :model :metric :question]]
+ [:card-uri {:optional true} :string]]}
+ [{:keys [id expected-type actual-type card-uri]}]
+ {:message (str "Card " id " is a " (name actual-type) ", not a " (name expected-type) ".")
+ :recovery (if card-uri
+ [{:text (str "Read `" card-uri "` instead.") :uses #{}}]
+ [{:text (str "Retry using a " (name expected-type) " ID.") :uses #{}}
+ {:text (str "Read `metabase://" (name actual-type) "/" id "` instead.")
+ :uses #{"read_resource"}}
+ {:text (str "Set `data_source` to \"" (name actual-type) "\" in `get_field_values` for this card.")
+ :uses #{"get_field_values"}}
+ {:text (str "Pass this ID as `source."
+ (case actual-type :model "model_id" :metric "metric_id" :question "report_id")
+ "` to `create_autogenerated_dashboard`.")
+ :uses #{"create_autogenerated_dashboard"}}])})
+
+(defn check-card-type
+ "Return `card` if it has `expected-type`, or throw a recoverable error with its actual type.
+
+ The caller must check read access first."
+ [card expected-type]
+ (when-not (= expected-type (:type card))
+ (wrong-source-type! {:id (:id card) :expected-type expected-type :actual-type (:type card)}))
+ card)
(defn get-card
"Retrieve a readable card by ID, optionally requiring a particular card type."
@@ -185,10 +206,8 @@
(-> (metabot.db/card id)
api/read-check))
([id expected-type]
- (let [card (get-card id)]
- (when-not (= expected-type (:type card))
- (wrong-source-type! {:id id :expected-type expected-type :actual-type (:type card)}))
- card)))
+ (cond-> (get-card id)
+ expected-type (check-card-type expected-type))))
(defn get-card-by-entity-id
"Retrieve a readable card by its 21-char NanoID `entity_id`, or `nil` if not found. Used by
diff --git a/test/metabase/metabot/tools/autogen_dashboard_test.clj b/test/metabase/metabot/tools/autogen_dashboard_test.clj
index 404a4b44388..29ad4766b00 100644
--- a/test/metabase/metabot/tools/autogen_dashboard_test.clj
+++ b/test/metabase/metabot/tools/autogen_dashboard_test.clj
@@ -313,3 +313,16 @@
(mt/with-dynamic-fn-redefs [metabot.db/table (fn [_] (throw (ex-info "boom" {})))]
(is (= {:class :unrecoverable :code :internal}
(:error (invoke {:table_id 1})))))))
+
+(deftest card-source-types-name-the-real-kind-test
+ (mt/as-admin
+ (doseq [[actual-type correct-argument] [[:model :model_id] [:question :report_id] [:metric :metric_id]]]
+ (mt/with-temp [:model/Card {card-id :id} {:type actual-type}]
+ (doseq [[expected-type argument] [[:model :model_id] [:question :report_id] [:metric :metric_id]]
+ :when (not= actual-type expected-type)]
+ (is (= (failure :metabase.metabot.tools.util/wrong-source-type
+ (str "Card " card-id " is a " (name actual-type) ", not a " (name expected-type) ".")
+ (str "Retry using a " (name expected-type) " ID.")
+ (str "Pass this ID as `source." (name correct-argument)
+ "` to `create_autogenerated_dashboard`."))
+ (invoke {argument card-id}))))))))
diff --git a/test/metabase/metabot/tools/curated_content_test.clj b/test/metabase/metabot/tools/curated_content_test.clj
index e810db5d9b1..1fc48b90a23 100644
--- a/test/metabase/metabot/tools/curated_content_test.clj
+++ b/test/metabase/metabot/tools/curated_content_test.clj
@@ -1282,6 +1282,22 @@
(is (= (mt/id :reviews) (get-in resource [:content :structured-output :base_table_id]))
"a metric's base table is its source"))))))))
+(deftest curated-metric-over-a-deleted-source-is-marked-unavailable-test
+ (testing "a curated metric whose source card was deleted is marked unavailable"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Card {question :id} {:name "doomed question" :dataset_query (orders-query)}
+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]
+ (let [mp (mt/metadata-provider)]
+ (mt/with-temp [:model/Card {metric :id} {:type :metric
+ :name "metric on a doomed question"
+ :dataset_query (count-metric-query
+ (lib/query mp (lib.metadata/card mp question)))}]
+ (verify-card! metric)
+ (t2/delete! :model/Card :id question)
+ (let [resource (first (read-uris metabot :internal (str "metabase://metric/" metric)))]
+ (is (nil? (:error resource)) (:error resource))
+ (is (true? (get-in resource [:content :structured-output :source_unavailable]))))))))))
+
(deftest uncurated-metric-over-an-unreadable-table-fails-closed-test
;; the user reads the published table through the Library grant, which exists only on EE
(when config/ee-available?
diff --git a/test/metabase/metabot/tools/entity_details_test.clj b/test/metabase/metabot/tools/entity_details_test.clj
index 776dcb9e5a7..7a97888c99c 100644
--- a/test/metabase/metabot/tools/entity_details_test.clj
+++ b/test/metabase/metabot/tools/entity_details_test.clj
@@ -8,6 +8,7 @@
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.metabot.tools.entity-details :as entity-details]
+ [metabase.metabot.tools.error :as tools.error]
[metabase.parameters.field-values :as params.field-values]
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
@@ -1296,3 +1297,20 @@
:with-field-values? false}))]
(is (nil? (:queryable-dimensions out)))
(is (nil? (:join-required-dimensions out)))))))))))
+
+(deftest card-details-check-types-before-reading-metadata-test
+ (mt/as-admin
+ (doseq [actual-type [:model :question :metric]]
+ (mt/with-temp [:model/Card {card-id :id} {:type actual-type}]
+ (doseq [expected-type [:model :question :metric]
+ :when (not= actual-type expected-type)]
+ (is (=? {:class :recoverable
+ :code :metabase.metabot.tools.util/wrong-source-type
+ :message (str "Card " card-id " is a " (name actual-type)
+ ", not a " (name expected-type) ".")}
+ (tools.error/classify
+ (is (thrown? clojure.lang.ExceptionInfo
+ (if (= expected-type :metric)
+ (entity-details/get-metric-details {:metric-id card-id})
+ (entity-details/get-table-details
+ {:entity-type expected-type :entity-id card-id}))))))))))))
diff --git a/test/metabase/metabot/tools/field_stats_test.clj b/test/metabase/metabot/tools/field_stats_test.clj
index 214f2321eea..1273486e7ad 100644
--- a/test/metabase/metabot/tools/field_stats_test.clj
+++ b/test/metabase/metabot/tools/field_stats_test.clj
@@ -179,7 +179,16 @@
(is (=? {:class :recoverable
:code :metabase.metabot.tools.util/wrong-source-type
:message (str "Card " card-id " is a " card-type ", not a metric.")
- :recovery [{:text "Retry using a metric ID." :uses #{}}]}
+ :recovery [{:text "Retry using a metric ID." :uses #{}}
+ {:text (str "Read `metabase://" card-type "/" card-id "` instead.")
+ :uses #{"read_resource"}}
+ {:text (str "Set `data_source` to \"" card-type
+ "\" in `get_field_values` for this card.")
+ :uses #{"get_field_values"}}
+ {:text (str "Pass this ID as `source."
+ (if (= card-type "model") "model_id" "report_id")
+ "` to `create_autogenerated_dashboard`.")
+ :uses #{"create_autogenerated_dashboard"}}]}
(tools.error/classify
(is (thrown? clojure.lang.ExceptionInfo (metric-values card-id))))))))
(testing "a metric that doesn't exist is refused as not found"
diff --git a/test/metabase/metabot/tools/metadata_test.clj b/test/metabase/metabot/tools/metadata_test.clj
index 3ea4ef031ff..fef06138c88 100644
--- a/test/metabase/metabot/tools/metadata_test.clj
+++ b/test/metabase/metabot/tools/metadata_test.clj
@@ -260,9 +260,9 @@
[metric-id "metric" "model"]
[model-id "model" "metric"]
[question-id "question" "metric"]]
- :let [message (str (if (= expected-type "metric") "Card " "ID ")
- source-id " is a " actual-type ", not a " expected-type "."
- "\nRetry using a " expected-type " ID.")]]
+ :let [message (str "Card " source-id " is a " actual-type ", not a " expected-type "."
+ "\nRetry using a " expected-type " ID."
+ "\nSet `data_source` to \"" actual-type "\" in `get_field_values` for this card.")]]
(let [result (invoke-metadata profile "list_available_fields"
(assoc {:table_ids [(mt/id :products)] :model_ids [] :metric_ids []}
(keyword (str expected-type "_ids")) [source-id]))]
@@ -277,7 +277,8 @@
(doseq [profile [:document-generate-content :slackbot]
[source-id actual-type] [[question-id "question"] [model-id "model"]]]
(is (= {:error {:class :recoverable :code ::tools.u/wrong-source-type}
- :output (str "Card " source-id " is a " actual-type ", not a metric.\nRetry using a metric ID.")}
+ :output (str "Card " source-id " is a " actual-type ", not a metric.\nRetry using a metric ID."
+ "\nSet `data_source` to \"" actual-type "\" in `get_field_values` for this card.")}
(invoke-metadata profile "get_field_values"
{:data_source "metric" :source_id source-id :field_id (mt/id :orders :id)})))))))
@@ -365,14 +366,14 @@
:metabase.metabot.tools.core/all-items-failed
::tools.u/wrong-source-type)} error))
(is (str/includes? (or output "")
- (if (= profile :internal)
- (str "Card " source-id " is a " actual-type ", not a " expected-type "."
- "\nRead `metabase://" actual-type "/" source-id
- (if (= actual-type "metric") "/dimensions/" "/fields/")
- (mt/id :orders :id) "` instead.")
- (str (if (= expected-type "metric") "Card " "ID ")
- source-id " is a " actual-type ", not a " expected-type "."
- "\nRetry using a " expected-type " ID."))))))))))
+ (str "Card " source-id " is a " actual-type ", not a " expected-type "."
+ (if (= profile :internal)
+ (str "\nRead `metabase://" actual-type "/" source-id
+ (if (= actual-type "metric") "/dimensions/" "/fields/")
+ (mt/id :orders :id) "` instead.")
+ (str "\nRetry using a " expected-type " ID."
+ "\nSet `data_source` to \"" actual-type
+ "\" in `get_field_values` for this card.")))))))))))
(deftest question-field-details-use-returned-columns-test
(mt/as-admin
@@ -463,3 +464,48 @@
:result_metadata columns}]
(t2/delete! :model/Card :id inner-id)
(check-unrestricted-statistics outer-id statistics))))
+
+(deftest joined-metric-dimensions-round-trip-test
+ (mt/as-admin
+ (let [mp (mt/metadata-provider)
+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))
+ (lib/join (lib/join-clause
+ (lib.metadata/table mp (mt/id :reviews))
+ [(lib/= (lib.metadata/field mp (mt/id :orders :product_id))
+ (lib.metadata/field mp (mt/id :reviews :product_id)))]))
+ (lib/aggregate (lib/count)))]
+ (mt/with-temp [:model/Card {metric-id :id} {:type :metric :dataset_query query}]
+ (let [dimensions (->> (entity-details/get-metric-details
+ {:metric-id metric-id :with-field-values? false})
+ :structured-output :join-required-dimensions (mapcat :dimensions))
+ valid-ids (mapv :field_id dimensions)]
+ (is (some #{(mt/id :reviews :rating)} valid-ids))
+ (doseq [profile [:document-generate-content :slackbot :internal]]
+ (doseq [field-id valid-ids]
+ (let [{:keys [error structured-output]} (invoke-field-detail profile "metric" metric-id field-id)]
+ (is (nil? error))
+ (is (=? {:field_id (if (= profile :internal) (str field-id) field-id)}
+ (if (= profile :internal) (first structured-output) structured-output)))))
+ (let [{:keys [output]} (invoke-field-detail profile "metric" metric-id "missing")]
+ (is (str/includes? output (str "Field missing was not found in metric " metric-id ".")))
+ (doseq [field-id valid-ids]
+ (is (str/includes? output (str field-id)))))))))))
+
+(deftest card-resource-types-name-the-real-kind-test
+ (mt/as-admin
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]
+ (doseq [actual-type [:model :question :metric]]
+ (mt/with-temp [:model/Card {card-id :id} {:type actual-type
+ :dataset_query (if (= actual-type :metric)
+ (lib/aggregate query (lib/count)) query)}]
+ (doseq [expected-type [:model :question :metric]
+ :when (not= expected-type actual-type)
+ path (if (= expected-type :metric) ["" "/dimensions"] ["" "/fields" "/sources"])]
+ (let [{:keys [error output]} (invoke-metadata :internal "read_resource"
+ {:uris [(str "metabase://" (name expected-type)
+ "/" card-id path)]})]
+ (is (= :recoverable (:class error)))
+ (is (str/includes? output (str "Card " card-id " is a " (name actual-type)
+ ", not a " (name expected-type) ".")))
+ (is (str/includes? output (str "metabase://" (name actual-type) "/" card-id))))))))))
diff --git a/test/metabase/metabot/tools/util_test.clj b/test/metabase/metabot/tools/util_test.clj
index eb64c4eea61..57e41467474 100644
--- a/test/metabase/metabot/tools/util_test.clj
+++ b/test/metabase/metabot/tools/util_test.clj
@@ -400,3 +400,10 @@
{:source_unavailable true}
{:database-name "Sample"})))
+
+(deftest declared-errors-keep-their-recovery-test
+ (let [error (try
+ (metabot.tools.util/wrong-source-type! {:id 1 :expected-type :metric :actual-type :question})
+ (catch clojure.lang.ExceptionInfo e e))]
+ (doseq [handle-error [metabot.tools.util/handle-agent-error metabot.tools.util/handle-agent-or-api-error]]
+ (is (identical? error (is (thrown? clojure.lang.ExceptionInfo (handle-error error))))))))