Skip to content

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

BOT-2111: Stop the Gemini stream when the client cancels (#81959)

Paolo Rascunà3ba6cb14796b
diff --git a/src/metabase/metabot/self/google/stream_generate_content.clj b/src/metabase/metabot/self/google/stream_generate_content.clj
index 5d615dacea5..d8e73b4e243 100644
--- a/src/metabase/metabot/self/google/stream_generate_content.clj
+++ b/src/metabase/metabot/self/google/stream_generate_content.clj
@@ -337,7 +337,7 @@
(not @message-id) (-> (u/prog1
(vreset! message-id (or responseId (core/mkid))))
(rf {:type :start :messageId @message-id}))
- (seq (:parts content)) (as-> res (reduce emit-part res (:parts content)))
+ (seq (:parts content)) (as-> res (u/reduce-preserving-reduced emit-part res (:parts content)))
(some? finishReason) (finish! finishReason)
;; A blocked prompt ends the stream with no candidates, only promptFeedback.
(some? block-reason) (-> (close-text!)
diff --git a/test/metabase/metabot/self/google/stream_generate_content_test.clj b/test/metabase/metabot/self/google/stream_generate_content_test.clj
index 62c5e59edb0..dc6de9aae83 100644
--- a/test/metabase/metabot/self/google/stream_generate_content_test.clj
+++ b/test/metabase/metabot/self/google/stream_generate_content_test.clj
@@ -700,3 +700,23 @@
{:type :text :text "partial answer"}
{:type :usage :usage {:promptTokens 4 :completionTokens 0}}]
(into [] (comp (sgc/->aisdk-chunks-xf) (self.core/aisdk-xf)) events))))))
+
+(deftest ^:parallel cancelled-consumer-is-not-fed-further-events-test
+ (testing "once the consumer stops early (reduced), nothing from later events reaches it"
+ ;; the agent loop signals a client disconnect or cancellation by returning `reduced` from the
+ ;; reducing fn; a plain `reduce` over an event's parts would strip that wrapper and keep feeding
+ ;; every event that follows
+ (let [event {:responseId "r15"
+ :candidates [{:content {:role "model" :parts [{:text "a"} {:text "b"}]}}]}
+ seen (atom [])
+ cancelled? (atom false)
+ rf (fn
+ ([] [])
+ ([acc] acc)
+ ([acc {:keys [type delta]}]
+ (swap! seen conj (or delta type))
+ (when (= :text-delta type)
+ (reset! cancelled? true))
+ (if @cancelled? (reduced acc) acc)))]
+ (transduce (sgc/->aisdk-chunks-xf) rf [event event event])
+ (is (= [:start :text-start "a" :text-end] @seen)))))