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
Point Metabot at the database and collection lists for a name in a URI (#86093)
Paolo Rascunàe87b42755f9c
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.cljindex aec34f13989..faaf1782374 100644--- a/src/metabase/metabot/tools/resources.clj+++ b/src/metabase/metabot/tools/resources.clj@@ -231,8 +231,33 @@{:message (str "No resource is served at " (pr-str (uri-preview uri)) "."):recovery (vec (keep identity [(templates-step kind) kinds-step search-step]))})+(def ^:private search-id-step+ "The step that finds a numeric id through `search`."+ {:uses #{"search"}+ :text (str "Call `search` and build the URI from a result's numeric `id`"+ " attribute, or copy its `uri` attribute as-is.")})++(def ^:private id-lookup-steps+ "A kind → step map for `database` and `collection`, because the agent-loop `search` tools do not return them.++ Each step names the URI that lists the entities of its kind.++ The steps name no tool, so `:uses` is empty: they stay in every profile and on the Agent API,+ whose tool set is `#{\"search\"}`."+ {"database" {:uses #{}+ :text (str "Read `metabase://databases` to list the databases you can read and their numeric"+ " `id` attributes. Use that `id` in the URI.")}+ "collection" {:uses #{}+ :text (str "Read `metabase://collections?tree=true` to list the collections you can read and"+ " their numeric `id` attributes. Use that `id` in the URI. The root collection"+ " (by default \"Our analytics\") has no numeric id: use `metabase://collection/root`.")}})+(tools/defrecoverable non-numeric-id!- "An entity URI carried something other than a numeric id — usually a 21-character entity id."+ "An entity URI carried something other than a numeric id.++ Usually it is a 21-character entity id, or the name of a database or collection. The agent-loop+ `search` tools return no databases or collections, so for those kinds the recovery names the URI+ that lists them."{:payload [:map {:closed true}[:uri :string][:kind :string]@@ -242,9 +267,7 @@" an entity_id, a name or a slug."):recovery (vec (keep identity[(templates-step kind)- {:uses #{"search"}- :text (str "Call `search` and build the URI from a result's numeric `id`"- " attribute, or copy its `uri` attribute as-is.")}]))})+ (get id-lookup-steps kind search-id-step)]))})(tools/defrecoverable inactive-table!"Report a readable table that is no longer active."diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.cljindex fd4a99a57ed..a7d0e2b4b9e 100644--- a/test/metabase/agent_api/api_test.clj+++ b/test/metabase/agent_api/api_test.clj@@ -2156,6 +2156,19 @@(is (<= (count attribute) 200))(is (str/ends-with? attribute "...[truncated]")))))+(deftest read-resource-non-numeric-database-id-test+ (testing "a database or collection name points at its listing URI, not at `search`"+ (let [{[database-item collection-item] :resources}+ (mt/user-http-request :crowberto :post 200 "agent/v1/read-resource"+ {:uris ["metabase://database/redshift/schemas/core/tables"+ "metabase://collection/our-analytics/items"]})]+ (doseq [[item listing-uri] [[database-item "metabase://databases"]+ [collection-item "metabase://collections?tree=true"]]]+ (testing (:uri item)+ (is (not (contains? item :content)))+ (is (str/includes? (:error item) listing-uri))+ (is (not (str/includes? (:error item) "Call `search`"))))))))+(deftest ^:parallel read-resource-tool-description-test(let [endpoint (some (fn [[_ endpoint]](when (= "read_resource" (get-in endpoint [:form :metadata :tool :name]))diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.cljindex 294f6f99cc0..969a1fab6dd 100644--- a/test/metabase/metabot/tools/resources_test.clj+++ b/test/metabase/metabot/tools/resources_test.clj@@ -505,6 +505,46 @@(is (nil? (#'read-resource/check-numeric-id-segment!"metabase://table/3/fields/c75/17" ["table" "3" "fields" "c75" "17"])))))+(deftest non-numeric-database-id-points-at-databases-test+ (mt/with-current-user (mt/user->id :crowberto)+ (doseq [[uri listing-uri] [["metabase://database/redshift" "metabase://databases"]+ ["metabase://database/redshift/tables" "metabase://databases"]+ ["metabase://database/redshift/schemas/core/tables" "metabase://databases"]+ ["metabase://database/99999999999999999999" "metabase://databases"]+ ["metabase://collection/our-analytics/items" "metabase://collections?tree=true"]]]+ (testing uri+ (let [{[{:keys [error content]}] :resources} (read-uris {:uris [uri]})]+ (is (nil? content))+ (is (str/includes? error listing-uri))+ (is (not (str/includes? error "Call `search`"))))))+ (testing "the collection step names the URI of the root collection"+ (let [{[{:keys [error]}] :resources} (read-uris {:uris ["metabase://collection/our-analytics/items"]})]+ (is (str/includes? error "has no numeric id: use `metabase://collection/root`"))))))++(deftest non-numeric-id-lookup-step-without-search-test+ (mt/with-current-user (mt/user->id :crowberto)+ (doseq [[uri listing-uri] [["metabase://database/redshift/schemas/core/tables" "metabase://databases"]+ ["metabase://collection/our-analytics/items" "metabase://collections?tree=true"]]]+ (testing (str "a tool set without `search` (nlq) still gets a way to find the id: " uri)+ (let [{:keys [output]} (runtime/invoke (tools/entries [read-resource/read-resource-tool])+ {:profile-id :nlq :metabot nil :user-scope api-scope/unrestricted+ :tool-names #{"read_resource"}}+ "read_resource"+ {:uris [uri]})]+ (is (str/includes? output listing-uri)))))))++(deftest non-numeric-id-other-kinds-keep-search-step-test+ (mt/with-current-user (mt/user->id :crowberto)+ (doseq [uri ["metabase://table/orders" "metabase://model/VZbHZIeqQ2HhZv5r0pO6a"]]+ (testing uri+ (let [{[{:keys [error]}] :resources} (read-uris {:uris [uri]})]+ (is (str/includes? error "Call `search` and build the URI"))+ (is (not (str/includes? error "metabase://databases"))))))+ (testing "a numeric database id that does not exist is still a plain not-found"+ (let [{[{:keys [error]}] :resources} (read-uris {:uris ["metabase://database/99999999"]})]+ (is (str/includes? error "was not found"))+ (is (not (str/includes? error "Invalid id")))))))+(comment(mt/with-current-user (mt/user->id :crowberto)(read-uris