Skip to content

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

Never send the model an empty user message from a Slack thread (#82443)

Chris Truter968b105665af
diff --git a/src/metabase/slackbot/streaming.clj b/src/metabase/slackbot/streaming.clj
index c74dca93eb2..baaac0519ba 100644
--- a/src/metabase/slackbot/streaming.clj
+++ b/src/metabase/slackbot/streaming.clj
@@ -66,6 +66,17 @@
(or (= (:text msg) thinking-placeholder)
(str/blank? (:text msg)))))
+(defn- user-msg-content
+ "What a user message contributes to the chat history: its text without the bot mention.
+ A message with no other text names its attachments instead, or contributes nothing."
+ [{:keys [text files]} bot-user-id]
+ ;; The model rejects an empty user message, and a file shared on its own arrives with empty text.
+ (let [content (slackbot.events/strip-bot-mention text bot-user-id)]
+ (if-not (str/blank? content)
+ content
+ (when-let [names (seq (keep :name files))]
+ (str "Attached files: " (str/join ", " names))))))
+
(defn- thread->bot-msg-ids
"Slack message ids produced by our bot."
[thread]
@@ -94,7 +105,8 @@
;; bot messages: merge on tool call info from db
(conj (get msg-history ts []) {:role :assistant :content text})
;; user messages: user slack history instead to respect user edits
- [{:role :user :content (slackbot.events/strip-bot-mention text bot-user-id)}])))
+ (when-let [content (user-msg-content msg bot-user-id)]
+ [{:role :user :content content}]))))
vec)))
(defn- compute-capabilities
diff --git a/test/metabase/slackbot/streaming_test.clj b/test/metabase/slackbot/streaming_test.clj
index f129f6a5eca..a2045ed17be 100644
--- a/test/metabase/slackbot/streaming_test.clj
+++ b/test/metabase/slackbot/streaming_test.clj
@@ -82,6 +82,22 @@
result (#'slackbot.streaming/thread->history thread "UBOT123" "conv-123")]
(is (= [{:role :assistant :content "real"}] result))))))
+(deftest thread->history-blank-user-messages-test
+ (testing "User messages with no text beyond the bot mention name their attachments, or are excluded"
+ (mt/with-dynamic-fn-redefs [slackbot.persistence/message-history (constantly {})]
+ (let [thread {:messages [{:ts "1709567890.000001" :text "" :user "U123"
+ :files [{:name "data.csv"} {:name "more.tsv"}]}
+ {:ts "1709567890.000002" :text "<@UBOT123>" :user "U123"}
+ {:ts "1709567890.000003" :text "<@UBOT123> " :user "U123"
+ :files [{:name "notes.csv"}]}
+ {:ts "1709567890.000004" :text "" :user "U123" :files [{:id "F1"}]}
+ {:ts "1709567890.000005" :text "chart it" :user "U123"}]}
+ result (#'slackbot.streaming/thread->history thread "UBOT123" "conv-123")]
+ (is (= [{:role :user :content "Attached files: data.csv, more.tsv"}
+ {:role :user :content "Attached files: notes.csv"}
+ {:role :user :content "chart it"}]
+ result))))))
+
(deftest thread->history-excludes-soft-deleted-bot-messages-test
(testing "thread->history excludes bot messages that have been soft-deleted"
(mt/with-dynamic-fn-redefs [slackbot.persistence/message-history (constantly {})