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
Support questions in `read_resource` (#72469)
lbrdnk2d50c382ae0f
diff --git a/enterprise/backend/test/metabase_enterprise/metabot/tools/entity_details_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/tools/entity_details_test.cljindex 0084f87c2a8..a8b18f17bf1 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/tools/entity_details_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/tools/entity_details_test.clj@@ -18,7 +18,7 @@(met/with-gtaps! {:gtaps {:categories {:query (sandboxed-query)}}}(let [field-id (mt/id :categories :name)](try- (let [result (entity-details/get-table-details {:table-id (mt/id :categories)})+ (let [result (entity-details/get-table-details {:entity-type :table :entity-id (mt/id :categories)})name-field (some #(when (= "NAME" (:name %)) %) (get-in result [:structured-output :fields]))](testing "returns sandboxed field values"(is (= ["African" "American"] (:field_values name-field)))))diff --git a/resources/metabot/prompts/llm_representations.selmer b/resources/metabot/prompts/llm_representations.selmerindex 76125eda88f..71b93086ce0 100644--- a/resources/metabot/prompts/llm_representations.selmer+++ b/resources/metabot/prompts/llm_representations.selmer@@ -188,6 +188,13 @@ The question is stored in the following collection:The question is visualized as follows:{{question_visualization_xml|safe}}{% endif %}+{% if question_fields_xml %}++### Fields++The following fields are returned by this question.+{{question_fields_xml|safe}}+{% endif %}</metabase_question>{% endif %}{% if is_text_card %}diff --git a/resources/metabot/prompts/tools/read_resource.md b/resources/metabot/prompts/tools/read_resource.mdindex 7604c042809..6d9db3e17d5 100644--- a/resources/metabot/prompts/tools/read_resource.md+++ b/resources/metabot/prompts/tools/read_resource.md@@ -24,6 +24,14 @@ You can request multiple resources in a single call by providing a list of URIs,- Want model structure (fields without value information)? → `metabase://model/456/fields`- Want detailed field information (sample values for format patterns)? → `metabase://model/456/fields/1`+## Question resources+- metabase://question/{id} - Get basic question info+- metabase://question/{id}/fields - Get question details with available fields+- metabase://question/{id}/fields/{field_id} - Get detailed field information with sample values and metadata++**Examples:**+- Want question structure (fields without value information)? → `metabase://question/456/fields`+- Want detailed field information (sample values for format patterns)? → `metabase://question/456/fields/1`## Metric resources- metabase://metric/{id} - Get basic metric infodiff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.cljindex 5a2251ad331..892d19a718f 100644--- a/src/metabase/agent_api/api.clj+++ b/src/metabase/agent_api/api.clj@@ -220,7 +220,8 @@[:with-segments {:optional true} [:maybe :boolean]]]](check-tool-result(entity-details/get-table-details- {:table-id id+ {:entity-type :table+ :entity-id id:with-fields? with-fields:with-field-values? with-field-values:with-related-tables? with-related-tablesdiff --git a/src/metabase/metabot/agent/user_context.clj b/src/metabase/metabot/agent/user_context.cljindex cfa156e3227..ef679483031 100644--- a/src/metabase/metabot/agent/user_context.clj+++ b/src/metabase/metabot/agent/user_context.clj@@ -140,7 +140,8 @@[entity](fetch-and-format entity"The user is currently looking at the rows of a table:"- #(entity-details/get-table-details {:table-id (:id entity)+ #(entity-details/get-table-details {:entity-type :table+ :entity-id (:id entity):with-field-values? false:with-metrics? false:with-measures? true@@ -151,7 +152,8 @@[entity](fetch-and-format entity"The user is currently looking at the rows of a model:"- #(entity-details/get-table-details {:model-id (:id entity)+ #(entity-details/get-table-details {:entity-type :model+ :entity-id (:id entity):with-field-values? false:with-metrics? false:with-measures? truediff --git a/src/metabase/metabot/tools/entity_details.clj b/src/metabase/metabot/tools/entity_details.cljindex a5247aec92e..b49ac98b8d4 100644--- a/src/metabase/metabot/tools/entity_details.clj+++ b/src/metabase/metabot/tools/entity_details.clj@@ -355,38 +355,47 @@{:metabot_id metabot-id, :status-code 400}))))(defn get-table-details- "Get information about the table or model with ID `table-id`.+ "Get information about the table, question or model with .`table-id` is string either encoding an integer that is the ID of a tableor a string containing the prefix card__ and the ID of a model (card) as suffix.Alternatively, `table-id` can be an integer ID of a table.`model-id` is an integer ID of a model (card). Exactly one of `table-id` or `model-id`should be supplied."- [{:keys [model-id table-id] :as arguments}]+ [{:keys [entity-type entity-id] :as arguments}](try(lib-be/with-metadata-provider-cache(let [options (cond-> arguments(= (:with-field-values? arguments) false) (assoc :field-values-fn identity))details (cond- (int? model-id)- (let [card (card-details model-id (assoc options :only-model true))]+ (= :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" model-id)+ (throw (ex-info (format "ID %s is not a valid model id, it's a question" entity-id){:agent-error? true :status-code 400}))))- (int? table-id)- (table-details table-id options)+ (= :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}))))++ (and (= :table entity-type)+ (int? entity-id))+ (table-details entity-id options)- (string? table-id)- (if-let [[_ card-id] (re-matches #"card__(\d+)" table-id)]+ (and (= :table entity-type)+ (string? entity-id))+ (if-let [[_ card-id] (re-matches #"card__(\d+)" entity-id)](card-details (parse-long card-id) options)- (if (re-matches #"\d+" table-id)- (table-details (parse-long table-id) options)+ (if (re-matches #"\d+" entity-id)+ (table-details (parse-long entity-id) options)(throw (ex-info "Invalid table_id format"{:agent-error? true :status-code 400})))):else- (throw (ex-info "Invalid arguments: must provide table_id or model_id"+ (throw (ex-info "Invalid arguments: must provide valid entity-type and entity-id"{:agent-error? true :status-code 400})))]{:structured-output (assoc details :result-type :entity)}))(catch Exception ediff --git a/src/metabase/metabot/tools/field_stats.clj b/src/metabase/metabot/tools/field_stats.cljindex 293092788c9..dcfd3cb4c56 100644--- a/src/metabase/metabot/tools/field_stats.clj+++ b/src/metabase/metabot/tools/field_stats.clj@@ -92,6 +92,6 @@"Return statistics and/or values for a given field of a given entity."[{:keys [entity-type entity-id field-id limit]}](case entity-type- "metric" (metric-field-stats entity-id field-id limit)- ("model" "report") (card-field-stats entity-id field-id limit entity-type)- "table" (table-field-stats entity-id field-id limit)))+ "metric" (metric-field-stats entity-id field-id limit)+ ("model" "report" "question") (card-field-stats entity-id field-id limit entity-type)+ "table" (table-field-stats entity-id field-id limit)))diff --git a/src/metabase/metabot/tools/metadata.clj b/src/metabase/metabot/tools/metadata.cljindex 20f7be64875..8ce39462913 100644--- a/src/metabase/metabot/tools/metadata.clj+++ b/src/metabase/metabot/tools/metadata.clj@@ -51,7 +51,8 @@(let [table-results (mapv #(safe-fetch(fn [table-id](entity-details-tools/get-table-details- {:table-id table-id+ {:entity-type :table+ :entity-id table-id:with-fields? true:with-field-values? false:with-related-tables? false@@ -64,7 +65,8 @@model-results (mapv #(safe-fetch(fn [model-id](entity-details-tools/get-table-details- {:model-id model-id+ {:entity-type :model+ :entity-id model-id:with-fields? true:with-field-values? false:with-related-tables? falsediff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.cljindex a08a2696778..08376578e8f 100644--- a/src/metabase/metabot/tools/resources.clj+++ b/src/metabase/metabot/tools/resources.clj@@ -78,7 +78,8 @@;; metabase://table/123/fields(= sub-resource "fields")- (entity-details/get-table-details {:table-id table-id+ (entity-details/get-table-details {:entity-type :table+ :entity-id table-id:with-fields? true:with-field-values? false:with-related-tables? false@@ -87,7 +88,8 @@;; metabase://table/123(nil? sub-resource)- (entity-details/get-table-details {:table-id table-id+ (entity-details/get-table-details {:entity-type :table+ :entity-id table-id:with-fields? false:with-field-values? false:with-related-tables? false@@ -98,30 +100,36 @@(throw (ex-info (str "Unsupported sub-resource '" sub-resource "' for table. Supported: fields"){:resource-id resource-id :sub-resource sub-resource})))))-(defn- fetch-model-resource+(defn- fetch-model-or-card-resource"Fetch model resource based on URI components."- [{:keys [resource-id sub-resource sub-resource-id]}]- (let [model-id (parse-long resource-id)]+ [{:keys [resource-id resource-type sub-resource sub-resource-id]}]+ (let [resource-id* (parse-long resource-id)+ resource-type* (keyword resource-type)]+ (assert (#{:question :model} resource-type*))(cond- ;; metabase://model/123/fields/FIELD_ID+ ;; metabase://<model,question>/123/fields/FIELD_ID(and (= sub-resource "fields") sub-resource-id)- (field-stats/field-values {:entity-type "model"- :entity-id model-id+ ;; field-values takes type as string and id as integer+ (field-stats/field-values {:entity-type resource-type+ :entity-id resource-id*:field-id sub-resource-id:limit 30})- ;; metabase://model/123/fields+ ;; metabase://<model,question>/123/fields(= sub-resource "fields")- (entity-details/get-table-details {:model-id model-id+ ;; get-table-details takes type as kw and id as integer+ (entity-details/get-table-details {:entity-type resource-type*+ :entity-id resource-id*:with-fields? true:with-field-values? false:with-related-tables? false:with-measures? true:with-segments? true})- ;; metabase://model/123+ ;; metabase://<model,question>/123(nil? sub-resource)- (entity-details/get-table-details {:model-id model-id+ (entity-details/get-table-details {:entity-type resource-type*+ :entity-id resource-id*:with-fields? false:with-field-values? false:with-related-tables? false@@ -183,7 +191,8 @@(def ^:private resource-handlers"Map of resource type to handler function."{"table" fetch-table-resource- "model" fetch-model-resource+ "model" fetch-model-or-card-resource+ "question" fetch-model-or-card-resource"metric" fetch-metric-resource"transform" fetch-transform-resource"dashboard" fetch-dashboard-resource})diff --git a/src/metabase/metabot/tools/shared/llm_representations.clj b/src/metabase/metabot/tools/shared/llm_representations.cljindex f3e51438374..08e6c020de7 100644--- a/src/metabase/metabot/tools/shared/llm_representations.clj+++ b/src/metabase/metabot/tools/shared/llm_representations.clj@@ -316,7 +316,7 @@(defn question->xml"Format question for LLM consumption."- [{:keys [id name description verified collection visualization display]}]+ [{:keys [id name description verified collection visualization display fields]}](render-llm-template:question{:question_id (str id)@@ -325,7 +325,9 @@:question_description description:question_display_type (some-> display clojure.core/name):question_collection_xml (when collection (collection->xml collection))- :question_visualization_xml (when visualization (visualization->xml visualization))}))+ :question_visualization_xml (when visualization (visualization->xml visualization))+ :question_fields_xml (when (seq fields)+ (str/join "\n" (map field->xml fields)))}))(defn- text-card->xml"Format a text card for LLM consumption."diff --git a/test/metabase/metabot/agent/user_context_test.clj b/test/metabase/metabot/agent/user_context_test.cljindex 9c546e261fb..fdd286d88bc 100644--- a/test/metabase/metabot/agent/user_context_test.clj+++ b/test/metabase/metabot/agent/user_context_test.clj@@ -378,9 +378,9 @@(testing "table viewing context omits measures/segments sections when none exist"(with-redefs [entity-details/get-table-details- (fn [{:keys [table-id]}]+ (fn [{:keys [entity-id]}]{:structured-output- {:id table-id+ {:id entity-id:type :table:name "plain_table":database_id 1diff --git a/test/metabase/metabot/tools/entity_details_test.clj b/test/metabase/metabot/tools/entity_details_test.cljindex 00152e9b3e0..9a804a7341f 100644--- a/test/metabase/metabot/tools/entity_details_test.clj+++ b/test/metabase/metabot/tools/entity_details_test.clj@@ -145,7 +145,8 @@products-query (lib/query (mt/metadata-provider) (mt/mbql-query products))expected-products-field-count (count (lib/visible-columns products-query -1 {:include-implicitly-joinable? false}));; Get Orders table details with related tables- result (entity-details/get-table-details {:table-id orders-id})+ result (entity-details/get-table-details {:entity-type :table+ :entity-id orders-id})output (:structured-output result)related-tables (:related_tables output)products-related (first (filter #(= products-id (:id %)) related-tables))]@@ -185,12 +186,14 @@:definition measure-def}](mt/with-current-user (mt/user->id :crowberto)(testing "with_measures: false (default) does not include measures"- (let [result (entity-details/get-table-details {:table-id (mt/id :orders)})+ (let [result (entity-details/get-table-details {:entity-type :table+ :entity-id (mt/id :orders)})output (:structured-output result)](is (nil? (:measures output)))))(testing "with_measures: true includes measures for the table"- (let [result (entity-details/get-table-details {:table-id (mt/id :orders)+ (let [result (entity-details/get-table-details {:entity-type :table+ :entity-id (mt/id :orders):with-measures? true})output (:structured-output result)measures (:measures output)]@@ -210,12 +213,14 @@:definition segment-def}](mt/with-current-user (mt/user->id :crowberto)(testing "with_segments: false (default) does not include segments"- (let [result (entity-details/get-table-details {:table-id (mt/id :orders)})+ (let [result (entity-details/get-table-details {:entity-type :table+ :entity-id (mt/id :orders)})output (:structured-output result)](is (nil? (:segments output)))))(testing "with_segments: true includes segments for the table"- (let [result (entity-details/get-table-details {:table-id (mt/id :orders)+ (let [result (entity-details/get-table-details {:entity-type :table+ :entity-id (mt/id :orders):with-segments? true})output (:structured-output result)segments (:segments output)]@@ -238,7 +243,8 @@:table_id (mt/id :products):definition products-measure-def}](mt/with-current-user (mt/user->id :crowberto)- (let [result (entity-details/get-table-details {:table-id (mt/id :orders)+ (let [result (entity-details/get-table-details {:entity-type :table+ :entity-id (mt/id :orders):with-measures? true})output (:structured-output result)measures (:measures output)]diff --git a/test/metabase/metabot/tools/filters_test.clj b/test/metabase/metabot/tools/filters_test.cljindex bcd13b1edb3..f2bb3a9c9f4 100644--- a/test/metabase/metabot/tools/filters_test.clj+++ b/test/metabase/metabot/tools/filters_test.clj@@ -238,7 +238,8 @@:filters []:group-by []}))))(mt/with-current-user (mt/user->id :crowberto)- (let [model-details (-> (metabot.tools.entity-details/get-table-details {:model-id model-id})+ (let [model-details (-> (metabot.tools.entity-details/get-table-details {:entity-type :model+ :entity-id model-id}):structured-output)->field-id #(u/prog1 (-> model-details :fields (by-name %) :field_id)(when-not <>@@ -656,7 +657,8 @@:description "Test model for orders":type :model}](mt/with-current-user (mt/user->id :crowberto)- (let [model-details (-> (metabot.tools.entity-details/get-table-details {:model-id model-id})+ (let [model-details (-> (metabot.tools.entity-details/get-table-details {:entity-type :model+ :entity-id model-id}):structured-output)->field-id #(u/prog1 (-> model-details :fields (by-name %) :field_id)(when-not <>diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.cljindex 6a55bee56a6..924d7d7caa5 100644--- a/test/metabase/metabot/tools/resources_test.clj+++ b/test/metabase/metabot/tools/resources_test.clj@@ -3,8 +3,11 @@(:require[clojure.string :as str][clojure.test :refer :all]+ [medley.core :as m][metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.metabot.tools.resources :as read-resource]+ [metabase.query-processor :as qp][metabase.test :as mt]))(deftest parse-uri-test@@ -43,6 +46,13 @@:sub-resource-id nil}(#'read-resource/parse-uri "metabase://model/456"))))+ (testing "parses question URI"+ (is (= {:resource-type "question"+ :resource-id "456"+ :sub-resource nil+ :sub-resource-id nil}+ (#'read-resource/parse-uri "metabase://question/456"))))+(testing "parses metric with dimensions"(is (= {:resource-type "metric":resource-id "789"@@ -172,3 +182,30 @@:error "Table not found"}]formatted (#'read-resource/format-resources resources)](is (str/includes? formatted "**Error:** Table not found")))))++(deftest read-question-resource-test+ (let [mp (mt/metadata-provider)+ query (as-> (lib/query mp (lib.metadata/table mp (mt/id :products))) $+ (lib/aggregate $ (lib/count))+ (lib/breakout $ (m/find-first (comp #{"Category"} :display-name)+ (lib/breakoutable-columns $))))+ metadata (-> query+ qp/process-query+ :data :results_metadata :columns)]++ (mt/with-temp+ [:model/Card {question-id :id} {:name "My fav card"+ :dataset_query query+ :result_metadata metadata}]+ (mt/with-test-user :crowberto+ (let [read-result (read-resource/read-resource-tool+ {:uris [(str "metabase://question/" question-id "/fields")]})+ output (:output read-result)+ structured (get-in read-result [:resources 0 :content :structured-output])]+ (testing "Output references expected fields"+ (is (re-find #"display_name\S+Count" output))+ (is (re-find #"display_name\S+Category" output)))+ (testing "Structured output contains expected fields"+ (is (=? {:fields [{:display_name "Category"}+ {:display_name "Count"}]}+ structured))))))))