Skip to content

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

Fix transform writing tools (#71100)

lbrdnk839784b50cfa
diff --git a/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms.clj b/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms.clj
index 350cfcac188..9841b37c42a 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms.clj
@@ -30,7 +30,7 @@
(tools.transforms/add-output (transforms-write-tools/get-transform-python-library-details {:path path})
format-python-library-output))
-(defenterprise-schema write-transform-python-tool
+(defenterprise-schema write-transform-python-tool :- :map
"Write new Python code or edit existing code for transforms.
Supports two modes:
@@ -45,21 +45,8 @@
Keep `import common` at the top of the file even if it is currently unused."
:feature :none
[{:keys [transform_id edit_action thinking transform_name transform_description
- source_database source_tables]}
- :- [:map {:closed true}
- [:transform_id {:optional true} [:maybe :int]]
- [:edit_action [:map
- [:mode [:enum "edit" "replace"]]
- [:edits {:optional true} [:maybe [:sequential [:map
- [:old_string :string]
- [:new_string :string]
- [:replace_all {:optional true} [:maybe :boolean]]]]]]
- [:new_content {:optional true} [:maybe :string]]]]
- [:thinking {:optional true} [:maybe :string]]
- [:transform_name {:optional true} [:maybe :string]]
- [:transform_description {:optional true} [:maybe :string]]
- [:source_database {:optional true} [:maybe :int]]
- [:source_tables {:optional true} [:maybe :map]]]]
+ database_id source_tables]}
+ :- tools.transforms/write-transform-python-schema]
(try
(tools.transforms/add-output
(transforms-write-tools/write-transform-python
@@ -68,7 +55,7 @@
:thinking thinking
:transform_name transform_name
:transform_description transform_description
- :source_database source_database
+ :source_database database_id
:source_tables source_tables
:memory-atom shared/*memory-atom*
:context (shared/current-context)})
diff --git a/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms/write.clj b/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms/write.clj
index 1c67850fcbf..f94664e73f5 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms/write.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot/tools/transforms/write.clj
@@ -55,7 +55,7 @@
{:agent-error? true
:transform-id transform_id})))
- current-python (get-in current-transform [:source :query] "")
+ current-python (get-in current-transform [:source :body] "")
;; Apply edits based on mode
new-python (case (:mode edit_action)
@@ -70,12 +70,12 @@
;; Build suggested transform
suggested-transform (cond-> current-transform
- true (assoc-in [:source :query] new-python)
- true (assoc-in [:source :type] "python")
- transform_name (assoc :name transform_name)
+ transform_name (assoc :name transform_name)
transform_description (assoc :description transform_description)
- source_database (assoc-in [:source :source-database] source_database)
- source_tables (assoc-in [:source :source-tables] source_tables))]
+ ;; source adjustments
+ source_database (assoc-in [:source :source-database] source_database)
+ source_tables (assoc-in [:source :source-tables] source_tables)
+ true (assoc-in [:source :body] new-python))]
;; Store in memory if we have an ID
(when (and transform_id memory-atom)
diff --git a/enterprise/backend/test/metabase_enterprise/metabot/tools/transforms/write_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/tools/transforms/write_test.clj
index 6bfc67f1b11..1b1ccf8ad4b 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot/tools/transforms/write_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot/tools/transforms/write_test.clj
@@ -2,14 +2,15 @@
(:require
[clojure.test :refer :all]
[metabase-enterprise.metabot.tools.transforms.write :as transforms-write]
- [metabase.metabot.tools.transforms.write :as oss-transforms-write]))
+ [metabase.metabot.tools.transforms.write :as oss-transforms-write]
+ [metabase.test :as mt]))
(deftest create-fresh-python-template-test
(testing "fresh Python transforms include common import"
(let [result (oss-transforms-write/create-fresh-transform
- :python "Python Transform" nil nil nil)]
+ :python "Python Transform" nil 1 nil)]
(is (= "import common\nimport pandas as pd\n\ndef transform():\n # Your transformation logic here\n return pd.DataFrame([{\"message\": \"Hello from Python transform!\"}])\n"
- (get-in result [:source :query]))))))
+ (get-in result [:source :body]))))))
(deftest create-fresh-python-transform-test
(testing "creates fresh Python transform"
@@ -18,6 +19,8 @@
{:edit_action {:mode "replace"
:new_content "def transform():\n return pd.DataFrame()"}
:transform_name "Python Transform"
+ :source_database (mt/id)
+ :source_tables [{:alias "t" :table_id 1 :schema "PUBLIC" :database_id 1}]
:memory-atom memory-atom})]
(is (= "Python Transform" (get-in result [:structured-output :transform :name])))
(is (= "python" (get-in result [:structured-output :transform :source :type]))))))
diff --git a/resources/metabot/prompts/tools/write_transform_python.md b/resources/metabot/prompts/tools/write_transform_python.md
new file mode 100644
index 00000000000..70ec5c9da01
--- /dev/null
+++ b/resources/metabot/prompts/tools/write_transform_python.md
@@ -0,0 +1,58 @@
+Write new Python code or edit existing code using exact string matching with safety checks for ambiguous edits.
+
+It is useful for modifying or fixing Python code based on user requests, e.g. when modifying a Python-based transform.
+This will not execute the Python code but only update the content of a Python transform
+(and therefore display the updated code to the user). You MUST never offer to apply the changes for the
+user, these are only suggestions which they must apply themselves.
+
+**Creating New Transforms:**
+When creating a new transform, you must provide:
+- transform_name: A descriptive name for the transform
+- transform_description: A detailed description of what the transform does
+- source_database: The database ID containing the source tables
+- source_tables: A list of source table objects, each with `alias` (parameter name in the transform function), `table_id` (database table ID), `schema` (e.g. "PUBLIC"), and `database_id`
+
+**Edit Modes:**
+
+1. **Edit Mode** (mode="edit") - For targeted string replacements:
+ - Use when making small, specific changes to existing Python code
+ - Provide list of edits with old_string, new_string, and optional replace_all
+ - Each edit must find exact matches or will fail
+
+2. **Replace Mode** (mode="replace") - For replacing the entire query:
+ - Use when creating a new Python code artifact
+ - Use when rewriting large portions or completely changing the code structure
+ - More token-efficient for major changes
+ - Provide the complete new Python code
+**Safety Features:**
+- PartialEdits FAIL if old_string matches multiple locations (unless replace_all=true)
+- Either provide more surrounding context to make matches unique, or use replace_all=true
+- All edits must succeed or none are applied (atomic operations)
+
+**Usage Examples:**
+- Complete replacement: {"mode": "replace", "new_content": "import pandas as pd\n\ndef transform(my_table_df):\n # New transformation logic here\n return my_table_df\n"}
+- Targeted edit: {"mode": "edit", "edits": [{"old_string": "price - discount", "new_string": "price * (1 - discount)"}]}
+- Global rename: {"mode": "edit", "edits": [{"old_string": "users", "new_string": "customers", "replace_all": true}]}
+
+**Best Practices:**
+- Use mode="replace" for major query changes (more token efficient)
+- Use mode="edit" for small targeted changes
+- Include surrounding context (whitespace, adjacent lines) to make PartialEdit matches unique
+- Use replace_all=true only when you want to change ALL occurrences in PartialEdit
+- Use the search_tables tool to find correct source_tables first - never guess or make up table IDs
+- Use real database tables for source_tables. NEVER use metabase models or model IDs in source_tables.
+- Copy exact text including all spaces, tabs, and newlines for PartialEdit
+- Use this tool for any Python-related requests when a Python transform is present in the context
+- Only use when assisting the user with editing or creating Python code
+- Never attempt to execute Python code or make changes outside the current user context
+- Take into account the Python environment you are working with if that information is
+ available in the artifact. This will help you to use the correct syntax and functions
+- When creating new transforms, provide meaningful names and descriptions that clearly explain the transform's purpose
+- Use `get_transform_python_library_details` before writing any Python code to inspect the shared library.
+- Use the shared library in your code by adding `import common` at the top of the file.
+- Keep `import common` at the top of the file even if it is currently unused.
+
+**Limitations:**
+* This tool does not execute Python code, it only modifies the source code text.
+* Edits must be unambiguous and uniquely identifiable within the code context.
+* You can not apply the edits for the user. Never say things like, "Let me know if you'd like me to apply this change".
\ No newline at end of file
diff --git a/resources/metabot/prompts/tools/write_transform_sql.md b/resources/metabot/prompts/tools/write_transform_sql.md
new file mode 100644
index 00000000000..f9ac4cabb40
--- /dev/null
+++ b/resources/metabot/prompts/tools/write_transform_sql.md
@@ -0,0 +1,56 @@
+Write new SQL queries or edit existing queries using exact string matching with safety checks for ambiguous edits.
+
+It is useful for modifying or fixing SQL queries, adding or changing clauses, or restructuring queries
+based on user requests. This will not execute the SQL query but only update the content of a SQL artifact
+(and therefore display the updated query to the user). You MUST never offer to apply the changes for the
+user, these are only suggestions which they must apply themselves.
+
+**Creating New Transforms:**
+When creating a new transform, you must provide:
+- transform_name: A descriptive name for the transform
+- transform_description: A detailed description of what the transform does
+- database_id: The database ID for the new transform
+
+**Edit Modes:**
+
+1. **Edit Mode** (mode="edit") - For targeted string replacements:
+ - Use when making small, specific changes to existing SQL
+ - Provide list of edits with old_string, new_string, and optional replace_all
+ - Each edit must find exact matches or will fail
+
+2. **Replace Mode** (mode="replace") - For replacing the entire query:
+ - Use when creating a new query
+ - Use when rewriting large portions or completely changing the query structure
+ - More token-efficient for major changes
+ - Provide the complete new SQL query
+
+**Safety Features:**
+- PartialEdits FAIL if old_string matches multiple locations (unless replace_all=true)
+- Either provide more surrounding context to make matches unique, or use replace_all=true
+- All edits must succeed or none are applied (atomic operations)
+
+**Usage Examples:**
+- Complete replacement: {"mode": "replace", "new_content": "SELECT * FROM users WHERE active = true"}
+- Targeted edit: {"mode": "edit", "edits": [{"old_string": "SELECT *", "new_string": "SELECT id, name"}]}
+- Global rename: {"mode": "edit", "edits": [{"old_string": "users", "new_string": "customers", "replace_all": true}]}
+
+**Best Practices:**
+- Use mode="replace" for major query changes (more token efficient)
+- Use mode="edit" for small targeted changes
+- Include surrounding context (whitespace, adjacent lines) to make PartialEdit matches unique
+- Use replace_all=true only when you want to change ALL occurrences in PartialEdit
+- Copy exact text including all spaces, tabs, and newlines for PartialEdit
+- Use this tool for any SQL-related requests when a SQL artifact is present in the context
+- Only use when assisting the user with editing or creating SQL queries
+- Never attempt to execute SQL or make changes outside the current user context
+- Take into account the SQL engine you are working with if that information is
+ available in the artifact. This will help you to use the correct syntax and functions
+
+- When querying Metabase models, remember that their fully qualified name is of the form `{{#model_id}}`,
+ e.g. `SELECT * FROM {{#5}}`
+
+**Limitations:**
+* This tool does not execute SQL queries, it only modifies the query text.
+* Edits must be unambiguous and uniquely identifiable within the query context.
+* You can only query tables and Metabase models; Metabase metrics are not supported in SQL for now
+* You can not apply the edits for the user. Never say things like, "Let me know if you'd like me to apply this change".
\ No newline at end of file
diff --git a/src/metabase/metabot/agent/profiles.clj b/src/metabase/metabot/agent/profiles.clj
index 794d6f26576..35db16c2908 100644
--- a/src/metabase/metabot/agent/profiles.clj
+++ b/src/metabase/metabot/agent/profiles.clj
@@ -183,14 +183,34 @@
;; gracefully without requiring a map update.
(keyword (-> (str cap) (str/replace ":" "-") (str/replace "_" "-"))))))
+;; TODO (lbrdnk 2026-03-18): `api-string->capability-keyword` hints there is need for feature snippets. I was unable
+;; to find matching logic in ai-service. I'll remove that in followup with related logic.
+(comment
+ (filter #(re-find #"snippet" (name %)) (keys (premium-features/token-features))))
+ ;;=> (:snippet_collections) <--- very probably we do not want to sentinel on this feature
+
+(def relevant-features
+ "Metabase features to add to capabilities set."
+ #{:transforms :transforms-python})
+
+(defn feature-capability-kws
+ "Get capabilities per features of this Metabase instance. Not cached to avoid staleness."
+ []
+ (into #{}
+ (comp (filter premium-features/has-feature?)
+ (map #(keyword (str "feature-" (name %)))))
+ relevant-features))
+
(defn- filter-by-capabilities
"Filter tool vars by user capabilities.
Removes tools that require capabilities the user doesn't have.
Capabilities from the API arrive as strings (e.g. \"frontend:navigate_user_v1\")
- while tool definitions use keywords (e.g. :frontend-navigate-user-v1), so we
+ while tool metadata uses keywords (e.g. :frontend-navigate-user-v1), so we
normalize to keywords before comparing."
[tool-vars capabilities]
- (let [capabilities-set (into #{} (map capability->keyword) capabilities)]
+ (let [capabilities-set (into (feature-capability-kws)
+ (map capability->keyword)
+ capabilities)]
(filter (fn [tool-var]
(every? capabilities-set (:capabilities (meta tool-var))))
tool-vars)))
diff --git a/src/metabase/metabot/tools/transforms.clj b/src/metabase/metabot/tools/transforms.clj
index 1b29f5c7ab2..b8e7d7472a6 100644
--- a/src/metabase/metabot/tools/transforms.clj
+++ b/src/metabase/metabot/tools/transforms.clj
@@ -103,21 +103,50 @@
(def ^:private write-transform-sql-schema
[:map {:closed true}
- [:transform_id {:optional true} [:maybe :int]]
- [:edit_action [:map
- [:mode [:enum "edit" "replace"]]
- [:edits {:optional true} [:maybe [:sequential [:map
- [:old_string :string]
- [:new_string :string]
- [:replace_all {:optional true} [:maybe :boolean]]]]]]
- [:new_content {:optional true} [:maybe :string]]]]
- [:thinking {:optional true} [:maybe :string]]
- [:transform_name {:optional true} [:maybe :string]]
- [:transform_description {:optional true} [:maybe :string]]
- [:source_database {:optional true} [:maybe :int]]
- [:source_tables {:optional true} [:maybe :map]]])
-
-(mu/defn ^{:tool-name "write_transform_sql"
+ [:database_id
+ {:optional true
+ :description (str "The database id of the database that contains the tables referenced in the query. "
+ "You MUST never select something that looks like a sample database with sample tables. ")}
+ [:maybe :int]]
+ [:transform_id
+ {:optional true
+ :description (str "The ID of the Transform with the SQL query to edit. "
+ "If not provided it's assumed a new Transform is to be created")}
+ [:maybe :int]]
+ [:edit_action
+ {:description "You MUST set this param. Use new_content and edits according to mode you choose."}
+ [:map
+ [:mode
+ {:description (str "Use 'edit' mode for targeted string replacements. "
+ "Use 'replace' mode to replace entire content.")}
+ [:enum "edit" "replace"]]
+ [:edits
+ {:optional true
+ :description "List of targeted string replacements to apply sequentially"}
+ [:maybe [:sequential [:map
+ [:old_string :string]
+ [:new_string :string]
+ [:replace_all {:optional true} [:maybe :boolean]]]]]]
+ [:new_content
+ {:optional true
+ :description "The complete new content to replace the current content with"}
+ [:maybe :string]]]]
+ [:thinking
+ {:optional true
+ :description "Brief explanation of what changes you're making and why"}
+ [:maybe :string]]
+ [:transform_name
+ {:optional true
+ :description (str "A descriptive name for the transform. Required when creating a new transform. "
+ "Do not provide when editing an existing transform with a transform_id.")}
+ [:maybe :string]]
+ [:transform_description
+ {:optional true
+ :description (str "A short description of what the transform does. Required when creating a new transform. "
+ "Do not provide when editing an existing transform with a transform_id.")}
+ [:maybe :string]]])
+
+(mu/defn ^{:tool-name "write_transform_sql"
:capabilities #{:feature-transforms :permission-write-transforms}}
write-transform-sql-tool
"Write new SQL queries or edit existing queries for transforms.
@@ -129,7 +158,8 @@
For edit mode, provide edits as an array of {old_string, new_string, replace_all} objects.
For replace mode, provide new_content with the complete SQL."
[{:keys [transform_id edit_action thinking transform_name transform_description
- source_database source_tables]} :- write-transform-sql-schema]
+ database_id source_tables]}
+ :- write-transform-sql-schema]
(try
(let [result (add-output
(transforms-write-tools/write-transform-sql
@@ -138,7 +168,7 @@
:thinking thinking
:transform_name transform_name
:transform_description transform_description
- :source_database source_database
+ :database_id database_id
:source_tables source_tables
:memory-atom shared/*memory-atom*
:context (shared/current-context)})
@@ -152,8 +182,76 @@
{:output (ex-message e)}
{:output (str "Failed to write SQL transform: " (or (ex-message e) "Unknown error"))}))))
+(def write-transform-python-schema
+ "Schema for write-transform-python-tool"
+ [:map {:closed true}
+ [:transform_id
+ {:optional true
+ :description (str "The ID of the Transform with the SQL query to edit. "
+ "If not provided it's assumed a new Transform is to be created")}
+ [:maybe :int]]
+ [:edit_action
+ {:description "You MUST set this param. Use new_content and edits according to mode you choose."}
+ [:map
+ [:mode
+ {:description (str "Use 'edit' mode for targeted string replacements. "
+ "Use 'replace' mode to replace entire content.")}
+ [:enum "edit" "replace"]]
+ [:edits
+ {:optional true
+ :description "List of targeted string replacements to apply sequentially"}
+ [:maybe [:sequential [:map
+ [:old_string :string]
+ [:new_string :string]
+ [:replace_all {:optional true} [:maybe :boolean]]]]]]
+ [:new_content
+ {:optional true
+ :description "The complete new content to replace the current content with"}
+ [:maybe :string]]]]
+ [:thinking
+ {:optional true
+ :description "Brief explanation of what changes you're making and why"}
+ [:maybe :string]]
+ [:transform_name
+ {:optional true
+ :description (str "A descriptive name for the transform. Required when creating a new transform. "
+ "Do not provide when editing an existing transform with a transform_id.")}
+ [:maybe :string]]
+ [:transform_description
+ {:optional true
+ :description (str "A short description of what the transform does. Required when creating a new transform. "
+ "Do not provide when editing an existing transform with a transform_id.")}
+ [:maybe :string]]
+ [:database_id
+ {:optional true
+ :description (str "When creating a Transform, the database id of the "
+ "tables being used to create the transform. Not provided when editing "
+ "an existing transform. You MUST never select something that looks "
+ "like a sample database with sample tables. ")}
+ [:maybe :int]]
+ [:source_tables
+ {:optional true
+ :description (str "A list of source tables, each described as an object with: "
+ "`alias` (the name used in the transform function, e.g. the parameter name in "
+ "`def transform(table_a, table_b):`), "
+ "`table_id` (the database table ID), "
+ "`schema` (the database schema name, e.g. \"PUBLIC\"), and "
+ "`database_id` (the database ID). "
+ "For example: [{\"alias\": \"table_a\", \"table_id\": 1, \"schema\": \"PUBLIC\", \"database_id\": 1}, "
+ "{\"alias\": \"table_b\", \"table_id\": 2, \"schema\": \"PUBLIC\", \"database_id\": 1}]. "
+ "The table_id values MUST be IDs of database tables. You CAN NOT use metabase model IDs. "
+ "You MUST provide this argument when modifying the source tables of an existing transform "
+ "or when creating a new transform. DO NOT guess or make up table IDs, use the "
+ "search_tables tool to find the correct table IDs first.")}
+ [:sequential
+ [:map
+ [:alias :string]
+ [:table_id :int]
+ [:schema :string]
+ [:database_id :int]]]]])
+
(defenterprise ^{:tool-name "write_transform_python"
- :schema [:=> [:cat write-transform-sql-schema] :map]
+ :schema [:=> [:cat write-transform-python-schema] :map]
:capabilities #{:feature-transforms :feature-transforms-python :permission-write-transforms}
:ee-feature :transforms}
write-transform-python-tool
diff --git a/src/metabase/metabot/tools/transforms/write.clj b/src/metabase/metabot/tools/transforms/write.clj
index 3625cd60198..d9713c493c2 100644
--- a/src/metabase/metabot/tools/transforms/write.clj
+++ b/src/metabase/metabot/tools/transforms/write.clj
@@ -3,6 +3,8 @@
Supports both partial edits (targeted string replacements) and full replacement modes."
(:require
[clojure.string :as str]
+ [metabase.lib-be.core :as lib-be]
+ [metabase.lib.core :as lib]
[metabase.metabot.agent.streaming :as streaming]
[metabase.util.log :as log]))
@@ -63,21 +65,31 @@ def transform():
return pd.DataFrame([{\"message\": \"Hello from Python transform!\"}])
")
+(defn- source-for-transform-type
+ [source-type database-id source-tables]
+ (case source-type
+ :sql (let [mp (lib-be/application-database-metadata-provider database-id)]
+ {:type "query"
+ :query (lib/native-query mp fresh-sql-template)})
+ :python {:type "python"
+ :body fresh-python-template
+ :source-database database-id
+ :source-tables source-tables}
+ (throw (ex-info "Unexpected source-type `%s`"
+ {:source-type source-type}))))
+
(defn create-fresh-transform
"Create a fresh transform structure with the given source type."
- [source-type transform-name transform-description source-database source-tables]
- (let [template (case source-type
- :sql fresh-sql-template
- :python fresh-python-template)]
- {:id nil
- :name (or transform-name (str "New " (name source-type) " Transform"))
- :description (or transform-description "")
- :source {:type (name source-type)
- :query template
- :source-database source-database
- :source-tables (or source-tables {})}
- :target {:type "table"
- :name ""}}))
+ [source-type transform-name transform-description database-id source-tables]
+ {:id nil
+ :name (or transform-name (str "New " (name source-type) " Transform"))
+ :description (or transform-description "")
+ :target {:type "table"
+ :name ""
+ :database database-id
+ :schema nil}
+ :source (source-for-transform-type
+ source-type database-id source-tables)})
;;; Write Transform SQL Tool
@@ -99,7 +111,7 @@ def transform():
- :structured-output - The suggested transform
- :data-parts - Transform suggestion data part for streaming"
[{:keys [transform_id edit_action thinking transform_name transform_description
- source_database source_tables memory-atom context]}]
+ database_id source_tables memory-atom context]}]
(log/info "Writing SQL transform" {:transform-id transform_id
:edit-mode (:mode edit_action)
:has-context (some? context)})
@@ -117,7 +129,7 @@ def transform():
;; Create fresh transform
:else
(create-fresh-transform :sql transform_name transform_description
- source_database source_tables))
+ database_id source_tables))
_ (when (and transform_id (nil? current-transform))
(throw (ex-info (str "Transform with ID " transform_id " not found. "
@@ -125,7 +137,8 @@ def transform():
{:agent-error? true
:transform-id transform_id})))
- current-sql (get-in current-transform [:source :query] "")
+ current-sql (some-> (get-in current-transform [:source :query])
+ lib/raw-native-query)
;; Apply edits based on mode
new-sql (case (:mode edit_action)
@@ -140,11 +153,10 @@ def transform():
;; Build suggested transform
suggested-transform (cond-> current-transform
- true (assoc-in [:source :query] new-sql)
+ true (update-in [:source :query] lib/with-native-query new-sql)
transform_name (assoc :name transform_name)
transform_description (assoc :description transform_description)
- source_database (assoc-in [:source :source-database] source_database)
- source_tables (assoc-in [:source :source-tables] source_tables))]
+ database_id (assoc-in [:source :database] database_id))]
;; Store in memory if we have an ID
(when (and transform_id memory-atom)
diff --git a/test/metabase/metabot/agent/profiles_test.clj b/test/metabase/metabot/agent/profiles_test.clj
index 9e3824f7d1a..173370476c8 100644
--- a/test/metabase/metabot/agent/profiles_test.clj
+++ b/test/metabase/metabot/agent/profiles_test.clj
@@ -2,6 +2,8 @@
(:require
[clojure.test :refer :all]
[metabase.metabot.agent.profiles :as profiles]
+ [metabase.metabot.tools.transforms :as tools.transforms]
+ [metabase.premium-features.core :as premium-features]
[metabase.test :as mt]))
(deftest get-profile-test
@@ -158,3 +160,24 @@
(is (not (contains? tools "create_sql_query"))
"SQL tools should be gated by permission:write_sql_queries")
(is (not (contains? tools "create_autogenerated_dashboard")))))))
+
+(deftest transform-feature-capabilities-test
+ (let [orig-has-feature premium-features/has-feature?
+ transform-tools #{#'tools.transforms/write-transform-sql-tool
+ #'tools.transforms/write-transform-python-tool}]
+ (testing "Available with features present"
+ (with-redefs [premium-features/has-feature? (fn [feat]
+ (if (#'profiles/relevant-features feat)
+ true
+ (orig-has-feature feat)))]
+ (is (= transform-tools
+ (set (#'profiles/filter-by-capabilities transform-tools
+ ["permission:write_transforms"]))))))
+ (testing "Not available with missing features"
+ (with-redefs [premium-features/has-feature? (fn [feat]
+ (if (#'profiles/relevant-features feat)
+ false
+ (orig-has-feature feat)))]
+ (is (= #{}
+ (set (#'profiles/filter-by-capabilities transform-tools
+ ["permission:write_transforms"]))))))))
diff --git a/test/metabase/metabot/tools/transforms/write_test.clj b/test/metabase/metabot/tools/transforms/write_test.clj
index 965a5706efd..985977abf66 100644
--- a/test/metabase/metabot/tools/transforms/write_test.clj
+++ b/test/metabase/metabot/tools/transforms/write_test.clj
@@ -1,7 +1,9 @@
(ns metabase.metabot.tools.transforms.write-test
(:require
[clojure.test :refer :all]
- [metabase.metabot.tools.transforms.write :as transforms-write]))
+ [metabase.lib.core :as lib]
+ [metabase.metabot.tools.transforms.write :as transforms-write]
+ [metabase.test :as mt]))
;;; Edit Application Tests
@@ -9,17 +11,20 @@
(testing "replace mode replaces entire content"
(let [memory-atom (atom {:state {}})
result (transforms-write/write-transform-sql
- {:edit_action {:mode "replace"
+ {:database_id (mt/id)
+ :edit_action {:mode "replace"
:new_content "SELECT * FROM users"}
:transform_name "Test Transform"
:memory-atom memory-atom})]
(is (= "SELECT * FROM users"
- (get-in result [:structured-output :transform :source :query])))))
+ (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query)))))
(testing "edit mode with single edit"
- (let [existing-transform {:id 1
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1
:name "Existing"
- :source {:query "SELECT id FROM orders"}}
+ :source {:query (lib/native-query mp "SELECT id FROM orders")}}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})
result (transforms-write/write-transform-sql
{:transform_id 1
@@ -28,12 +33,14 @@
:new_string "customers"}]}
:memory-atom memory-atom})]
(is (= "SELECT id FROM customers"
- (get-in result [:structured-output :transform :source :query])))))
+ (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query)))))
(testing "edit mode with multiple edits"
- (let [existing-transform {:id 1
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1
:name "Existing"
- :source {:query "SELECT col_a, col_b FROM table1"}}
+ :source {:query (lib/native-query mp "SELECT col_a, col_b FROM table1")}}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})
result (transforms-write/write-transform-sql
{:transform_id 1
@@ -43,12 +50,14 @@
{:old_string "table1" :new_string "table2"}]}
:memory-atom memory-atom})]
(is (= "SELECT col_x, col_y FROM table2"
- (get-in result [:structured-output :transform :source :query])))))
+ (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query)))))
(testing "edit mode fails when text not found"
- (let [existing-transform {:id 1
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1
:name "Existing"
- :source {:query "SELECT * FROM users"}}
+ :source (lib/native-query mp "SELECT * FROM users")}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
@@ -61,9 +70,10 @@
:memory-atom memory-atom})))))
(testing "edit mode fails for ambiguous matches without replace_all"
- (let [existing-transform {:id 1
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1
:name "Existing"
- :source {:query "SELECT foo, foo FROM mytable"}}
+ :source {:query (lib/native-query mp "SELECT foo, foo FROM mytable")}}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
@@ -76,9 +86,10 @@
:memory-atom memory-atom})))))
(testing "edit mode with replace_all replaces all occurrences"
- (let [existing-transform {:id 1
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1
:name "Existing"
- :source {:query "SELECT xyz, xyz, xyz FROM mytable"}}
+ :source {:query (lib/native-query mp "SELECT xyz, xyz, xyz FROM mytable")}}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})
result (transforms-write/write-transform-sql
{:transform_id 1
@@ -88,7 +99,8 @@
:replace_all true}]}
:memory-atom memory-atom})]
(is (= "SELECT abc, abc, abc FROM mytable"
- (get-in result [:structured-output :transform :source :query]))))))
+ (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query))))))
;;; Transform Creation Tests
@@ -100,10 +112,12 @@
:new_content "SELECT 1"}
:transform_name "New Transform"
:transform_description "A test transform"
+ :database_id (mt/id)
:memory-atom memory-atom})]
(is (= "New Transform" (get-in result [:structured-output :transform :name])))
(is (= "A test transform" (get-in result [:structured-output :transform :description])))
- (is (= "SELECT 1" (get-in result [:structured-output :transform :source :query]))))))
+ (is (= "SELECT 1" (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query))))))
;;; Data Parts Tests
@@ -112,6 +126,7 @@
(let [memory-atom (atom {:state {}})
result (transforms-write/write-transform-sql
{:edit_action {:mode "replace" :new_content "SELECT 1"}
+ :database_id (mt/id)
:transform_name "Test"
:memory-atom memory-atom})]
(is (contains? result :data-parts))
@@ -125,18 +140,21 @@
(deftest memory-storage-test
(testing "stores updated transform in memory when transform_id provided"
- (let [existing-transform {:id 1 :name "Existing" :source {:query "SELECT 1"}}
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1 :name "Existing" :source {:query (lib/native-query mp "SELECT 1")}}
memory-atom (atom {:state {:transforms {"1" existing-transform}}})
_ (transforms-write/write-transform-sql
{:transform_id 1
:edit_action {:mode "replace" :new_content "SELECT 2"}
:memory-atom memory-atom})]
- (is (= "SELECT 2" (get-in @memory-atom [:state :transforms "1" :source :query])))))
+ (is (= "SELECT 2" (some-> (get-in @memory-atom [:state :transforms "1" :source :query])
+ lib/raw-native-query)))))
(testing "does not store in memory when no transform_id"
(let [memory-atom (atom {:state {:transforms {}}})
_ (transforms-write/write-transform-sql
{:edit_action {:mode "replace" :new_content "SELECT 1"}
+ :database_id (mt/id)
:transform_name "New"
:memory-atom memory-atom})]
(is (empty? (get-in @memory-atom [:state :transforms]))))))
@@ -155,10 +173,13 @@
:memory-atom memory-atom})))))
(testing "fails when edit_action invalid"
- (let [memory-atom (atom {:state {}})]
+ (let [mp (mt/metadata-provider)
+ existing-transform {:id 1 :name "Existing" :source {:query (lib/native-query mp "select 1")}}
+ memory-atom (atom {:state {:transforms {"1" existing-transform}}})]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Invalid edit_action"
(transforms-write/write-transform-sql
- {:edit_action {:mode "invalid"}
+ {:transform_id 1
+ :edit_action {:mode "invalid"}
:memory-atom memory-atom}))))))
diff --git a/test/metabase/metabot/tools/transforms_test.clj b/test/metabase/metabot/tools/transforms_test.clj
index 6c35fdab941..5088d21441c 100644
--- a/test/metabase/metabot/tools/transforms_test.clj
+++ b/test/metabase/metabot/tools/transforms_test.clj
@@ -4,10 +4,71 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.lib.core :as lib]
[metabase.metabot.tools.dependencies :as deps]
[metabase.metabot.tools.shared :as shared]
[metabase.metabot.tools.transforms :as agent-transforms]
- [metabase.metabot.tools.transforms.write :as transforms-write]))
+ [metabase.metabot.tools.transforms.write :as transforms-write]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.test :as mt]))
+
+;;; ----------------------------------- write tool integration tests --------------------------------------------------
+
+(deftest write-transform-sql-tool-test
+ (testing "creates new SQL transform with correct name, SQL content, and output message"
+ (let [memory-atom (atom {:state {}})
+ result (binding [shared/*memory-atom* memory-atom]
+ (agent-transforms/write-transform-sql-tool
+ {:edit_action {:mode "replace" :new_content "SELECT id FROM orders"}
+ :transform_name "Orders Transform"
+ :database_id (mt/id)}))]
+ (is (= "SELECT id FROM orders"
+ (some-> (get-in result [:structured-output :transform :source :query])
+ lib/raw-native-query)))
+ (is (= "Transform SQL updated successfully." (:output result)))
+ (is (= "transform_suggestion" (-> result :data-parts first :data-type))))))
+
+(deftest write-transform-python-tool-test
+ (when (premium-features/has-feature? :transforms-python)
+ (testing "creates new Python transform with correct body, source tables, and output message"
+ (let [memory-atom (atom {:state {}})
+ result (binding [shared/*memory-atom* memory-atom]
+ (agent-transforms/write-transform-python-tool
+ {:edit_action {:mode "replace" :new_content "import common\ndef transform(t): return t"}
+ :transform_name "Python Transform"
+ :database_id (mt/id)
+ :source_tables [{:alias "t" :table_id 10 :schema "PUBLIC" :database_id (mt/id)}]}))]
+ (is (= "import common\ndef transform(t): return t"
+ (get-in result [:structured-output :transform :source :body])))
+ (is (= [{:alias "t" :table_id 10 :schema "PUBLIC" :database_id (mt/id)}]
+ (get-in result [:structured-output :transform :source :source-tables])))
+ (is (= "transform_suggestion" (-> result :data-parts first :data-type)))))))
+
+(deftest write-transform-tool-nil-transform-id-test
+ (when (premium-features/has-feature? :transforms-python)
+ (testing "SQL: nil transform_id creates fresh transform with nil :id and does not store in memory"
+ (let [memory-atom (atom {:state {:transforms {}}})
+ result (binding [shared/*memory-atom* memory-atom]
+ (agent-transforms/write-transform-sql-tool
+ {:transform_id nil
+ :edit_action {:mode "replace" :new_content "SELECT 1"}
+ :transform_name "Fresh SQL"
+ :database_id (mt/id)}))]
+ (is (nil? (get-in result [:structured-output :transform :id])))
+ (is (empty? (get-in @memory-atom [:state :transforms])))
+ (is (= "transform_suggestion" (-> result :data-parts first :data-type)))))
+ (testing "Python: nil transform_id creates fresh transform with nil :id and does not store in memory"
+ (let [memory-atom (atom {:state {:transforms {}}})
+ result (binding [shared/*memory-atom* memory-atom]
+ (agent-transforms/write-transform-python-tool
+ {:transform_id nil
+ :edit_action {:mode "replace" :new_content "import common\ndef transform(): pass"}
+ :transform_name "Fresh Python"
+ :database_id (mt/id)
+ :source_tables [{:alias "t" :table_id 1 :schema "PUBLIC" :database_id (mt/id)}]}))]
+ (is (nil? (get-in result [:structured-output :transform :id])))
+ (is (empty? (get-in @memory-atom [:state :transforms])))
+ (is (= "transform_suggestion" (-> result :data-parts first :data-type)))))))
;;; ----------------------------------- dependency check integration tests -------------------------------------------