Skip to content

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

[METABOT] Do not apply update-keys to ai-service request state (#62137)

Tyler Plude5c7bc9630e35
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/client.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/client.clj
index 6f5e2194aef..1d2fa41f967 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/client.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/client.clj
@@ -13,7 +13,6 @@
[metabase-enterprise.metabot-v3.config :as metabot-v3.config]
[metabase-enterprise.metabot-v3.context :as metabot-v3.context]
[metabase-enterprise.metabot-v3.settings :as metabot-v3.settings]
- [metabase-enterprise.metabot-v3.util :as metabot-v3.u]
[metabase.api.common :as api]
[metabase.premium-features.core :as premium-features]
[metabase.server.streaming-response :as sr]
@@ -130,9 +129,9 @@
:context context
:conversation_id conversation-id
:profile_id profile-id
- :state state
- :user_id api/*current-user-id*}
- (metabot-v3.u/recursive-update-keys metabot-v3.u/safe->snake_case_en))
+ :user_id api/*current-user-id*
+ :state state}
+ (u/deep-kebab->snake-keys))
_ (metabot-v3.context/log body :llm.log/be->llm)
_ (log/debugf "V2 request to AI Service:\n%s" (u/pprint-to-str body))
options (cond-> {:headers {"Accept" "application/json"
@@ -188,9 +187,9 @@
:context context
:conversation_id conversation-id
:profile_id profile-id
- :state state
- :user_id api/*current-user-id*}
- (metabot-v3.u/recursive-update-keys metabot-v3.u/safe->snake_case_en))
+ :user_id api/*current-user-id*
+ :state state}
+ (u/deep-kebab->snake-keys))
_ (metabot-v3.context/log body :llm.log/be->llm)
_ (log/debugf "V2 request to AI Proxy:\n%s" (u/pprint-to-str body))
options (cond-> {:headers {"Accept" "text/event-stream"
@@ -207,11 +206,11 @@
(log/debugf "Response from AI Proxy:\n%s" (u/pprint-to-str (select-keys response #{:body :status :headers})))
(if (= (:status response) 200)
(sr/streaming-response {:content-type "text/event-stream; charset=utf-8"} [os canceled-chan]
- ;; Response from the AI Service will send response parts separated by newline
+ ;; Response from the AI Service will send response parts separated by newline
(with-open [response-lines ^BufferedReader (io/reader (:body response))]
(loop []
- ;; Grab the next line and write it to the output stream with appended newline (frontend depends on it)
- ;; Immediately flush so it get's sent to the frontend as soon as possible
+ ;; Grab the next line and write it to the output stream with appended newline (frontend depends on it)
+ ;; Immediately flush so it get's sent to the frontend as soon as possible
(when-let [line (.readLine response-lines)]
(.write os (.getBytes (str line "\n") "UTF-8"))
(.flush os)
diff --git a/src/metabase/util.cljc b/src/metabase/util.cljc
index 2a65454e05c..0f56d94133b 100644
--- a/src/metabase/util.cljc
+++ b/src/metabase/util.cljc
@@ -283,16 +283,37 @@
(str (upper-case-en (subs s 0 1))
(subs s 1))))
+(defn kebab->snake
+ "Simple conversion from kebab-case to snake_case by replacing hyphens with underscores.
+ Does not detect or convert camelCase, preserving mixed-case identifiers."
+ [x]
+ (cond
+ (keyword? x) (keyword (namespace x) (str/replace (name x) #"-" "_"))
+ (string? x) (str/replace x #"-" "_")
+ :else x))
+
(defn snake-keys
"Convert the top-level keys in a map to `snake_case`."
[m]
(perf/update-keys m ->snake_case_en))
+(defn kebab->snake-keys
+ "Convert the top-level kebab-case keys in a map to snake_case by replacing hyphens.
+ Preserves camelCase and other formatting."
+ [m]
+ (perf/update-keys m kebab->snake))
+
(defn deep-snake-keys
"Recursively convert the keys in a map to `snake_case`."
[m]
(recursive-map-keys ->snake_case_en m))
+(defn deep-kebab->snake-keys
+ "Recursively convert kebab-case keys in a map to snake_case by replacing hyphens.
+ Preserves camelCase and other formatting."
+ [m]
+ (recursive-map-keys kebab->snake m))
+
(defn normalize-map
"Given any map-like object, return it as a Clojure map with :kebab-case keyword keys.
The input map can be a:
diff --git a/test/metabase/util_test.cljc b/test/metabase/util_test.cljc
index 025da26cdf4..c19415ad229 100644
--- a/test/metabase/util_test.cljc
+++ b/test/metabase/util_test.cljc
@@ -234,6 +234,72 @@
(is (= {:num_cans 2, :lisp_case? {:nested_maps? true}}
(u/deep-snake-keys {:num-cans 2, :lisp-case? {:nested-maps? true}}))))
+(deftest ^:parallel kebab->snake-test
+ (testing "kebab->snake converts kebab-case to snake_case"
+ (are [input expected] (= expected (u/kebab->snake input))
+ ;; Keywords
+ :kebab-case :kebab_case
+ :multi-word-example :multi_word_example
+ :already_snake :already_snake
+
+ ;; Namespaced keywords
+ :namespace/kebab-key :namespace/kebab_key
+
+ ;; Strings
+ "kebab-case" "kebab_case"
+ "multi-word-example" "multi_word_example"
+
+ ;; Preserves camelCase and mixed case
+ :camelCase :camelCase
+ :PascalCase :PascalCase
+ :mixedCase-with-kebab :mixedCase_with_kebab
+
+ ;; Edge cases
+ :simple :simple
+ "" ""
+ nil nil
+ 123 123)))
+
+(deftest ^:parallel kebab->snake-keys-test
+ (testing "kebab->snake-keys converts top-level keys only"
+ (is (= {:user_id 1
+ :profile_id "abc"
+ :nested {:still-kebab-case true}}
+ (u/kebab->snake-keys {:user-id 1
+ :profile-id "abc"
+ :nested {:still-kebab-case true}})))
+
+ (testing "preserves camelCase keys"
+ (is (= {:userId 1
+ :profileId "abc"
+ :conversation_id "def"}
+ (u/kebab->snake-keys {:userId 1
+ :profileId "abc"
+ :conversation-id "def"}))))))
+
+(deftest ^:parallel deep-kebab->snake-keys-test
+ (testing "deep-kebab->snake-keys recursively converts all keys"
+ (is (= {:user_id 1
+ :profile_id "abc"
+ :nested {:inner_key {:deeply_nested true}}}
+ (u/deep-kebab->snake-keys {:user-id 1
+ :profile-id "abc"
+ :nested {:inner-key {:deeply-nested true}}}))))
+
+ (testing "preserves camelCase throughout the structure"
+ (is (= {:userId 1
+ :nested {:innerKey {:deeplyNested true
+ :kebab_converted "yes"}}}
+ (u/deep-kebab->snake-keys {:userId 1
+ :nested {:innerKey {:deeplyNested true
+ :kebab-converted "yes"}}}))))
+
+ (testing "works with vectors and other collections"
+ (is (= [{:user_id 1} {:user_id 2}]
+ (u/deep-kebab->snake-keys [{:user-id 1} {:user-id 2}])))
+ (is (= {:items [{:item_id 1 :item_name "test"}]}
+ (u/deep-kebab->snake-keys {:items [{:item-id 1 :item-name "test"}]})))))
+
(deftest ^:parallel one-or-many-test
(are [input expected] (= expected
(u/one-or-many input))