Skip to content

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

Keep text that ends mid-link ahead of the tool calls after it (#83786)

Andrei Simionescu6d43ebbe8a2a
diff --git a/src/metabase/metabot/agent/markdown_link_buffer.clj b/src/metabase/metabot/agent/markdown_link_buffer.clj
index e52022ae48b..a80519d3901 100644
--- a/src/metabase/metabot/agent/markdown_link_buffer.clj
+++ b/src/metabase/metabot/agent/markdown_link_buffer.clj
@@ -118,7 +118,7 @@
1. Accumulates queries/charts from tool-output parts (via :structured-output)
2. Buffers markdown links to handle links split across text chunks
3. Resolves metabase:// URLs to proper Metabase paths
- 4. Flushes any remaining buffered content at stream end
+ 4. Flushes buffered content where the text ends: before the next part other than text or usage, and at stream end
Parameters:
- initial-queries: Initial map of query-id to query data
@@ -131,15 +131,18 @@
(or initial-charts {})
link-registry-atom))
queries (volatile! (or initial-queries {}))
- charts (volatile! (or initial-charts {}))]
+ charts (volatile! (or initial-charts {}))
+ text-id (volatile! nil)
+ flush! (fn [result]
+ (let [flushed (flush-state @state)]
+ (if (seq flushed)
+ (do (vswap! state assoc :buffer "")
+ (rf result {:type :text :id @text-id :text flushed}))
+ result)))]
(fn
([] (rf))
([result]
- ;; Flush any remaining buffered content
- (let [flushed (flush-state @state)]
- (if (seq flushed)
- (rf (rf result {:type :text :text flushed}))
- (rf result))))
+ (rf (unreduced (flush! result))))
([result part]
;; Accumulate state from tool outputs
(when (= (:type part) :tool-output)
@@ -153,10 +156,18 @@
;; Update state with new context
(vswap! state with-context @queries @charts))
;; Process text parts through link buffer
- (if (= (:type part) :text)
+ (case (:type part)
+ :text
(let [[new-state processed-text] (step @state (:text part))]
(vreset! state new-state)
+ (vreset! text-id (:id part))
(if (seq processed-text)
(rf result (assoc part :text processed-text))
result))
- (rf result part)))))))
+ ;; Some servers send usage with every chunk, so a usage part doesn't end the text
+ :usage
+ (rf result part)
+ (let [result (flush! result)]
+ (if (reduced? result)
+ result
+ (rf result part)))))))))
diff --git a/test/metabase/metabot/agent/markdown_link_buffer_test.clj b/test/metabase/metabot/agent/markdown_link_buffer_test.clj
index 5fdad206e6c..2cb309e6479 100644
--- a/test/metabase/metabot/agent/markdown_link_buffer_test.clj
+++ b/test/metabase/metabot/agent/markdown_link_buffer_test.clj
@@ -288,6 +288,24 @@
(is (= 1 (count @registry)))
(is (= "metabase://query/q1" (first (vals @registry)))))))
+(deftest ^:parallel resolve-xf-flushes-held-text-where-the-text-ends-test
+ (let [resolve-parts #(into [] (mlb/resolve-xf {} {} (atom {})) %)]
+ (testing "text held for a possible link comes out before the tool call that follows it"
+ (is (=? [{:type :text :id "t1" :text "Let me check the "}
+ {:type :text :id "t1" :text "[Orders"}
+ {:type :tool-input :id "call-1"}
+ {:type :tool-output :id "call-1"}]
+ (resolve-parts [{:type :text :id "t1" :text "Let me check the [Orders"}
+ {:type :tool-input :id "call-1" :function "search" :arguments {}}
+ {:type :tool-output :id "call-1" :result {:output "Found ORDERS"}}]))))
+ (testing "a usage part between two chunks of a link doesn't split it"
+ (is (=? [{:type :text :text "See "}
+ {:type :usage}
+ {:type :text :text "[My Link](http://example.com)"}]
+ (resolve-parts [{:type :text :id "t1" :text "See [My "}
+ {:type :usage :usage {:promptTokens 1}}
+ {:type :text :id "t1" :text "Link](http://example.com)"}]))))))
+
(deftest ^:parallel with-context-test
(testing "updates state for subsequent link resolution"
(let [registry (atom {})