Skip to content

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

Clarify resource URIs and preserve bounded batch outcomes (#85177)

Andrei Simionescu626fb0a60ffe
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index e4ceff35c18..66fca434770 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -685,11 +685,7 @@
;;; -------------------------------------------------- Read Resource -------------------------------------------------
(mr/def ::read-resource-request
- "Request shape for /v1/read-resource: the tool's own published arguments.
-
- Taken from the tool rather than written out again, so this endpoint and the agent loop offer the
- same shape and the same limits. Before, this was a hand-written copy that had already lost the
- 5-URI cap the tool enforces."
+ "Arguments for /v1/read-resource, shared with the tool declaration."
(:args (tools.core/validate-tool! metabot-resources/read-resource-tool)))
(mr/def ::read-resource-item
@@ -732,14 +728,10 @@
Calls the `read_resource` tool in `metabase.metabot.tools.resources` — the same record the agent
loop calls, so URI dispatch, per-URI permission checks and the per-URI failure text are shared.
A URI that cannot be read becomes that item's `:error`; the call still returns 200 with the items
- that did read. Up to 5 URIs per call, rejected as a 400 by `::read-resource-request`."
+ that did read. Each request accepts up to five URIs; oversized batches return 400 before any reads."
{:scope metabot/agent-resource-read
:tool {:name "read_resource"
- :description (str "Read Metabase entities by metabase:// URI. "
- "Examples: metabase://databases, metabase://database/{id}/tables, "
- "metabase://collection/{id}/items, metabase://question/{id}, "
- "metabase://dashboard/{id}/items, metabase://table/{id}/fields. "
- "Up to 5 URIs per call. List endpoints cap at 25 items.")}}
+ :description metabot-resources/agent-api-description}}
[_route-params
_query-params
body :- ::read-resource-request]
diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.clj
index 8563db62fcd..2dd11b770cb 100644
--- a/src/metabase/metabot/db.clj
+++ b/src/metabase/metabot/db.clj
@@ -847,8 +847,10 @@
:archived false))
(mu/defn cards-in-collection
- "The presentable columns of the unarchived Cards in the Collection with `collection-id`, ordered by name."
- [collection-id :- ::lib.schema.id/collection]
+ "Return unarchived card summaries in a collection, ordered by name.
+
+ A nil `collection-id` selects the root collection."
+ [collection-id :- [:maybe ::lib.schema.id/collection]]
(t2/select [:model/Card :id :name :type :description :collection_id :database_id :table_id]
{:where [:and [:= :collection_id collection-id] [:= :archived false]]
:order-by [[:%lower.name :asc]]}))
@@ -956,9 +958,10 @@
(t2/select-one-fn :name :model/Dashboard :id dashboard-id))
(mu/defn dashboards-in-collection
- "The ID, name, description, and Collection ID of the unarchived Dashboards in the Collection with
- `collection-id`, ordered by name."
- [collection-id :- ::lib.schema.id/collection]
+ "Return unarchived dashboard summaries in a collection, ordered by name.
+
+ A nil `collection-id` selects the root collection."
+ [collection-id :- [:maybe ::lib.schema.id/collection]]
(t2/select [:model/Dashboard :id :name :description :collection_id]
:collection_id collection-id
:archived false
@@ -978,9 +981,10 @@
{:order-by [[:row :asc] [:col :asc]]}))
(mu/defn documents-in-collection
- "The ID, name, Collection ID, and exploration of the unarchived, non-exploration Documents in the Collection with
- `collection-id`, ordered by name."
- [collection-id :- ::lib.schema.id/collection]
+ "Return unarchived document summaries in a collection, ordered by name.
+
+ Excludes exploration summaries. A nil `collection-id` selects the root collection."
+ [collection-id :- [:maybe ::lib.schema.id/collection]]
(t2/select [:model/Document :id :name :collection_id :exploration_id]
:collection_id collection-id
:archived false
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index 4289a43990c..9334204284d 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -68,6 +68,7 @@
[clojure.string :as str]
[metabase.activity-feed.core :as activity-feed]
[metabase.api.common :as api]
+ [metabase.collections.models.collection :as collection]
[metabase.documents.core :as documents]
[metabase.documents.prose-mirror :as prose-mirror]
[metabase.metabot.agent.streaming :as streaming]
@@ -98,6 +99,40 @@
"Maximum number of URIs that can be fetched in a single call."
5)
+(def ^:private max-uri-length
+ "Maximum number of characters in a resource URI."
+ 4096)
+
+(def ^:private max-uri-preview-length
+ "URI echoes occupy at most this many characters after escaping."
+ 200)
+
+(def ^:private max-batch-error-length
+ "The failed resources in one call share this many characters of displayed error text."
+ 2000)
+
+(def ^:private truncation-marker "...[truncated]")
+
+(defn- text-preview
+ "Return a prefix and marker that fit the rendered text budget."
+ [^String text limit length-fn]
+ (if (<= (length-fn text) limit)
+ text
+ (loop [end 0]
+ (let [next-end (.offsetByCodePoints text (int end) 1)
+ candidate (str (subs text 0 next-end) truncation-marker)]
+ (if (> (length-fn candidate) limit)
+ (str (subs text 0 end) truncation-marker)
+ (recur (long next-end)))))))
+
+(defn- uri-preview
+ "Return a URI preview of at most 200 characters after XML or string escaping."
+ [uri]
+ (text-preview uri max-uri-preview-length
+ (fn [text]
+ (max (count (llm-shape/escape-xml text))
+ (count (pr-str text))))))
+
(def ^:private page-size
"Page size for list responses."
25)
@@ -121,7 +156,10 @@
"metabase://database/{id}/schemas/{schema_name}/tables"]
"collection" ["metabase://collection/{id}"
"metabase://collection/{id}/items"
- "metabase://collection/{id}/subcollections"]
+ "metabase://collection/{id}/subcollections"
+ "metabase://collection/root"
+ "metabase://collection/root/items"
+ "metabase://collection/root/subcollections"]
"table" ["metabase://table/{id}"
"metabase://table/{id}/fields"
"metabase://table/{id}/fields/{field_id}"
@@ -176,7 +214,7 @@
"The string is not a metabase:// URI at all."
{:payload [:map {:closed true} [:uri :string]]}
[{:keys [uri]}]
- {:message (str (pr-str uri) " is not a Metabase resource URI. One starts with `metabase://` and"
+ {:message (str (pr-str (uri-preview uri)) " is not a Metabase resource URI. One starts with `metabase://` and"
" has at least one path segment, for example `metabase://databases`.")
:recovery [kinds-step search-step]})
@@ -188,7 +226,7 @@
;; itself unknown — then there is no kind to list shapes for.
[:kind {:optional true} [:maybe :string]]]}
[{:keys [uri kind]}]
- {:message (str "No resource is served at " (pr-str uri) ".")
+ {:message (str "No resource is served at " (pr-str (uri-preview uri)) ".")
:recovery (vec (keep identity [(templates-step kind) kinds-step search-step]))})
(tools/defrecoverable non-numeric-id!
@@ -198,7 +236,7 @@
[:kind :string]
[:id :string]]}
[{:keys [kind id]}]
- {:message (str "Invalid id " (pr-str id) " in URI. Resource URIs use the numeric entity id, not"
+ {:message (str "Invalid id " (pr-str (uri-preview id)) " in URI. Resource URIs use the numeric entity id, not"
" an entity_id, a name or a slug.")
:recovery (vec (keep identity
[(templates-step kind)
@@ -215,7 +253,36 @@
[{:keys [id type card-uri]}]
{:message (str "Card " id " is a " type ", not a metric.")
:recovery [{:uses #{}
- :text (str "Read `" card-uri "` instead.")}]})
+ :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]]}
+ [{:keys [id database-id]}]
+ {:message (str "Table " id " is inactive.")
+ :recovery [{:uses #{}
+ :text (str "Read `metabase://database/" database-id "/tables` to find active tables.")}]})
+
+(tools/defrecoverable missing-schema!
+ "Report a database schema with no active tables."
+ {:payload [:map {:closed true} [:database-id :int] [:schema :string]]
+ :status-code 404}
+ [{:keys [database-id schema]}]
+ {:message (str "Schema " (pr-str (uri-preview schema)) " was not found in database " database-id "."
+ " Schema names are case-sensitive.")
+ :recovery [{:uses #{}
+ :text (str "Read `metabase://database/" database-id "/schemas` for available schema names.")}]})
(tools/defrecoverable invalid-page!
"A `?page=N` outside the list's range."
@@ -236,9 +303,9 @@
[:available [:sequential :string]]]}
[{:keys [id available]}]
{:message (if (seq available)
- (str "No chart or query with id " (pr-str id) " exists in this conversation."
- " The ids that do: " (str/join ", " available) ".")
- (str "No chart or query with id " (pr-str id) " exists in this conversation, and"
+ (str "No chart or query with id " (pr-str (uri-preview id)) " exists in this conversation."
+ " The ids that do: " (str/join ", " (map uri-preview available)) ".")
+ (str "No chart or query with id " (pr-str (uri-preview id)) " exists in this conversation, and"
" none have been created in it yet."))
:recovery [{:uses #{}
:text (str "Charts and queries are per-conversation. If the user mentioned this one,"
@@ -314,6 +381,13 @@
(when k [(keyword k) (or v "")])))))
(into {}))))
+(def ^:private uri-aspect-aliases
+ "Alternate aspect names accepted for each resource kind."
+ {"table" {"columns" "fields"}
+ "model" {"columns" "fields"}
+ "question" {"columns" "fields"}
+ "dashboard" {"cards" "items"}})
+
(defn- parse-uri
"Parse a metabase:// URI into a vector of path segments and a query-params map.
@@ -334,7 +408,9 @@
(mapv codec/url-decode))]
(when (zero? (count segments))
(malformed-uri! {:uri uri}))
- {:segments segments
+ {:segments (if-let [aspect (get-in uri-aspect-aliases [(first segments) (get segments 2)])]
+ (assoc segments 2 aspect)
+ segments)
:query-params (parse-query-string qs)}))
;; ----- Item presenters (list-row shapes) -----
@@ -543,21 +619,29 @@
(defn- fetch-database-schema-tables [id-str schema-name query-params]
(let [db-id (parse-long id-str)
_ (warehouses/get-database db-id)
- tables (->> (metabot.db/active-tables-in-schema db-id schema-name)
+ rows (metabot.db/active-tables-in-schema db-id schema-name)
+ _ (when-not (seq rows)
+ (missing-schema! {:database-id db-id :schema schema-name}))
+ tables (->> rows
(filter mi/can-read?)
(mapv present-table))]
(list-result :database-schema-tables tables query-params)))
;; ----- Collection drill-down -----
+(defn- readable-collection [id-str]
+ (if (= id-str "root")
+ (api/read-check (collection/root-collection-with-ui-details nil))
+ (api/read-check :model/Collection (parse-long id-str))))
+
(defn- fetch-collection [id-str]
- (let [coll (api/read-check :model/Collection (parse-long id-str))]
+ (let [coll (readable-collection id-str)]
(entity-result (present-collection coll))))
(defn- fetch-collection-items [id-str query-params]
(documents/with-content-gate-cache
(let [coll-id (parse-long id-str)
- coll (api/read-check :model/Collection coll-id)
+ coll (when-not (= id-str "root") (readable-collection id-str))
cards (->> (metabot.db/cards-in-collection coll-id)
(filter mi/can-read?))
dashboards (->> (metabot.db/dashboards-in-collection coll-id)
@@ -565,7 +649,9 @@
;; Exploration Summary documents are only through their exploration — so they stay out of this listing
documents (->> (metabot.db/documents-in-collection coll-id)
(filter mi/can-read?))
- subcollections (->> (metabot.db/unarchived-collections-at-location (str (:location coll) coll-id "/"))
+ subcollections (->> (if (= id-str "root")
+ (metabot.db/navigable-collections false)
+ (metabot.db/unarchived-collections-at-location (collection/children-location coll)))
(filter mi/can-read?))
items (concat (map present-collection subcollections)
(map present-card cards)
@@ -574,9 +660,10 @@
(list-result :collection-items items query-params))))
(defn- fetch-collection-subcollections [id-str query-params]
- (let [coll-id (parse-long id-str)
- coll (api/read-check :model/Collection coll-id)
- subs (->> (metabot.db/unarchived-collections-at-location (str (:location coll) coll-id "/"))
+ (let [coll (when-not (= id-str "root") (readable-collection id-str))
+ subs (->> (if (= id-str "root")
+ (metabot.db/navigable-collections false)
+ (metabot.db/unarchived-collections-at-location (collection/children-location coll)))
(filter mi/can-read?)
(mapv present-collection))]
(list-result :collection-subcollections subs query-params)))
@@ -592,12 +679,13 @@
(warehouses/get-database db-id)))
(defn check-table-resource-database
- "Require that `table-id`'s backing database is addressable as a Metabot resource (see
- [[check-resource-database]]). Exported for [[metabase.metabot.tools.metadata]], which needs the
- same guard for the `get_field_values` tool."
+ "Require that `table-id`'s backing database is addressable as a Metabot resource and return the table.
+
+ See [[check-resource-database]]. Also used by [[metabase.metabot.tools.metadata]]."
[table-id]
(when-let [table (api/read-check :model/Table table-id)]
- (check-resource-database (:db_id table))))
+ (check-resource-database (:db_id table))
+ table))
(defn check-card-resource-database
"Require that `card-id`'s backing database is addressable as a Metabot resource, and return the card.
@@ -615,13 +703,30 @@
:model/Segment (metabot.db/segment-table-id id))]
(check-table-resource-database table-id)))
+(defn- active-table-resource [id]
+ (let [table (check-table-resource-database id)]
+ (when-not (:active table)
+ (inactive-table! {:id id :database-id (:db_id table)}))
+ table))
+
+(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)})))
+ card))
+
(defn- table-details
"Shared `entity-details/get-table-details` call for both /table/{id} and /table/{id}/fields.
`entity-type` is :table, :model, or :question."
[entity-type id with-fields?]
(case entity-type
- :table (check-table-resource-database id)
- (:model :question) (check-card-resource-database id))
+ :table (active-table-resource id)
+ (:model :question) (apply check-card-kind id entity-type (when with-fields? ["fields"])))
(entity-details/get-table-details {:entity-type entity-type
:entity-id id
:with-fields? with-fields?
@@ -646,10 +751,8 @@
result))))
(defn- fetch-table-field [id-str field-id]
- (let [table-id (parse-long id-str)
- table (api/read-check :model/Table table-id)]
- (api/check-404 (:active table))
- (check-resource-database (:db_id table))
+ (let [table-id (parse-long id-str)]
+ (active-table-resource table-id)
(field-values {:entity-type "table"
:entity-id table-id
:field-id field-id
@@ -657,9 +760,8 @@
(defn- fetch-table-derived [id-str query-params]
(let [table-id (parse-long id-str)
- table (api/read-check :model/Table table-id)
+ table (active-table-resource table-id)
db-id (:db_id table)
- _ (check-resource-database db-id)
cards (->> (metabot.db/cards-for-table table-id)
(filter mi/can-read?)
(mapv present-card))
@@ -686,15 +788,14 @@
(defn- fetch-card-field [type-str id-str field-id]
(let [card-id (parse-long id-str)]
- (check-card-resource-database card-id)
+ (check-card-kind card-id (keyword type-str) "fields" field-id)
(field-values {:entity-type type-str
:entity-id card-id
:field-id field-id
:limit 30})))
-(defn- fetch-card-sources [id-str query-params]
- (let [card (api/read-check :model/Card (parse-long id-str))]
- (check-resource-database (:database_id card))
+(defn- fetch-card-sources [type-str id-str query-params]
+ (let [card (check-card-kind (parse-long id-str) (keyword type-str) "sources")]
(list-result :card-sources (card-sources-items card) query-params)))
;; ----- Metric -----
@@ -879,7 +980,7 @@
"Present a document as an indexed outline of its top-level blocks. The indices are the
`position` values the `save_entity` tool accepts when inserting a chart into the document."
[id-str]
- (let [document (documents/get-document (parse-long id-str))
+ (let [document (documents/get-document (parse-long id-str) :log-view? false)
blocks (get-in document [:document :content])]
(entity-result
{:type "document"
@@ -954,7 +1055,7 @@
(def ^:private numeric-id-uri-types
"URI entity-type segments whose next segment must be a numeric id (see `dispatch`)."
#{"database" "collection" "table" "model" "question" "metric"
- "measure" "segment" "transform" "dashboard"})
+ "measure" "segment" "transform" "dashboard" "document"})
(defn- check-numeric-id-segment!
"Entity URIs take numeric ids only. The common miss is the LLM pasting a 21-char entity id
@@ -964,6 +1065,7 @@
[uri [type-seg id-seg]]
(when (and (numeric-id-uri-types type-seg)
(some? id-seg)
+ (not (and (= type-seg "collection") (= id-seg "root")))
(nil? (parse-long id-seg)))
(non-numeric-id! {:uri uri :kind type-seg :id id-seg})))
@@ -994,7 +1096,8 @@
[[type-seg id-seg]]
(if-let [kind (get uri-entity-kinds type-seg)]
(cond-> {:kind kind}
- (some-> id-seg parse-long) (assoc :id (parse-long id-seg)))
+ (some-> id-seg parse-long) (assoc :id (parse-long id-seg))
+ (and (= type-seg "collection") (= id-seg "root")) (assoc :id "root"))
{}))
(defn- dispatch
@@ -1040,7 +1143,7 @@
[(t :guard #{"model" "question"}) id] (fetch-card t id)
[(t :guard #{"model" "question"}) id "fields"] (fetch-card-fields t id)
[(t :guard #{"model" "question"}) id "fields" & rst] (fetch-card-field t id (str/join "/" rst))
- [(t :guard #{"model" "question"}) id "sources"] (fetch-card-sources id query-params)
+ [(t :guard #{"model" "question"}) id "sources"] (fetch-card-sources t id query-params)
;; Metric
["metric" id] (fetch-metric id)
@@ -1171,67 +1274,65 @@
(defn- resource-element
"Wrap a resource body in XML, escaping its URI attribute."
[uri body]
- (str "<resource uri=\"" (llm-shape/escape-xml uri) "\">\n" body "\n</resource>"))
+ (str "<resource uri=\"" (llm-shape/escape-xml (uri-preview uri)) "\">\n" body "\n</resource>"))
(def ^:private uri-arg
"The one thing a read takes. Both declarations below build their `:args` from this, so the single
and batched forms cannot drift."
- [:string {:min 1 :description "A metabase:// resource URI"}])
+ [:string {:min 1
+ :max max-uri-length
+ :description "A metabase:// resource URI"
+ :error/message (str "must be a non-empty URI string of at most " max-uri-length " characters")}])
+
+(def ^:private conversation-kinds
+ "URI kinds that read charts and queries from the conversation's state."
+ #{"chart" "query"})
+
+(defn- description
+ "What a caller is told `read_resource` is. Without `agent-loop?`, the chart and query URIs and the
+ `save_entity` note are left out, since they need the agent loop's conversation state and tools."
+ [agent-loop?]
+ (str "Read detailed information about Metabase resources. Copy URIs from `search` or navigation results.\n"
+ "Entity IDs are numeric. Use `collection/root` for the root collection. Schema names are case-sensitive\n"
+ "and URI path segments must be URL-encoded.\n\n"
+ "Shortened URI echoes and error text end with ...[truncated]. Reuse the original URI for another read.\n\n"
+ "SUPPORTED URI FORMS:\n"
+ (str/join "\n" (for [[kind templates] (sort-by key uri-templates)
+ :when (or agent-loop? (not (conversation-kinds kind)))
+ uri templates]
+ (str "- " uri)))
+ "\n\nALIASES (the same trailing segments are accepted):\n"
+ (str/join "\n" (for [[kind aliases] (sort-by key uri-aspect-aliases)
+ [alias canonical] (sort-by key aliases)]
+ (str "- metabase://" kind "/{id}/" alias " is an alias for /" canonical)))
+ "\n\nDocuments return a name and an indexed outline of top-level blocks."
+ (when agent-loop?
+ (str " The indices are valid\n"
+ "`position` values for `save_entity` document destinations.\n\n"
+ "Chart and query IDs come from this conversation's state. Chart URIs return the chart type\n"
+ "and its query; query URIs return a query from this conversation."))))
(def ^:private tool-description
"What the model is told `read_resource` is.
A value rather than a docstring: the batched form has to add its own per-call limits to it, and a
docstring cannot be computed."
- (str
- "Read detailed information about Metabase resources via URI patterns. Use this to navigate\n"
- "the instance and drill into specific entities. URIs returned by `search` can be fed directly\n"
- "back here. Only numeric IDs accepted, never alphanumeric entity-id's.\n"
- "\n"
- "NAVIGATION (top-level lists):\n"
- "- metabase://databases - all databases\n"
- "- metabase://collections - root collections\n"
- "- metabase://collections?tree=true - flat list of all collections (use :location for hierarchy)\n"
- "- metabase://user/recent-items - your recently-viewed items\n"
- "\n"
- "DATABASE DRILL-DOWN:\n"
- "- metabase://database/{id}\n"
- "- metabase://database/{id}/tables\n"
- "- metabase://database/{id}/models\n"
- "- metabase://database/{id}/schemas\n"
- "- metabase://database/{id}/schemas/{schemaName}/tables\n"
- "\n"
- "COLLECTION DRILL-DOWN:\n"
- "- metabase://collection/{id}\n"
- "- metabase://collection/{id}/items - subcollections + leaves\n"
- "- metabase://collection/{id}/subcollections\n"
- "\n"
- "ENTITY DRILL-DOWN:\n"
- "- metabase://table/{id}[/fields[/{field_id}]] [/derived]\n"
- "- metabase://model/{id}[/fields[/{field_id}]] [/sources]\n"
- "- metabase://question/{id}[/fields[/{field_id}]] [/sources]\n"
- "- metabase://metric/{id}[/dimensions[/{dim_id}]]\n"
- "- metabase://measure/{id}\n"
- "- metabase://segment/{id}\n"
- "- metabase://transform/{id}[/sources|/target]\n"
- "- metabase://dashboard/{id}[/items]\n"
- "- metabase://document/{id} - name + indexed outline of the document's top-level blocks\n"
- " (the indices are valid `position` values for `save_entity` document destinations)\n"
- "\n"
- "CONVERSATION STATE (charts and queries generated in or pasted into this conversation,\n"
- "e.g. referenced in a user message as [name](metabase://chart/{id})):\n"
- "- metabase://chart/{chart_id} - the chart's type and its query\n"
- "- metabase://query/{query_id} - a query from this conversation's state"))
+ (description true))
(def ^:private batch-note
"What the batched form adds to [[tool-description]]: the limits that exist only because one call
can carry several URIs."
(str "\n\n"
- "Up to " max-concurrent-uris " URIs may be requested in one call. List responses are capped\n"
+ "Up to " max-concurrent-uris " URIs may be requested in one call.\n"
+ "List responses are capped\n"
"at " page-size " items per page; when a list is truncated its structured output carries a\n"
"ready-to-fetch `next-page-uri` — request that rather than building a `page=N` query param\n"
"by hand. The response also includes `page` (current, 1-indexed) and `pages` (total)."))
+(def agent-api-description
+ "The batched description for callers outside the agent loop, such as the Agent API."
+ (str (description false) batch-note))
+
(defrecord ReadResourceTool []
tools/Tool
(declaration [_]
@@ -1259,8 +1360,11 @@
:args [:map {:closed true}
[:uris [:sequential {:min 1
:max max-concurrent-uris
- :error/message (str "must be an array of 1 to "
- max-concurrent-uris " URI strings")}
+ :description (str "A non-empty array of at most " max-concurrent-uris
+ " URIs, each at most " max-uri-length " characters. "
+ "Request fewer URIs or shorten them to fit these limits.")
+ :error/message (str "must be a non-empty array of at most "
+ max-concurrent-uris " URIs. Request fewer URIs per call")}
uri-arg]]]))
(batched-args [_ {:keys [uris]}]
@@ -1279,13 +1383,14 @@
(keep (fn [{:keys [item structured-output]}]
(uri-label (:uri item) structured-output)))
(distinct))
- entries)]
+ entries)
+ error-length (quot max-batch-error-length (max 1 (count (filter :failed? entries))))]
(cond-> {:output (str "<resources>\n"
(str/join "\n"
(for [{:keys [item output failed?]} entries]
(resource-element (:uri item)
(if failed?
- (str "**Error:** " output)
+ (str "**Error:** " (text-preview output error-length count))
output))))
"\n</resources>")}
(some :structured-output entries)
diff --git a/src/metabase/metabot/tools/runtime.clj b/src/metabase/metabot/tools/runtime.clj
index 591a1abdd4c..77cb3fdc10a 100644
--- a/src/metabase/metabot/tools/runtime.clj
+++ b/src/metabase/metabot/tools/runtime.clj
@@ -197,6 +197,8 @@
| `:recoverable` | the message, then each kept recovery step on its own line |
| `:unrecoverable` | a fixed sentence carrying only the code |
+ Failure text is capped at [[max-output-length]] characters.
+
`:user-message` is copied onto the outcome for unrecoverable errors so the agent loop can show it
to the user. It never becomes part of `:output`."
[{:keys [class code message recovery user-message] :as error} tool-names]
diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.clj
index 0d78a539953..560e6fa5009 100644
--- a/test/metabase/agent_api/api_test.clj
+++ b/test/metabase/agent_api/api_test.clj
@@ -11,6 +11,7 @@
[metabase.ai-tracing.log :as ait.log]
[metabase.ai-tracing.settings :as ai-tracing.settings]
[metabase.api.macros :as api.macros]
+ [metabase.api.macros.defendpoint.tools-manifest :as tools-manifest]
[metabase.api.macros.scope :as api.scope]
[metabase.collections.models.collection :as collection]
[metabase.lib-be.db :as lib-be.db]
@@ -2022,6 +2023,28 @@
;;; ------------------------------------------------- Read Resource Tests -----------------------------------------
+(deftest read-resource-uri-preview-test
+ (doseq [[uri success?] [[(str "metabase://databases?ignored=" (apply str (repeat 4000 "x"))) true]
+ [(apply str (repeat 4096 "\"")) false]
+ [(apply str (repeat 4096 "\u08FF")) false]]]
+ (let [response (mt/user-http-request :crowberto :post 200 "agent/v1/read-resource" {:uris [uri]})
+ resource (first (:resources response))
+ attribute (second (re-find #"<resource uri=\"([^\"]*)\">" (:output response)))]
+ (is (= success? (contains? resource :content)))
+ (is (= uri (:uri resource)))
+ (is (<= (count attribute) 200))
+ (is (str/ends-with? attribute "...[truncated]")))))
+
+(deftest ^:parallel read-resource-tool-description-test
+ (let [endpoint (some (fn [[_ endpoint]]
+ (when (= "read_resource" (get-in endpoint [:form :metadata :tool :name]))
+ endpoint))
+ (api.macros/ns-routes 'metabase.agent-api.api))
+ description (:description (tools-manifest/endpoint->tool-definition "/api/agent" endpoint))]
+ (is (str/includes? description "metabase://collection/root/items"))
+ (doseq [text ["metabase://chart/" "metabase://query/" "conversation" "save_entity"]]
+ (is (not (str/includes? description text)) text))))
+
(deftest read-resource-test
(testing "Dispatches a top-level URI through the shared resolver"
(let [resp (mt/user-http-request :crowberto :post 200 "agent/v1/read-resource"
@@ -2038,13 +2061,12 @@
{:uris [(str "metabase://table/" (mt/id :orders))]})]
(is (= 1 (count (:resources resp))))
(is (some? (-> resp :resources first :content)))))
- (testing "Returns 400 when too many URIs"
- (let [uris (vec (repeat 10 "metabase://databases"))]
- (mt/user-http-request :crowberto :post 400 "agent/v1/read-resource"
- {:uris uris})))
- (testing "this endpoint publishes the tool's own argument schema, so its limits cannot drift from
- the ones the agent loop enforces. The hand-written copy this replaced had already lost
- the 5-URI cap."
+ (testing "Rejects batches above the five-URI limit"
+ (let [uris (conj (vec (repeat 5 "metabase://databases")) "metabase://collections")
+ resp (mt/user-http-request :crowberto :post 400 "agent/v1/read-resource" {:uris uris})]
+ (is (str/includes? (pr-str (:errors resp)) "at most 5 URIs"))
+ (is (str/includes? (pr-str (:errors resp)) "Request fewer URIs"))))
+ (testing "This endpoint publishes the tool's argument schema"
;; Compared on the `:uris` entry: `mr/def` stamps its own docstring onto the map's properties,
;; so the outer form differs by a `:description` the endpoint never publishes.
(is (= (nth (:args (tools.core/validate-tool! metabot-resources/read-resource-tool)) 2)
diff --git a/test/metabase/metabot/tools/metadata_test.clj b/test/metabase/metabot/tools/metadata_test.clj
index b75eb71567b..c5b4cec6c9c 100644
--- a/test/metabase/metabot/tools/metadata_test.clj
+++ b/test/metabase/metabot/tools/metadata_test.clj
@@ -363,12 +363,14 @@
:metabase.metabot.tools.core/all-items-failed
::tools.u/wrong-source-type)} error))
(is (str/includes? (or output "")
- (str (if (= expected-type "metric") "Card " "ID ")
- source-id " is a " actual-type ", not a " expected-type "."
- (if (and (= profile :internal) (= expected-type "metric"))
- (str "\nRead `metabase://" actual-type "/" source-id "/fields/"
- (mt/id :orders :id) "` instead.")
- (str "\nRetry using a " expected-type " ID.")))))))))))
+ (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."))))))))))
(deftest question-field-details-use-returned-columns-test
(mt/as-admin
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 5c46c2a5de2..128daabbf8d 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -7,15 +7,19 @@
[clojure.test :refer :all]
[medley.core :as m]
[metabase.activity-feed.core :as activity-feed]
+ [metabase.api-scope.core :as api-scope]
+ [metabase.documents.core :as documents]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.id :as lib.schema.id]
+ [metabase.metabot.scope :as scope]
[metabase.metabot.test-util :as test-util]
[metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.entity-details :as entity-details]
[metabase.metabot.tools.field-stats :as field-stats]
[metabase.metabot.tools.resources :as read-resource]
+ [metabase.metabot.tools.runtime :as runtime]
[metabase.metabot.tools.shared :as tools.shared]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
[metabase.metabot.tools.shared.llm-shape :as llm-shape]
@@ -29,6 +33,8 @@
[metabase.transforms.core :as transforms.core]
[toucan2.core :as t2]))
+(set! *warn-on-reflection* true)
+
(def ^:private uri-arg
"The tool's item schema, read off the tool so the assertions below compare against what it
actually publishes."
@@ -62,8 +68,8 @@
(deftest ^:parallel scalar-uris-arg-test
(testing "a scalar `uris` is rejected with guidance on how to repair the call"
- (is (= (str "Invalid tool arguments: `uris` must be an array of 1 to 5 URI strings; "
- "received a string.")
+ (is (= (str "Invalid tool arguments: `uris` must be a non-empty array of at most 5 URIs. "
+ "Request fewer URIs per call; received a string.")
(test-util/tool-boundary-error "read_resource" #'metabot.tools/read-resource-tool
{:uris "metabase://table/1"})))))
@@ -118,12 +124,117 @@
(is (= "metabase://database/1/schemas/weird%2Fname/tables" uri))
(is (= ["database" "1" "schemas" "weird/name" "tables"] (:segments parsed)))))))
-(deftest ^:parallel the-item-limit-is-a-schema-fact-test
- (testing "the 5-URI cap is in the batched `:args`, so the runtime rejects a sixth before the tool runs"
- (is (= (str "Invalid tool arguments: `uris` must be an array of 1 to 5 URI strings; "
- "received an array.")
- (test-util/tool-boundary-error "read_resource" #'metabot.tools/read-resource-tool
- {:uris (vec (repeat 10 "metabase://table/123"))})))))
+(deftest mixed-outcome-batch-test
+ (let [uris ["metabase://databases" "metabase://nonsense/1" "metabase://collections"
+ "metabase://databases" "metabase://nonsense/2"]
+ called (atom [])]
+ (mt/with-dynamic-fn-redefs [read-resource/dispatch
+ (fn [uri]
+ (swap! called conj uri)
+ (if (str/includes? uri "nonsense")
+ (read-resource/unsupported-uri! {:uri uri})
+ {:structured-output {:result-type :metabot-list :list-type :databases
+ :items [] :total 0 :page 1 :pages 1}}))]
+ (binding [scope/*current-user-scope* #{"*"}]
+ (is (nil? (test-util/tool-boundary-error "read_resource" #'metabot.tools/read-resource-tool
+ {:uris uris}))))
+ (reset! called [])
+ (let [{:keys [output resources]} (read-uris {:uris uris})]
+ (is (= uris @called))
+ (is (= uris (mapv :uri resources)))
+ (is (= [false true false false true] (mapv #(contains? % :error) resources)))
+ (is (= 5 (count (re-seq #"<resource uri=" output))))))))
+
+(deftest very-long-failing-batches-test
+ (doseq [n [6 30 1000 10000]]
+ (testing (str n " URIs are rejected before dispatch")
+ (let [uris (mapv #(str "metabase://nonsense/" %) (range n))
+ dispatched (atom [])
+ dispatch (mt/original-fn #'read-resource/dispatch)]
+ (mt/with-dynamic-fn-redefs [read-resource/dispatch
+ (fn [uri]
+ (swap! dispatched conj uri)
+ (dispatch uri))]
+ (let [result (runtime/invoke (tools/entries [read-resource/read-resource-tool])
+ {:profile-id :sql :metabot nil :user-scope api-scope/unrestricted
+ :tool-names #{"read_resource"}}
+ "read_resource" {:uris uris})]
+ (is (= :validation (get-in result [:error :class])))
+ (is (<= (count (:output result)) 4000))
+ (is (true? (str/includes? (:output result) "at most 5 URIs")))
+ (is (true? (str/includes? (:output result) "Request fewer URIs")))
+ (is (= [] @dispatched))))))))
+
+(deftest very-long-failing-uri-test
+ (let [uris [(str "metabase://nonsense/" (apply str (repeat 100000 "x")))
+ "metabase://nonsense/last"]
+ result (runtime/invoke (tools/entries [read-resource/read-resource-tool])
+ {:profile-id :sql :metabot nil :user-scope api-scope/unrestricted
+ :tool-names #{"read_resource"}}
+ "read_resource" {:uris uris})]
+ (is (= :validation (get-in result [:error :class])))
+ (is (<= (count (:output result)) 4000))
+ (is (true? (str/includes? (:output result) "4096")))))
+
+(deftest failed-batch-echo-budget-test
+ (doseq [tool-names [#{"read_resource"} #{"read_resource" "search"}]
+ prefix ["" "metabase://nonsense/" "metabase://collection/1/"
+ "metabase://collection/" "metabase://chart/"]
+ character ["x" "\"" "&" "\\" "\u08FF" "🦜"]]
+ (testing (str "prefix " (pr-str prefix) ", character " (pr-str character) ", tools " tool-names)
+ (let [uri (str prefix (apply str (repeat (quot (- 4096 (count prefix)) (count character)) character)))
+ result (runtime/invoke (tools/entries [read-resource/read-resource-tool])
+ {:profile-id :sql :metabot nil :user-scope api-scope/unrestricted
+ :tool-names tool-names}
+ "read_resource" {:uris (vec (repeat 5 uri))})
+ ^String output (:output result)
+ attributes (map second (re-seq #"<resource uri=\"([^\"]*)\">" output))
+ quoted-uris (for [line (str/split-lines output)
+ [start stop] [["**Error:** " " is not a Metabase resource URI."]
+ ["**Error:** No resource is served at " "."]
+ ["**Error:** Invalid id " " in URI."]
+ ["**Error:** No chart or query with id " " exists in this conversation"]]
+ :when (str/starts-with? line start)
+ :let [end (str/last-index-of line stop)]
+ :when end]
+ (subs line (count start) end))]
+ (is (= :metabase.metabot.tools.core/all-items-failed (get-in result [:error :code])))
+ (is (< (count output) 3500))
+ (is (= 5 (count attributes)))
+ (is (true? (every? #(<= (count %) 200) attributes)))
+ (is (true? (every? #(not (re-find #"&[^;]*$" %)) attributes)))
+ (is (seq quoted-uris))
+ (is (true? (every? #(<= (count %) 200) quoted-uris)))
+ (is (true? (str/includes? output "[truncated]")))
+ (is (str/ends-with? output "</resources>"))
+ (is (true? (= output (String. (.getBytes output "UTF-8") "UTF-8"))))))))
+
+(deftest all-failed-batch-retains-each-uri-test
+ (doseq [tool-names [#{"read_resource"} #{"read_resource" "search"}]
+ uris [(mapv #(str "metabase://nonsense/" %) (range 5))
+ (mapv (fn [n]
+ (let [prefix (str "metabase://nonsense/" n "/")]
+ (str prefix (apply str (repeat (- 4096 (count prefix)) "x")))))
+ (range 5))]]
+ (let [result (runtime/invoke (tools/entries [read-resource/read-resource-tool])
+ {:profile-id :sql :metabot nil :user-scope api-scope/unrestricted
+ :tool-names tool-names}
+ "read_resource" {:uris uris})
+ output (:output result)]
+ (is (= :recoverable (get-in result [:error :class])))
+ (is (= (count uris) (count (re-seq #"<resource uri=" output))))
+ (is (every? #(str/includes? output (str "uri=\"" (subs % 0 (min 80 (count %))))) uris))
+ (is (str/ends-with? output "</resources>")))))
+
+(deftest few-failures-keep-full-error-text-test
+ (doseq [uris [["metabase://collection/5/children"]
+ ["metabase://collection/5/children" "metabase://collection/VZbHZIeqQ2HhZv5r0pO6a"]]]
+ (let [{:keys [output]} (runtime/invoke (tools/entries [read-resource/read-resource-tool])
+ {:profile-id :sql :metabot nil :user-scope api-scope/unrestricted
+ :tool-names (:tool-names read-ctx)}
+ "read_resource" {:uris uris})]
+ (doseq [{:keys [error]} (:resources (read-uris {:uris uris}))]
+ (is (str/includes? output error))))))
(deftest ^:parallel single-and-batched-declarations-test
(let [single (tools/declaration read-resource/read-resource-tool)
@@ -135,8 +246,12 @@
(testing "the batched form publishes a sequence of plain strings, not of maps —
the wire shape the model has always been offered"
(is (= [:map {:closed true}
- [:uris [:sequential {:min 1 :max 5
- :error/message "must be an array of 1 to 5 URI strings"}
+ [:uris [:sequential {:min 1
+ :max 5
+ :description (str "A non-empty array of at most 5 URIs, each at most 4096 characters. "
+ "Request fewer URIs or shorten them to fit these limits.")
+ :error/message (str "must be a non-empty array of at most 5 URIs. "
+ "Request fewer URIs per call")}
uri-arg]]]
(:args batched))))
(testing "both forms share the item schema, so they cannot drift"
@@ -275,13 +390,13 @@
["metabase://model/4/fields" :card-fields ["model" "4"]]
["metabase://model/4/fields/99" :card-field ["model" "4" "99"]]
["metabase://model/4/fields/c75/17" :card-field ["model" "4" "c75/17"]]
- ["metabase://model/4/sources" :card-sources ["4" nil]]
- ["metabase://model/4/sources?page=2" :card-sources ["4" {:page "2"}]]
+ ["metabase://model/4/sources" :card-sources ["model" "4" nil]]
+ ["metabase://model/4/sources?page=2" :card-sources ["model" "4" {:page "2"}]]
;; ----- Question (a card type) -----
["metabase://question/5" :card ["question" "5"]]
["metabase://question/5/fields" :card-fields ["question" "5"]]
["metabase://question/5/fields/99" :card-field ["question" "5" "99"]]
- ["metabase://question/5/sources" :card-sources ["5" nil]]
+ ["metabase://question/5/sources" :card-sources ["question" "5" nil]]
;; ----- Metric -----
["metabase://metric/6" :metric ["6"]]
["metabase://metric/6/dimensions" :metric-dimensions ["6"]]
@@ -374,7 +489,7 @@
(is (thrown-with-msg? Exception #"URIs use the numeric entity id"
(#'read-resource/dispatch "metabase://table/orders")))
(is (thrown-with-msg? Exception #"URIs use the numeric entity id"
- (#'read-resource/dispatch "metabase://collection/root"))))
+ (#'read-resource/dispatch "metabase://collection/our-analytics"))))
(testing "the error carries agent-error metadata"
(let [e (try
(#'read-resource/dispatch "metabase://model/VZbHZIeqQ2HhZv5r0pO6a")
@@ -428,8 +543,8 @@
(is (=? {:resources [{:content {:structured-output map?}}]}
result))
(is (str/includes? (:output result) dashboard-name))))
- (testing "rejects sub-resources"
- (is (=? {:resources [{:error string?}]}
+ (testing "fetches dashboard items through the cards alias"
+ (is (=? {:resources [{:content {:structured-output {:list-type :dashboard-items}}}]}
(read-uris {:uris [(str "metabase://dashboard/" dashboard-id "/cards")]}))))
(testing "returns error for unknown dashboard"
(is (=? {:resources [{:error string?}]}
@@ -1489,14 +1604,6 @@
(is (= :recoverable class))
(is (str/includes? text (str "**Error:** Table " (mt/id :products) " was not found.")))))))
-(deftest read-inactive-field-parent-test
- (mt/with-temp [:model/Table {table-id :id} {:db_id (mt/id) :active false}]
- (mt/with-current-user (mt/user->id :crowberto)
- (let [uri (str "metabase://table/" table-id "/fields/123")
- {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
- (is (= :recoverable class))
- (is (str/includes? text (str "**Error:** Table " table-id " was not found.")))))))
-
(deftest read-field-refusal-test
(mt/with-current-user (mt/user->id :crowberto)
(doseq [status [403 404]]
@@ -1944,3 +2051,117 @@
(let [output (:output (read-uris {:uris [(str "metabase://table/" (mt/id :orders))]}))]
(is (str/includes? output "### Related Tables") "premise: ORDERS renders related tables")
(is (not (re-find #"(?i)for SQL queries" output)))))))
+
+(deftest card-kind-canonical-uri-test
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :products)))]
+ (mt/with-temp [:model/Card {question-id :id} {:type :question :dataset_query query}
+ :model/Card {model-id :id} {:type :model :dataset_query query}
+ :model/Card {metric-id :id} {:type :metric :dataset_query (lib/aggregate query (lib/count))}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [[actual id] [["question" question-id] ["model" model-id] ["metric" metric-id]]
+ requested ["question" "model"]
+ :when (not= actual requested)
+ suffix ["" "/fields" "/fields/123" "/sources"]
+ :let [canonical-suffix (if (= actual "metric")
+ (case suffix "/fields" "/dimensions"
+ "/fields/123" "/dimensions/123" "")
+ suffix)
+ uri (str "metabase://" requested "/" id suffix)
+ canonical (str "metabase://" actual "/" id canonical-suffix)]]
+ (testing uri
+ (is (= (str "Card " id " is a " actual ", not a " requested ".\n"
+ "Read `" canonical "` instead.")
+ (-> (read-uris {:uris [uri]}) :resources first :error)))))))))
+
+(deftest inactive-table-resources-test
+ (mt/with-temp [:model/Table {id :id} {:db_id (mt/id) :active false}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [suffix ["" "/fields" "/fields/123" "/derived"]]
+ (let [uri (str "metabase://table/" id suffix)]
+ (testing uri
+ (is (str/starts-with? (or (-> (read-uris {:uris [uri]}) :resources first :error) "")
+ (str "Table " id " is inactive.")))))))))
+
+(deftest missing-schema-test
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table _ {:db_id db-id :schema "PUBLIC" :active true}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [schema ["public" "missing"]]
+ (let [uri (str "metabase://database/" db-id "/schemas/" schema "/tables")
+ error (-> (read-uris {:uris [uri]}) :resources first :error)]
+ (is (str/includes? (or error "") "Schema names are case-sensitive."))
+ (is (str/includes? (or error "") (str "metabase://database/" db-id "/schemas")))))
+ (is (not (error? (read-uris {:uris [(str "metabase://database/" db-id "/schemas/PUBLIC/tables")]})))))))
+
+(deftest root-collection-resources-test
+ (mt/with-temp [:model/Collection {coll-id :id} {:location "/"}
+ :model/Card {card-id :id} {:collection_id nil}
+ :model/Dashboard {dash-id :id} {:collection_id nil}
+ :model/Document {doc-id :id} {:collection_id nil}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [[suffix ids] [["" #{["collection" "root"]}]
+ ["/items" #{["collection" coll-id] ["question" card-id]
+ ["dashboard" dash-id] ["document" doc-id]}]
+ ["/subcollections" #{["collection" coll-id]}]]]
+ (let [result (read-uris {:uris [(str "metabase://collection/root" suffix)]})
+ structured (get-in result [:resources 0 :content :structured-output])]
+ (is (not (error? result)))
+ (is (set/subset? ids (set (map (juxt :type :id) (if (= suffix "") [structured] (:items structured)))))))))
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (error? (read-uris {:uris ["metabase://collection/root"]})))
+ (doseq [suffix ["/items" "/subcollections"]]
+ (let [ids (->> (read-uris {:uris [(str "metabase://collection/root" suffix)]})
+ :resources first :content :structured-output :items
+ (map (juxt :type :id))
+ set)]
+ (is (contains? ids ["collection" coll-id]))
+ (is (empty? (set/intersection ids #{["question" card-id] ["dashboard" dash-id] ["document" doc-id]})))))))))
+
+(deftest root-navigation-excludes-other-namespaces-and-trash-test
+ (mt/with-temp [:model/Collection {root-id :id} {:name "Default root" :location "/"}
+ :model/Collection {child-id :id} {:name "Nested child" :location (str "/" root-id "/")}
+ :model/Collection _ {:name "Snippet root" :namespace "snippets" :location "/"}
+ :model/Collection _ {:name "Archived root" :archived true :location "/"}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [collection-ids (fn [uri]
+ (->> (read-uris {:uris [uri]})
+ :resources first :content :structured-output :items
+ (filter #(= "collection" (:type %)))
+ (map :id)
+ set))
+ expected (collection-ids "metabase://collections")]
+ (is (contains? expected root-id))
+ (doseq [suffix ["/items" "/subcollections"]]
+ (is (= expected (collection-ids (str "metabase://collection/root" suffix))))
+ (doseq [suffix ["/items" "/subcollections"]]
+ (is (= #{child-id} (collection-ids (str "metabase://collection/" root-id suffix))))))))))
+
+(deftest ^:parallel document-numeric-id-test
+ (let [uri "metabase://document/not-a-number"
+ error (-> (read-uris {:uris [uri]}) :resources first :error)]
+ (is (str/includes? (or error "") "Resource URIs use the numeric entity id"))))
+
+(deftest document-read-view-test
+ (mt/with-temporary-setting-values [synchronous-batch-updates true]
+ (mt/with-temp [:model/Document {id :id} {:name "Plan" :view_count 0}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (is (not (error? (read-uris {:uris [(str "metabase://document/" id)]}))))
+ (is (= 0 (t2/select-one-fn :view_count :model/Document id)))
+ (documents/get-document id)
+ (is (= 1 (t2/select-one-fn :view_count :model/Document id)))))))
+
+(deftest ^:parallel uri-aspect-aliases-test
+ (doseq [[alias canonical] [["metabase://table/1/columns/2" "metabase://table/1/fields/2"]
+ ["metabase://model/1/columns" "metabase://model/1/fields"]
+ ["metabase://question/1/columns/2" "metabase://question/1/fields/2"]
+ ["metabase://dashboard/1/cards?page=2" "metabase://dashboard/1/items?page=2"]]]
+ (is (= (#'read-resource/parse-uri canonical) (#'read-resource/parse-uri alias)))))
+
+(deftest ^:parallel description-lists-uri-forms-test
+ (let [description (:description (tools/validate-tool! read-resource/read-resource-tool))]
+ (doseq [uri (mapcat val @#'read-resource/uri-templates)]
+ (is (str/includes? description uri) uri))
+ (doseq [wording ["collection/root" "columns" "cards" "case-sensitive" "Up to 5 URIs"]]
+ (is (str/includes? description wording) wording))))
diff --git a/test/metabase/metabot/tools/runtime_test.clj b/test/metabase/metabot/tools/runtime_test.clj
index 9b401b33cb7..a229a47a52a 100644
--- a/test/metabase/metabot/tools/runtime_test.clj
+++ b/test/metabase/metabot/tools/runtime_test.clj
@@ -384,11 +384,11 @@
;; whole catalog against every tool name in the product rather than one call against one profile.
(deftest ^:parallel render-caps-the-output-test
- (let [long-message (apply str (repeat 5000 "x"))
- outcome (tools.runtime/render {:class :validation :code :invalid-arguments
- :message long-message}
- all-tool-names)]
- (is (= 4000 (count (:output outcome))))))
+ (doseq [error [{:class :validation :code :invalid-arguments}
+ {:class :recoverable :code ::tools/all-items-failed :recovery []}]]
+ (let [outcome (tools.runtime/render (assoc error :message (apply str (repeat 5000 "x")))
+ all-tool-names)]
+ (is (= 4000 (count (:output outcome)))))))
(deftest ^:parallel runtime-codes-are-declared-test
(testing "the agent loop and the adapters branch on these, so the list is shared rather than